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) { |
| 88 | CHECK_GE(cov_fd, 0); |
| 89 | unsigned module_name_length = internal_strlen(module); |
| 90 | CovHeader header = {pid, module_name_length, blob_size}; |
| 91 | |
| 92 | if (cov_max_block_size == 0) { |
| 93 | // Writing to a file. Just go ahead. |
| 94 | internal_write(cov_fd, &header, sizeof(header)); |
| 95 | internal_write(cov_fd, module, module_name_length); |
| 96 | internal_write(cov_fd, blob, blob_size); |
| 97 | } else { |
| 98 | // Writing to a socket. We want to split the data into appropriately sized |
| 99 | // blocks. |
| 100 | InternalScopedBuffer<char> block(cov_max_block_size); |
| 101 | CHECK_EQ((uptr)block.data(), (uptr)(CovHeader *)block.data()); |
| 102 | uptr header_size_with_module = sizeof(header) + module_name_length; |
| 103 | CHECK_LT(header_size_with_module, cov_max_block_size); |
| 104 | unsigned int max_payload_size = |
| 105 | cov_max_block_size - header_size_with_module; |
| 106 | char *block_pos = block.data(); |
| 107 | internal_memcpy(block_pos, &header, sizeof(header)); |
| 108 | block_pos += sizeof(header); |
| 109 | internal_memcpy(block_pos, module, module_name_length); |
| 110 | block_pos += module_name_length; |
| 111 | char *block_data_begin = block_pos; |
| 112 | char *blob_pos = (char *)blob; |
| 113 | while (blob_size > 0) { |
| 114 | unsigned int payload_size = Min(blob_size, max_payload_size); |
| 115 | blob_size -= payload_size; |
| 116 | internal_memcpy(block_data_begin, blob_pos, payload_size); |
| 117 | blob_pos += payload_size; |
| 118 | ((CovHeader *)block.data())->data_length = payload_size; |
| 119 | internal_write(cov_fd, block.data(), |
| 120 | header_size_with_module + payload_size); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 125 | // Dump the coverage on disk. |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 126 | static void CovDump() { |
| 127 | if (!common_flags()->coverage) return; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 128 | #if !SANITIZER_WINDOWS |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 129 | if (atomic_fetch_add(&dump_once_guard, 1, memory_order_relaxed)) |
| 130 | return; |
| 131 | uptr size = atomic_load(&pc_array_index, memory_order_relaxed); |
| 132 | InternalSort(&pc_array, size, CompareLess); |
| 133 | InternalMmapVector<u32> offsets(size); |
| 134 | const uptr *vb = pc_array; |
| 135 | const uptr *ve = vb + size; |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 136 | MemoryMappingLayout proc_maps(/*cache_enabled*/true); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 137 | uptr mb, me, off, prot; |
| 138 | InternalScopedBuffer<char> module(4096); |
| 139 | InternalScopedBuffer<char> path(4096 * 2); |
| 140 | for (int i = 0; |
| 141 | proc_maps.Next(&mb, &me, &off, module.data(), module.size(), &prot); |
| 142 | i++) { |
| 143 | if ((prot & MemoryMappingLayout::kProtectionExecute) == 0) |
| 144 | continue; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 145 | while (vb < ve && *vb < mb) vb++; |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 146 | if (vb >= ve) break; |
Sergey Matveev | 76e02e9 | 2014-05-08 16:09:54 +0000 | [diff] [blame] | 147 | if (*vb < me) { |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 148 | offsets.clear(); |
| 149 | const uptr *old_vb = vb; |
| 150 | CHECK_LE(off, *vb); |
| 151 | for (; vb < ve && *vb < me; vb++) { |
| 152 | uptr diff = *vb - (i ? mb : 0) + off; |
| 153 | CHECK_LE(diff, 0xffffffffU); |
| 154 | offsets.push_back(static_cast<u32>(diff)); |
| 155 | } |
| 156 | char *module_name = StripModuleName(module.data()); |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 157 | if (cov_sandboxed) { |
| 158 | CovWritePacked(internal_getpid(), module_name, offsets.data(), |
| 159 | offsets.size() * sizeof(u32)); |
| 160 | VReport(1, " CovDump: %zd PCs written to packed file\n", vb - old_vb); |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 161 | } else { |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 162 | // One file per module per process. |
| 163 | internal_snprintf((char *)path.data(), path.size(), "%s.%zd.sancov", |
| 164 | module_name, internal_getpid()); |
| 165 | uptr fd = OpenFile(path.data(), true); |
| 166 | if (internal_iserror(fd)) { |
| 167 | Report(" CovDump: failed to open %s for writing\n", path.data()); |
| 168 | } else { |
| 169 | internal_write(fd, offsets.data(), offsets.size() * sizeof(u32)); |
| 170 | internal_close(fd); |
| 171 | VReport(1, " CovDump: %s: %zd PCs written\n", path.data(), |
| 172 | vb - old_vb); |
| 173 | } |
Evgeniy Stepanov | 8ab205f | 2014-02-12 15:29:22 +0000 | [diff] [blame] | 174 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 175 | InternalFree(module_name); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 176 | } |
| 177 | } |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 178 | if (cov_fd >= 0) |
| 179 | internal_close(cov_fd); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 180 | #endif // !SANITIZER_WINDOWS |
| 181 | } |
| 182 | |
Sergey Matveev | 6cb47a08 | 2014-05-19 12:53:03 +0000 | [diff] [blame^] | 183 | static void OpenPackedFileForWriting() { |
| 184 | CHECK(cov_fd == kInvalidFd); |
| 185 | InternalScopedBuffer<char> path(1024); |
| 186 | internal_snprintf((char *)path.data(), path.size(), "%zd.sancov.packed", |
| 187 | internal_getpid()); |
| 188 | uptr fd = OpenFile(path.data(), true); |
| 189 | if (internal_iserror(fd)) { |
| 190 | Report(" Coverage: failed to open %s for writing\n", path.data()); |
| 191 | Die(); |
| 192 | } |
| 193 | cov_fd = fd; |
| 194 | } |
| 195 | |
| 196 | void CovPrepareForSandboxing(__sanitizer_sandbox_arguments *args) { |
| 197 | if (!args) return; |
| 198 | if (!common_flags()->coverage) return; |
| 199 | cov_sandboxed = args->coverage_sandboxed; |
| 200 | if (!cov_sandboxed) return; |
| 201 | cov_fd = args->coverage_fd; |
| 202 | cov_max_block_size = args->coverage_max_block_size; |
| 203 | if (cov_fd < 0) |
| 204 | // Pre-open the file now. The sandbox won't allow us to do it later. |
| 205 | OpenPackedFileForWriting(); |
| 206 | } |
| 207 | |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 208 | } // namespace __sanitizer |
| 209 | |
| 210 | extern "C" { |
Kostya Serebryany | 714c67c | 2014-01-17 11:00:30 +0000 | [diff] [blame] | 211 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov() { |
| 212 | CovAdd(StackTrace::GetPreviousInstructionPc(GET_CALLER_PC())); |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 213 | } |
| 214 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() { CovDump(); } |
Kostya Serebryany | 8b530e1 | 2014-04-30 10:40:48 +0000 | [diff] [blame] | 215 | SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_init() { CovInit(); } |
Bob Wilson | a08e9ac | 2013-11-15 07:18:15 +0000 | [diff] [blame] | 216 | } // extern "C" |