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

NormalizeArrayLen MIR opt unsound due to missed writes #105929

Closed
JakobDegen opened this issue Dec 20, 2022 · 1 comment · Fixed by #107172
Closed

NormalizeArrayLen MIR opt unsound due to missed writes #105929

JakobDegen opened this issue Dec 20, 2022 · 1 comment · Fixed by #107172
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@JakobDegen
Copy link
Contributor

The opt, as written today, will miscompile something that looks like this:

slice_ptr = Cast(array_ptr);
r = &mut slice_ptr;
*r = something_else;
Len(slice_ptr)

The bug is roughly on this line:

This makes an attempt to detect other writes to the slice_ptr local, but it misses a lot of cases. Taking a reference like in the example above is an easy one, but SetDiscriminant is also missed, as well as writes to any aliasing pointers.

As far as I can tell this needs Rvalue::Cast support in custom MIR to reproduce.

@rustbot label A-mir-opt T-compiler requires-nightly

@JakobDegen JakobDegen added the C-bug Category: This is a bug. label Dec 20, 2022
@rustbot rustbot added A-mir-opt Area: MIR optimizations requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 20, 2022
@shamatar
Copy link
Contributor

This optimization is more a workaround of unwillingness to add "len()" function to arrays, so method resolution would not need to use coercion to slice, but I'll check what can be done to fix it

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 20, 2022
@bors bors closed this as completed in db97749 Jan 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-mir-opt Area: MIR optimizations C-bug Category: This is a bug. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants