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

Set -Zrandomize-layout? #2656

Closed
RalfJung opened this issue Nov 7, 2022 · 5 comments
Closed

Set -Zrandomize-layout? #2656

RalfJung opened this issue Nov 7, 2022 · 5 comments

Comments

@RalfJung
Copy link
Member

RalfJung commented Nov 7, 2022

rustc now has a flag to randomize the layout of repr(Rust) types which can help detect code that makes incorrect layout assumptions.

@rust-lang/miri Is that something Miri should enable? Sounds like it, doesn't it?

@RalfJung
Copy link
Member Author

RalfJung commented Nov 7, 2022

Doing this causes a test failure in liballoc:

  0.005647   ---- collections::btree::node::tests::test_sizes stdout ----
  0.000079   thread 'main' panicked at 'assertion failed: `(left == right)`
  0.000008     left: `200`,
  0.000006    right: `192`', alloc_miri_test/../library/alloc/src/collections/btree/node/tests.rs:99:5
  0.000005   
  0.005277   
  0.000074   failures:
  0.021831       collections::btree::node::tests::test_sizes

@bjorn3
Copy link
Member

bjorn3 commented Nov 7, 2022

That is just a type size test to avoid size regressions. Should be fine to disable.

https://github.com/rust-lang/rust/blob/9b735a7132acd58b3bd34c084e9ca5b4ca7450a2/library/alloc/src/collections/btree/node/tests.rs#L99

In any case I don't think it should be enabled for the standard library as it probably breaks layout assumptions in the standard library.

@RalfJung
Copy link
Member Author

RalfJung commented Nov 7, 2022

The stdlib tests all pass except for that one. So seems fine to enable for the standard library? Might be a good way to discover these layout assumptions, and maybe add #[rustc_layout_assumption] attributes to opt-out certain types from randomization and also document the assumptions.

@saethlin
Copy link
Member

saethlin commented Nov 7, 2022

Just rehashing the usual discussion about this flag here so we're all on the same page.

  • These size assertion tests aren't common, but more crates than rustc have such tests, and it is annoying to break them.
  • -Zrandomize-layout doesn't add padding to structs with one field: Added randomized field padding to -Z randomize-layout rust#97861 (If I recall correctly, kixiron needs help pushing this through). This change would probably find a lot more issues than currently.
  • -Zrandomize-layout doesn't reorder the components of fat pointers, but there is ambient talk about the fact that we should stabilize that.
  • The errors you tend get out of Miri when you accidentally rely on field layout are strange, for example:
error: Undefined Behavior: constructing invalid value at .borrow.borrow: encountered a dangling reference (address 0x221b48 is unallocated)
   --> src/cell.rs:318:45
    |
318 |         let _ignore: Ref<String> = unsafe { mem::transmute((pointer, cell)) };
    |                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .borrow.borrow: encountered a dangling reference (address 0x221b48 is unallocated)
    |

(this is a crate trying to rely on the layout of std::cell::Ref)

In aggregate, I am ambivalent-to-no on this. It's not hard to turn on the flag yourself if you want to, and it finds almost no bugs in its current form. The cost and benefit are both rather small, but I think in its current form the flag is more likely to annoy authors of size assertions (because they tend to be written by people who care) than it is to find accidental layout dependencies (because I have debugged a few of those, and they seem to both be rare and mostly written by people who do not care so much about soundness).

@RalfJung
Copy link
Member Author

Based on the above, I have removed the flag from cargo-careful, and it seems like we will not set it in Miri (for now). Thanks for the feedback!

@RalfJung RalfJung closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants