1 //===-- sanitizer_coverage_mapping.cc -------------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Mmap-based implementation of sanitizer coverage.
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.
15 // $pid.sancov.map describes process memory layout in the following text-based
17 // <pointer size in bits> // 1 line, 32 or 64
18 // <mapping start> <mapping end> <base address> <dso name> // repeated
20 // Mapping lines are NOT sorted. This file is updated every time memory layout
21 // is changed (i.e. in dlopen() and dlclose() interceptors).
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.
27 // To convert these 2 files to the usual .sancov format, run sancov.py rawunpack
30 //===----------------------------------------------------------------------===//
32 #include "sanitizer_allocator_internal.h"
33 #include "sanitizer_libc.h"
34 #include "sanitizer_procmaps.h"
36 namespace __sanitizer
{
38 static const uptr kMaxTextSize
= 64 * 1024;
40 struct CachedMapping
{
42 bool NeedsUpdate(uptr pc
) {
43 int new_pid
= internal_getpid();
44 if (last_pid
== new_pid
&& pc
&& pc
>= last_range_start
&&
51 void SetModuleRange(uptr start
, uptr end
) {
52 last_range_start
= start
;
57 uptr last_range_start
, last_range_end
;
61 static CachedMapping cached_mapping
;
62 static StaticSpinMutex mapping_mu
;
64 void CovUpdateMapping(const char *coverage_dir
, uptr caller_pc
) {
65 if (!common_flags()->coverage_direct
) return;
67 SpinMutexLock
l(&mapping_mu
);
69 if (!cached_mapping
.NeedsUpdate(caller_pc
))
72 InternalScopedString
text(kMaxTextSize
);
75 text
.append("%d\n", sizeof(uptr
) * 8);
76 ListOfModules modules
;
78 for (const LoadedModule
&module
: modules
) {
79 const char *module_name
= StripModuleName(module
.full_name());
80 uptr base
= module
.base_address();
81 for (const auto &range
: module
.ranges()) {
82 if (range
.executable
) {
83 uptr start
= range
.beg
;
85 text
.append("%zx %zx %zx %s\n", start
, end
, base
, module_name
);
86 if (caller_pc
&& caller_pc
>= start
&& caller_pc
< end
)
87 cached_mapping
.SetModuleRange(start
, end
);
94 InternalScopedString
tmp_path(64 + internal_strlen(coverage_dir
));
95 uptr res
= internal_snprintf((char *)tmp_path
.data(), tmp_path
.size(),
96 "%s/%zd.sancov.map.tmp", coverage_dir
,
98 CHECK_LE(res
, tmp_path
.size());
99 fd_t map_fd
= OpenFile(tmp_path
.data(), WrOnly
, &err
);
100 if (map_fd
== kInvalidFd
) {
101 Report("Coverage: failed to open %s for writing: %d\n", tmp_path
.data(),
106 if (!WriteToFile(map_fd
, text
.data(), text
.length(), nullptr, &err
)) {
107 Printf("sancov.map write failed: %d\n", err
);
112 InternalScopedString
path(64 + internal_strlen(coverage_dir
));
113 res
= internal_snprintf((char *)path
.data(), path
.size(), "%s/%zd.sancov.map",
114 coverage_dir
, internal_getpid());
115 CHECK_LE(res
, path
.size());
116 if (!RenameFile(tmp_path
.data(), path
.data(), &err
)) {
117 Printf("sancov.map rename failed: %d\n", err
);
122 } // namespace __sanitizer