blob: 32c1110e268a574ef32c2d32276c543b0cd70fda [file] [log] [blame]
Robert Swiecki0e673882016-03-31 18:50:29 +02001/*
2 *
3 * honggfuzz - routines dealing with subprocesses
4 * -----------------------------------------
5 *
6 * Author:
7 * Robert Swiecki <swiecki@google.com>
8 * Felix Gröbert <groebert@google.com>
9 *
10 * Copyright 2010-2015 by Google Inc. All Rights Reserved.
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License"); you may
13 * not use this file except in compliance with the License. You may obtain
14 * a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
21 * implied. See the License for the specific language governing
22 * permissions and limitations under the License.
23 *
24 */
25
Robert Swiecki0e673882016-03-31 18:50:29 +020026#include "subproc.h"
27
Robert Swiecki1ddf6742017-05-08 21:17:32 +020028#include <errno.h>
Jagger93253f72016-09-01 22:40:12 +020029#include <fcntl.h>
Jaggerc7743512016-08-31 22:00:19 +020030#include <inttypes.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020031#include <signal.h>
32#include <stdio.h>
Jaggerc7743512016-08-31 22:00:19 +020033#include <stdlib.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020034#include <string.h>
Jaggerc7743512016-08-31 22:00:19 +020035#include <sys/resource.h>
Jagger93253f72016-09-01 22:40:12 +020036#include <sys/socket.h>
Jaggerc7743512016-08-31 22:00:19 +020037#include <sys/time.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020038#include <sys/types.h>
39#include <sys/wait.h>
Jaggerc7743512016-08-31 22:00:19 +020040#include <unistd.h>
Robert Swiecki0e673882016-03-31 18:50:29 +020041
Robert Swieckid0fa62c2017-09-28 18:11:05 +020042#include "arch.h"
Robert Swiecki35978ac2017-11-16 18:00:53 +010043#include "fuzz.h"
Robert Swiecki246af3e2018-01-05 14:56:32 +010044#include "libhfcommon/common.h"
45#include "libhfcommon/files.h"
46#include "libhfcommon/log.h"
47#include "libhfcommon/util.h"
Jaggerc7743512016-08-31 22:00:19 +020048
Robert Swiecki4e595fb2017-10-11 17:26:51 +020049extern char** environ;
Robert Swiecki0e673882016-03-31 18:50:29 +020050
Robert Swieckid50ed422017-11-13 23:32:26 +010051const char* subproc_StatusToStr(int status, char* str, size_t len) {
Robert Swiecki0e673882016-03-31 18:50:29 +020052 if (WIFEXITED(status)) {
53 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
54 return str;
55 }
56
57 if (WIFSIGNALED(status)) {
Robert Swiecki0b566112017-10-17 17:39:07 +020058 snprintf(
59 str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020060 return str;
61 }
Robert Swiecki0e673882016-03-31 18:50:29 +020062 if (WIFCONTINUED(status)) {
63 snprintf(str, len, "CONTINUED");
64 return str;
65 }
66
67 if (!WIFSTOPPED(status)) {
68 snprintf(str, len, "UNKNOWN STATUS: %d", status);
69 return str;
70 }
71
72 /* Must be in a stopped state */
73 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
74 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020075 strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020076 return str;
77 }
Robert Swiecki2531d752016-04-19 14:00:17 +020078#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020079#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
80 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
81 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swieckid50ed422017-11-13 23:32:26 +010082 case PTRACE_EVENT_FORK:
83 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
84 strsignal(WSTOPSIG(status)));
85 return str;
86 case PTRACE_EVENT_VFORK:
87 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
88 strsignal(WSTOPSIG(status)));
89 return str;
90 case PTRACE_EVENT_CLONE:
91 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
92 strsignal(WSTOPSIG(status)));
93 return str;
94 case PTRACE_EVENT_EXEC:
95 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
96 strsignal(WSTOPSIG(status)));
97 return str;
98 case PTRACE_EVENT_VFORK_DONE:
99 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
100 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
101 return str;
102 case PTRACE_EVENT_EXIT:
103 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
104 strsignal(WSTOPSIG(status)));
105 return str;
106 case PTRACE_EVENT_SECCOMP:
107 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)",
108 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
109 return str;
110 case PTRACE_EVENT_STOP:
111 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
112 strsignal(WSTOPSIG(status)));
113 return str;
114 default:
115 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
116 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
117 return str;
Robert Swiecki0e673882016-03-31 18:50:29 +0200118 }
119 }
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200120#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200121
Robert Swiecki0b566112017-10-17 17:39:07 +0200122 snprintf(
123 str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +0200124 return str;
125}
Jaggerc7743512016-08-31 22:00:19 +0200126
Robert Swieckid50ed422017-11-13 23:32:26 +0100127bool subproc_persistentModeRoundDone(run_t* run) {
Robert Swiecki78633d12017-11-13 23:24:55 +0100128 if (!run->global->persistent) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100129 return false;
130 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100131 uint8_t rcv;
132 if (recv(run->persistentSock, &rcv, sizeof(rcv), MSG_DONTWAIT) != sizeof(rcv)) {
133 return false;
134 }
135 if (rcv == HFdoneTag) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100136 return true;
137 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100138 LOG_F("Received invalid message from the persistent process: '%c' (0x%" PRIx8
139 ") , expected '%c' (0x%" PRIx8 ")",
140 rcv, rcv, HFdoneTag, HFdoneTag);
141 return false;
142}
143
Robert Swieckid50ed422017-11-13 23:32:26 +0100144static bool subproc_persistentSendFile(run_t* run) {
Robert Swiecki599dee12018-01-10 02:21:58 +0100145 uint64_t len = (uint64_t)run->dynamicFileSz;
Robert Swieckie7294ca2017-11-11 02:46:32 +0100146 if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) {
Robert Swiecki2036e202017-06-06 00:01:44 +0200147 PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len));
Robert Swiecki940a0802016-11-02 12:42:09 +0100148 return false;
149 }
Robert Swiecki940a0802016-11-02 12:42:09 +0100150 return true;
151}
152
Robert Swiecki599dee12018-01-10 02:21:58 +0100153static bool subproc_PrepareExecv(run_t* run) {
Jaggerc7743512016-08-31 22:00:19 +0200154 /*
Jaggerc7743512016-08-31 22:00:19 +0200155 * The address space limit. If big enough - roughly the size of RAM used
156 */
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100157 if (run->global->exe.asLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100158 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100159 .rlim_cur = run->global->exe.asLimit * 1024ULL * 1024ULL,
160 .rlim_max = run->global->exe.asLimit * 1024ULL * 1024ULL,
Jaggerc7743512016-08-31 22:00:19 +0200161 };
162 if (setrlimit(RLIMIT_AS, &rl) == -1) {
Robert Swiecki8954afd2017-11-14 18:14:22 +0100163 PLOG_W("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
164 }
165 }
Robert Swieckicd238692017-11-27 16:48:19 +0100166#if defined(RLIMIT_RSS)
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100167 if (run->global->exe.rssLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100168 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100169 .rlim_cur = run->global->exe.rssLimit * 1024ULL * 1024ULL,
170 .rlim_max = run->global->exe.rssLimit * 1024ULL * 1024ULL,
Robert Swiecki8954afd2017-11-14 18:14:22 +0100171 };
172 if (setrlimit(RLIMIT_RSS, &rl) == -1) {
173 PLOG_W("Couldn't enforce the RLIMIT_RSS resource limit, ignoring");
174 }
175 }
Robert Swieckicd238692017-11-27 16:48:19 +0100176#endif /* defined(RLIMIT_RSS) */
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100177 if (run->global->exe.dataLimit) {
Robert Swieckidc8047c2018-01-07 21:18:26 +0100178 const struct rlimit rl = {
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100179 .rlim_cur = run->global->exe.dataLimit * 1024ULL * 1024ULL,
180 .rlim_max = run->global->exe.dataLimit * 1024ULL * 1024ULL,
Robert Swiecki8954afd2017-11-14 18:14:22 +0100181 };
Robert Swiecki1f9f2132017-11-27 16:49:37 +0100182 if (setrlimit(RLIMIT_DATA, &rl) == -1) {
Robert Swiecki8954afd2017-11-14 18:14:22 +0100183 PLOG_W("Couldn't enforce the RLIMIT_DATA resource limit, ignoring");
Jaggerc7743512016-08-31 22:00:19 +0200184 }
185 }
186
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100187 if (run->global->exe.clearEnv) {
Jaggerc7743512016-08-31 22:00:19 +0200188 environ = NULL;
189 }
Robert Swiecki97d0cee2017-12-18 00:17:50 +0100190 for (size_t i = 0; i < ARRAYSIZE(run->global->exe.envs) && run->global->exe.envs[i]; i++) {
191 putenv(run->global->exe.envs[i]);
Jaggerc7743512016-08-31 22:00:19 +0200192 }
193 char fuzzNo[128];
Robert Swieckie7294ca2017-11-11 02:46:32 +0100194 snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, run->fuzzNo);
Jaggerc7743512016-08-31 22:00:19 +0200195 setenv(_HF_THREAD_NO_ENV, fuzzNo, 1);
196
197 setsid();
198
Robert Swiecki78633d12017-11-13 23:24:55 +0100199 if (run->global->bbFd != -1) {
200 if (dup2(run->global->bbFd, _HF_BITMAP_FD) == -1) {
Robert Swiecki2aeff252018-01-10 14:58:44 +0100201 PLOG_E("dup2('%d', %d)", run->global->bbFd, _HF_BITMAP_FD);
202 return false;
Jaggerc7743512016-08-31 22:00:19 +0200203 }
204 }
205
Robert Swiecki0a01ea72018-01-11 01:50:18 +0100206 /* Step 1: dup the input file to _HF_INPUT_FD */
Robert Swiecki599dee12018-01-10 02:21:58 +0100207 if (dup2(run->dynamicFileFd, _HF_INPUT_FD) == -1) {
Robert Swiecki0a01ea72018-01-11 01:50:18 +0100208 PLOG_E("dup2('%d', _HF_INPUT_FD='%d')", run->dynamicFileFd, _HF_INPUT_FD);
209 return false;
210 }
211 /*
212 * Step 2: map _HF_INPUT_FD back to run->dynamicFileFd, in order to remove potential O_CLOEXEC
213 * from run->dynamicFileFd
214 */
215 if (dup2(_HF_INPUT_FD, run->dynamicFileFd) == -1) {
216 PLOG_E("dup2(_HF_INPUT_FD='%d', '%d')", _HF_INPUT_FD, run->dynamicFileFd);
217 return false;
Robert Swiecki599dee12018-01-10 02:21:58 +0100218 }
Robert Swiecki599dee12018-01-10 02:21:58 +0100219
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100220 sigset_t sset;
221 sigemptyset(&sset);
222 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
223 PLOG_W("sigprocmask(empty_set)");
224 }
225
Robert Swieckic8a95362018-01-10 02:29:49 +0100226 if (run->global->exe.nullifyStdio) {
227 util_nullifyStdio();
228 }
Robert Swiecki0a01ea72018-01-11 01:50:18 +0100229 if (run->global->exe.fuzzStdin && dup2(_HF_INPUT_FD, STDIN_FILENO) == -1) {
230 PLOG_E("dup2(_HF_INPUT_FD=%d, STDIN_FILENO=%d)", _HF_INPUT_FD, STDIN_FILENO);
Robert Swiecki9a753962018-01-10 04:45:24 +0100231 return false;
Robert Swiecki3975afe2018-01-10 04:32:21 +0100232 }
Robert Swieckic8a95362018-01-10 02:29:49 +0100233
Jaggerc7743512016-08-31 22:00:19 +0200234 return true;
235}
Jagger93253f72016-09-01 22:40:12 +0200236
Robert Swieckid50ed422017-11-13 23:32:26 +0100237static bool subproc_New(run_t* run) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100238 run->pid = run->persistentPid;
239 if (run->pid != 0) {
Jagger93253f72016-09-01 22:40:12 +0200240 return true;
241 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100242 run->tmOutSignaled = false;
Jagger93253f72016-09-01 22:40:12 +0200243
244 int sv[2];
Robert Swiecki78633d12017-11-13 23:24:55 +0100245 if (run->global->persistent) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100246 if (run->persistentSock != -1) {
247 close(run->persistentSock);
Jagger82a20c02016-09-02 01:36:39 +0200248 }
249
Jaggere4dad652016-09-10 03:35:50 +0200250 int sock_type = SOCK_STREAM;
251#if defined(SOCK_CLOEXEC)
252 sock_type |= SOCK_CLOEXEC;
253#endif
254 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200255 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
256 return false;
257 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100258 run->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200259 }
260
Robert Swiecki158fe042018-01-10 02:33:56 +0100261 LOG_D("Forking new process for thread: %" PRId32, run->fuzzNo);
262
Robert Swiecki78633d12017-11-13 23:24:55 +0100263 run->pid = arch_fork(run);
Robert Swieckie7294ca2017-11-11 02:46:32 +0100264 if (run->pid == -1) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200265 PLOG_E("Couldn't fork");
266 return false;
Jagger93253f72016-09-01 22:40:12 +0200267 }
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200268 /* The child process */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100269 if (!run->pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200270 logMutexReset();
271 /*
272 * Reset sighandlers, and set alarm(1). It's a guarantee against dead-locks
273 * in the child, where we ensure here that the child process will either
274 * execve or get signaled by SIGALRM within 1 second.
Robert Swieckic337d4f2017-06-01 13:09:41 +0200275 *
276 * Those deadlocks typically stem from the fact, that malloc() can behave weirdly
277 * when fork()-ing a single thread of a process: e.g. with glibc < 2.24
278 * (or, Ubuntu's 2.23-0ubuntu6). For more see
279 * http://changelogs.ubuntu.com/changelogs/pool/main/g/glibc/glibc_2.23-0ubuntu7/changelog
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200280 */
281 alarm(1);
282 signal(SIGALRM, SIG_DFL);
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200283
Robert Swiecki78633d12017-11-13 23:24:55 +0100284 if (run->global->persistent) {
Jagger93253f72016-09-01 22:40:12 +0200285 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
286 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
287 }
288 close(sv[0]);
289 close(sv[1]);
290 }
291
Robert Swiecki599dee12018-01-10 02:21:58 +0100292 if (!subproc_PrepareExecv(run)) {
Jagger93253f72016-09-01 22:40:12 +0200293 LOG_E("subproc_PrepareExecv() failed");
294 exit(EXIT_FAILURE);
295 }
Robert Swiecki78633d12017-11-13 23:24:55 +0100296 if (!arch_launchChild(run)) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200297 LOG_E("Error launching child process");
Robert Swiecki82c707c2017-11-14 16:36:23 +0100298 kill(run->global->threads.mainPid, SIGTERM);
Robert Swieckia375f4b2017-06-01 13:20:54 +0200299 _exit(1);
Jagger93253f72016-09-01 22:40:12 +0200300 }
Robert Swiecki42cc9822017-02-12 22:20:46 +0100301 abort();
Jagger93253f72016-09-01 22:40:12 +0200302 }
Robert Swieckic337d4f2017-06-01 13:09:41 +0200303
304 /* Parent */
Robert Swiecki158fe042018-01-10 02:33:56 +0100305 LOG_D("Launched new process, PID: %d, thread: %" PRId32 " (concurrency: %zd)", run->pid,
306 run->fuzzNo, run->global->threads.threadsMax);
Jagger68f33592016-09-03 04:52:16 +0200307
Robert Swiecki78633d12017-11-13 23:24:55 +0100308 if (run->global->persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300309 close(sv[1]);
Robert Swieckie7294ca2017-11-11 02:46:32 +0100310 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid);
311 run->persistentPid = run->pid;
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300312 }
Jagger93253f72016-09-01 22:40:12 +0200313
Robert Swiecki78633d12017-11-13 23:24:55 +0100314 arch_prepareParentAfterFork(run);
Robert Swiecki810d2c92017-06-01 21:37:27 +0200315
Jagger93253f72016-09-01 22:40:12 +0200316 return true;
317}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200318
Robert Swieckid50ed422017-11-13 23:32:26 +0100319bool subproc_Run(run_t* run) {
Robert Swiecki0a01ea72018-01-11 01:50:18 +0100320 /* Truncate input file to the desired size */
321 if (ftruncate(run->dynamicFileFd, run->dynamicFileSz) == -1) {
322 PLOG_E("ftruncate(fd=%d, size=%zu)", run->dynamicFileFd, run->dynamicFileSz);
323 return false;
324 }
325
Robert Swiecki78633d12017-11-13 23:24:55 +0100326 if (!subproc_New(run)) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100327 LOG_E("subproc_New()");
Jaggerc4729592016-09-02 01:52:30 +0200328 return false;
329 }
Jaggerc4729592016-09-02 01:52:30 +0200330
Robert Swiecki78633d12017-11-13 23:24:55 +0100331 arch_prepareParent(run);
Robert Swiecki599dee12018-01-10 02:21:58 +0100332
Robert Swiecki78633d12017-11-13 23:24:55 +0100333 if (run->global->persistent && !subproc_persistentSendFile(run)) {
Robert Swiecki599dee12018-01-10 02:21:58 +0100334 LOG_W("Could not send file size to the persistent process");
Robert Swieckie7294ca2017-11-11 02:46:32 +0100335 kill(run->persistentPid, SIGKILL);
Jaggerc4729592016-09-02 01:52:30 +0200336 }
Robert Swiecki78633d12017-11-13 23:24:55 +0100337 arch_reapChild(run);
Robert Swiecki940a0802016-11-02 12:42:09 +0100338
339 return true;
Jaggerd1fc9ed2016-09-02 01:06:51 +0200340}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200341
Robert Swieckid50ed422017-11-13 23:32:26 +0100342uint8_t subproc_System(run_t* run, const char* const argv[]) {
Robert Swiecki78633d12017-11-13 23:24:55 +0100343 pid_t pid = arch_fork(run);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200344 if (pid == -1) {
345 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200346 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200347 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200348 if (!pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200349 logMutexReset();
Robert Swiecki5dbab882017-10-20 16:00:20 +0200350
351 sigset_t sset;
352 sigemptyset(&sset);
353 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
354 PLOG_W("sigprocmask(empty_set)");
355 }
356
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200357 execv(argv[0], (char* const*)&argv[0]);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200358 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200359 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200360 }
361
362 int status;
363 int flags = 0;
364#if defined(__WNOTHREAD)
365 flags |= __WNOTHREAD;
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200366#endif /* defined(__WNOTHREAD) */
Robert Swiecki54333cd2016-09-07 17:51:48 +0200367
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200368 for (;;) {
369 int ret = wait4(pid, &status, flags, NULL);
370 if (ret == -1 && errno == EINTR) {
371 continue;
372 }
373 if (ret == -1) {
374 PLOG_E("wait4() for process PID: %d", (int)pid);
375 return 255;
376 }
377 if (ret != pid) {
378 LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid);
379 return 255;
380 }
381 if (WIFSIGNALED(status)) {
382 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
383 return (100 + WTERMSIG(status));
384 }
385 if (WIFEXITED(status)) {
386 if (WEXITSTATUS(status) == 0) {
387 return 0U;
388 }
389 LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status));
390 return 1U;
391 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200392
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200393 LOG_D("wait4() returned with status: %d", status);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200394 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200395}
Robert Swieckifddc0862016-10-27 12:42:46 +0200396
Robert Swieckid50ed422017-11-13 23:32:26 +0100397void subproc_checkTimeLimit(run_t* run) {
Robert Swiecki371e1292017-12-18 01:10:33 +0100398 if (run->global->timing.tmOut == 0) {
Robert Swieckifddc0862016-10-27 12:42:46 +0200399 return;
400 }
401
402 int64_t curMillis = util_timeNowMillis();
Robert Swieckie7294ca2017-11-11 02:46:32 +0100403 int64_t diffMillis = curMillis - run->timeStartedMillis;
Robert Swiecki013bc9c2016-12-12 17:31:06 +0100404
Robert Swiecki371e1292017-12-18 01:10:33 +0100405 if (run->tmOutSignaled && (diffMillis > ((run->global->timing.tmOut + 1) * 1000))) {
Robert Swiecki66d3e252017-07-06 01:53:40 +0200406 /* Has this instance been already signaled due to timeout? Just, SIGKILL it */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100407 LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid);
408 kill(run->pid, SIGKILL);
Robert Swiecki66d3e252017-07-06 01:53:40 +0200409 return;
410 }
411
Robert Swiecki371e1292017-12-18 01:10:33 +0100412 if ((diffMillis > (run->global->timing.tmOut * 1000)) && !run->tmOutSignaled) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100413 run->tmOutSignaled = true;
Robert Swiecki78633d12017-11-13 23:24:55 +0100414 LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid,
Robert Swieckieba27172017-12-18 01:12:02 +0100415 run->global->timing.tmOut, run->global->timing.tmoutVTALRM ? "SIGVTALRM" : "SIGKILL");
416 if (run->global->timing.tmoutVTALRM) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100417 kill(run->pid, SIGVTALRM);
Robert Swieckie84b6452016-12-12 12:42:04 +0100418 } else {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100419 kill(run->pid, SIGKILL);
Robert Swieckie84b6452016-12-12 12:42:04 +0100420 }
Robert Swiecki2542dc02017-11-14 03:35:59 +0100421 ATOMIC_POST_INC(run->global->cnts.timeoutedCnt);
Robert Swieckifddc0862016-10-27 12:42:46 +0200422 }
423}
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100424
Robert Swieckid50ed422017-11-13 23:32:26 +0100425void subproc_checkTermination(run_t* run) {
Robert Swiecki0dde76d2017-11-16 19:25:44 +0100426 if (fuzz_isTerminating()) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100427 LOG_D("Killing PID: %d", (int)run->pid);
428 kill(run->pid, SIGKILL);
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100429 }
430}