23 extern "C" char **environ;
27 static std::mutex SignalMutex;
30 static int ActiveThreadCount = 0;
31 static struct sigaction OldSigIntAction;
32 static struct sigaction OldSigQuitAction;
33 static sigset_t OldBlockedSignalsSet;
42 posix_spawnattr_t SpawnAttributes;
43 if (posix_spawnattr_init(&SpawnAttributes))
48 std::lock_guard<std::mutex>
Lock(SignalMutex);
49 if (ActiveThreadCount == 0) {
50 static struct sigaction IgnoreSignalAction;
51 sigset_t BlockedSignalsSet;
52 memset(&IgnoreSignalAction, 0,
sizeof(IgnoreSignalAction));
53 IgnoreSignalAction.sa_handler = SIG_IGN;
55 if (sigaction(SIGINT, &IgnoreSignalAction, &OldSigIntAction) == -1) {
56 Printf(
"Failed to ignore SIGINT\n");
57 (void)posix_spawnattr_destroy(&SpawnAttributes);
60 if (sigaction(SIGQUIT, &IgnoreSignalAction, &OldSigQuitAction) == -1) {
61 Printf(
"Failed to ignore SIGQUIT\n");
63 (void)sigaction(SIGINT, &OldSigIntAction, NULL);
64 (void)posix_spawnattr_destroy(&SpawnAttributes);
68 (void)sigemptyset(&BlockedSignalsSet);
69 (void)sigaddset(&BlockedSignalsSet, SIGCHLD);
70 if (sigprocmask(SIG_BLOCK, &BlockedSignalsSet, &OldBlockedSignalsSet) ==
72 Printf(
"Failed to block SIGCHLD\n");
74 (void)sigaction(SIGQUIT, &OldSigQuitAction, NULL);
75 (void)sigaction(SIGINT, &OldSigIntAction, NULL);
76 (void)posix_spawnattr_destroy(&SpawnAttributes);
89 sigset_t DefaultSigSet;
90 (void)sigemptyset(&DefaultSigSet);
91 (void)sigaddset(&DefaultSigSet, SIGQUIT);
92 (void)sigaddset(&DefaultSigSet, SIGINT);
93 (void)posix_spawnattr_setsigdefault(&SpawnAttributes, &DefaultSigSet);
95 (void)posix_spawnattr_setsigmask(&SpawnAttributes, &OldBlockedSignalsSet);
96 short SpawnFlags = POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK;
97 (void)posix_spawnattr_setflags(&SpawnAttributes, SpawnFlags);
100 char **Environ = environ;
101 const char *CommandCStr = Command.c_str();
102 char *
const Argv[] = {
108 int ErrorCode = 0, ProcessStatus = 0;
110 ErrorCode = posix_spawn(&Pid,
"/bin/sh", NULL, &SpawnAttributes,
112 (void)posix_spawnattr_destroy(&SpawnAttributes);
114 pid_t SavedPid = Pid;
117 Pid = waitpid(SavedPid, &ProcessStatus, 0);
118 }
while (Pid == -1 && errno == EINTR);
123 }
else if (ErrorCode == ENOMEM || ErrorCode == EAGAIN) {
128 ProcessStatus = W_EXITCODE(127, 0);
130 for (
unsigned i = 0, n =
sizeof(Argv) /
sizeof(Argv[0]); i < n; ++i)
136 std::lock_guard<std::mutex>
Lock(SignalMutex);
138 if (ActiveThreadCount == 0) {
139 bool FailedRestore =
false;
140 if (sigaction(SIGINT, &OldSigIntAction, NULL) == -1) {
141 Printf(
"Failed to restore SIGINT handling\n");
142 FailedRestore =
true;
144 if (sigaction(SIGQUIT, &OldSigQuitAction, NULL) == -1) {
145 Printf(
"Failed to restore SIGQUIT handling\n");
146 FailedRestore =
true;
148 if (sigprocmask(SIG_BLOCK, &OldBlockedSignalsSet, NULL) == -1) {
149 Printf(
"Failed to unblock SIGCHLD\n");
150 FailedRestore =
true;
156 return ProcessStatus;
161 #endif // LIBFUZZER_APPLE
Printf(" # Score: %d, Used: %d\n", Scores[i], Usages[i])
int ExecuteCommand(const std::string &Command)