237 lines · cpp
1//===-- fuchsia.cpp ---------------------------------------------*- C++ -*-===//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#include "platform.h"10 11#if SCUDO_FUCHSIA12 13#include "common.h"14#include "mutex.h"15#include "string_utils.h"16 17#include <lib/sync/mutex.h> // for sync_mutex_t18#include <stdlib.h> // for getenv()19#include <zircon/compiler.h>20#include <zircon/process.h>21#include <zircon/sanitizer.h>22#include <zircon/status.h>23#include <zircon/syscalls.h>24 25namespace scudo {26 27uptr getPageSize() { return _zx_system_get_page_size(); }28 29void NORETURN die() { __builtin_trap(); }30 31// We zero-initialize the Extra parameter of map(), make sure this is consistent32// with ZX_HANDLE_INVALID.33static_assert(ZX_HANDLE_INVALID == 0, "");34 35static void NORETURN dieOnError(zx_status_t Status, const char *FnName,36 uptr Size) {37 ScopedString Error;38 Error.append("SCUDO ERROR: %s failed with size %zuKB (%s)", FnName,39 Size >> 10, zx_status_get_string(Status));40 outputRaw(Error.data());41 die();42}43 44static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) {45 // Only scenario so far.46 DCHECK(Data);47 DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID);48 49 const zx_status_t Status = _zx_vmar_allocate(50 _zx_vmar_root_self(),51 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,52 Size, &Data->Vmar, &Data->VmarBase);53 if (UNLIKELY(Status != ZX_OK)) {54 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)55 dieOnError(Status, "zx_vmar_allocate", Size);56 return nullptr;57 }58 return reinterpret_cast<void *>(Data->VmarBase);59}60 61void *map(void *Addr, uptr Size, const char *Name, uptr Flags,62 MapPlatformData *Data) {63 DCHECK_EQ(Size % getPageSizeCached(), 0);64 const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);65 66 // For MAP_NOACCESS, just allocate a Vmar and return.67 if (Flags & MAP_NOACCESS)68 return allocateVmar(Size, Data, AllowNoMem);69 70 const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID)71 ? Data->Vmar72 : _zx_vmar_root_self();73 74 zx_status_t Status;75 zx_handle_t Vmo;76 uint64_t VmoSize = 0;77 if (Data && Data->Vmo != ZX_HANDLE_INVALID) {78 // If a Vmo was specified, it's a resize operation.79 CHECK(Addr);80 DCHECK(Flags & MAP_RESIZABLE);81 Vmo = Data->Vmo;82 VmoSize = Data->VmoSize;83 Status = _zx_vmo_set_size(Vmo, VmoSize + Size);84 if (Status != ZX_OK) {85 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)86 dieOnError(Status, "zx_vmo_set_size", VmoSize + Size);87 return nullptr;88 }89 } else {90 // Otherwise, create a Vmo and set its name.91 Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo);92 if (UNLIKELY(Status != ZX_OK)) {93 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)94 dieOnError(Status, "zx_vmo_create", Size);95 return nullptr;96 }97 _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name));98 }99 100 uintptr_t P;101 zx_vm_option_t MapFlags =102 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS;103 if (Addr)104 DCHECK(Data);105 const uint64_t Offset =106 Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0;107 if (Offset)108 MapFlags |= ZX_VM_SPECIFIC;109 Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P);110 if (UNLIKELY(Status != ZX_OK)) {111 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)112 dieOnError(Status, "zx_vmar_map", Size);113 return nullptr;114 }115 116 if (Flags & MAP_PRECOMMIT) {117 Status = _zx_vmar_op_range(Vmar, ZX_VMAR_OP_COMMIT, P, Size,118 /*buffer=*/nullptr, /*buffer_size=*/0);119 }120 121 // No need to track the Vmo if we don't intend on resizing it. Close it.122 if (Flags & MAP_RESIZABLE) {123 DCHECK(Data);124 if (Data->Vmo == ZX_HANDLE_INVALID)125 Data->Vmo = Vmo;126 else127 DCHECK_EQ(Data->Vmo, Vmo);128 } else {129 CHECK_EQ(_zx_handle_close(Vmo), ZX_OK);130 }131 if (UNLIKELY(Status != ZX_OK)) {132 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)133 dieOnError(Status, "zx_vmar_op_range", Size);134 return nullptr;135 }136 137 if (Data)138 Data->VmoSize += Size;139 140 return reinterpret_cast<void *>(P);141}142 143void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) {144 if (Flags & UNMAP_ALL) {145 DCHECK_NE(Data, nullptr);146 const zx_handle_t Vmar = Data->Vmar;147 DCHECK_NE(Vmar, _zx_vmar_root_self());148 // Destroying the vmar effectively unmaps the whole mapping.149 CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK);150 CHECK_EQ(_zx_handle_close(Vmar), ZX_OK);151 } else {152 const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID)153 ? Data->Vmar154 : _zx_vmar_root_self();155 const zx_status_t Status =156 _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size);157 if (UNLIKELY(Status != ZX_OK))158 dieOnError(Status, "zx_vmar_unmap", Size);159 }160 if (Data) {161 if (Data->Vmo != ZX_HANDLE_INVALID)162 CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK);163 memset(Data, 0, sizeof(*Data));164 }165}166 167void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,168 UNUSED MapPlatformData *Data) {169 const zx_vm_option_t Prot =170 (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE);171 DCHECK(Data);172 DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);173 const zx_status_t Status = _zx_vmar_protect(Data->Vmar, Prot, Addr, Size);174 if (Status != ZX_OK)175 dieOnError(Status, "zx_vmar_protect", Size);176}177 178void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size,179 MapPlatformData *Data) {180 // TODO: DCHECK the BaseAddress is consistent with the data in181 // MapPlatformData.182 DCHECK(Data);183 DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);184 DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID);185 const zx_status_t Status =186 _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0);187 CHECK_EQ(Status, ZX_OK);188}189 190const char *getEnv(const char *Name) { return getenv(Name); }191 192// Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS193// because the Fuchsia implementation of sync_mutex_t has clang thread safety194// annotations. Were we to apply proper capability annotations to the top level195// HybridMutex class itself, they would not be needed. As it stands, the196// thread analysis thinks that we are locking the mutex and accidentally leaving197// it locked on the way out.198bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS {199 // Size and alignment must be compatible between both types.200 return sync_mutex_trylock(&M) == ZX_OK;201}202 203void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS {204 sync_mutex_lock(&M);205}206 207void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS {208 sync_mutex_unlock(&M);209}210 211void HybridMutex::assertHeldImpl() __TA_NO_THREAD_SAFETY_ANALYSIS {}212 213u64 getMonotonicTime() { return _zx_clock_get_monotonic(); }214u64 getMonotonicTimeFast() { return _zx_clock_get_monotonic(); }215 216u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); }217 218u32 getThreadID() { return 0; }219 220bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {221 static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, "");222 if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength))223 return false;224 _zx_cprng_draw(Buffer, Length);225 return true;226}227 228void outputRaw(const char *Buffer) {229 __sanitizer_log_write(Buffer, strlen(Buffer));230}231 232void setAbortMessage(const char *Message) {}233 234} // namespace scudo235 236#endif // SCUDO_FUCHSIA237