forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#3831 - RalfJung:zero-sized-protector, r=RalfJung
borrow tracking: add a test for zero-sized protectors
- Loading branch information
Showing
5 changed files
with
72 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/tools/miri/tests/fail/both_borrows/zero-sized-protected.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
15 changes: 15 additions & 0 deletions
15
src/tools/miri/tests/fail/both_borrows/zero-sized-protected.stack.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
36 changes: 36 additions & 0 deletions
36
src/tools/miri/tests/fail/both_borrows/zero-sized-protected.tree.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|