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