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 | |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 43 | atomic_uint32_t dump_once_guard; // Ensure that CovDump runs only once. |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 44 | |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 45 | // pc_array is the array containing the covered PCs. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 46 | // To make the pc_array thread- and async-signal-safe it has to be large enough. |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 47 | // 128M counters "ought to be enough for anybody" (4M on 32-bit). |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 48 | |
| 49 | // With coverage_direct=1 in ASAN_OPTIONS, pc_array memory is mapped to a file. |
| 50 | // In this mode, __sanitizer_cov_dump does nothing, and CovUpdateMapping() |
| 51 | // dump current memory layout to another file. |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 52 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 53 | static bool cov_sandboxed = false; |
| 54 | static int cov_fd = kInvalidFd; |
| 55 | static unsigned int cov_max_block_size = 0; |
| 56 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 57 | namespace __sanitizer { |
| 58 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 59 | class CoverageData { |
| 60 | public: |
| 61 | void Init(); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 62 | void BeforeFork(); |
| 63 | void AfterFork(int child_pid); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 64 | void Extend(uptr npcs); |
| 65 | void Add(uptr pc); |
| 66 | |
| 67 | uptr *data(); |
| 68 | uptr size(); |
| 69 | |
| 70 | private: |
| 71 | // Maximal size pc array may ever grow. |
| 72 | // We MmapNoReserve this space to ensure that the array is contiguous. |
| 73 | static const uptr kPcArrayMaxSize = FIRST_32_SECOND_64(1 << 22, 1 << 27); |
| 74 | // The amount file mapping for the pc array is grown by. |
| 75 | static const uptr kPcArrayMmapSize = 64 * 1024; |
| 76 | |
| 77 | // pc_array is allocated with MmapNoReserveOrDie and so it uses only as |
| 78 | // much RAM as it really needs. |
| 79 | uptr *pc_array; |
| 80 | // Index of the first available pc_array slot. |
| 81 | atomic_uintptr_t pc_array_index; |
| 82 | // Array size. |
| 83 | atomic_uintptr_t pc_array_size; |
| 84 | // Current file mapped size of the pc array. |
| 85 | uptr pc_array_mapped_size; |
| 86 | // Descriptor of the file mapped pc array. |
| 87 | int pc_fd; |
| 88 | StaticSpinMutex mu; |
| 89 | |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 90 | void DirectOpen(); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 91 | void ReInit(); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static CoverageData coverage_data; |
| 95 | |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 96 | void CoverageData::DirectOpen() { |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 97 | InternalScopedString path(1024); |
| 98 | internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.raw", |
| 99 | common_flags()->coverage_dir, internal_getpid()); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 100 | pc_fd = OpenFile(path.data(), true); |
| 101 | if (internal_iserror(pc_fd)) { |
| 102 | Report(" Coverage: failed to open %s for writing\n", path.data()); |
| 103 | Die(); |
| 104 | } |
| 105 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 106 | pc_array_mapped_size = 0; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 107 | CovUpdateMapping(); |
| 108 | } |
| 109 | |
| 110 | void CoverageData::Init() { |
| 111 | pc_array = reinterpret_cast<uptr *>( |
| 112 | MmapNoReserveOrDie(sizeof(uptr) * kPcArrayMaxSize, "CovInit")); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 113 | pc_fd = kInvalidFd; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 114 | if (common_flags()->coverage_direct) { |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 115 | atomic_store(&pc_array_size, 0, memory_order_relaxed); |
| 116 | atomic_store(&pc_array_index, 0, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 117 | } else { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 118 | atomic_store(&pc_array_size, kPcArrayMaxSize, memory_order_relaxed); |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 119 | atomic_store(&pc_array_index, 0, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 123 | void CoverageData::ReInit() { |
| 124 | internal_munmap(pc_array, sizeof(uptr) * kPcArrayMaxSize); |
| 125 | if (pc_fd != kInvalidFd) internal_close(pc_fd); |
| 126 | if (common_flags()->coverage_direct) { |
| 127 | // In memory-mapped mode we must extend the new file to the known array |
| 128 | // size. |
| 129 | uptr size = atomic_load(&pc_array_size, memory_order_relaxed); |
| 130 | Init(); |
| 131 | if (size) Extend(size); |
| 132 | } else { |
| 133 | Init(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void CoverageData::BeforeFork() { |
| 138 | mu.Lock(); |
| 139 | } |
| 140 | |
| 141 | void CoverageData::AfterFork(int child_pid) { |
| 142 | // We are single-threaded so it's OK to release the lock early. |
| 143 | mu.Unlock(); |
| 144 | if (child_pid == 0) ReInit(); |
| 145 | } |
| 146 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 147 | // Extend coverage PC array to fit additional npcs elements. |
| 148 | void CoverageData::Extend(uptr npcs) { |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 149 | if (!common_flags()->coverage_direct) return; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 150 | SpinMutexLock l(&mu); |
| 151 | |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 152 | if (pc_fd == kInvalidFd) DirectOpen(); |
| 153 | CHECK_NE(pc_fd, kInvalidFd); |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 154 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 155 | uptr size = atomic_load(&pc_array_size, memory_order_relaxed); |
| 156 | size += npcs * sizeof(uptr); |
| 157 | |
| 158 | if (size > pc_array_mapped_size) { |
| 159 | uptr new_mapped_size = pc_array_mapped_size; |
| 160 | while (size > new_mapped_size) new_mapped_size += kPcArrayMmapSize; |
| 161 | |
| 162 | // Extend the file and map the new space at the end of pc_array. |
| 163 | uptr res = internal_ftruncate(pc_fd, new_mapped_size); |
| 164 | int err; |
| 165 | if (internal_iserror(res, &err)) { |
| 166 | Printf("failed to extend raw coverage file: %d\n", err); |
| 167 | Die(); |
| 168 | } |
| 169 | void *p = MapWritableFileToMemory(pc_array + pc_array_mapped_size, |
| 170 | new_mapped_size - pc_array_mapped_size, |
| 171 | pc_fd, pc_array_mapped_size); |
| 172 | CHECK_EQ(p, pc_array + pc_array_mapped_size); |
| 173 | pc_array_mapped_size = new_mapped_size; |
| 174 | } |
| 175 | |
| 176 | atomic_store(&pc_array_size, size, memory_order_release); |
| 177 | } |
| 178 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 179 | // Simply add the pc into the vector under lock. If the function is called more |
| 180 | // than once for a given PC it will be inserted multiple times, which is fine. |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 181 | void CoverageData::Add(uptr pc) { |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 182 | if (!pc_array) return; |
| 183 | uptr idx = atomic_fetch_add(&pc_array_index, 1, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 184 | CHECK_LT(idx * sizeof(uptr), |
| 185 | atomic_load(&pc_array_size, memory_order_acquire)); |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 186 | pc_array[idx] = pc; |
| 187 | } |
| 188 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 189 | uptr *CoverageData::data() { |
| 190 | return pc_array; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 193 | uptr CoverageData::size() { |
| 194 | return atomic_load(&pc_array_index, memory_order_relaxed); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 197 | // Block layout for packed file format: header, followed by module name (no |
| 198 | // trailing zero), followed by data blob. |
| 199 | struct CovHeader { |
| 200 | int pid; |
| 201 | unsigned int module_name_length; |
| 202 | unsigned int data_length; |
| 203 | }; |
| 204 | |
| 205 | static void CovWritePacked(int pid, const char *module, const void *blob, |
| 206 | unsigned int blob_size) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 207 | if (cov_fd < 0) return; |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 208 | unsigned module_name_length = internal_strlen(module); |
| 209 | CovHeader header = {pid, module_name_length, blob_size}; |
| 210 | |
| 211 | if (cov_max_block_size == 0) { |
| 212 | // Writing to a file. Just go ahead. |
| 213 | internal_write(cov_fd, &header, sizeof(header)); |
| 214 | internal_write(cov_fd, module, module_name_length); |
| 215 | internal_write(cov_fd, blob, blob_size); |
| 216 | } else { |
| 217 | // Writing to a socket. We want to split the data into appropriately sized |
| 218 | // blocks. |
| 219 | InternalScopedBuffer<char> block(cov_max_block_size); |
| 220 | CHECK_EQ((uptr)block.data(), (uptr)(CovHeader *)block.data()); |
| 221 | uptr header_size_with_module = sizeof(header) + module_name_length; |
| 222 | CHECK_LT(header_size_with_module, cov_max_block_size); |
| 223 | unsigned int max_payload_size = |
| 224 | cov_max_block_size - header_size_with_module; |
| 225 | char *block_pos = block.data(); |
| 226 | internal_memcpy(block_pos, &header, sizeof(header)); |
| 227 | block_pos += sizeof(header); |
| 228 | internal_memcpy(block_pos, module, module_name_length); |
| 229 | block_pos += module_name_length; |
| 230 | char *block_data_begin = block_pos; |
| 231 | char *blob_pos = (char *)blob; |
| 232 | while (blob_size > 0) { |
| 233 | unsigned int payload_size = Min(blob_size, max_payload_size); |
| 234 | blob_size -= payload_size; |
| 235 | internal_memcpy(block_data_begin, blob_pos, payload_size); |
| 236 | blob_pos += payload_size; |
| 237 | ((CovHeader *)block.data())->data_length = payload_size; |
| 238 | internal_write(cov_fd, block.data(), |
| 239 | header_size_with_module + payload_size); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 244 | // If packed = false: <name>.<pid>.<sancov> (name = module name). |
| 245 | // If packed = true and name == 0: <pid>.<sancov>.<packed>. |
| 246 | // If packed = true and name != 0: <name>.<sancov>.<packed> (name is |
| 247 | // user-supplied). |
| 248 | static int CovOpenFile(bool packed, const char* name) { |
| 249 | InternalScopedBuffer<char> path(1024); |
| 250 | if (!packed) { |
| 251 | CHECK(name); |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 252 | Printf("%s\n", common_flags()->coverage_dir); |
| 253 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov", |
| 254 | common_flags()->coverage_dir, name, internal_getpid()); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 255 | } else { |
| 256 | if (!name) |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 257 | internal_snprintf((char *)path.data(), path.size(), |
| 258 | "%s/%zd.sancov.packed", common_flags()->coverage_dir, |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 259 | internal_getpid()); |
| 260 | else |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 261 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.sancov.packed", |
| 262 | common_flags()->coverage_dir, name); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 263 | } |
| 264 | uptr fd = OpenFile(path.data(), true); |
| 265 | if (internal_iserror(fd)) { |
| 266 | Report(" SanitizerCoverage: failed to open %s for writing\n", path.data()); |
| 267 | return -1; |
| 268 | } |
| 269 | return fd; |
| 270 | } |
| 271 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 272 | // Dump the coverage on disk. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 273 | static void CovDump() { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 274 | if (!common_flags()->coverage || common_flags()->coverage_direct) return; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 275 | #if !SANITIZER_WINDOWS |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 276 | if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed)) |
| 277 | return; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 278 | uptr size = coverage_data.size(); |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 279 | InternalMmapVector<u32> offsets(size); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 280 | uptr *vb = coverage_data.data(); |
| 281 | uptr *ve = vb + size; |
| 282 | SortArray(vb, size); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 283 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 284 | uptr mb, me, off, prot; |
| 285 | InternalScopedBuffer<char> module(4096); |
| 286 | InternalScopedBuffer<char> path(4096 * 2); |
| 287 | for (int i = 0; |
| 288 | proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 289 | i++) { |
| 290 | if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 291 | continue; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 292 | while (vb < ve && *vb < mb) vb++; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 293 | if (vb >= ve) break; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 294 | if (*vb < me) { |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 295 | offsets.clear(); |
| 296 | const uptr *old_vb = vb; |
| 297 | CHECK_LE(off, *vb); |
| 298 | for (; vb < ve && *vb < me; vb++) { |
| 299 | uptr diff = *vb - (i ? mb : 0) + off; |
| 300 | CHECK_LE(diff, 0xffffffffU); |
| 301 | offsets.push_back(static_cast<u32>(diff)); |
| 302 | } |
| 303 | char *module_name = StripModuleName(module.data()); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 304 | if (cov_sandboxed) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 305 | if (cov_fd >= 0) { |
| 306 | CovWritePacked(internal_getpid(), module_name, offsets.data(), |
| 307 | offsets.size() * sizeof(u32)); |
| 308 | VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb); |
| 309 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 310 | } else { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 311 | // One file per module per process. |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 312 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov", |
| 313 | common_flags()->coverage_dir, module_name, |
| 314 | internal_getpid()); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 315 | int fd = CovOpenFile(false /* packed */, module_name); |
| 316 | if (fd > 0) { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 317 | internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 318 | internal_close(fd); |
| 319 | VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), |
| 320 | vb - old_vb); |
| 321 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 322 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 323 | InternalFree(module_name); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 326 | if (cov_fd >= 0) |
| 327 | internal_close(cov_fd); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 328 | #endif // !SANITIZER_WINDOWS |
| 329 | } |
| 330 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 331 | void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
| 332 | if (!args) return; |
| 333 | if (!common_flags()->coverage) return; |
| 334 | cov_sandboxed = args->coverage_sandboxed; |
| 335 | if (!cov_sandboxed) return; |
| 336 | cov_fd = args->coverage_fd; |
| 337 | cov_max_block_size = args->coverage_max_block_size; |
| 338 | if (cov_fd < 0) |
| 339 | // Pre-open the file now. The sandbox won't allow us to do it later. |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 340 | cov_fd = CovOpenFile(true /* packed */, 0); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 341 | } |
| 342 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 343 | int MaybeOpenCovFile(const char *name) { |
| 344 | CHECK(name); |
| 345 | if (!common_flags()->coverage) return -1; |
| 346 | return CovOpenFile(true /* packed */, name); |
| 347 | } |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 348 | |
| 349 | void CovBeforeFork() { |
| 350 | coverage_data.BeforeFork(); |
| 351 | } |
| 352 | |
| 353 | void CovAfterFork(int child_pid) { |
| 354 | coverage_data.AfterFork(child_pid); |
| 355 | } |
| 356 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 357 | } // namespace __sanitizer |
| 358 | |
| 359 | extern "C" { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 360 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 361 | coverage_data.Add(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 362 | } |
| 363 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 364 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { |
| 365 | coverage_data.Init(); |
| 366 | } |
| 367 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_module_init(uptr npcs) { |
Evgeniy Stepanov | dfa5439 | 2014-06-11 15:11:26 +0000 | [diff] [blame^] | 368 | if (!common_flags()->coverage || !common_flags()->coverage_direct) return; |
| 369 | if (SANITIZER_ANDROID) { |
Evgeniy Stepanov | 38c228a | 2014-06-05 14:38:53 +0000 | [diff] [blame] | 370 | // dlopen/dlclose interceptors do not work on Android, so we rely on |
| 371 | // Extend() calls to update .sancov.map. |
| 372 | CovUpdateMapping(GET_CALLER_PC()); |
| 373 | } |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 374 | coverage_data.Extend(npcs); |
| 375 | } |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 376 | SANITIZER_INTERFACE_ATTRIBUTE |
| 377 | sptr __sanitizer_maybe_open_cov_file(const char *name) { |
| 378 | return MaybeOpenCovFile(name); |
| 379 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 380 | } // extern "C" |