This is an archive of the discontinued LLVM Phabricator instance.

[lldb] FIx data race in ThreadedCommunication
ClosedPublic

Authored by JDevlieghere on Aug 7 2023, 10:00 PM.

Details

Summary

TSan reports the following race:

Write of size 8 at 0x000107707ee8 by main thread:
  #0 lldb_private::ThreadedCommunication::StartReadThread(...) ThreadedCommunication.cpp:175
  #1 lldb_private::Process::SetSTDIOFileDescriptor(...) Process.cpp:4533
  #2 lldb_private::Platform::DebugProcess(...) Platform.cpp:1121
  #3 lldb_private::PlatformDarwin::DebugProcess(...) PlatformDarwin.cpp:711
  #4 lldb_private::Target::Launch(...) Target.cpp:3235
  #5 CommandObjectProcessLaunch::DoExecute(...) CommandObjectProcess.cpp:256
  #6 lldb_private::CommandObjectParsed::Execute(...) CommandObject.cpp:751
  #7 lldb_private::CommandInterpreter::HandleCommand(...) CommandInterpreter.cpp:2054

Previous read of size 8 at 0x000107707ee8 by thread T5:
  #0 lldb_private::HostThread::IsJoinable(...) const HostThread.cpp:30
  #1 lldb_private::ThreadedCommunication::StopReadThread(...) ThreadedCommunication.cpp:192
  #2 lldb_private::Process::ShouldBroadcastEvent(...) Process.cpp:3420
  #3 lldb_private::Process::HandlePrivateEvent(...) Process.cpp:3728
  #4 lldb_private::Process::RunPrivateStateThread(...) Process.cpp:3914
  #5 std::__1::__function::__func<lldb_private::Process::StartPrivateStateThread(...) function.h:356
  #6 lldb_private::HostNativeThreadBase::ThreadCreateTrampoline(...) HostNativeThreadBase.cpp:62
  #7 lldb_private::HostThreadMacOSX::ThreadCreateTrampoline(...) HostThreadMacOSX.mm:18

The problem is the lack of synchronization between starting and stopping the read thread. This patch fixes that by protecting those operations with a mutex.

Diff Detail

Event Timeline

JDevlieghere created this revision.Aug 7 2023, 10:00 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 7 2023, 10:00 PM
JDevlieghere requested review of this revision.Aug 7 2023, 10:00 PM
bulbazord accepted this revision.Aug 8 2023, 3:07 PM

Looks straightforward and makes sense. Thanks!

This revision is now accepted and ready to land.Aug 8 2023, 3:07 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2023, 3:16 PM