blob: d7da6c9b1fb958ff8dc2bb5b90b82ce943820267 [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"
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +000041#include "sanitizer_symbolizer.h"
Bob Wilsona08e9ac2013-11-15 07:18:15 +000042#include "sanitizer_flags.h"
43
Kostya Serebryany183cb6e2014-11-14 23:15:55 +000044static atomic_uint32_t dump_once_guard; // Ensure that CovDump runs only once.
45
46static atomic_uintptr_t coverage_counter;
Bob Wilsona08e9ac2013-11-15 07:18:15 +000047
Kostya Serebryany8b530e12014-04-30 10:40:48 +000048// pc_array is the array containing the covered PCs.
Sergey Matveev6cb47a082014-05-19 12:53:03 +000049// To make the pc_array thread- and async-signal-safe it has to be large enough.
Kostya Serebryany8b530e12014-04-30 10:40:48 +000050// 128M counters "ought to be enough for anybody" (4M on 32-bit).
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000051
52// With coverage_direct=1 in ASAN_OPTIONS, pc_array memory is mapped to a file.
53// In this mode, __sanitizer_cov_dump does nothing, and CovUpdateMapping()
54// dump current memory layout to another file.
Bob Wilsona08e9ac2013-11-15 07:18:15 +000055
Sergey Matveev6cb47a082014-05-19 12:53:03 +000056static bool cov_sandboxed = false;
57static int cov_fd = kInvalidFd;
58static unsigned int cov_max_block_size = 0;
59
Bob Wilsona08e9ac2013-11-15 07:18:15 +000060namespace __sanitizer {
61
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000062class CoverageData {
63 public:
64 void Init();
Evgeniy Stepanovfe181022014-06-04 12:13:54 +000065 void BeforeFork();
66 void AfterFork(int child_pid);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000067 void Extend(uptr npcs);
68 void Add(uptr pc);
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +000069 void IndirCall(uptr caller, uptr callee, uptr callee_cache[],
70 uptr cache_size);
71 void DumpCallerCalleePairs();
Evgeniy Stepanov567e5162014-05-27 12:37:52 +000072
73 uptr *data();
74 uptr size();
75
76 private:
77 // Maximal size pc array may ever grow.
78 // We MmapNoReserve this space to ensure that the array is contiguous.
79 static const uptr kPcArrayMaxSize = FIRST_32_SECOND_64(1 << 22, 1 << 27);
80 // The amount file mapping for the pc array is grown by.
81 static const uptr kPcArrayMmapSize = 64 * 1024;
82
83 // pc_array is allocated with MmapNoReserveOrDie and so it uses only as
84 // much RAM as it really needs.
85 uptr *pc_array;
86 // Index of the first available pc_array slot.
87 atomic_uintptr_t pc_array_index;
88 // Array size.
89 atomic_uintptr_t pc_array_size;
90 // Current file mapped size of the pc array.
91 uptr pc_array_mapped_size;
92 // Descriptor of the file mapped pc array.
93 int pc_fd;
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +000094
95 // Caller-Callee (cc) array, size and current index.
96 static const uptr kCcArrayMaxSize = FIRST_32_SECOND_64(1 << 18, 1 << 24);
97 uptr **cc_array;
98 atomic_uintptr_t cc_array_index;
99 atomic_uintptr_t cc_array_size;
100
101
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000102 StaticSpinMutex mu;
103
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000104 void DirectOpen();
Evgeniy Stepanovfe181022014-06-04 12:13:54 +0000105 void ReInit();
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000106};
107
108static CoverageData coverage_data;
109
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000110void CoverageData::DirectOpen() {
Evgeniy Stepanovfa5c0752014-05-29 14:33:16 +0000111 InternalScopedString path(1024);
112 internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.raw",
113 common_flags()->coverage_dir, internal_getpid());
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000114 pc_fd = OpenFile(path.data(), true);
115 if (internal_iserror(pc_fd)) {
116 Report(" Coverage: failed to open %s for writing\n", path.data());
117 Die();
118 }
119
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000120 pc_array_mapped_size = 0;
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000121 CovUpdateMapping();
122}
123
124void CoverageData::Init() {
125 pc_array = reinterpret_cast<uptr *>(
126 MmapNoReserveOrDie(sizeof(uptr) * kPcArrayMaxSize, "CovInit"));
Evgeniy Stepanovfe181022014-06-04 12:13:54 +0000127 pc_fd = kInvalidFd;
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000128 if (common_flags()->coverage_direct) {
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000129 atomic_store(&pc_array_size, 0, memory_order_relaxed);
130 atomic_store(&pc_array_index, 0, memory_order_relaxed);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000131 } else {
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000132 atomic_store(&pc_array_size, kPcArrayMaxSize, memory_order_relaxed);
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000133 atomic_store(&pc_array_index, 0, memory_order_relaxed);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000134 }
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000135
136 cc_array = reinterpret_cast<uptr **>(MmapNoReserveOrDie(
137 sizeof(uptr *) * kCcArrayMaxSize, "CovInit::cc_array"));
138 atomic_store(&cc_array_size, kCcArrayMaxSize, memory_order_relaxed);
139 atomic_store(&cc_array_index, 0, memory_order_relaxed);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000140}
141
Evgeniy Stepanovfe181022014-06-04 12:13:54 +0000142void CoverageData::ReInit() {
143 internal_munmap(pc_array, sizeof(uptr) * kPcArrayMaxSize);
144 if (pc_fd != kInvalidFd) internal_close(pc_fd);
145 if (common_flags()->coverage_direct) {
146 // In memory-mapped mode we must extend the new file to the known array
147 // size.
148 uptr size = atomic_load(&pc_array_size, memory_order_relaxed);
149 Init();
150 if (size) Extend(size);
151 } else {
152 Init();
153 }
154}
155
156void CoverageData::BeforeFork() {
157 mu.Lock();
158}
159
160void CoverageData::AfterFork(int child_pid) {
161 // We are single-threaded so it's OK to release the lock early.
162 mu.Unlock();
163 if (child_pid == 0) ReInit();
164}
165
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000166// Extend coverage PC array to fit additional npcs elements.
167void CoverageData::Extend(uptr npcs) {
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000168 if (!common_flags()->coverage_direct) return;
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000169 SpinMutexLock l(&mu);
170
Evgeniy Stepanovfe181022014-06-04 12:13:54 +0000171 if (pc_fd == kInvalidFd) DirectOpen();
172 CHECK_NE(pc_fd, kInvalidFd);
Evgeniy Stepanovce984522014-06-03 15:27:15 +0000173
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000174 uptr size = atomic_load(&pc_array_size, memory_order_relaxed);
175 size += npcs * sizeof(uptr);
176
177 if (size > pc_array_mapped_size) {
178 uptr new_mapped_size = pc_array_mapped_size;
179 while (size > new_mapped_size) new_mapped_size += kPcArrayMmapSize;
180
181 // Extend the file and map the new space at the end of pc_array.
182 uptr res = internal_ftruncate(pc_fd, new_mapped_size);
183 int err;
184 if (internal_iserror(res, &err)) {
185 Printf("failed to extend raw coverage file: %d\n", err);
186 Die();
187 }
188 void *p = MapWritableFileToMemory(pc_array + pc_array_mapped_size,
189 new_mapped_size - pc_array_mapped_size,
190 pc_fd, pc_array_mapped_size);
191 CHECK_EQ(p, pc_array + pc_array_mapped_size);
192 pc_array_mapped_size = new_mapped_size;
193 }
194
195 atomic_store(&pc_array_size, size, memory_order_release);
196}
197
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000198// Simply add the pc into the vector under lock. If the function is called more
199// than once for a given PC it will be inserted multiple times, which is fine.
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000200void CoverageData::Add(uptr pc) {
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000201 if (!pc_array) return;
202 uptr idx = atomic_fetch_add(&pc_array_index, 1, memory_order_relaxed);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000203 CHECK_LT(idx * sizeof(uptr),
204 atomic_load(&pc_array_size, memory_order_acquire));
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000205 pc_array[idx] = pc;
Kostya Serebryany183cb6e2014-11-14 23:15:55 +0000206 atomic_fetch_add(&coverage_counter, 1, memory_order_relaxed);
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000207}
208
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000209// Registers a pair caller=>callee.
210// When a given caller is seen for the first time, the callee_cache is added
211// to the global array cc_array, callee_cache[0] is set to caller and
212// callee_cache[1] is set to cache_size.
213// Then we are trying to add callee to callee_cache [2,cache_size) if it is
214// not there yet.
215// If the cache is full we drop the callee (may want to fix this later).
216void CoverageData::IndirCall(uptr caller, uptr callee, uptr callee_cache[],
217 uptr cache_size) {
218 if (!cc_array) return;
219 atomic_uintptr_t *atomic_callee_cache =
220 reinterpret_cast<atomic_uintptr_t *>(callee_cache);
221 uptr zero = 0;
222 if (atomic_compare_exchange_strong(&atomic_callee_cache[0], &zero, caller,
223 memory_order_seq_cst)) {
224 uptr idx = atomic_fetch_add(&cc_array_index, 1, memory_order_relaxed);
225 CHECK_LT(idx * sizeof(uptr),
226 atomic_load(&cc_array_size, memory_order_acquire));
227 callee_cache[1] = cache_size;
228 cc_array[idx] = callee_cache;
229 }
230 CHECK_EQ(atomic_load(&atomic_callee_cache[0], memory_order_relaxed), caller);
231 for (uptr i = 2; i < cache_size; i++) {
232 uptr was = 0;
233 if (atomic_compare_exchange_strong(&atomic_callee_cache[i], &was, callee,
Kostya Serebryany183cb6e2014-11-14 23:15:55 +0000234 memory_order_seq_cst)) {
235 atomic_fetch_add(&coverage_counter, 1, memory_order_relaxed);
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000236 return;
Kostya Serebryany183cb6e2014-11-14 23:15:55 +0000237 }
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000238 if (was == callee) // Already have this callee.
239 return;
240 }
241}
242
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000243uptr *CoverageData::data() {
244 return pc_array;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000245}
246
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000247uptr CoverageData::size() {
248 return atomic_load(&pc_array_index, memory_order_relaxed);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000249}
250
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000251// Block layout for packed file format: header, followed by module name (no
252// trailing zero), followed by data blob.
253struct CovHeader {
254 int pid;
255 unsigned int module_name_length;
256 unsigned int data_length;
257};
258
259static void CovWritePacked(int pid, const char *module, const void *blob,
260 unsigned int blob_size) {
Sergey Matveev83f91e72014-05-21 13:43:52 +0000261 if (cov_fd < 0) return;
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000262 unsigned module_name_length = internal_strlen(module);
263 CovHeader header = {pid, module_name_length, blob_size};
264
265 if (cov_max_block_size == 0) {
266 // Writing to a file. Just go ahead.
267 internal_write(cov_fd, &header, sizeof(header));
268 internal_write(cov_fd, module, module_name_length);
269 internal_write(cov_fd, blob, blob_size);
270 } else {
271 // Writing to a socket. We want to split the data into appropriately sized
272 // blocks.
273 InternalScopedBuffer<char> block(cov_max_block_size);
274 CHECK_EQ((uptr)block.data(), (uptr)(CovHeader *)block.data());
275 uptr header_size_with_module = sizeof(header) + module_name_length;
276 CHECK_LT(header_size_with_module, cov_max_block_size);
277 unsigned int max_payload_size =
278 cov_max_block_size - header_size_with_module;
279 char *block_pos = block.data();
280 internal_memcpy(block_pos, &header, sizeof(header));
281 block_pos += sizeof(header);
282 internal_memcpy(block_pos, module, module_name_length);
283 block_pos += module_name_length;
284 char *block_data_begin = block_pos;
Alexey Samsonov4925fd42014-11-13 22:40:59 +0000285 const char *blob_pos = (const char *)blob;
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000286 while (blob_size > 0) {
287 unsigned int payload_size = Min(blob_size, max_payload_size);
288 blob_size -= payload_size;
289 internal_memcpy(block_data_begin, blob_pos, payload_size);
290 blob_pos += payload_size;
291 ((CovHeader *)block.data())->data_length = payload_size;
292 internal_write(cov_fd, block.data(),
293 header_size_with_module + payload_size);
294 }
295 }
296}
297
Sergey Matveev83f91e72014-05-21 13:43:52 +0000298// If packed = false: <name>.<pid>.<sancov> (name = module name).
299// If packed = true and name == 0: <pid>.<sancov>.<packed>.
300// If packed = true and name != 0: <name>.<sancov>.<packed> (name is
301// user-supplied).
302static int CovOpenFile(bool packed, const char* name) {
303 InternalScopedBuffer<char> path(1024);
304 if (!packed) {
305 CHECK(name);
Evgeniy Stepanovfa5c0752014-05-29 14:33:16 +0000306 internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov",
307 common_flags()->coverage_dir, name, internal_getpid());
Sergey Matveev83f91e72014-05-21 13:43:52 +0000308 } else {
309 if (!name)
Evgeniy Stepanovfa5c0752014-05-29 14:33:16 +0000310 internal_snprintf((char *)path.data(), path.size(),
311 "%s/%zd.sancov.packed", common_flags()->coverage_dir,
Sergey Matveev83f91e72014-05-21 13:43:52 +0000312 internal_getpid());
313 else
Evgeniy Stepanovfa5c0752014-05-29 14:33:16 +0000314 internal_snprintf((char *)path.data(), path.size(), "%s/%s.sancov.packed",
315 common_flags()->coverage_dir, name);
Sergey Matveev83f91e72014-05-21 13:43:52 +0000316 }
317 uptr fd = OpenFile(path.data(), true);
318 if (internal_iserror(fd)) {
319 Report(" SanitizerCoverage: failed to open %s for writing\n", path.data());
320 return -1;
321 }
322 return fd;
323}
324
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000325// This function dumps the caller=>callee pairs into a file as a sequence of
326// lines like "module_name offset".
327void CoverageData::DumpCallerCalleePairs() {
328 uptr max_idx = atomic_load(&cc_array_index, memory_order_relaxed);
329 if (!max_idx) return;
330 auto sym = Symbolizer::GetOrInit();
331 if (!sym)
332 return;
Kostya Serebryany40aa4a22014-10-31 19:49:46 +0000333 InternalScopedString out(32 << 20);
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000334 uptr total = 0;
335 for (uptr i = 0; i < max_idx; i++) {
336 uptr *cc_cache = cc_array[i];
337 CHECK(cc_cache);
338 uptr caller = cc_cache[0];
339 uptr n_callees = cc_cache[1];
340 const char *caller_module_name = "<unknown>";
341 uptr caller_module_address = 0;
342 sym->GetModuleNameAndOffsetForPC(caller, &caller_module_name,
343 &caller_module_address);
344 for (uptr j = 2; j < n_callees; j++) {
345 uptr callee = cc_cache[j];
346 if (!callee) break;
347 total++;
348 const char *callee_module_name = "<unknown>";
349 uptr callee_module_address = 0;
350 sym->GetModuleNameAndOffsetForPC(callee, &callee_module_name,
351 &callee_module_address);
352 out.append("%s 0x%zx\n%s 0x%zx\n", caller_module_name,
353 caller_module_address, callee_module_name,
354 callee_module_address);
355 }
356 }
357 int fd = CovOpenFile(false, "caller-callee");
358 if (fd < 0) return;
359 internal_write(fd, out.data(), out.length());
360 internal_close(fd);
361 VReport(1, " CovDump: %zd caller-callee pairs written\n", total);
362}
363
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000364// Dump the coverage on disk.
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000365static void CovDump() {
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000366 if (!common_flags()->coverage || common_flags()->coverage_direct) return;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000367#if !SANITIZER_WINDOWS
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000368 if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed))
369 return;
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000370 uptr size = coverage_data.size();
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000371 InternalMmapVector<u32> offsets(size);
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000372 uptr *vb = coverage_data.data();
373 uptr *ve = vb + size;
374 SortArray(vb, size);
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000375 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000376 uptr mb, me, off, prot;
377 InternalScopedBuffer<char> module(4096);
378 InternalScopedBuffer<char> path(4096 * 2);
379 for (int i = 0;
380 proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot);
381 i++) {
382 if ((prot & MemoryMappingLayout::kProtectionExecute) == 0)
383 continue;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000384 while (vb < ve && *vb < mb) vb++;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000385 if (vb >= ve) break;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000386 if (*vb < me) {
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000387 offsets.clear();
388 const uptr *old_vb = vb;
389 CHECK_LE(off, *vb);
390 for (; vb < ve && *vb < me; vb++) {
391 uptr diff = *vb - (i ? mb : 0) + off;
392 CHECK_LE(diff, 0xffffffffU);
393 offsets.push_back(static_cast<u32>(diff));
394 }
Alexey Samsonov26ca05a2014-11-04 19:34:29 +0000395 const char *module_name = StripModuleName(module.data());
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000396 if (cov_sandboxed) {
Sergey Matveev83f91e72014-05-21 13:43:52 +0000397 if (cov_fd >= 0) {
398 CovWritePacked(internal_getpid(), module_name, offsets.data(),
399 offsets.size() * sizeof(u32));
400 VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb);
401 }
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000402 } else {
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000403 // One file per module per process.
Evgeniy Stepanovfa5c0752014-05-29 14:33:16 +0000404 internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov",
405 common_flags()->coverage_dir, module_name,
406 internal_getpid());
Sergey Matveev83f91e72014-05-21 13:43:52 +0000407 int fd = CovOpenFile(false /* packed */, module_name);
408 if (fd > 0) {
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000409 internal_write(fd, offsets.data(), offsets.size() * sizeof(u32));
410 internal_close(fd);
411 VReport(1, " CovDump: %s: %zd PCs written\n", path.data(),
412 vb - old_vb);
413 }
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000414 }
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000415 }
416 }
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000417 if (cov_fd >= 0)
418 internal_close(cov_fd);
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000419 coverage_data.DumpCallerCalleePairs();
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000420#endif // !SANITIZER_WINDOWS
421}
422
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000423void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
424 if (!args) return;
425 if (!common_flags()->coverage) return;
426 cov_sandboxed = args->coverage_sandboxed;
427 if (!cov_sandboxed) return;
428 cov_fd = args->coverage_fd;
429 cov_max_block_size = args->coverage_max_block_size;
430 if (cov_fd < 0)
431 // Pre-open the file now. The sandbox won't allow us to do it later.
Sergey Matveev83f91e72014-05-21 13:43:52 +0000432 cov_fd = CovOpenFile(true /* packed */, 0);
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000433}
434
Sergey Matveev83f91e72014-05-21 13:43:52 +0000435int MaybeOpenCovFile(const char *name) {
436 CHECK(name);
437 if (!common_flags()->coverage) return -1;
438 return CovOpenFile(true /* packed */, name);
439}
Evgeniy Stepanovfe181022014-06-04 12:13:54 +0000440
441void CovBeforeFork() {
442 coverage_data.BeforeFork();
443}
444
445void CovAfterFork(int child_pid) {
446 coverage_data.AfterFork(child_pid);
447}
448
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000449} // namespace __sanitizer
450
451extern "C" {
Kostya Serebryany714c67c2014-01-17 11:00:30 +0000452SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() {
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000453 coverage_data.Add(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()));
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000454}
Kostya Serebryanyb6eae0c2014-10-31 17:19:11 +0000455SANITIZER_INTERFACE_ATTRIBUTE void
456__sanitizer_cov_indir_call16(uptr callee, uptr callee_cache16[]) {
457 coverage_data.IndirCall(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()),
458 callee, callee_cache16, 16);
459}
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000460SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); }
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000461SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() {
462 coverage_data.Init();
463}
464SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_module_init(uptr npcs) {
Evgeniy Stepanovdfa54392014-06-11 15:11:26 +0000465 if (!common_flags()->coverage || !common_flags()->coverage_direct) return;
466 if (SANITIZER_ANDROID) {
Evgeniy Stepanov38c228a2014-06-05 14:38:53 +0000467 // dlopen/dlclose interceptors do not work on Android, so we rely on
468 // Extend() calls to update .sancov.map.
469 CovUpdateMapping(GET_CALLER_PC());
470 }
Evgeniy Stepanov567e5162014-05-27 12:37:52 +0000471 coverage_data.Extend(npcs);
472}
Sergey Matveev83f91e72014-05-21 13:43:52 +0000473SANITIZER_INTERFACE_ATTRIBUTE
474sptr __sanitizer_maybe_open_cov_file(const char *name) {
475 return MaybeOpenCovFile(name);
476}
Kostya Serebryany183cb6e2014-11-14 23:15:55 +0000477SANITIZER_INTERFACE_ATTRIBUTE
478uptr __sanitizer_get_total_unique_coverage() {
479 return atomic_load(&coverage_counter, memory_order_relaxed);
480}
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000481} // extern "C"