Skip to content

Commit 6453885

Browse files
committed
actually, reentrant uninitialized mutex acquisition is outright UB
1 parent ab3e4a2 commit 6453885

File tree

7 files changed

+15
-21
lines changed

7 files changed

+15
-21
lines changed

src/libstd/io/lazy.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,8 @@ impl<T: Send + Sync + 'static> Lazy<T> {
2929
/// Safety: `init` must not call `get` on the variable that is being
3030
/// initialized.
3131
pub const unsafe fn new(init: fn() -> Arc<T>) -> Lazy<T> {
32-
// `lock` is never initialized fully, so this mutex is reentrant!
33-
// Do not use it in a way that might be reentrant, that could lead to
34-
// aliasing `&mut`.
32+
// `lock` is never initialized fully, so it is UB to attempt to
33+
// acquire this mutex reentrantly!
3534
Lazy {
3635
lock: Mutex::new(),
3736
ptr: Cell::new(ptr::null_mut()),

src/libstd/sys/unix/args.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,8 @@ mod imp {
8080

8181
static mut ARGC: isize = 0;
8282
static mut ARGV: *const *const u8 = ptr::null();
83-
// `ENV_LOCK` is never initialized fully, so this mutex is reentrant!
84-
// Do not use it in a way that might be reentrant, that could lead to
85-
// aliasing `&mut`.
83+
// `ENV_LOCK` is never initialized fully, so it is UB to attempt to
84+
// acquire this mutex reentrantly!
8685
static LOCK: Mutex = Mutex::new();
8786

8887
pub unsafe fn init(argc: isize, argv: *const *const u8) {

src/libstd/sys/unix/os.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ use sys::fd;
3333
use vec;
3434

3535
const TMPBUF_SZ: usize = 128;
36-
// `ENV_LOCK` is never initialized fully, so this mutex is reentrant!
37-
// Do not use it in a way that might be reentrant, that could lead to
38-
// aliasing `&mut`.
36+
// `ENV_LOCK` is never initialized fully, so it is UB to attempt to
37+
// acquire this mutex reentrantly!
3938
static ENV_LOCK: Mutex = Mutex::new();
4039

4140

src/libstd/sys_common/at_exit_imp.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ type Queue = Vec<Box<dyn FnBox()>>;
2323
// on poisoning and this module needs to operate at a lower level than requiring
2424
// the thread infrastructure to be in place (useful on the borders of
2525
// initialization/destruction).
26-
// `LOCK` is never initialized fully, so this mutex is reentrant!
27-
// Do not use it in a way that might be reentrant, that could lead to
28-
// aliasing `&mut`.
26+
// `LOCK` is never initialized fully, so it is UB to attempt to
27+
// acquire this mutex reentrantly!
2928
static LOCK: Mutex = Mutex::new();
3029
static mut QUEUE: *mut Queue = ptr::null_mut();
3130

src/libstd/sys_common/mutex.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ impl Mutex {
2424
///
2525
/// Behavior is undefined if the mutex is moved after it is
2626
/// first used with any of the functions below.
27-
/// Also, the mutex might not be fully functional without calling
28-
/// `init`! For example, on unix, the mutex is reentrant
29-
/// until `init` reconfigures it appropriately.
27+
/// Also, until `init` is called, behavior is undefined if this
28+
/// mutex is ever used reentrantly, i.e., `raw_lock` or `try_lock`
29+
/// are called by the thread currently holding the lock.
3030
pub const fn new() -> Mutex { Mutex(imp::Mutex::new()) }
3131

3232
/// Prepare the mutex for use.

src/libstd/sys_common/thread_local.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,8 @@ impl StaticKey {
161161
// Additionally a 0-index of a tls key hasn't been seen on windows, so
162162
// we just simplify the whole branch.
163163
if imp::requires_synchronized_create() {
164-
// `INIT_LOCK` is never initialized fully, so this mutex is reentrant!
165-
// Do not use it in a way that might be reentrant, that could lead to
166-
// aliasing `&mut`.
164+
// `INIT_LOCK` is never initialized fully, so it is UB to attempt to
165+
// acquire this mutex reentrantly!
167166
static INIT_LOCK: Mutex = Mutex::new();
168167
let _guard = INIT_LOCK.lock();
169168
let mut key = self.key.load(Ordering::SeqCst);

src/libstd/thread/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -940,9 +940,8 @@ pub struct ThreadId(u64);
940940
impl ThreadId {
941941
// Generate a new unique thread ID.
942942
fn new() -> ThreadId {
943-
// `GUARD` is never initialized fully, so this mutex is reentrant!
944-
// Do not use it in a way that might be reentrant, that could lead to
945-
// aliasing `&mut`.
943+
// `GUARD` is never initialized fully, so it is UB to attempt to
944+
// acquire this mutex reentrantly!
946945
static GUARD: mutex::Mutex = mutex::Mutex::new();
947946
static mut COUNTER: u64 = 0;
948947

0 commit comments

Comments
 (0)