diff options
| author | Ralf Jung <post@ralfj.de> | 2023-07-31 16:00:48 +0200 |
|---|---|---|
| committer | Ralf Jung <post@ralfj.de> | 2023-07-31 16:07:15 +0200 |
| commit | a63db6de5ae034bd006be663e760b9f81226b44c (patch) | |
| tree | 8d95cb7f44754c78e5534531422c8e3d75565359 /src/tools/miri/tests/fail/unaligned_pointers | |
| parent | e1071af908f70aaa32903a92a167cfb0cf1e9d84 (diff) | |
| download | rust-a63db6de5ae034bd006be663e760b9f81226b44c.tar.gz rust-a63db6de5ae034bd006be663e760b9f81226b44c.zip | |
add some interesting tests for alignment corner cases
Diffstat (limited to 'src/tools/miri/tests/fail/unaligned_pointers')
2 files changed, 44 insertions, 0 deletions
diff --git a/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.rs b/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.rs new file mode 100644 index 00000000000..fa1812adc29 --- /dev/null +++ b/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.rs @@ -0,0 +1,24 @@ +/// This tests that when a field sits at offset 0 in a 4-aligned struct, accessing the field +/// requires alignment 4 even if the field type has lower alignment requirements. + +#[repr(C)] +pub struct S { + x: u8, + y: u32, +} + +unsafe fn foo(x: *const S) -> u8 { + unsafe { (*x).x } //~ERROR: accessing memory with alignment 1, but alignment 4 is required +} + +fn main() { + unsafe { + let mem = [0u64; 16]; + let odd_ptr = std::ptr::addr_of!(mem).cast::<u8>().add(1); + // `odd_ptr` is now not aligned enough for `S`. + // If accessing field `x` can exploit that it is at offset 0 + // in a 4-aligned struct, that field access requires alignment 4, + // thus making this UB. + foo(odd_ptr.cast()); + } +} diff --git a/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.stderr b/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.stderr new file mode 100644 index 00000000000..0f030a6e27c --- /dev/null +++ b/src/tools/miri/tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.stderr @@ -0,0 +1,20 @@ +error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required + --> $DIR/field_requires_parent_struct_alignment.rs:LL:CC + | +LL | unsafe { (*x).x } + | ^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required + | + = 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 + = note: BACKTRACE: + = note: inside `foo` at $DIR/field_requires_parent_struct_alignment.rs:LL:CC +note: inside `main` + --> $DIR/field_requires_parent_struct_alignment.rs:LL:CC + | +LL | foo(odd_ptr.cast()); + | ^^^^^^^^^^^^^^^^^^^ + +note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace + +error: aborting due to previous error + |
