Skip to content

Commit bb90063

Browse files
committed
[lldb] Fix data race in ThreadList
ThreadSanitizer reports the following issue: ``` Write of size 8 at 0x00010a70abb0 by thread T3 (mutexes: write M0): #0 lldb_private::ThreadList::Update(lldb_private::ThreadList&) ThreadList.cpp:741 (liblldb.18.0.0git.dylib:arm64+0x5dedf4) (BuildId: 9bced2aafa373580ae9d750d9cf79a8f32000000200000000100000000000e00) #1 lldb_private::Process::UpdateThreadListIfNeeded() Process.cpp:1212 (liblldb.18.0.0git.dylib:arm64+0x53bbec) (BuildId: 9bced2aafa373580ae9d750d9cf79a8f32000000200000000100000000000e00) Previous read of size 8 at 0x00010a70abb0 by main thread (mutexes: write M1): #0 lldb_private::ThreadList::GetMutex() const ThreadList.cpp:785 (liblldb.18.0.0git.dylib:arm64+0x5df138) (BuildId: 9bced2aafa373580ae9d750d9cf79a8f32000000200000000100000000000e00) #1 lldb_private::ThreadList::DidResume() ThreadList.cpp:656 (liblldb.18.0.0git.dylib:arm64+0x5de5c0) (BuildId: 9bced2aafa373580ae9d750d9cf79a8f32000000200000000100000000000e00) #2 lldb_private::Process::PrivateResume() Process.cpp:3130 (liblldb.18.0.0git.dylib:arm64+0x53cd7c) (BuildId: 9bced2aafa373580ae9d750d9cf79a8f32000000200000000100000000000e00) ``` Fix this by only using the mutex in ThreadList and removing the one in process entirely. Differential Revision: https://reviews.llvm.org/D158034
1 parent 9c08e76 commit bb90063

File tree

5 files changed

+5
-11
lines changed

5 files changed

+5
-11
lines changed

lldb/include/lldb/Target/Process.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2970,7 +2970,6 @@ void PruneThreadPlans();
29702970
std::string m_exit_string; ///< A textual description of why a process exited.
29712971
std::mutex m_exit_status_mutex; ///< Mutex so m_exit_status m_exit_string can
29722972
///be safely accessed from multiple threads
2973-
std::recursive_mutex m_thread_mutex;
29742973
ThreadList m_thread_list_real; ///< The threads for this process as are known
29752974
///to the protocol we are debugging with
29762975
ThreadList m_thread_list; ///< The threads for this process as the user will

lldb/include/lldb/Target/ThreadCollection.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ThreadCollection {
4747
return ThreadIterable(m_threads, GetMutex());
4848
}
4949

50-
virtual std::recursive_mutex &GetMutex() const { return m_mutex; }
50+
std::recursive_mutex &GetMutex() const { return m_mutex; }
5151

5252
protected:
5353
collection m_threads;

lldb/include/lldb/Target/ThreadList.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ class ThreadList : public ThreadCollection {
133133

134134
void SetStopID(uint32_t stop_id);
135135

136-
std::recursive_mutex &GetMutex() const override;
137-
138136
void Update(ThreadList &rhs);
139137

140138
protected:

lldb/source/Target/Process.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp,
435435
Listener::MakeListener("lldb.process.internal_state_listener")),
436436
m_mod_id(), m_process_unique_id(0), m_thread_index_id(0),
437437
m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(),
438-
m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this),
438+
m_exit_status_mutex(), m_thread_list_real(this),
439439
m_thread_list(this), m_thread_plans(*this), m_extended_thread_list(this),
440440
m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0),
441441
m_notifications(), m_image_tokens(),
@@ -2456,7 +2456,7 @@ Process::WaitForProcessStopPrivate(EventSP &event_sp,
24562456
}
24572457

24582458
void Process::LoadOperatingSystemPlugin(bool flush) {
2459-
std::lock_guard<std::recursive_mutex> guard(m_thread_mutex);
2459+
std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex());
24602460
if (flush)
24612461
m_thread_list.Clear();
24622462
m_os_up.reset(OperatingSystem::FindPlugin(this, nullptr));

lldb/source/Target/ThreadList.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,8 @@ void ThreadList::Update(ThreadList &rhs) {
736736
if (this != &rhs) {
737737
// Lock both mutexes to make sure neither side changes anyone on us while
738738
// the assignment occurs
739-
std::scoped_lock<std::recursive_mutex, std::recursive_mutex> guard(GetMutex(), rhs.GetMutex());
739+
std::scoped_lock<std::recursive_mutex, std::recursive_mutex> guard(
740+
GetMutex(), rhs.GetMutex());
740741

741742
m_process = rhs.m_process;
742743
m_stop_id = rhs.m_stop_id;
@@ -781,10 +782,6 @@ void ThreadList::Flush() {
781782
(*pos)->Flush();
782783
}
783784

784-
std::recursive_mutex &ThreadList::GetMutex() const {
785-
return m_process->m_thread_mutex;
786-
}
787-
788785
ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
789786
lldb::ThreadSP thread_sp)
790787
: m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {

0 commit comments

Comments
 (0)