Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 26 | #include "common.h" |
| 27 | #include "subproc.h" |
| 28 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 29 | #include <fcntl.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 30 | #include <inttypes.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 31 | #include <signal.h> |
| 32 | #include <stdio.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 33 | #include <stdlib.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 34 | #include <string.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 35 | #include <sys/resource.h> |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 36 | #include <sys/socket.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 37 | #include <sys/time.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 38 | #include <sys/types.h> |
| 39 | #include <sys/wait.h> |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 40 | #include <unistd.h> |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 41 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 42 | #include "arch.h" |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 43 | #include "files.h" |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 44 | #include "log.h" |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 45 | #include "sancov.h" |
| 46 | #include "util.h" |
| 47 | |
| 48 | extern char **environ; |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 49 | |
| 50 | const char *subproc_StatusToStr(int status, char *str, size_t len) |
| 51 | { |
| 52 | if (WIFEXITED(status)) { |
| 53 | snprintf(str, len, "EXITED, exit code: %d", WEXITSTATUS(status)); |
| 54 | return str; |
| 55 | } |
| 56 | |
| 57 | if (WIFSIGNALED(status)) { |
| 58 | snprintf(str, len, "SIGNALED, signal: %d (%s)", WTERMSIG(status), |
| 59 | strsignal(WTERMSIG(status))); |
| 60 | return str; |
| 61 | } |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 62 | 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), |
| 75 | strsignal(WSTOPSIG(status))); |
| 76 | return str; |
| 77 | } |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 78 | #if defined(PTRACE_EVENT_STOP) |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 79 | #define __LINUX_WPTRACEEVENT(x) ((x & 0xff0000) >> 16) |
| 80 | if (WSTOPSIG(status) == SIGTRAP && __LINUX_WPTRACEEVENT(status) != 0) { |
| 81 | switch (__LINUX_WPTRACEEVENT(status)) { |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 82 | case PTRACE_EVENT_FORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 83 | snprintf(str, len, "EVENT (Linux) - fork - with signal: %d (%s)", WSTOPSIG(status), |
| 84 | strsignal(WSTOPSIG(status))); |
| 85 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 86 | case PTRACE_EVENT_VFORK: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 87 | snprintf(str, len, "EVENT (Linux) - vfork - with signal: %d (%s)", WSTOPSIG(status), |
| 88 | strsignal(WSTOPSIG(status))); |
| 89 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 90 | case PTRACE_EVENT_CLONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 91 | snprintf(str, len, "EVENT (Linux) - clone - with signal: %d (%s)", WSTOPSIG(status), |
| 92 | strsignal(WSTOPSIG(status))); |
| 93 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 94 | case PTRACE_EVENT_EXEC: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 95 | snprintf(str, len, "EVENT (Linux) - exec - with signal: %d (%s)", WSTOPSIG(status), |
| 96 | strsignal(WSTOPSIG(status))); |
| 97 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 98 | case PTRACE_EVENT_VFORK_DONE: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 99 | snprintf(str, len, "EVENT (Linux) - vfork_done - with signal: %d (%s)", |
| 100 | WSTOPSIG(status), strsignal(WSTOPSIG(status))); |
| 101 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 102 | case PTRACE_EVENT_EXIT: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 103 | snprintf(str, len, "EVENT (Linux) - exit - with signal: %d (%s)", WSTOPSIG(status), |
| 104 | strsignal(WSTOPSIG(status))); |
| 105 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 106 | case PTRACE_EVENT_SECCOMP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 107 | snprintf(str, len, "EVENT (Linux) - seccomp - with signal: %d (%s)", WSTOPSIG(status), |
| 108 | strsignal(WSTOPSIG(status))); |
| 109 | return str; |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 110 | case PTRACE_EVENT_STOP: |
Robert Swiecki | cd3eb5b | 2016-03-31 18:55:38 +0200 | [diff] [blame] | 111 | snprintf(str, len, "EVENT (Linux) - stop - with signal: %d (%s)", WSTOPSIG(status), |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 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; |
| 118 | } |
| 119 | } |
Robert Swiecki | 2531d75 | 2016-04-19 14:00:17 +0200 | [diff] [blame] | 120 | #endif /* defined(PTRACE_EVENT_STOP) */ |
Robert Swiecki | 0e67388 | 2016-03-31 18:50:29 +0200 | [diff] [blame] | 121 | |
| 122 | snprintf(str, len, "STOPPED with signal: %d (%s)", WSTOPSIG(status), |
| 123 | strsignal(WSTOPSIG(status))); |
| 124 | return str; |
| 125 | } |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 126 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 127 | bool subproc_persistentModeRoundDone(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 128 | { |
| 129 | if (hfuzz->persistent == false) { |
| 130 | return false; |
| 131 | } |
Robert Swiecki | bcfdb97 | 2016-11-23 20:33:41 +0100 | [diff] [blame] | 132 | char z; |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 133 | if (recv(fuzzer->persistentSock, &z, sizeof(z), MSG_DONTWAIT) == sizeof(z)) { |
| 134 | LOG_D("Persistent mode round finished"); |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | static bool subproc_persistentSendFile(fuzzer_t * fuzzer) |
| 141 | { |
| 142 | uint32_t len = (uint64_t) fuzzer->dynamicFileSz; |
| 143 | if (files_writeToFd(fuzzer->persistentSock, (uint8_t *) & len, sizeof(len)) == false) { |
| 144 | return false; |
| 145 | } |
| 146 | if (files_writeToFd(fuzzer->persistentSock, fuzzer->dynamicFile, fuzzer->dynamicFileSz) == |
| 147 | false) { |
| 148 | return false; |
| 149 | } |
| 150 | return true; |
| 151 | } |
| 152 | |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 153 | bool subproc_PrepareExecv(honggfuzz_t * hfuzz, fuzzer_t * fuzzer, const char *fileName) |
| 154 | { |
| 155 | /* |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 156 | * The address space limit. If big enough - roughly the size of RAM used |
| 157 | */ |
| 158 | if (hfuzz->asLimit) { |
| 159 | struct rlimit rl = { |
| 160 | .rlim_cur = hfuzz->asLimit * 1024ULL * 1024ULL, |
| 161 | .rlim_max = hfuzz->asLimit * 1024ULL * 1024ULL, |
| 162 | }; |
| 163 | if (setrlimit(RLIMIT_AS, &rl) == -1) { |
| 164 | PLOG_D("Couldn't enforce the RLIMIT_AS resource limit, ignoring"); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (hfuzz->nullifyStdio) { |
| 169 | util_nullifyStdio(); |
| 170 | } |
| 171 | |
| 172 | if (hfuzz->fuzzStdin) { |
| 173 | /* |
| 174 | * Uglyyyyyy ;) |
| 175 | */ |
| 176 | if (!util_redirectStdin(fileName)) { |
| 177 | return false; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | if (hfuzz->clearEnv) { |
| 182 | environ = NULL; |
| 183 | } |
Anestis Bechtsoudis | e5f09f8 | 2016-12-27 16:06:05 +0200 | [diff] [blame] | 184 | if (sanitizers_prepareExecve(hfuzz) == false) { |
| 185 | LOG_E("sanitizers_prepareExecve() failed"); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 186 | return false; |
| 187 | } |
| 188 | for (size_t i = 0; i < ARRAYSIZE(hfuzz->envs) && hfuzz->envs[i]; i++) { |
| 189 | putenv(hfuzz->envs[i]); |
| 190 | } |
| 191 | char fuzzNo[128]; |
| 192 | snprintf(fuzzNo, sizeof(fuzzNo), "%" PRId32, fuzzer->fuzzNo); |
| 193 | setenv(_HF_THREAD_NO_ENV, fuzzNo, 1); |
| 194 | |
| 195 | setsid(); |
| 196 | |
| 197 | if (hfuzz->bbFd != -1) { |
| 198 | if (dup2(hfuzz->bbFd, _HF_BITMAP_FD) == -1) { |
| 199 | PLOG_F("dup2('%d', %d)", hfuzz->bbFd, _HF_BITMAP_FD); |
| 200 | } |
Jagger | ae663be | 2016-09-10 03:27:10 +0200 | [diff] [blame] | 201 | close(hfuzz->bbFd); |
Jagger | c774351 | 2016-08-31 22:00:19 +0200 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | return true; |
| 205 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 206 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 207 | static bool subproc_New(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 208 | { |
| 209 | fuzzer->pid = fuzzer->persistentPid; |
| 210 | if (fuzzer->pid != 0) { |
| 211 | return true; |
| 212 | } |
Gergely Nagy | f95cda6 | 2016-12-12 23:40:16 +0100 | [diff] [blame] | 213 | fuzzer->tmOutSignaled = false; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 214 | |
| 215 | int sv[2]; |
| 216 | if (hfuzz->persistent) { |
Jagger | 82a20c0 | 2016-09-02 01:36:39 +0200 | [diff] [blame] | 217 | if (fuzzer->persistentSock != -1) { |
| 218 | close(fuzzer->persistentSock); |
| 219 | } |
| 220 | |
Jagger | e4dad65 | 2016-09-10 03:35:50 +0200 | [diff] [blame] | 221 | int sock_type = SOCK_STREAM; |
| 222 | #if defined(SOCK_CLOEXEC) |
| 223 | sock_type |= SOCK_CLOEXEC; |
| 224 | #endif |
| 225 | if (socketpair(AF_UNIX, sock_type, 0, sv) == -1) { |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 226 | PLOG_W("socketpair(AF_UNIX, SOCK_STREAM, 0, sv)"); |
| 227 | return false; |
| 228 | } |
Robert Swiecki | c7a95f8 | 2016-10-27 19:29:35 +0200 | [diff] [blame] | 229 | fuzzer->persistentSock = sv[0]; |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | fuzzer->pid = arch_fork(hfuzz, fuzzer); |
| 233 | if (fuzzer->pid == -1) { |
| 234 | PLOG_F("Couldn't fork"); |
| 235 | } |
Robert Swiecki | a71a499 | 2016-09-02 14:47:55 +0200 | [diff] [blame] | 236 | // Child |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 237 | if (!fuzzer->pid) { |
| 238 | if (hfuzz->persistent) { |
| 239 | if (dup2(sv[1], _HF_PERSISTENT_FD) == -1) { |
| 240 | PLOG_F("dup2('%d', '%d')", sv[1], _HF_PERSISTENT_FD); |
| 241 | } |
| 242 | close(sv[0]); |
| 243 | close(sv[1]); |
| 244 | } |
| 245 | |
| 246 | if (!subproc_PrepareExecv(hfuzz, fuzzer, fuzzer->fileName)) { |
| 247 | LOG_E("subproc_PrepareExecv() failed"); |
| 248 | exit(EXIT_FAILURE); |
| 249 | } |
| 250 | if (!arch_launchChild(hfuzz, fuzzer->fileName)) { |
| 251 | LOG_E("Error launching child process"); |
Robert Swiecki | a7ecf16 | 2017-02-12 23:17:41 +0100 | [diff] [blame] | 252 | kill(hfuzz->mainPid, SIGTERM); |
Robert Swiecki | 9907100 | 2017-02-13 17:16:18 +0100 | [diff] [blame] | 253 | exit(EXIT_FAILURE); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 254 | } |
Robert Swiecki | 42cc982 | 2017-02-12 22:20:46 +0100 | [diff] [blame] | 255 | abort(); |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 256 | } |
Robert Swiecki | a71a499 | 2016-09-02 14:47:55 +0200 | [diff] [blame] | 257 | // Parent |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 258 | LOG_D("Launched new process, pid: %d, (concurrency: %zd)", fuzzer->pid, hfuzz->threadsMax); |
Jagger | 68f3359 | 2016-09-03 04:52:16 +0200 | [diff] [blame] | 259 | |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 260 | if (hfuzz->persistent) { |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 261 | close(sv[1]); |
Robert Swiecki | 2a7da34 | 2016-10-27 19:27:30 +0200 | [diff] [blame] | 262 | LOG_I("Persistent mode: Launched new persistent PID: %d", (int)fuzzer->pid); |
| 263 | fuzzer->persistentPid = fuzzer->pid; |
Anestis Bechtsoudis | 8171d52 | 2016-10-27 11:22:06 +0300 | [diff] [blame] | 264 | } |
Jagger | 93253f7 | 2016-09-01 22:40:12 +0200 | [diff] [blame] | 265 | |
| 266 | return true; |
| 267 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 268 | |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 269 | bool subproc_Run(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 270 | { |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 271 | if (subproc_New(hfuzz, fuzzer) == false) { |
| 272 | LOG_E("subproc_New()"); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 273 | return false; |
| 274 | } |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 275 | |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 276 | arch_prepareChild(hfuzz, fuzzer); |
Jagger | c472959 | 2016-09-02 01:52:30 +0200 | [diff] [blame] | 277 | if (hfuzz->persistent == true && subproc_persistentSendFile(fuzzer) == false) { |
| 278 | LOG_W("Could not send file contents to the persistent process"); |
| 279 | } |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 280 | arch_reapChild(hfuzz, fuzzer); |
Robert Swiecki | 940a080 | 2016-11-02 12:42:09 +0100 | [diff] [blame] | 281 | |
| 282 | return true; |
Jagger | d1fc9ed | 2016-09-02 01:06:51 +0200 | [diff] [blame] | 283 | } |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 284 | |
| 285 | uint8_t subproc_System(const char *const argv[]) |
| 286 | { |
| 287 | pid_t pid = fork(); |
| 288 | if (pid == -1) { |
| 289 | PLOG_E("Couldn't fork"); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 290 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | if (!pid) { |
| 294 | execv(argv[0], (char *const *)&argv[0]); |
| 295 | PLOG_F("Couldn't execute '%s'", argv[0]); |
Robert Swiecki | b0a8d58 | 2016-09-09 18:13:00 +0200 | [diff] [blame] | 296 | return 255; |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | int status; |
| 300 | int flags = 0; |
| 301 | #if defined(__WNOTHREAD) |
| 302 | flags |= __WNOTHREAD; |
| 303 | #endif /* defined(__WNOTHREAD) */ |
| 304 | while (wait4(pid, &status, flags, NULL) != pid) ; |
| 305 | if (WIFSIGNALED(status)) { |
| 306 | LOG_E("Command '%s' terminated with signal: %d", argv[0], WTERMSIG(status)); |
| 307 | return (100 + WTERMSIG(status)); |
| 308 | } |
| 309 | if (!WIFEXITED(status)) { |
| 310 | LOG_F("Command '%s' terminated abnormally, status: %d", argv[0], status); |
| 311 | return 100; |
| 312 | } |
| 313 | |
| 314 | LOG_D("Command '%s' exited with: %d", argv[0], WEXITSTATUS(status)); |
| 315 | |
| 316 | if (WEXITSTATUS(status)) { |
Robert Swiecki | caef4f3 | 2016-09-09 18:18:22 +0200 | [diff] [blame] | 317 | LOG_W("Command '%s' exited with code: %d", argv[0], WEXITSTATUS(status)); |
Robert Swiecki | 54333cd | 2016-09-07 17:51:48 +0200 | [diff] [blame] | 318 | return 1U; |
| 319 | } |
| 320 | |
| 321 | return 0U; |
| 322 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 323 | |
| 324 | void subproc_checkTimeLimit(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 325 | { |
| 326 | if (hfuzz->tmOut == 0) { |
| 327 | return; |
| 328 | } |
| 329 | |
| 330 | int64_t curMillis = util_timeNowMillis(); |
| 331 | int64_t diffMillis = curMillis - fuzzer->timeStartedMillis; |
| 332 | if (diffMillis > (hfuzz->tmOut * 1000)) { |
Robert Swiecki | 013bc9c | 2016-12-12 17:31:06 +0100 | [diff] [blame] | 333 | /* Has this instance been already signaled due to timeout? Just, SIGKILL it */ |
| 334 | if (fuzzer->tmOutSignaled) { |
| 335 | LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", |
| 336 | fuzzer->pid); |
| 337 | kill(fuzzer->pid, SIGKILL); |
| 338 | return; |
| 339 | } |
| 340 | fuzzer->tmOutSignaled = true; |
| 341 | |
Robert Swiecki | e84b645 | 2016-12-12 12:42:04 +0100 | [diff] [blame] | 342 | LOG_W("PID %d took too much time (limit %ld s). Killing it", fuzzer->pid, hfuzz->tmOut); |
| 343 | if (hfuzz->tmout_vtalrm) { |
| 344 | kill(fuzzer->pid, SIGVTALRM); |
| 345 | } else { |
| 346 | kill(fuzzer->pid, SIGKILL); |
| 347 | } |
Robert Swiecki | fddc086 | 2016-10-27 12:42:46 +0200 | [diff] [blame] | 348 | ATOMIC_POST_INC(hfuzz->timeoutedCnt); |
| 349 | } |
| 350 | } |
Robert Swiecki | 4f09ce0 | 2017-02-25 22:47:55 +0100 | [diff] [blame] | 351 | |
| 352 | void subproc_checkTermination(honggfuzz_t * hfuzz, fuzzer_t * fuzzer) |
| 353 | { |
| 354 | if (ATOMIC_GET(hfuzz->terminating)) { |
| 355 | LOG_D("Killing PID: %d", (int)fuzzer->pid); |
| 356 | kill(fuzzer->pid, SIGKILL); |
| 357 | } |
| 358 | } |