blob: 0f6435d2b9d23e0a460ba48a0a28673d30cc69a6 [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
Kostya Serebryany8b530e12014-04-30 10:40:48 +000043atomic_uint32_t dump_once_guard; // Ensure that CovDump runs only once.
Bob Wilsona08e9ac2013-11-15 07:18:15 +000044
Kostya Serebryany8b530e12014-04-30 10:40:48 +000045// pc_array is the array containing the covered PCs.
Sergey Matveev6cb47a082014-05-19 12:53:03 +000046// To make the pc_array thread- and async-signal-safe it has to be large enough.
Kostya Serebryany8b530e12014-04-30 10:40:48 +000047// 128M counters "ought to be enough for anybody" (4M on 32-bit).
48// pc_array is allocated with MmapNoReserveOrDie and so it uses only as
49// much RAM as it really needs.
50static const uptr kPcArraySize = FIRST_32_SECOND_64(1 << 22, 1 << 27);
51static uptr *pc_array;
52static atomic_uintptr_t pc_array_index;
Bob Wilsona08e9ac2013-11-15 07:18:15 +000053
Sergey Matveev6cb47a082014-05-19 12:53:03 +000054static bool cov_sandboxed = false;
55static int cov_fd = kInvalidFd;
56static unsigned int cov_max_block_size = 0;
57
Bob Wilsona08e9ac2013-11-15 07:18:15 +000058namespace __sanitizer {
59
60// Simply add the pc into the vector under lock. If the function is called more
61// than once for a given PC it will be inserted multiple times, which is fine.
62static void CovAdd(uptr pc) {
Kostya Serebryany8b530e12014-04-30 10:40:48 +000063 if (!pc_array) return;
64 uptr idx = atomic_fetch_add(&pc_array_index, 1, memory_order_relaxed);
65 CHECK_LT(idx, kPcArraySize);
66 pc_array[idx] = pc;
67}
68
69void CovInit() {
70 pc_array = reinterpret_cast<uptr *>(
71 MmapNoReserveOrDie(sizeof(uptr) * kPcArraySize, "CovInit"));
Bob Wilsona08e9ac2013-11-15 07:18:15 +000072}
73
74static inline bool CompareLess(const uptr &a, const uptr &b) {
75 return a < b;
76}
77
Sergey Matveev6cb47a082014-05-19 12:53:03 +000078// Block layout for packed file format: header, followed by module name (no
79// trailing zero), followed by data blob.
80struct CovHeader {
81 int pid;
82 unsigned int module_name_length;
83 unsigned int data_length;
84};
85
86static void CovWritePacked(int pid, const char *module, const void *blob,
87 unsigned int blob_size) {
Sergey Matveev83f91e72014-05-21 13:43:52 +000088 if (cov_fd < 0) return;
Sergey Matveev6cb47a082014-05-19 12:53:03 +000089 unsigned module_name_length = internal_strlen(module);
90 CovHeader header = {pid, module_name_length, blob_size};
91
92 if (cov_max_block_size == 0) {
93 // Writing to a file. Just go ahead.
94 internal_write(cov_fd, &header, sizeof(header));
95 internal_write(cov_fd, module, module_name_length);
96 internal_write(cov_fd, blob, blob_size);
97 } else {
98 // Writing to a socket. We want to split the data into appropriately sized
99 // blocks.
100 InternalScopedBuffer<char> block(cov_max_block_size);
101 CHECK_EQ((uptr)block.data(), (uptr)(CovHeader *)block.data());
102 uptr header_size_with_module = sizeof(header) + module_name_length;
103 CHECK_LT(header_size_with_module, cov_max_block_size);
104 unsigned int max_payload_size =
105 cov_max_block_size - header_size_with_module;
106 char *block_pos = block.data();
107 internal_memcpy(block_pos, &header, sizeof(header));
108 block_pos += sizeof(header);
109 internal_memcpy(block_pos, module, module_name_length);
110 block_pos += module_name_length;
111 char *block_data_begin = block_pos;
112 char *blob_pos = (char *)blob;
113 while (blob_size > 0) {
114 unsigned int payload_size = Min(blob_size, max_payload_size);
115 blob_size -= payload_size;
116 internal_memcpy(block_data_begin, blob_pos, payload_size);
117 blob_pos += payload_size;
118 ((CovHeader *)block.data())->data_length = payload_size;
119 internal_write(cov_fd, block.data(),
120 header_size_with_module + payload_size);
121 }
122 }
123}
124
Sergey Matveev83f91e72014-05-21 13:43:52 +0000125// If packed = false: <name>.<pid>.<sancov> (name = module name).
126// If packed = true and name == 0: <pid>.<sancov>.<packed>.
127// If packed = true and name != 0: <name>.<sancov>.<packed> (name is
128// user-supplied).
129static int CovOpenFile(bool packed, const char* name) {
130 InternalScopedBuffer<char> path(1024);
131 if (!packed) {
132 CHECK(name);
133 internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov",
134 name, internal_getpid());
135 } else {
136 if (!name)
137 internal_snprintf((char *)path.data(), path.size(), "%zd.sancov.packed",
138 internal_getpid());
139 else
140 internal_snprintf((char *)path.data(), path.size(), "%s.sancov.packed",
141 name);
142 }
143 uptr fd = OpenFile(path.data(), true);
144 if (internal_iserror(fd)) {
145 Report(" SanitizerCoverage: failed to open %s for writing\n", path.data());
146 return -1;
147 }
148 return fd;
149}
150
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000151// Dump the coverage on disk.
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000152static void CovDump() {
153 if (!common_flags()->coverage) return;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000154#if !SANITIZER_WINDOWS
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000155 if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed))
156 return;
157 uptr size = atomic_load(&pc_array_index, memory_order_relaxed);
158 InternalSort(&pc_array, size, CompareLess);
159 InternalMmapVector<u32> offsets(size);
160 const uptr *vb = pc_array;
161 const uptr *ve = vb + size;
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000162 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000163 uptr mb, me, off, prot;
164 InternalScopedBuffer<char> module(4096);
165 InternalScopedBuffer<char> path(4096 * 2);
166 for (int i = 0;
167 proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot);
168 i++) {
169 if ((prot & MemoryMappingLayout::kProtectionExecute) == 0)
170 continue;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000171 while (vb < ve && *vb < mb) vb++;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000172 if (vb >= ve) break;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000173 if (*vb < me) {
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000174 offsets.clear();
175 const uptr *old_vb = vb;
176 CHECK_LE(off, *vb);
177 for (; vb < ve && *vb < me; vb++) {
178 uptr diff = *vb - (i ? mb : 0) + off;
179 CHECK_LE(diff, 0xffffffffU);
180 offsets.push_back(static_cast<u32>(diff));
181 }
182 char *module_name = StripModuleName(module.data());
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000183 if (cov_sandboxed) {
Sergey Matveev83f91e72014-05-21 13:43:52 +0000184 if (cov_fd >= 0) {
185 CovWritePacked(internal_getpid(), module_name, offsets.data(),
186 offsets.size() * sizeof(u32));
187 VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb);
188 }
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000189 } else {
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000190 // One file per module per process.
191 internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov",
192 module_name, internal_getpid());
Sergey Matveev83f91e72014-05-21 13:43:52 +0000193 int fd = CovOpenFile(false /* packed */, module_name);
194 if (fd > 0) {
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000195 internal_write(fd, offsets.data(), offsets.size() * sizeof(u32));
196 internal_close(fd);
197 VReport(1, " CovDump: %s: %zd PCs written\n", path.data(),
198 vb - old_vb);
199 }
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000200 }
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000201 InternalFree(module_name);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000202 }
203 }
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000204 if (cov_fd >= 0)
205 internal_close(cov_fd);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000206#endif // !SANITIZER_WINDOWS
207}
208
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000209void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
210 if (!args) return;
211 if (!common_flags()->coverage) return;
212 cov_sandboxed = args->coverage_sandboxed;
213 if (!cov_sandboxed) return;
214 cov_fd = args->coverage_fd;
215 cov_max_block_size = args->coverage_max_block_size;
216 if (cov_fd < 0)
217 // Pre-open the file now. The sandbox won't allow us to do it later.
Sergey Matveev83f91e72014-05-21 13:43:52 +0000218 cov_fd = CovOpenFile(true /* packed */, 0);
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000219}
220
Sergey Matveev83f91e72014-05-21 13:43:52 +0000221int MaybeOpenCovFile(const char *name) {
222 CHECK(name);
223 if (!common_flags()->coverage) return -1;
224 return CovOpenFile(true /* packed */, name);
225}
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000226} // namespace __sanitizer
227
228extern "C" {
Kostya Serebryany714c67c2014-01-17 11:00:30 +0000229SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() {
230 CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()));
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000231}
232SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); }
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000233SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { CovInit(); }
Sergey Matveev83f91e72014-05-21 13:43:52 +0000234SANITIZER_INTERFACE_ATTRIBUTE
235sptr __sanitizer_maybe_open_cov_file(const char *name) {
236 return MaybeOpenCovFile(name);
237}
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000238} // extern "C"