1//===-- sanitizer_coverage_fuchsia.cpp ------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Sanitizer Coverage Controller for Trace PC Guard, Fuchsia-specific version.
10//
11// This Fuchsia-specific implementation uses the same basic scheme and the
12// same simple '.sancov' file format as the generic implementation. The
13// difference is that we just produce a single blob of output for the whole
14// program, not a separate one per DSO. We do not sort the PC table and do
15// not prune the zeros, so the resulting file is always as large as it
16// would be to report 100% coverage. Implicit tracing information about
17// the address ranges of DSOs allows offline tools to split the one big
18// blob into separate files that the 'sancov' tool can understand.
19//
20// Unlike the traditional implementation that uses an atexit hook to write
21// out data files at the end, the results on Fuchsia do not go into a file
22// per se. The 'coverage_dir' option is ignored. Instead, they are stored
23// directly into a shared memory object (a Zircon VMO). At exit, that VMO
24// is handed over to a system service that's responsible for getting the
25// data out to somewhere that it can be fed into the sancov tool (where and
26// how is not our problem).
27
28#include "sanitizer_platform.h"
29#if SANITIZER_FUCHSIA
30#include <zircon/process.h>
31#include <zircon/sanitizer.h>
32#include <zircon/syscalls.h>
33
34#include "sanitizer_atomic.h"
35#include "sanitizer_common.h"
36#include "sanitizer_interface_internal.h"
37#include "sanitizer_internal_defs.h"
38# include "sanitizer_symbolizer_markup_constants.h"
39
40using namespace __sanitizer;
41
42namespace __sancov {
43namespace {
44
45// TODO(mcgrathr): Move the constant into a header shared with other impls.
46constexpr u64 Magic64 = 0xC0BFFFFFFFFFFF64ULL;
47static_assert(SANITIZER_WORDSIZE == 64, "Fuchsia is always LP64");
48
49constexpr const char kSancovSinkName[] = "sancov";
50
51// Collects trace-pc guard coverage.
52// This class relies on zero-initialization.
53class TracePcGuardController final {
54 public:
55 constexpr TracePcGuardController() {}
56
57 // For each PC location being tracked, there is a u32 reserved in global
58 // data called the "guard". At startup, we assign each guard slot a
59 // unique index into the big results array. Later during runtime, the
60 // first call to TracePcGuard (below) will store the corresponding PC at
61 // that index in the array. (Each later call with the same guard slot is
62 // presumed to be from the same PC.) Then it clears the guard slot back
63 // to zero, which tells the compiler not to bother calling in again. At
64 // the end of the run, we have a big array where each element is either
65 // zero or is a tracked PC location that was hit in the trace.
66
67 // This is called from global constructors. Each translation unit has a
68 // contiguous array of guard slots, and a constructor that calls here
69 // with the bounds of its array. Those constructors are allowed to call
70 // here more than once for the same array. Usually all of these
71 // constructors run in the initial thread, but it's possible that a
72 // dlopen call on a secondary thread will run constructors that get here.
73 void InitTracePcGuard(u32 *start, u32 *end) {
74 if (end > start && *start == 0 && common_flags()->coverage) {
75 // Complete the setup before filling in any guards with indices.
76 // This avoids the possibility of code called from Setup reentering
77 // TracePcGuard.
78 u32 idx = Setup(end - start);
79 for (u32 *p = start; p < end; ++p) {
80 *p = idx++;
81 }
82 }
83 }
84
85 void TracePcGuard(u32 *guard, uptr pc) {
86 atomic_uint32_t *guard_ptr = reinterpret_cast<atomic_uint32_t *>(guard);
87 u32 idx = atomic_exchange(guard_ptr, 0, memory_order_relaxed);
88 if (idx > 0)
89 array_[idx] = pc;
90 }
91
92 void Dump() {
93 Lock locked(&setup_lock_);
94 if (array_) {
95 CHECK_NE(vmo_, ZX_HANDLE_INVALID);
96
97 // Publish the VMO to the system, where it can be collected and
98 // analyzed after this process exits. This always consumes the VMO
99 // handle. Any failure is just logged and not indicated to us.
100 __sanitizer_publish_data(kSancovSinkName, vmo_);
101 vmo_ = ZX_HANDLE_INVALID;
102
103 // This will route to __sanitizer_log_write, which will ensure that
104 // information about shared libraries is written out. This message
105 // uses the `dumpfile` symbolizer markup element to highlight the
106 // dump. See the explanation for this in:
107 // https://fuchsia.googlesource.com/zircon/+/master/docs/symbolizer_markup.md
108 if (common_flags()->print_coverage_summary)
109 Printf("SanitizerCoverage: " FORMAT_DUMPFILE " with up to %u PCs\n",
110 kSancovSinkName, vmo_name_, next_index_ - 1);
111 }
112 }
113
114 private:
115 // We map in the largest possible view into the VMO: one word
116 // for every possible 32-bit index value. This avoids the need
117 // to change the mapping when increasing the size of the VMO.
118 // We can always spare the 32G of address space.
119 static constexpr size_t MappingSize = sizeof(uptr) << 32;
120
121 Mutex setup_lock_;
122 uptr *array_ = nullptr;
123 u32 next_index_ = 0;
124 zx_handle_t vmo_ = {};
125 char vmo_name_[ZX_MAX_NAME_LEN] = {};
126
127 size_t DataSize() const { return next_index_ * sizeof(uintptr_t); }
128
129 u32 Setup(u32 num_guards) {
130 Lock locked(&setup_lock_);
131 DCHECK(common_flags()->coverage);
132
133 if (next_index_ == 0) {
134 CHECK_EQ(vmo_, ZX_HANDLE_INVALID);
135 CHECK_EQ(array_, nullptr);
136
137 // The first sample goes at [1] to reserve [0] for the magic number.
138 next_index_ = 1 + num_guards;
139
140 zx_status_t status = _zx_vmo_create(DataSize(), ZX_VMO_RESIZABLE, &vmo_);
141 CHECK_EQ(status, ZX_OK);
142
143 // Give the VMO a name including our process KOID so it's easy to spot.
144 internal_snprintf(vmo_name_, sizeof(vmo_name_), "%s.%zu", kSancovSinkName,
145 internal_getpid());
146 _zx_object_set_property(vmo_, ZX_PROP_NAME, vmo_name_,
147 internal_strlen(vmo_name_));
148 uint64_t size = DataSize();
149 status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size,
150 sizeof(size));
151 CHECK_EQ(status, ZX_OK);
152
153 // Map the largest possible view we might need into the VMO. Later
154 // we might need to increase the VMO's size before we can use larger
155 // indices, but we'll never move the mapping address so we don't have
156 // any multi-thread synchronization issues with that.
157 uintptr_t mapping;
158 status =
159 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE,
160 0, vmo_, 0, MappingSize, &mapping);
161 CHECK_EQ(status, ZX_OK);
162
163 // Hereafter other threads are free to start storing into
164 // elements [1, next_index_) of the big array.
165 array_ = reinterpret_cast<uptr *>(mapping);
166
167 // Store the magic number.
168 // Hereafter, the VMO serves as the contents of the '.sancov' file.
169 array_[0] = Magic64;
170
171 return 1;
172 } else {
173 // The VMO is already mapped in, but it's not big enough to use the
174 // new indices. So increase the size to cover the new maximum index.
175
176 CHECK_NE(vmo_, ZX_HANDLE_INVALID);
177 CHECK_NE(array_, nullptr);
178
179 uint32_t first_index = next_index_;
180 next_index_ += num_guards;
181
182 zx_status_t status = _zx_vmo_set_size(vmo_, DataSize());
183 CHECK_EQ(status, ZX_OK);
184 uint64_t size = DataSize();
185 status = _zx_object_set_property(vmo_, ZX_PROP_VMO_CONTENT_SIZE, &size,
186 sizeof(size));
187 CHECK_EQ(status, ZX_OK);
188
189 return first_index;
190 }
191 }
192};
193
194static TracePcGuardController pc_guard_controller;
195
196} // namespace
197} // namespace __sancov
198
199namespace __sanitizer {
200void InitializeCoverage(bool enabled, const char *dir) {
201 CHECK_EQ(enabled, common_flags()->coverage);
202 CHECK_EQ(dir, common_flags()->coverage_dir);
203
204 static bool coverage_enabled = false;
205 if (!coverage_enabled) {
206 coverage_enabled = enabled;
207 Atexit(__sanitizer_cov_dump);
208 AddDieCallback(__sanitizer_cov_dump);
209 }
210}
211} // namespace __sanitizer
212
213extern "C" {
214SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_coverage(const uptr *pcs,
215 uptr len) {
216 UNIMPLEMENTED();
217}
218
219SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard, u32 *guard) {
220 if (!*guard)
221 return;
222 __sancov::pc_guard_controller.TracePcGuard(guard, GET_CALLER_PC() - 1);
223}
224
225SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_guard_init,
226 u32 *start, u32 *end) {
227 if (start == end || *start)
228 return;
229 __sancov::pc_guard_controller.InitTracePcGuard(start, end);
230}
231
232SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_dump_trace_pc_guard_coverage() {
233 __sancov::pc_guard_controller.Dump();
234}
235SANITIZER_INTERFACE_ATTRIBUTE void __sanitizer_cov_dump() {
236 __sanitizer_dump_trace_pc_guard_coverage();
237}
238// Default empty implementations (weak). Users should redefine them.
239SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp, void) {}
240SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp1, void) {}
241SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp2, void) {}
242SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp4, void) {}
243SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_cmp8, void) {}
244SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp1, void) {}
245SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp2, void) {}
246SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp4, void) {}
247SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_const_cmp8, void) {}
248SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_switch, void) {}
249SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div4, void) {}
250SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_div8, void) {}
251SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_gep, void) {}
252SANITIZER_INTERFACE_WEAK_DEF(void, __sanitizer_cov_trace_pc_indir, void) {}
253} // extern "C"
254
255#endif // !SANITIZER_FUCHSIA
256