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