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