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

Miri errors when transmuting part of a pointer to an integer #2456

Closed
GoldsteinE opened this issue Aug 2, 2022 · 1 comment · Fixed by rust-lang/rust#101101 or #2516
Closed

Miri errors when transmuting part of a pointer to an integer #2456

GoldsteinE opened this issue Aug 2, 2022 · 1 comment · Fixed by rust-lang/rust#101101 or #2516
Labels
A-interpreter Area: affects the core interpreter C-bug Category: This is a bug.

Comments

@GoldsteinE
Copy link

GoldsteinE commented Aug 2, 2022

This program triggers Miri:

fn main() {
    let x: [u8; 8] = unsafe { std::mem::transmute(&0) };
    println!("{x:?}");
}

But these two don’t:

fn main() {
    let x: usize = unsafe { std::mem::transmute(&0) };
    println!("{x:?}");
}
fn main() {
    let x: [u8; 8] = unsafe { std::mem::transmute(std::mem::transmute::<_, usize>(&0)) };
    println!("{x:?}");
}

It looks like it should trigger in all of these snippets.

@RalfJung
Copy link
Member

RalfJung commented Aug 2, 2022

Thanks for the report!

The intention is that Miri should not trigger on any of these snippets, they should all be allowed.

@RalfJung RalfJung changed the title Incosistent behavior when transmuting pointers Miri errors when transmuting part of a pointer to an integer Aug 2, 2022
@RalfJung RalfJung added C-bug Category: This is a bug. A-interpreter Area: affects the core interpreter labels Aug 4, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 30, 2022
…oli-obk

interpret: make read-pointer-as-bytes a CTFE-only error with extra information

Next step in the reaction to rust-lang#99923. Also teaches Miri to implicitly strip provenance in more situations when transmuting pointers to integers, which fixes rust-lang/miri#2456.

Pointer-to-int transmutation during CTFE now produces a message like this:
```
   = help: this code performed an operation that depends on the underlying bytes representing a pointer
   = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
```

r? `@oli-obk`
bors added a commit that referenced this issue Aug 31, 2022
Adjust for supporting more implicit ptr-to-int transmutation

This is the Miri side of rust-lang/rust#101101.
Fixes #2456.
bors added a commit that referenced this issue Aug 31, 2022
Adjust for supporting more implicit ptr-to-int transmutation

This is the Miri side of rust-lang/rust#101101.
Fixes #2456.
bjorn3 pushed a commit to rust-lang/rustc_codegen_cranelift that referenced this issue Sep 4, 2022
interpret: make read-pointer-as-bytes a CTFE-only error with extra information

Next step in the reaction to rust-lang/rust#99923. Also teaches Miri to implicitly strip provenance in more situations when transmuting pointers to integers, which fixes rust-lang/miri#2456.

Pointer-to-int transmutation during CTFE now produces a message like this:
```
   = help: this code performed an operation that depends on the underlying bytes representing a pointer
   = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
```

r? ``@oli-obk``
antoyo pushed a commit to rust-lang/rustc_codegen_gcc that referenced this issue Jun 3, 2023
interpret: make read-pointer-as-bytes a CTFE-only error with extra information

Next step in the reaction to rust-lang/rust#99923. Also teaches Miri to implicitly strip provenance in more situations when transmuting pointers to integers, which fixes rust-lang/miri#2456.

Pointer-to-int transmutation during CTFE now produces a message like this:
```
   = help: this code performed an operation that depends on the underlying bytes representing a pointer
   = help: the absolute address of a pointer is not known at compile-time, so such operations are not supported
```

r? ``@oli-obk``
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-interpreter Area: affects the core interpreter C-bug Category: This is a bug.
Projects
None yet
2 participants