Skip to content

Commit 4e32ccb

Browse files
Rollup merge of #143692 - RalfJung:miri-oob, r=oli-obk
miri: fix out-of-bounds error for ptrs with negative offsets r? ```````@oli-obk```````
2 parents a03eaf3 + cb1a944 commit 4e32ccb

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let v: Vec<u16> = vec![1, 2];
3+
// This read is also misaligned. We make sure that the OOB message has priority.
4+
let x = unsafe { *v.as_ptr().wrapping_byte_sub(5) }; //~ ERROR: before the beginning of the allocation
5+
panic!("this should never print: {}", x);
6+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error: Undefined Behavior: memory access failed: attempting to access 2 bytes, but got ALLOC-0x5 which points to before the beginning of the allocation
2+
--> tests/fail/dangling_pointers/out_of_bounds_read_neg_offset.rs:LL:CC
3+
|
4+
LL | let x = unsafe { *v.as_ptr().wrapping_byte_sub(5) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
help: ALLOC was allocated here:
10+
--> tests/fail/dangling_pointers/out_of_bounds_read_neg_offset.rs:LL:CC
11+
|
12+
LL | let v: Vec<u16> = vec![1, 2];
13+
| ^^^^^^^^^^
14+
= note: BACKTRACE (of the first span):
15+
= note: inside `main` at tests/fail/dangling_pointers/out_of_bounds_read_neg_offset.rs:LL:CC
16+
= note: this error originates in the macro `vec` (in Nightly builds, run with -Z macro-backtrace for more info)
17+
18+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
19+
20+
error: aborting due to 1 previous error
21+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn main() {
2+
let v = [0i8; 4];
3+
let x = &v as *const i8;
4+
let x = unsafe { x.wrapping_offset(-1).offset(-1) }; //~ERROR: before the beginning of the allocation
5+
panic!("this should never print: {:?}", x);
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
error: Undefined Behavior: in-bounds pointer arithmetic failed: attempting to offset pointer by -1 bytes, but got ALLOC-0x1 which points to before the beginning of the allocation
2+
--> tests/fail/intrinsics/ptr_offset_out_of_bounds_neg2.rs:LL:CC
3+
|
4+
LL | let x = unsafe { x.wrapping_offset(-1).offset(-1) };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
6+
|
7+
= help: this indicates a bug in the program: it performed an invalid operation, and caused Undefined Behavior
8+
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
9+
help: ALLOC was allocated here:
10+
--> tests/fail/intrinsics/ptr_offset_out_of_bounds_neg2.rs:LL:CC
11+
|
12+
LL | let v = [0i8; 4];
13+
| ^
14+
= note: BACKTRACE (of the first span):
15+
= note: inside `main` at tests/fail/intrinsics/ptr_offset_out_of_bounds_neg2.rs:LL:CC
16+
17+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
18+
19+
error: aborting due to 1 previous error
20+

0 commit comments

Comments
 (0)