[lldb][NFC] Fix all formatting errors in .cpp file headers
Summary:
A *.cpp file header in LLDB (and in LLDB) should like this:
```
//===-- TestUtilities.cpp -------------------------------------------------===//
```
However in LLDB most of our source files have arbitrary changes to this format and
these changes are spreading through LLDB as folks usually just use the existing
source files as templates for their new files (most notably the unnecessary
editor language indicator `-*- C++ -*-` is spreading and in every review
someone is pointing out that this is wrong, resulting in people pointing out that this
is done in the same way in other files).
This patch removes most of these inconsistencies including the editor language indicators,
all the different missing/additional '-' characters, files that center the file name, missing
trailing `===//` (mostly caused by clang-format breaking the line).
Reviewers: aprantl, espindola, jfb, shafik, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: dexonsmith, wuzish, emaste, sdardis, nemanjai, kbarton, MaskRay, atanasyan, arphaman, jfb, abidh, jsji, JDevlieghere, usaxena95, lldb-commits
Tags: #lldb
Differential Revision: https://reviews.llvm.org/D73258
2020-01-24 15:23:27 +08:00
|
|
|
//===-- ProcessLauncherPosixFork.cpp --------------------------------------===//
|
2017-02-01 22:30:40 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2017-02-01 22:30:40 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Host/posix/ProcessLauncherPosixFork.h"
|
|
|
|
#include "lldb/Host/Host.h"
|
|
|
|
#include "lldb/Host/HostProcess.h"
|
|
|
|
#include "lldb/Host/Pipe.h"
|
Move FileAction, ProcessInfo and ProcessLaunchInfo from Target to Host
Summary:
These classes describe the details of the process we are about to
launch, and so they are naturally used by the launching code in the Host
module. Previously they were present in Target because that is the most
important (but by far not the only) user of the launching code.
Since the launching code has other customers, must of which do not care
about Targets, it makes sense to move these classes to the Host layer,
next to the launching code.
This move reduces the number of times that Target is included from host
to 8 (it used to be 14).
Reviewers: zturner, clayborg, jingham, davide, teemperor
Subscribers: emaste, mgorny, lldb-commits
Differential Revision: https://reviews.llvm.org/D56602
llvm-svn: 353047
2019-02-04 22:28:08 +08:00
|
|
|
#include "lldb/Host/ProcessLaunchInfo.h"
|
2017-03-23 02:40:07 +08:00
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-03-04 04:56:28 +08:00
|
|
|
#include "lldb/Utility/Log.h"
|
2017-06-06 22:06:17 +08:00
|
|
|
#include "llvm/Support/Errno.h"
|
2021-08-20 05:38:04 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2017-02-01 22:30:40 +08:00
|
|
|
|
2021-05-26 18:19:37 +08:00
|
|
|
#include <climits>
|
2017-02-01 22:30:40 +08:00
|
|
|
#include <sys/ptrace.h>
|
|
|
|
#include <sys/wait.h>
|
2017-07-18 21:14:01 +08:00
|
|
|
#include <unistd.h>
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
#include <sstream>
|
2017-07-18 22:03:47 +08:00
|
|
|
#include <csignal>
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
#include <android/api-level.h>
|
2017-02-02 00:43:22 +08:00
|
|
|
#define PT_TRACE_ME PTRACE_TRACEME
|
2017-02-01 22:30:40 +08:00
|
|
|
#endif
|
|
|
|
|
2017-09-16 10:19:21 +08:00
|
|
|
#if defined(__ANDROID_API__) && __ANDROID_API__ < 15
|
2017-02-01 22:30:40 +08:00
|
|
|
#include <linux/personality.h>
|
|
|
|
#elif defined(__linux__)
|
|
|
|
#include <sys/personality.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
// Begin code running in the child process
|
|
|
|
// NB: This code needs to be async-signal safe, since we're invoking fork from
|
|
|
|
// multithreaded contexts.
|
|
|
|
|
|
|
|
static void write_string(int error_fd, const char *str) {
|
|
|
|
int r = write(error_fd, str, strlen(str));
|
|
|
|
(void)r;
|
2017-02-01 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
2021-07-30 00:59:44 +08:00
|
|
|
[[noreturn]] static void ExitWithError(int error_fd,
|
|
|
|
const char *operation) {
|
2017-06-20 16:11:37 +08:00
|
|
|
int err = errno;
|
2021-12-21 20:59:14 +08:00
|
|
|
write_string(error_fd, operation);
|
|
|
|
write_string(error_fd, " failed: ");
|
|
|
|
// strerror is not guaranteed to be async-signal safe, but it usually is.
|
|
|
|
write_string(error_fd, strerror(err));
|
2017-02-01 22:30:40 +08:00
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
static void DisableASLR(int error_fd) {
|
2017-02-01 22:30:40 +08:00
|
|
|
#if defined(__linux__)
|
2021-12-21 20:59:14 +08:00
|
|
|
const unsigned long personality_get_current = 0xffffffff;
|
|
|
|
int value = personality(personality_get_current);
|
|
|
|
if (value == -1)
|
|
|
|
ExitWithError(error_fd, "personality get");
|
|
|
|
|
|
|
|
value = personality(ADDR_NO_RANDOMIZE | value);
|
|
|
|
if (value == -1)
|
|
|
|
ExitWithError(error_fd, "personality set");
|
2017-02-01 22:30:40 +08:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
static void DupDescriptor(int error_fd, const char *file, int fd, int flags) {
|
|
|
|
int target_fd = llvm::sys::RetryAfterSignal(-1, ::open, file, flags, 0666);
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
if (target_fd == -1)
|
|
|
|
ExitWithError(error_fd, "DupDescriptor-open");
|
|
|
|
|
|
|
|
if (target_fd == fd)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (::dup2(target_fd, fd) == -1)
|
|
|
|
ExitWithError(error_fd, "DupDescriptor-dup2");
|
|
|
|
|
|
|
|
::close(target_fd);
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
namespace {
|
|
|
|
struct ForkFileAction {
|
|
|
|
ForkFileAction(const FileAction &act);
|
|
|
|
|
|
|
|
FileAction::Action action;
|
|
|
|
int fd;
|
|
|
|
std::string path;
|
|
|
|
int arg;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ForkLaunchInfo {
|
|
|
|
ForkLaunchInfo(const ProcessLaunchInfo &info);
|
|
|
|
|
|
|
|
bool separate_process_group;
|
|
|
|
bool debug;
|
|
|
|
bool disable_aslr;
|
|
|
|
std::string wd;
|
|
|
|
const char **argv;
|
|
|
|
Environment::Envp envp;
|
|
|
|
std::vector<ForkFileAction> actions;
|
|
|
|
|
|
|
|
bool has_action(int fd) const {
|
|
|
|
for (const ForkFileAction &action : actions) {
|
|
|
|
if (action.fd == fd)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
[[noreturn]] static void ChildFunc(int error_fd, const ForkLaunchInfo &info) {
|
|
|
|
if (info.separate_process_group) {
|
2017-02-01 22:30:40 +08:00
|
|
|
if (setpgid(0, 0) != 0)
|
|
|
|
ExitWithError(error_fd, "setpgid");
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
for (const ForkFileAction &action : info.actions) {
|
|
|
|
switch (action.action) {
|
2017-02-01 22:30:40 +08:00
|
|
|
case FileAction::eFileActionClose:
|
2021-12-21 20:59:14 +08:00
|
|
|
if (close(action.fd) != 0)
|
2017-02-01 22:30:40 +08:00
|
|
|
ExitWithError(error_fd, "close");
|
|
|
|
break;
|
|
|
|
case FileAction::eFileActionDuplicate:
|
2021-12-21 20:59:14 +08:00
|
|
|
if (dup2(action.fd, action.arg) == -1)
|
2017-02-01 22:30:40 +08:00
|
|
|
ExitWithError(error_fd, "dup2");
|
|
|
|
break;
|
|
|
|
case FileAction::eFileActionOpen:
|
2021-12-21 20:59:14 +08:00
|
|
|
DupDescriptor(error_fd, action.path.c_str(), action.fd, action.arg);
|
2017-02-01 22:30:40 +08:00
|
|
|
break;
|
|
|
|
case FileAction::eFileActionNone:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change working directory
|
2021-12-21 20:59:14 +08:00
|
|
|
if (!info.wd.empty() && 0 != ::chdir(info.wd.c_str()))
|
2017-02-01 22:30:40 +08:00
|
|
|
ExitWithError(error_fd, "chdir");
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
if (info.disable_aslr)
|
|
|
|
DisableASLR(error_fd);
|
2017-02-01 22:30:40 +08:00
|
|
|
|
2018-05-01 00:49:04 +08:00
|
|
|
// Clear the signal mask to prevent the child from being affected by any
|
|
|
|
// masking done by the parent.
|
2017-02-01 22:30:40 +08:00
|
|
|
sigset_t set;
|
|
|
|
if (sigemptyset(&set) != 0 ||
|
|
|
|
pthread_sigmask(SIG_SETMASK, &set, nullptr) != 0)
|
|
|
|
ExitWithError(error_fd, "pthread_sigmask");
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
if (info.debug) {
|
2018-05-23 18:10:36 +08:00
|
|
|
// Do not inherit setgid powers.
|
|
|
|
if (setgid(getgid()) != 0)
|
|
|
|
ExitWithError(error_fd, "setgid");
|
|
|
|
|
2017-02-01 22:30:40 +08:00
|
|
|
// HACK:
|
|
|
|
// Close everything besides stdin, stdout, and stderr that has no file
|
|
|
|
// action to avoid leaking. Only do this when debugging, as elsewhere we
|
2018-05-01 00:49:04 +08:00
|
|
|
// actually rely on passing open descriptors to child processes.
|
2021-12-21 20:59:14 +08:00
|
|
|
// NB: This code is not async-signal safe, but we currently do not launch
|
|
|
|
// processes for debugging from within multithreaded contexts.
|
2021-08-20 05:38:04 +08:00
|
|
|
|
|
|
|
const llvm::StringRef proc_fd_path = "/proc/self/fd";
|
|
|
|
std::error_code ec;
|
|
|
|
bool result;
|
|
|
|
ec = llvm::sys::fs::is_directory(proc_fd_path, result);
|
|
|
|
if (result) {
|
|
|
|
std::vector<int> files_to_close;
|
|
|
|
// Directory iterator doesn't ensure any sequence.
|
|
|
|
for (llvm::sys::fs::directory_iterator iter(proc_fd_path, ec), file_end;
|
|
|
|
iter != file_end && !ec; iter.increment(ec)) {
|
|
|
|
int fd = std::stoi(iter->path().substr(proc_fd_path.size() + 1));
|
|
|
|
|
|
|
|
// Don't close first three entries since they are stdin, stdout and
|
|
|
|
// stderr.
|
2021-12-21 20:59:14 +08:00
|
|
|
if (fd > 2 && !info.has_action(fd) && fd != error_fd)
|
2021-08-20 05:38:04 +08:00
|
|
|
files_to_close.push_back(fd);
|
|
|
|
}
|
|
|
|
for (int file_to_close : files_to_close)
|
|
|
|
close(file_to_close);
|
|
|
|
} else {
|
|
|
|
// Since /proc/self/fd didn't work, trying the slow way instead.
|
|
|
|
int max_fd = sysconf(_SC_OPEN_MAX);
|
|
|
|
for (int fd = 3; fd < max_fd; ++fd)
|
2021-12-21 20:59:14 +08:00
|
|
|
if (!info.has_action(fd) && fd != error_fd)
|
2021-08-20 05:38:04 +08:00
|
|
|
close(fd);
|
|
|
|
}
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
// Start tracing this child that is about to exec.
|
|
|
|
if (ptrace(PT_TRACE_ME, 0, nullptr, 0) == -1)
|
|
|
|
ExitWithError(error_fd, "ptrace");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Execute. We should never return...
|
2021-12-21 20:59:14 +08:00
|
|
|
execve(info.argv[0], const_cast<char *const *>(info.argv), info.envp);
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
#if defined(__linux__)
|
|
|
|
if (errno == ETXTBSY) {
|
2018-10-05 06:33:39 +08:00
|
|
|
// On android M and earlier we can get this error because the adb daemon
|
2018-05-01 00:49:04 +08:00
|
|
|
// can hold a write handle on the executable even after it has finished
|
|
|
|
// uploading it. This state lasts only a short time and happens only when
|
|
|
|
// there are many concurrent adb commands being issued, such as when
|
|
|
|
// running the test suite. (The file remains open when someone does an "adb
|
|
|
|
// shell" command in the fork() child before it has had a chance to exec.)
|
|
|
|
// Since this state should clear up quickly, wait a while and then give it
|
|
|
|
// one more go.
|
2017-02-01 22:30:40 +08:00
|
|
|
usleep(50000);
|
2021-12-21 20:59:14 +08:00
|
|
|
execve(info.argv[0], const_cast<char *const *>(info.argv), info.envp);
|
2017-02-01 22:30:40 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ...unless exec fails. In which case we definitely need to end the child
|
|
|
|
// here.
|
|
|
|
ExitWithError(error_fd, "execve");
|
|
|
|
}
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
// End of code running in the child process.
|
|
|
|
|
|
|
|
ForkFileAction::ForkFileAction(const FileAction &act)
|
|
|
|
: action(act.GetAction()), fd(act.GetFD()), path(act.GetPath().str()),
|
|
|
|
arg(act.GetActionArgument()) {}
|
|
|
|
|
|
|
|
static std::vector<ForkFileAction>
|
|
|
|
MakeForkActions(const ProcessLaunchInfo &info) {
|
|
|
|
std::vector<ForkFileAction> result;
|
|
|
|
for (size_t i = 0; i < info.GetNumFileActions(); ++i)
|
|
|
|
result.emplace_back(*info.GetFileActionAtIndex(i));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static Environment::Envp FixupEnvironment(Environment env) {
|
|
|
|
#ifdef __ANDROID__
|
|
|
|
// If there is no PATH variable specified inside the environment then set the
|
|
|
|
// path to /system/bin. It is required because the default path used by
|
|
|
|
// execve() is wrong on android.
|
|
|
|
env.try_emplace("PATH", "/system/bin");
|
|
|
|
#endif
|
|
|
|
return env.getEnvp();
|
|
|
|
}
|
|
|
|
|
|
|
|
ForkLaunchInfo::ForkLaunchInfo(const ProcessLaunchInfo &info)
|
|
|
|
: separate_process_group(
|
|
|
|
info.GetFlags().Test(eLaunchFlagLaunchInSeparateProcessGroup)),
|
|
|
|
debug(info.GetFlags().Test(eLaunchFlagDebug)),
|
|
|
|
disable_aslr(info.GetFlags().Test(eLaunchFlagDisableASLR)),
|
|
|
|
wd(info.GetWorkingDirectory().GetPath()),
|
|
|
|
argv(info.GetArguments().GetConstArgumentVector()),
|
|
|
|
envp(FixupEnvironment(info.GetEnvironment())),
|
|
|
|
actions(MakeForkActions(info)) {}
|
|
|
|
|
2017-02-01 22:30:40 +08:00
|
|
|
HostProcess
|
|
|
|
ProcessLauncherPosixFork::LaunchProcess(const ProcessLaunchInfo &launch_info,
|
2017-05-12 12:51:55 +08:00
|
|
|
Status &error) {
|
2017-02-01 22:30:40 +08:00
|
|
|
// A pipe used by the child process to report errors.
|
|
|
|
PipePosix pipe;
|
|
|
|
const bool child_processes_inherit = false;
|
|
|
|
error = pipe.CreateNew(child_processes_inherit);
|
|
|
|
if (error.Fail())
|
|
|
|
return HostProcess();
|
|
|
|
|
2021-12-21 20:59:14 +08:00
|
|
|
const ForkLaunchInfo fork_launch_info(launch_info);
|
|
|
|
|
2017-02-01 22:30:40 +08:00
|
|
|
::pid_t pid = ::fork();
|
|
|
|
if (pid == -1) {
|
|
|
|
// Fork failed
|
2017-06-06 22:06:17 +08:00
|
|
|
error.SetErrorStringWithFormatv("Fork failed with error message: {0}",
|
|
|
|
llvm::sys::StrError());
|
2017-02-01 22:30:40 +08:00
|
|
|
return HostProcess(LLDB_INVALID_PROCESS_ID);
|
|
|
|
}
|
|
|
|
if (pid == 0) {
|
|
|
|
// child process
|
|
|
|
pipe.CloseReadFileDescriptor();
|
2021-12-21 20:59:14 +08:00
|
|
|
ChildFunc(pipe.ReleaseWriteFileDescriptor(), fork_launch_info);
|
2017-02-01 22:30:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// parent process
|
|
|
|
|
|
|
|
pipe.CloseWriteFileDescriptor();
|
|
|
|
char buf[1000];
|
|
|
|
int r = read(pipe.GetReadFileDescriptor(), buf, sizeof buf);
|
|
|
|
|
|
|
|
if (r == 0)
|
|
|
|
return HostProcess(pid); // No error. We're done.
|
|
|
|
|
|
|
|
error.SetErrorString(buf);
|
|
|
|
|
2019-03-22 03:35:55 +08:00
|
|
|
llvm::sys::RetryAfterSignal(-1, waitpid, pid, nullptr, 0);
|
2017-02-01 22:30:40 +08:00
|
|
|
|
|
|
|
return HostProcess();
|
|
|
|
}
|