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 InternalScopedBuffer
<LoadedModule
> modules(kMaxNumberOfModules
);
74 CHECK(modules
.data());
75 int n_modules
= GetListOfModules(modules
.data(), kMaxNumberOfModules
,
76 /* filter */ nullptr);
78 text
.append("%d\n", sizeof(uptr
) * 8);
79 for (int i
= 0; i
< n_modules
; ++i
) {
80 const char *module_name
= StripModuleName(modules
[i
].full_name());
81 uptr base
= modules
[i
].base_address();
82 for (auto iter
= modules
[i
].ranges(); iter
.hasNext();) {
83 const auto *range
= iter
.next();
84 if (range
->executable
) {
85 uptr start
= range
->beg
;
86 uptr end
= range
->end
;
87 text
.append("%zx %zx %zx %s\n", start
, end
, base
, module_name
);
88 if (caller_pc
&& caller_pc
>= start
&& caller_pc
< end
)
89 cached_mapping
.SetModuleRange(start
, end
);
97 InternalScopedString
tmp_path(64 + internal_strlen(coverage_dir
));
98 uptr res
= internal_snprintf((char *)tmp_path
.data(), tmp_path
.size(),
99 "%s/%zd.sancov.map.tmp", coverage_dir
,
101 CHECK_LE(res
, tmp_path
.size());
102 fd_t map_fd
= OpenFile(tmp_path
.data(), WrOnly
, &err
);
103 if (map_fd
== kInvalidFd
) {
104 Report("Coverage: failed to open %s for writing: %d\n", tmp_path
.data(),
109 if (!WriteToFile(map_fd
, text
.data(), text
.length(), nullptr, &err
)) {
110 Printf("sancov.map write failed: %d\n", err
);
115 InternalScopedString
path(64 + internal_strlen(coverage_dir
));
116 res
= internal_snprintf((char *)path
.data(), path
.size(), "%s/%zd.sancov.map",
117 coverage_dir
, internal_getpid());
118 CHECK_LE(res
, path
.size());
119 if (!RenameFile(tmp_path
.data(), path
.data(), &err
)) {
120 Printf("sancov.map rename failed: %d\n", err
);
125 } // namespace __sanitizer