Skip to content
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

Merged
merged 1 commit into from
Jun 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/dbghelp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() };
Copy link
Contributor

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.

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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

Use the CloseHandle function to close the handle. The system closes the handle automatically when the process terminates. The mutex object is destroyed when its last handle has been closed.

if lock == 0 {
return Err(());
}
Expand Down