-
Notifications
You must be signed in to change notification settings - Fork 246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
dbghlp: Make mutex name unique to the process #518
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,23 @@ pub struct Init { | |
pub fn init() -> Result<Init, ()> { | ||
use core::sync::atomic::{AtomicUsize, Ordering::SeqCst}; | ||
|
||
// Helper function for generating a name that's unique to the process. | ||
fn mutex_name() -> [u8; 33] { | ||
let mut name: [u8; 33] = *b"Local\\RustBacktraceMutex00000000\0"; | ||
let mut id = unsafe { GetCurrentProcessId() }; | ||
// Quick and dirty no alloc u32 to hex. | ||
let mut index = name.len() - 1; | ||
while id > 0 { | ||
name[index - 1] = match (id & 0xF) as u8 { | ||
h @ 0..=9 => b'0' + h, | ||
h => b'A' + (h - 10), | ||
}; | ||
id >>= 4; | ||
index -= 1; | ||
Comment on lines
+249
to
+254
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is... quite rudimentary. I'm impressed. |
||
} | ||
name | ||
} | ||
|
||
unsafe { | ||
// First thing we need to do is to synchronize this function. This can | ||
// be called concurrently from other threads or recursively within one | ||
|
@@ -277,11 +294,8 @@ pub fn init() -> Result<Init, ()> { | |
static LOCK: AtomicUsize = AtomicUsize::new(0); | ||
let mut lock = LOCK.load(SeqCst); | ||
if lock == 0 { | ||
lock = CreateMutexA( | ||
ptr::null_mut(), | ||
0, | ||
"Local\\RustBacktraceMutex\0".as_ptr() as _, | ||
) as usize; | ||
let name = mutex_name(); | ||
lock = CreateMutexA(ptr::null_mut(), 0, name.as_ptr().cast::<i8>()) as usize; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does windows remove the mutex again once all open handles are closed or will this keep accumulating mutexes until the session ends? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-createmutexa
|
||
if lock == 0 { | ||
return Err(()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Windows guarantees the return value is unique across the system until the process dies. Since this effectively means a process-wide mutex, this could conceivably complicate the implementation of something like #473 further. However, that does not seem to have momentum, and this is a necessary fix, so it's fine to take this even if it inconveniences that.