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