1 //===-- sanitizer_coverage_libcdep_new.cc ---------------------------------===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
7 // Sanitizer Coverage Controller for Trace PC Guard.
9 #include "sanitizer_platform.h"
11 #if !SANITIZER_FUCHSIA
12 #include "sancov_flags.h"
13 #include "sanitizer_allocator_internal.h"
14 #include "sanitizer_atomic.h"
15 #include "sanitizer_common.h"
16 #include "sanitizer_file.h"
17 #include "sanitizer_symbolizer.h"
19 using namespace __sanitizer
;
21 using AddressRange
= LoadedModule::AddressRange
;
26 static const u64 Magic64
= 0xC0BFFFFFFFFFFF64ULL
;
27 static const u64 Magic32
= 0xC0BFFFFFFFFFFF32ULL
;
28 static const u64 Magic
= SANITIZER_WORDSIZE
== 64 ? Magic64
: Magic32
;
30 static fd_t
OpenFile(const char* path
) {
32 fd_t fd
= OpenFile(path
, WrOnly
, &err
);
34 Report("SanitizerCoverage: failed to open %s for writing (reason: %d)\n",
39 static void GetCoverageFilename(char* path
, const char* name
,
40 const char* extension
) {
42 internal_snprintf(path
, kMaxPathLength
, "%s/%s.%zd.%s",
43 common_flags()->coverage_dir
, name
, internal_getpid(),
47 static void WriteModuleCoverage(char* file_path
, const char* module_name
,
48 const uptr
* pcs
, uptr len
) {
49 GetCoverageFilename(file_path
, StripModuleName(module_name
), "sancov");
50 fd_t fd
= OpenFile(file_path
);
51 WriteToFile(fd
, &Magic
, sizeof(Magic
));
52 WriteToFile(fd
, pcs
, len
* sizeof(*pcs
));
54 Printf("SanitizerCoverage: %s: %zd PCs written\n", file_path
, len
);
57 static void SanitizerDumpCoverage(const uptr
* unsorted_pcs
, uptr len
) {
60 char* file_path
= static_cast<char*>(InternalAlloc(kMaxPathLength
));
61 char* module_name
= static_cast<char*>(InternalAlloc(kMaxPathLength
));
62 uptr
* pcs
= static_cast<uptr
*>(InternalAlloc(len
* sizeof(uptr
)));
64 internal_memcpy(pcs
, unsorted_pcs
, len
* sizeof(uptr
));
67 bool module_found
= false;
69 uptr module_start_idx
= 0;
71 for (uptr i
= 0; i
< len
; ++i
) {
72 const uptr pc
= pcs
[i
];
75 if (!__sanitizer_get_module_and_offset_for_pc(pc
, nullptr, 0, &pcs
[i
])) {
76 Printf("ERROR: unknown pc 0x%x (may happen if dlclose is used)\n", pc
);
79 uptr module_base
= pc
- pcs
[i
];
81 if (module_base
!= last_base
|| !module_found
) {
83 WriteModuleCoverage(file_path
, module_name
, &pcs
[module_start_idx
],
84 i
- module_start_idx
);
87 last_base
= module_base
;
90 __sanitizer_get_module_and_offset_for_pc(pc
, module_name
, kMaxPathLength
,
96 WriteModuleCoverage(file_path
, module_name
, &pcs
[module_start_idx
],
97 len
- module_start_idx
);
100 InternalFree(file_path
);
101 InternalFree(module_name
);
105 // Collects trace-pc guard coverage.
106 // This class relies on zero-initialization.
107 class TracePcGuardController
{
113 InitializeSancovFlags();
115 pc_vector
.Initialize(0);
118 void InitTracePcGuard(u32
* start
, u32
* end
) {
119 if (!initialized
) Initialize();
121 CHECK_NE(start
, end
);
123 u32 i
= pc_vector
.size();
124 for (u32
* p
= start
; p
< end
; p
++) *p
= ++i
;
128 void TracePcGuard(u32
* guard
, uptr pc
) {
131 // we start indices from 1.
132 atomic_uintptr_t
* pc_ptr
=
133 reinterpret_cast<atomic_uintptr_t
*>(&pc_vector
[idx
- 1]);
134 if (atomic_load(pc_ptr
, memory_order_relaxed
) == 0)
135 atomic_store(pc_ptr
, pc
, memory_order_relaxed
);
139 internal_memset(&pc_vector
[0], 0, sizeof(pc_vector
[0]) * pc_vector
.size());
143 if (!initialized
|| !common_flags()->coverage
) return;
144 __sanitizer_dump_coverage(pc_vector
.data(), pc_vector
.size());
149 InternalMmapVectorNoCtor
<uptr
> pc_vector
;
152 static TracePcGuardController pc_guard_controller
;
155 } // namespace __sancov
157 namespace __sanitizer
{
158 void InitializeCoverage(bool enabled
, const char *dir
) {
159 static bool coverage_enabled
= false;
160 if (coverage_enabled
)
161 return; // May happen if two sanitizer enable coverage in the same process.
162 coverage_enabled
= enabled
;
163 Atexit(__sanitizer_cov_dump
);
164 AddDieCallback(__sanitizer_cov_dump
);
166 } // namespace __sanitizer
169 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_dump_coverage( // NOLINT
170 const uptr
* pcs
, uptr len
) {
171 return __sancov::SanitizerDumpCoverage(pcs
, len
);
174 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard
, u32
* guard
) {
176 __sancov::pc_guard_controller
.TracePcGuard(guard
, GET_CALLER_PC() - 1);
179 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init
,
180 u32
* start
, u32
* end
) {
181 if (start
== end
|| *start
) return;
182 __sancov::pc_guard_controller
.InitTracePcGuard(start
, end
);
185 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_dump_trace_pc_guard_coverage() {
186 __sancov::pc_guard_controller
.Dump();
188 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_dump() {
189 __sanitizer_dump_trace_pc_guard_coverage();
191 SANITIZER_INTERFACE_ATTRIBUTE
void __sanitizer_cov_reset() {
192 __sancov::pc_guard_controller
.Reset();
194 // Default empty implementations (weak). Users should redefine them.
195 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp
, void) {}
196 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp1
, void) {}
197 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp2
, void) {}
198 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp4
, void) {}
199 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp8
, void) {}
200 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp1
, void) {}
201 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp2
, void) {}
202 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp4
, void) {}
203 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp8
, void) {}
204 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_switch
, void) {}
205 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div4
, void) {}
206 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div8
, void) {}
207 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_gep
, void) {}
208 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir
, void) {}
209 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_8bit_counters_init
, void) {}
210 SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_pcs_init
, void) {}
212 // Weak definition for code instrumented with -fsanitize-coverage=stack-depth
213 // and later linked with code containing a strong definition.
214 // E.g., -fsanitize=fuzzer-no-link
215 SANITIZER_INTERFACE_ATTRIBUTE SANITIZER_WEAK_ATTRIBUTE
216 SANITIZER_TLS_INITIAL_EXEC_ATTRIBUTE uptr __sancov_lowest_stack
;
218 #endif // !SANITIZER_FUCHSIA