Skip to content

Commit 64af367

Browse files
committed
Add inline(never) to prevent reordering after TLS has been destroyed
1 parent 4b85f27 commit 64af367

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

library/std/src/sys/thread/xous.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,25 @@ impl Thread {
6969
)
7070
.map_err(|code| io::Error::from_raw_os_error(code as i32))?;
7171

72+
#[inline(never)]
73+
fn rust_main_thread_not_inlined(init: Box<ThreadInit>) {
74+
let rust_start = init.init();
75+
rust_start();
76+
}
77+
7278
extern "C" fn thread_start(
7379
data: *mut usize,
7480
guard_page_pre: usize,
7581
stack_size: usize,
7682
) -> ! {
7783
// SAFETY: we are simply recreating the box that was leaked earlier.
7884
let init = unsafe { Box::from_raw(data as *mut ThreadInit) };
79-
let rust_start = init.init();
80-
rust_start();
85+
86+
// Run the main thread with an inline(never) barrier to prevent
87+
// dealloc calls from being reordered to after the TLS has been destroyed.
88+
// See https://github.com/rust-lang/rust/pull/144465#pullrequestreview-3289729950
89+
// for more context.
90+
run_main_thread_not_inlined(init);
8191

8292
// Destroy TLS, which will free the TLS page and call the destructor for
8393
// any thread local storage (if any).

library/std/src/sys/thread_local/key/xous.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ pub unsafe fn destroy_tls() {
186186
};
187187
}
188188

189+
// This is marked inline(never) to prevent dealloc calls from being reordered
190+
// to after the TLS has been destroyed.
191+
// See https://github.com/rust-lang/rust/pull/144465#pullrequestreview-3289729950
192+
// for more context.
193+
#[inline(never)]
189194
unsafe fn run_dtors() {
190195
let mut any_run = true;
191196

0 commit comments

Comments
 (0)