morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 1 | //===- FuzzerUtilFuchsia.cpp - Misc utils for Fuchsia. --------------------===// |
| 2 | // |
chandlerc | 4028449 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // Misc utils implementation using Fuchsia/Zircon APIs. |
| 9 | //===----------------------------------------------------------------------===// |
| 10 | #include "FuzzerDefs.h" |
| 11 | |
| 12 | #if LIBFUZZER_FUCHSIA |
| 13 | |
| 14 | #include "FuzzerInternal.h" |
| 15 | #include "FuzzerUtil.h" |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 16 | #include <cassert> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 17 | #include <cerrno> |
| 18 | #include <cinttypes> |
| 19 | #include <cstdint> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 20 | #include <fcntl.h> |
phosek | e7c6a59 | 2018-06-07 18:41:35 +0000 | [diff] [blame] | 21 | #include <lib/fdio/spawn.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 22 | #include <string> |
phosek | 00c7332 | 2018-04-20 00:41:06 +0000 | [diff] [blame] | 23 | #include <sys/select.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 24 | #include <thread> |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 25 | #include <unistd.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 26 | #include <zircon/errors.h> |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 27 | #include <zircon/process.h> |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 28 | #include <zircon/sanitizer.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 29 | #include <zircon/status.h> |
| 30 | #include <zircon/syscalls.h> |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 31 | #include <zircon/syscalls/debug.h> |
| 32 | #include <zircon/syscalls/exception.h> |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 33 | #include <zircon/syscalls/object.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 34 | #include <zircon/types.h> |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 35 | |
| 36 | namespace fuzzer { |
| 37 | |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 38 | // Given that Fuchsia doesn't have the POSIX signals that libFuzzer was written |
| 39 | // around, the general approach is to spin up dedicated threads to watch for |
| 40 | // each requested condition (alarm, interrupt, crash). Of these, the crash |
| 41 | // handler is the most involved, as it requires resuming the crashed thread in |
| 42 | // order to invoke the sanitizers to get the needed state. |
| 43 | |
| 44 | // Forward declaration of assembly trampoline needed to resume crashed threads. |
| 45 | // This appears to have external linkage to C++, which is why it's not in the |
| 46 | // anonymous namespace. The assembly definition inside MakeTrampoline() |
| 47 | // actually defines the symbol with internal linkage only. |
| 48 | void CrashTrampolineAsm() __asm__("CrashTrampolineAsm"); |
| 49 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 50 | namespace { |
| 51 | |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 52 | // Helper function to handle Zircon syscall failures. |
| 53 | void ExitOnErr(zx_status_t Status, const char *Syscall) { |
| 54 | if (Status != ZX_OK) { |
| 55 | Printf("libFuzzer: %s failed: %s\n", Syscall, |
| 56 | _zx_status_get_string(Status)); |
| 57 | exit(1); |
| 58 | } |
| 59 | } |
| 60 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 61 | void AlarmHandler(int Seconds) { |
| 62 | while (true) { |
| 63 | SleepSeconds(Seconds); |
| 64 | Fuzzer::StaticAlarmCallback(); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void InterruptHandler() { |
phosek | 1631e45 | 2018-04-19 14:01:46 +0000 | [diff] [blame] | 69 | fd_set readfds; |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 70 | // Ctrl-C sends ETX in Zircon. |
phosek | 1631e45 | 2018-04-19 14:01:46 +0000 | [diff] [blame] | 71 | do { |
| 72 | FD_ZERO(&readfds); |
| 73 | FD_SET(STDIN_FILENO, &readfds); |
| 74 | select(STDIN_FILENO + 1, &readfds, nullptr, nullptr, nullptr); |
| 75 | } while(!FD_ISSET(STDIN_FILENO, &readfds) || getchar() != 0x03); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 76 | Fuzzer::StaticInterruptCallback(); |
| 77 | } |
| 78 | |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 79 | // For the crash handler, we need to call Fuzzer::StaticCrashSignalCallback |
| 80 | // without POSIX signal handlers. To achieve this, we use an assembly function |
| 81 | // to add the necessary CFI unwinding information and a C function to bridge |
| 82 | // from that back into C++. |
| 83 | |
| 84 | // FIXME: This works as a short-term solution, but this code really shouldn't be |
| 85 | // architecture dependent. A better long term solution is to implement remote |
| 86 | // unwinding and expose the necessary APIs through sanitizer_common and/or ASAN |
| 87 | // to allow the exception handling thread to gather the crash state directly. |
| 88 | // |
| 89 | // Alternatively, Fuchsia may in future actually implement basic signal |
| 90 | // handling for the machine trap signals. |
| 91 | #if defined(__x86_64__) |
| 92 | #define FOREACH_REGISTER(OP_REG, OP_NUM) \ |
| 93 | OP_REG(rax) \ |
| 94 | OP_REG(rbx) \ |
| 95 | OP_REG(rcx) \ |
| 96 | OP_REG(rdx) \ |
| 97 | OP_REG(rsi) \ |
| 98 | OP_REG(rdi) \ |
| 99 | OP_REG(rbp) \ |
| 100 | OP_REG(rsp) \ |
| 101 | OP_REG(r8) \ |
| 102 | OP_REG(r9) \ |
| 103 | OP_REG(r10) \ |
| 104 | OP_REG(r11) \ |
| 105 | OP_REG(r12) \ |
| 106 | OP_REG(r13) \ |
| 107 | OP_REG(r14) \ |
| 108 | OP_REG(r15) \ |
| 109 | OP_REG(rip) |
| 110 | |
| 111 | #elif defined(__aarch64__) |
| 112 | #define FOREACH_REGISTER(OP_REG, OP_NUM) \ |
| 113 | OP_NUM(0) \ |
| 114 | OP_NUM(1) \ |
| 115 | OP_NUM(2) \ |
| 116 | OP_NUM(3) \ |
| 117 | OP_NUM(4) \ |
| 118 | OP_NUM(5) \ |
| 119 | OP_NUM(6) \ |
| 120 | OP_NUM(7) \ |
| 121 | OP_NUM(8) \ |
| 122 | OP_NUM(9) \ |
| 123 | OP_NUM(10) \ |
| 124 | OP_NUM(11) \ |
| 125 | OP_NUM(12) \ |
| 126 | OP_NUM(13) \ |
| 127 | OP_NUM(14) \ |
| 128 | OP_NUM(15) \ |
| 129 | OP_NUM(16) \ |
| 130 | OP_NUM(17) \ |
| 131 | OP_NUM(18) \ |
| 132 | OP_NUM(19) \ |
| 133 | OP_NUM(20) \ |
| 134 | OP_NUM(21) \ |
| 135 | OP_NUM(22) \ |
| 136 | OP_NUM(23) \ |
| 137 | OP_NUM(24) \ |
| 138 | OP_NUM(25) \ |
| 139 | OP_NUM(26) \ |
| 140 | OP_NUM(27) \ |
| 141 | OP_NUM(28) \ |
| 142 | OP_NUM(29) \ |
| 143 | OP_NUM(30) \ |
| 144 | OP_REG(sp) |
| 145 | |
| 146 | #else |
| 147 | #error "Unsupported architecture for fuzzing on Fuchsia" |
| 148 | #endif |
| 149 | |
| 150 | // Produces a CFI directive for the named or numbered register. |
| 151 | #define CFI_OFFSET_REG(reg) ".cfi_offset " #reg ", %c[" #reg "]\n" |
| 152 | #define CFI_OFFSET_NUM(num) CFI_OFFSET_REG(r##num) |
| 153 | |
| 154 | // Produces an assembler input operand for the named or numbered register. |
| 155 | #define ASM_OPERAND_REG(reg) \ |
| 156 | [reg] "i"(offsetof(zx_thread_state_general_regs_t, reg)), |
| 157 | #define ASM_OPERAND_NUM(num) \ |
| 158 | [r##num] "i"(offsetof(zx_thread_state_general_regs_t, r[num])), |
| 159 | |
| 160 | // Trampoline to bridge from the assembly below to the static C++ crash |
| 161 | // callback. |
| 162 | __attribute__((noreturn)) |
| 163 | static void StaticCrashHandler() { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 164 | Fuzzer::StaticCrashSignalCallback(); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 165 | for (;;) { |
| 166 | _Exit(1); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // Creates the trampoline with the necessary CFI information to unwind through |
| 171 | // to the crashing call stack. The attribute is necessary because the function |
| 172 | // is never called; it's just a container around the assembly to allow it to |
| 173 | // use operands for compile-time computed constants. |
| 174 | __attribute__((used)) |
| 175 | void MakeTrampoline() { |
| 176 | __asm__(".cfi_endproc\n" |
| 177 | ".pushsection .text.CrashTrampolineAsm\n" |
| 178 | ".type CrashTrampolineAsm,STT_FUNC\n" |
| 179 | "CrashTrampolineAsm:\n" |
| 180 | ".cfi_startproc simple\n" |
| 181 | ".cfi_signal_frame\n" |
| 182 | #if defined(__x86_64__) |
| 183 | ".cfi_return_column rip\n" |
| 184 | ".cfi_def_cfa rsp, 0\n" |
| 185 | FOREACH_REGISTER(CFI_OFFSET_REG, CFI_OFFSET_NUM) |
| 186 | "call %c[StaticCrashHandler]\n" |
| 187 | "ud2\n" |
| 188 | #elif defined(__aarch64__) |
| 189 | ".cfi_return_column 33\n" |
| 190 | ".cfi_def_cfa sp, 0\n" |
| 191 | ".cfi_offset 33, %c[pc]\n" |
| 192 | FOREACH_REGISTER(CFI_OFFSET_REG, CFI_OFFSET_NUM) |
| 193 | "bl %[StaticCrashHandler]\n" |
| 194 | #else |
| 195 | #error "Unsupported architecture for fuzzing on Fuchsia" |
| 196 | #endif |
| 197 | ".cfi_endproc\n" |
| 198 | ".size CrashTrampolineAsm, . - CrashTrampolineAsm\n" |
| 199 | ".popsection\n" |
| 200 | ".cfi_startproc\n" |
| 201 | : // No outputs |
| 202 | : FOREACH_REGISTER(ASM_OPERAND_REG, ASM_OPERAND_NUM) |
| 203 | #if defined(__aarch64__) |
| 204 | ASM_OPERAND_REG(pc) |
| 205 | #endif |
| 206 | [StaticCrashHandler] "i" (StaticCrashHandler)); |
| 207 | } |
| 208 | |
| 209 | void CrashHandler(zx_handle_t *Event) { |
| 210 | // This structure is used to ensure we close handles to objects we create in |
| 211 | // this handler. |
| 212 | struct ScopedHandle { |
| 213 | ~ScopedHandle() { _zx_handle_close(Handle); } |
| 214 | zx_handle_t Handle = ZX_HANDLE_INVALID; |
| 215 | }; |
| 216 | |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 217 | // Create the exception channel. We need to claim to be a "debugger" so the |
| 218 | // kernel will allow us to modify and resume dying threads (see below). Once |
| 219 | // the channel is set, we can signal the main thread to continue and wait |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 220 | // for the exception to arrive. |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 221 | ScopedHandle Channel; |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 222 | zx_handle_t Self = _zx_process_self(); |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 223 | ExitOnErr(_zx_task_create_exception_channel( |
| 224 | Self, ZX_EXCEPTION_CHANNEL_DEBUGGER, &Channel.Handle), |
| 225 | "_zx_task_create_exception_channel"); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 226 | |
| 227 | ExitOnErr(_zx_object_signal(*Event, 0, ZX_USER_SIGNAL_0), |
| 228 | "_zx_object_signal"); |
| 229 | |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 230 | // This thread lives as long as the process in order to keep handling |
| 231 | // crashes. In practice, the first crashed thread to reach the end of the |
| 232 | // StaticCrashHandler will end the process. |
| 233 | while (true) { |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 234 | ExitOnErr(_zx_object_wait_one(Channel.Handle, ZX_CHANNEL_READABLE, |
| 235 | ZX_TIME_INFINITE, nullptr), |
| 236 | "_zx_object_wait_one"); |
| 237 | |
| 238 | zx_exception_info_t ExceptionInfo; |
| 239 | ScopedHandle Exception; |
| 240 | ExitOnErr(_zx_channel_read(Channel.Handle, 0, &ExceptionInfo, |
| 241 | &Exception.Handle, sizeof(ExceptionInfo), 1, |
| 242 | nullptr, nullptr), |
| 243 | "_zx_channel_read"); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 244 | |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 245 | // Ignore informational synthetic exceptions. |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 246 | if (ZX_EXCP_THREAD_STARTING == ExceptionInfo.type || |
| 247 | ZX_EXCP_THREAD_EXITING == ExceptionInfo.type || |
| 248 | ZX_EXCP_PROCESS_STARTING == ExceptionInfo.type) { |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 249 | continue; |
| 250 | } |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 251 | |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 252 | // At this point, we want to get the state of the crashing thread, but |
| 253 | // libFuzzer and the sanitizers assume this will happen from that same |
| 254 | // thread via a POSIX signal handler. "Resurrecting" the thread in the |
| 255 | // middle of the appropriate callback is as simple as forcibly setting the |
| 256 | // instruction pointer/program counter, provided we NEVER EVER return from |
| 257 | // that function (since otherwise our stack will not be valid). |
| 258 | ScopedHandle Thread; |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 259 | ExitOnErr(_zx_exception_get_thread(Exception.Handle, &Thread.Handle), |
| 260 | "_zx_exception_get_thread"); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 261 | |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 262 | zx_thread_state_general_regs_t GeneralRegisters; |
| 263 | ExitOnErr(_zx_thread_read_state(Thread.Handle, ZX_THREAD_STATE_GENERAL_REGS, |
| 264 | &GeneralRegisters, |
| 265 | sizeof(GeneralRegisters)), |
| 266 | "_zx_thread_read_state"); |
| 267 | |
| 268 | // To unwind properly, we need to push the crashing thread's register state |
| 269 | // onto the stack and jump into a trampoline with CFI instructions on how |
| 270 | // to restore it. |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 271 | #if defined(__x86_64__) |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 272 | uintptr_t StackPtr = |
| 273 | (GeneralRegisters.rsp - (128 + sizeof(GeneralRegisters))) & |
| 274 | -(uintptr_t)16; |
| 275 | __unsanitized_memcpy(reinterpret_cast<void *>(StackPtr), &GeneralRegisters, |
| 276 | sizeof(GeneralRegisters)); |
| 277 | GeneralRegisters.rsp = StackPtr; |
| 278 | GeneralRegisters.rip = reinterpret_cast<zx_vaddr_t>(CrashTrampolineAsm); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 279 | |
| 280 | #elif defined(__aarch64__) |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 281 | uintptr_t StackPtr = |
| 282 | (GeneralRegisters.sp - sizeof(GeneralRegisters)) & -(uintptr_t)16; |
| 283 | __unsanitized_memcpy(reinterpret_cast<void *>(StackPtr), &GeneralRegisters, |
| 284 | sizeof(GeneralRegisters)); |
| 285 | GeneralRegisters.sp = StackPtr; |
| 286 | GeneralRegisters.pc = reinterpret_cast<zx_vaddr_t>(CrashTrampolineAsm); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 287 | |
| 288 | #else |
| 289 | #error "Unsupported architecture for fuzzing on Fuchsia" |
| 290 | #endif |
| 291 | |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 292 | // Now force the crashing thread's state. |
| 293 | ExitOnErr( |
| 294 | _zx_thread_write_state(Thread.Handle, ZX_THREAD_STATE_GENERAL_REGS, |
| 295 | &GeneralRegisters, sizeof(GeneralRegisters)), |
| 296 | "_zx_thread_write_state"); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 297 | |
phosek | 154d069 | 2019-06-27 21:13:06 +0000 | [diff] [blame] | 298 | // Set the exception to HANDLED so it resumes the thread on close. |
| 299 | uint32_t ExceptionState = ZX_EXCEPTION_STATE_HANDLED; |
| 300 | ExitOnErr(_zx_object_set_property(Exception.Handle, ZX_PROP_EXCEPTION_STATE, |
| 301 | &ExceptionState, sizeof(ExceptionState)), |
| 302 | "zx_object_set_property"); |
phosek | ed73fdf | 2019-05-22 16:36:35 +0000 | [diff] [blame] | 303 | } |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | } // namespace |
| 307 | |
kcc | da16893 | 2019-01-31 00:09:43 +0000 | [diff] [blame] | 308 | bool Mprotect(void *Ptr, size_t Size, bool AllowReadWrite) { |
| 309 | return false; // UNIMPLEMENTED |
| 310 | } |
| 311 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 312 | // Platform specific functions. |
| 313 | void SetSignalHandler(const FuzzingOptions &Options) { |
jakehehrlich | 1c53389 | 2019-09-17 00:34:41 +0000 | [diff] [blame^] | 314 | // Make sure information from libFuzzer and the sanitizers are easy to |
| 315 | // reassemble. `__sanitizer_log_write` has the added benefit of ensuring the |
| 316 | // DSO map is always available for the symbolizer. |
| 317 | // A uint64_t fits in 20 chars, so 64 is plenty. |
| 318 | char Buf[64]; |
| 319 | memset(Buf, 0, sizeof(Buf)); |
| 320 | snprintf(Buf, sizeof(Buf), "==%lu== INFO: libFuzzer starting.\n", GetPid()); |
| 321 | if (EF->__sanitizer_log_write) |
| 322 | __sanitizer_log_write(Buf, sizeof(Buf)); |
| 323 | Printf("%s", Buf); |
| 324 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 325 | // Set up alarm handler if needed. |
| 326 | if (Options.UnitTimeoutSec > 0) { |
| 327 | std::thread T(AlarmHandler, Options.UnitTimeoutSec / 2 + 1); |
| 328 | T.detach(); |
| 329 | } |
| 330 | |
| 331 | // Set up interrupt handler if needed. |
| 332 | if (Options.HandleInt || Options.HandleTerm) { |
| 333 | std::thread T(InterruptHandler); |
| 334 | T.detach(); |
| 335 | } |
| 336 | |
| 337 | // Early exit if no crash handler needed. |
| 338 | if (!Options.HandleSegv && !Options.HandleBus && !Options.HandleIll && |
| 339 | !Options.HandleFpe && !Options.HandleAbrt) |
| 340 | return; |
| 341 | |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 342 | // Set up the crash handler and wait until it is ready before proceeding. |
| 343 | zx_handle_t Event; |
| 344 | ExitOnErr(_zx_event_create(0, &Event), "_zx_event_create"); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 345 | |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 346 | std::thread T(CrashHandler, &Event); |
phosek | d2551d6 | 2018-08-27 17:51:52 +0000 | [diff] [blame] | 347 | zx_status_t Status = |
| 348 | _zx_object_wait_one(Event, ZX_USER_SIGNAL_0, ZX_TIME_INFINITE, nullptr); |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 349 | _zx_handle_close(Event); |
| 350 | ExitOnErr(Status, "_zx_object_wait_one"); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 351 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 352 | T.detach(); |
| 353 | } |
| 354 | |
| 355 | void SleepSeconds(int Seconds) { |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 356 | _zx_nanosleep(_zx_deadline_after(ZX_SEC(Seconds))); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | unsigned long GetPid() { |
| 360 | zx_status_t rc; |
| 361 | zx_info_handle_basic_t Info; |
phosek | e81389f | 2018-05-26 01:02:34 +0000 | [diff] [blame] | 362 | if ((rc = _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &Info, |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 363 | sizeof(Info), NULL, NULL)) != ZX_OK) { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 364 | Printf("libFuzzer: unable to get info about self: %s\n", |
phosek | e81389f | 2018-05-26 01:02:34 +0000 | [diff] [blame] | 365 | _zx_status_get_string(rc)); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 366 | exit(1); |
| 367 | } |
| 368 | return Info.koid; |
| 369 | } |
| 370 | |
| 371 | size_t GetPeakRSSMb() { |
| 372 | zx_status_t rc; |
| 373 | zx_info_task_stats_t Info; |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 374 | if ((rc = _zx_object_get_info(_zx_process_self(), ZX_INFO_TASK_STATS, &Info, |
phosek | e59ddd2 | 2018-07-18 19:20:47 +0000 | [diff] [blame] | 375 | sizeof(Info), NULL, NULL)) != ZX_OK) { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 376 | Printf("libFuzzer: unable to get info about self: %s\n", |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 377 | _zx_status_get_string(rc)); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 378 | exit(1); |
| 379 | } |
| 380 | return (Info.mem_private_bytes + Info.mem_shared_bytes) >> 20; |
| 381 | } |
| 382 | |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 383 | template <typename Fn> |
| 384 | class RunOnDestruction { |
| 385 | public: |
| 386 | explicit RunOnDestruction(Fn fn) : fn_(fn) {} |
| 387 | ~RunOnDestruction() { fn_(); } |
| 388 | |
| 389 | private: |
| 390 | Fn fn_; |
| 391 | }; |
| 392 | |
| 393 | template <typename Fn> |
| 394 | RunOnDestruction<Fn> at_scope_exit(Fn fn) { |
| 395 | return RunOnDestruction<Fn>(fn); |
| 396 | } |
| 397 | |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 398 | int ExecuteCommand(const Command &Cmd) { |
| 399 | zx_status_t rc; |
| 400 | |
| 401 | // Convert arguments to C array |
| 402 | auto Args = Cmd.getArguments(); |
| 403 | size_t Argc = Args.size(); |
| 404 | assert(Argc != 0); |
phosek | 4b9efd1 | 2018-06-02 01:17:10 +0000 | [diff] [blame] | 405 | std::unique_ptr<const char *[]> Argv(new const char *[Argc + 1]); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 406 | for (size_t i = 0; i < Argc; ++i) |
| 407 | Argv[i] = Args[i].c_str(); |
phosek | 4b9efd1 | 2018-06-02 01:17:10 +0000 | [diff] [blame] | 408 | Argv[Argc] = nullptr; |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 409 | |
phosek | ca62a26 | 2018-10-02 17:21:04 +0000 | [diff] [blame] | 410 | // Determine output. On Fuchsia, the fuzzer is typically run as a component |
| 411 | // that lacks a mutable working directory. Fortunately, when this is the case |
| 412 | // a mutable output directory must be specified using "-artifact_prefix=...", |
| 413 | // so write the log file(s) there. |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 414 | int FdOut = STDOUT_FILENO; |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 415 | if (Cmd.hasOutputFile()) { |
phosek | ca62a26 | 2018-10-02 17:21:04 +0000 | [diff] [blame] | 416 | std::string Path; |
| 417 | if (Cmd.hasFlag("artifact_prefix")) |
| 418 | Path = Cmd.getFlagValue("artifact_prefix") + "/" + Cmd.getOutputFile(); |
| 419 | else |
| 420 | Path = Cmd.getOutputFile(); |
| 421 | FdOut = open(Path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0); |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 422 | if (FdOut == -1) { |
phosek | ca62a26 | 2018-10-02 17:21:04 +0000 | [diff] [blame] | 423 | Printf("libFuzzer: failed to open %s: %s\n", Path.c_str(), |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 424 | strerror(errno)); |
| 425 | return ZX_ERR_IO; |
| 426 | } |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 427 | } |
phosek | ca62a26 | 2018-10-02 17:21:04 +0000 | [diff] [blame] | 428 | auto CloseFdOut = at_scope_exit([FdOut]() { |
| 429 | if (FdOut != STDOUT_FILENO) |
| 430 | close(FdOut); |
| 431 | }); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 432 | |
| 433 | // Determine stderr |
| 434 | int FdErr = STDERR_FILENO; |
| 435 | if (Cmd.isOutAndErrCombined()) |
| 436 | FdErr = FdOut; |
| 437 | |
| 438 | // Clone the file descriptors into the new process |
phosek | 4b9efd1 | 2018-06-02 01:17:10 +0000 | [diff] [blame] | 439 | fdio_spawn_action_t SpawnAction[] = { |
| 440 | { |
| 441 | .action = FDIO_SPAWN_ACTION_CLONE_FD, |
| 442 | .fd = |
| 443 | { |
| 444 | .local_fd = STDIN_FILENO, |
| 445 | .target_fd = STDIN_FILENO, |
| 446 | }, |
| 447 | }, |
| 448 | { |
| 449 | .action = FDIO_SPAWN_ACTION_CLONE_FD, |
| 450 | .fd = |
| 451 | { |
| 452 | .local_fd = FdOut, |
| 453 | .target_fd = STDOUT_FILENO, |
| 454 | }, |
| 455 | }, |
| 456 | { |
| 457 | .action = FDIO_SPAWN_ACTION_CLONE_FD, |
| 458 | .fd = |
| 459 | { |
| 460 | .local_fd = FdErr, |
| 461 | .target_fd = STDERR_FILENO, |
| 462 | }, |
| 463 | }, |
| 464 | }; |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 465 | |
phosek | 4b9efd1 | 2018-06-02 01:17:10 +0000 | [diff] [blame] | 466 | // Start the process. |
| 467 | char ErrorMsg[FDIO_SPAWN_ERR_MSG_MAX_LENGTH]; |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 468 | zx_handle_t ProcessHandle = ZX_HANDLE_INVALID; |
phosek | 4b9efd1 | 2018-06-02 01:17:10 +0000 | [diff] [blame] | 469 | rc = fdio_spawn_etc( |
| 470 | ZX_HANDLE_INVALID, FDIO_SPAWN_CLONE_ALL & (~FDIO_SPAWN_CLONE_STDIO), |
| 471 | Argv[0], Argv.get(), nullptr, 3, SpawnAction, &ProcessHandle, ErrorMsg); |
| 472 | if (rc != ZX_OK) { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 473 | Printf("libFuzzer: failed to launch '%s': %s, %s\n", Argv[0], ErrorMsg, |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 474 | _zx_status_get_string(rc)); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 475 | return rc; |
| 476 | } |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 477 | auto CloseHandle = at_scope_exit([&]() { _zx_handle_close(ProcessHandle); }); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 478 | |
| 479 | // Now join the process and return the exit status. |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 480 | if ((rc = _zx_object_wait_one(ProcessHandle, ZX_PROCESS_TERMINATED, |
phosek | d2551d6 | 2018-08-27 17:51:52 +0000 | [diff] [blame] | 481 | ZX_TIME_INFINITE, nullptr)) != ZX_OK) { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 482 | Printf("libFuzzer: failed to join '%s': %s\n", Argv[0], |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 483 | _zx_status_get_string(rc)); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 484 | return rc; |
| 485 | } |
| 486 | |
| 487 | zx_info_process_t Info; |
phosek | f912376 | 2018-02-07 08:22:58 +0000 | [diff] [blame] | 488 | if ((rc = _zx_object_get_info(ProcessHandle, ZX_INFO_PROCESS, &Info, |
| 489 | sizeof(Info), nullptr, nullptr)) != ZX_OK) { |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 490 | Printf("libFuzzer: unable to get return code from '%s': %s\n", Argv[0], |
phosek | e81389f | 2018-05-26 01:02:34 +0000 | [diff] [blame] | 491 | _zx_status_get_string(rc)); |
morehouse | 400262a | 2017-12-08 22:54:44 +0000 | [diff] [blame] | 492 | return rc; |
| 493 | } |
| 494 | |
| 495 | return Info.return_code; |
| 496 | } |
| 497 | |
| 498 | const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt, |
| 499 | size_t PattLen) { |
| 500 | return memmem(Data, DataLen, Patt, PattLen); |
| 501 | } |
| 502 | |
| 503 | } // namespace fuzzer |
| 504 | |
| 505 | #endif // LIBFUZZER_FUCHSIA |