-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Mutating through addr_of produces LLVM IR with UB #111502
Comments
If you mark |
No, it doesn't change the behaviour (and there's an |
That is very strange, I was under the impression that we generate the same code for addr_of and addr_of_mut. Making |
|
AFAIK it was never intended to be considered non-mutating. In fact we still track this Miri behavior as a bug: #56604. |
Sure, I am just describing why generated LLVM IR has undefined behavior. |
@rust-lang/wg-mir-opt @rust-lang/opsem do you know any other places where we assume one cannot mutate through an |
While I want to be able to say "yes this is DB", doesn't this hit issues depending on how the pointer is obtained? If you do Why not just use the mutability the user asked for? This is literally what Note that in #56604 the issue was with two ways to obtain a pointer from a |
IMO it would be strange if I don't consider |
I agree with this, but perhaps draw the opposite conclusion from it. :)
Put another way, if you want to make it legal to mutate |
I'm quite sympathetic to Mario's point here, but this is still the wrong order to consider this in. We should turn off the sketchy optimization while we figure out if it's ok |
I find it funny that first there was a wave of complaints when Miri started making a difference between But anyway, that's a topic for rust-lang/unsafe-code-guidelines#257. |
allow mutating function args through `&raw const` Fixes rust-lang/rust#111502 by "turning off the sketchy optimization while we figure out if this is ok", like `@JakobDegen` said. The first commit in this PR removes some suspicious looking logic from the same method, but should have no functional changes, since it doesn't modify the `context` outside of the method. Best reviewed commit by commit. r? opsem
As there is a write through a
*const
derived pointer, this code has UB under Stacked Borrows, but is fine under Tree Borrows,-C opt-level=0
results intrue
(which Miri agrees and is the right result), but-C opt-level=3
miscompiles it to printfalse
Interestingly, if you remove
unused
in functionsecond
, the miscompilation goes away.-Zmir-opt-level=0
is required to prevent mir-opt from doing this.Directly getting a
*mut
throughaddr_of_mut!
in the first line ofsecond
also makes the bug go away.cc @nikic @RalfJung
The text was updated successfully, but these errors were encountered: