Path: blob/main/contrib/llvm-project/compiler-rt/lib/sanitizer_common/sanitizer_coverage_fuchsia.cpp
35233 views
//===-- sanitizer_coverage_fuchsia.cpp ------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// Sanitizer Coverage Controller for Trace PC Guard, Fuchsia-specific version.9//10// This Fuchsia-specific implementation uses the same basic scheme and the11// same simple '.sancov' file format as the generic implementation. The12// difference is that we just produce a single blob of output for the whole13// program, not a separate one per DSO. We do not sort the PC table and do14// not prune the zeros, so the resulting file is always as large as it15// would be to report 100% coverage. Implicit tracing information about16// the address ranges of DSOs allows offline tools to split the one big17// blob into separate files that the 'sancov' tool can understand.18//19// Unlike the traditional implementation that uses an atexit hook to write20// out data files at the end, the results on Fuchsia do not go into a file21// per se. The 'coverage_dir' option is ignored. Instead, they are stored22// directly into a shared memory object (a Zircon VMO). At exit, that VMO23// is handed over to a system service that's responsible for getting the24// data out to somewhere that it can be fed into the sancov tool (where and25// how is not our problem).2627#include "sanitizer_platform.h"28#if SANITIZER_FUCHSIA29#include <zircon/process.h>30#include <zircon/sanitizer.h>31#include <zircon/syscalls.h>3233#include "sanitizer_atomic.h"34#include "sanitizer_common.h"35#include "sanitizer_interface_internal.h"36#include "sanitizer_internal_defs.h"37# include "sanitizer_symbolizer_markup_constants.h"3839using namespace __sanitizer;4041namespace __sancov {42namespace {4344// TODO(mcgrathr): Move the constant into a header shared with other impls.45constexpr u64 Magic64 = 0xC0BFFFFFFFFFFF64ULL;46static_assert(SANITIZER_WORDSIZE == 64, "Fuchsia is always LP64");4748constexpr const char kSancovSinkName[] = "sancov";4950// Collects trace-pc guard coverage.51// This class relies on zero-initialization.52class TracePcGuardController final {53public:54constexpr TracePcGuardController() {}5556// For each PC location being tracked, there is a u32 reserved in global57// data called the "guard". At startup, we assign each guard slot a58// unique index into the big results array. Later during runtime, the59// first call to TracePcGuard (below) will store the corresponding PC at60// that index in the array. (Each later call with the same guard slot is61// presumed to be from the same PC.) Then it clears the guard slot back62// to zero, which tells the compiler not to bother calling in again. At63// the end of the run, we have a big array where each element is either64// zero or is a tracked PC location that was hit in the trace.6566// This is called from global constructors. Each translation unit has a67// contiguous array of guard slots, and a constructor that calls here68// with the bounds of its array. Those constructors are allowed to call69// here more than once for the same array. Usually all of these70// constructors run in the initial thread, but it's possible that a71// dlopen call on a secondary thread will run constructors that get here.72void InitTracePcGuard(u32 *start, u32 *end) {73if (end > start && *start == 0 && common_flags()->coverage) {74// Complete the setup before filling in any guards with indices.75// This avoids the possibility of code called from Setup reentering76// TracePcGuard.77u32 idx = Setup(end - start);78for (u32 *p = start; p < end; ++p) {79*p = idx++;80}81}82}8384void TracePcGuard(u32 *guard, uptr pc) {85atomic_uint32_t *guard_ptr = reinterpret_cast<atomic_uint32_t *>(guard);86u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed);87if (idx > 0)88array_[idx] = pc;89}9091void Dump() {92Lock locked(&setup_lock_);93if (array_) {94CHECK_NE(vmo_, ZX_HANDLE_INVALID);9596// Publish the VMO to the system, where it can be collected and97// analyzed after this process exits. This always consumes the VMO98// handle. Any failure is just logged and not indicated to us.99__sanitizer_publish_data(kSancovSinkName, vmo_);100vmo_ = ZX_HANDLE_INVALID;101102// This will route to __sanitizer_log_write, which will ensure that103// information about shared libraries is written out. This message104// uses the `dumpfile` symbolizer markup element to highlight the105// dump. See the explanation for this in:106// https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md107Printf("SanitizerCoverage: " FORMAT_DUMPFILE " with up to %u PCs\n",108kSancovSinkName, vmo_name_, next_index_ - 1);109}110}111112private:113// We map in the largest possible view into the VMO: one word114// for every possible 32-bit index value. This avoids the need115// to change the mapping when increasing the size of the VMO.116// We can always spare the 32G of address space.117static constexpr size_t MappingSize = sizeof(uptr) << 32;118119Mutex setup_lock_;120uptr *array_ = nullptr;121u32 next_index_ = 0;122zx_handle_t vmo_ = {};123char vmo_name_[ZX_MAX_NAME_LEN] = {};124125size_t DataSize() const { return next_index_ * sizeof(uintptr_t); }126127u32 Setup(u32 num_guards) {128Lock locked(&setup_lock_);129DCHECK(common_flags()->coverage);130131if (next_index_ == 0) {132CHECK_EQ(vmo_, ZX_HANDLE_INVALID);133CHECK_EQ(array_, nullptr);134135// The first sample goes at [1] to reserve [0] for the magic number.136next_index_ = 1 + num_guards;137138zx_status_t status = _zx_vmo_create(DataSize(), ZX_VMO_RESIZABLE, &vmo_);139CHECK_EQ(status, ZX_OK);140141// Give the VMO a name including our process KOID so it's easy to spot.142internal_snprintf(vmo_name_, sizeof(vmo_name_), "%s.%zu", kSancovSinkName,143internal_getpid());144_zx_object_set_property(vmo_, ZX_PROP_NAME, vmo_name_,145internal_strlen(vmo_name_));146uint64_t size = DataSize();147status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size,148sizeof(size));149CHECK_EQ(status, ZX_OK);150151// Map the largest possible view we might need into the VMO. Later152// we might need to increase the VMO's size before we can use larger153// indices, but we'll never move the mapping address so we don't have154// any multi-thread synchronization issues with that.155uintptr_t mapping;156status =157_zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE,1580, vmo_, 0, MappingSize, &mapping);159CHECK_EQ(status, ZX_OK);160161// Hereafter other threads are free to start storing into162// elements [1, next_index_) of the big array.163array_ = reinterpret_cast<uptr *>(mapping);164165// Store the magic number.166// Hereafter, the VMO serves as the contents of the '.sancov' file.167array_[0] = Magic64;168169return 1;170} else {171// The VMO is already mapped in, but it's not big enough to use the172// new indices. So increase the size to cover the new maximum index.173174CHECK_NE(vmo_, ZX_HANDLE_INVALID);175CHECK_NE(array_, nullptr);176177uint32_t first_index = next_index_;178next_index_ += num_guards;179180zx_status_t status = _zx_vmo_set_size(vmo_, DataSize());181CHECK_EQ(status, ZX_OK);182uint64_t size = DataSize();183status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size,184sizeof(size));185CHECK_EQ(status, ZX_OK);186187return first_index;188}189}190};191192static TracePcGuardController pc_guard_controller;193194} // namespace195} // namespace __sancov196197namespace __sanitizer {198void InitializeCoverage(bool enabled, const char *dir) {199CHECK_EQ(enabled, common_flags()->coverage);200CHECK_EQ(dir, common_flags()->coverage_dir);201202static bool coverage_enabled = false;203if (!coverage_enabled) {204coverage_enabled = enabled;205Atexit(__sanitizer_cov_dump);206AddDieCallback(__sanitizer_cov_dump);207}208}209} // namespace __sanitizer210211extern "C" {212SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage(const uptr *pcs,213uptr len) {214UNIMPLEMENTED();215}216217SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32 *guard) {218if (!*guard)219return;220__sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1);221}222223SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init,224u32 *start, u32 *end) {225if (start == end || *start)226return;227__sancov::pc_guard_controller.InitTracePcGuard(start, end);228}229230SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() {231__sancov::pc_guard_controller.Dump();232}233SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() {234__sanitizer_dump_trace_pc_guard_coverage();235}236// Default empty implementations (weak). Users should redefine them.237SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp, void) {}238SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp1, void) {}239SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp2, void) {}240SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp4, void) {}241SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp8, void) {}242SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp1, void) {}243SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp2, void) {}244SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp4, void) {}245SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp8, void) {}246SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_switch, void) {}247SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div4, void) {}248SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div8, void) {}249SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_gep, void) {}250SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {}251} // extern "C"252253#endif // !SANITIZER_FUCHSIA254255256