blob: 49235fe5225e652449a2837c896d95b7bcafc95a [file] [log] [blame]
Bob Wilsona08e9ac2013-11-15 07:18:15 +00001//===-- 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 Serebryany714c67c2014-01-17 11:00:30 +000014// For every interesting basic block the compiler injects the following code:
Bob Wilsona08e9ac2013-11-15 07:18:15 +000015// if (*Guard) {
Kostya Serebryany714c67c2014-01-17 11:00:30 +000016// __sanitizer_cov();
Bob Wilsona08e9ac2013-11-15 07:18:15 +000017// *Guard = 1;
18// }
Kostya Serebryany714c67c2014-01-17 11:00:30 +000019// It's fine to call __sanitizer_cov more than once for a given block.
Bob Wilsona08e9ac2013-11-15 07:18:15 +000020//
21// Run-time:
Kostya Serebryany714c67c2014-01-17 11:00:30 +000022// - __sanitizer_cov(): record that we've executed the PC (GET_CALLER_PC).
Bob Wilsona08e9ac2013-11-15 07:18:15 +000023// - __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 Serebryany714c67c2014-01-17 11:00:30 +000040#include "sanitizer_stacktrace.h"
Bob Wilsona08e9ac2013-11-15 07:18:15 +000041#include "sanitizer_flags.h"
42
43struct CovData {
44 BlockingMutex mu;
45 InternalMmapVector<uptr> v;
46};
47
48static uptr cov_data_placeholder[sizeof(CovData) / sizeof(uptr)];
49COMPILER_CHECK(sizeof(cov_data_placeholder) >= sizeof(CovData));
50static CovData *cov_data = reinterpret_cast<CovData*>(cov_data_placeholder);
51
52namespace __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.
56static void CovAdd(uptr pc) {
57 BlockingMutexLock lock(&cov_data->mu);
58 cov_data->v.push_back(pc);
59}
60
61static inline bool CompareLess(const uptr &a, const uptr &b) {
62 return a < b;
63}
64
65// Dump the coverage on disk.
66void 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 Stepanov8ab205f2014-02-12 15:29:22 +000098 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 Wilsona08e9ac2013-11-15 07:18:15 +0000105 }
106 }
107#endif // !SANITIZER_WINDOWS
108}
109
110} // namespace __sanitizer
111
112extern "C" {
Kostya Serebryany714c67c2014-01-17 11:00:30 +0000113SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() {
114 CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()));
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000115}
116SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); }
117} // extern "C"