This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Protect RNBRemote from a data race
Needs ReviewPublic

Authored by augusto2112 on Aug 15 2023, 4:19 PM.

Details

Summary

Thread sanitizer reports the following data race:

Write of size 8 at 0x000103303e70 by thread T1 (mutexes: write M0):
  #0 RNBRemote::CommDataReceived(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) RNBRemote.cpp:1075 (debugserver:arm64+0x100038db8) (BuildId: f130b34f693c4f3eba96139104af2b7132000000200000000100000000000e00)
  #1 RNBRemote::ThreadFunctionReadRemoteData(void*) RNBRemote.cpp:1180 (debugserver:arm64+0x1000391dc) (BuildId: f130b34f693c4f3eba96139104af2b7132000000200000000100000000000e00)

Previous read of size 8 at 0x000103303e70 by main thread:
  #0 RNBRemote::GetPacketPayload(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&) RNBRemote.cpp:797 (debugserver:arm64+0x100037c5c) (BuildId: f130b34f693c4f3eba96139104af2b7132000000200000000100000000000e00)
  #1 RNBRemote::GetPacket(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&, RNBRemote::Packet&, bool) RNBRemote.cpp:907 (debugserver:arm64+0x1000378cc) (BuildId: f130b34f693c4f3eba96139104af2b7132000000200000000100000000000e00)

RNBRemote already has a mutex, extend its usage to protect the read of
m_rx_packets.

Diff Detail

Event Timeline

augusto2112 created this revision.Aug 15 2023, 4:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 4:19 PM
augusto2112 requested review of this revision.Aug 15 2023, 4:19 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 15 2023, 4:19 PM
JDevlieghere added inline comments.
lldb/tools/debugserver/source/RNBRemote.cpp
780

This is an RAII object, right? Can we just block scope it? Right now it looks like we might not unlock if we return early on line 787.

jasonmolenda added inline comments.Sep 8 2023, 4:00 PM
lldb/tools/debugserver/source/RNBRemote.cpp
780

I agree with the suggested change. But yeah I think putting the part of this method that touches m_rx_packets in a block, and holding the lock in that block, would be best, make it a little clearer. As for the early return on 787, won't locker release the lock in its dtor?