1*490215a3Smrg //===-- sanitizer_fuchsia.cc ----------------------------------------------===//
2*490215a3Smrg //
3*490215a3Smrg // This file is distributed under the University of Illinois Open Source
4*490215a3Smrg // License. See LICENSE.TXT for details.
5*490215a3Smrg //
6*490215a3Smrg //===----------------------------------------------------------------------===//
7*490215a3Smrg //
8*490215a3Smrg // This file is shared between AddressSanitizer and other sanitizer
9*490215a3Smrg // run-time libraries and implements Fuchsia-specific functions from
10*490215a3Smrg // sanitizer_common.h.
11*490215a3Smrg //===----------------------------------------------------------------------===//
12*490215a3Smrg
13*490215a3Smrg #include "sanitizer_fuchsia.h"
14*490215a3Smrg #if SANITIZER_FUCHSIA
15*490215a3Smrg
16*490215a3Smrg #include "sanitizer_common.h"
17*490215a3Smrg #include "sanitizer_libc.h"
18*490215a3Smrg #include "sanitizer_mutex.h"
19*490215a3Smrg
20*490215a3Smrg #include <limits.h>
21*490215a3Smrg #include <pthread.h>
22*490215a3Smrg #include <stdlib.h>
23*490215a3Smrg #include <unistd.h>
24*490215a3Smrg #include <zircon/errors.h>
25*490215a3Smrg #include <zircon/process.h>
26*490215a3Smrg #include <zircon/syscalls.h>
27*490215a3Smrg
28*490215a3Smrg namespace __sanitizer {
29*490215a3Smrg
internal__exit(int exitcode)30*490215a3Smrg void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); }
31*490215a3Smrg
internal_sched_yield()32*490215a3Smrg uptr internal_sched_yield() {
33*490215a3Smrg zx_status_t status = _zx_nanosleep(0);
34*490215a3Smrg CHECK_EQ(status, ZX_OK);
35*490215a3Smrg return 0; // Why doesn't this return void?
36*490215a3Smrg }
37*490215a3Smrg
internal_nanosleep(zx_time_t ns)38*490215a3Smrg static void internal_nanosleep(zx_time_t ns) {
39*490215a3Smrg zx_status_t status = _zx_nanosleep(_zx_deadline_after(ns));
40*490215a3Smrg CHECK_EQ(status, ZX_OK);
41*490215a3Smrg }
42*490215a3Smrg
internal_sleep(unsigned int seconds)43*490215a3Smrg unsigned int internal_sleep(unsigned int seconds) {
44*490215a3Smrg internal_nanosleep(ZX_SEC(seconds));
45*490215a3Smrg return 0;
46*490215a3Smrg }
47*490215a3Smrg
NanoTime()48*490215a3Smrg u64 NanoTime() { return _zx_clock_get(ZX_CLOCK_UTC); }
49*490215a3Smrg
MonotonicNanoTime()50*490215a3Smrg u64 MonotonicNanoTime() { return _zx_clock_get(ZX_CLOCK_MONOTONIC); }
51*490215a3Smrg
internal_getpid()52*490215a3Smrg uptr internal_getpid() {
53*490215a3Smrg zx_info_handle_basic_t info;
54*490215a3Smrg zx_status_t status =
55*490215a3Smrg _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info,
56*490215a3Smrg sizeof(info), NULL, NULL);
57*490215a3Smrg CHECK_EQ(status, ZX_OK);
58*490215a3Smrg uptr pid = static_cast<uptr>(info.koid);
59*490215a3Smrg CHECK_EQ(pid, info.koid);
60*490215a3Smrg return pid;
61*490215a3Smrg }
62*490215a3Smrg
internal_dlinfo(void * handle,int request,void * p)63*490215a3Smrg int internal_dlinfo(void *handle, int request, void *p) {
64*490215a3Smrg UNIMPLEMENTED();
65*490215a3Smrg }
66*490215a3Smrg
GetThreadSelf()67*490215a3Smrg uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }
68*490215a3Smrg
GetTid()69*490215a3Smrg tid_t GetTid() { return GetThreadSelf(); }
70*490215a3Smrg
Abort()71*490215a3Smrg void Abort() { abort(); }
72*490215a3Smrg
Atexit(void (* function)(void))73*490215a3Smrg int Atexit(void (*function)(void)) { return atexit(function); }
74*490215a3Smrg
SleepForSeconds(int seconds)75*490215a3Smrg void SleepForSeconds(int seconds) { internal_sleep(seconds); }
76*490215a3Smrg
SleepForMillis(int millis)77*490215a3Smrg void SleepForMillis(int millis) { internal_nanosleep(ZX_MSEC(millis)); }
78*490215a3Smrg
GetThreadStackTopAndBottom(bool,uptr * stack_top,uptr * stack_bottom)79*490215a3Smrg void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
80*490215a3Smrg pthread_attr_t attr;
81*490215a3Smrg CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
82*490215a3Smrg void *base;
83*490215a3Smrg size_t size;
84*490215a3Smrg CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0);
85*490215a3Smrg CHECK_EQ(pthread_attr_destroy(&attr), 0);
86*490215a3Smrg
87*490215a3Smrg *stack_bottom = reinterpret_cast<uptr>(base);
88*490215a3Smrg *stack_top = *stack_bottom + size;
89*490215a3Smrg }
90*490215a3Smrg
MaybeReexec()91*490215a3Smrg void MaybeReexec() {}
CheckASLR()92*490215a3Smrg void CheckASLR() {}
PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments * args)93*490215a3Smrg void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {}
DisableCoreDumperIfNecessary()94*490215a3Smrg void DisableCoreDumperIfNecessary() {}
InstallDeadlySignalHandlers(SignalHandlerType handler)95*490215a3Smrg void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
SetAlternateSignalStack()96*490215a3Smrg void SetAlternateSignalStack() {}
UnsetAlternateSignalStack()97*490215a3Smrg void UnsetAlternateSignalStack() {}
InitTlsSize()98*490215a3Smrg void InitTlsSize() {}
99*490215a3Smrg
PrintModuleMap()100*490215a3Smrg void PrintModuleMap() {}
101*490215a3Smrg
IsStackOverflow() const102*490215a3Smrg bool SignalContext::IsStackOverflow() const { return false; }
DumpAllRegisters(void * context)103*490215a3Smrg void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
Describe() const104*490215a3Smrg const char *SignalContext::Describe() const { UNIMPLEMENTED(); }
105*490215a3Smrg
106*490215a3Smrg enum MutexState : int { MtxUnlocked = 0, MtxLocked = 1, MtxSleeping = 2 };
107*490215a3Smrg
BlockingMutex()108*490215a3Smrg BlockingMutex::BlockingMutex() {
109*490215a3Smrg // NOTE! It's important that this use internal_memset, because plain
110*490215a3Smrg // memset might be intercepted (e.g., actually be __asan_memset).
111*490215a3Smrg // Defining this so the compiler initializes each field, e.g.:
112*490215a3Smrg // BlockingMutex::BlockingMutex() : BlockingMutex(LINKER_INITIALIZED) {}
113*490215a3Smrg // might result in the compiler generating a call to memset, which would
114*490215a3Smrg // have the same problem.
115*490215a3Smrg internal_memset(this, 0, sizeof(*this));
116*490215a3Smrg }
117*490215a3Smrg
Lock()118*490215a3Smrg void BlockingMutex::Lock() {
119*490215a3Smrg CHECK_EQ(owner_, 0);
120*490215a3Smrg atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
121*490215a3Smrg if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
122*490215a3Smrg return;
123*490215a3Smrg while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
124*490215a3Smrg zx_status_t status = _zx_futex_wait(reinterpret_cast<zx_futex_t *>(m),
125*490215a3Smrg MtxSleeping, ZX_TIME_INFINITE);
126*490215a3Smrg if (status != ZX_ERR_BAD_STATE) // Normal race.
127*490215a3Smrg CHECK_EQ(status, ZX_OK);
128*490215a3Smrg }
129*490215a3Smrg }
130*490215a3Smrg
Unlock()131*490215a3Smrg void BlockingMutex::Unlock() {
132*490215a3Smrg atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
133*490215a3Smrg u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
134*490215a3Smrg CHECK_NE(v, MtxUnlocked);
135*490215a3Smrg if (v == MtxSleeping) {
136*490215a3Smrg zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(m), 1);
137*490215a3Smrg CHECK_EQ(status, ZX_OK);
138*490215a3Smrg }
139*490215a3Smrg }
140*490215a3Smrg
CheckLocked()141*490215a3Smrg void BlockingMutex::CheckLocked() {
142*490215a3Smrg atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
143*490215a3Smrg CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
144*490215a3Smrg }
145*490215a3Smrg
GetPageSize()146*490215a3Smrg uptr GetPageSize() { return PAGE_SIZE; }
147*490215a3Smrg
GetMmapGranularity()148*490215a3Smrg uptr GetMmapGranularity() { return PAGE_SIZE; }
149*490215a3Smrg
150*490215a3Smrg sanitizer_shadow_bounds_t ShadowBounds;
151*490215a3Smrg
GetMaxUserVirtualAddress()152*490215a3Smrg uptr GetMaxUserVirtualAddress() {
153*490215a3Smrg ShadowBounds = __sanitizer_shadow_bounds();
154*490215a3Smrg return ShadowBounds.memory_limit - 1;
155*490215a3Smrg }
156*490215a3Smrg
GetMaxVirtualAddress()157*490215a3Smrg uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); }
158*490215a3Smrg
DoAnonymousMmapOrDie(uptr size,const char * mem_type,bool raw_report,bool die_for_nomem)159*490215a3Smrg static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type,
160*490215a3Smrg bool raw_report, bool die_for_nomem) {
161*490215a3Smrg size = RoundUpTo(size, PAGE_SIZE);
162*490215a3Smrg
163*490215a3Smrg zx_handle_t vmo;
164*490215a3Smrg zx_status_t status = _zx_vmo_create(size, 0, &vmo);
165*490215a3Smrg if (status != ZX_OK) {
166*490215a3Smrg if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
167*490215a3Smrg ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status,
168*490215a3Smrg raw_report);
169*490215a3Smrg return nullptr;
170*490215a3Smrg }
171*490215a3Smrg _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
172*490215a3Smrg internal_strlen(mem_type));
173*490215a3Smrg
174*490215a3Smrg // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
175*490215a3Smrg uintptr_t addr;
176*490215a3Smrg status =
177*490215a3Smrg _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
178*490215a3Smrg vmo, 0, size, &addr);
179*490215a3Smrg _zx_handle_close(vmo);
180*490215a3Smrg
181*490215a3Smrg if (status != ZX_OK) {
182*490215a3Smrg if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
183*490215a3Smrg ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status,
184*490215a3Smrg raw_report);
185*490215a3Smrg return nullptr;
186*490215a3Smrg }
187*490215a3Smrg
188*490215a3Smrg IncreaseTotalMmap(size);
189*490215a3Smrg
190*490215a3Smrg return reinterpret_cast<void *>(addr);
191*490215a3Smrg }
192*490215a3Smrg
MmapOrDie(uptr size,const char * mem_type,bool raw_report)193*490215a3Smrg void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
194*490215a3Smrg return DoAnonymousMmapOrDie(size, mem_type, raw_report, true);
195*490215a3Smrg }
196*490215a3Smrg
MmapNoReserveOrDie(uptr size,const char * mem_type)197*490215a3Smrg void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
198*490215a3Smrg return MmapOrDie(size, mem_type);
199*490215a3Smrg }
200*490215a3Smrg
MmapOrDieOnFatalError(uptr size,const char * mem_type)201*490215a3Smrg void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
202*490215a3Smrg return DoAnonymousMmapOrDie(size, mem_type, false, false);
203*490215a3Smrg }
204*490215a3Smrg
Init(uptr init_size,const char * name,uptr fixed_addr)205*490215a3Smrg uptr ReservedAddressRange::Init(uptr init_size, const char *name,
206*490215a3Smrg uptr fixed_addr) {
207*490215a3Smrg init_size = RoundUpTo(init_size, PAGE_SIZE);
208*490215a3Smrg DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID);
209*490215a3Smrg uintptr_t base;
210*490215a3Smrg zx_handle_t vmar;
211*490215a3Smrg zx_status_t status =
212*490215a3Smrg _zx_vmar_allocate_old(_zx_vmar_root_self(), 0, init_size,
213*490215a3Smrg ZX_VM_FLAG_CAN_MAP_READ | ZX_VM_FLAG_CAN_MAP_WRITE |
214*490215a3Smrg ZX_VM_FLAG_CAN_MAP_SPECIFIC,
215*490215a3Smrg &vmar, &base);
216*490215a3Smrg if (status != ZX_OK)
217*490215a3Smrg ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status);
218*490215a3Smrg base_ = reinterpret_cast<void *>(base);
219*490215a3Smrg size_ = init_size;
220*490215a3Smrg name_ = name;
221*490215a3Smrg os_handle_ = vmar;
222*490215a3Smrg
223*490215a3Smrg return reinterpret_cast<uptr>(base_);
224*490215a3Smrg }
225*490215a3Smrg
DoMmapFixedOrDie(zx_handle_t vmar,uptr fixed_addr,uptr map_size,void * base,const char * name,bool die_for_nomem)226*490215a3Smrg static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size,
227*490215a3Smrg void *base, const char *name, bool die_for_nomem) {
228*490215a3Smrg uptr offset = fixed_addr - reinterpret_cast<uptr>(base);
229*490215a3Smrg map_size = RoundUpTo(map_size, PAGE_SIZE);
230*490215a3Smrg zx_handle_t vmo;
231*490215a3Smrg zx_status_t status = _zx_vmo_create(map_size, 0, &vmo);
232*490215a3Smrg if (status != ZX_OK) {
233*490215a3Smrg if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
234*490215a3Smrg ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status);
235*490215a3Smrg return 0;
236*490215a3Smrg }
237*490215a3Smrg _zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name));
238*490215a3Smrg DCHECK_GE(base + size_, map_size + offset);
239*490215a3Smrg uintptr_t addr;
240*490215a3Smrg
241*490215a3Smrg status =
242*490215a3Smrg _zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC,
243*490215a3Smrg offset, vmo, 0, map_size, &addr);
244*490215a3Smrg _zx_handle_close(vmo);
245*490215a3Smrg if (status != ZX_OK) {
246*490215a3Smrg if (status != ZX_ERR_NO_MEMORY || die_for_nomem) {
247*490215a3Smrg ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status);
248*490215a3Smrg }
249*490215a3Smrg return 0;
250*490215a3Smrg }
251*490215a3Smrg IncreaseTotalMmap(map_size);
252*490215a3Smrg return addr;
253*490215a3Smrg }
254*490215a3Smrg
Map(uptr fixed_addr,uptr map_size)255*490215a3Smrg uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size) {
256*490215a3Smrg return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
257*490215a3Smrg name_, false);
258*490215a3Smrg }
259*490215a3Smrg
MapOrDie(uptr fixed_addr,uptr map_size)260*490215a3Smrg uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size) {
261*490215a3Smrg return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
262*490215a3Smrg name_, true);
263*490215a3Smrg }
264*490215a3Smrg
UnmapOrDieVmar(void * addr,uptr size,zx_handle_t target_vmar)265*490215a3Smrg void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar) {
266*490215a3Smrg if (!addr || !size) return;
267*490215a3Smrg size = RoundUpTo(size, PAGE_SIZE);
268*490215a3Smrg
269*490215a3Smrg zx_status_t status =
270*490215a3Smrg _zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size);
271*490215a3Smrg if (status != ZX_OK) {
272*490215a3Smrg Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
273*490215a3Smrg SanitizerToolName, size, size, addr);
274*490215a3Smrg CHECK("unable to unmap" && 0);
275*490215a3Smrg }
276*490215a3Smrg
277*490215a3Smrg DecreaseTotalMmap(size);
278*490215a3Smrg }
279*490215a3Smrg
Unmap(uptr addr,uptr size)280*490215a3Smrg void ReservedAddressRange::Unmap(uptr addr, uptr size) {
281*490215a3Smrg CHECK_LE(size, size_);
282*490215a3Smrg const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_);
283*490215a3Smrg if (addr == reinterpret_cast<uptr>(base_)) {
284*490215a3Smrg if (size == size_) {
285*490215a3Smrg // Destroying the vmar effectively unmaps the whole mapping.
286*490215a3Smrg _zx_vmar_destroy(vmar);
287*490215a3Smrg _zx_handle_close(vmar);
288*490215a3Smrg os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID);
289*490215a3Smrg DecreaseTotalMmap(size);
290*490215a3Smrg return;
291*490215a3Smrg }
292*490215a3Smrg } else {
293*490215a3Smrg CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
294*490215a3Smrg }
295*490215a3Smrg // Partial unmapping does not affect the fact that the initial range is still
296*490215a3Smrg // reserved, and the resulting unmapped memory can't be reused.
297*490215a3Smrg UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar);
298*490215a3Smrg }
299*490215a3Smrg
300*490215a3Smrg // This should never be called.
MmapFixedNoAccess(uptr fixed_addr,uptr size,const char * name)301*490215a3Smrg void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
302*490215a3Smrg UNIMPLEMENTED();
303*490215a3Smrg }
304*490215a3Smrg
MmapAlignedOrDieOnFatalError(uptr size,uptr alignment,const char * mem_type)305*490215a3Smrg void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
306*490215a3Smrg const char *mem_type) {
307*490215a3Smrg CHECK_GE(size, PAGE_SIZE);
308*490215a3Smrg CHECK(IsPowerOfTwo(size));
309*490215a3Smrg CHECK(IsPowerOfTwo(alignment));
310*490215a3Smrg
311*490215a3Smrg zx_handle_t vmo;
312*490215a3Smrg zx_status_t status = _zx_vmo_create(size, 0, &vmo);
313*490215a3Smrg if (status != ZX_OK) {
314*490215a3Smrg if (status != ZX_ERR_NO_MEMORY)
315*490215a3Smrg ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false);
316*490215a3Smrg return nullptr;
317*490215a3Smrg }
318*490215a3Smrg _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
319*490215a3Smrg internal_strlen(mem_type));
320*490215a3Smrg
321*490215a3Smrg // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
322*490215a3Smrg
323*490215a3Smrg // Map a larger size to get a chunk of address space big enough that
324*490215a3Smrg // it surely contains an aligned region of the requested size. Then
325*490215a3Smrg // overwrite the aligned middle portion with a mapping from the
326*490215a3Smrg // beginning of the VMO, and unmap the excess before and after.
327*490215a3Smrg size_t map_size = size + alignment;
328*490215a3Smrg uintptr_t addr;
329*490215a3Smrg status =
330*490215a3Smrg _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
331*490215a3Smrg vmo, 0, map_size, &addr);
332*490215a3Smrg if (status == ZX_OK) {
333*490215a3Smrg uintptr_t map_addr = addr;
334*490215a3Smrg uintptr_t map_end = map_addr + map_size;
335*490215a3Smrg addr = RoundUpTo(map_addr, alignment);
336*490215a3Smrg uintptr_t end = addr + size;
337*490215a3Smrg if (addr != map_addr) {
338*490215a3Smrg zx_info_vmar_t info;
339*490215a3Smrg status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info,
340*490215a3Smrg sizeof(info), NULL, NULL);
341*490215a3Smrg if (status == ZX_OK) {
342*490215a3Smrg uintptr_t new_addr;
343*490215a3Smrg status = _zx_vmar_map(
344*490215a3Smrg _zx_vmar_root_self(),
345*490215a3Smrg ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE,
346*490215a3Smrg addr - info.base, vmo, 0, size, &new_addr);
347*490215a3Smrg if (status == ZX_OK) CHECK_EQ(new_addr, addr);
348*490215a3Smrg }
349*490215a3Smrg }
350*490215a3Smrg if (status == ZX_OK && addr != map_addr)
351*490215a3Smrg status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr);
352*490215a3Smrg if (status == ZX_OK && end != map_end)
353*490215a3Smrg status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end);
354*490215a3Smrg }
355*490215a3Smrg _zx_handle_close(vmo);
356*490215a3Smrg
357*490215a3Smrg if (status != ZX_OK) {
358*490215a3Smrg if (status != ZX_ERR_NO_MEMORY)
359*490215a3Smrg ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false);
360*490215a3Smrg return nullptr;
361*490215a3Smrg }
362*490215a3Smrg
363*490215a3Smrg IncreaseTotalMmap(size);
364*490215a3Smrg
365*490215a3Smrg return reinterpret_cast<void *>(addr);
366*490215a3Smrg }
367*490215a3Smrg
UnmapOrDie(void * addr,uptr size)368*490215a3Smrg void UnmapOrDie(void *addr, uptr size) {
369*490215a3Smrg UnmapOrDieVmar(addr, size, _zx_vmar_root_self());
370*490215a3Smrg }
371*490215a3Smrg
372*490215a3Smrg // This is used on the shadow mapping, which cannot be changed.
373*490215a3Smrg // Zircon doesn't have anything like MADV_DONTNEED.
ReleaseMemoryPagesToOS(uptr beg,uptr end)374*490215a3Smrg void ReleaseMemoryPagesToOS(uptr beg, uptr end) {}
375*490215a3Smrg
DumpProcessMap()376*490215a3Smrg void DumpProcessMap() {
377*490215a3Smrg // TODO(mcgrathr): write it
378*490215a3Smrg return;
379*490215a3Smrg }
380*490215a3Smrg
IsAccessibleMemoryRange(uptr beg,uptr size)381*490215a3Smrg bool IsAccessibleMemoryRange(uptr beg, uptr size) {
382*490215a3Smrg // TODO(mcgrathr): Figure out a better way.
383*490215a3Smrg zx_handle_t vmo;
384*490215a3Smrg zx_status_t status = _zx_vmo_create(size, 0, &vmo);
385*490215a3Smrg if (status == ZX_OK) {
386*490215a3Smrg status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size);
387*490215a3Smrg _zx_handle_close(vmo);
388*490215a3Smrg }
389*490215a3Smrg return status == ZX_OK;
390*490215a3Smrg }
391*490215a3Smrg
392*490215a3Smrg // FIXME implement on this platform.
GetMemoryProfile(fill_profile_f cb,uptr * stats,uptr stats_size)393*490215a3Smrg void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {}
394*490215a3Smrg
ReadFileToBuffer(const char * file_name,char ** buff,uptr * buff_size,uptr * read_len,uptr max_len,error_t * errno_p)395*490215a3Smrg bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
396*490215a3Smrg uptr *read_len, uptr max_len, error_t *errno_p) {
397*490215a3Smrg zx_handle_t vmo;
398*490215a3Smrg zx_status_t status = __sanitizer_get_configuration(file_name, &vmo);
399*490215a3Smrg if (status == ZX_OK) {
400*490215a3Smrg uint64_t vmo_size;
401*490215a3Smrg status = _zx_vmo_get_size(vmo, &vmo_size);
402*490215a3Smrg if (status == ZX_OK) {
403*490215a3Smrg if (vmo_size < max_len) max_len = vmo_size;
404*490215a3Smrg size_t map_size = RoundUpTo(max_len, PAGE_SIZE);
405*490215a3Smrg uintptr_t addr;
406*490215a3Smrg status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0,
407*490215a3Smrg map_size, &addr);
408*490215a3Smrg if (status == ZX_OK) {
409*490215a3Smrg *buff = reinterpret_cast<char *>(addr);
410*490215a3Smrg *buff_size = map_size;
411*490215a3Smrg *read_len = max_len;
412*490215a3Smrg }
413*490215a3Smrg }
414*490215a3Smrg _zx_handle_close(vmo);
415*490215a3Smrg }
416*490215a3Smrg if (status != ZX_OK && errno_p) *errno_p = status;
417*490215a3Smrg return status == ZX_OK;
418*490215a3Smrg }
419*490215a3Smrg
RawWrite(const char * buffer)420*490215a3Smrg void RawWrite(const char *buffer) {
421*490215a3Smrg constexpr size_t size = 128;
422*490215a3Smrg static _Thread_local char line[size];
423*490215a3Smrg static _Thread_local size_t lastLineEnd = 0;
424*490215a3Smrg static _Thread_local size_t cur = 0;
425*490215a3Smrg
426*490215a3Smrg while (*buffer) {
427*490215a3Smrg if (cur >= size) {
428*490215a3Smrg if (lastLineEnd == 0)
429*490215a3Smrg lastLineEnd = size;
430*490215a3Smrg __sanitizer_log_write(line, lastLineEnd);
431*490215a3Smrg internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
432*490215a3Smrg cur = cur - lastLineEnd;
433*490215a3Smrg lastLineEnd = 0;
434*490215a3Smrg }
435*490215a3Smrg if (*buffer == '\n')
436*490215a3Smrg lastLineEnd = cur + 1;
437*490215a3Smrg line[cur++] = *buffer++;
438*490215a3Smrg }
439*490215a3Smrg // Flush all complete lines before returning.
440*490215a3Smrg if (lastLineEnd != 0) {
441*490215a3Smrg __sanitizer_log_write(line, lastLineEnd);
442*490215a3Smrg internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
443*490215a3Smrg cur = cur - lastLineEnd;
444*490215a3Smrg lastLineEnd = 0;
445*490215a3Smrg }
446*490215a3Smrg }
447*490215a3Smrg
CatastrophicErrorWrite(const char * buffer,uptr length)448*490215a3Smrg void CatastrophicErrorWrite(const char *buffer, uptr length) {
449*490215a3Smrg __sanitizer_log_write(buffer, length);
450*490215a3Smrg }
451*490215a3Smrg
452*490215a3Smrg char **StoredArgv;
453*490215a3Smrg char **StoredEnviron;
454*490215a3Smrg
GetArgv()455*490215a3Smrg char **GetArgv() { return StoredArgv; }
456*490215a3Smrg
GetEnv(const char * name)457*490215a3Smrg const char *GetEnv(const char *name) {
458*490215a3Smrg if (StoredEnviron) {
459*490215a3Smrg uptr NameLen = internal_strlen(name);
460*490215a3Smrg for (char **Env = StoredEnviron; *Env != 0; Env++) {
461*490215a3Smrg if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
462*490215a3Smrg return (*Env) + NameLen + 1;
463*490215a3Smrg }
464*490215a3Smrg }
465*490215a3Smrg return nullptr;
466*490215a3Smrg }
467*490215a3Smrg
ReadBinaryName(char * buf,uptr buf_len)468*490215a3Smrg uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
469*490215a3Smrg const char *argv0 = "<UNKNOWN>";
470*490215a3Smrg if (StoredArgv && StoredArgv[0]) {
471*490215a3Smrg argv0 = StoredArgv[0];
472*490215a3Smrg }
473*490215a3Smrg internal_strncpy(buf, argv0, buf_len);
474*490215a3Smrg return internal_strlen(buf);
475*490215a3Smrg }
476*490215a3Smrg
ReadLongProcessName(char * buf,uptr buf_len)477*490215a3Smrg uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
478*490215a3Smrg return ReadBinaryName(buf, buf_len);
479*490215a3Smrg }
480*490215a3Smrg
481*490215a3Smrg uptr MainThreadStackBase, MainThreadStackSize;
482*490215a3Smrg
GetRandom(void * buffer,uptr length,bool blocking)483*490215a3Smrg bool GetRandom(void *buffer, uptr length, bool blocking) {
484*490215a3Smrg CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN);
485*490215a3Smrg _zx_cprng_draw(buffer, length);
486*490215a3Smrg return true;
487*490215a3Smrg }
488*490215a3Smrg
GetNumberOfCPUs()489*490215a3Smrg u32 GetNumberOfCPUs() {
490*490215a3Smrg return zx_system_get_num_cpus();
491*490215a3Smrg }
492*490215a3Smrg
GetRSS()493*490215a3Smrg uptr GetRSS() { UNIMPLEMENTED(); }
494*490215a3Smrg
495*490215a3Smrg } // namespace __sanitizer
496*490215a3Smrg
497*490215a3Smrg using namespace __sanitizer; // NOLINT
498*490215a3Smrg
499*490215a3Smrg extern "C" {
__sanitizer_startup_hook(int argc,char ** argv,char ** envp,void * stack_base,size_t stack_size)500*490215a3Smrg void __sanitizer_startup_hook(int argc, char **argv, char **envp,
501*490215a3Smrg void *stack_base, size_t stack_size) {
502*490215a3Smrg __sanitizer::StoredArgv = argv;
503*490215a3Smrg __sanitizer::StoredEnviron = envp;
504*490215a3Smrg __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);
505*490215a3Smrg __sanitizer::MainThreadStackSize = stack_size;
506*490215a3Smrg }
507*490215a3Smrg
__sanitizer_set_report_path(const char * path)508*490215a3Smrg void __sanitizer_set_report_path(const char *path) {
509*490215a3Smrg // Handle the initialization code in each sanitizer, but no other calls.
510*490215a3Smrg // This setting is never consulted on Fuchsia.
511*490215a3Smrg DCHECK_EQ(path, common_flags()->log_path);
512*490215a3Smrg }
513*490215a3Smrg
__sanitizer_set_report_fd(void * fd)514*490215a3Smrg void __sanitizer_set_report_fd(void *fd) {
515*490215a3Smrg UNREACHABLE("not available on Fuchsia");
516*490215a3Smrg }
517*490215a3Smrg } // extern "C"
518*490215a3Smrg
519*490215a3Smrg #endif // SANITIZER_FUCHSIA
520