Skip to content

miri: ub: type validation failed: encountered an unaligned reference (required 8 byte alignment but found 2) #96200

Closed

Description

@matthiaskrgr

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

Activity

  1. PatchMixolydic commented on Apr 19, 2022

    @PatchMixolydic
    Contributor

    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.

  2. RalfJung commented on Apr 19, 2022

    @RalfJung
    Member

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-miriArea: The miri toolC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions