Skip to content

Commit ad2e756

Browse files
committed
Auto merge of #162361 - RalfJung:maybe-dangling-perf2, r=saethlin
give noalias back to refs in closures Reverts parts of #160745 to fix #163220. Needs team approval; FCP is happening in #163220.
2 parents 3670d25 + a18393e commit ad2e756

8 files changed

Lines changed: 104 additions & 19 deletions

File tree

‎compiler/rustc_middle/src/ty/sty.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2190,7 +2190,7 @@ impl<'tcx> Ty<'tcx> {
21902190
def.flags().contains(AdtFlags::IS_MAYBE_DANGLING)
21912191
|| def.flags().contains(AdtFlags::IS_MANUALLY_DROP)
21922192
}
2193-
ty::Closure(..) | ty::Coroutine(..) | ty::CoroutineClosure(..) => true,
2193+
ty::Coroutine(..) | ty::CoroutineClosure(..) => true,
21942194
_ => false,
21952195
}
21962196
}

‎library/std/src/thread/lifecycle.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use super::thread::Thread;
77
use super::{Result, spawnhook};
88
use crate::cell::UnsafeCell;
99
use crate::marker::PhantomData;
10+
use crate::mem::MaybeDangling;
1011
use crate::sync::Arc;
1112
use crate::sync::atomic::{Atomic, AtomicUsize, Ordering};
1213
use crate::sys::{AsInner, IntoInner, thread as imp};
@@ -56,9 +57,14 @@ where
5657
Arc::new(Packet { scope: scope_data, result: UnsafeCell::new(None), _marker: PhantomData });
5758
let their_packet = my_packet.clone();
5859

60+
// Pass `f` in `MaybeDangling` because actually that closure might *run longer than the lifetime of `F`*.
61+
// See <https://github.com/rust-lang/rust/issues/101983> for more details.
62+
let f = MaybeDangling::new(f);
63+
5964
// The entrypoint of the Rust thread, after platform-specific thread
6065
// initialization is done.
6166
let rust_start = move || {
67+
let f = f.into_inner();
6268
let try_result = panic::catch_unwind(panic::AssertUnwindSafe(|| {
6369
crate::sys::backtrace::__rust_begin_short_backtrace(|| hooks.inherit_and_run());
6470
crate::sys::backtrace::__rust_begin_short_backtrace(f)

‎src/tools/miri/tests/fail/async-shared-mutable.stack.stderr‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ LL | *x = 1;
99
help: <TAG> was created by a Unique retag at offsets [RANGE]
1010
--> tests/fail/async-shared-mutable.rs:LL:CC
1111
|
12-
LL | let x = &mut 0u8;
13-
| ^^^^^^^^
12+
LL | / core::future::poll_fn(move |_| {
13+
LL | | *x = 1;
14+
LL | | Poll::<()>::Pending
15+
LL | | })
16+
LL | | .await
17+
| |______________^
1418
help: <TAG> was later invalidated at offsets [RANGE] by a SharedReadOnly retag
1519
--> tests/fail/async-shared-mutable.rs:LL:CC
1620
|

‎src/tools/miri/tests/fail/async-shared-mutable.tree.stderr‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ LL | *x = 1;
1010
help: the accessed tag <TAG> was created here, in the initial state Reserved
1111
--> tests/fail/async-shared-mutable.rs:LL:CC
1212
|
13-
LL | let x = &mut 0u8;
14-
| ^^^^^^^^
13+
LL | / core::future::poll_fn(move |_| {
14+
LL | | *x = 1;
15+
LL | | Poll::<()>::Pending
16+
LL | | })
17+
LL | | .await
18+
| |______________^
1519
help: the accessed tag <TAG> later transitioned to Unique due to a child write access at offsets [RANGE]
1620
--> tests/fail/async-shared-mutable.rs:LL:CC
1721
|
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//@revisions: stack tree
2+
//@[tree]compile-flags: -Zmiri-tree-borrows
3+
4+
//@[stack]error-in-other-file: protected
5+
//@[tree]error-in-other-file: forbidden
6+
7+
// A reference in a closure is treated like a reference in a struct.
8+
fn main() {
9+
fn invoke(f: impl FnOnce()) {
10+
// The closure has captured a reference that will be freed while `invoke` runs.
11+
f()
12+
}
13+
14+
let p = Box::leak(Box::new(0i32));
15+
invoke(move || {
16+
drop(unsafe { Box::from_raw(p) });
17+
});
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error: Undefined Behavior: deallocating while item [Unique for <TAG>] is strongly protected
2+
--> RUSTLIB/alloc/src/boxed.rs:LL:CC
3+
|
4+
LL | self.1.deallocate(From::from(ptr.cast()), layout);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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+
= note: stack backtrace:
10+
0: <std::boxed::Box<i32> as std::ops::Drop>::drop
11+
at RUSTLIB/alloc/src/boxed.rs:LL:CC
12+
1: std::ptr::drop_glue::<std::boxed::Box<i32>> - shim(Some(std::boxed::Box<i32>))
13+
at RUSTLIB/core/src/ptr/mod.rs:LL:CC
14+
2: std::mem::drop::<std::boxed::Box<i32>>
15+
at RUSTLIB/core/src/mem/mod.rs:LL:CC
16+
3: main::{closure#0}
17+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
18+
4: main::invoke::<{closure@tests/fail/both_borrows/closure-captured-ref.rs:LL:CC}>
19+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
20+
5: main
21+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
22+
23+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
24+
25+
error: aborting due to 1 previous error
26+
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
error: Undefined Behavior: deallocation through <TAG> at ALLOC[0x0] is forbidden
2+
--> RUSTLIB/alloc/src/boxed.rs:LL:CC
3+
|
4+
LL | self.1.deallocate(From::from(ptr.cast()), layout);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Undefined Behavior occurred here
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: see https://github.com/rust-lang/unsafe-code-guidelines/blob/master/wip/tree-borrows.md for further information
9+
= help: the allocation of the accessed tag <TAG> also contains the strongly protected tag <TAG>
10+
= help: the strongly protected tag <TAG> disallows deallocations
11+
help: the accessed tag <TAG> was created here
12+
--> tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
13+
|
14+
LL | drop(unsafe { Box::from_raw(p) });
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
help: the strongly protected tag <TAG> was created here, in the initial state Reserved
17+
--> tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
18+
|
19+
LL | invoke(move || {
20+
| ____________^
21+
LL | | drop(unsafe { Box::from_raw(p) });
22+
LL | | });
23+
| |_____^
24+
= note: stack backtrace:
25+
0: <std::boxed::Box<i32> as std::ops::Drop>::drop
26+
at RUSTLIB/alloc/src/boxed.rs:LL:CC
27+
1: std::ptr::drop_glue::<std::boxed::Box<i32>> - shim(Some(std::boxed::Box<i32>))
28+
at RUSTLIB/core/src/ptr/mod.rs:LL:CC
29+
2: std::mem::drop::<std::boxed::Box<i32>>
30+
at RUSTLIB/core/src/mem/mod.rs:LL:CC
31+
3: main::{closure#0}
32+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
33+
4: main::invoke::<{closure@tests/fail/both_borrows/closure-captured-ref.rs:LL:CC}>
34+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
35+
5: main
36+
at tests/fail/both_borrows/closure-captured-ref.rs:LL:CC
37+
38+
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
39+
40+
error: aborting due to 1 previous error
41+

‎src/tools/miri/tests/pass/both_borrows/maybe_dangling.rs‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ fn main() {
1414
reference();
1515
write_through_shared_ref();
1616
large();
17-
closure();
1817
}
1918

2019
fn boxy() {
@@ -65,16 +64,3 @@ fn large() {
6564
// Used to be rejected due to faulty logic for the "does this fit the address space" check.
6665
let _x: MaybeDangling<&i8> = unsafe { mem::transmute(usize::MAX - 127) };
6766
}
68-
69-
// A closure acts like MaybeDangling.
70-
fn closure() {
71-
fn invoke(f: impl FnOnce()) {
72-
// The closure has captured a reference that will be freed while `invoke` runs.
73-
f()
74-
}
75-
76-
let p = Box::leak(Box::new(0i32));
77-
invoke(move || {
78-
drop(unsafe { Box::from_raw(p) });
79-
});
80-
}

0 commit comments

Comments
 (0)