Skip to content

Avoiding naming clashes for result in function contracts #2597

Closed

Description

@JustusAdam

Function contracts provide the result of the call as a variable called result which can cause name clashes if an input is named the same.

We should handle this more gracefully and allow the user to rename the binding to something else, avoiding the clash.

Activity

  1. self-assigned this
    on Jul 10, 2023
  2. added
    [C] Feature / Enhancement
    on Sep 22, 2023
  3. self-assigned this
    on Feb 28, 2024
  4. pi314mm commented on May 28, 2024

    @pi314mm
    Contributor

    There are also more complicated naming clashes in play.

    For checking
    fn_name(arg1 : u32, arg2 : u32)
    we produce something like

    #[allow(dead_code, unused_variables)]
    #[kanitool :: is_contract_generated(check)]
    fn fn_name_check_HASH(arg1 : u32, arg2 : u32) -> u32
    {
        let arg1_renamed = kani :: internal :: untracked_deref(& arg1);
        let arg2_renamed = kani :: internal :: untracked_deref(& arg2);
        let result = [function call with arg1 and arg2];
        std :: mem :: forget(arg1);
        std :: mem :: forget(arg2);
        result
    }
    

    If the user decided to name two of their input variables as "a" and "a_renamed" such as in this test code:

    #[kani::requires(a_renamed == 5 && a == 4)]
    #[kani::ensures(a == 4 && a_renamed == 5)]
    fn simple_addition(a_renamed: u32, a: u32) -> u32 {
        return a.wrapping_add(a_renamed);
    }
    

    It produces a conflict in shadowing variable names as the following:

    #[allow(dead_code, unused_variables)]
    #[kanitool :: is_contract_generated(check)]
    fn simple_addition_check_222d75(a_renamed : u32, a : u32) -> u32
    {
        let a_renamed = kani :: internal :: untracked_deref(& a);
        let a_renamed_renamed = kani :: internal :: untracked_deref(& a_renamed);
        kani  :: assume(a_renamed == 5 && a == 4);
        let result : u32 =  simple_addition_wrapper_222d75(a_renamed, a);
        kani :: assert(a_renamed == 4 && a_renamed_renamed == 5, stringify! (a == 4 && a_renamed == 5));
        std :: mem :: forget(a_renamed);
        std :: mem :: forget(a_renamed_renamed);
        result
    }
    

    Specifically naming your variables "a" and "a_renamed" seems to be unlikely to come up in practice, while naming a variable "result" is far more likely. Moreover, the specific order in which variables are binded is dependent on the order in which they are populated into a hashmap, so it is difficult to produce this error.

    The same can technically be done with function names via already knowing the hash that will be produced and intentionally creating a function with that same hash, but to do that would require quite a lot of hacking and won't come up without nefarious intent.

    This is related to #3026, where the internal representation of the variables in checks leaks out to the user. If we rename result internally to avoid collisions, there might also be error leaks with this internal result name

  5. JustusAdam commented on May 28, 2024

    @JustusAdam
    ContributorAuthor

    Hey @pi314mm, have you tested whether this name clash actually causes compile/semantic failures? The reason I ask is there are theoretically hygiene protections in macros which (I would hope) would mean that the arg1 and arg1_renamed cases should actually not conflict, but I could be wrong.

  6. added a commit that references this issue on Jun 5, 2024
    9f4fc30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[C] Feature / Enhancement

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions