Skip to content

Commit

Permalink
Auto merge of rust-lang#3831 - RalfJung:zero-sized-protector, r=RalfJung
Browse files Browse the repository at this point in the history
borrow tracking: add a test for zero-sized protectors
  • Loading branch information
bors committed Aug 21, 2024
2 parents f203b42 + 13b02e3 commit 41c65e6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 4 deletions.
4 changes: 1 addition & 3 deletions src/tools/miri/tests/fail/alloc/global_system_mixup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ use std::alloc::{Allocator, Global, Layout, System};
fn main() {
let l = Layout::from_size_align(1, 1).unwrap();
let ptr = Global.allocate(l).unwrap().as_non_null_ptr();
unsafe {
System.deallocate(ptr, l);
}
unsafe { System.deallocate(ptr, l) };
}
2 changes: 1 addition & 1 deletion src/tools/miri/tests/fail/alloc/global_system_mixup.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ LL | FREE();
note: inside `main`
--> $DIR/global_system_mixup.rs:LL:CC
|
LL | System.deallocate(ptr, l);
LL | unsafe { System.deallocate(ptr, l) };
| ^

note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
Expand Down
19 changes: 19 additions & 0 deletions src/tools/miri/tests/fail/both_borrows/zero-sized-protected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//@revisions: stack tree
//@[tree]compile-flags: -Zmiri-tree-borrows
//@[tree]error-in-other-file: /deallocation .* is forbidden/
use std::alloc::{alloc, dealloc, Layout};

// `x` is strongly protected but covers zero bytes.
// Let's see if deallocating the allocation x points to is UB:
// in TB, it is UB, but in SB it is not.
fn test(_x: &mut (), ptr: *mut u8, l: Layout) {
unsafe { dealloc(ptr, l) };
}

fn main() {
let l = Layout::from_size_align(1, 1).unwrap();
let ptr = unsafe { alloc(l) };
unsafe { test(&mut *ptr.cast::<()>(), ptr, l) };
// In SB the test would pass if it weren't for this line.
unsafe { std::hint::unreachable_unchecked() }; //~[stack] ERROR: unreachable
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: Undefined Behavior: entering unreachable code
--> $DIR/zero-sized-protected.rs:LL:CC
|
LL | unsafe { std::hint::unreachable_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ entering unreachable code
|
= 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 `main` at $DIR/zero-sized-protected.rs:LL:CC

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

error: aborting due to 1 previous error

Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: Undefined Behavior: deallocation through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
|
LL | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ deallocation through <TAG> (root of the allocation) at ALLOC[0x0] is forbidden
|
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Tree Borrows rules it violated are still experimental
= help: the allocation of the accessed tag <TAG> (root of the allocation) also contains the strongly protected tag <TAG>
= help: the strongly protected tag <TAG> disallows deallocations
help: the accessed tag <TAG> was created here
--> $DIR/zero-sized-protected.rs:LL:CC
|
LL | let ptr = unsafe { alloc(l) };
| ^^^^^^^^
help: the strongly protected tag <TAG> was created here, in the initial state Reserved
--> $DIR/zero-sized-protected.rs:LL:CC
|
LL | fn test(_x: &mut (), ptr: *mut u8, l: Layout) {
| ^^
= note: BACKTRACE (of the first span):
= note: inside `std::alloc::dealloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
note: inside `test`
--> $DIR/zero-sized-protected.rs:LL:CC
|
LL | unsafe { dealloc(ptr, l) };
| ^^^^^^^^^^^^^^^
note: inside `main`
--> $DIR/zero-sized-protected.rs:LL:CC
|
LL | unsafe { test(&mut *ptr.cast::<()>(), ptr, l) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 1 previous error

0 comments on commit 41c65e6

Please sign in to comment.