Skip to content

Commit

Permalink
[lldb] Fix data race in ThreadList
Browse files Browse the repository at this point in the history
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
  • Loading branch information
augusto2112 committed Aug 18, 2023
1 parent 9c08e76 commit bb90063
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 11 deletions.
1 change: 0 additions & 1 deletion lldb/include/lldb/Target/Process.h
Original file line number Diff line number Diff line change
Expand Up @@ -2970,7 +2970,6 @@ void PruneThreadPlans();
std::string m_exit_string; ///< A textual description of why a process exited.
std::mutex m_exit_status_mutex; ///< Mutex so m_exit_status m_exit_string can
///be safely accessed from multiple threads
std::recursive_mutex m_thread_mutex;
ThreadList m_thread_list_real; ///< The threads for this process as are known
///to the protocol we are debugging with
ThreadList m_thread_list; ///< The threads for this process as the user will
Expand Down
2 changes: 1 addition & 1 deletion lldb/include/lldb/Target/ThreadCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class ThreadCollection {
return ThreadIterable(m_threads, GetMutex());
}

virtual std::recursive_mutex &GetMutex() const { return m_mutex; }
std::recursive_mutex &GetMutex() const { return m_mutex; }

protected:
collection m_threads;
Expand Down
2 changes: 0 additions & 2 deletions lldb/include/lldb/Target/ThreadList.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ class ThreadList : public ThreadCollection {

void SetStopID(uint32_t stop_id);

std::recursive_mutex &GetMutex() const override;

void Update(ThreadList &rhs);

protected:
Expand Down
4 changes: 2 additions & 2 deletions lldb/source/Target/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ Process::Process(lldb::TargetSP target_sp, ListenerSP listener_sp,
Listener::MakeListener("lldb.process.internal_state_listener")),
m_mod_id(), m_process_unique_id(0), m_thread_index_id(0),
m_thread_id_to_index_id_map(), m_exit_status(-1), m_exit_string(),
m_exit_status_mutex(), m_thread_mutex(), m_thread_list_real(this),
m_exit_status_mutex(), m_thread_list_real(this),
m_thread_list(this), m_thread_plans(*this), m_extended_thread_list(this),
m_extended_thread_stop_id(0), m_queue_list(this), m_queue_list_stop_id(0),
m_notifications(), m_image_tokens(),
Expand Down Expand Up @@ -2456,7 +2456,7 @@ Process::WaitForProcessStopPrivate(EventSP &event_sp,
}

void Process::LoadOperatingSystemPlugin(bool flush) {
std::lock_guard<std::recursive_mutex> guard(m_thread_mutex);
std::lock_guard<std::recursive_mutex> guard(GetThreadList().GetMutex());
if (flush)
m_thread_list.Clear();
m_os_up.reset(OperatingSystem::FindPlugin(this, nullptr));
Expand Down
7 changes: 2 additions & 5 deletions lldb/source/Target/ThreadList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,8 @@ void ThreadList::Update(ThreadList &rhs) {
if (this != &rhs) {
// Lock both mutexes to make sure neither side changes anyone on us while
// the assignment occurs
std::scoped_lock<std::recursive_mutex, std::recursive_mutex> guard(GetMutex(), rhs.GetMutex());
std::scoped_lock<std::recursive_mutex, std::recursive_mutex> guard(
GetMutex(), rhs.GetMutex());

m_process = rhs.m_process;
m_stop_id = rhs.m_stop_id;
Expand Down Expand Up @@ -781,10 +782,6 @@ void ThreadList::Flush() {
(*pos)->Flush();
}

std::recursive_mutex &ThreadList::GetMutex() const {
return m_process->m_thread_mutex;
}

ThreadList::ExpressionExecutionThreadPusher::ExpressionExecutionThreadPusher(
lldb::ThreadSP thread_sp)
: m_thread_list(nullptr), m_tid(LLDB_INVALID_THREAD_ID) {
Expand Down

0 comments on commit bb90063

Please sign in to comment.