Skip to content

Commit 320084e

Browse files
committed
Auto merge of rust-lang#2268 - RalfJung:not-unpin-protected, r=RalfJung
test that &mut !Unpin references are protected
2 parents 54a495c + d3ca71b commit 320084e

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// error-pattern: deallocating while item is protected
2+
use std::marker::PhantomPinned;
3+
4+
pub struct NotUnpin(i32, PhantomPinned);
5+
6+
fn inner(x: &mut NotUnpin, f: fn(&mut NotUnpin)) {
7+
// `f` may mutate, but it may not deallocate!
8+
f(x)
9+
}
10+
11+
fn main() {
12+
inner(Box::leak(Box::new(NotUnpin(0, PhantomPinned))), |x| {
13+
let raw = x as *mut _;
14+
drop(unsafe { Box::from_raw(raw) });
15+
});
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
error: Undefined Behavior: deallocating while item is protected: [SharedReadWrite for <TAG> (call ID)]
2+
--> RUSTLIB/alloc/src/alloc.rs:LL:CC
3+
|
4+
LL | unsafe { __rust_dealloc(ptr, layout.size(), layout.align()) }
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ deallocating while item is protected: [SharedReadWrite for <TAG> (call ID)]
6+
|
7+
= help: this indicates a potential bug in the program: it performed an invalid operation, but the Stacked Borrows rules it violated are still experimental
8+
= help: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/stacked-borrows.md for further information
9+
10+
= note: inside `std::alloc::dealloc` at RUSTLIB/alloc/src/alloc.rs:LL:CC
11+
= note: inside `<std::alloc::Global as std::alloc::Allocator>::deallocate` at RUSTLIB/alloc/src/alloc.rs:LL:CC
12+
= note: inside `alloc::alloc::box_free::<NotUnpin, std::alloc::Global>` at RUSTLIB/alloc/src/alloc.rs:LL:CC
13+
= note: inside `std::ptr::drop_in_place::<std::boxed::Box<NotUnpin>> - shim(Some(std::boxed::Box<NotUnpin>))` at RUSTLIB/core/src/ptr/mod.rs:LL:CC
14+
= note: inside `std::mem::drop::<std::boxed::Box<NotUnpin>>` at RUSTLIB/core/src/mem/mod.rs:LL:CC
15+
note: inside closure at $DIR/deallocate_against_barrier2.rs:LL:CC
16+
--> $DIR/deallocate_against_barrier2.rs:LL:CC
17+
|
18+
LL | drop(unsafe { Box::from_raw(raw) });
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
= note: inside `<[closure@$DIR/deallocate_against_barrier2.rs:LL:CC] as std::ops::FnOnce<(&mut NotUnpin,)>>::call_once - shim` at RUSTLIB/core/src/ops/function.rs:LL:CC
21+
note: inside `inner` at $DIR/deallocate_against_barrier2.rs:LL:CC
22+
--> $DIR/deallocate_against_barrier2.rs:LL:CC
23+
|
24+
LL | f(x)
25+
| ^^^^
26+
note: inside `main` at $DIR/deallocate_against_barrier2.rs:LL:CC
27+
--> $DIR/deallocate_against_barrier2.rs:LL:CC
28+
|
29+
LL | / inner(Box::leak(Box::new(NotUnpin(0, PhantomPinned))), |x| {
30+
LL | | let raw = x as *mut _;
31+
LL | | drop(unsafe { Box::from_raw(raw) });
32+
LL | | });
33+
| |______^
34+
35+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
36+
37+
error: aborting due to previous error
38+

0 commit comments

Comments
 (0)