Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 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: |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 14 | // For every interesting basic block the compiler injects the following code: |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 15 | // if (*Guard) { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 16 | // __sanitizer_cov(); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 17 | // *Guard = 1; |
| 18 | // } |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 19 | // It's fine to call __sanitizer_cov more than once for a given block. |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 20 | // |
| 21 | // Run-time: |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 22 | // - __sanitizer_cov(): record that we've executed the PC (GET_CALLER_PC). |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 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" |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 40 | #include "sanitizer_stacktrace.h" |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 41 | #include "sanitizer_flags.h" |
| 42 | |
| 43 | struct CovData { |
| 44 | BlockingMutex mu; |
| 45 | InternalMmapVector<uptr> v; |
| 46 | }; |
| 47 | |
| 48 | static uptr cov_data_placeholder[sizeof(CovData) / sizeof(uptr)]; |
| 49 | COMPILER_CHECK(sizeof(cov_data_placeholder) >= sizeof(CovData)); |
| 50 | static CovData *cov_data = reinterpret_cast<CovData*>(cov_data_placeholder); |
| 51 | |
| 52 | namespace __sanitizer { |
| 53 | |
| 54 | // Simply add the pc into the vector under lock. If the function is called more |
| 55 | // than once for a given PC it will be inserted multiple times, which is fine. |
| 56 | static void CovAdd(uptr pc) { |
| 57 | BlockingMutexLock lock(&cov_data->mu); |
| 58 | cov_data->v.push_back(pc); |
| 59 | } |
| 60 | |
| 61 | static inline bool CompareLess(const uptr &a, const uptr &b) { |
| 62 | return a < b; |
| 63 | } |
| 64 | |
| 65 | // Dump the coverage on disk. |
| 66 | void CovDump() { |
| 67 | #if !SANITIZER_WINDOWS |
| 68 | BlockingMutexLock lock(&cov_data->mu); |
| 69 | InternalMmapVector<uptr> &v = cov_data->v; |
| 70 | InternalSort(&v, v.size(), CompareLess); |
| 71 | InternalMmapVector<u32> offsets(v.size()); |
| 72 | const uptr *vb = v.data(); |
| 73 | const uptr *ve = vb + v.size(); |
| 74 | MemoryMappingLayout proc_maps(/*cache_enabled*/false); |
| 75 | uptr mb, me, off, prot; |
| 76 | InternalScopedBuffer<char> module(4096); |
| 77 | InternalScopedBuffer<char> path(4096 * 2); |
| 78 | for (int i = 0; |
| 79 | proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 80 | i++) { |
| 81 | if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 82 | continue; |
| 83 | if (vb >= ve) break; |
| 84 | if (mb <= *vb && *vb < me) { |
| 85 | offsets.clear(); |
| 86 | const uptr *old_vb = vb; |
| 87 | CHECK_LE(off, *vb); |
| 88 | for (; vb < ve && *vb < me; vb++) { |
| 89 | uptr diff = *vb - (i ? mb : 0) + off; |
| 90 | CHECK_LE(diff, 0xffffffffU); |
| 91 | offsets.push_back(static_cast<u32>(diff)); |
| 92 | } |
| 93 | char *module_name = StripModuleName(module.data()); |
| 94 | internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov", |
| 95 | module_name, internal_getpid()); |
| 96 | InternalFree(module_name); |
| 97 | uptr fd = OpenFile(path.data(), true); |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame^] | 98 | if (internal_iserror(fd)) { |
| 99 | Report(" CovDump: failed to open %s for writing\n", path.data()); |
| 100 | } else { |
| 101 | internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 102 | internal_close(fd); |
| 103 | VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), vb - old_vb); |
| 104 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | #endif // !SANITIZER_WINDOWS |
| 108 | } |
| 109 | |
| 110 | } // namespace __sanitizer |
| 111 | |
| 112 | extern "C" { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 113 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { |
| 114 | CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 115 | } |
| 116 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
| 117 | } // extern "C" |