Skip to content

Commit 13b02e3

Browse files
committed
add a test for zero-sized protectors
1 parent f203b42 commit 13b02e3

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

Diff for: src/tools/miri/tests/fail/alloc/global_system_mixup.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,5 @@ use std::alloc::{Allocator, Global, Layout, System};
1313
fn main() {
1414
let l = Layout::from_size_align(1, 1).unwrap();
1515
let ptr = Global.allocate(l).unwrap().as_non_null_ptr();
16-
unsafe {
17-
System.deallocate(ptr, l);
18-
}
16+
unsafe { System.deallocate(ptr, l) };
1917
}

Diff for: src/tools/miri/tests/fail/alloc/global_system_mixup.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL | FREE();
1212
note: inside `main`
1313
--> $DIR/global_system_mixup.rs:LL:CC
1414
|
15-
LL | System.deallocate(ptr, l);
15+
LL | unsafe { System.deallocate(ptr, l) };
1616
| ^
1717

1818
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
//@[tree]error-in-other-file: /deallocation .* is forbidden/
4+
use std::alloc::{alloc, dealloc, Layout};
5+
6+
// `x` is strongly protected but covers zero bytes.
7+
// Let's see if deallocating the allocation x points to is UB:
8+
// in TB, it is UB, but in SB it is not.
9+
fn test(_x: &mut (), ptr: *mut u8, l: Layout) {
10+
unsafe { dealloc(ptr, l) };
11+
}
12+
13+
fn main() {
14+
let l = Layout::from_size_align(1, 1).unwrap();
15+
let ptr = unsafe { alloc(l) };
16+
unsafe { test(&mut *ptr.cast::<()>(), ptr, l) };
17+
// In SB the test would pass if it weren't for this line.
18+
unsafe { std::hint::unreachable_unchecked() }; //~[stack] ERROR: unreachable
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: Undefined Behavior: entering unreachable code
2+
--> $DIR/zero-sized-protected.rs:LL:CC
3+
|
4+
LL | unsafe { std::hint::unreachable_unchecked() };
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
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+
= note: BACKTRACE:
10+
= note: inside `main` at $DIR/zero-sized-protected.rs:LL:CC
11+
12+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
13+
14+
error: aborting due to 1 previous error
15+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: Undefined Behavior: deallocation through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
2+
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
3+
|
4+
LL | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ deallocation through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
8+
= help: the allocation of the accessed tag <TAG> (root of the allocation) also contains the strongly protected tag <TAG>
9+
= help: the strongly protected tag <TAG> disallows deallocations
10+
help: the accessed tag <TAG> was created here
11+
--> $DIR/zero-sized-protected.rs:LL:CC
12+
|
13+
LL | let ptr = unsafe { alloc(l) };
14+
| ^^^^^^^^
15+
help: the strongly protected tag <TAG> was created here, in the initial state Reserved
16+
--> $DIR/zero-sized-protected.rs:LL:CC
17+
|
18+
LL | fn test(_x: &mut (), ptr: *mut u8, l: Layout) {
19+
| ^^
20+
= note: BACKTRACE (of the first span):
21+
= note: inside `std::alloc::dealloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
22+
note: inside `test`
23+
--> $DIR/zero-sized-protected.rs:LL:CC
24+
|
25+
LL | unsafe { dealloc(ptr, l) };
26+
| ^^^^^^^^^^^^^^^
27+
note: inside `main`
28+
--> $DIR/zero-sized-protected.rs:LL:CC
29+
|
30+
LL | unsafe { test(&mut *ptr.cast::<()>(), ptr, l) };
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
33+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
34+
35+
error: aborting due to 1 previous error
36+

0 commit comments

Comments
 (0)