168d75effSDimitry Andric //===-- fuchsia.cpp ---------------------------------------------*- C++ -*-===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric 
968d75effSDimitry Andric #include "platform.h"
1068d75effSDimitry Andric 
1168d75effSDimitry Andric #if SCUDO_FUCHSIA
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #include "common.h"
1468d75effSDimitry Andric #include "mutex.h"
1568d75effSDimitry Andric #include "string_utils.h"
1668d75effSDimitry Andric 
1768d75effSDimitry Andric #include <lib/sync/mutex.h> // for sync_mutex_t
1868d75effSDimitry Andric #include <stdlib.h>         // for getenv()
1968d75effSDimitry Andric #include <zircon/compiler.h>
2081ad6265SDimitry Andric #include <zircon/process.h>
2168d75effSDimitry Andric #include <zircon/sanitizer.h>
22*06c3fb27SDimitry Andric #include <zircon/status.h>
2368d75effSDimitry Andric #include <zircon/syscalls.h>
2468d75effSDimitry Andric 
2568d75effSDimitry Andric namespace scudo {
2668d75effSDimitry Andric 
getPageSize()27fe6060f1SDimitry Andric uptr getPageSize() { return _zx_system_get_page_size(); }
2868d75effSDimitry Andric 
die()2968d75effSDimitry Andric void NORETURN die() { __builtin_trap(); }
3068d75effSDimitry Andric 
3168d75effSDimitry Andric // We zero-initialize the Extra parameter of map(), make sure this is consistent
3268d75effSDimitry Andric // with ZX_HANDLE_INVALID.
33480093f4SDimitry Andric static_assert(ZX_HANDLE_INVALID == 0, "");
3468d75effSDimitry Andric 
dieOnError(zx_status_t Status,const char * FnName,uptr Size)35*06c3fb27SDimitry Andric static void NORETURN dieOnError(zx_status_t Status, const char *FnName,
36*06c3fb27SDimitry Andric                                 uptr Size) {
37*06c3fb27SDimitry Andric   char Error[128];
38*06c3fb27SDimitry Andric   formatString(Error, sizeof(Error),
39*06c3fb27SDimitry Andric                "SCUDO ERROR: %s failed with size %zuKB (%s)", FnName,
40*06c3fb27SDimitry Andric                Size >> 10, zx_status_get_string(Status));
41*06c3fb27SDimitry Andric   outputRaw(Error);
42*06c3fb27SDimitry Andric   die();
43*06c3fb27SDimitry Andric }
44*06c3fb27SDimitry Andric 
allocateVmar(uptr Size,MapPlatformData * Data,bool AllowNoMem)4568d75effSDimitry Andric static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) {
4668d75effSDimitry Andric   // Only scenario so far.
4768d75effSDimitry Andric   DCHECK(Data);
4868d75effSDimitry Andric   DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID);
4968d75effSDimitry Andric 
5068d75effSDimitry Andric   const zx_status_t Status = _zx_vmar_allocate(
5168d75effSDimitry Andric       _zx_vmar_root_self(),
5268d75effSDimitry Andric       ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,
5368d75effSDimitry Andric       Size, &Data->Vmar, &Data->VmarBase);
5468d75effSDimitry Andric   if (UNLIKELY(Status != ZX_OK)) {
5568d75effSDimitry Andric     if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
56*06c3fb27SDimitry Andric       dieOnError(Status, "zx_vmar_allocate", Size);
5768d75effSDimitry Andric     return nullptr;
5868d75effSDimitry Andric   }
5968d75effSDimitry Andric   return reinterpret_cast<void *>(Data->VmarBase);
6068d75effSDimitry Andric }
6168d75effSDimitry Andric 
map(void * Addr,uptr Size,const char * Name,uptr Flags,MapPlatformData * Data)6268d75effSDimitry Andric void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
6368d75effSDimitry Andric           MapPlatformData *Data) {
64fe6060f1SDimitry Andric   DCHECK_EQ(Size % getPageSizeCached(), 0);
6568d75effSDimitry Andric   const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
6668d75effSDimitry Andric 
6768d75effSDimitry Andric   // For MAP_NOACCESS, just allocate a Vmar and return.
6868d75effSDimitry Andric   if (Flags & MAP_NOACCESS)
6968d75effSDimitry Andric     return allocateVmar(Size, Data, AllowNoMem);
7068d75effSDimitry Andric 
71753f127fSDimitry Andric   const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID)
72753f127fSDimitry Andric                                ? Data->Vmar
73753f127fSDimitry Andric                                : _zx_vmar_root_self();
7468d75effSDimitry Andric 
7568d75effSDimitry Andric   zx_status_t Status;
7668d75effSDimitry Andric   zx_handle_t Vmo;
7768d75effSDimitry Andric   uint64_t VmoSize = 0;
7868d75effSDimitry Andric   if (Data && Data->Vmo != ZX_HANDLE_INVALID) {
7968d75effSDimitry Andric     // If a Vmo was specified, it's a resize operation.
8068d75effSDimitry Andric     CHECK(Addr);
8168d75effSDimitry Andric     DCHECK(Flags & MAP_RESIZABLE);
8268d75effSDimitry Andric     Vmo = Data->Vmo;
8368d75effSDimitry Andric     VmoSize = Data->VmoSize;
8468d75effSDimitry Andric     Status = _zx_vmo_set_size(Vmo, VmoSize + Size);
8568d75effSDimitry Andric     if (Status != ZX_OK) {
8668d75effSDimitry Andric       if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
87*06c3fb27SDimitry Andric         dieOnError(Status, "zx_vmo_set_size", VmoSize + Size);
8868d75effSDimitry Andric       return nullptr;
8968d75effSDimitry Andric     }
9068d75effSDimitry Andric   } else {
9168d75effSDimitry Andric     // Otherwise, create a Vmo and set its name.
9268d75effSDimitry Andric     Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo);
9368d75effSDimitry Andric     if (UNLIKELY(Status != ZX_OK)) {
9468d75effSDimitry Andric       if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
95*06c3fb27SDimitry Andric         dieOnError(Status, "zx_vmo_create", Size);
9668d75effSDimitry Andric       return nullptr;
9768d75effSDimitry Andric     }
9868d75effSDimitry Andric     _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name));
9968d75effSDimitry Andric   }
10068d75effSDimitry Andric 
10168d75effSDimitry Andric   uintptr_t P;
10268d75effSDimitry Andric   zx_vm_option_t MapFlags =
10368d75effSDimitry Andric       ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS;
10481ad6265SDimitry Andric   if (Addr)
10581ad6265SDimitry Andric     DCHECK(Data);
10668d75effSDimitry Andric   const uint64_t Offset =
10768d75effSDimitry Andric       Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0;
10868d75effSDimitry Andric   if (Offset)
10968d75effSDimitry Andric     MapFlags |= ZX_VM_SPECIFIC;
11068d75effSDimitry Andric   Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P);
111bdd1243dSDimitry Andric   if (UNLIKELY(Status != ZX_OK)) {
112bdd1243dSDimitry Andric     if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
113*06c3fb27SDimitry Andric       dieOnError(Status, "zx_vmar_map", Size);
114bdd1243dSDimitry Andric     return nullptr;
115bdd1243dSDimitry Andric   }
116bdd1243dSDimitry Andric 
117bdd1243dSDimitry Andric   if (Flags & MAP_PRECOMMIT) {
118bdd1243dSDimitry Andric     Status = _zx_vmar_op_range(Vmar, ZX_VMAR_OP_COMMIT, P, Size,
119bdd1243dSDimitry Andric                                /*buffer=*/nullptr, /*buffer_size=*/0);
120bdd1243dSDimitry Andric   }
121bdd1243dSDimitry Andric 
12268d75effSDimitry Andric   // No need to track the Vmo if we don't intend on resizing it. Close it.
12368d75effSDimitry Andric   if (Flags & MAP_RESIZABLE) {
12468d75effSDimitry Andric     DCHECK(Data);
125fe6060f1SDimitry Andric     if (Data->Vmo == ZX_HANDLE_INVALID)
12668d75effSDimitry Andric       Data->Vmo = Vmo;
127fe6060f1SDimitry Andric     else
128fe6060f1SDimitry Andric       DCHECK_EQ(Data->Vmo, Vmo);
12968d75effSDimitry Andric   } else {
13068d75effSDimitry Andric     CHECK_EQ(_zx_handle_close(Vmo), ZX_OK);
13168d75effSDimitry Andric   }
13268d75effSDimitry Andric   if (UNLIKELY(Status != ZX_OK)) {
13368d75effSDimitry Andric     if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
134*06c3fb27SDimitry Andric       dieOnError(Status, "zx_vmar_op_range", Size);
13568d75effSDimitry Andric     return nullptr;
13668d75effSDimitry Andric   }
137bdd1243dSDimitry Andric 
13868d75effSDimitry Andric   if (Data)
13968d75effSDimitry Andric     Data->VmoSize += Size;
14068d75effSDimitry Andric 
14168d75effSDimitry Andric   return reinterpret_cast<void *>(P);
14268d75effSDimitry Andric }
14368d75effSDimitry Andric 
unmap(void * Addr,uptr Size,uptr Flags,MapPlatformData * Data)14468d75effSDimitry Andric void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) {
14568d75effSDimitry Andric   if (Flags & UNMAP_ALL) {
14668d75effSDimitry Andric     DCHECK_NE(Data, nullptr);
14768d75effSDimitry Andric     const zx_handle_t Vmar = Data->Vmar;
14868d75effSDimitry Andric     DCHECK_NE(Vmar, _zx_vmar_root_self());
14968d75effSDimitry Andric     // Destroying the vmar effectively unmaps the whole mapping.
15068d75effSDimitry Andric     CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK);
15168d75effSDimitry Andric     CHECK_EQ(_zx_handle_close(Vmar), ZX_OK);
15268d75effSDimitry Andric   } else {
153753f127fSDimitry Andric     const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID)
154753f127fSDimitry Andric                                  ? Data->Vmar
155753f127fSDimitry Andric                                  : _zx_vmar_root_self();
15668d75effSDimitry Andric     const zx_status_t Status =
15768d75effSDimitry Andric         _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size);
15868d75effSDimitry Andric     if (UNLIKELY(Status != ZX_OK))
159*06c3fb27SDimitry Andric       dieOnError(Status, "zx_vmar_unmap", Size);
16068d75effSDimitry Andric   }
16168d75effSDimitry Andric   if (Data) {
16268d75effSDimitry Andric     if (Data->Vmo != ZX_HANDLE_INVALID)
16368d75effSDimitry Andric       CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK);
16468d75effSDimitry Andric     memset(Data, 0, sizeof(*Data));
16568d75effSDimitry Andric   }
16668d75effSDimitry Andric }
16768d75effSDimitry Andric 
setMemoryPermission(UNUSED uptr Addr,UNUSED uptr Size,UNUSED uptr Flags,UNUSED MapPlatformData * Data)168fe6060f1SDimitry Andric void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
169fe6060f1SDimitry Andric                          UNUSED MapPlatformData *Data) {
170fe6060f1SDimitry Andric   const zx_vm_option_t Prot =
171fe6060f1SDimitry Andric       (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE);
172fe6060f1SDimitry Andric   DCHECK(Data);
173fe6060f1SDimitry Andric   DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
174*06c3fb27SDimitry Andric   const zx_status_t Status = _zx_vmar_protect(Data->Vmar, Prot, Addr, Size);
175*06c3fb27SDimitry Andric   if (Status != ZX_OK)
176*06c3fb27SDimitry Andric     dieOnError(Status, "zx_vmar_protect", Size);
177fe6060f1SDimitry Andric }
178fe6060f1SDimitry Andric 
releasePagesToOS(UNUSED uptr BaseAddress,uptr Offset,uptr Size,MapPlatformData * Data)17968d75effSDimitry Andric void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size,
18068d75effSDimitry Andric                       MapPlatformData *Data) {
181*06c3fb27SDimitry Andric   // TODO: DCHECK the BaseAddress is consistent with the data in
182*06c3fb27SDimitry Andric   // MapPlatformData.
18368d75effSDimitry Andric   DCHECK(Data);
18468d75effSDimitry Andric   DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
18568d75effSDimitry Andric   DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID);
18668d75effSDimitry Andric   const zx_status_t Status =
18768d75effSDimitry Andric       _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0);
18868d75effSDimitry Andric   CHECK_EQ(Status, ZX_OK);
18968d75effSDimitry Andric }
19068d75effSDimitry Andric 
getEnv(const char * Name)19168d75effSDimitry Andric const char *getEnv(const char *Name) { return getenv(Name); }
19268d75effSDimitry Andric 
19368d75effSDimitry Andric // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS
19468d75effSDimitry Andric // because the Fuchsia implementation of sync_mutex_t has clang thread safety
19568d75effSDimitry Andric // annotations. Were we to apply proper capability annotations to the top level
19668d75effSDimitry Andric // HybridMutex class itself, they would not be needed. As it stands, the
19768d75effSDimitry Andric // thread analysis thinks that we are locking the mutex and accidentally leaving
19868d75effSDimitry Andric // it locked on the way out.
tryLock()19968d75effSDimitry Andric bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS {
20068d75effSDimitry Andric   // Size and alignment must be compatible between both types.
20168d75effSDimitry Andric   return sync_mutex_trylock(&M) == ZX_OK;
20268d75effSDimitry Andric }
20368d75effSDimitry Andric 
lockSlow()20468d75effSDimitry Andric void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS {
20568d75effSDimitry Andric   sync_mutex_lock(&M);
20668d75effSDimitry Andric }
20768d75effSDimitry Andric 
unlock()20868d75effSDimitry Andric void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS {
20968d75effSDimitry Andric   sync_mutex_unlock(&M);
21068d75effSDimitry Andric }
21168d75effSDimitry Andric 
assertHeldImpl()212*06c3fb27SDimitry Andric void HybridMutex::assertHeldImpl() __TA_NO_THREAD_SAFETY_ANALYSIS {}
213*06c3fb27SDimitry Andric 
getMonotonicTime()21468d75effSDimitry Andric u64 getMonotonicTime() { return _zx_clock_get_monotonic(); }
getMonotonicTimeFast()215*06c3fb27SDimitry Andric u64 getMonotonicTimeFast() { return _zx_clock_get_monotonic(); }
21668d75effSDimitry Andric 
getNumberOfCPUs()21768d75effSDimitry Andric u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); }
21868d75effSDimitry Andric 
getThreadID()2195ffd83dbSDimitry Andric u32 getThreadID() { return 0; }
2205ffd83dbSDimitry Andric 
getRandom(void * Buffer,uptr Length,UNUSED bool Blocking)22168d75effSDimitry Andric bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
222480093f4SDimitry Andric   static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, "");
22368d75effSDimitry Andric   if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength))
22468d75effSDimitry Andric     return false;
22568d75effSDimitry Andric   _zx_cprng_draw(Buffer, Length);
22668d75effSDimitry Andric   return true;
22768d75effSDimitry Andric }
22868d75effSDimitry Andric 
outputRaw(const char * Buffer)22968d75effSDimitry Andric void outputRaw(const char *Buffer) {
23068d75effSDimitry Andric   __sanitizer_log_write(Buffer, strlen(Buffer));
23168d75effSDimitry Andric }
23268d75effSDimitry Andric 
setAbortMessage(const char * Message)23368d75effSDimitry Andric void setAbortMessage(const char *Message) {}
23468d75effSDimitry Andric 
23568d75effSDimitry Andric } // namespace scudo
23668d75effSDimitry Andric 
23768d75effSDimitry Andric #endif // SCUDO_FUCHSIA
238