]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/sanitizer_common/sanitizer_coverage_mapping_libcdep.cc
MFV: r283965
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / sanitizer_common / sanitizer_coverage_mapping_libcdep.cc
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
38 static const uptr kMaxNumberOfModules = 1 << 14;
39 static const uptr kMaxTextSize = 64 * 1024;
40
41 struct CachedMapping {
42  public:
43   bool NeedsUpdate(uptr pc) {
44     int new_pid = internal_getpid();
45     if (last_pid == new_pid && pc && pc >= last_range_start &&
46         pc < last_range_end)
47       return false;
48     last_pid = new_pid;
49     return true;
50   }
51
52   void SetModuleRange(uptr start, uptr end) {
53     last_range_start = start;
54     last_range_end = end;
55   }
56
57  private:
58   uptr last_range_start, last_range_end;
59   int last_pid;
60 };
61
62 static CachedMapping cached_mapping;
63 static StaticSpinMutex mapping_mu;
64
65 void CovUpdateMapping(const char *coverage_dir, uptr caller_pc) {
66   if (!common_flags()->coverage_direct) return;
67
68   SpinMutexLock l(&mapping_mu);
69
70   if (!cached_mapping.NeedsUpdate(caller_pc))
71     return;
72
73   InternalScopedString text(kMaxTextSize);
74
75   {
76     InternalScopedBuffer<LoadedModule> modules(kMaxNumberOfModules);
77     CHECK(modules.data());
78     int n_modules = GetListOfModules(modules.data(), kMaxNumberOfModules,
79                                      /* filter */ 0);
80
81     text.append("%d\n", sizeof(uptr) * 8);
82     for (int i = 0; i < n_modules; ++i) {
83       const char *module_name = StripModuleName(modules[i].full_name());
84       uptr base = modules[i].base_address();
85       for (auto iter = modules[i].ranges(); iter.hasNext();) {
86         const auto *range = iter.next();
87         if (range->executable) {
88           uptr start = range->beg;
89           uptr end = range->end;
90           text.append("%zx %zx %zx %s\n", start, end, base, module_name);
91           if (caller_pc && caller_pc >= start && caller_pc < end)
92             cached_mapping.SetModuleRange(start, end);
93         }
94       }
95       modules[i].clear();
96     }
97   }
98
99   int err;
100   InternalScopedString tmp_path(64 + internal_strlen(coverage_dir));
101   uptr res = internal_snprintf((char *)tmp_path.data(), tmp_path.size(),
102                                "%s/%zd.sancov.map.tmp", coverage_dir,
103                                internal_getpid());
104   CHECK_LE(res, tmp_path.size());
105   uptr map_fd = OpenFile(tmp_path.data(), true);
106   if (internal_iserror(map_fd, &err)) {
107     Report(" Coverage: failed to open %s for writing: %d\n", tmp_path.data(),
108            err);
109     Die();
110   }
111
112   res = internal_write(map_fd, text.data(), text.length());
113   if (internal_iserror(res, &err)) {
114     Printf("sancov.map write failed: %d\n", err);
115     Die();
116   }
117   internal_close(map_fd);
118
119   InternalScopedString path(64 + internal_strlen(coverage_dir));
120   res = internal_snprintf((char *)path.data(), path.size(), "%s/%zd.sancov.map",
121                           coverage_dir, internal_getpid());
122   CHECK_LE(res, path.size());
123   res = internal_rename(tmp_path.data(), path.data());
124   if (internal_iserror(res, &err)) {
125     Printf("sancov.map rename failed: %d\n", err);
126     Die();
127   }
128 }
129
130 }  // namespace __sanitizer