Skip to content

Commit 6a5731b

Browse files
committed
more precise error for 'based on misaligned pointer' case
1 parent c87797e commit 6a5731b

21 files changed

+55
-50
lines changed

tests/fail/const-ub-checks.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: evaluation of constant value failed
22
--> $DIR/const-ub-checks.rs:LL:CC
33
|
44
LL | ptr.read();
5-
| ^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66

77
note: erroneous constant encountered
88
--> $DIR/const-ub-checks.rs:LL:CC
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(pointer_byte_offsets)]
2+
3+
fn main() {
4+
let v: Vec<u16> = vec![1, 2];
5+
// This read is also misaligned. We make sure that the OOB message has priority.
6+
let x = unsafe { *v.as_ptr().wrapping_byte_add(5) }; //~ ERROR: out-of-bounds
7+
panic!("this should never print: {}", x);
8+
}

tests/fail/dangling_pointers/out_of_bounds_read1.stderr renamed to tests/fail/dangling_pointers/out_of_bounds_read.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
2-
--> $DIR/out_of_bounds_read1.rs:LL:CC
1+
error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
2+
--> $DIR/out_of_bounds_read.rs:LL:CC
33
|
4-
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
4+
LL | let x = unsafe { *v.as_ptr().wrapping_byte_add(5) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
help: ALLOC was allocated here:
10-
--> $DIR/out_of_bounds_read1.rs:LL:CC
10+
--> $DIR/out_of_bounds_read.rs:LL:CC
1111
|
12-
LL | let v: Vec<u8> = vec![1, 2];
13-
| ^^^^^^^^^^
12+
LL | let v: Vec<u16> = vec![1, 2];
13+
| ^^^^^^^^^^
1414
= note: BACKTRACE (of the first span):
15-
= note: inside `main` at $DIR/out_of_bounds_read1.rs:LL:CC
15+
= note: inside `main` at $DIR/out_of_bounds_read.rs:LL:CC
1616
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/dangling_pointers/out_of_bounds_read1.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/fail/dangling_pointers/out_of_bounds_read2.rs

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(pointer_byte_offsets)]
2+
3+
fn main() {
4+
let mut v: Vec<u16> = vec![1, 2];
5+
// This read is also misaligned. We make sure that the OOB message has priority.
6+
unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 }; //~ ERROR: out-of-bounds
7+
}

tests/fail/dangling_pointers/out_of_bounds_read2.stderr renamed to tests/fail/dangling_pointers/out_of_bounds_write.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
error: Undefined Behavior: memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
2-
--> $DIR/out_of_bounds_read2.rs:LL:CC
1+
error: Undefined Behavior: memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
2+
--> $DIR/out_of_bounds_write.rs:LL:CC
33
|
4-
LL | let x = unsafe { *v.as_ptr().wrapping_offset(5) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 2, so pointer to 1 byte starting at offset 5 is out-of-bounds
4+
LL | unsafe { *v.as_mut_ptr().wrapping_byte_add(5) = 0 };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ memory access failed: ALLOC has size 4, so pointer to 2 bytes starting at offset 5 is out-of-bounds
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
99
help: ALLOC was allocated here:
10-
--> $DIR/out_of_bounds_read2.rs:LL:CC
10+
--> $DIR/out_of_bounds_write.rs:LL:CC
1111
|
12-
LL | let v: Vec<u8> = vec![1, 2];
13-
| ^^^^^^^^^^
12+
LL | let mut v: Vec<u16> = vec![1, 2];
13+
| ^^^^^^^^^^
1414
= note: BACKTRACE (of the first span):
15-
= note: inside `main` at $DIR/out_of_bounds_read2.rs:LL:CC
15+
= note: inside `main` at $DIR/out_of_bounds_write.rs:LL:CC
1616
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
1717

1818
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace

tests/fail/unaligned_pointers/alignment.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/alignment.rs:LL:CC
33
|
44
LL | *(x_ptr as *mut u32) = 42; *(x_ptr.add(1) as *mut u32) = 42;
5-
| ^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub struct S {
88
}
99

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

1414
fn main() {

tests/fail/unaligned_pointers/field_requires_parent_struct_alignment.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/field_requires_parent_struct_alignment.rs:LL:CC
33
|
44
LL | unsafe { (*x).x }
5-
| ^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/field_requires_parent_struct_alignment2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub struct Packed {
1515
}
1616

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

2121
fn main() {

tests/fail/unaligned_pointers/field_requires_parent_struct_alignment2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/field_requires_parent_struct_alignment2.rs:LL:CC
33
|
44
LL | unsafe { (*x).packed.x }
5-
| ^^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/intptrcast_alignment_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ fn main() {
1212
// Manually make sure the pointer is properly aligned.
1313
let base_addr_aligned = if base_addr % 2 == 0 { base_addr } else { base_addr + 1 };
1414
let u16_ptr = base_addr_aligned as *mut u16;
15-
unsafe { *u16_ptr = 2 }; //~ERROR: memory with alignment 1, but alignment 2 is required
15+
unsafe { *u16_ptr = 2 }; //~ERROR: with alignment 1, but alignment 2 is required
1616
println!("{:?}", x);
1717
}

tests/fail/unaligned_pointers/intptrcast_alignment_check.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/intptrcast_alignment_check.rs:LL:CC
33
|
44
LL | unsafe { *u16_ptr = 2 };
5-
| ^^^^^^^^^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^^^^^^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this usually indicates that your program performed an invalid operation and caused Undefined Behavior
88
= help: but due to `-Zmiri-symbolic-alignment-check`, alignment errors can also be false positives

tests/fail/unaligned_pointers/unaligned_ptr1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ fn main() {
77
let x = [2u16, 3, 4]; // Make it big enough so we don't get an out-of-bounds error.
88
let x = &x[0] as *const _ as *const u32;
99
// This must fail because alignment is violated: the allocation's base is not sufficiently aligned.
10-
let _x = unsafe { *x }; //~ERROR: memory with alignment 2, but alignment 4 is required
10+
let _x = unsafe { *x }; //~ERROR: with alignment 2, but alignment 4 is required
1111
}
1212
}

tests/fail/unaligned_pointers/unaligned_ptr1.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/unaligned_ptr1.rs:LL:CC
33
|
44
LL | let _x = unsafe { *x };
5-
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/unaligned_ptr2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ fn main() {
88
let x = (x.as_ptr() as *const u8).wrapping_offset(3) as *const u32;
99
// This must fail because alignment is violated: the offset is not sufficiently aligned.
1010
// Also make the offset not a power of 2, that used to ICE.
11-
let _x = unsafe { *x }; //~ERROR: memory with alignment 1, but alignment 4 is required
11+
let _x = unsafe { *x }; //~ERROR: with alignment 1, but alignment 4 is required
1212
}

tests/fail/unaligned_pointers/unaligned_ptr2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/unaligned_ptr2.rs:LL:CC
33
|
44
LL | let _x = unsafe { *x };
5-
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/unaligned_ptr3.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/unaligned_ptr3.rs:LL:CC
33
|
44
LL | let _x = unsafe { *x };
5-
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/unaligned_ptr4.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/unaligned_ptr4.rs:LL:CC
33
|
44
LL | let _val = unsafe { *ptr };
5-
| ^^^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

tests/fail/unaligned_pointers/unaligned_ptr_zst.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: Undefined Behavior: accessing memory with alignment ALIGN, but alignment ALIGN is required
1+
error: Undefined Behavior: accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
22
--> $DIR/unaligned_ptr_zst.rs:LL:CC
33
|
44
LL | let _x = unsafe { *x };
5-
| ^^ accessing memory with alignment ALIGN, but alignment ALIGN is required
5+
| ^^ accessing memory based on pointer with alignment ALIGN, but alignment ALIGN is required
66
|
77
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
88
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information

0 commit comments

Comments
 (0)