blob: cb0644f6df162e1a3dd59191ba7f0df0ad6a9308 [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) {
88 CHECK_GE(cov_fd, 0);
89 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
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000125// Dump the coverage on disk.
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000126static void CovDump() {
127 if (!common_flags()->coverage) return;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000128#if !SANITIZER_WINDOWS
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000129 if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed))
130 return;
131 uptr size = atomic_load(&pc_array_index, memory_order_relaxed);
132 InternalSort(&pc_array, size, CompareLess);
133 InternalMmapVector<u32> offsets(size);
134 const uptr *vb = pc_array;
135 const uptr *ve = vb + size;
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000136 MemoryMappingLayout proc_maps(/*cache_enabled*/true);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000137 uptr mb, me, off, prot;
138 InternalScopedBuffer<char> module(4096);
139 InternalScopedBuffer<char> path(4096 * 2);
140 for (int i = 0;
141 proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot);
142 i++) {
143 if ((prot & MemoryMappingLayout::kProtectionExecute) == 0)
144 continue;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000145 while (vb < ve && *vb < mb) vb++;
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000146 if (vb >= ve) break;
Sergey Matveev76e02e92014-05-08 16:09:54 +0000147 if (*vb < me) {
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000148 offsets.clear();
149 const uptr *old_vb = vb;
150 CHECK_LE(off, *vb);
151 for (; vb < ve && *vb < me; vb++) {
152 uptr diff = *vb - (i ? mb : 0) + off;
153 CHECK_LE(diff, 0xffffffffU);
154 offsets.push_back(static_cast<u32>(diff));
155 }
156 char *module_name = StripModuleName(module.data());
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000157 if (cov_sandboxed) {
158 CovWritePacked(internal_getpid(), module_name, offsets.data(),
159 offsets.size() * sizeof(u32));
160 VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb);
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000161 } else {
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000162 // One file per module per process.
163 internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov",
164 module_name, internal_getpid());
165 uptr fd = OpenFile(path.data(), true);
166 if (internal_iserror(fd)) {
167 Report(" CovDump: failed to open %s for writing\n", path.data());
168 } else {
169 internal_write(fd, offsets.data(), offsets.size() * sizeof(u32));
170 internal_close(fd);
171 VReport(1, " CovDump: %s: %zd PCs written\n", path.data(),
172 vb - old_vb);
173 }
Evgeniy Stepanov8ab205f2014-02-12 15:29:22 +0000174 }
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000175 InternalFree(module_name);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000176 }
177 }
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000178 if (cov_fd >= 0)
179 internal_close(cov_fd);
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000180#endif // !SANITIZER_WINDOWS
181}
182
Sergey Matveev6cb47a082014-05-19 12:53:03 +0000183static void OpenPackedFileForWriting() {
184 CHECK(cov_fd == kInvalidFd);
185 InternalScopedBuffer<char> path(1024);
186 internal_snprintf((char *)path.data(), path.size(), "%zd.sancov.packed",
187 internal_getpid());
188 uptr fd = OpenFile(path.data(), true);
189 if (internal_iserror(fd)) {
190 Report(" Coverage: failed to open %s for writing\n", path.data());
191 Die();
192 }
193 cov_fd = fd;
194}
195
196void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {
197 if (!args) return;
198 if (!common_flags()->coverage) return;
199 cov_sandboxed = args->coverage_sandboxed;
200 if (!cov_sandboxed) return;
201 cov_fd = args->coverage_fd;
202 cov_max_block_size = args->coverage_max_block_size;
203 if (cov_fd < 0)
204 // Pre-open the file now. The sandbox won't allow us to do it later.
205 OpenPackedFileForWriting();
206}
207
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000208} // namespace __sanitizer
209
210extern "C" {
Kostya Serebryany714c67c2014-01-17 11:00:30 +0000211SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() {
212 CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()));
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000213}
214SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); }
Kostya Serebryany8b530e12014-04-30 10:40:48 +0000215SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { CovInit(); }
Bob Wilsona08e9ac2013-11-15 07:18:15 +0000216} // extern "C"