Skip to content

[lldb] Respect LaunchInfo::SetExecutable in ProcessLauncherPosixFork #133093

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lldb/source/Host/posix/ProcessLauncherPosixFork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ struct ForkLaunchInfo {
bool debug;
bool disable_aslr;
std::string wd;
std::string executable;
Comment on lines 96 to +97
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is consistent with the rest of the file, but any reason these are std::strings and not FileSpecs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are accessed from the forked process (line 34 in this file), so they need to be async-signal safe (nothing really guarantees std::string is that, but it's pretty hard to write a c_str implementation that isn't).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, thanks for explaining!

const char **argv;
Environment::Envp envp;
std::vector<ForkFileAction> actions;
Expand Down Expand Up @@ -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) {
Expand All @@ -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

Expand Down Expand Up @@ -236,6 +239,7 @@ 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)) {}

Expand Down
43 changes: 42 additions & 1 deletion lldb/unittests/Host/HostTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,24 @@
//===----------------------------------------------------------------------===//

#include "lldb/Host/Host.h"
#include "TestingSupport/SubsystemRAII.h"
#include "lldb/Host/FileSystem.h"
#include "lldb/Host/ProcessLaunchInfo.h"
#include "lldb/Utility/ProcessInfo.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Testing/Support/Error.h"
#include "gtest/gtest.h"
#include <future>

using namespace lldb_private;
using namespace llvm;

// From TestMain.cpp.
extern const char *TestMainArgv0;

static cl::opt<uint64_t> test_arg("test-arg");

TEST(Host, WaitStatusFormat) {
EXPECT_EQ("W01", formatv("{0:g}", WaitStatus{WaitStatus::Exit, 1}).str());
EXPECT_EQ("X02", formatv("{0:g}", WaitStatus{WaitStatus::Signal, 2}).str());
Expand Down Expand Up @@ -45,4 +57,33 @@ TEST(Host, ProcessInstanceInfoCumulativeSystemTimeIsValid) {
EXPECT_TRUE(info.CumulativeSystemTimeIsValid());
info.SetCumulativeSystemTime(ProcessInstanceInfo::timespec{1, 0});
EXPECT_TRUE(info.CumulativeSystemTimeIsValid());
}
}

TEST(Host, LaunchProcessSetsArgv0) {
SubsystemRAII<FileSystem> subsystems;

static constexpr StringLiteral TestArgv0 = "HelloArgv0";
if (test_arg != 0) {
// In subprocess
if (TestMainArgv0 != TestArgv0) {
errs() << formatv("Got '{0}' for argv[0]\n", TestMainArgv0);
exit(1);
}
exit(0);
}

ProcessLaunchInfo info;
info.SetExecutableFile(
FileSpec(llvm::sys::fs::getMainExecutable(TestMainArgv0, &test_arg)),
/*add_exe_file_as_first_arg=*/false);
info.GetArguments().AppendArgument("HelloArgv0");
info.GetArguments().AppendArgument(
"--gtest_filter=Host.LaunchProcessSetsArgv0");
info.GetArguments().AppendArgument("--test-arg=47");
std::promise<int> exit_status;
info.SetMonitorProcessCallback([&](lldb::pid_t pid, int signal, int status) {
exit_status.set_value(status);
});
ASSERT_THAT_ERROR(Host::LaunchProcess(info).takeError(), Succeeded());
ASSERT_THAT(exit_status.get_future().get(), 0);
}