diff options
author | Robert Swiecki <robert@swiecki.net> | 2019-01-30 11:50:18 +0100 |
---|---|---|
committer | Robert Swiecki <robert@swiecki.net> | 2019-01-30 11:50:18 +0100 |
commit | 98e2337238c5a49960c88f4c65823f5c088ffc53 (patch) | |
tree | 6b3546f71ab0414e2be50c01d6c0276ab048acaa /subproc.c | |
parent | e2be7a962bad2ab1598b8ae1f55103968096c82c (diff) | |
download | honggfuzz-98e2337238c5a49960c88f4c65823f5c088ffc53.tar.gz |
ALL: remove -p (pid), simplify the subproc state machine. NetBSD will stop working for some time, will be fixed in the coming days
Diffstat (limited to 'subproc.c')
-rw-r--r-- | subproc.c | 96 |
1 files changed, 58 insertions, 38 deletions
@@ -123,30 +123,63 @@ const char* subproc_StatusToStr(int status, char* str, size_t len) { return str; } -bool subproc_persistentModeRoundDone(run_t* run) { - if (!run->global->exe.persistent) { +static bool subproc_persistentSendFileIndicator(run_t* run) { + uint64_t len = (uint64_t)run->dynamicFileSz; + if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) { + PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len)); return false; } + return true; +} + +static bool subproc_persistentGetReady(run_t* run) { uint8_t rcv; if (recv(run->persistentSock, &rcv, sizeof(rcv), MSG_DONTWAIT) != sizeof(rcv)) { return false; } - if (rcv == HFdoneTag) { - return true; + if (rcv != HFReadyTag) { + LOG_E("Received invalid message from the persistent process: '%c' (0x%" PRIx8 + ") , expected '%c' (0x%" PRIx8 ")", + rcv, rcv, HFReadyTag, HFReadyTag); + return false; } - LOG_F("Received invalid message from the persistent process: '%c' (0x%" PRIx8 - ") , expected '%c' (0x%" PRIx8 ")", - rcv, rcv, HFdoneTag, HFdoneTag); - return false; + return true; } -static bool subproc_persistentSendFileIndicator(run_t* run) { - uint64_t len = (uint64_t)run->dynamicFileSz; - if (!files_sendToSocketNB(run->persistentSock, (uint8_t*)&len, sizeof(len))) { - PLOG_W("files_sendToSocketNB(len=%zu)", sizeof(len)); +bool subproc_persistentModeStateMachine(run_t* run) { + if (!run->global->exe.persistent) { return false; } - return true; + + for (;;) { + switch (run->runState) { + case _HF_RS_WAITING_FOR_INITIAL_READY: { + if (!subproc_persistentGetReady(run)) { + return false; + } + run->runState = _HF_RS_SEND_DATA; + }; break; + case _HF_RS_SEND_DATA: { + if (!subproc_persistentSendFileIndicator(run)) { + LOG_E("Could not send the file size indicator to the persistent process. " + "Killing the process pid=%d", + (int)run->pid); + kill(run->pid, SIGKILL); + return false; + } + run->runState = _HF_RS_WAITING_FOR_READY; + }; break; + case _HF_RS_WAITING_FOR_READY: { + if (!subproc_persistentGetReady(run)) { + return false; + } + run->runState = _HF_RS_SEND_DATA; + /* The current persistent round is done */ + return true; + }; break; + default: { LOG_F("Unknown runState: %d", run->runState); }; break; + } + } } static bool subproc_PrepareExecv(run_t* run) { @@ -256,15 +289,10 @@ static bool subproc_PrepareExecv(run_t* run) { } static bool subproc_New(run_t* run) { - run->pid = run->persistentPid; - if (run->pid != 0 && run->hasCrashed == false) { + if (run->pid) { return true; } - LOG_D("SocketFuzzer: subproc_new: Start New Process"); - run->hasCrashed = false; - run->tmOutSignaled = false; - int sv[2]; if (run->global->exe.persistent) { if (run->persistentSock != -1) { @@ -287,6 +315,7 @@ static bool subproc_New(run_t* run) { run->pid = arch_fork(run); if (run->pid == -1) { PLOG_E("Couldn't fork"); + run->pid = 0; return false; } /* The child process */ @@ -326,21 +355,17 @@ static bool subproc_New(run_t* run) { } /* Parent */ - LOG_D("Launched new process, PID: %d, thread: %" PRId32 " (concurrency: %zd)", run->pid, + LOG_D("Launched new process, pid=%d, thread: %" PRId32 " (concurrency: %zd)", (int)run->pid, run->fuzzNo, run->global->threads.threadsMax); - if (run->global->socketFuzzer.enabled) { - /* (dobin): Don't know why, but this is important */ - run->persistentPid = run->pid; - } + arch_prepareParentAfterFork(run); + if (run->global->exe.persistent) { close(sv[1]); - LOG_I("Persistent mode: Launched new persistent PID: %d", (int)run->pid); - run->persistentPid = run->pid; + run->runState = _HF_RS_WAITING_FOR_INITIAL_READY; + LOG_I("Persistent mode: Launched new persistent pid=%d", (int)run->pid); } - arch_prepareParentAfterFork(run); - return true; } @@ -353,11 +378,6 @@ bool subproc_Run(run_t* run) { } arch_prepareParent(run); - - if (run->global->exe.persistent && !subproc_persistentSendFileIndicator(run)) { - LOG_W("Could not send file size to the persistent process"); - kill(run->persistentPid, SIGKILL); - } arch_reapChild(run); return true; @@ -399,7 +419,7 @@ uint8_t subproc_System(run_t* run, const char* const argv[]) { continue; } if (ret == -1) { - PLOG_E("wait4() for process PID: %d", (int)pid); + PLOG_E("wait4() for process pid=%d", (int)pid); return 255; } if (ret != pid) { @@ -423,7 +443,7 @@ uint8_t subproc_System(run_t* run, const char* const argv[]) { } void subproc_checkTimeLimit(run_t* run) { - if (run->global->timing.tmOut == 0) { + if (!run->global->timing.tmOut) { return; } @@ -432,14 +452,14 @@ void subproc_checkTimeLimit(run_t* run) { if (run->tmOutSignaled && (diffMillis > ((run->global->timing.tmOut + 1) * 1000))) { /* Has this instance been already signaled due to timeout? Just, SIGKILL it */ - LOG_W("PID %d has already been signaled due to timeout. Killing it with SIGKILL", run->pid); + LOG_W("pid=%d has already been signaled due to timeout. Killing it with SIGKILL", run->pid); kill(run->pid, SIGKILL); return; } if ((diffMillis > (run->global->timing.tmOut * 1000)) && !run->tmOutSignaled) { run->tmOutSignaled = true; - LOG_W("PID %d took too much time (limit %ld s). Killing it with %s", run->pid, + LOG_W("pid=%d took too much time (limit %ld s). Killing it with %s", (int)run->pid, (long)run->global->timing.tmOut, run->global->timing.tmoutVTALRM ? "SIGVTALRM" : "SIGKILL"); if (run->global->timing.tmoutVTALRM) { @@ -453,7 +473,7 @@ void subproc_checkTimeLimit(run_t* run) { void subproc_checkTermination(run_t* run) { if (fuzz_isTerminating()) { - LOG_D("Killing PID: %d", (int)run->pid); + LOG_D("Killing pid=%d", (int)run->pid); kill(run->pid, SIGKILL); } } |