[Lldb-commits] [lldb] [lldb] Use correct path for lldb-server executable (PR #131519)

Yuval Deutscher via lldb-commits lldb-commits at lists.llvm.org
Thu Mar 20 08:02:24 PDT 2025


https://github.com/yuvald-sweet-security updated https://github.com/llvm/llvm-project/pull/131519

>From ea0a08e60f472c518c98db0acf7e32ec328146d1 Mon Sep 17 00:00:00 2001
From: Yuval Deutscher <yuvald at sweet.security>
Date: Sun, 16 Mar 2025 14:08:57 +0000
Subject: [PATCH 1/2] [lldb] Use correct path for lldb-server executable

---
 lldb/tools/lldb-server/lldb-platform.cpp | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp
index 880b45b989b9c..51174a0f443c3 100644
--- a/lldb/tools/lldb-server/lldb-platform.cpp
+++ b/lldb/tools/lldb-server/lldb-platform.cpp
@@ -31,6 +31,7 @@
 #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
 #include "lldb/Host/ConnectionFileDescriptor.h"
 #include "lldb/Host/HostGetOpt.h"
+#include "lldb/Host/HostInfo.h"
 #include "lldb/Host/MainLoop.h"
 #include "lldb/Host/OptionParser.h"
 #include "lldb/Host/Socket.h"
@@ -256,8 +257,9 @@ static void client_handle(GDBRemoteCommunicationServerPlatform &platform,
   printf("Disconnected.\n");
 }
 
-static Status spawn_process(const char *progname, const Socket *conn_socket,
-                            uint16_t gdb_port, const lldb_private::Args &args,
+static Status spawn_process(const char *progname, const FileSpec &prog,
+                            const Socket *conn_socket, uint16_t gdb_port,
+                            const lldb_private::Args &args,
                             const std::string &log_file,
                             const StringRef log_channels, MainLoop &main_loop) {
   Status error;
@@ -267,9 +269,10 @@ static Status spawn_process(const char *progname, const Socket *conn_socket,
 
   ProcessLaunchInfo launch_info;
 
-  FileSpec self_spec(progname, FileSpec::Style::native);
-  launch_info.SetExecutableFile(self_spec, true);
+  launch_info.SetExecutableFile(prog, false);
+  launch_info.SetArg0(progname);
   Args &self_args = launch_info.GetArguments();
+  self_args.AppendArgument(progname);
   self_args.AppendArgument(llvm::StringRef("platform"));
   self_args.AppendArgument(llvm::StringRef("--child-platform-fd"));
   self_args.AppendArgument(llvm::to_string(shared_socket.GetSendableFD()));
@@ -551,9 +554,10 @@ int main_platform(int argc, char *argv[]) {
                         log_channels, &main_loop,
                         &platform_handles](std::unique_ptr<Socket> sock_up) {
               printf("Connection established.\n");
-              Status error = spawn_process(progname, sock_up.get(),
-                                           gdbserver_port, inferior_arguments,
-                                           log_file, log_channels, main_loop);
+              Status error = spawn_process(
+                  progname, HostInfo::GetProgramFileSpec(), sock_up.get(),
+                  gdbserver_port, inferior_arguments, log_file, log_channels,
+                  main_loop);
               if (error.Fail()) {
                 Log *log = GetLog(LLDBLog::Platform);
                 LLDB_LOGF(log, "spawn_process failed: %s", error.AsCString());

>From dc67b5776c60a0a4443fa76402f182ed6ca3b3b1 Mon Sep 17 00:00:00 2001
From: Yuval Deutscher <yuvald at sweet.security>
Date: Thu, 20 Mar 2025 17:01:41 +0200
Subject: [PATCH 2/2] [lldb] Respect arg0 override in fork/exec launcher

---
 .../Host/posix/ProcessLauncherPosixFork.cpp    | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp b/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
index 3e956290c3055..cbc0234dbd016 100644
--- a/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
+++ b/lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
@@ -94,6 +94,7 @@ struct ForkLaunchInfo {
   bool debug;
   bool disable_aslr;
   std::string wd;
+  std::string executable;
   const char **argv;
   Environment::Envp envp;
   std::vector<ForkFileAction> actions;
@@ -194,7 +195,8 @@ struct ForkLaunchInfo {
   }
 
   // Execute.  We should never return...
-  execve(info.argv[0], const_cast<char *const *>(info.argv), info.envp);
+  execve(info.executable.c_str(), const_cast<char *const *>(info.argv),
+         info.envp);
 
 #if defined(__linux__)
   if (errno == ETXTBSY) {
@@ -207,7 +209,8 @@ struct ForkLaunchInfo {
     // Since this state should clear up quickly, wait a while and then give it
     // one more go.
     usleep(50000);
-    execve(info.argv[0], const_cast<char *const *>(info.argv), info.envp);
+    execve(info.executable.c_str(), const_cast<char *const *>(info.argv),
+           info.envp);
   }
 #endif
 
@@ -236,8 +239,17 @@ ForkLaunchInfo::ForkLaunchInfo(const ProcessLaunchInfo &info)
       debug(info.GetFlags().Test(eLaunchFlagDebug)),
       disable_aslr(info.GetFlags().Test(eLaunchFlagDisableASLR)),
       wd(info.GetWorkingDirectory().GetPath()),
+      executable(info.GetExecutableFile().GetPath()),
       argv(info.GetArguments().GetConstArgumentVector()),
-      envp(info.GetEnvironment().getEnvp()), actions(MakeForkActions(info)) {}
+      envp(info.GetEnvironment().getEnvp()), actions(MakeForkActions(info)) {
+  // If we didn't get an executable, use args vector to locate the executable
+  // instead
+  if (executable.empty())
+    executable = argv[0];
+  // If it was requested to override arg 0 at exec, respect that
+  if (!info.GetArg0().empty())
+    argv[0] = info.GetArg0().data();
+}
 
 HostProcess
 ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info,



More information about the lldb-commits mailing list