Add support for ARMv8-R architecture
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_coverage_mapping_libcdep.cc
blobb2e724ab221a8a3aa56d2c7ec15bde8f71a4fe4b
1 //===-- sanitizer_coverage_mapping.cc -------------------------------------===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // Mmap-based implementation of sanitizer coverage.
9 //
10 // This is part of the implementation of code coverage that does not require
11 // __sanitizer_cov_dump() call. Data is stored in 2 files per process.
13 // $pid.sancov.map describes process memory layout in the following text-based
14 // format:
15 // <pointer size in bits> // 1 line, 32 or 64
16 // <mapping start> <mapping end> <base address> <dso name> // repeated
17 // ...
18 // Mapping lines are NOT sorted. This file is updated every time memory layout
19 // is changed (i.e. in dlopen() and dlclose() interceptors).
21 // $pid.sancov.raw is a binary dump of PC values, sizeof(uptr) each. Again, not
22 // sorted. This file is extended by 64Kb at a time and mapped into memory. It
23 // contains one or more 0 words at the end, up to the next 64Kb aligned offset.
25 // To convert these 2 files to the usual .sancov format, run sancov.py rawunpack
26 // $pid.sancov.raw.
28 //===----------------------------------------------------------------------===//
30 #include "sanitizer_allocator_internal.h"
31 #include "sanitizer_libc.h"
32 #include "sanitizer_procmaps.h"
34 namespace __sanitizer {
36 static const uptr kMaxTextSize = 64 * 1024;
38 struct CachedMapping {
39 public:
40 bool NeedsUpdate(uptr pc) {
41 int new_pid = internal_getpid();
42 if (last_pid == new_pid && pc && pc >= last_range_start &&
43 pc < last_range_end)
44 return false;
45 last_pid = new_pid;
46 return true;
49 void SetModuleRange(uptr start, uptr end) {
50 last_range_start = start;
51 last_range_end = end;
54 private:
55 uptr last_range_start, last_range_end;
56 int last_pid;
59 static CachedMapping cached_mapping;
60 static StaticSpinMutex mapping_mu;
62 void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) {
63 if (!common_flags()->coverage_direct) return;
65 SpinMutexLock l(&mapping_mu);
67 if (!cached_mapping.NeedsUpdate(caller_pc))
68 return;
70 InternalScopedString text(kMaxTextSize);
73 text.append("%d\n", sizeof(uptr) * 8);
74 ListOfModules modules;
75 modules.init();
76 for (const LoadedModule &module : modules) {
77 const char *module_name = StripModuleName(module.full_name());
78 uptr base = module.base_address();
79 for (const auto &range : module.ranges()) {
80 if (range.executable) {
81 uptr start = range.beg;
82 uptr end = range.end;
83 text.append("%zx %zx %zx %s\n", start, end, base, module_name);
84 if (caller_pc && caller_pc >= start && caller_pc < end)
85 cached_mapping.SetModuleRange(start, end);
91 error_t err;
92 InternalScopedString tmp_path(64 + internal_strlen(coverage_dir));
93 uptr res = internal_snprintf((char *)tmp_path.data(), tmp_path.size(),
94 "%s/%zd.sancov.map.tmp", coverage_dir,
95 internal_getpid());
96 CHECK_LE(res, tmp_path.size());
97 fd_t map_fd = OpenFile(tmp_path.data(), WrOnly, &err);
98 if (map_fd == kInvalidFd) {
99 Report("Coverage: failed to open %s for writing: %d\n", tmp_path.data(),
100 err);
101 Die();
104 if (!WriteToFile(map_fd, text.data(), text.length(), nullptr, &err)) {
105 Printf("sancov.map write failed: %d\n", err);
106 Die();
108 CloseFile(map_fd);
110 InternalScopedString path(64 + internal_strlen(coverage_dir));
111 res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map",
112 coverage_dir, internal_getpid());
113 CHECK_LE(res, path.size());
114 if (!RenameFile(tmp_path.data(), path.data(), &err)) {
115 Printf("sancov.map rename failed: %d\n", err);
116 Die();
120 } // namespace __sanitizer