1 //===-- sanitizer_fuchsia.cc ----------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file is shared between AddressSanitizer and other sanitizer
10 // run-time libraries and implements Fuchsia-specific functions from
11 // sanitizer_common.h.
12 //===----------------------------------------------------------------------===//
14 #include "sanitizer_fuchsia.h"
17 #include "sanitizer_common.h"
18 #include "sanitizer_libc.h"
19 #include "sanitizer_mutex.h"
25 #include <zircon/errors.h>
26 #include <zircon/process.h>
27 #include <zircon/syscalls.h>
29 namespace __sanitizer {
31 void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); }
33 uptr internal_sched_yield() {
34 zx_status_t status = _zx_nanosleep(0);
35 CHECK_EQ(status, ZX_OK);
36 return 0; // Why doesn't this return void?
39 static void internal_nanosleep(zx_time_t ns) {
40 zx_status_t status = _zx_nanosleep(_zx_deadline_after(ns));
41 CHECK_EQ(status, ZX_OK);
44 unsigned int internal_sleep(unsigned int seconds) {
45 internal_nanosleep(ZX_SEC(seconds));
51 zx_status_t status = _zx_clock_get(ZX_CLOCK_UTC, &time);
52 CHECK_EQ(status, ZX_OK);
56 u64 MonotonicNanoTime() { return _zx_clock_get_monotonic(); }
58 uptr internal_getpid() {
59 zx_info_handle_basic_t info;
61 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info,
62 sizeof(info), NULL, NULL);
63 CHECK_EQ(status, ZX_OK);
64 uptr pid = static_cast<uptr>(info.koid);
65 CHECK_EQ(pid, info.koid);
69 uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }
71 tid_t GetTid() { return GetThreadSelf(); }
73 void Abort() { abort(); }
75 int Atexit(void (*function)(void)) { return atexit(function); }
77 void SleepForSeconds(int seconds) { internal_sleep(seconds); }
79 void SleepForMillis(int millis) { internal_nanosleep(ZX_MSEC(millis)); }
81 void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {
83 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);
86 CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0);
87 CHECK_EQ(pthread_attr_destroy(&attr), 0);
89 *stack_bottom = reinterpret_cast<uptr>(base);
90 *stack_top = *stack_bottom + size;
93 void InitializePlatformEarly() {}
96 void CheckMPROTECT() {}
97 void PlatformPrepareForSandboxing(__sanitizer_sandbox_arguments *args) {}
98 void DisableCoreDumperIfNecessary() {}
99 void InstallDeadlySignalHandlers(SignalHandlerType handler) {}
100 void SetAlternateSignalStack() {}
101 void UnsetAlternateSignalStack() {}
102 void InitTlsSize() {}
104 void PrintModuleMap() {}
106 bool SignalContext::IsStackOverflow() const { return false; }
107 void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }
108 const char *SignalContext::Describe() const { UNIMPLEMENTED(); }
110 enum MutexState : int { MtxUnlocked = 0, MtxLocked = 1, MtxSleeping = 2 };
112 BlockingMutex::BlockingMutex() {
113 // NOTE! It's important that this use internal_memset, because plain
114 // memset might be intercepted (e.g., actually be __asan_memset).
115 // Defining this so the compiler initializes each field, e.g.:
116 // BlockingMutex::BlockingMutex() : BlockingMutex(LINKER_INITIALIZED) {}
117 // might result in the compiler generating a call to memset, which would
118 // have the same problem.
119 internal_memset(this, 0, sizeof(*this));
122 void BlockingMutex::Lock() {
124 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
125 if (atomic_exchange(m, MtxLocked, memory_order_acquire) == MtxUnlocked)
127 while (atomic_exchange(m, MtxSleeping, memory_order_acquire) != MtxUnlocked) {
129 _zx_futex_wait(reinterpret_cast<zx_futex_t *>(m), MtxSleeping,
130 ZX_HANDLE_INVALID, ZX_TIME_INFINITE);
131 if (status != ZX_ERR_BAD_STATE) // Normal race.
132 CHECK_EQ(status, ZX_OK);
136 void BlockingMutex::Unlock() {
137 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
138 u32 v = atomic_exchange(m, MtxUnlocked, memory_order_release);
139 CHECK_NE(v, MtxUnlocked);
140 if (v == MtxSleeping) {
141 zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(m), 1);
142 CHECK_EQ(status, ZX_OK);
146 void BlockingMutex::CheckLocked() {
147 atomic_uint32_t *m = reinterpret_cast<atomic_uint32_t *>(&opaque_storage_);
148 CHECK_NE(MtxUnlocked, atomic_load(m, memory_order_relaxed));
151 uptr GetPageSize() { return PAGE_SIZE; }
153 uptr GetMmapGranularity() { return PAGE_SIZE; }
155 sanitizer_shadow_bounds_t ShadowBounds;
157 uptr GetMaxUserVirtualAddress() {
158 ShadowBounds = __sanitizer_shadow_bounds();
159 return ShadowBounds.memory_limit - 1;
162 uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); }
164 static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type,
165 bool raw_report, bool die_for_nomem) {
166 size = RoundUpTo(size, PAGE_SIZE);
169 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
170 if (status != ZX_OK) {
171 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
172 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status,
176 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
177 internal_strlen(mem_type));
179 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
182 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
183 vmo, 0, size, &addr);
184 _zx_handle_close(vmo);
186 if (status != ZX_OK) {
187 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
188 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status,
193 IncreaseTotalMmap(size);
195 return reinterpret_cast<void *>(addr);
198 void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {
199 return DoAnonymousMmapOrDie(size, mem_type, raw_report, true);
202 void *MmapNoReserveOrDie(uptr size, const char *mem_type) {
203 return MmapOrDie(size, mem_type);
206 void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {
207 return DoAnonymousMmapOrDie(size, mem_type, false, false);
210 uptr ReservedAddressRange::Init(uptr init_size, const char *name,
212 init_size = RoundUpTo(init_size, PAGE_SIZE);
213 DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID);
218 _zx_vmar_root_self(),
219 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC,
220 0, init_size, &vmar, &base);
222 ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status);
223 base_ = reinterpret_cast<void *>(base);
228 return reinterpret_cast<uptr>(base_);
231 static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size,
232 void *base, const char *name, bool die_for_nomem) {
233 uptr offset = fixed_addr - reinterpret_cast<uptr>(base);
234 map_size = RoundUpTo(map_size, PAGE_SIZE);
236 zx_status_t status = _zx_vmo_create(map_size, 0, &vmo);
237 if (status != ZX_OK) {
238 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)
239 ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status);
242 _zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name));
243 DCHECK_GE(base + size_, map_size + offset);
247 _zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC,
248 offset, vmo, 0, map_size, &addr);
249 _zx_handle_close(vmo);
250 if (status != ZX_OK) {
251 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) {
252 ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status);
256 IncreaseTotalMmap(map_size);
260 uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size,
262 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
266 uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size,
268 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,
272 void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar) {
273 if (!addr || !size) return;
274 size = RoundUpTo(size, PAGE_SIZE);
277 _zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size);
278 if (status != ZX_OK) {
279 Report("ERROR: %s failed to deallocate 0x%zx (%zd) bytes at address %p\n",
280 SanitizerToolName, size, size, addr);
281 CHECK("unable to unmap" && 0);
284 DecreaseTotalMmap(size);
287 void ReservedAddressRange::Unmap(uptr addr, uptr size) {
288 CHECK_LE(size, size_);
289 const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_);
290 if (addr == reinterpret_cast<uptr>(base_)) {
292 // Destroying the vmar effectively unmaps the whole mapping.
293 _zx_vmar_destroy(vmar);
294 _zx_handle_close(vmar);
295 os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID);
296 DecreaseTotalMmap(size);
300 CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);
302 // Partial unmapping does not affect the fact that the initial range is still
303 // reserved, and the resulting unmapped memory can't be reused.
304 UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar);
307 // This should never be called.
308 void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {
312 void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,
313 const char *mem_type) {
314 CHECK_GE(size, PAGE_SIZE);
315 CHECK(IsPowerOfTwo(size));
316 CHECK(IsPowerOfTwo(alignment));
319 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
320 if (status != ZX_OK) {
321 if (status != ZX_ERR_NO_MEMORY)
322 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false);
325 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,
326 internal_strlen(mem_type));
328 // TODO(mcgrathr): Maybe allocate a VMAR for all sanitizer heap and use that?
330 // Map a larger size to get a chunk of address space big enough that
331 // it surely contains an aligned region of the requested size. Then
332 // overwrite the aligned middle portion with a mapping from the
333 // beginning of the VMO, and unmap the excess before and after.
334 size_t map_size = size + alignment;
337 _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, 0,
338 vmo, 0, map_size, &addr);
339 if (status == ZX_OK) {
340 uintptr_t map_addr = addr;
341 uintptr_t map_end = map_addr + map_size;
342 addr = RoundUpTo(map_addr, alignment);
343 uintptr_t end = addr + size;
344 if (addr != map_addr) {
346 status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR, &info,
347 sizeof(info), NULL, NULL);
348 if (status == ZX_OK) {
350 status = _zx_vmar_map(
351 _zx_vmar_root_self(),
352 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE,
353 addr - info.base, vmo, 0, size, &new_addr);
354 if (status == ZX_OK) CHECK_EQ(new_addr, addr);
357 if (status == ZX_OK && addr != map_addr)
358 status = _zx_vmar_unmap(_zx_vmar_root_self(), map_addr, addr - map_addr);
359 if (status == ZX_OK && end != map_end)
360 status = _zx_vmar_unmap(_zx_vmar_root_self(), end, map_end - end);
362 _zx_handle_close(vmo);
364 if (status != ZX_OK) {
365 if (status != ZX_ERR_NO_MEMORY)
366 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false);
370 IncreaseTotalMmap(size);
372 return reinterpret_cast<void *>(addr);
375 void UnmapOrDie(void *addr, uptr size) {
376 UnmapOrDieVmar(addr, size, _zx_vmar_root_self());
379 // This is used on the shadow mapping, which cannot be changed.
380 // Zircon doesn't have anything like MADV_DONTNEED.
381 void ReleaseMemoryPagesToOS(uptr beg, uptr end) {}
383 void DumpProcessMap() {
384 // TODO(mcgrathr): write it
388 bool IsAccessibleMemoryRange(uptr beg, uptr size) {
389 // TODO(mcgrathr): Figure out a better way.
391 zx_status_t status = _zx_vmo_create(size, 0, &vmo);
392 if (status == ZX_OK) {
393 status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size);
394 _zx_handle_close(vmo);
396 return status == ZX_OK;
399 // FIXME implement on this platform.
400 void GetMemoryProfile(fill_profile_f cb, uptr *stats, uptr stats_size) {}
402 bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,
403 uptr *read_len, uptr max_len, error_t *errno_p) {
405 zx_status_t status = __sanitizer_get_configuration(file_name, &vmo);
406 if (status == ZX_OK) {
408 status = _zx_vmo_get_size(vmo, &vmo_size);
409 if (status == ZX_OK) {
410 if (vmo_size < max_len) max_len = vmo_size;
411 size_t map_size = RoundUpTo(max_len, PAGE_SIZE);
413 status = _zx_vmar_map(_zx_vmar_root_self(), ZX_VM_PERM_READ, 0, vmo, 0,
415 if (status == ZX_OK) {
416 *buff = reinterpret_cast<char *>(addr);
417 *buff_size = map_size;
421 _zx_handle_close(vmo);
423 if (status != ZX_OK && errno_p) *errno_p = status;
424 return status == ZX_OK;
427 void RawWrite(const char *buffer) {
428 constexpr size_t size = 128;
429 static _Thread_local char line[size];
430 static _Thread_local size_t lastLineEnd = 0;
431 static _Thread_local size_t cur = 0;
435 if (lastLineEnd == 0)
437 __sanitizer_log_write(line, lastLineEnd);
438 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
439 cur = cur - lastLineEnd;
443 lastLineEnd = cur + 1;
444 line[cur++] = *buffer++;
446 // Flush all complete lines before returning.
447 if (lastLineEnd != 0) {
448 __sanitizer_log_write(line, lastLineEnd);
449 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);
450 cur = cur - lastLineEnd;
455 void CatastrophicErrorWrite(const char *buffer, uptr length) {
456 __sanitizer_log_write(buffer, length);
460 char **StoredEnviron;
462 char **GetArgv() { return StoredArgv; }
463 char **GetEnviron() { return StoredEnviron; }
465 const char *GetEnv(const char *name) {
467 uptr NameLen = internal_strlen(name);
468 for (char **Env = StoredEnviron; *Env != 0; Env++) {
469 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')
470 return (*Env) + NameLen + 1;
476 uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {
477 const char *argv0 = "<UNKNOWN>";
478 if (StoredArgv && StoredArgv[0]) {
479 argv0 = StoredArgv[0];
481 internal_strncpy(buf, argv0, buf_len);
482 return internal_strlen(buf);
485 uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {
486 return ReadBinaryName(buf, buf_len);
489 uptr MainThreadStackBase, MainThreadStackSize;
491 bool GetRandom(void *buffer, uptr length, bool blocking) {
492 CHECK_LE(length, ZX_CPRNG_DRAW_MAX_LEN);
493 _zx_cprng_draw(buffer, length);
497 u32 GetNumberOfCPUs() {
498 return zx_system_get_num_cpus();
501 uptr GetRSS() { UNIMPLEMENTED(); }
503 } // namespace __sanitizer
505 using namespace __sanitizer; // NOLINT
508 void __sanitizer_startup_hook(int argc, char **argv, char **envp,
509 void *stack_base, size_t stack_size) {
510 __sanitizer::StoredArgv = argv;
511 __sanitizer::StoredEnviron = envp;
512 __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);
513 __sanitizer::MainThreadStackSize = stack_size;
516 void __sanitizer_set_report_path(const char *path) {
517 // Handle the initialization code in each sanitizer, but no other calls.
518 // This setting is never consulted on Fuchsia.
519 DCHECK_EQ(path, common_flags()->log_path);
522 void __sanitizer_set_report_fd(void *fd) {
523 UNREACHABLE("not available on Fuchsia");
527 #endif // SANITIZER_FUCHSIA