Skip to content

Commit

Permalink
more precise error for 'based on misaligned pointer' case
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Oct 15, 2023
1 parent c87797e commit 6a5731b
Show file tree
Hide file tree
Showing 21 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion tests/fail/const-ub-checks.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
--> $DIR/const-ub-checks.rs:LL:CC
|
LL | ptr.read();
| ^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required

note: erroneous constant encountered
--> $DIR/const-ub-checks.rs:LL:CC
Expand Down
8 changes: 8 additions & 0 deletions tests/fail/dangling_pointers/out_of_bounds_read.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(pointer_byte_offsets)]

fn main() {
let v: Vec<u16> = vec![1, 2];
// This read is also misaligned. We make sure that the OOB message has priority.
let x = unsafe { *v.as_ptr().wrapping_byte_add(5) }; //~ ERROR: out-of-bounds
panic!("this should never print: {}", x);
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_read1.rs:LL:CC
error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_read.rs:LL:CC
|
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
LL | let x = unsafe { *v.as_ptr().wrapping_byte_add(5) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
|
= 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
help: ALLOC was allocated here:
--> $DIR/out_of_bounds_read1.rs:LL:CC
--> $DIR/out_of_bounds_read.rs:LL:CC
|
LL | let v: Vec<u8> = vec![1, 2];
| ^^^^^^^^^^
LL | let v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/out_of_bounds_read1.rs:LL:CC
= note: inside `main` at $DIR/out_of_bounds_read.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
5 changes: 0 additions & 5 deletions tests/fail/dangling_pointers/out_of_bounds_read1.rs

This file was deleted.

5 changes: 0 additions & 5 deletions tests/fail/dangling_pointers/out_of_bounds_read2.rs

This file was deleted.

7 changes: 7 additions & 0 deletions tests/fail/dangling_pointers/out_of_bounds_write.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(pointer_byte_offsets)]

fn main() {
let mut v: Vec<u16> = vec![1, 2];
// This read is also misaligned. We make sure that the OOB message has priority.
unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 }; //~ ERROR: out-of-bounds
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_read2.rs:LL:CC
error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
--> $DIR/out_of_bounds_write.rs:LL:CC
|
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
LL | unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
|
= 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
help: ALLOC was allocated here:
--> $DIR/out_of_bounds_read2.rs:LL:CC
--> $DIR/out_of_bounds_write.rs:LL:CC
|
LL | let v: Vec<u8> = vec![1, 2];
| ^^^^^^^^^^
LL | let mut v: Vec<u16> = vec![1, 2];
| ^^^^^^^^^^
= note: BACKTRACE (of the first span):
= note: inside `main` at $DIR/out_of_bounds_read2.rs:LL:CC
= note: inside `main` at $DIR/out_of_bounds_write.rs:LL:CC
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/alignment.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/alignment.rs:LL:CC
|
LL | *(x_ptr as *mut u32) = 42; *(x_ptr.add(1) as *mut u32) = 42;
| ^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^ accessing memory based on pointer 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct S {
}

unsafe fn foo(x: *const S) -> u8 {
unsafe { (*x).x } //~ERROR: accessing memory with alignment 1, but alignment 4 is required
unsafe { (*x).x } //~ERROR: based on pointer with alignment 1, but alignment 4 is required
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer 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
| ^^^^^^ accessing memory based on pointer 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Packed {
}

unsafe fn foo(x: *const Aligned) -> u8 {
unsafe { (*x).packed.x } //~ERROR: accessing memory with alignment 1, but alignment 16 is required
unsafe { (*x).packed.x } //~ERROR: based on pointer with alignment 1, but alignment 16 is required
}

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/field_requires_parent_struct_alignment2.rs:LL:CC
|
LL | unsafe { (*x).packed.x }
| ^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^^^^^^^^^^^^ accessing memory based on pointer 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ fn main() {
// Manually make sure the pointer is properly aligned.
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
let u16_ptr = base_addr_aligned as *mut u16;
unsafe { *u16_ptr = 2 }; //~ERROR: memory with alignment 1, but alignment 2 is required
unsafe { *u16_ptr = 2 }; //~ERROR: with alignment 1, but alignment 2 is required
println!("{:?}", x);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/intptrcast_alignment_check.rs:LL:CC
|
LL | unsafe { *u16_ptr = 2 };
| ^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
|
= help: this usually indicates that your program performed an invalid operation and caused Undefined Behavior
= help: but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives
Expand Down
2 changes: 1 addition & 1 deletion tests/fail/unaligned_pointers/unaligned_ptr1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ fn main() {
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
let x = &x[0] as *const _ as *const u32;
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
let _x = unsafe { *x }; //~ERROR: memory with alignment 2, but alignment 4 is required
let _x = unsafe { *x }; //~ERROR: with alignment 2, but alignment 4 is required
}
}
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/unaligned_ptr1.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr1.rs:LL:CC
|
LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^ accessing memory based on pointer 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
Expand Down
2 changes: 1 addition & 1 deletion tests/fail/unaligned_pointers/unaligned_ptr2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ fn main() {
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
// This must fail because alignment is violated: the offset is not sufficiently aligned.
// Also make the offset not a power of 2, that used to ICE.
let _x = unsafe { *x }; //~ERROR: memory with alignment 1, but alignment 4 is required
let _x = unsafe { *x }; //~ERROR: with alignment 1, but alignment 4 is required
}
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/unaligned_ptr2.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr2.rs:LL:CC
|
LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^ accessing memory based on pointer 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
Expand Down
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/unaligned_ptr3.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr3.rs:LL:CC
|
LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^ accessing memory based on pointer 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
Expand Down
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/unaligned_ptr4.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr4.rs:LL:CC
|
LL | let _val = unsafe { *ptr };
| ^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^^^ accessing memory based on pointer 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
Expand Down
4 changes: 2 additions & 2 deletions tests/fail/unaligned_pointers/unaligned_ptr_zst.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
--> $DIR/unaligned_ptr_zst.rs:LL:CC
|
LL | let _x = unsafe { *x };
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
| ^^ accessing memory based on pointer 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
Expand Down

0 comments on commit 6a5731b

Please sign in to comment.