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

Pointer to integer casts that expose provenance are incorrectly removed #97421

Closed
tmiasko opened this issue May 26, 2022 · 1 comment · Fixed by #97597
Closed

Pointer to integer casts that expose provenance are incorrectly removed #97421

tmiasko opened this issue May 26, 2022 · 1 comment · Fixed by #97597
Labels
A-mir-opt Area: MIR optimizations A-strict-provenance Area: Strict provenance for raw pointers C-bug Category: This is a bug.

Comments

@tmiasko
Copy link
Contributor

tmiasko commented May 26, 2022

The pointer to integer cast has an implicit side effect of exposing the provenance, so it cannot be removed under strict provenance model.

Example of an incorrect transformation:

pub fn expose_addr<T>(a: &T) {
    a as *const T as usize;
}
-// MIR for `expose_addr` before SimplifyLocals
+// MIR for `expose_addr` after SimplifyLocals
 
 fn expose_addr(_1: &T) -> () {
     debug a => _1;                       // in scope 0 at a.rs:1:23: 1:24
     let mut _0: ();                      // return place in scope 0 at a.rs:1:30: 1:30
-    let _2: usize;                       // in scope 0 at a.rs:2:5: 2:27
-    let mut _3: *const T;                // in scope 0 at a.rs:2:5: 2:18
 
     bb0: {
-        _3 = &raw const (*_1);           // scope 0 at a.rs:2:5: 2:6
-        _2 = move _3 as usize (Misc);    // scope 0 at a.rs:2:5: 2:27
         return;                          // scope 0 at a.rs:3:2: 3:2
     }
 }
@tmiasko tmiasko added C-bug Category: This is a bug. A-mir-opt Area: MIR optimizations A-strict-provenance Area: Strict provenance for raw pointers labels May 26, 2022
@RalfJung
Copy link
Member

Thanks!
Indeed, this optimization can cause the miscompilation outlined in my blog post.

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Jun 7, 2022
…s, r=RalfJung,JakobDegen

Preserve unused pointer to address casts

Fixes rust-lang#97421.

cc `@RalfJung`
@bors bors closed this as completed in d380b45 Jun 8, 2022
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 A-strict-provenance Area: Strict provenance for raw pointers C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants