|
| 1 | +//===-- sanitizer_coverage.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 | +// |
| 10 | +// Sanitizer Coverage. |
| 11 | +// This file implements run-time support for a poor man's coverage tool. |
| 12 | +// |
| 13 | +// Compiler instrumentation: |
| 14 | +// For every function F the compiler injects the following code: |
| 15 | +// if (*Guard) { |
| 16 | +// __sanitizer_cov(&F); |
| 17 | +// *Guard = 1; |
| 18 | +// } |
| 19 | +// It's fine to call __sanitizer_cov more than once for a given function. |
| 20 | +// |
| 21 | +// Run-time: |
| 22 | +// - __sanitizer_cov(pc): record that we've executed a given PC. |
| 23 | +// - __sanitizer_cov_dump: dump the coverage data to disk. |
| 24 | +// For every module of the current process that has coverage data |
| 25 | +// this will create a file module_name.PID.sancov. The file format is simple: |
| 26 | +// it's just a sorted sequence of 4-byte offsets in the module. |
| 27 | +// |
| 28 | +// Eventually, this coverage implementation should be obsoleted by a more |
| 29 | +// powerful general purpose Clang/LLVM coverage instrumentation. |
| 30 | +// Consider this implementation as prototype. |
| 31 | +// |
| 32 | +// FIXME: support (or at least test with) dlclose. |
| 33 | +//===----------------------------------------------------------------------===// |
| 34 | + |
| 35 | +#include "sanitizer_allocator_internal.h" |
| 36 | +#include "sanitizer_common.h" |
| 37 | +#include "sanitizer_libc.h" |
| 38 | +#include "sanitizer_mutex.h" |
| 39 | +#include "sanitizer_procmaps.h" |
| 40 | +#include "sanitizer_flags.h" |
| 41 | + |
| 42 | +struct CovData { |
| 43 | + BlockingMutex mu; |
| 44 | + InternalMmapVector<uptr> v; |
| 45 | +}; |
| 46 | + |
| 47 | +static uptr cov_data_placeholder[sizeof(CovData) / sizeof(uptr)]; |
| 48 | +COMPILER_CHECK(sizeof(cov_data_placeholder) >= sizeof(CovData)); |
| 49 | +static CovData *cov_data = reinterpret_cast<CovData*>(cov_data_placeholder); |
| 50 | + |
| 51 | +namespace __sanitizer { |
| 52 | + |
| 53 | +// Simply add the pc into the vector under lock. If the function is called more |
| 54 | +// than once for a given PC it will be inserted multiple times, which is fine. |
| 55 | +static void CovAdd(uptr pc) { |
| 56 | + BlockingMutexLock lock(&cov_data->mu); |
| 57 | + cov_data->v.push_back(pc); |
| 58 | +} |
| 59 | + |
| 60 | +static inline bool CompareLess(const uptr &a, const uptr &b) { |
| 61 | + return a < b; |
| 62 | +} |
| 63 | + |
| 64 | +// Dump the coverage on disk. |
| 65 | +void CovDump() { |
| 66 | +#if !SANITIZER_WINDOWS |
| 67 | + BlockingMutexLock lock(&cov_data->mu); |
| 68 | + InternalMmapVector<uptr> &v = cov_data->v; |
| 69 | + InternalSort(&v, v.size(), CompareLess); |
| 70 | + InternalMmapVector<u32> offsets(v.size()); |
| 71 | + const uptr *vb = v.data(); |
| 72 | + const uptr *ve = vb + v.size(); |
| 73 | + MemoryMappingLayout proc_maps(/*cache_enabled*/false); |
| 74 | + uptr mb, me, off, prot; |
| 75 | + InternalScopedBuffer<char> module(4096); |
| 76 | + InternalScopedBuffer<char> path(4096 * 2); |
| 77 | + for (int i = 0; |
| 78 | + proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 79 | + i++) { |
| 80 | + if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 81 | + continue; |
| 82 | + if (vb >= ve) break; |
| 83 | + if (mb <= *vb && *vb < me) { |
| 84 | + offsets.clear(); |
| 85 | + const uptr *old_vb = vb; |
| 86 | + CHECK_LE(off, *vb); |
| 87 | + for (; vb < ve && *vb < me; vb++) { |
| 88 | + uptr diff = *vb - (i ? mb : 0) + off; |
| 89 | + CHECK_LE(diff, 0xffffffffU); |
| 90 | + offsets.push_back(static_cast<u32>(diff)); |
| 91 | + } |
| 92 | + char *module_name = StripModuleName(module.data()); |
| 93 | + internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov", |
| 94 | + module_name, internal_getpid()); |
| 95 | + InternalFree(module_name); |
| 96 | + uptr fd = OpenFile(path.data(), true); |
| 97 | + internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 98 | + internal_close(fd); |
| 99 | + if (common_flags()->verbosity) |
| 100 | + Report(" CovDump: %s: %zd PCs written\n", path.data(), vb - old_vb); |
| 101 | + } |
| 102 | + } |
| 103 | +#endif // !SANITIZER_WINDOWS |
| 104 | +} |
| 105 | + |
| 106 | +} // namespace __sanitizer |
| 107 | + |
| 108 | +extern "C" { |
| 109 | +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov(void *pc) { |
| 110 | + CovAdd(reinterpret_cast<uptr>(pc)); |
| 111 | +} |
| 112 | +SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
| 113 | +} // extern "C" |
0 commit comments