Skip to content

Commit 1de082f

Browse files
committed
[lldb] Avoid data race in IOHandlerProcessSTDIO
This patch fixes a data race in IOHandlerProcessSTDIO. The race is happens between the main thread and the event handling thread. The main thread is running the IOHandler (IOHandlerProcessSTDIO::Run()) when an event comes in that makes us pop the process IO handler which involves cancelling the IOHandler (IOHandlerProcessSTDIO::Cancel). The latter calls SetIsDone(true) which modifies m_is_done. At the same time, we have the main thread reading the variable through GetIsDone(). This patch avoids the race by using a mutex to synchronize the two threads. On the event thread, in IOHandlerProcessSTDIO ::Cancel method, we obtain the lock before changing the value of m_is_done. On the main thread, in IOHandlerProcessSTDIO::Run(), we obtain the lock before reading the value of m_is_done. Additionally, we delay calling SetIsDone until after the loop exists, to avoid a potential race between the two writes. Write of size 1 at 0x00010b66bb68 by thread T7 (mutexes: write M2862, write M718324145051843688): #0 lldb_private::IOHandler::SetIsDone(bool) IOHandler.h:90 (liblldb.15.0.0git.dylib:arm64+0x971d84) #1 IOHandlerProcessSTDIO::Cancel() Process.cpp:4382 (liblldb.15.0.0git.dylib:arm64+0x5ddfec) #2 lldb_private::Debugger::PopIOHandler(std::__1::shared_ptr<lldb_private::IOHandler> const&) Debugger.cpp:1156 (liblldb.15.0.0git.dylib:arm64+0x3cb2a8) #3 lldb_private::Debugger::RemoveIOHandler(std::__1::shared_ptr<lldb_private::IOHandler> const&) Debugger.cpp:1063 (liblldb.15.0.0git.dylib:arm64+0x3cbd2c) #4 lldb_private::Process::PopProcessIOHandler() Process.cpp:4487 (liblldb.15.0.0git.dylib:arm64+0x5c583c) #5 lldb_private::Debugger::HandleProcessEvent(std::__1::shared_ptr<lldb_private::Event> const&) Debugger.cpp:1549 (liblldb.15.0.0git.dylib:arm64+0x3ceabc) #6 lldb_private::Debugger::DefaultEventHandler() Debugger.cpp:1622 (liblldb.15.0.0git.dylib:arm64+0x3cf2c0) #7 std::__1::__function::__func<lldb_private::Debugger::StartEventHandlerThread()::$_2, std::__1::allocator<lldb_private::Debugger::StartEventHandlerThread()::$_2>, void* ()>::operator()() function.h:352 (liblldb.15.0.0git.dylib:arm64+0x3d1bd8) #8 lldb_private::HostNativeThreadBase::ThreadCreateTrampoline(void*) HostNativeThreadBase.cpp:62 (liblldb.15.0.0git.dylib:arm64+0x4c71ac) #9 lldb_private::HostThreadMacOSX::ThreadCreateTrampoline(void*) HostThreadMacOSX.mm:18 (liblldb.15.0.0git.dylib:arm64+0x29ef544) Previous read of size 1 at 0x00010b66bb68 by main thread: #0 lldb_private::IOHandler::GetIsDone() IOHandler.h:92 (liblldb.15.0.0git.dylib:arm64+0x971db8) #1 IOHandlerProcessSTDIO::Run() Process.cpp:4339 (liblldb.15.0.0git.dylib:arm64+0x5ddc7c) #2 lldb_private::Debugger::RunIOHandlers() Debugger.cpp:982 (liblldb.15.0.0git.dylib:arm64+0x3cb48c) #3 lldb_private::CommandInterpreter::RunCommandInterpreter(lldb_private::CommandInterpreterRunOptions&) CommandInterpreter.cpp:3298 (liblldb.15.0.0git.dylib:arm64+0x506478) #4 lldb::SBDebugger::RunCommandInterpreter(bool, bool) SBDebugger.cpp:1166 (liblldb.15.0.0git.dylib:arm64+0x53604) #5 Driver::MainLoop() Driver.cpp:634 (lldb:arm64+0x100006294) #6 main Driver.cpp:853 (lldb:arm64+0x100007344) Differential revision: https://reviews.llvm.org/D120762 (cherry picked from commit 1022276)
1 parent 0aadad8 commit 1de082f

File tree

4 files changed

+74
-23
lines changed

4 files changed

+74
-23
lines changed

lldb/source/Target/Process.cpp

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4485,6 +4485,12 @@ class IOHandlerProcessSTDIO : public IOHandler {
44854485

44864486
~IOHandlerProcessSTDIO() override = default;
44874487

4488+
void SetIsRunning(bool running) {
4489+
std::lock_guard<std::mutex> guard(m_mutex);
4490+
SetIsDone(!running);
4491+
m_is_running = running;
4492+
}
4493+
44884494
// Each IOHandler gets to run until it is done. It should read data from the
44894495
// "in" and place output into "out" and "err and return when done.
44904496
void Run() override {
@@ -4504,49 +4510,52 @@ class IOHandlerProcessSTDIO : public IOHandler {
45044510
// FD_ZERO, FD_SET are not supported on windows
45054511
#ifndef _WIN32
45064512
const int pipe_read_fd = m_pipe.GetReadFileDescriptor();
4507-
m_is_running = true;
4508-
while (!GetIsDone()) {
4513+
SetIsRunning(true);
4514+
while (true) {
4515+
{
4516+
std::lock_guard<std::mutex> guard(m_mutex);
4517+
if (GetIsDone())
4518+
break;
4519+
}
4520+
45094521
SelectHelper select_helper;
45104522
select_helper.FDSetRead(read_fd);
45114523
select_helper.FDSetRead(pipe_read_fd);
45124524
Status error = select_helper.Select();
45134525

4514-
if (error.Fail()) {
4515-
SetIsDone(true);
4516-
} else {
4517-
char ch = 0;
4518-
size_t n;
4519-
if (select_helper.FDIsSetRead(read_fd)) {
4520-
n = 1;
4521-
if (m_read_file.Read(&ch, n).Success() && n == 1) {
4522-
if (m_write_file.Write(&ch, n).Fail() || n != 1)
4523-
SetIsDone(true);
4524-
} else
4525-
SetIsDone(true);
4526-
}
4526+
if (error.Fail())
4527+
break;
4528+
4529+
char ch = 0;
4530+
size_t n;
4531+
if (select_helper.FDIsSetRead(read_fd)) {
4532+
n = 1;
4533+
if (m_read_file.Read(&ch, n).Success() && n == 1) {
4534+
if (m_write_file.Write(&ch, n).Fail() || n != 1)
4535+
break;
4536+
} else
4537+
break;
4538+
45274539
if (select_helper.FDIsSetRead(pipe_read_fd)) {
45284540
size_t bytes_read;
45294541
// Consume the interrupt byte
45304542
Status error = m_pipe.Read(&ch, 1, bytes_read);
45314543
if (error.Success()) {
4532-
switch (ch) {
4533-
case 'q':
4534-
SetIsDone(true);
4544+
if (ch == 'q')
45354545
break;
4536-
case 'i':
4546+
if (ch == 'i')
45374547
if (StateIsRunningState(m_process->GetState()))
45384548
m_process->SendAsyncInterrupt();
4539-
break;
4540-
}
45414549
}
45424550
}
45434551
}
45444552
}
4545-
m_is_running = false;
4553+
SetIsRunning(false);
45464554
#endif
45474555
}
45484556

45494557
void Cancel() override {
4558+
std::lock_guard<std::mutex> guard(m_mutex);
45504559
SetIsDone(true);
45514560
// Only write to our pipe to cancel if we are in
45524561
// IOHandlerProcessSTDIO::Run(). We can end up with a python command that
@@ -4603,7 +4612,8 @@ class IOHandlerProcessSTDIO : public IOHandler {
46034612
NativeFile m_write_file; // Write to this file (usually the master pty for
46044613
// getting io to debuggee)
46054614
Pipe m_pipe;
4606-
std::atomic<bool> m_is_running{false};
4615+
std::mutex m_mutex;
4616+
bool m_is_running = false;
46074617
};
46084618

46094619
void Process::SetSTDIOFileDescriptor(int fd) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test.lldbpexpect import PExpectTest
5+
6+
class TestIOHandlerProcessSTDIO(PExpectTest):
7+
8+
mydir = TestBase.compute_mydir(__file__)
9+
NO_DEBUG_INFO_TESTCASE = True
10+
11+
# PExpect uses many timeouts internally and doesn't play well
12+
# under ASAN on a loaded machine..
13+
@skipIfAsan
14+
def test(self):
15+
self.build()
16+
self.launch(executable=self.getBuildArtifact("a.out"))
17+
self.child.sendline("run")
18+
19+
self.child.send("foo\n")
20+
self.child.expect_exact("stdout: foo")
21+
22+
self.child.send("bar\n")
23+
self.child.expect_exact("stdout: bar")
24+
25+
self.child.send("baz\n")
26+
self.child.expect_exact("stdout: baz")
27+
28+
self.child.sendcontrol('d')
29+
self.expect_prompt()
30+
self.quit()
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
int main(int argc, char **argv) {
5+
for (std::string line; std::getline(std::cin, line);)
6+
std::cout << "stdout: " << line << '\n';
7+
return 0;
8+
}

0 commit comments

Comments
 (0)