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: ub: type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2) #96200

Closed
matthiaskrgr opened this issue Apr 19, 2022 · 2 comments
Labels
A-miri Area: The miri tool C-bug Category: This is a bug.

Comments

@matthiaskrgr
Copy link
Member

I tried this code:

// run-pass
#![allow(dead_code)]
#[repr(packed)]
pub struct Good {
    data: &'static u32,
    data2: [&'static u32; 2],
    aligned: [u8; 32],
}

// kill this test when that turns to a hard error
#[allow(unaligned_references)]
fn main() {
    let good = Good { data: &0, data2: [&0, &0], aligned: [0; 32] };

    let _ = &good.data; // ok
    let _ = &good.data2[0]; // ok

    let _ = &good.data;
    let _ = &good.data2[0];
    let _ = &*good.data; // ok, behind a pointer
    let _ = &good.aligned; // ok, has align 1
    let _ = &good.aligned[2]; // ok, has align 1
}
error: Undefined Behavior: type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
  --> src/main.rs:15:13
   |
15 |     let _ = &good.data; // ok
   |             ^^^^^^^^^^ type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2)
   |
   = help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
   = help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
rustc 1.62.0-nightly (ec77f2524 2022-04-17)
binary: rustc
commit-hash: ec77f252434a532fdb5699ae4f21a3072d211edd
commit-date: 2022-04-17
host: x86_64-unknown-linux-gnu
release: 1.62.0-nightly
LLVM version: 14.0.0
@matthiaskrgr matthiaskrgr added C-bug Category: This is a bug. A-miri Area: The miri tool labels Apr 19, 2022
@PatchMixolydic
Copy link
Contributor

PatchMixolydic commented Apr 19, 2022

This might be intentional. The original file is a test which seems to be related to #27060 (which has moved to #82523), which tracks the deprecation of unsound references to repr(packed) fields. Given the presence of #[allow(unaligned_references)], this appears to be a regression test to ensure that this code doesn't break before unaligned_references becomes a hard error.

@RalfJung
Copy link
Member

RalfJung commented Apr 19, 2022

Indeed this is not a bug. You allowed the unaligned_references lint which would have told you that there is a soundness issue in this code. :)

warning: reference to packed field is unaligned
  [--> src/main.rs:16:13
](https://play.rust-lang.org/#)   |
16 |     let _ = &good.data;
   |             ^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
   = note: for more information, [see issue #82523 <https://github.com/rust-lang/rust/issues/82523>](https://github.com/rust-lang/rust/issues/82523)
   = note: fields of packed structs are not properly aligned, and creating a misaligned reference is undefined behavior (even if that reference is never dereferenced)
   = help: copy the field contents to a local variable, or replace the reference with a raw pointer and use `read_unaligned`/`write_unaligned` (loads and stores via `*p` must be properly aligned even when using raw pointers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-miri Area: The miri tool C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

3 participants