1 //===-- fuchsia.cc ----------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
15 #include "string_utils.h"
17 #include <lib/sync/mutex.h> // for sync_mutex_t
18 #include <limits.h> // for PAGE_SIZE
19 #include <stdlib.h> // for getenv()
20 #include <zircon/compiler.h>
21 #include <zircon/sanitizer.h>
22 #include <zircon/syscalls.h>
26 uptr getPageSize() { return PAGE_SIZE; }
28 void NORETURN die() { __builtin_trap(); }
30 // We zero-initialize the Extra parameter of map(), make sure this is consistent
31 // with ZX_HANDLE_INVALID.
32 COMPILER_CHECK(ZX_HANDLE_INVALID == 0);
34 static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) {
35 // Only scenario so far.
37 DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID);
39 const zx_status_t Status = _zx_vmar_allocate(
41 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,
42 Size, &Data->Vmar, &Data->VmarBase);
43 if (Status != ZX_OK) {
44 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
45 dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY);
48 return reinterpret_cast<void *>(Data->VmarBase);
51 void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
52 MapPlatformData *Data) {
53 DCHECK_EQ(Size % PAGE_SIZE, 0);
54 const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
56 // For MAP_NOACCESS, just allocate a Vmar and return.
57 if (Flags & MAP_NOACCESS)
58 return allocateVmar(Size, Data, AllowNoMem);
60 const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
61 CHECK_NE(Vmar, ZX_HANDLE_INVALID);
66 if (Data && Data->Vmo != ZX_HANDLE_INVALID) {
67 // If a Vmo was specified, it's a resize operation.
69 DCHECK(Flags & MAP_RESIZABLE);
71 VmoSize = Data->VmoSize;
72 Status = _zx_vmo_set_size(Vmo, VmoSize + Size);
73 if (Status != ZX_OK) {
74 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
75 dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY);
79 // Otherwise, create a Vmo and set its name.
80 Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo);
81 if (Status != ZX_OK) {
82 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
83 dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY);
86 _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name));
90 zx_vm_option_t MapFlags =
91 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS;
92 const uint64_t Offset =
93 Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0;
95 MapFlags |= ZX_VM_SPECIFIC;
96 Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P);
97 // No need to track the Vmo if we don't intend on resizing it. Close it.
98 if (Flags & MAP_RESIZABLE) {
100 DCHECK_EQ(Data->Vmo, ZX_HANDLE_INVALID);
103 CHECK_EQ(_zx_handle_close(Vmo), ZX_OK);
105 if (Status != ZX_OK) {
106 if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
107 dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY);
111 Data->VmoSize += Size;
113 return reinterpret_cast<void *>(P);
116 void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) {
117 if (Flags & UNMAP_ALL) {
118 DCHECK_NE(Data, nullptr);
119 const zx_handle_t Vmar = Data->Vmar;
120 DCHECK_NE(Vmar, _zx_vmar_root_self());
121 // Destroying the vmar effectively unmaps the whole mapping.
122 CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK);
123 CHECK_EQ(_zx_handle_close(Vmar), ZX_OK);
125 const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
126 const zx_status_t Status =
127 _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size);
129 dieOnMapUnmapError();
132 if (Data->Vmo != ZX_HANDLE_INVALID)
133 CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK);
134 memset(Data, 0, sizeof(*Data));
138 void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size,
139 MapPlatformData *Data) {
141 DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
142 DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID);
143 const zx_status_t Status =
144 _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0);
145 CHECK_EQ(Status, ZX_OK);
148 const char *getEnv(const char *Name) { return getenv(Name); }
150 // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS
151 // because the Fuchsia implementation of sync_mutex_t has clang thread safety
152 // annotations. Were we to apply proper capability annotations to the top level
153 // HybridMutex class itself, they would not be needed. As it stands, the
154 // thread analysis thinks that we are locking the mutex and accidentally leaving
155 // it locked on the way out.
156 bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS {
157 // Size and alignment must be compatible between both types.
158 return sync_mutex_trylock(&M) == ZX_OK;
161 void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS {
165 void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS {
166 sync_mutex_unlock(&M);
169 u64 getMonotonicTime() { return _zx_clock_get_monotonic(); }
171 u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); }
173 bool getRandom(void *Buffer, uptr Length, bool Blocking) {
174 COMPILER_CHECK(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN);
175 if (!Buffer || !Length || Length > MaxRandomLength)
177 _zx_cprng_draw(Buffer, Length);
181 void outputRaw(const char *Buffer) {
182 __sanitizer_log_write(Buffer, strlen(Buffer));
185 void setAbortMessage(const char *Message) {}
189 #endif // SCUDO_FUCHSIA