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). |
| 48 | // pc_array is allocated with MmapNoReserveOrDie and so it uses only as |
| 49 | // much RAM as it really needs. |
| 50 | static const uptr kPcArraySize = FIRST_32_SECOND_64(1 << 22, 1 << 27); |
| 51 | static uptr *pc_array; |
| 52 | static atomic_uintptr_t pc_array_index; |
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 | |
| 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. |
| 62 | static void CovAdd(uptr pc) { |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 63 | 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 | |
| 69 | void CovInit() { |
| 70 | pc_array = reinterpret_cast<uptr *>( |
| 71 | MmapNoReserveOrDie(sizeof(uptr) * kPcArraySize, "CovInit")); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | static inline bool CompareLess(const uptr &a, const uptr &b) { |
| 75 | return a < b; |
| 76 | } |
| 77 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 78 | // Block layout for packed file format: header, followed by module name (no |
| 79 | // trailing zero), followed by data blob. |
| 80 | struct CovHeader { |
| 81 | int pid; |
| 82 | unsigned int module_name_length; |
| 83 | unsigned int data_length; |
| 84 | }; |
| 85 | |
| 86 | static void CovWritePacked(int pid, const char *module, const void *blob, |
| 87 | unsigned int blob_size) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 88 | if (cov_fd < 0) return; |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 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 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 125 | // If packed = false: <name>.<pid>.<sancov> (name = module name). |
| 126 | // If packed = true and name == 0: <pid>.<sancov>.<packed>. |
| 127 | // If packed = true and name != 0: <name>.<sancov>.<packed> (name is |
| 128 | // user-supplied). |
| 129 | static int CovOpenFile(bool packed, const char* name) { |
| 130 | InternalScopedBuffer<char> path(1024); |
| 131 | if (!packed) { |
| 132 | CHECK(name); |
| 133 | internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov", |
| 134 | name, internal_getpid()); |
| 135 | } else { |
| 136 | if (!name) |
| 137 | internal_snprintf((char *)path.data(), path.size(), "%zd.sancov.packed", |
| 138 | internal_getpid()); |
| 139 | else |
| 140 | internal_snprintf((char *)path.data(), path.size(), "%s.sancov.packed", |
| 141 | name); |
| 142 | } |
| 143 | uptr fd = OpenFile(path.data(), true); |
| 144 | if (internal_iserror(fd)) { |
| 145 | Report(" SanitizerCoverage: failed to open %s for writing\n", path.data()); |
| 146 | return -1; |
| 147 | } |
| 148 | return fd; |
| 149 | } |
| 150 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 151 | // Dump the coverage on disk. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 152 | static void CovDump() { |
| 153 | if (!common_flags()->coverage) return; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 154 | #if !SANITIZER_WINDOWS |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 155 | if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed)) |
| 156 | return; |
| 157 | uptr size = atomic_load(&pc_array_index, memory_order_relaxed); |
| 158 | InternalSort(&pc_array, size, CompareLess); |
| 159 | InternalMmapVector<u32> offsets(size); |
| 160 | const uptr *vb = pc_array; |
| 161 | const uptr *ve = vb + size; |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 162 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 163 | uptr mb, me, off, prot; |
| 164 | InternalScopedBuffer<char> module(4096); |
| 165 | InternalScopedBuffer<char> path(4096 * 2); |
| 166 | for (int i = 0; |
| 167 | proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 168 | i++) { |
| 169 | if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 170 | continue; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 171 | while (vb < ve && *vb < mb) vb++; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 172 | if (vb >= ve) break; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 173 | if (*vb < me) { |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 174 | offsets.clear(); |
| 175 | const uptr *old_vb = vb; |
| 176 | CHECK_LE(off, *vb); |
| 177 | for (; vb < ve && *vb < me; vb++) { |
| 178 | uptr diff = *vb - (i ? mb : 0) + off; |
| 179 | CHECK_LE(diff, 0xffffffffU); |
| 180 | offsets.push_back(static_cast<u32>(diff)); |
| 181 | } |
| 182 | char *module_name = StripModuleName(module.data()); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 183 | if (cov_sandboxed) { |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 184 | if (cov_fd >= 0) { |
| 185 | CovWritePacked(internal_getpid(), module_name, offsets.data(), |
| 186 | offsets.size() * sizeof(u32)); |
| 187 | VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb); |
| 188 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 189 | } else { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 190 | // One file per module per process. |
| 191 | internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov", |
| 192 | module_name, internal_getpid()); |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 193 | int fd = CovOpenFile(false /* packed */, module_name); |
| 194 | if (fd > 0) { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 195 | internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 196 | internal_close(fd); |
| 197 | VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), |
| 198 | vb - old_vb); |
| 199 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 200 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 201 | InternalFree(module_name); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 202 | } |
| 203 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 204 | if (cov_fd >= 0) |
| 205 | internal_close(cov_fd); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 206 | #endif // !SANITIZER_WINDOWS |
| 207 | } |
| 208 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 209 | void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
| 210 | if (!args) return; |
| 211 | if (!common_flags()->coverage) return; |
| 212 | cov_sandboxed = args->coverage_sandboxed; |
| 213 | if (!cov_sandboxed) return; |
| 214 | cov_fd = args->coverage_fd; |
| 215 | cov_max_block_size = args->coverage_max_block_size; |
| 216 | if (cov_fd < 0) |
| 217 | // 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^] | 218 | cov_fd = CovOpenFile(true /* packed */, 0); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 221 | int MaybeOpenCovFile(const char *name) { |
| 222 | CHECK(name); |
| 223 | if (!common_flags()->coverage) return -1; |
| 224 | return CovOpenFile(true /* packed */, name); |
| 225 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 226 | } // namespace __sanitizer |
| 227 | |
| 228 | extern "C" { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 229 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { |
| 230 | CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 231 | } |
| 232 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 233 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { CovInit(); } |
Sergey Matveev | 83f91e7 | 2014-05-21 13:43:52 +0000 | [diff] [blame^] | 234 | SANITIZER_INTERFACE_ATTRIBUTE |
| 235 | sptr __sanitizer_maybe_open_cov_file(const char *name) { |
| 236 | return MaybeOpenCovFile(name); |
| 237 | } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 238 | } // extern "C" |