From 81ea21b4ab5d32d50a350a77ea1885d795326208 Mon Sep 17 00:00:00 2001 From: Max Heller Date: Sat, 8 Jul 2023 11:06:53 -0400 Subject: [PATCH] skip deallocating main-thread TLS backing memory --- src/concurrency/thread.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/concurrency/thread.rs b/src/concurrency/thread.rs index 04f0a6c66a..488541e13f 100644 --- a/src/concurrency/thread.rs +++ b/src/concurrency/thread.rs @@ -1070,14 +1070,26 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> { thread.state = ThreadState::Terminated; let current_span = this.machine.current_span(); - for ptr in - this.machine.threads.thread_terminated(this.machine.data_race.as_mut(), current_span) - { - if let Some(alloc) = ptr.provenance.get_alloc_id() { - trace!("Main thread thread-local static stored as static root: {:?}", alloc); - this.machine.static_roots.push(alloc); - } - this.deallocate_ptr(ptr.into(), None, MiriMemoryKind::Tls.into())?; + let thread_local_allocations = + this.machine.threads.thread_terminated(this.machine.data_race.as_mut(), current_span); + match this.get_active_thread() { + // For main thread, add thread-local statics to static roots and skip deallocating + // backing memory + ThreadId(0) => + for ptr in thread_local_allocations { + if let Some(alloc) = ptr.provenance.get_alloc_id() { + trace!( + "Main thread thread-local static stored as static root: {:?}", + alloc + ); + this.machine.static_roots.push(alloc); + } + }, + // Deallocate backing memory of thread-local statics + ThreadId(_) => + for ptr in thread_local_allocations { + this.deallocate_ptr(ptr.into(), None, MiriMemoryKind::Tls.into())?; + }, } Ok(()) }