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" |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 41 | #include "sanitizer_symbolizer.h" |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 42 | #include "sanitizer_flags.h" |
| 43 | |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 44 | atomic_uint32_t dump_once_guard; // Ensure that CovDump runs only once. |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 45 | |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 46 | // pc_array is the array containing the covered PCs. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 47 | // 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] | 48 | // 128M counters "ought to be enough for anybody" (4M on 32-bit). |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 49 | |
| 50 | // With coverage_direct=1 in ASAN_OPTIONS, pc_array memory is mapped to a file. |
| 51 | // In this mode, __sanitizer_cov_dump does nothing, and CovUpdateMapping() |
| 52 | // dump current memory layout to another file. |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 53 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 54 | static bool cov_sandboxed = false; |
| 55 | static int cov_fd = kInvalidFd; |
| 56 | static unsigned int cov_max_block_size = 0; |
| 57 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 58 | namespace __sanitizer { |
| 59 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 60 | class CoverageData { |
| 61 | public: |
| 62 | void Init(); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 63 | void BeforeFork(); |
| 64 | void AfterFork(int child_pid); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 65 | void Extend(uptr npcs); |
| 66 | void Add(uptr pc); |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 67 | void IndirCall(uptr caller, uptr callee, uptr callee_cache[], |
| 68 | uptr cache_size); |
| 69 | void DumpCallerCalleePairs(); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 70 | |
| 71 | uptr *data(); |
| 72 | uptr size(); |
| 73 | |
| 74 | private: |
| 75 | // Maximal size pc array may ever grow. |
| 76 | // We MmapNoReserve this space to ensure that the array is contiguous. |
| 77 | static const uptr kPcArrayMaxSize = FIRST_32_SECOND_64(1 << 22, 1 << 27); |
| 78 | // The amount file mapping for the pc array is grown by. |
| 79 | static const uptr kPcArrayMmapSize = 64 * 1024; |
| 80 | |
| 81 | // pc_array is allocated with MmapNoReserveOrDie and so it uses only as |
| 82 | // much RAM as it really needs. |
| 83 | uptr *pc_array; |
| 84 | // Index of the first available pc_array slot. |
| 85 | atomic_uintptr_t pc_array_index; |
| 86 | // Array size. |
| 87 | atomic_uintptr_t pc_array_size; |
| 88 | // Current file mapped size of the pc array. |
| 89 | uptr pc_array_mapped_size; |
| 90 | // Descriptor of the file mapped pc array. |
| 91 | int pc_fd; |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 92 | |
| 93 | // Caller-Callee (cc) array, size and current index. |
| 94 | static const uptr kCcArrayMaxSize = FIRST_32_SECOND_64(1 << 18, 1 << 24); |
| 95 | uptr **cc_array; |
| 96 | atomic_uintptr_t cc_array_index; |
| 97 | atomic_uintptr_t cc_array_size; |
| 98 | |
| 99 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 100 | StaticSpinMutex mu; |
| 101 | |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 102 | void DirectOpen(); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 103 | void ReInit(); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | static CoverageData coverage_data; |
| 107 | |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 108 | void CoverageData::DirectOpen() { |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 109 | InternalScopedString path(1024); |
| 110 | internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.raw", |
| 111 | common_flags()->coverage_dir, internal_getpid()); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 112 | pc_fd = OpenFile(path.data(), true); |
| 113 | if (internal_iserror(pc_fd)) { |
| 114 | Report(" Coverage: failed to open %s for writing\n", path.data()); |
| 115 | Die(); |
| 116 | } |
| 117 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 118 | pc_array_mapped_size = 0; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 119 | CovUpdateMapping(); |
| 120 | } |
| 121 | |
| 122 | void CoverageData::Init() { |
| 123 | pc_array = reinterpret_cast<uptr *>( |
| 124 | MmapNoReserveOrDie(sizeof(uptr) * kPcArrayMaxSize, "CovInit")); |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 125 | pc_fd = kInvalidFd; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 126 | if (common_flags()->coverage_direct) { |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 127 | atomic_store(&pc_array_size, 0, memory_order_relaxed); |
| 128 | atomic_store(&pc_array_index, 0, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 129 | } else { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 130 | atomic_store(&pc_array_size, kPcArrayMaxSize, memory_order_relaxed); |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 131 | atomic_store(&pc_array_index, 0, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 132 | } |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 133 | |
| 134 | cc_array = reinterpret_cast<uptr **>(MmapNoReserveOrDie( |
| 135 | sizeof(uptr *) * kCcArrayMaxSize, "CovInit::cc_array")); |
| 136 | atomic_store(&cc_array_size, kCcArrayMaxSize, memory_order_relaxed); |
| 137 | atomic_store(&cc_array_index, 0, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 140 | void CoverageData::ReInit() { |
| 141 | internal_munmap(pc_array, sizeof(uptr) * kPcArrayMaxSize); |
| 142 | if (pc_fd != kInvalidFd) internal_close(pc_fd); |
| 143 | if (common_flags()->coverage_direct) { |
| 144 | // In memory-mapped mode we must extend the new file to the known array |
| 145 | // size. |
| 146 | uptr size = atomic_load(&pc_array_size, memory_order_relaxed); |
| 147 | Init(); |
| 148 | if (size) Extend(size); |
| 149 | } else { |
| 150 | Init(); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | void CoverageData::BeforeFork() { |
| 155 | mu.Lock(); |
| 156 | } |
| 157 | |
| 158 | void CoverageData::AfterFork(int child_pid) { |
| 159 | // We are single-threaded so it's OK to release the lock early. |
| 160 | mu.Unlock(); |
| 161 | if (child_pid == 0) ReInit(); |
| 162 | } |
| 163 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 164 | // Extend coverage PC array to fit additional npcs elements. |
| 165 | void CoverageData::Extend(uptr npcs) { |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 166 | if (!common_flags()->coverage_direct) return; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 167 | SpinMutexLock l(&mu); |
| 168 | |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 169 | if (pc_fd == kInvalidFd) DirectOpen(); |
| 170 | CHECK_NE(pc_fd, kInvalidFd); |
Evgeniy Stepanov | ce98452 | 2014-06-03 15:27:15 +0000 | [diff] [blame] | 171 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 172 | uptr size = atomic_load(&pc_array_size, memory_order_relaxed); |
| 173 | size += npcs * sizeof(uptr); |
| 174 | |
| 175 | if (size > pc_array_mapped_size) { |
| 176 | uptr new_mapped_size = pc_array_mapped_size; |
| 177 | while (size > new_mapped_size) new_mapped_size += kPcArrayMmapSize; |
| 178 | |
| 179 | // Extend the file and map the new space at the end of pc_array. |
| 180 | uptr res = internal_ftruncate(pc_fd, new_mapped_size); |
| 181 | int err; |
| 182 | if (internal_iserror(res, &err)) { |
| 183 | Printf("failed to extend raw coverage file: %d\n", err); |
| 184 | Die(); |
| 185 | } |
| 186 | void *p = MapWritableFileToMemory(pc_array + pc_array_mapped_size, |
| 187 | new_mapped_size - pc_array_mapped_size, |
| 188 | pc_fd, pc_array_mapped_size); |
| 189 | CHECK_EQ(p, pc_array + pc_array_mapped_size); |
| 190 | pc_array_mapped_size = new_mapped_size; |
| 191 | } |
| 192 | |
| 193 | atomic_store(&pc_array_size, size, memory_order_release); |
| 194 | } |
| 195 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 196 | // Simply add the pc into the vector under lock. If the function is called more |
| 197 | // 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] | 198 | void CoverageData::Add(uptr pc) { |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 199 | if (!pc_array) return; |
| 200 | uptr idx = atomic_fetch_add(&pc_array_index, 1, memory_order_relaxed); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 201 | CHECK_LT(idx * sizeof(uptr), |
| 202 | atomic_load(&pc_array_size, memory_order_acquire)); |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 203 | pc_array[idx] = pc; |
| 204 | } |
| 205 | |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 206 | // Registers a pair caller=>callee. |
| 207 | // When a given caller is seen for the first time, the callee_cache is added |
| 208 | // to the global array cc_array, callee_cache[0] is set to caller and |
| 209 | // callee_cache[1] is set to cache_size. |
| 210 | // Then we are trying to add callee to callee_cache [2,cache_size) if it is |
| 211 | // not there yet. |
| 212 | // If the cache is full we drop the callee (may want to fix this later). |
| 213 | void CoverageData::IndirCall(uptr caller, uptr callee, uptr callee_cache[], |
| 214 | uptr cache_size) { |
| 215 | if (!cc_array) return; |
| 216 | atomic_uintptr_t *atomic_callee_cache = |
| 217 | reinterpret_cast<atomic_uintptr_t *>(callee_cache); |
| 218 | uptr zero = 0; |
| 219 | if (atomic_compare_exchange_strong(&atomic_callee_cache[0], &zero, caller, |
| 220 | memory_order_seq_cst)) { |
| 221 | uptr idx = atomic_fetch_add(&cc_array_index, 1, memory_order_relaxed); |
| 222 | CHECK_LT(idx * sizeof(uptr), |
| 223 | atomic_load(&cc_array_size, memory_order_acquire)); |
| 224 | callee_cache[1] = cache_size; |
| 225 | cc_array[idx] = callee_cache; |
| 226 | } |
| 227 | CHECK_EQ(atomic_load(&atomic_callee_cache[0], memory_order_relaxed), caller); |
| 228 | for (uptr i = 2; i < cache_size; i++) { |
| 229 | uptr was = 0; |
| 230 | if (atomic_compare_exchange_strong(&atomic_callee_cache[i], &was, callee, |
| 231 | memory_order_seq_cst)) |
| 232 | return; |
| 233 | if (was == callee) // Already have this callee. |
| 234 | return; |
| 235 | } |
| 236 | } |
| 237 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 238 | uptr *CoverageData::data() { |
| 239 | return pc_array; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 240 | } |
| 241 | |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 242 | uptr CoverageData::size() { |
| 243 | return atomic_load(&pc_array_index, memory_order_relaxed); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 246 | // Block layout for packed file format: header, followed by module name (no |
| 247 | // trailing zero), followed by data blob. |
| 248 | struct CovHeader { |
| 249 | int pid; |
| 250 | unsigned int module_name_length; |
| 251 | unsigned int data_length; |
| 252 | }; |
| 253 | |
| 254 | static void CovWritePacked(int pid, const char *module, const void *blob, |
| 255 | unsigned int blob_size) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 256 | if (cov_fd < 0) return; |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 257 | unsigned module_name_length = internal_strlen(module); |
| 258 | CovHeader header = {pid, module_name_length, blob_size}; |
| 259 | |
| 260 | if (cov_max_block_size == 0) { |
| 261 | // Writing to a file. Just go ahead. |
| 262 | internal_write(cov_fd, &header, sizeof(header)); |
| 263 | internal_write(cov_fd, module, module_name_length); |
| 264 | internal_write(cov_fd, blob, blob_size); |
| 265 | } else { |
| 266 | // Writing to a socket. We want to split the data into appropriately sized |
| 267 | // blocks. |
| 268 | InternalScopedBuffer<char> block(cov_max_block_size); |
| 269 | CHECK_EQ((uptr)block.data(), (uptr)(CovHeader *)block.data()); |
| 270 | uptr header_size_with_module = sizeof(header) + module_name_length; |
| 271 | CHECK_LT(header_size_with_module, cov_max_block_size); |
| 272 | unsigned int max_payload_size = |
| 273 | cov_max_block_size - header_size_with_module; |
| 274 | char *block_pos = block.data(); |
| 275 | internal_memcpy(block_pos, &header, sizeof(header)); |
| 276 | block_pos += sizeof(header); |
| 277 | internal_memcpy(block_pos, module, module_name_length); |
| 278 | block_pos += module_name_length; |
| 279 | char *block_data_begin = block_pos; |
| 280 | char *blob_pos = (char *)blob; |
| 281 | while (blob_size > 0) { |
| 282 | unsigned int payload_size = Min(blob_size, max_payload_size); |
| 283 | blob_size -= payload_size; |
| 284 | internal_memcpy(block_data_begin, blob_pos, payload_size); |
| 285 | blob_pos += payload_size; |
| 286 | ((CovHeader *)block.data())->data_length = payload_size; |
| 287 | internal_write(cov_fd, block.data(), |
| 288 | header_size_with_module + payload_size); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 293 | // If packed = false: <name>.<pid>.<sancov> (name = module name). |
| 294 | // If packed = true and name == 0: <pid>.<sancov>.<packed>. |
| 295 | // If packed = true and name != 0: <name>.<sancov>.<packed> (name is |
| 296 | // user-supplied). |
| 297 | static int CovOpenFile(bool packed, const char* name) { |
| 298 | InternalScopedBuffer<char> path(1024); |
| 299 | if (!packed) { |
| 300 | CHECK(name); |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 301 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov", |
| 302 | common_flags()->coverage_dir, name, internal_getpid()); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 303 | } else { |
| 304 | if (!name) |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 305 | internal_snprintf((char *)path.data(), path.size(), |
| 306 | "%s/%zd.sancov.packed", common_flags()->coverage_dir, |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 307 | internal_getpid()); |
| 308 | else |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 309 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.sancov.packed", |
| 310 | common_flags()->coverage_dir, name); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 311 | } |
| 312 | uptr fd = OpenFile(path.data(), true); |
| 313 | if (internal_iserror(fd)) { |
| 314 | Report(" SanitizerCoverage: failed to open %s for writing\n", path.data()); |
| 315 | return -1; |
| 316 | } |
| 317 | return fd; |
| 318 | } |
| 319 | |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 320 | // This function dumps the caller=>callee pairs into a file as a sequence of |
| 321 | // lines like "module_name offset". |
| 322 | void CoverageData::DumpCallerCalleePairs() { |
| 323 | uptr max_idx = atomic_load(&cc_array_index, memory_order_relaxed); |
| 324 | if (!max_idx) return; |
| 325 | auto sym = Symbolizer::GetOrInit(); |
| 326 | if (!sym) |
| 327 | return; |
Kostya Serebryany | 40aa4a2 | 2014-10-31 19:49:46 +0000 | [diff] [blame] | 328 | InternalScopedString out(32 << 20); |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 329 | uptr total = 0; |
| 330 | for (uptr i = 0; i < max_idx; i++) { |
| 331 | uptr *cc_cache = cc_array[i]; |
| 332 | CHECK(cc_cache); |
| 333 | uptr caller = cc_cache[0]; |
| 334 | uptr n_callees = cc_cache[1]; |
| 335 | const char *caller_module_name = "<unknown>"; |
| 336 | uptr caller_module_address = 0; |
| 337 | sym->GetModuleNameAndOffsetForPC(caller, &caller_module_name, |
| 338 | &caller_module_address); |
| 339 | for (uptr j = 2; j < n_callees; j++) { |
| 340 | uptr callee = cc_cache[j]; |
| 341 | if (!callee) break; |
| 342 | total++; |
| 343 | const char *callee_module_name = "<unknown>"; |
| 344 | uptr callee_module_address = 0; |
| 345 | sym->GetModuleNameAndOffsetForPC(callee, &callee_module_name, |
| 346 | &callee_module_address); |
| 347 | out.append("%s 0x%zx\n%s 0x%zx\n", caller_module_name, |
| 348 | caller_module_address, callee_module_name, |
| 349 | callee_module_address); |
| 350 | } |
| 351 | } |
| 352 | int fd = CovOpenFile(false, "caller-callee"); |
| 353 | if (fd < 0) return; |
| 354 | internal_write(fd, out.data(), out.length()); |
| 355 | internal_close(fd); |
| 356 | VReport(1, " CovDump: %zd caller-callee pairs written\n", total); |
| 357 | } |
| 358 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 359 | // Dump the coverage on disk. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 360 | static void CovDump() { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 361 | if (!common_flags()->coverage || common_flags()->coverage_direct) return; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 362 | #if !SANITIZER_WINDOWS |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 363 | if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed)) |
| 364 | return; |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 365 | uptr size = coverage_data.size(); |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 366 | InternalMmapVector<u32> offsets(size); |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 367 | uptr *vb = coverage_data.data(); |
| 368 | uptr *ve = vb + size; |
| 369 | SortArray(vb, size); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 370 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 371 | uptr mb, me, off, prot; |
| 372 | InternalScopedBuffer<char> module(4096); |
| 373 | InternalScopedBuffer<char> path(4096 * 2); |
| 374 | for (int i = 0; |
| 375 | proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 376 | i++) { |
| 377 | if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 378 | continue; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 379 | while (vb < ve && *vb < mb) vb++; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 380 | if (vb >= ve) break; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 381 | if (*vb < me) { |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 382 | offsets.clear(); |
| 383 | const uptr *old_vb = vb; |
| 384 | CHECK_LE(off, *vb); |
| 385 | for (; vb < ve && *vb < me; vb++) { |
| 386 | uptr diff = *vb - (i ? mb : 0) + off; |
| 387 | CHECK_LE(diff, 0xffffffffU); |
| 388 | offsets.push_back(static_cast<u32>(diff)); |
| 389 | } |
| 390 | char *module_name = StripModuleName(module.data()); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 391 | if (cov_sandboxed) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 392 | if (cov_fd >= 0) { |
| 393 | CovWritePacked(internal_getpid(), module_name, offsets.data(), |
| 394 | offsets.size() * sizeof(u32)); |
| 395 | VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb); |
| 396 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 397 | } else { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 398 | // One file per module per process. |
Evgeniy Stepanov | fa5c075 | 2014-05-29 14:33:16 +0000 | [diff] [blame] | 399 | internal_snprintf((char *)path.data(), path.size(), "%s/%s.%zd.sancov", |
| 400 | common_flags()->coverage_dir, module_name, |
| 401 | internal_getpid()); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 402 | int fd = CovOpenFile(false /* packed */, module_name); |
| 403 | if (fd > 0) { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 404 | internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 405 | internal_close(fd); |
| 406 | VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), |
| 407 | vb - old_vb); |
| 408 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 409 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 410 | InternalFree(module_name); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 413 | if (cov_fd >= 0) |
| 414 | internal_close(cov_fd); |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 415 | coverage_data.DumpCallerCalleePairs(); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 416 | #endif // !SANITIZER_WINDOWS |
| 417 | } |
| 418 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 419 | void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
| 420 | if (!args) return; |
| 421 | if (!common_flags()->coverage) return; |
| 422 | cov_sandboxed = args->coverage_sandboxed; |
| 423 | if (!cov_sandboxed) return; |
| 424 | cov_fd = args->coverage_fd; |
| 425 | cov_max_block_size = args->coverage_max_block_size; |
| 426 | if (cov_fd < 0) |
| 427 | // 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] | 428 | cov_fd = CovOpenFile(true /* packed */, 0); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 431 | int MaybeOpenCovFile(const char *name) { |
| 432 | CHECK(name); |
| 433 | if (!common_flags()->coverage) return -1; |
| 434 | return CovOpenFile(true /* packed */, name); |
| 435 | } |
Evgeniy Stepanov | fe18102 | 2014-06-04 12:13:54 +0000 | [diff] [blame] | 436 | |
| 437 | void CovBeforeFork() { |
| 438 | coverage_data.BeforeFork(); |
| 439 | } |
| 440 | |
| 441 | void CovAfterFork(int child_pid) { |
| 442 | coverage_data.AfterFork(child_pid); |
| 443 | } |
| 444 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 445 | } // namespace __sanitizer |
| 446 | |
| 447 | extern "C" { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 448 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 449 | coverage_data.Add(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 450 | } |
Kostya Serebryany | b6eae0c | 2014-10-31 17:19:11 +0000 | [diff] [blame] | 451 | SANITIZER_INTERFACE_ATTRIBUTE void |
| 452 | __sanitizer_cov_indir_call16(uptr callee, uptr callee_cache16[]) { |
| 453 | coverage_data.IndirCall(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC()), |
| 454 | callee, callee_cache16, 16); |
| 455 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 456 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 457 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { |
| 458 | coverage_data.Init(); |
| 459 | } |
| 460 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_module_init(uptr npcs) { |
Evgeniy Stepanov | dfa5439 | 2014-06-11 15:11:26 +0000 | [diff] [blame] | 461 | if (!common_flags()->coverage || !common_flags()->coverage_direct) return; |
| 462 | if (SANITIZER_ANDROID) { |
Evgeniy Stepanov | 38c228a | 2014-06-05 14:38:53 +0000 | [diff] [blame] | 463 | // dlopen/dlclose interceptors do not work on Android, so we rely on |
| 464 | // Extend() calls to update .sancov.map. |
| 465 | CovUpdateMapping(GET_CALLER_PC()); |
| 466 | } |
Evgeniy Stepanov | 567e516 | 2014-05-27 12:37:52 +0000 | [diff] [blame] | 467 | coverage_data.Extend(npcs); |
| 468 | } |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame] | 469 | SANITIZER_INTERFACE_ATTRIBUTE |
| 470 | sptr __sanitizer_maybe_open_cov_file(const char *name) { |
| 471 | return MaybeOpenCovFile(name); |
| 472 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 473 | } // extern "C" |