1*68d75effSDimitry Andric //===-- fuchsia.cpp ---------------------------------------------*- C++ -*-===// 2*68d75effSDimitry Andric // 3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*68d75effSDimitry Andric // 7*68d75effSDimitry Andric //===----------------------------------------------------------------------===// 8*68d75effSDimitry Andric 9*68d75effSDimitry Andric #include "platform.h" 10*68d75effSDimitry Andric 11*68d75effSDimitry Andric #if SCUDO_FUCHSIA 12*68d75effSDimitry Andric 13*68d75effSDimitry Andric #include "common.h" 14*68d75effSDimitry Andric #include "mutex.h" 15*68d75effSDimitry Andric #include "string_utils.h" 16*68d75effSDimitry Andric 17*68d75effSDimitry Andric #include <lib/sync/mutex.h> // for sync_mutex_t 18*68d75effSDimitry Andric #include <limits.h> // for PAGE_SIZE 19*68d75effSDimitry Andric #include <stdlib.h> // for getenv() 20*68d75effSDimitry Andric #include <zircon/compiler.h> 21*68d75effSDimitry Andric #include <zircon/sanitizer.h> 22*68d75effSDimitry Andric #include <zircon/syscalls.h> 23*68d75effSDimitry Andric 24*68d75effSDimitry Andric namespace scudo { 25*68d75effSDimitry Andric 26*68d75effSDimitry Andric uptr getPageSize() { return PAGE_SIZE; } 27*68d75effSDimitry Andric 28*68d75effSDimitry Andric void NORETURN die() { __builtin_trap(); } 29*68d75effSDimitry Andric 30*68d75effSDimitry Andric // We zero-initialize the Extra parameter of map(), make sure this is consistent 31*68d75effSDimitry Andric // with ZX_HANDLE_INVALID. 32*68d75effSDimitry Andric COMPILER_CHECK(ZX_HANDLE_INVALID == 0); 33*68d75effSDimitry Andric 34*68d75effSDimitry Andric static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) { 35*68d75effSDimitry Andric // Only scenario so far. 36*68d75effSDimitry Andric DCHECK(Data); 37*68d75effSDimitry Andric DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID); 38*68d75effSDimitry Andric 39*68d75effSDimitry Andric const zx_status_t Status = _zx_vmar_allocate( 40*68d75effSDimitry Andric _zx_vmar_root_self(), 41*68d75effSDimitry Andric ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0, 42*68d75effSDimitry Andric Size, &Data->Vmar, &Data->VmarBase); 43*68d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 44*68d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 45*68d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 46*68d75effSDimitry Andric return nullptr; 47*68d75effSDimitry Andric } 48*68d75effSDimitry Andric return reinterpret_cast<void *>(Data->VmarBase); 49*68d75effSDimitry Andric } 50*68d75effSDimitry Andric 51*68d75effSDimitry Andric void *map(void *Addr, uptr Size, const char *Name, uptr Flags, 52*68d75effSDimitry Andric MapPlatformData *Data) { 53*68d75effSDimitry Andric DCHECK_EQ(Size % PAGE_SIZE, 0); 54*68d75effSDimitry Andric const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM); 55*68d75effSDimitry Andric 56*68d75effSDimitry Andric // For MAP_NOACCESS, just allocate a Vmar and return. 57*68d75effSDimitry Andric if (Flags & MAP_NOACCESS) 58*68d75effSDimitry Andric return allocateVmar(Size, Data, AllowNoMem); 59*68d75effSDimitry Andric 60*68d75effSDimitry Andric const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self(); 61*68d75effSDimitry Andric CHECK_NE(Vmar, ZX_HANDLE_INVALID); 62*68d75effSDimitry Andric 63*68d75effSDimitry Andric zx_status_t Status; 64*68d75effSDimitry Andric zx_handle_t Vmo; 65*68d75effSDimitry Andric uint64_t VmoSize = 0; 66*68d75effSDimitry Andric if (Data && Data->Vmo != ZX_HANDLE_INVALID) { 67*68d75effSDimitry Andric // If a Vmo was specified, it's a resize operation. 68*68d75effSDimitry Andric CHECK(Addr); 69*68d75effSDimitry Andric DCHECK(Flags & MAP_RESIZABLE); 70*68d75effSDimitry Andric Vmo = Data->Vmo; 71*68d75effSDimitry Andric VmoSize = Data->VmoSize; 72*68d75effSDimitry Andric Status = _zx_vmo_set_size(Vmo, VmoSize + Size); 73*68d75effSDimitry Andric if (Status != ZX_OK) { 74*68d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 75*68d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 76*68d75effSDimitry Andric return nullptr; 77*68d75effSDimitry Andric } 78*68d75effSDimitry Andric } else { 79*68d75effSDimitry Andric // Otherwise, create a Vmo and set its name. 80*68d75effSDimitry Andric Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo); 81*68d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 82*68d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 83*68d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 84*68d75effSDimitry Andric return nullptr; 85*68d75effSDimitry Andric } 86*68d75effSDimitry Andric _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name)); 87*68d75effSDimitry Andric } 88*68d75effSDimitry Andric 89*68d75effSDimitry Andric uintptr_t P; 90*68d75effSDimitry Andric zx_vm_option_t MapFlags = 91*68d75effSDimitry Andric ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS; 92*68d75effSDimitry Andric const uint64_t Offset = 93*68d75effSDimitry Andric Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0; 94*68d75effSDimitry Andric if (Offset) 95*68d75effSDimitry Andric MapFlags |= ZX_VM_SPECIFIC; 96*68d75effSDimitry Andric Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P); 97*68d75effSDimitry Andric // No need to track the Vmo if we don't intend on resizing it. Close it. 98*68d75effSDimitry Andric if (Flags & MAP_RESIZABLE) { 99*68d75effSDimitry Andric DCHECK(Data); 100*68d75effSDimitry Andric DCHECK_EQ(Data->Vmo, ZX_HANDLE_INVALID); 101*68d75effSDimitry Andric Data->Vmo = Vmo; 102*68d75effSDimitry Andric } else { 103*68d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmo), ZX_OK); 104*68d75effSDimitry Andric } 105*68d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 106*68d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 107*68d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 108*68d75effSDimitry Andric return nullptr; 109*68d75effSDimitry Andric } 110*68d75effSDimitry Andric if (Data) 111*68d75effSDimitry Andric Data->VmoSize += Size; 112*68d75effSDimitry Andric 113*68d75effSDimitry Andric return reinterpret_cast<void *>(P); 114*68d75effSDimitry Andric } 115*68d75effSDimitry Andric 116*68d75effSDimitry Andric void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) { 117*68d75effSDimitry Andric if (Flags & UNMAP_ALL) { 118*68d75effSDimitry Andric DCHECK_NE(Data, nullptr); 119*68d75effSDimitry Andric const zx_handle_t Vmar = Data->Vmar; 120*68d75effSDimitry Andric DCHECK_NE(Vmar, _zx_vmar_root_self()); 121*68d75effSDimitry Andric // Destroying the vmar effectively unmaps the whole mapping. 122*68d75effSDimitry Andric CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK); 123*68d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmar), ZX_OK); 124*68d75effSDimitry Andric } else { 125*68d75effSDimitry Andric const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self(); 126*68d75effSDimitry Andric const zx_status_t Status = 127*68d75effSDimitry Andric _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size); 128*68d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) 129*68d75effSDimitry Andric dieOnMapUnmapError(); 130*68d75effSDimitry Andric } 131*68d75effSDimitry Andric if (Data) { 132*68d75effSDimitry Andric if (Data->Vmo != ZX_HANDLE_INVALID) 133*68d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK); 134*68d75effSDimitry Andric memset(Data, 0, sizeof(*Data)); 135*68d75effSDimitry Andric } 136*68d75effSDimitry Andric } 137*68d75effSDimitry Andric 138*68d75effSDimitry Andric void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size, 139*68d75effSDimitry Andric MapPlatformData *Data) { 140*68d75effSDimitry Andric DCHECK(Data); 141*68d75effSDimitry Andric DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); 142*68d75effSDimitry Andric DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID); 143*68d75effSDimitry Andric const zx_status_t Status = 144*68d75effSDimitry Andric _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0); 145*68d75effSDimitry Andric CHECK_EQ(Status, ZX_OK); 146*68d75effSDimitry Andric } 147*68d75effSDimitry Andric 148*68d75effSDimitry Andric const char *getEnv(const char *Name) { return getenv(Name); } 149*68d75effSDimitry Andric 150*68d75effSDimitry Andric // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS 151*68d75effSDimitry Andric // because the Fuchsia implementation of sync_mutex_t has clang thread safety 152*68d75effSDimitry Andric // annotations. Were we to apply proper capability annotations to the top level 153*68d75effSDimitry Andric // HybridMutex class itself, they would not be needed. As it stands, the 154*68d75effSDimitry Andric // thread analysis thinks that we are locking the mutex and accidentally leaving 155*68d75effSDimitry Andric // it locked on the way out. 156*68d75effSDimitry Andric bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS { 157*68d75effSDimitry Andric // Size and alignment must be compatible between both types. 158*68d75effSDimitry Andric return sync_mutex_trylock(&M) == ZX_OK; 159*68d75effSDimitry Andric } 160*68d75effSDimitry Andric 161*68d75effSDimitry Andric void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS { 162*68d75effSDimitry Andric sync_mutex_lock(&M); 163*68d75effSDimitry Andric } 164*68d75effSDimitry Andric 165*68d75effSDimitry Andric void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { 166*68d75effSDimitry Andric sync_mutex_unlock(&M); 167*68d75effSDimitry Andric } 168*68d75effSDimitry Andric 169*68d75effSDimitry Andric u64 getMonotonicTime() { return _zx_clock_get_monotonic(); } 170*68d75effSDimitry Andric 171*68d75effSDimitry Andric u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); } 172*68d75effSDimitry Andric 173*68d75effSDimitry Andric bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) { 174*68d75effSDimitry Andric COMPILER_CHECK(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN); 175*68d75effSDimitry Andric if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength)) 176*68d75effSDimitry Andric return false; 177*68d75effSDimitry Andric _zx_cprng_draw(Buffer, Length); 178*68d75effSDimitry Andric return true; 179*68d75effSDimitry Andric } 180*68d75effSDimitry Andric 181*68d75effSDimitry Andric void outputRaw(const char *Buffer) { 182*68d75effSDimitry Andric __sanitizer_log_write(Buffer, strlen(Buffer)); 183*68d75effSDimitry Andric } 184*68d75effSDimitry Andric 185*68d75effSDimitry Andric void setAbortMessage(const char *Message) {} 186*68d75effSDimitry Andric 187*68d75effSDimitry Andric } // namespace scudo 188*68d75effSDimitry Andric 189*68d75effSDimitry Andric #endif // SCUDO_FUCHSIA 190