1 //===-- sanitizer_coverage_mapping.cc -------------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // Mmap-based implementation of sanitizer coverage.
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
15 // <pointer size in bits> // 1 line, 32 or 64
16 // <mapping start> <mapping end> <base address> <dso name> // repeated
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
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
{
40 bool NeedsUpdate(uptr pc
) {
41 int new_pid
= internal_getpid();
42 if (last_pid
== new_pid
&& pc
&& pc
>= last_range_start
&&
49 void SetModuleRange(uptr start
, uptr end
) {
50 last_range_start
= start
;
55 uptr last_range_start
, last_range_end
;
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
))
70 InternalScopedString
text(kMaxTextSize
);
73 text
.append("%d\n", sizeof(uptr
) * 8);
74 ListOfModules modules
;
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
;
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
);
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
,
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(),
104 if (!WriteToFile(map_fd
, text
.data(), text
.length(), nullptr, &err
)) {
105 Printf("sancov.map write failed: %d\n", err
);
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
);
120 } // namespace __sanitizer