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