Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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 |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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 | |
Roland McGrath | ce5094b | 2021-02-02 20:55:52 -0800 | [diff] [blame] | 25 | uptr getPageSize() { return _zx_system_get_page_size(); } |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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. |
Dynamic Tools Team | 09e6d48 | 2019-11-26 18:18:14 -0800 | [diff] [blame] | 31 | static_assert(ZX_HANDLE_INVALID == 0, ""); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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) |
Kostya Kortchinsky | 4d022f5 | 2021-05-24 09:26:21 -0700 | [diff] [blame^] | 44 | dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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) { |
Kostya Kortchinsky | 0167893 | 2021-04-14 21:09:20 -0700 | [diff] [blame] | 52 | DCHECK_EQ(Size % getPageSizeCached(), 0); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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) |
Kostya Kortchinsky | 4d022f5 | 2021-05-24 09:26:21 -0700 | [diff] [blame^] | 74 | dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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) |
Kostya Kortchinsky | 4d022f5 | 2021-05-24 09:26:21 -0700 | [diff] [blame^] | 82 | dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 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); |
Kostya Kortchinsky | 0167893 | 2021-04-14 21:09:20 -0700 | [diff] [blame] | 99 | if (Data->Vmo == ZX_HANDLE_INVALID) |
| 100 | Data->Vmo = Vmo; |
| 101 | else |
| 102 | DCHECK_EQ(Data->Vmo, Vmo); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 103 | } else { |
| 104 | CHECK_EQ(_zx_handle_close(Vmo), ZX_OK); |
| 105 | } |
| 106 | if (UNLIKELY(Status != ZX_OK)) { |
| 107 | if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) |
Kostya Kortchinsky | 4d022f5 | 2021-05-24 09:26:21 -0700 | [diff] [blame^] | 108 | dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 109 | return nullptr; |
| 110 | } |
| 111 | if (Data) |
| 112 | Data->VmoSize += Size; |
| 113 | |
| 114 | return reinterpret_cast<void *>(P); |
| 115 | } |
| 116 | |
| 117 | void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) { |
| 118 | if (Flags & UNMAP_ALL) { |
| 119 | DCHECK_NE(Data, nullptr); |
| 120 | const zx_handle_t Vmar = Data->Vmar; |
| 121 | DCHECK_NE(Vmar, _zx_vmar_root_self()); |
| 122 | // Destroying the vmar effectively unmaps the whole mapping. |
| 123 | CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK); |
| 124 | CHECK_EQ(_zx_handle_close(Vmar), ZX_OK); |
| 125 | } else { |
| 126 | const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self(); |
| 127 | const zx_status_t Status = |
| 128 | _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size); |
| 129 | if (UNLIKELY(Status != ZX_OK)) |
| 130 | dieOnMapUnmapError(); |
| 131 | } |
| 132 | if (Data) { |
| 133 | if (Data->Vmo != ZX_HANDLE_INVALID) |
| 134 | CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK); |
| 135 | memset(Data, 0, sizeof(*Data)); |
| 136 | } |
| 137 | } |
| 138 | |
Peter Collingbourne | cc3d493 | 2020-12-21 18:39:03 -0800 | [diff] [blame] | 139 | void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags, |
| 140 | UNUSED MapPlatformData *Data) { |
| 141 | const zx_vm_option_t Prot = |
| 142 | (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE); |
| 143 | DCHECK(Data); |
| 144 | DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); |
| 145 | if (_zx_vmar_protect(Data->Vmar, Prot, Addr, Size) != ZX_OK) |
| 146 | dieOnMapUnmapError(); |
| 147 | } |
| 148 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 149 | void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size, |
| 150 | MapPlatformData *Data) { |
| 151 | DCHECK(Data); |
| 152 | DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); |
| 153 | DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID); |
| 154 | const zx_status_t Status = |
| 155 | _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0); |
| 156 | CHECK_EQ(Status, ZX_OK); |
| 157 | } |
| 158 | |
| 159 | const char *getEnv(const char *Name) { return getenv(Name); } |
| 160 | |
| 161 | // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS |
| 162 | // because the Fuchsia implementation of sync_mutex_t has clang thread safety |
| 163 | // annotations. Were we to apply proper capability annotations to the top level |
| 164 | // HybridMutex class itself, they would not be needed. As it stands, the |
| 165 | // thread analysis thinks that we are locking the mutex and accidentally leaving |
| 166 | // it locked on the way out. |
| 167 | bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS { |
| 168 | // Size and alignment must be compatible between both types. |
| 169 | return sync_mutex_trylock(&M) == ZX_OK; |
| 170 | } |
| 171 | |
| 172 | void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS { |
| 173 | sync_mutex_lock(&M); |
| 174 | } |
| 175 | |
| 176 | void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { |
| 177 | sync_mutex_unlock(&M); |
| 178 | } |
| 179 | |
| 180 | u64 getMonotonicTime() { return _zx_clock_get_monotonic(); } |
| 181 | |
| 182 | u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); } |
| 183 | |
Dynamic Tools Team | 0d7d2ae | 2020-04-21 15:30:50 -0700 | [diff] [blame] | 184 | u32 getThreadID() { return 0; } |
| 185 | |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 186 | bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) { |
Dynamic Tools Team | 09e6d48 | 2019-11-26 18:18:14 -0800 | [diff] [blame] | 187 | static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, ""); |
Dynamic Tools Team | 517193e | 2019-09-11 14:48:41 +0000 | [diff] [blame] | 188 | if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength)) |
| 189 | return false; |
| 190 | _zx_cprng_draw(Buffer, Length); |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | void outputRaw(const char *Buffer) { |
| 195 | __sanitizer_log_write(Buffer, strlen(Buffer)); |
| 196 | } |
| 197 | |
| 198 | void setAbortMessage(const char *Message) {} |
| 199 | |
| 200 | } // namespace scudo |
| 201 | |
| 202 | #endif // SCUDO_FUCHSIA |