|
| 1 | +//===-- sanitizer_coverage_libcdep_new.cc ---------------------------------===// |
| 2 | +// |
| 3 | +// The LLVM Compiler Infrastructure |
| 4 | +// |
| 5 | +// This file is distributed under the University of Illinois Open Source |
| 6 | +// License. See LICENSE.TXT for details. |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | +// Sanitizer Coverage Controller for Trace PC Guard. |
| 10 | + |
| 11 | +#include "sanitizer_allocator_internal.h" |
| 12 | +#include "sanitizer_atomic.h" |
| 13 | +#include "sanitizer_common.h" |
| 14 | +#include "sanitizer_symbolizer.h" |
| 15 | + |
| 16 | +using namespace __sanitizer; |
| 17 | + |
| 18 | +using AddressRange = LoadedModule::AddressRange; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +static const u64 Magic64 = 0xC0BFFFFFFFFFFF64ULL; |
| 23 | +static const u64 Magic32 = 0xC0BFFFFFFFFFFF32ULL; |
| 24 | +static const u64 Magic = SANITIZER_WORDSIZE == 64 ? Magic64 : Magic32; |
| 25 | + |
| 26 | +static fd_t OpenFile(const char* path) { |
| 27 | + error_t err; |
| 28 | + fd_t fd = OpenFile(path, WrOnly, &err); |
| 29 | + if (fd == kInvalidFd) |
| 30 | + Report("SanitizerCoverage: failed to open %s for writing (reason: %d)\n", |
| 31 | + path, err); |
| 32 | + return fd; |
| 33 | +} |
| 34 | + |
| 35 | +static void GetCoverageFilename(char* path, const char* name, |
| 36 | + const char* extension) { |
| 37 | + CHECK(name); |
| 38 | + internal_snprintf(path, kMaxPathLength, "%s/%s.%zd.%s", |
| 39 | + common_flags()->coverage_dir, name, internal_getpid(), |
| 40 | + extension); |
| 41 | +} |
| 42 | + |
| 43 | +static void WriteModuleCoverage(char* file_path, const char* module_name, |
| 44 | + const uptr* pcs, uptr len) { |
| 45 | + GetCoverageFilename(file_path, StripModuleName(module_name), "sancov"); |
| 46 | + fd_t fd = OpenFile(file_path); |
| 47 | + WriteToFile(fd, &Magic, sizeof(Magic)); |
| 48 | + WriteToFile(fd, pcs, len * sizeof(*pcs)); |
| 49 | + CloseFile(fd); |
| 50 | + Printf("SanitizerCoverage: %s %zd PCs written\n", file_path, len); |
| 51 | +} |
| 52 | + |
| 53 | +static void SanitizerDumpCoverage(const uptr* unsorted_pcs, uptr len) { |
| 54 | + if (!len) return; |
| 55 | + |
| 56 | + char* file_path = static_cast<char*>(InternalAlloc(kMaxPathLength)); |
| 57 | + char* module_name = static_cast<char*>(InternalAlloc(kMaxPathLength)); |
| 58 | + uptr* pcs = static_cast<uptr*>(InternalAlloc(len * sizeof(uptr))); |
| 59 | + |
| 60 | + internal_memcpy(pcs, unsorted_pcs, len * sizeof(uptr)); |
| 61 | + SortArray(pcs, len); |
| 62 | + |
| 63 | + bool module_found = false; |
| 64 | + uptr last_base = 0; |
| 65 | + uptr module_start_idx = 0; |
| 66 | + |
| 67 | + for (uptr i = 0; i < len; ++i) { |
| 68 | + const uptr pc = pcs[i]; |
| 69 | + if (!pc) continue; |
| 70 | + |
| 71 | + if (!__sanitizer_get_module_and_offset_for_pc(pc, nullptr, 0, &pcs[i])) { |
| 72 | + Printf("ERROR: bad pc %x\n", pc); |
| 73 | + continue; |
| 74 | + } |
| 75 | + uptr module_base = pc - pcs[i]; |
| 76 | + |
| 77 | + if (module_base != last_base || !module_found) { |
| 78 | + if (module_found) { |
| 79 | + WriteModuleCoverage(file_path, module_name, &pcs[module_start_idx], |
| 80 | + i - module_start_idx); |
| 81 | + } |
| 82 | + |
| 83 | + last_base = module_base; |
| 84 | + module_start_idx = i; |
| 85 | + module_found = true; |
| 86 | + __sanitizer_get_module_and_offset_for_pc(pc, module_name, kMaxPathLength, |
| 87 | + &pcs[i]); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + if (module_found) { |
| 92 | + WriteModuleCoverage(file_path, module_name, &pcs[module_start_idx], |
| 93 | + len - module_start_idx); |
| 94 | + } |
| 95 | + |
| 96 | + InternalFree(file_path); |
| 97 | + InternalFree(module_name); |
| 98 | + InternalFree(pcs); |
| 99 | +} |
| 100 | + |
| 101 | +// Collects trace-pc guard coverage. |
| 102 | +// This class relies on zero-initialization. |
| 103 | +class TracePcGuardController { |
| 104 | + public: |
| 105 | + void Initialize() { |
| 106 | + CHECK(!initialized); |
| 107 | + |
| 108 | + initialized = true; |
| 109 | + pc_vector.Initialize(0); |
| 110 | + } |
| 111 | + |
| 112 | + void InitTracePcGuard(u32* start, u32* end) { |
| 113 | + if (!initialized) Initialize(); |
| 114 | + CHECK(!*start); |
| 115 | + CHECK_NE(start, end); |
| 116 | + |
| 117 | + u32 i = pc_vector.size(); |
| 118 | + for (u32* p = start; p < end; p++) *p = ++i; |
| 119 | + pc_vector.resize(i); |
| 120 | + } |
| 121 | + |
| 122 | + void TracePcGuard(u32* guard, uptr pc) { |
| 123 | + atomic_uint32_t* guard_ptr = reinterpret_cast<atomic_uint32_t*>(guard); |
| 124 | + u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed); |
| 125 | + if (!idx) return; |
| 126 | + // we start indices from 1. |
| 127 | + pc_vector[idx - 1] = pc; |
| 128 | + } |
| 129 | + |
| 130 | + void Dump() { |
| 131 | + if (!initialized) return; |
| 132 | + __sanitizer_dump_coverage(pc_vector.data(), pc_vector.size()); |
| 133 | + } |
| 134 | + |
| 135 | + private: |
| 136 | + bool initialized; |
| 137 | + InternalMmapVectorNoCtor<uptr> pc_vector; |
| 138 | +}; |
| 139 | + |
| 140 | +static TracePcGuardController pc_guard_controller; |
| 141 | + |
| 142 | +}; // namespace |
| 143 | + |
| 144 | +extern "C" { |
| 145 | +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage( // NOLINT |
| 146 | + const uptr* pcs, uptr len) { |
| 147 | + return SanitizerDumpCoverage(pcs, len); |
| 148 | +} |
| 149 | + |
| 150 | +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void |
| 151 | +__sanitizer_cov_trace_pc_guard(u32* guard) { |
| 152 | + if (!*guard) return; |
| 153 | + pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1); |
| 154 | +} |
| 155 | + |
| 156 | +SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE void |
| 157 | +__sanitizer_cov_trace_pc_guard_init(u32* start, u32* end) { |
| 158 | + if (start == end || *start) return; |
| 159 | + pc_guard_controller.InitTracePcGuard(start, end); |
| 160 | +} |
| 161 | + |
| 162 | +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() { |
| 163 | + pc_guard_controller.Dump(); |
| 164 | +} |
| 165 | +} // extern "C" |
0 commit comments