597 lines · cpp
1//===-- sanitizer_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-exception6//7//===----------------------------------------------------------------------===//8//9// This file is shared between AddressSanitizer and other sanitizer10// run-time libraries and implements Fuchsia-specific functions from11// sanitizer_common.h.12//===----------------------------------------------------------------------===//13 14#include "sanitizer_fuchsia.h"15#if SANITIZER_FUCHSIA16 17# include <limits.h>18# include <pthread.h>19# include <stdlib.h>20# include <unistd.h>21# include <zircon/errors.h>22# include <zircon/process.h>23# include <zircon/syscalls.h>24# include <zircon/utc.h>25 26# include "sanitizer_common.h"27# include "sanitizer_interface_internal.h"28# include "sanitizer_libc.h"29# include "sanitizer_mutex.h"30 31namespace __sanitizer {32 33void NORETURN internal__exit(int exitcode) { _zx_process_exit(exitcode); }34 35uptr internal_sched_yield() {36 zx_status_t status = _zx_thread_legacy_yield(0u);37 CHECK_EQ(status, ZX_OK);38 return 0; // Why doesn't this return void?39}40 41void internal_usleep(u64 useconds) {42 zx_status_t status = _zx_nanosleep(_zx_deadline_after(ZX_USEC(useconds)));43 CHECK_EQ(status, ZX_OK);44}45 46u64 NanoTime() {47 zx_handle_t utc_clock = _zx_utc_reference_get();48 CHECK_NE(utc_clock, ZX_HANDLE_INVALID);49 zx_time_t time;50 zx_status_t status = _zx_clock_read(utc_clock, &time);51 CHECK_EQ(status, ZX_OK);52 return time;53}54 55u64 MonotonicNanoTime() { return _zx_clock_get_monotonic(); }56 57uptr internal_getpid() {58 zx_info_handle_basic_t info;59 zx_status_t status =60 _zx_object_get_info(_zx_process_self(), ZX_INFO_HANDLE_BASIC, &info,61 sizeof(info), NULL, NULL);62 CHECK_EQ(status, ZX_OK);63 uptr pid = static_cast<uptr>(info.koid);64 CHECK_EQ(pid, info.koid);65 return pid;66}67 68int internal_dlinfo(void *handle, int request, void *p) { UNIMPLEMENTED(); }69 70uptr GetThreadSelf() { return reinterpret_cast<uptr>(thrd_current()); }71 72ThreadID GetTid() { return GetThreadSelf(); }73 74void Abort() { abort(); }75 76int Atexit(void (*function)(void)) { return atexit(function); }77 78void GetThreadStackTopAndBottom(bool, uptr *stack_top, uptr *stack_bottom) {79 pthread_attr_t attr;80 CHECK_EQ(pthread_getattr_np(pthread_self(), &attr), 0);81 void *base;82 size_t size;83 CHECK_EQ(pthread_attr_getstack(&attr, &base, &size), 0);84 CHECK_EQ(pthread_attr_destroy(&attr), 0);85 86 *stack_bottom = reinterpret_cast<uptr>(base);87 *stack_top = *stack_bottom + size;88}89 90void InitializePlatformEarly() {}91void CheckASLR() {}92void CheckMPROTECT() {}93void PlatformPrepareForSandboxing(void *args) {}94void DisableCoreDumperIfNecessary() {}95void InstallDeadlySignalHandlers(SignalHandlerType handler) {}96void SetAlternateSignalStack() {}97void UnsetAlternateSignalStack() {}98 99bool SignalContext::IsStackOverflow() const { return false; }100void SignalContext::DumpAllRegisters(void *context) { UNIMPLEMENTED(); }101const char *SignalContext::Describe() const { UNIMPLEMENTED(); }102 103void FutexWait(atomic_uint32_t *p, u32 cmp) {104 zx_status_t status = _zx_futex_wait(reinterpret_cast<zx_futex_t *>(p), cmp,105 ZX_HANDLE_INVALID, ZX_TIME_INFINITE);106 if (status != ZX_ERR_BAD_STATE) // Normal race.107 CHECK_EQ(status, ZX_OK);108}109 110void FutexWake(atomic_uint32_t *p, u32 count) {111 zx_status_t status = _zx_futex_wake(reinterpret_cast<zx_futex_t *>(p), count);112 CHECK_EQ(status, ZX_OK);113}114 115uptr GetPageSize() { return _zx_system_get_page_size(); }116 117uptr GetMmapGranularity() { return _zx_system_get_page_size(); }118 119sanitizer_shadow_bounds_t ShadowBounds;120 121// Any sanitizer that utilizes shadow should explicitly call whenever it's122// appropriate for that sanitizer to reference shadow bounds. For ASan, this is123// done in `InitializeShadowMemory` and for HWASan, this is done in124// `InitShadow`.125void InitShadowBounds() { ShadowBounds = __sanitizer_shadow_bounds(); }126 127// TODO(leonardchan): It's not immediately clear from a user perspective if128// `GetMaxUserVirtualAddress` should be called exatly once on runtime startup129// or can be called multiple times. Currently it looks like most instances of130// `GetMaxUserVirtualAddress` are meant to be called once, but if someone131// decides to call this multiple times in the future, we should have a separate132// function that's ok to call multiple times. Ideally we would just invoke this133// syscall once. Also for Fuchsia, this syscall technically gets invoked twice134// since `__sanitizer_shadow_bounds` also invokes this syscall under the hood.135uptr GetMaxUserVirtualAddress() {136 zx_info_vmar_t info;137 zx_status_t status = _zx_object_get_info(_zx_vmar_root_self(), ZX_INFO_VMAR,138 &info, sizeof(info), NULL, NULL);139 CHECK_EQ(status, ZX_OK);140 141 // Find the top of the accessible address space.142 uintptr_t top = info.base + info.len;143 144 // Round it up to a power-of-two size. There may be some pages at145 // the top that can't actually be mapped, but for purposes of the146 // the shadow, we'll pretend they could be.147 int bit = (sizeof(uintptr_t) * CHAR_BIT) - __builtin_clzl(top);148 if (top != (uintptr_t)1 << bit)149 top = (uintptr_t)1 << (bit + 1);150 151 return top - 1;152}153 154uptr GetMaxVirtualAddress() { return GetMaxUserVirtualAddress(); }155 156bool ErrorIsOOM(error_t err) { return err == ZX_ERR_NO_MEMORY; }157 158// For any sanitizer internal that needs to map something which can be unmapped159// later, first attempt to map to a pre-allocated VMAR. This helps reduce160// fragmentation from many small anonymous mmap calls. A good value for this161// VMAR size would be the total size of your typical sanitizer internal objects162// allocated in an "average" process lifetime. Examples of this include:163// FakeStack, LowLevelAllocator mappings, TwoLevelMap, InternalMmapVector,164// StackStore, CreateAsanThread, etc.165//166// This is roughly equal to the total sum of sanitizer internal mappings for a167// large test case.168constexpr size_t kSanitizerHeapVmarSize = 13ULL << 20;169static zx_handle_t gSanitizerHeapVmar = ZX_HANDLE_INVALID;170 171static zx_status_t GetSanitizerHeapVmar(zx_handle_t *vmar) {172 zx_status_t status = ZX_OK;173 if (gSanitizerHeapVmar == ZX_HANDLE_INVALID) {174 CHECK_EQ(kSanitizerHeapVmarSize % GetPageSizeCached(), 0);175 uintptr_t base;176 status = _zx_vmar_allocate(177 _zx_vmar_root_self(),178 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,179 kSanitizerHeapVmarSize, &gSanitizerHeapVmar, &base);180 }181 *vmar = gSanitizerHeapVmar;182 if (status == ZX_OK)183 CHECK_NE(gSanitizerHeapVmar, ZX_HANDLE_INVALID);184 return status;185}186 187static zx_status_t TryVmoMapSanitizerVmar(zx_vm_option_t options,188 size_t vmar_offset, zx_handle_t vmo,189 size_t size, uintptr_t *addr,190 zx_handle_t *vmar_used = nullptr) {191 zx_handle_t vmar;192 zx_status_t status = GetSanitizerHeapVmar(&vmar);193 if (status != ZX_OK)194 return status;195 196 status = _zx_vmar_map(gSanitizerHeapVmar, options, vmar_offset, vmo,197 /*vmo_offset=*/0, size, addr);198 if (vmar_used)199 *vmar_used = gSanitizerHeapVmar;200 if (status == ZX_ERR_NO_RESOURCES || status == ZX_ERR_INVALID_ARGS) {201 // This means there's no space in the heap VMAR, so fallback to the root202 // VMAR.203 status = _zx_vmar_map(_zx_vmar_root_self(), options, vmar_offset, vmo,204 /*vmo_offset=*/0, size, addr);205 if (vmar_used)206 *vmar_used = _zx_vmar_root_self();207 }208 209 return status;210}211 212static void *DoAnonymousMmapOrDie(uptr size, const char *mem_type,213 bool raw_report, bool die_for_nomem) {214 size = RoundUpTo(size, GetPageSize());215 216 zx_handle_t vmo;217 zx_status_t status = _zx_vmo_create(size, 0, &vmo);218 if (status != ZX_OK) {219 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)220 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status,221 raw_report);222 return nullptr;223 }224 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,225 internal_strlen(mem_type));226 227 uintptr_t addr;228 status = TryVmoMapSanitizerVmar(ZX_VM_PERM_READ | ZX_VM_PERM_WRITE,229 /*vmar_offset=*/0, vmo, size, &addr);230 _zx_handle_close(vmo);231 232 if (status != ZX_OK) {233 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)234 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status,235 raw_report);236 return nullptr;237 }238 239 IncreaseTotalMmap(size);240 241 return reinterpret_cast<void *>(addr);242}243 244void *MmapOrDie(uptr size, const char *mem_type, bool raw_report) {245 return DoAnonymousMmapOrDie(size, mem_type, raw_report, true);246}247 248void *MmapNoReserveOrDie(uptr size, const char *mem_type) {249 return MmapOrDie(size, mem_type);250}251 252void *MmapOrDieOnFatalError(uptr size, const char *mem_type) {253 return DoAnonymousMmapOrDie(size, mem_type, false, false);254}255 256uptr ReservedAddressRange::Init(uptr init_size, const char *name,257 uptr fixed_addr) {258 init_size = RoundUpTo(init_size, GetPageSize());259 DCHECK_EQ(os_handle_, ZX_HANDLE_INVALID);260 uintptr_t base;261 zx_handle_t vmar;262 zx_status_t status = _zx_vmar_allocate(263 _zx_vmar_root_self(),264 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,265 init_size, &vmar, &base);266 if (status != ZX_OK)267 ReportMmapFailureAndDie(init_size, name, "zx_vmar_allocate", status);268 base_ = reinterpret_cast<void *>(base);269 size_ = init_size;270 name_ = name;271 os_handle_ = vmar;272 273 return reinterpret_cast<uptr>(base_);274}275 276static uptr DoMmapFixedOrDie(zx_handle_t vmar, uptr fixed_addr, uptr map_size,277 void *base, const char *name, bool die_for_nomem) {278 uptr offset = fixed_addr - reinterpret_cast<uptr>(base);279 map_size = RoundUpTo(map_size, GetPageSize());280 zx_handle_t vmo;281 zx_status_t status = _zx_vmo_create(map_size, 0, &vmo);282 if (status != ZX_OK) {283 if (status != ZX_ERR_NO_MEMORY || die_for_nomem)284 ReportMmapFailureAndDie(map_size, name, "zx_vmo_create", status);285 return 0;286 }287 _zx_object_set_property(vmo, ZX_PROP_NAME, name, internal_strlen(name));288 DCHECK_GE(base + size_, map_size + offset);289 uintptr_t addr;290 291 status =292 _zx_vmar_map(vmar, ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC,293 offset, vmo, 0, map_size, &addr);294 _zx_handle_close(vmo);295 if (status != ZX_OK) {296 if (status != ZX_ERR_NO_MEMORY || die_for_nomem) {297 ReportMmapFailureAndDie(map_size, name, "zx_vmar_map", status);298 }299 return 0;300 }301 IncreaseTotalMmap(map_size);302 return addr;303}304 305uptr ReservedAddressRange::Map(uptr fixed_addr, uptr map_size,306 const char *name) {307 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,308 name ? name : name_, false);309}310 311uptr ReservedAddressRange::MapOrDie(uptr fixed_addr, uptr map_size,312 const char *name) {313 return DoMmapFixedOrDie(os_handle_, fixed_addr, map_size, base_,314 name ? name : name_, true);315}316 317void UnmapOrDieVmar(void *addr, uptr size, zx_handle_t target_vmar,318 bool raw_report) {319 if (!addr || !size)320 return;321 size = RoundUpTo(size, GetPageSize());322 323 zx_status_t status =324 _zx_vmar_unmap(target_vmar, reinterpret_cast<uintptr_t>(addr), size);325 if (status == ZX_ERR_INVALID_ARGS && target_vmar == gSanitizerHeapVmar) {326 // If there wasn't any space in the heap vmar, the fallback was the root327 // vmar.328 status = _zx_vmar_unmap(_zx_vmar_root_self(),329 reinterpret_cast<uintptr_t>(addr), size);330 }331 if (status != ZX_OK)332 ReportMunmapFailureAndDie(addr, size, status, raw_report);333 334 DecreaseTotalMmap(size);335}336 337void ReservedAddressRange::Unmap(uptr addr, uptr size) {338 CHECK_LE(size, size_);339 const zx_handle_t vmar = static_cast<zx_handle_t>(os_handle_);340 if (addr == reinterpret_cast<uptr>(base_)) {341 if (size == size_) {342 // Destroying the vmar effectively unmaps the whole mapping.343 _zx_vmar_destroy(vmar);344 _zx_handle_close(vmar);345 os_handle_ = static_cast<uptr>(ZX_HANDLE_INVALID);346 DecreaseTotalMmap(size);347 return;348 }349 } else {350 CHECK_EQ(addr + size, reinterpret_cast<uptr>(base_) + size_);351 }352 // Partial unmapping does not affect the fact that the initial range is still353 // reserved, and the resulting unmapped memory can't be reused.354 UnmapOrDieVmar(reinterpret_cast<void *>(addr), size, vmar,355 /*raw_report=*/false);356}357 358// This should never be called.359void *MmapFixedNoAccess(uptr fixed_addr, uptr size, const char *name) {360 UNIMPLEMENTED();361}362 363bool MprotectNoAccess(uptr addr, uptr size) {364 return _zx_vmar_protect(_zx_vmar_root_self(), 0, addr, size) == ZX_OK;365}366 367bool MprotectReadOnly(uptr addr, uptr size) {368 return _zx_vmar_protect(_zx_vmar_root_self(), ZX_VM_PERM_READ, addr, size) ==369 ZX_OK;370}371 372bool MprotectReadWrite(uptr addr, uptr size) {373 return _zx_vmar_protect(_zx_vmar_root_self(),374 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE, addr,375 size) == ZX_OK;376}377 378void *MmapAlignedOrDieOnFatalError(uptr size, uptr alignment,379 const char *mem_type) {380 CHECK_GE(size, GetPageSize());381 CHECK(IsPowerOfTwo(size));382 CHECK(IsPowerOfTwo(alignment));383 384 zx_handle_t vmo;385 zx_status_t status = _zx_vmo_create(size, 0, &vmo);386 if (status != ZX_OK) {387 if (status != ZX_ERR_NO_MEMORY)388 ReportMmapFailureAndDie(size, mem_type, "zx_vmo_create", status, false);389 return nullptr;390 }391 _zx_object_set_property(vmo, ZX_PROP_NAME, mem_type,392 internal_strlen(mem_type));393 394 // Map a larger size to get a chunk of address space big enough that395 // it surely contains an aligned region of the requested size. Then396 // overwrite the aligned middle portion with a mapping from the397 // beginning of the VMO, and unmap the excess before and after.398 size_t map_size = size + alignment;399 uintptr_t addr;400 zx_handle_t vmar_used;401 status = TryVmoMapSanitizerVmar(ZX_VM_PERM_READ | ZX_VM_PERM_WRITE,402 /*vmar_offset=*/0, vmo, map_size, &addr,403 &vmar_used);404 if (status == ZX_OK) {405 uintptr_t map_addr = addr;406 uintptr_t map_end = map_addr + map_size;407 addr = RoundUpTo(map_addr, alignment);408 uintptr_t end = addr + size;409 if (addr != map_addr) {410 zx_info_vmar_t info;411 status = _zx_object_get_info(vmar_used, ZX_INFO_VMAR, &info, sizeof(info),412 NULL, NULL);413 if (status == ZX_OK) {414 uintptr_t new_addr;415 status = _zx_vmar_map(416 vmar_used,417 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_SPECIFIC_OVERWRITE,418 addr - info.base, vmo, 0, size, &new_addr);419 if (status == ZX_OK)420 CHECK_EQ(new_addr, addr);421 }422 }423 if (status == ZX_OK && addr != map_addr)424 status = _zx_vmar_unmap(vmar_used, map_addr, addr - map_addr);425 if (status == ZX_OK && end != map_end)426 status = _zx_vmar_unmap(vmar_used, end, map_end - end);427 }428 _zx_handle_close(vmo);429 430 if (status != ZX_OK) {431 if (status != ZX_ERR_NO_MEMORY)432 ReportMmapFailureAndDie(size, mem_type, "zx_vmar_map", status, false);433 return nullptr;434 }435 436 IncreaseTotalMmap(size);437 438 return reinterpret_cast<void *>(addr);439}440 441void UnmapOrDie(void *addr, uptr size, bool raw_report) {442 UnmapOrDieVmar(addr, size, gSanitizerHeapVmar, raw_report);443}444 445void ReleaseMemoryPagesToOS(uptr beg, uptr end) {446 uptr beg_aligned = RoundUpTo(beg, GetPageSize());447 uptr end_aligned = RoundDownTo(end, GetPageSize());448 if (beg_aligned < end_aligned) {449 zx_handle_t root_vmar = _zx_vmar_root_self();450 CHECK_NE(root_vmar, ZX_HANDLE_INVALID);451 zx_status_t status =452 _zx_vmar_op_range(root_vmar, ZX_VMAR_OP_DECOMMIT, beg_aligned,453 end_aligned - beg_aligned, nullptr, 0);454 CHECK_EQ(status, ZX_OK);455 }456}457 458void DumpProcessMap() {459 // TODO(mcgrathr): write it460 return;461}462 463bool IsAccessibleMemoryRange(uptr beg, uptr size) {464 // TODO(mcgrathr): Figure out a better way.465 zx_handle_t vmo;466 zx_status_t status = _zx_vmo_create(size, 0, &vmo);467 if (status == ZX_OK) {468 status = _zx_vmo_write(vmo, reinterpret_cast<const void *>(beg), 0, size);469 _zx_handle_close(vmo);470 }471 return status == ZX_OK;472}473 474bool TryMemCpy(void *dest, const void *src, uptr n) {475 // TODO: implement.476 return false;477}478 479// FIXME implement on this platform.480void GetMemoryProfile(fill_profile_f cb, uptr *stats) {}481 482bool ReadFileToBuffer(const char *file_name, char **buff, uptr *buff_size,483 uptr *read_len, uptr max_len, error_t *errno_p) {484 *errno_p = ZX_ERR_NOT_SUPPORTED;485 return false;486}487 488void RawWrite(const char *buffer) {489 constexpr size_t size = 128;490 static _Thread_local char line[size];491 static _Thread_local size_t lastLineEnd = 0;492 static _Thread_local size_t cur = 0;493 494 while (*buffer) {495 if (cur >= size) {496 if (lastLineEnd == 0)497 lastLineEnd = size;498 __sanitizer_log_write(line, lastLineEnd);499 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);500 cur = cur - lastLineEnd;501 lastLineEnd = 0;502 }503 if (*buffer == '\n')504 lastLineEnd = cur + 1;505 line[cur++] = *buffer++;506 }507 // Flush all complete lines before returning.508 if (lastLineEnd != 0) {509 __sanitizer_log_write(line, lastLineEnd);510 internal_memmove(line, line + lastLineEnd, cur - lastLineEnd);511 cur = cur - lastLineEnd;512 lastLineEnd = 0;513 }514}515 516void CatastrophicErrorWrite(const char *buffer, uptr length) {517 __sanitizer_log_write(buffer, length);518}519 520char **StoredArgv;521char **StoredEnviron;522 523char **GetArgv() { return StoredArgv; }524char **GetEnviron() { return StoredEnviron; }525 526const char *GetEnv(const char *name) {527 if (StoredEnviron) {528 uptr NameLen = internal_strlen(name);529 for (char **Env = StoredEnviron; *Env != 0; Env++) {530 if (internal_strncmp(*Env, name, NameLen) == 0 && (*Env)[NameLen] == '=')531 return (*Env) + NameLen + 1;532 }533 }534 return nullptr;535}536 537uptr ReadBinaryName(/*out*/ char *buf, uptr buf_len) {538 const char *argv0 = "<UNKNOWN>";539 if (StoredArgv && StoredArgv[0]) {540 argv0 = StoredArgv[0];541 }542 internal_strncpy(buf, argv0, buf_len);543 return internal_strlen(buf);544}545 546uptr ReadLongProcessName(/*out*/ char *buf, uptr buf_len) {547 return ReadBinaryName(buf, buf_len);548}549 550uptr MainThreadStackBase, MainThreadStackSize;551 552bool GetRandom(void *buffer, uptr length, bool blocking) {553 _zx_cprng_draw(buffer, length);554 return true;555}556 557u32 GetNumberOfCPUs() { return zx_system_get_num_cpus(); }558 559uptr GetRSS() { UNIMPLEMENTED(); }560 561void *internal_start_thread(void *(*func)(void *arg), void *arg) { return 0; }562void internal_join_thread(void *th) {}563 564void InitializePlatformCommonFlags(CommonFlags *cf) {}565 566} // namespace __sanitizer567 568using namespace __sanitizer;569 570extern "C" {571void __sanitizer_startup_hook(int argc, char **argv, char **envp,572 void *stack_base, size_t stack_size) {573 __sanitizer::StoredArgv = argv;574 __sanitizer::StoredEnviron = envp;575 __sanitizer::MainThreadStackBase = reinterpret_cast<uintptr_t>(stack_base);576 __sanitizer::MainThreadStackSize = stack_size;577 578 EarlySanitizerInit();579}580 581void __sanitizer_set_report_path(const char *path) {582 // Handle the initialization code in each sanitizer, but no other calls.583 // This setting is never consulted on Fuchsia.584 DCHECK_EQ(path, common_flags()->log_path);585}586 587void __sanitizer_set_report_fd(void *fd) {588 UNREACHABLE("not available on Fuchsia");589}590 591const char *__sanitizer_get_report_path() {592 UNREACHABLE("not available on Fuchsia");593}594} // extern "C"595 596#endif // SANITIZER_FUCHSIA597