Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass-by-value semantics not preserved when using __addr_of #5250

Closed
ironcev opened this issue Nov 1, 2023 · 0 comments · Fixed by #5253
Closed

Pass-by-value semantics not preserved when using __addr_of #5250

ironcev opened this issue Nov 1, 2023 · 0 comments · Fixed by #5253
Assignees
Labels
bug Something isn't working

Comments

@ironcev
Copy link
Member

ironcev commented Nov 1, 2023

Consider the following test that is passing on v0.46.0 and the Sway latest master branch:

script;

fn main() -> u64 {
   0
}

struct A {
  a: u64,
}

struct B {
  a: A,
  x: (u64, u64),
}

#[test]
fn ptr_issue() -> u64 {
    let a = A { a: 11 };
    let mut b = B { a: a, x: (11, 11) };

    let mut ptr_b = ptr(b);

    ptr_b.write(B { a: A { a: 22 }, x: (22, 22)});

    assert(b.a.a == 22);
    assert(b.x.0 == 22);
    assert(b.x.1 == 22);

    42
}

#[inline(never)]
fn ptr<T>(t: T) -> raw_ptr {
    __addr_of(t)
}

Because of the pass-by-value semantics ptr should get the copy of b and thus a different pointer should be returned from the function. (A pointer to the local value indeed, which is an issue on its own, but different then the one described here.)

Since ptr is actually getting the pointer to the original value and no copy is made writing to ptr_b overwrites b.

Notice that if we don't use _addr_of (or references one day) to the immutable parameters, the fact that the copy is not made is a valid optimization.

@ironcev ironcev changed the title Pass by-value semantics not preserved when using __addr_of Pass-by-value semantics not preserved when using __addr_of Nov 1, 2023
@vaivaswatha vaivaswatha self-assigned this Nov 2, 2023
@vaivaswatha vaivaswatha added the bug Something isn't working label Nov 2, 2023
vaivaswatha added a commit that referenced this issue Nov 2, 2023
This fixes a bug in `local_copy_prop_prememcpy` which didn't
make an exception when a local gets passed its address into
another function, thus making it unsafe to optimize it.

Closes #5250
Related to #4600
vaivaswatha added a commit that referenced this issue Nov 2, 2023
This fixes a bug in `local_copy_prop_prememcpy` which didn't make an
exception when a local gets passed its address into another function,
thus making it unsafe to optimize it.

Closes #5250
Related to #4600
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants