From 46010c4618d4602cd6999c613fce7081fad438c2 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 17 Jul 2021 05:47:39 -0700 Subject: [PATCH 1/3] Remove args cleanup code. As of 91c3eee1735ad72b579f99cbb6919c3471747d94, the global ARGC and ARGV no longer reference dynamically-allocated memory, so they don't need to be cleaned up. --- library/std/src/sys/unix/args.rs | 13 ------------- library/std/src/sys/unix/mod.rs | 1 - 2 files changed, 14 deletions(-) diff --git a/library/std/src/sys/unix/args.rs b/library/std/src/sys/unix/args.rs index fc423e393d4a4..d9e3c29f36b1e 100644 --- a/library/std/src/sys/unix/args.rs +++ b/library/std/src/sys/unix/args.rs @@ -14,11 +14,6 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) { imp::init(argc, argv) } -/// One-time global cleanup. -pub unsafe fn cleanup() { - imp::cleanup() -} - /// Returns the command line arguments pub fn args() -> Args { imp::args() @@ -127,12 +122,6 @@ mod imp { init_wrapper }; - pub unsafe fn cleanup() { - let _guard = LOCK.lock(); - ARGC.store(0, Ordering::Relaxed); - ARGV.store(ptr::null_mut(), Ordering::Relaxed); - } - pub fn args() -> Args { Args { iter: clone().into_iter() } } @@ -159,8 +148,6 @@ mod imp { pub unsafe fn init(_argc: isize, _argv: *const *const u8) {} - pub fn cleanup() {} - #[cfg(target_os = "macos")] pub fn args() -> Args { use crate::os::unix::prelude::*; diff --git a/library/std/src/sys/unix/mod.rs b/library/std/src/sys/unix/mod.rs index 2da71b2a448ac..9e553ec7682b1 100644 --- a/library/std/src/sys/unix/mod.rs +++ b/library/std/src/sys/unix/mod.rs @@ -123,7 +123,6 @@ pub unsafe fn init(argc: isize, argv: *const *const u8) { // SAFETY: must be called only once during runtime cleanup. // NOTE: this is not guaranteed to run, for example when the program aborts. pub unsafe fn cleanup() { - args::cleanup(); stack_overflow::cleanup(); } From 9bb11ba51135b0398b5a8b9feca24149d96fa5e8 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 17 Jul 2021 12:33:31 -0700 Subject: [PATCH 2/3] Remove an unnecessary `Mutex` around argument initialization. In the command-line argument initialization code, remove the Mutex around the `ARGV` and `ARGC` variables, and simply check whether ARGV is non-null before dereferencing it. This way, if either of ARGV or ARGC is not initialized, we'll get an empty argument list. This allows simple cdylibs to avoid having `pthread_mutex_lock`/`pthread_mutex_unlock` appear in their symbol tables if they don't otherwise use threads. --- library/std/src/sys/unix/args.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/library/std/src/sys/unix/args.rs b/library/std/src/sys/unix/args.rs index d9e3c29f36b1e..a6480199e7048 100644 --- a/library/std/src/sys/unix/args.rs +++ b/library/std/src/sys/unix/args.rs @@ -77,16 +77,10 @@ mod imp { use crate::ptr; use crate::sync::atomic::{AtomicIsize, AtomicPtr, Ordering}; - use crate::sys_common::mutex::StaticMutex; - static ARGC: AtomicIsize = AtomicIsize::new(0); static ARGV: AtomicPtr<*const u8> = AtomicPtr::new(ptr::null_mut()); - // We never call `ENV_LOCK.init()`, so it is UB to attempt to - // acquire this mutex reentrantly! - static LOCK: StaticMutex = StaticMutex::new(); unsafe fn really_init(argc: isize, argv: *const *const u8) { - let _guard = LOCK.lock(); ARGC.store(argc, Ordering::Relaxed); ARGV.store(argv as *mut _, Ordering::Relaxed); } @@ -128,9 +122,14 @@ mod imp { fn clone() -> Vec { unsafe { - let _guard = LOCK.lock(); - let argc = ARGC.load(Ordering::Relaxed); + // Load ARGC and ARGV without a lock. If the store to either ARGV or + // ARGC isn't visible yet, we'll return an empty argument list. let argv = ARGV.load(Ordering::Relaxed); + let argc = if argv.is_null() { + 0 + } else { + ARGC.load(Ordering::Relaxed) + }; (0..argc) .map(|i| { let cstr = CStr::from_ptr(*argv.offset(i) as *const libc::c_char); From c3df0ae97f9c03ce5937b9677c27da891a4a0fe0 Mon Sep 17 00:00:00 2001 From: Dan Gohman Date: Sat, 17 Jul 2021 18:31:51 -0700 Subject: [PATCH 3/3] x.py fmt --- library/std/src/sys/unix/args.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/library/std/src/sys/unix/args.rs b/library/std/src/sys/unix/args.rs index a6480199e7048..ad93fa610c481 100644 --- a/library/std/src/sys/unix/args.rs +++ b/library/std/src/sys/unix/args.rs @@ -125,11 +125,7 @@ mod imp { // Load ARGC and ARGV without a lock. If the store to either ARGV or // ARGC isn't visible yet, we'll return an empty argument list. let argv = ARGV.load(Ordering::Relaxed); - let argc = if argv.is_null() { - 0 - } else { - ARGC.load(Ordering::Relaxed) - }; + let argc = if argv.is_null() { 0 } else { ARGC.load(Ordering::Relaxed) }; (0..argc) .map(|i| { let cstr = CStr::from_ptr(*argv.offset(i) as *const libc::c_char);