Skip to content

New test for loading a pointer as part of a read #1706

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

Closed
oli-obk opened this issue Feb 12, 2021 · 6 comments · Fixed by #1712
Closed

New test for loading a pointer as part of a read #1706

oli-obk opened this issue Feb 12, 2021 · 6 comments · Fixed by #1712
Labels
A-intptrcast Area: affects int2ptr and ptr2int casts

Comments

@oli-obk
Copy link
Contributor

oli-obk commented Feb 12, 2021

@eddyb would like

use std::mem::transmute;
fn main() {
    for &my_bool in &[true, false] {
        unsafe {
            println!("{}", transmute::<_, &str>(!-(my_bool as i128) & transmute::<_, i128>("false !") | -(my_bool as i128) & transmute::<_, i128>("true !")));
        }
    }
}

to be in the miri test suite.

The diagnostic is also wrong, it's actually that we want to put two pointers into a single i128 immediate value.

@RalfJung
Copy link
Member

Yeah sure, we can add that.

For the error message, I guess it's the other way around here -- encountered pointer in part of a read. ;) Not sure how to best word it though.

@RalfJung
Copy link
Member

But also, I wouldn't mind an explanation of what the point of this is.^^ !-(my_bool as i128) & looks like gibberish, what is going on there...?

@RalfJung
Copy link
Member

Most of the code in this test is actually irrelevant; transmute::<_, i128>("false !") already triggers the error. So I am not sure what this is supposed to test, but the !-(my_bool as i128) & part mostly obfuscates what is going on.

@RalfJung RalfJung added the A-intptrcast Area: affects int2ptr and ptr2int casts label Feb 13, 2021
@RalfJung RalfJung changed the title New test New test for loading a pointer as part of a read Feb 13, 2021
@eddyb
Copy link
Member

eddyb commented Feb 13, 2021

Really sorry about the confusion!

(~-cond & a) | (-cond & b) is "standard" (bitwise) branchless if-else/"select". but:

  • ~ is ! in Rust
  • Rust also needs explicit casts from bool

The weird -cond that looks like obfuscation is just sign-extending a boolean to get either all zeros or all ones.

It makes a bit more sense in context, where [a, b][cond as usize] was @m-ou-se's sugestion.

@eddyb
Copy link
Member

eddyb commented Feb 13, 2021

This version might look less obfuscated, and is probably a better fit for a test (originally it was just a Discord one-liner):

use std::mem::transmute;
fn main() {
    for &my_bool in &[true, false] {
        let mask = -(my_bool as i128); // false -> 0, true -> -1 aka !0
        unsafe {
            println!(
                "{}",
                transmute::<_, &str>(
                    !mask & transmute::<_, i128>("false !") | mask & transmute::<_, i128>("true !")
                )
            );
        }
    }
}

@RalfJung
Copy link
Member

I see. However, given that the transmute already fails, I am not sure what the point of testing the rest is.^^

That said, I hope that Miri will support that kind of transmute one day (once #841 is done, which I am still occasionally working on). So maybe it's good to add the test now so we are reminded of it then.^^

@RalfJung RalfJung mentioned this issue Feb 16, 2021
bors added a commit that referenced this issue Feb 16, 2021
Rustup

Fixes #1706
Cc #1711 for the temporarily disabled tests
@bors bors closed this as completed in 776644c Feb 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intptrcast Area: affects int2ptr and ptr2int casts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants