168d75effSDimitry Andric //===-- sanitizer_coverage_fuchsia.cpp ------------------------------------===// 268d75effSDimitry Andric // 368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 668d75effSDimitry Andric // 768d75effSDimitry Andric //===----------------------------------------------------------------------===// 868d75effSDimitry Andric // 968d75effSDimitry Andric // Sanitizer Coverage Controller for Trace PC Guard, Fuchsia-specific version. 1068d75effSDimitry Andric // 1168d75effSDimitry Andric // This Fuchsia-specific implementation uses the same basic scheme and the 1268d75effSDimitry Andric // same simple '.sancov' file format as the generic implementation. The 1368d75effSDimitry Andric // difference is that we just produce a single blob of output for the whole 1468d75effSDimitry Andric // program, not a separate one per DSO. We do not sort the PC table and do 1568d75effSDimitry Andric // not prune the zeros, so the resulting file is always as large as it 1668d75effSDimitry Andric // would be to report 100% coverage. Implicit tracing information about 1768d75effSDimitry Andric // the address ranges of DSOs allows offline tools to split the one big 1868d75effSDimitry Andric // blob into separate files that the 'sancov' tool can understand. 1968d75effSDimitry Andric // 2068d75effSDimitry Andric // Unlike the traditional implementation that uses an atexit hook to write 2168d75effSDimitry Andric // out data files at the end, the results on Fuchsia do not go into a file 2268d75effSDimitry Andric // per se. The 'coverage_dir' option is ignored. Instead, they are stored 2368d75effSDimitry Andric // directly into a shared memory object (a Zircon VMO). At exit, that VMO 2468d75effSDimitry Andric // is handed over to a system service that's responsible for getting the 2568d75effSDimitry Andric // data out to somewhere that it can be fed into the sancov tool (where and 2668d75effSDimitry Andric // how is not our problem). 2768d75effSDimitry Andric 2868d75effSDimitry Andric #include "sanitizer_platform.h" 2968d75effSDimitry Andric #if SANITIZER_FUCHSIA 305ffd83dbSDimitry Andric #include <zircon/process.h> 315ffd83dbSDimitry Andric #include <zircon/sanitizer.h> 325ffd83dbSDimitry Andric #include <zircon/syscalls.h> 335ffd83dbSDimitry Andric 3468d75effSDimitry Andric #include "sanitizer_atomic.h" 3568d75effSDimitry Andric #include "sanitizer_common.h" 3681ad6265SDimitry Andric #include "sanitizer_interface_internal.h" 3768d75effSDimitry Andric #include "sanitizer_internal_defs.h" 38*5f757f3fSDimitry Andric # include "sanitizer_symbolizer_markup_constants.h" 3968d75effSDimitry Andric 4068d75effSDimitry Andric using namespace __sanitizer; 4168d75effSDimitry Andric 4268d75effSDimitry Andric namespace __sancov { 4368d75effSDimitry Andric namespace { 4468d75effSDimitry Andric 4568d75effSDimitry Andric // TODO(mcgrathr): Move the constant into a header shared with other impls. 4668d75effSDimitry Andric constexpr u64 Magic64 = 0xC0BFFFFFFFFFFF64ULL; 4768d75effSDimitry Andric static_assert(SANITIZER_WORDSIZE == 64, "Fuchsia is always LP64"); 4868d75effSDimitry Andric 4968d75effSDimitry Andric constexpr const char kSancovSinkName[] = "sancov"; 5068d75effSDimitry Andric 5168d75effSDimitry Andric // Collects trace-pc guard coverage. 5268d75effSDimitry Andric // This class relies on zero-initialization. 5368d75effSDimitry Andric class TracePcGuardController final { 5468d75effSDimitry Andric public: 55349cc55cSDimitry Andric constexpr TracePcGuardController() {} 56349cc55cSDimitry Andric 5768d75effSDimitry Andric // For each PC location being tracked, there is a u32 reserved in global 5868d75effSDimitry Andric // data called the "guard". At startup, we assign each guard slot a 5968d75effSDimitry Andric // unique index into the big results array. Later during runtime, the 6068d75effSDimitry Andric // first call to TracePcGuard (below) will store the corresponding PC at 6168d75effSDimitry Andric // that index in the array. (Each later call with the same guard slot is 6268d75effSDimitry Andric // presumed to be from the same PC.) Then it clears the guard slot back 6368d75effSDimitry Andric // to zero, which tells the compiler not to bother calling in again. At 6468d75effSDimitry Andric // the end of the run, we have a big array where each element is either 6568d75effSDimitry Andric // zero or is a tracked PC location that was hit in the trace. 6668d75effSDimitry Andric 6768d75effSDimitry Andric // This is called from global constructors. Each translation unit has a 6868d75effSDimitry Andric // contiguous array of guard slots, and a constructor that calls here 6968d75effSDimitry Andric // with the bounds of its array. Those constructors are allowed to call 7068d75effSDimitry Andric // here more than once for the same array. Usually all of these 7168d75effSDimitry Andric // constructors run in the initial thread, but it's possible that a 7268d75effSDimitry Andric // dlopen call on a secondary thread will run constructors that get here. 7368d75effSDimitry Andric void InitTracePcGuard(u32 *start, u32 *end) { 7468d75effSDimitry Andric if (end > start && *start == 0 && common_flags()->coverage) { 7568d75effSDimitry Andric // Complete the setup before filling in any guards with indices. 7668d75effSDimitry Andric // This avoids the possibility of code called from Setup reentering 7768d75effSDimitry Andric // TracePcGuard. 7868d75effSDimitry Andric u32 idx = Setup(end - start); 7968d75effSDimitry Andric for (u32 *p = start; p < end; ++p) { 8068d75effSDimitry Andric *p = idx++; 8168d75effSDimitry Andric } 8268d75effSDimitry Andric } 8368d75effSDimitry Andric } 8468d75effSDimitry Andric 8568d75effSDimitry Andric void TracePcGuard(u32 *guard, uptr pc) { 8668d75effSDimitry Andric atomic_uint32_t *guard_ptr = reinterpret_cast<atomic_uint32_t *>(guard); 8768d75effSDimitry Andric u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed); 885ffd83dbSDimitry Andric if (idx > 0) 895ffd83dbSDimitry Andric array_[idx] = pc; 9068d75effSDimitry Andric } 9168d75effSDimitry Andric 9268d75effSDimitry Andric void Dump() { 93349cc55cSDimitry Andric Lock locked(&setup_lock_); 9468d75effSDimitry Andric if (array_) { 9568d75effSDimitry Andric CHECK_NE(vmo_, ZX_HANDLE_INVALID); 9668d75effSDimitry Andric 9768d75effSDimitry Andric // Publish the VMO to the system, where it can be collected and 9868d75effSDimitry Andric // analyzed after this process exits. This always consumes the VMO 9968d75effSDimitry Andric // handle. Any failure is just logged and not indicated to us. 10068d75effSDimitry Andric __sanitizer_publish_data(kSancovSinkName, vmo_); 10168d75effSDimitry Andric vmo_ = ZX_HANDLE_INVALID; 10268d75effSDimitry Andric 10368d75effSDimitry Andric // This will route to __sanitizer_log_write, which will ensure that 10468d75effSDimitry Andric // information about shared libraries is written out. This message 10568d75effSDimitry Andric // uses the `dumpfile` symbolizer markup element to highlight the 10668d75effSDimitry Andric // dump. See the explanation for this in: 10768d75effSDimitry Andric // https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md 10868d75effSDimitry Andric Printf("SanitizerCoverage: " FORMAT_DUMPFILE " with up to %u PCs\n", 10968d75effSDimitry Andric kSancovSinkName, vmo_name_, next_index_ - 1); 11068d75effSDimitry Andric } 11168d75effSDimitry Andric } 11268d75effSDimitry Andric 11368d75effSDimitry Andric private: 11468d75effSDimitry Andric // We map in the largest possible view into the VMO: one word 11568d75effSDimitry Andric // for every possible 32-bit index value. This avoids the need 11668d75effSDimitry Andric // to change the mapping when increasing the size of the VMO. 11768d75effSDimitry Andric // We can always spare the 32G of address space. 11868d75effSDimitry Andric static constexpr size_t MappingSize = sizeof(uptr) << 32; 11968d75effSDimitry Andric 120349cc55cSDimitry Andric Mutex setup_lock_; 12168d75effSDimitry Andric uptr *array_ = nullptr; 12268d75effSDimitry Andric u32 next_index_ = 0; 12368d75effSDimitry Andric zx_handle_t vmo_ = {}; 12468d75effSDimitry Andric char vmo_name_[ZX_MAX_NAME_LEN] = {}; 12568d75effSDimitry Andric 12668d75effSDimitry Andric size_t DataSize() const { return next_index_ * sizeof(uintptr_t); } 12768d75effSDimitry Andric 12868d75effSDimitry Andric u32 Setup(u32 num_guards) { 129349cc55cSDimitry Andric Lock locked(&setup_lock_); 13068d75effSDimitry Andric DCHECK(common_flags()->coverage); 13168d75effSDimitry Andric 13268d75effSDimitry Andric if (next_index_ == 0) { 13368d75effSDimitry Andric CHECK_EQ(vmo_, ZX_HANDLE_INVALID); 13468d75effSDimitry Andric CHECK_EQ(array_, nullptr); 13568d75effSDimitry Andric 13668d75effSDimitry Andric // The first sample goes at [1] to reserve [0] for the magic number. 13768d75effSDimitry Andric next_index_ = 1 + num_guards; 13868d75effSDimitry Andric 13968d75effSDimitry Andric zx_status_t status = _zx_vmo_create(DataSize(), ZX_VMO_RESIZABLE, &vmo_); 14068d75effSDimitry Andric CHECK_EQ(status, ZX_OK); 14168d75effSDimitry Andric 14268d75effSDimitry Andric // Give the VMO a name including our process KOID so it's easy to spot. 14368d75effSDimitry Andric internal_snprintf(vmo_name_, sizeof(vmo_name_), "%s.%zu", kSancovSinkName, 14468d75effSDimitry Andric internal_getpid()); 14568d75effSDimitry Andric _zx_object_set_property(vmo_, ZX_PROP_NAME, vmo_name_, 14668d75effSDimitry Andric internal_strlen(vmo_name_)); 1475ffd83dbSDimitry Andric uint64_t size = DataSize(); 1485ffd83dbSDimitry Andric status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size, 1495ffd83dbSDimitry Andric sizeof(size)); 1505ffd83dbSDimitry Andric CHECK_EQ(status, ZX_OK); 15168d75effSDimitry Andric 15268d75effSDimitry Andric // Map the largest possible view we might need into the VMO. Later 15368d75effSDimitry Andric // we might need to increase the VMO's size before we can use larger 15468d75effSDimitry Andric // indices, but we'll never move the mapping address so we don't have 15568d75effSDimitry Andric // any multi-thread synchronization issues with that. 15668d75effSDimitry Andric uintptr_t mapping; 15768d75effSDimitry Andric status = 15868d75effSDimitry Andric _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 15968d75effSDimitry Andric 0, vmo_, 0, MappingSize, &mapping); 16068d75effSDimitry Andric CHECK_EQ(status, ZX_OK); 16168d75effSDimitry Andric 16268d75effSDimitry Andric // Hereafter other threads are free to start storing into 16368d75effSDimitry Andric // elements [1, next_index_) of the big array. 16468d75effSDimitry Andric array_ = reinterpret_cast<uptr *>(mapping); 16568d75effSDimitry Andric 16668d75effSDimitry Andric // Store the magic number. 16768d75effSDimitry Andric // Hereafter, the VMO serves as the contents of the '.sancov' file. 16868d75effSDimitry Andric array_[0] = Magic64; 16968d75effSDimitry Andric 17068d75effSDimitry Andric return 1; 17168d75effSDimitry Andric } else { 17268d75effSDimitry Andric // The VMO is already mapped in, but it's not big enough to use the 17368d75effSDimitry Andric // new indices. So increase the size to cover the new maximum index. 17468d75effSDimitry Andric 17568d75effSDimitry Andric CHECK_NE(vmo_, ZX_HANDLE_INVALID); 17668d75effSDimitry Andric CHECK_NE(array_, nullptr); 17768d75effSDimitry Andric 17868d75effSDimitry Andric uint32_t first_index = next_index_; 17968d75effSDimitry Andric next_index_ += num_guards; 18068d75effSDimitry Andric 18168d75effSDimitry Andric zx_status_t status = _zx_vmo_set_size(vmo_, DataSize()); 18268d75effSDimitry Andric CHECK_EQ(status, ZX_OK); 1835ffd83dbSDimitry Andric uint64_t size = DataSize(); 1845ffd83dbSDimitry Andric status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size, 1855ffd83dbSDimitry Andric sizeof(size)); 1865ffd83dbSDimitry Andric CHECK_EQ(status, ZX_OK); 18768d75effSDimitry Andric 18868d75effSDimitry Andric return first_index; 18968d75effSDimitry Andric } 19068d75effSDimitry Andric } 19168d75effSDimitry Andric }; 19268d75effSDimitry Andric 19368d75effSDimitry Andric static TracePcGuardController pc_guard_controller; 19468d75effSDimitry Andric 19568d75effSDimitry Andric } // namespace 19668d75effSDimitry Andric } // namespace __sancov 19768d75effSDimitry Andric 19868d75effSDimitry Andric namespace __sanitizer { 19968d75effSDimitry Andric void InitializeCoverage(bool enabled, const char *dir) { 20068d75effSDimitry Andric CHECK_EQ(enabled, common_flags()->coverage); 20168d75effSDimitry Andric CHECK_EQ(dir, common_flags()->coverage_dir); 20268d75effSDimitry Andric 20368d75effSDimitry Andric static bool coverage_enabled = false; 20468d75effSDimitry Andric if (!coverage_enabled) { 20568d75effSDimitry Andric coverage_enabled = enabled; 20668d75effSDimitry Andric Atexit(__sanitizer_cov_dump); 20768d75effSDimitry Andric AddDieCallback(__sanitizer_cov_dump); 20868d75effSDimitry Andric } 20968d75effSDimitry Andric } 21068d75effSDimitry Andric } // namespace __sanitizer 21168d75effSDimitry Andric 21268d75effSDimitry Andric extern "C" { 21368d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage(const uptr *pcs, 21468d75effSDimitry Andric uptr len) { 21568d75effSDimitry Andric UNIMPLEMENTED(); 21668d75effSDimitry Andric } 21768d75effSDimitry Andric 21868d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32 *guard) { 2195ffd83dbSDimitry Andric if (!*guard) 2205ffd83dbSDimitry Andric return; 22168d75effSDimitry Andric __sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1); 22268d75effSDimitry Andric } 22368d75effSDimitry Andric 22468d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init, 22568d75effSDimitry Andric u32 *start, u32 *end) { 2265ffd83dbSDimitry Andric if (start == end || *start) 2275ffd83dbSDimitry Andric return; 22868d75effSDimitry Andric __sancov::pc_guard_controller.InitTracePcGuard(start, end); 22968d75effSDimitry Andric } 23068d75effSDimitry Andric 23168d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() { 23268d75effSDimitry Andric __sancov::pc_guard_controller.Dump(); 23368d75effSDimitry Andric } 23468d75effSDimitry Andric SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { 23568d75effSDimitry Andric __sanitizer_dump_trace_pc_guard_coverage(); 23668d75effSDimitry Andric } 23768d75effSDimitry Andric // Default empty implementations (weak). Users should redefine them. 23868d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp, void) {} 23968d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp1, void) {} 24068d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp2, void) {} 24168d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp4, void) {} 24268d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp8, void) {} 24368d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp1, void) {} 24468d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp2, void) {} 24568d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp4, void) {} 24668d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp8, void) {} 24768d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_switch, void) {} 24868d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div4, void) {} 24968d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div8, void) {} 25068d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_gep, void) {} 25168d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {} 25268d75effSDimitry Andric } // extern "C" 25368d75effSDimitry Andric 25468d75effSDimitry Andric #endif // !SANITIZER_FUCHSIA 255