Skip to content

Commit 18edd41

Browse files
[lldb][test] Wait for a file before attempting to attach to lldb-server test inferior (llvm#162064)
Fixes llvm#161510 Addresses the Linux parts of llvm#138085 The situation we have to handle here is systems where Yama ptrace_scope set to 1. > 1 - restricted ptrace: a process must have a predefined relationship > with the inferior it wants to call PTRACE_ATTACH on. By default, > this relationship is that of only its descendants when the above > classic criteria is also met. To change the relationship, an > inferior can call prctl(PR_SET_PTRACER, debugger, ...) to declare > an allowed debugger PID to call PTRACE_ATTACH on the inferior. > Using PTRACE_TRACEME is unchanged. (https://www.kernel.org/doc/Documentation/security/Yama.txt) The inferior was addressing this by calling this at the start of main(): prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, 0, 0, 0); Which is ok if lldb-server tries to attach after that call has happened, but there was nothing to synchronise this. So if the system was heavily loaded, the inferior may be stalled, delaying the call, causing lldb-server to fail to attach with EPERM (permission denied). We were not using any mechanism to retry the attach or wait for some signal from the inferior. Except we do do this in other tests, even other lldb-server tests. So I have adopted that mechanism to these tests: * The inferior is launched with `syncfile:<path>` as its first argument. * It creates this file at `<path>`, at a point where we know attaching has been allowed. * The test framework launches the inferior then waits for the file to appear. * This check is retried a few times, increasing the delay each time and eventually giving up. * Only once it has seen the file does it start lldb-server and tell it to attach to the inferior. I have tested this by insterting a `sleep()` call before the attach enable call and running the test on a machine with ptrace_scope set to 1. I was able to increase the sleep to 6 seconds before tests failed (when running just these tests, single threaded). With OS scheduling, you could be stalled indefinitely, so we may have to increase this timeout but this is easy to do with wait_for_file_on_target. The alternative is to have the test runner check ptrace_scope and only enable these on systems where it's 0. Would be good to keep them running if we can though.
1 parent b88af66 commit 18edd41

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

lldb/packages/Python/lldbsuite/test/tools/lldb-server/gdbremote_testcase.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,13 +444,20 @@ def launch_process_for_attach(
444444
if not exe_path:
445445
exe_path = self.getBuildArtifact("a.out")
446446

447-
args = []
447+
# This file will be created once the inferior has enabled attaching.
448+
sync_file_path = lldbutil.append_to_process_working_directory(
449+
self, "process_ready"
450+
)
451+
args = [f"syncfile:{sync_file_path}"]
448452
if inferior_args:
449453
args.extend(inferior_args)
450454
if sleep_seconds:
451455
args.append("sleep:%d" % sleep_seconds)
452456

453-
return self.spawnSubprocess(exe_path, args)
457+
inferior = self.spawnSubprocess(exe_path, args)
458+
lldbutil.wait_for_file_on_target(self, sync_file_path)
459+
460+
return inferior
454461

455462
def prep_debug_monitor_and_inferior(
456463
self,

lldb/test/API/tools/lldb-server/main.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstdlib>
66
#include <cstring>
77
#include <errno.h>
8+
#include <fstream>
89
#include <future>
910
#include <inttypes.h>
1011
#include <memory>
@@ -265,7 +266,11 @@ int main(int argc, char **argv) {
265266
// Process command line args.
266267
for (int i = 1; i < argc; ++i) {
267268
std::string arg = argv[i];
268-
if (consume_front(arg, "stderr:")) {
269+
if (consume_front(arg, "syncfile:")) {
270+
// Write to this file to tell test framework that attaching is now
271+
// possible.
272+
std::ofstream(arg).close();
273+
} else if (consume_front(arg, "stderr:")) {
269274
// Treat remainder as text to go to stderr.
270275
fprintf(stderr, "%s\n", arg.c_str());
271276
} else if (consume_front(arg, "retval:")) {

0 commit comments

Comments
 (0)