Zachary Turner | 6fa57ad | 2016-12-02 23:02:01 +0000 | [diff] [blame] | 1 | //===- FuzzerUtilWindows.cpp - Misc utils for Windows. --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // Misc utils implementation for Windows. |
| 10 | //===----------------------------------------------------------------------===// |
| 11 | |
| 12 | #include "FuzzerDefs.h" |
| 13 | #if LIBFUZZER_WINDOWS |
| 14 | #include "FuzzerIO.h" |
| 15 | #include "FuzzerInternal.h" |
| 16 | #include <Psapi.h> |
| 17 | #include <cassert> |
| 18 | #include <chrono> |
| 19 | #include <cstring> |
| 20 | #include <errno.h> |
| 21 | #include <iomanip> |
| 22 | #include <signal.h> |
| 23 | #include <sstream> |
| 24 | #include <stdio.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <windows.h> |
| 27 | |
| 28 | namespace fuzzer { |
| 29 | |
| 30 | LONG WINAPI SEGVHandler(PEXCEPTION_POINTERS ExceptionInfo) { |
| 31 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) { |
| 32 | case EXCEPTION_ACCESS_VIOLATION: |
| 33 | case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: |
| 34 | case EXCEPTION_STACK_OVERFLOW: |
| 35 | Fuzzer::StaticCrashSignalCallback(); |
| 36 | break; |
| 37 | } |
| 38 | return EXCEPTION_CONTINUE_SEARCH; |
| 39 | } |
| 40 | |
| 41 | LONG WINAPI BUSHandler(PEXCEPTION_POINTERS ExceptionInfo) { |
| 42 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) { |
| 43 | case EXCEPTION_DATATYPE_MISALIGNMENT: |
| 44 | case EXCEPTION_IN_PAGE_ERROR: |
| 45 | Fuzzer::StaticCrashSignalCallback(); |
| 46 | break; |
| 47 | } |
| 48 | return EXCEPTION_CONTINUE_SEARCH; |
| 49 | } |
| 50 | |
| 51 | LONG WINAPI ILLHandler(PEXCEPTION_POINTERS ExceptionInfo) { |
| 52 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) { |
| 53 | case EXCEPTION_ILLEGAL_INSTRUCTION: |
| 54 | case EXCEPTION_PRIV_INSTRUCTION: |
| 55 | Fuzzer::StaticCrashSignalCallback(); |
| 56 | break; |
| 57 | } |
| 58 | return EXCEPTION_CONTINUE_SEARCH; |
| 59 | } |
| 60 | |
| 61 | LONG WINAPI FPEHandler(PEXCEPTION_POINTERS ExceptionInfo) { |
| 62 | switch (ExceptionInfo->ExceptionRecord->ExceptionCode) { |
| 63 | case EXCEPTION_FLT_DENORMAL_OPERAND: |
| 64 | case EXCEPTION_FLT_DIVIDE_BY_ZERO: |
| 65 | case EXCEPTION_FLT_INEXACT_RESULT: |
| 66 | case EXCEPTION_FLT_INVALID_OPERATION: |
| 67 | case EXCEPTION_FLT_OVERFLOW: |
| 68 | case EXCEPTION_FLT_STACK_CHECK: |
| 69 | case EXCEPTION_FLT_UNDERFLOW: |
| 70 | case EXCEPTION_INT_DIVIDE_BY_ZERO: |
| 71 | case EXCEPTION_INT_OVERFLOW: |
| 72 | Fuzzer::StaticCrashSignalCallback(); |
| 73 | break; |
| 74 | } |
| 75 | return EXCEPTION_CONTINUE_SEARCH; |
| 76 | } |
| 77 | |
| 78 | BOOL WINAPI INTHandler(DWORD dwCtrlType) { |
| 79 | switch (dwCtrlType) { |
| 80 | case CTRL_C_EVENT: |
| 81 | Fuzzer::StaticInterruptCallback(); |
| 82 | return TRUE; |
| 83 | default: |
| 84 | return FALSE; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | BOOL WINAPI TERMHandler(DWORD dwCtrlType) { |
| 89 | switch (dwCtrlType) { |
| 90 | case CTRL_BREAK_EVENT: |
| 91 | Fuzzer::StaticInterruptCallback(); |
| 92 | return TRUE; |
| 93 | default: |
| 94 | return FALSE; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | void SetTimer(int Seconds) { |
| 99 | // TODO: Complete this implementation. |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | void SetSigSegvHandler() { |
| 104 | if (!AddVectoredExceptionHandler(1, SEGVHandler)) { |
| 105 | Printf("libFuzzer: AddVectoredExceptionHandler failed.\n"); |
| 106 | exit(1); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void SetSigBusHandler() { |
| 111 | if (!AddVectoredExceptionHandler(1, BUSHandler)) { |
| 112 | Printf("libFuzzer: AddVectoredExceptionHandler failed.\n"); |
| 113 | exit(1); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static void CrashHandler(int) { Fuzzer::StaticCrashSignalCallback(); } |
| 118 | |
| 119 | void SetSigAbrtHandler() { signal(SIGABRT, CrashHandler); } |
| 120 | |
| 121 | void SetSigIllHandler() { |
| 122 | if (!AddVectoredExceptionHandler(1, ILLHandler)) { |
| 123 | Printf("libFuzzer: AddVectoredExceptionHandler failed.\n"); |
| 124 | exit(1); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void SetSigFpeHandler() { |
| 129 | if (!AddVectoredExceptionHandler(1, FPEHandler)) { |
| 130 | Printf("libFuzzer: AddVectoredExceptionHandler failed.\n"); |
| 131 | exit(1); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | void SetSigIntHandler() { |
| 136 | if (!SetConsoleCtrlHandler(INTHandler, TRUE)) { |
| 137 | DWORD LastError = GetLastError(); |
| 138 | Printf("libFuzzer: SetConsoleCtrlHandler failed (Error code: %lu).\n", |
| 139 | LastError); |
| 140 | exit(1); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void SetSigTermHandler() { |
| 145 | if (!SetConsoleCtrlHandler(TERMHandler, TRUE)) { |
| 146 | DWORD LastError = GetLastError(); |
| 147 | Printf("libFuzzer: SetConsoleCtrlHandler failed (Error code: %lu).\n", |
| 148 | LastError); |
| 149 | exit(1); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void SleepSeconds(int Seconds) { Sleep(Seconds * 1000); } |
| 154 | |
| 155 | int GetPid() { return GetCurrentProcessId(); } |
| 156 | |
| 157 | size_t GetPeakRSSMb() { |
| 158 | PROCESS_MEMORY_COUNTERS info; |
| 159 | if (!GetProcessMemoryInfo(GetCurrentProcess(), &info, sizeof(info))) |
| 160 | return 0; |
| 161 | return info.PeakWorkingSetSize >> 20; |
| 162 | } |
| 163 | |
| 164 | FILE *OpenProcessPipe(const char *Command, const char *Mode) { |
| 165 | return _popen(Command, Mode); |
| 166 | } |
| 167 | |
| 168 | int ExecuteCommand(const std::string &Command) { |
| 169 | return system(Command.c_str()); |
| 170 | } |
| 171 | |
| 172 | const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt, |
| 173 | size_t PattLen) { |
| 174 | // TODO: make this implementation more efficient. |
| 175 | const char *Cdata = (const char *)Data; |
| 176 | const char *Cpatt = (const char *)Patt; |
| 177 | |
| 178 | if (!Data || !Patt || DataLen == 0 || PattLen == 0 || DataLen < PattLen) |
| 179 | return NULL; |
| 180 | |
| 181 | if (PattLen == 1) |
| 182 | return memchr(Data, *Cpatt, DataLen); |
| 183 | |
| 184 | const char *End = Cdata + DataLen - PattLen; |
| 185 | |
| 186 | for (const char *It = Cdata; It < End; ++It) |
| 187 | if (It[0] == Cpatt[0] && memcmp(It, Cpatt, PattLen) == 0) |
| 188 | return It; |
| 189 | |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | } // namespace fuzzer |
| 194 | #endif // LIBFUZZER_WINDOWS |