diff options
| author | bors <bors@rust-lang.org> | 2022-06-06 01:03:26 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-06-06 01:03:26 +0000 |
| commit | 760237ff785fd14ac7fdab799f4d695d86cf9cbf (patch) | |
| tree | ee42fe59a5f36eb959f09b680909cba20bbf7227 /compiler/rustc_codegen_gcc/src | |
| parent | fee3a459dd6aba8e34a5b99f0fbcb4218a1e2401 (diff) | |
| parent | 4291332175d12e79e6061cdc3f5dccac2e28b969 (diff) | |
| download | rust-760237ff785fd14ac7fdab799f4d695d86cf9cbf.tar.gz rust-760237ff785fd14ac7fdab799f4d695d86cf9cbf.zip | |
Auto merge of #97710 - RalfJung:ptr-addr, r=thomcc
implement ptr.addr() via transmute
As per the discussion in https://github.com/rust-lang/unsafe-code-guidelines/issues/286, the semantics for ptr-to-int transmutes that we are going with for now is to make them strip provenance without exposing it. That's exactly what `ptr.addr()` does! So we can implement `ptr.addr()` via `transmute`. This also means that once https://github.com/rust-lang/rust/pull/97684 lands, Miri can distinguish `ptr.addr()` from `ptr.expose_addr()`, and the following code will correctly be called out as having UB (if permissive provenance mode is enabled, which will become the default once the [implementation is complete](https://github.com/rust-lang/miri/issues/2133)):
```rust
fn main() {
let x: i32 = 3;
let x_ptr = &x as *const i32;
let x_usize: usize = x_ptr.addr();
// Cast back an address that did *not* get exposed.
let ptr = std::ptr::from_exposed_addr::<i32>(x_usize);
assert_eq!(unsafe { *ptr }, 3); //~ ERROR Undefined Behavior: dereferencing pointer failed
}
```
This completes the Miri implementation of the new distinctions introduced by strict provenance. :)
Cc `@Gankra` -- for now I left in your `FIXME(strict_provenance_magic)` saying these should be intrinsics, but I do not necessarily agree that they should be. Or if we have an intrinsic, I think it should behave exactly like the `transmute` does, which makes one wonder why the intrinsic should be needed.
Diffstat (limited to 'compiler/rustc_codegen_gcc/src')
0 files changed, 0 insertions, 0 deletions
