Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 206678c

Browse files
committedJul 13, 2024
Auto merge of rust-lang#126606 - zachs18:patch-2, r=joboet
Guard against calling `libc::exit` multiple times on Linux. Mitigates (but does not fix) rust-lang#126600 by ensuring only one thread which calls Rust `exit` actually calls `libc::exit`, and all other callers of Rust `exit` block.
2 parents cac6664 + e74955e commit 206678c

File tree

4 files changed

+77
-0
lines changed

4 files changed

+77
-0
lines changed
 

‎std/src/rt.rs

+3
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ fn lang_start_internal(
144144
rtabort!("drop of the panic payload panicked");
145145
});
146146
panic::catch_unwind(cleanup).map_err(rt_abort)?;
147+
// Guard against multple threads calling `libc::exit` concurrently.
148+
// See the documentation for `unique_thread_exit` for more information.
149+
panic::catch_unwind(|| crate::sys::exit_guard::unique_thread_exit()).map_err(rt_abort)?;
147150
ret_code
148151
}
149152

‎std/src/sys/exit_guard.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
cfg_if::cfg_if! {
2+
if #[cfg(target_os = "linux")] {
3+
/// pthread_t is a pointer on some platforms,
4+
/// so we wrap it in this to impl Send + Sync.
5+
#[derive(Clone, Copy)]
6+
#[repr(transparent)]
7+
struct PThread(libc::pthread_t);
8+
// Safety: pthread_t is safe to send between threads
9+
unsafe impl Send for PThread {}
10+
// Safety: pthread_t is safe to share between threads
11+
unsafe impl Sync for PThread {}
12+
/// Mitigation for <https://github.com/rust-lang/rust/issues/126600>
13+
///
14+
/// On glibc, `libc::exit` has been observed to not always be thread-safe.
15+
/// It is currently unclear whether that is a glibc bug or allowed by the standard.
16+
/// To mitigate this problem, we ensure that only one
17+
/// Rust thread calls `libc::exit` (or returns from `main`) by calling this function before
18+
/// calling `libc::exit` (or returning from `main`).
19+
///
20+
/// Technically, this is not enough to ensure soundness, since other code directly calling
21+
/// `libc::exit` will still race with this.
22+
///
23+
/// *This function does not itself call `libc::exit`.* This is so it can also be used
24+
/// to guard returning from `main`.
25+
///
26+
/// This function will return only the first time it is called in a process.
27+
///
28+
/// * If it is called again on the same thread as the first call, it will abort.
29+
/// * If it is called again on a different thread, it will wait in a loop
30+
/// (waiting for the process to exit).
31+
#[cfg_attr(any(test, doctest), allow(dead_code))]
32+
pub(crate) fn unique_thread_exit() {
33+
let this_thread_id = unsafe { libc::pthread_self() };
34+
use crate::sync::{Mutex, PoisonError};
35+
static EXITING_THREAD_ID: Mutex<Option<PThread>> = Mutex::new(None);
36+
let mut exiting_thread_id =
37+
EXITING_THREAD_ID.lock().unwrap_or_else(PoisonError::into_inner);
38+
match *exiting_thread_id {
39+
None => {
40+
// This is the first thread to call `unique_thread_exit`,
41+
// and this is the first time it is called.
42+
// Set EXITING_THREAD_ID to this thread's ID and return.
43+
*exiting_thread_id = Some(PThread(this_thread_id));
44+
},
45+
Some(exiting_thread_id) if exiting_thread_id.0 == this_thread_id => {
46+
// This is the first thread to call `unique_thread_exit`,
47+
// but this is the second time it is called.
48+
// Abort the process.
49+
core::panicking::panic_nounwind("std::process::exit called re-entrantly")
50+
}
51+
Some(_) => {
52+
// This is not the first thread to call `unique_thread_exit`.
53+
// Pause until the process exits.
54+
drop(exiting_thread_id);
55+
loop {
56+
// Safety: libc::pause is safe to call.
57+
unsafe { libc::pause(); }
58+
}
59+
}
60+
}
61+
}
62+
} else {
63+
/// Mitigation for <https://github.com/rust-lang/rust/issues/126600>
64+
///
65+
/// Mitigation is ***NOT*** implemented on this platform, either because this platform
66+
/// is not affected, or because mitigation is not yet implemented for this platform.
67+
#[cfg_attr(any(test, doctest), allow(dead_code))]
68+
pub(crate) fn unique_thread_exit() {
69+
// Mitigation not required on platforms where `exit` is thread-safe.
70+
}
71+
}
72+
}

‎std/src/sys/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod personality;
77

88
pub mod backtrace;
99
pub mod cmath;
10+
pub mod exit_guard;
1011
pub mod os_str;
1112
pub mod path;
1213
pub mod sync;

‎std/src/sys/pal/unix/os.rs

+1
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,7 @@ pub fn home_dir() -> Option<PathBuf> {
758758
}
759759

760760
pub fn exit(code: i32) -> ! {
761+
crate::sys::exit_guard::unique_thread_exit();
761762
unsafe { libc::exit(code as c_int) }
762763
}
763764

0 commit comments

Comments
 (0)
Failed to load comments.