//===- FuzzerUtilFuchsia.cpp - Misc utils for Fuchsia. --------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // Misc utils implementation using Fuchsia/Zircon APIs. //===----------------------------------------------------------------------===// #include"FuzzerPlatform.h"
// Given that Fuchsia doesn't have the POSIX signals that libFuzzer was written // around, the general approach is to spin up dedicated threads to watch for // each requested condition (alarm, interrupt, crash). Of these, the crash // handler is the most involved, as it requires resuming the crashed thread in // order to invoke the sanitizers to get the needed state.
// Forward declaration of assembly trampoline needed to resume crashed threads. // This appears to have external linkage to C++, which is why it's not in the // anonymous namespace. The assembly definition inside MakeTrampoline() // actually defines the symbol with internal linkage only. void CrashTrampolineAsm() __asm__("CrashTrampolineAsm");
namespace {
// Helper function to handle Zircon syscall failures. void ExitOnErr(zx_status_t Status, constchar *Syscall) { if (Status != ZX_OK) {
Printf("libFuzzer: %s failed: %s\n", Syscall,
_zx_status_get_string(Status)); exit(1);
}
}
// CFAOffset is used to reference the stack pointer before entering the // trampoline (Stack Pointer + CFAOffset = prev Stack Pointer). Before jumping // to the trampoline we copy all the registers onto the stack. We need to make // sure that the new stack has enough space to store all the registers. // // The trampoline holds CFI information regarding the registers stored in the // stack, which is then used by the unwinder to restore them. #ifdefined(__x86_64__) // In x86_64 the crashing function might also be using the red zone (128 bytes // on top of their rsp).
constexpr size_t CFAOffset = 128 + sizeof(zx_thread_state_general_regs_t); #elifdefined(__aarch64__) // In aarch64 we need to always have the stack pointer aligned to 16 bytes, so we // make sure that we are keeping that same alignment.
constexpr size_t CFAOffset = (sizeof(zx_thread_state_general_regs_t) + 15) & -(uintptr_t)16; #endif
// For the crash handler, we need to call Fuzzer::StaticCrashSignalCallback // without POSIX signal handlers. To achieve this, we use an assembly function // to add the necessary CFI unwinding information and a C function to bridge // from that back into C++.
// FIXME: This works as a short-term solution, but this code really shouldn't be // architecture dependent. A better long term solution is to implement remote // unwinding and expose the necessary APIs through sanitizer_common and/or ASAN // to allow the exception handling thread to gather the crash state directly. // // Alternatively, Fuchsia may in future actually implement basic signal // handling for the machine trap signals. #ifdefined(__x86_64__) #define FOREACH_REGISTER(OP_REG, OP_NUM) \
OP_REG(rax) \
OP_REG(rbx) \
OP_REG(rcx) \
OP_REG(rdx) \
OP_REG(rsi) \
OP_REG(rdi) \
OP_REG(rbp) \
OP_REG(rsp) \
OP_REG(r8) \
OP_REG(r9) \
OP_REG(r10) \
OP_REG(r11) \
OP_REG(r12) \
OP_REG(r13) \
OP_REG(r14) \
OP_REG(r15) \
OP_REG(rip)
#else #error"Unsupported architecture for fuzzing on Fuchsia" #endif
// Produces a CFI directive for the named or numbered register. // The value used refers to an assembler immediate operand with the same name // as the register (see ASM_OPERAND_REG). #define CFI_OFFSET_REG(reg) ".cfi_offset "#reg", %c["#reg"]\n" #define CFI_OFFSET_NUM(num) CFI_OFFSET_REG(x##num)
// Produces an assembler immediate operand for the named or numbered register. // This operand contains the offset of the register relative to the CFA. #define ASM_OPERAND_REG(reg) \
[reg] "i"(offsetof(zx_thread_state_general_regs_t, reg) - CFAOffset), #define ASM_OPERAND_NUM(num) \
[x##num] "i"(offsetof(zx_thread_state_general_regs_t, r[num]) - CFAOffset),
// Trampoline to bridge from the assembly below to the static C++ crash // callback.
__attribute__((noreturn)) staticvoid StaticCrashHandler() {
Fuzzer::StaticCrashSignalCallback(); for (;;) {
_Exit(1);
}
}
// Creates the trampoline with the necessary CFI information to unwind through // to the crashing call stack: // * Defining the CFA so that it points to the stack pointer at the point // of crash. // * Storing all registers at the point of crash in the stack and refer to them // via CFI information (relative to the CFA). // * Setting the return column so the unwinder knows how to continue unwinding. // * (x86_64) making sure rsp is aligned before calling StaticCrashHandler. // * Calling StaticCrashHandler that will trigger the unwinder. // // The __attribute__((used)) is necessary because the function // is never called; it's just a container around the assembly to allow it to // use operands for compile-time computed constants.
__attribute__((used)) void MakeTrampoline() {
__asm__(".cfi_endproc\n" ".pushsection .text.CrashTrampolineAsm\n" ".type CrashTrampolineAsm,STT_FUNC\n" "CrashTrampolineAsm:\n" ".cfi_startproc simple\n" ".cfi_signal_frame\n" #ifdefined(__x86_64__) ".cfi_return_column rip\n" ".cfi_def_cfa rsp, %c[CFAOffset]\n"
FOREACH_REGISTER(CFI_OFFSET_REG, CFI_OFFSET_NUM) "mov %%rsp, %%rbp\n" ".cfi_def_cfa_register rbp\n" "andq $-16, %%rsp\n" "call %c[StaticCrashHandler]\n" "ud2\n" #elifdefined(__aarch64__) ".cfi_return_column 33\n" ".cfi_def_cfa sp, %c[CFAOffset]\n"
FOREACH_REGISTER(CFI_OFFSET_REG, CFI_OFFSET_NUM) ".cfi_offset 33, %c[pc]\n" ".cfi_offset 30, %c[lr]\n" "bl %c[StaticCrashHandler]\n" "brk 1\n" #else #error"Unsupported architecture for fuzzing on Fuchsia" #endif ".cfi_endproc\n" ".size CrashTrampolineAsm, . - CrashTrampolineAsm\n" ".popsection\n" ".cfi_startproc\n"
: // No outputs
: FOREACH_REGISTER(ASM_OPERAND_REG, ASM_OPERAND_NUM) #ifdefined(__aarch64__)
ASM_OPERAND_REG(pc)
ASM_OPERAND_REG(lr) #endif
[StaticCrashHandler] "i" (StaticCrashHandler),
[CFAOffset] "i" (CFAOffset));
}
void CrashHandler(zx_handle_t *Event) { // This structure is used to ensure we close handles to objects we create in // this handler. struct ScopedHandle {
~ScopedHandle() { _zx_handle_close(Handle); }
zx_handle_t Handle = ZX_HANDLE_INVALID;
};
// Create the exception channel. We need to claim to be a "debugger" so the // kernel will allow us to modify and resume dying threads (see below). Once // the channel is set, we can signal the main thread to continue and wait // for the exception to arrive.
ScopedHandle Channel;
zx_handle_t Self = _zx_process_self();
ExitOnErr(_zx_task_create_exception_channel(
Self, ZX_EXCEPTION_CHANNEL_DEBUGGER, &Channel.Handle), "_zx_task_create_exception_channel");
// This thread lives as long as the process in order to keep handling // crashes. In practice, the first crashed thread to reach the end of the // StaticCrashHandler will end the process. while (true) {
ExitOnErr(_zx_object_wait_one(Channel.Handle, ZX_CHANNEL_READABLE,
ZX_TIME_INFINITE, nullptr), "_zx_object_wait_one");
// At this point, we want to get the state of the crashing thread, but // libFuzzer and the sanitizers assume this will happen from that same // thread via a POSIX signal handler. "Resurrecting" the thread in the // middle of the appropriate callback is as simple as forcibly setting the // instruction pointer/program counter, provided we NEVER EVER return from // that function (since otherwise our stack will not be valid).
ScopedHandle Thread;
ExitOnErr(_zx_exception_get_thread(Exception.Handle, &Thread.Handle), "_zx_exception_get_thread");
// To unwind properly, we need to push the crashing thread's register state // onto the stack and jump into a trampoline with CFI instructions on how // to restore it. #ifdefined(__x86_64__)
uintptr_t StackPtr = GeneralRegisters.rsp - CFAOffset;
__unsanitized_memcpy(reinterpret_cast<void *>(StackPtr), &GeneralRegisters, sizeof(GeneralRegisters));
GeneralRegisters.rsp = StackPtr;
GeneralRegisters.rip = reinterpret_cast<zx_vaddr_t>(CrashTrampolineAsm);
#else #error"Unsupported architecture for fuzzing on Fuchsia" #endif
// Now force the crashing thread's state.
ExitOnErr(
_zx_thread_write_state(Thread.Handle, ZX_THREAD_STATE_GENERAL_REGS,
&GeneralRegisters, sizeof(GeneralRegisters)), "_zx_thread_write_state");
// Set the exception to HANDLED so it resumes the thread on close.
uint32_t ExceptionState = ZX_EXCEPTION_STATE_HANDLED;
ExitOnErr(_zx_object_set_property(Exception.Handle, ZX_PROP_EXCEPTION_STATE,
&ExceptionState, sizeof(ExceptionState)), "zx_object_set_property");
}
}
} // namespace
// Platform specific functions. void SetSignalHandler(const FuzzingOptions &Options) { // Make sure information from libFuzzer and the sanitizers are easy to // reassemble. `__sanitizer_log_write` has the added benefit of ensuring the // DSO map is always available for the symbolizer. // A uint64_t fits in 20 chars, so 64 is plenty. char Buf[64];
memset(Buf, 0, sizeof(Buf));
snprintf(Buf, sizeof(Buf), "==%lu== INFO: libFuzzer starting.\n", GetPid()); if (EF->__sanitizer_log_write)
__sanitizer_log_write(Buf, sizeof(Buf));
Printf("%s", Buf);
// Set up alarm handler if needed. if (Options.UnitTimeoutSec > 0) {
std::thread T(AlarmHandler, Options.UnitTimeoutSec / 2 + 1);
T.detach();
}
// Set up interrupt handler if needed. if (Options.HandleInt || Options.HandleTerm) {
std::thread T(InterruptHandler);
T.detach();
}
// Early exit if no crash handler needed. if (!Options.HandleSegv && !Options.HandleBus && !Options.HandleIll &&
!Options.HandleFpe && !Options.HandleAbrt) return;
// Set up the crash handler and wait until it is ready before proceeding.
zx_handle_t Event;
ExitOnErr(_zx_event_create(0, &Event), "_zx_event_create");
int ExecuteCommand(const Command &Cmd) {
zx_status_t rc;
// Convert arguments to C array auto Args = Cmd.getArguments();
size_t Argc = Args.size();
assert(Argc != 0);
std::unique_ptr<constchar *[]> Argv(newconstchar *[Argc + 1]); for (size_t i = 0; i < Argc; ++i)
Argv[i] = Args[i].c_str();
Argv[Argc] = nullptr;
// Determine output. On Fuchsia, the fuzzer is typically run as a component // that lacks a mutable working directory. Fortunately, when this is the case // a mutable output directory must be specified using "-artifact_prefix=...", // so write the log file(s) there. // However, we don't want to apply this logic for absolute paths. int FdOut = STDOUT_FILENO; bool discardStdout = false; bool discardStderr = false;
if (Cmd.hasOutputFile()) {
std::string Path = Cmd.getOutputFile(); if (Path == getDevNull()) { // On Fuchsia, there's no "/dev/null" like-file, so we // just don't copy the FDs into the spawned process.
discardStdout = true;
} else { bool IsAbsolutePath = Path.length() > 1 && Path[0] == '/'; if (!IsAbsolutePath && Cmd.hasFlag("artifact_prefix"))
Path = Cmd.getFlagValue("artifact_prefix") + "/" + Path;
FdOut = open(Path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0); if (FdOut == -1) {
Printf("libFuzzer: failed to open %s: %s\n", Path.c_str(),
strerror(errno)); return ZX_ERR_IO;
}
}
} auto CloseFdOut = at_scope_exit([FdOut]() { if (FdOut != STDOUT_FILENO)
close(FdOut);
});
// Determine stderr int FdErr = STDERR_FILENO; if (Cmd.isOutAndErrCombined()) {
FdErr = FdOut; if (discardStdout)
discardStderr = true;
}
// Clone the file descriptors into the new process
std::vector<fdio_spawn_action_t> SpawnActions;
SpawnActions.push_back(clone_fd_action(STDIN_FILENO, STDIN_FILENO));
if (!discardStdout)
SpawnActions.push_back(clone_fd_action(FdOut, STDOUT_FILENO)); if (!discardStderr)
SpawnActions.push_back(clone_fd_action(FdErr, STDERR_FILENO));
if (rc != ZX_OK) {
Printf("libFuzzer: failed to launch '%s': %s, %s\n", Argv[0], ErrorMsg,
_zx_status_get_string(rc)); return rc;
} auto CloseHandle = at_scope_exit([&]() { _zx_handle_close(ProcessHandle); });
// Now join the process and return the exit status. if ((rc = _zx_object_wait_one(ProcessHandle, ZX_PROCESS_TERMINATED,
ZX_TIME_INFINITE, nullptr)) != ZX_OK) {
Printf("libFuzzer: failed to join '%s': %s\n", Argv[0],
_zx_status_get_string(rc)); return rc;
}
zx_info_process_t Info; if ((rc = _zx_object_get_info(ProcessHandle, ZX_INFO_PROCESS, &Info, sizeof(Info), nullptr, nullptr)) != ZX_OK) {
Printf("libFuzzer: unable to get return code from '%s': %s\n", Argv[0],
_zx_status_get_string(rc)); return rc;
}
return Info.return_code;
}
bool ExecuteCommand(const Command &BaseCmd, std::string *CmdOutput) { auto LogFilePath = TempPath("SimPopenOut", ".txt");
Command Cmd(BaseCmd);
Cmd.setOutputFile(LogFilePath); int Ret = ExecuteCommand(Cmd);
*CmdOutput = FileToString(LogFilePath);
RemoveFile(LogFilePath); return Ret == 0;
}
// In fuchsia, accessing /dev/null is not supported. There's nothing // similar to a file that discards everything that is written to it. // The way of doing something similar in fuchsia is by using // fdio_null_create and binding that to a file descriptor. void DiscardOutput(int Fd) {
fdio_t *fdio_null = fdio_null_create(); if (fdio_null == nullptr) return; int nullfd = fdio_bind_to_fd(fdio_null, -1, 0); if (nullfd < 0) return;
dup2(nullfd, Fd);
}
} // namespace fuzzer
#endif// LIBFUZZER_FUCHSIA
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.