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