Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 1 | //===-- sanitizer_coverage_mapping.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 | // Mmap-based implementation of sanitizer coverage. |
| 11 | // |
| 12 | // This is part of the implementation of code coverage that does not require |
| 13 | // __sanitizer_cov_dump() call. Data is stored in 2 files per process. |
| 14 | // |
| 15 | // $pid.sancov.map describes process memory layout in the following text-based |
| 16 | // format: |
| 17 | // <pointer size in bits> // 1 line, 32 or 64 |
| 18 | // <mapping start> <mapping end> <base address> <dso name> // repeated |
| 19 | // ... |
| 20 | // Mapping lines are NOT sorted. This file is updated every time memory layout |
| 21 | // is changed (i.e. in dlopen() and dlclose() interceptors). |
| 22 | // |
| 23 | // $pid.sancov.raw is a binary dump of PC values, sizeof(uptr) each. Again, not |
| 24 | // sorted. This file is extended by 64Kb at a time and mapped into memory. It |
| 25 | // contains one or more 0 words at the end, up to the next 64Kb aligned offset. |
| 26 | // |
| 27 | // To convert these 2 files to the usual .sancov format, run sancov.py rawunpack |
| 28 | // $pid.sancov.raw. |
| 29 | // |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | |
| 32 | #include "sanitizer_allocator_internal.h" |
| 33 | #include "sanitizer_libc.h" |
| 34 | #include "sanitizer_procmaps.h" |
| 35 | |
| 36 | namespace __sanitizer { |
| 37 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 38 | static const uptr kMaxTextSize = 64 * 1024; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 39 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 40 | struct CachedMapping { |
| 41 | public: |
| 42 | bool NeedsUpdate(uptr pc) { |
| 43 | int new_pid = internal_getpid(); |
| 44 | if (last_pid == new_pid && pc && pc >= last_range_start && |
| 45 | pc < last_range_end) |
| 46 | return false; |
| 47 | last_pid = new_pid; |
| 48 | return true; |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 51 | void SetModuleRange(uptr start, uptr end) { |
| 52 | last_range_start = start; |
| 53 | last_range_end = end; |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | uptr last_range_start, last_range_end; |
| 58 | int last_pid; |
| 59 | }; |
| 60 | |
| 61 | static CachedMapping cached_mapping; |
| 62 | static StaticSpinMutex mapping_mu; |
| 63 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 64 | void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) { |
| 65 | if (!common_flags()->coverage_direct) return; |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 66 | |
| 67 | SpinMutexLock l(&mapping_mu); |
| 68 | |
| 69 | if (!cached_mapping.NeedsUpdate(caller_pc)) |
| 70 | return; |
| 71 | |
| 72 | InternalScopedString text(kMaxTextSize); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 73 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 74 | { |
| 75 | InternalScopedBuffer<LoadedModule> modules(kMaxNumberOfModules); |
| 76 | CHECK(modules.data()); |
| 77 | int n_modules = GetListOfModules(modules.data(), kMaxNumberOfModules, |
| 78 | /* filter */ 0); |
| 79 | |
| 80 | text.append("%d\n", sizeof(uptr) * 8); |
| 81 | for (int i = 0; i < n_modules; ++i) { |
| 82 | const char *module_name = StripModuleName(modules[i].full_name()); |
| 83 | uptr base = modules[i].base_address(); |
| 84 | for (auto iter = modules[i].ranges(); iter.hasNext();) { |
| 85 | const auto *range = iter.next(); |
| 86 | if (range->executable) { |
| 87 | uptr start = range->beg; |
| 88 | uptr end = range->end; |
| 89 | text.append("%zx %zx %zx %s\n", start, end, base, module_name); |
| 90 | if (caller_pc && caller_pc >= start && caller_pc < end) |
| 91 | cached_mapping.SetModuleRange(start, end); |
| 92 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 93 | } |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 94 | modules[i].clear(); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 95 | } |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 98 | error_t err; |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 99 | InternalScopedString tmp_path(64 + internal_strlen(coverage_dir)); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 100 | uptr res = internal_snprintf((char *)tmp_path.data(), tmp_path.size(), |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 101 | "%s/%zd.sancov.map.tmp", coverage_dir, |
| 102 | internal_getpid()); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 103 | CHECK_LE(res, tmp_path.size()); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 104 | fd_t map_fd = OpenFile(tmp_path.data(), WrOnly, &err); |
| 105 | if (map_fd == kInvalidFd) { |
| 106 | Report("Coverage: failed to open %s for writing: %d\n", tmp_path.data(), |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 107 | err); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 108 | Die(); |
| 109 | } |
| 110 | |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 111 | if (!WriteToFile(map_fd, text.data(), text.length(), nullptr, &err)) { |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 112 | Printf("sancov.map write failed: %d\n", err); |
| 113 | Die(); |
| 114 | } |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 115 | CloseFile(map_fd); |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 116 | |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 117 | InternalScopedString path(64 + internal_strlen(coverage_dir)); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 118 | res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map", |
Stephen Hines | 86277eb | 2015-03-23 12:06:32 -0700 | [diff] [blame] | 119 | coverage_dir, internal_getpid()); |
Stephen Hines | 6a211c5 | 2014-07-21 00:49:56 -0700 | [diff] [blame] | 120 | CHECK_LE(res, path.size()); |
Pirama Arumuga Nainar | 259f706 | 2015-05-06 11:49:53 -0700 | [diff] [blame] | 121 | if (!RenameFile(tmp_path.data(), path.data(), &err)) { |
Stephen Hines | 2d1fdb2 | 2014-05-28 23:58:16 -0700 | [diff] [blame] | 122 | Printf("sancov.map rename failed: %d\n", err); |
| 123 | Die(); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | } // namespace __sanitizer |