blob: af2b6f02d90453e8873a4f009396d7acab935436 [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 Swiecki81f16622017-09-28 18:01:45 +020043#include "libcommon/common.h"
Robert Swiecki2d016902017-05-24 02:12:30 +020044#include "libcommon/files.h"
45#include "libcommon/log.h"
Robert Swiecki2d016902017-05-24 02:12:30 +020046#include "libcommon/util.h"
Robert Swieckiec7b8452017-06-01 13:25:56 +020047#include "sanitizers.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 Swiecki4e595fb2017-10-11 17:26:51 +020051const char* subproc_StatusToStr(int status, char* str, size_t len)
Robert Swiecki0e673882016-03-31 18:50:29 +020052{
53 if (WIFEXITED(status)) {
54 snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status));
55 return str;
56 }
57
58 if (WIFSIGNALED(status)) {
Robert Swiecki0b566112017-10-17 17:39:07 +020059 snprintf(
60 str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), strsignal(WTERMSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020061 return str;
62 }
Robert Swiecki0e673882016-03-31 18:50:29 +020063 if (WIFCONTINUED(status)) {
64 snprintf(str, len, "CONTINUED");
65 return str;
66 }
67
68 if (!WIFSTOPPED(status)) {
69 snprintf(str, len, "UNKNOWN STATUS: %d", status);
70 return str;
71 }
72
73 /* Must be in a stopped state */
74 if (WSTOPSIG(status) == (SIGTRAP | 0x80)) {
75 snprintf(str, len, "STOPPED (linux syscall): %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020076 strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +020077 return str;
78 }
Robert Swiecki2531d752016-04-19 14:00:17 +020079#if defined(PTRACE_EVENT_STOP)
Robert Swiecki0e673882016-03-31 18:50:29 +020080#define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16)
81 if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) {
82 switch (__LINUX_WPTRACEEVENT(status)) {
Robert Swiecki2531d752016-04-19 14:00:17 +020083 case PTRACE_EVENT_FORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020084 snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020085 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020086 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020087 case PTRACE_EVENT_VFORK:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020088 snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020089 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020090 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020091 case PTRACE_EVENT_CLONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020092 snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020093 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020094 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020095 case PTRACE_EVENT_EXEC:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020096 snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +020097 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +020098 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +020099 case PTRACE_EVENT_VFORK_DONE:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200100 snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)",
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200101 WSTOPSIG(status), strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200102 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200103 case PTRACE_EVENT_EXIT:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200104 snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200105 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200106 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200107 case PTRACE_EVENT_SECCOMP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200108 snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200109 strsignal(WSTOPSIG(status)));
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200110 return str;
Robert Swiecki2531d752016-04-19 14:00:17 +0200111 case PTRACE_EVENT_STOP:
Robert Swieckicd3eb5b2016-03-31 18:55:38 +0200112 snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status),
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200113 strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +0200114 return str;
115 default:
116 snprintf(str, len, "EVENT (Linux) UNKNOWN (%d): with signal: %d (%s)",
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200117 __LINUX_WPTRACEEVENT(status), WSTOPSIG(status), strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +0200118 return str;
119 }
120 }
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200121#endif /* defined(PTRACE_EVENT_STOP) */
Robert Swiecki0e673882016-03-31 18:50:29 +0200122
Robert Swiecki0b566112017-10-17 17:39:07 +0200123 snprintf(
124 str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), strsignal(WSTOPSIG(status)));
Robert Swiecki0e673882016-03-31 18:50:29 +0200125 return str;
126}
Jaggerc7743512016-08-31 22:00:19 +0200127
Robert Swieckie7294ca2017-11-11 02:46:32 +0100128bool subproc_persistentModeRoundDone(honggfuzz_t* hfuzz, run_t* run)
Robert Swiecki940a0802016-11-02 12:42:09 +0100129{
Robert Swiecki059db072017-10-20 20:03:49 +0200130 if (!hfuzz->persistent) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100131 return false;
132 }
Robert Swieckibcfdb972016-11-23 20:33:41 +0100133 char z;
Robert Swieckie7294ca2017-11-11 02:46:32 +0100134 if (recv(run->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100135 LOG_D("Persistent mode round finished");
136 return true;
137 }
138 return false;
139}
140
Robert Swieckie7294ca2017-11-11 02:46:32 +0100141static bool subproc_persistentSendFile(run_t* run)
Robert Swiecki940a0802016-11-02 12:42:09 +0100142{
Robert Swieckie7294ca2017-11-11 02:46:32 +0100143 uint32_t len = (uint64_t)run->dynamicFileSz;
144 if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) {
Robert Swiecki2036e202017-06-06 00:01:44 +0200145 PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len));
Robert Swiecki940a0802016-11-02 12:42:09 +0100146 return false;
147 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100148 if (!files_sendToSocketNB(run->persistentSock, run->dynamicFile, run->dynamicFileSz)) {
149 PLOG_W("files_sendToSocketNB(len=%zu)", run->dynamicFileSz);
Robert Swiecki940a0802016-11-02 12:42:09 +0100150 return false;
151 }
152 return true;
153}
154
Robert Swieckie7294ca2017-11-11 02:46:32 +0100155bool subproc_PrepareExecv(honggfuzz_t* hfuzz, run_t* run, const char* fileName)
Jaggerc7743512016-08-31 22:00:19 +0200156{
157 /*
Jaggerc7743512016-08-31 22:00:19 +0200158 * The address space limit. If big enough - roughly the size of RAM used
159 */
160 if (hfuzz->asLimit) {
161 struct rlimit rl = {
162 .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL,
163 .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL,
164 };
165 if (setrlimit(RLIMIT_AS, &rl) == -1) {
166 PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring");
167 }
168 }
169
170 if (hfuzz->nullifyStdio) {
171 util_nullifyStdio();
172 }
173
174 if (hfuzz->fuzzStdin) {
175 /*
176 * Uglyyyyyy ;)
177 */
178 if (!util_redirectStdin(fileName)) {
179 return false;
180 }
181 }
182
183 if (hfuzz->clearEnv) {
184 environ = NULL;
185 }
Robert Swiecki059db072017-10-20 20:03:49 +0200186 if (!sanitizers_prepareExecve(hfuzz)) {
Anestis Bechtsoudise5f09f82016-12-27 16:06:05 +0200187 LOG_E("sanitizers_prepareExecve() failed");
Jaggerc7743512016-08-31 22:00:19 +0200188 return false;
189 }
190 for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) {
191 putenv(hfuzz->envs[i]);
192 }
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
199 if (hfuzz->bbFd != -1) {
200 if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) {
201 PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD);
202 }
Jaggerae663be2016-09-10 03:27:10 +0200203 close(hfuzz->bbFd);
Jaggerc7743512016-08-31 22:00:19 +0200204 }
205
Robert Swiecki491d7bb2017-02-28 03:02:53 +0100206 sigset_t sset;
207 sigemptyset(&sset);
208 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
209 PLOG_W("sigprocmask(empty_set)");
210 }
211
Jaggerc7743512016-08-31 22:00:19 +0200212 return true;
213}
Jagger93253f72016-09-01 22:40:12 +0200214
Robert Swieckie7294ca2017-11-11 02:46:32 +0100215static bool subproc_New(honggfuzz_t* hfuzz, run_t* run)
Jagger93253f72016-09-01 22:40:12 +0200216{
Robert Swieckie7294ca2017-11-11 02:46:32 +0100217 run->pid = run->persistentPid;
218 if (run->pid != 0) {
Jagger93253f72016-09-01 22:40:12 +0200219 return true;
220 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100221 run->tmOutSignaled = false;
Jagger93253f72016-09-01 22:40:12 +0200222
223 int sv[2];
224 if (hfuzz->persistent) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100225 if (run->persistentSock != -1) {
226 close(run->persistentSock);
Jagger82a20c02016-09-02 01:36:39 +0200227 }
228
Jaggere4dad652016-09-10 03:35:50 +0200229 int sock_type = SOCK_STREAM;
230#if defined(SOCK_CLOEXEC)
231 sock_type |= SOCK_CLOEXEC;
232#endif
233 if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) {
Jagger93253f72016-09-01 22:40:12 +0200234 PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)");
235 return false;
236 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100237 run->persistentSock = sv[0];
Jagger93253f72016-09-01 22:40:12 +0200238 }
239
Robert Swieckie7294ca2017-11-11 02:46:32 +0100240 run->pid = arch_fork(hfuzz, run);
241 if (run->pid == -1) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200242 PLOG_E("Couldn't fork");
243 return false;
Jagger93253f72016-09-01 22:40:12 +0200244 }
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200245 /* The child process */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100246 if (!run->pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200247 logMutexReset();
248 /*
249 * Reset sighandlers, and set alarm(1). It's a guarantee against dead-locks
250 * in the child, where we ensure here that the child process will either
251 * execve or get signaled by SIGALRM within 1 second.
Robert Swieckic337d4f2017-06-01 13:09:41 +0200252 *
253 * Those deadlocks typically stem from the fact, that malloc() can behave weirdly
254 * when fork()-ing a single thread of a process: e.g. with glibc < 2.24
255 * (or, Ubuntu's 2.23-0ubuntu6). For more see
256 * http://changelogs.ubuntu.com/changelogs/pool/main/g/glibc/glibc_2.23-0ubuntu7/changelog
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200257 */
258 alarm(1);
259 signal(SIGALRM, SIG_DFL);
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200260
Jagger93253f72016-09-01 22:40:12 +0200261 if (hfuzz->persistent) {
262 if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) {
263 PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD);
264 }
265 close(sv[0]);
266 close(sv[1]);
267 }
268
Robert Swieckie7294ca2017-11-11 02:46:32 +0100269 if (!subproc_PrepareExecv(hfuzz, run, run->fileName)) {
Jagger93253f72016-09-01 22:40:12 +0200270 LOG_E("subproc_PrepareExecv() failed");
271 exit(EXIT_FAILURE);
272 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100273 if (!arch_launchChild(hfuzz, run->fileName)) {
Robert Swieckia7ecf162017-02-12 23:17:41 +0100274 kill(hfuzz->mainPid, SIGTERM);
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200275 LOG_E("Error launching child process");
Robert Swieckia375f4b2017-06-01 13:20:54 +0200276 _exit(1);
Jagger93253f72016-09-01 22:40:12 +0200277 }
Robert Swiecki42cc9822017-02-12 22:20:46 +0100278 abort();
Jagger93253f72016-09-01 22:40:12 +0200279 }
Robert Swieckic337d4f2017-06-01 13:09:41 +0200280
281 /* Parent */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100282 LOG_D("Launched new process, pid: %d, (concurrency: %zd)", run->pid, hfuzz->threadsMax);
Jagger68f33592016-09-03 04:52:16 +0200283
Jagger93253f72016-09-01 22:40:12 +0200284 if (hfuzz->persistent) {
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300285 close(sv[1]);
Robert Swieckie7294ca2017-11-11 02:46:32 +0100286 LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid);
287 run->persistentPid = run->pid;
Robert Swiecki9415fe62017-06-05 23:10:39 +0200288
289 int sndbuf = hfuzz->maxFileSz + 256;
Robert Swieckie7294ca2017-11-11 02:46:32 +0100290 if (setsockopt(run->persistentSock, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf)) == -1) {
Robert Swiecki9415fe62017-06-05 23:10:39 +0200291 LOG_W("Couldn't set FD send buffer to '%d' bytes", sndbuf);
292 }
Anestis Bechtsoudis8171d522016-10-27 11:22:06 +0300293 }
Jagger93253f72016-09-01 22:40:12 +0200294
Robert Swieckie7294ca2017-11-11 02:46:32 +0100295 arch_prepareParentAfterFork(hfuzz, run);
Robert Swiecki810d2c92017-06-01 21:37:27 +0200296
Jagger93253f72016-09-01 22:40:12 +0200297 return true;
298}
Jaggerd1fc9ed2016-09-02 01:06:51 +0200299
Robert Swieckie7294ca2017-11-11 02:46:32 +0100300bool subproc_Run(honggfuzz_t* hfuzz, run_t* run)
Jaggerc4729592016-09-02 01:52:30 +0200301{
Robert Swieckie7294ca2017-11-11 02:46:32 +0100302 if (!subproc_New(hfuzz, run)) {
Robert Swiecki940a0802016-11-02 12:42:09 +0100303 LOG_E("subproc_New()");
Jaggerc4729592016-09-02 01:52:30 +0200304 return false;
305 }
Jaggerc4729592016-09-02 01:52:30 +0200306
Robert Swieckie7294ca2017-11-11 02:46:32 +0100307 arch_prepareParent(hfuzz, run);
308 if (hfuzz->persistent && !subproc_persistentSendFile(run)) {
Jaggerc4729592016-09-02 01:52:30 +0200309 LOG_W("Could not send file contents to the persistent process");
Robert Swieckie7294ca2017-11-11 02:46:32 +0100310 kill(run->persistentPid, SIGKILL);
Jaggerc4729592016-09-02 01:52:30 +0200311 }
Robert Swieckie7294ca2017-11-11 02:46:32 +0100312 arch_reapChild(hfuzz, run);
Robert Swiecki940a0802016-11-02 12:42:09 +0100313
314 return true;
Jaggerd1fc9ed2016-09-02 01:06:51 +0200315}
Robert Swiecki54333cd2016-09-07 17:51:48 +0200316
Robert Swieckie7294ca2017-11-11 02:46:32 +0100317uint8_t subproc_System(honggfuzz_t* hfuzz, run_t* run, const char* const argv[])
Robert Swiecki54333cd2016-09-07 17:51:48 +0200318{
Robert Swieckie7294ca2017-11-11 02:46:32 +0100319 pid_t pid = arch_fork(hfuzz, run);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200320 if (pid == -1) {
321 PLOG_E("Couldn't fork");
Robert Swieckib0a8d582016-09-09 18:13:00 +0200322 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200323 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200324 if (!pid) {
Robert Swiecki1c9d8092017-06-01 02:39:09 +0200325 logMutexReset();
Robert Swiecki5dbab882017-10-20 16:00:20 +0200326
327 sigset_t sset;
328 sigemptyset(&sset);
329 if (sigprocmask(SIG_SETMASK, &sset, NULL) == -1) {
330 PLOG_W("sigprocmask(empty_set)");
331 }
332
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200333 execv(argv[0], (char* const*)&argv[0]);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200334 PLOG_F("Couldn't execute '%s'", argv[0]);
Robert Swieckib0a8d582016-09-09 18:13:00 +0200335 return 255;
Robert Swiecki54333cd2016-09-07 17:51:48 +0200336 }
337
338 int status;
339 int flags = 0;
340#if defined(__WNOTHREAD)
341 flags |= __WNOTHREAD;
Robert Swiecki4e595fb2017-10-11 17:26:51 +0200342#endif /* defined(__WNOTHREAD) */
Robert Swiecki54333cd2016-09-07 17:51:48 +0200343
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200344 for (;;) {
345 int ret = wait4(pid, &status, flags, NULL);
346 if (ret == -1 && errno == EINTR) {
347 continue;
348 }
349 if (ret == -1) {
350 PLOG_E("wait4() for process PID: %d", (int)pid);
351 return 255;
352 }
353 if (ret != pid) {
354 LOG_E("wait4() returned %d, but waited for %d", ret, (int)pid);
355 return 255;
356 }
357 if (WIFSIGNALED(status)) {
358 LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status));
359 return (100 + WTERMSIG(status));
360 }
361 if (WIFEXITED(status)) {
362 if (WEXITSTATUS(status) == 0) {
363 return 0U;
364 }
365 LOG_E("Command '%s' returned with exit code %d", argv[0], WEXITSTATUS(status));
366 return 1U;
367 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200368
Robert Swiecki1ddf6742017-05-08 21:17:32 +0200369 LOG_D("wait4() returned with status: %d", status);
Robert Swiecki54333cd2016-09-07 17:51:48 +0200370 }
Robert Swiecki54333cd2016-09-07 17:51:48 +0200371}
Robert Swieckifddc0862016-10-27 12:42:46 +0200372
Robert Swieckie7294ca2017-11-11 02:46:32 +0100373void subproc_checkTimeLimit(honggfuzz_t* hfuzz, run_t* run)
Robert Swieckifddc0862016-10-27 12:42:46 +0200374{
375 if (hfuzz->tmOut == 0) {
376 return;
377 }
378
379 int64_t curMillis = util_timeNowMillis();
Robert Swieckie7294ca2017-11-11 02:46:32 +0100380 int64_t diffMillis = curMillis - run->timeStartedMillis;
Robert Swiecki013bc9c2016-12-12 17:31:06 +0100381
Robert Swieckie7294ca2017-11-11 02:46:32 +0100382 if (run->tmOutSignaled && (diffMillis > ((hfuzz->tmOut + 1) * 1000))) {
Robert Swiecki66d3e252017-07-06 01:53:40 +0200383 /* Has this instance been already signaled due to timeout? Just, SIGKILL it */
Robert Swieckie7294ca2017-11-11 02:46:32 +0100384 LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid);
385 kill(run->pid, SIGKILL);
Robert Swiecki66d3e252017-07-06 01:53:40 +0200386 return;
387 }
388
Robert Swieckie7294ca2017-11-11 02:46:32 +0100389 if ((diffMillis > (hfuzz->tmOut * 1000)) && !run->tmOutSignaled) {
390 run->tmOutSignaled = true;
391 LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid, hfuzz->tmOut,
392 hfuzz->tmout_vtalrm ? "SIGVTALRM" : "SIGKILL");
Robert Swieckie84b6452016-12-12 12:42:04 +0100393 if (hfuzz->tmout_vtalrm) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100394 kill(run->pid, SIGVTALRM);
Robert Swieckie84b6452016-12-12 12:42:04 +0100395 } else {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100396 kill(run->pid, SIGKILL);
Robert Swieckie84b6452016-12-12 12:42:04 +0100397 }
Robert Swieckifddc0862016-10-27 12:42:46 +0200398 ATOMIC_POST_INC(hfuzz->timeoutedCnt);
399 }
400}
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100401
Robert Swieckie7294ca2017-11-11 02:46:32 +0100402void subproc_checkTermination(honggfuzz_t* hfuzz, run_t* run)
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100403{
404 if (ATOMIC_GET(hfuzz->terminating)) {
Robert Swieckie7294ca2017-11-11 02:46:32 +0100405 LOG_D("Killing PID: %d", (int)run->pid);
406 kill(run->pid, SIGKILL);
Robert Swiecki4f09ce02017-02-25 22:47:55 +0100407 }
408}