xref: /freebsd-src/contrib/llvm-project/compiler-rt/lib/scudo/standalone/fuchsia.cpp (revision 81ad626541db97eb356e2c1d4a20eb2a26a766ab)
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>
20*81ad6265SDimitry Andric #include <zircon/process.h>
2168d75effSDimitry Andric #include <zircon/sanitizer.h>
2268d75effSDimitry Andric #include <zircon/syscalls.h>
2368d75effSDimitry Andric 
2468d75effSDimitry Andric namespace scudo {
2568d75effSDimitry Andric 
26fe6060f1SDimitry Andric uptr getPageSize() { return _zx_system_get_page_size(); }
2768d75effSDimitry Andric 
2868d75effSDimitry Andric void NORETURN die() { __builtin_trap(); }
2968d75effSDimitry Andric 
3068d75effSDimitry Andric // We zero-initialize the Extra parameter of map(), make sure this is consistent
3168d75effSDimitry Andric // with ZX_HANDLE_INVALID.
32480093f4SDimitry Andric static_assert(ZX_HANDLE_INVALID == 0, "");
3368d75effSDimitry Andric 
3468d75effSDimitry Andric static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) {
3568d75effSDimitry Andric   // Only scenario so far.
3668d75effSDimitry Andric   DCHECK(Data);
3768d75effSDimitry Andric   DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID);
3868d75effSDimitry Andric 
3968d75effSDimitry Andric   const zx_status_t Status = _zx_vmar_allocate(
4068d75effSDimitry Andric       _zx_vmar_root_self(),
4168d75effSDimitry Andric       ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0,
4268d75effSDimitry Andric       Size, &Data->Vmar, &Data->VmarBase);
4368d75effSDimitry Andric   if (UNLIKELY(Status != ZX_OK)) {
4468d75effSDimitry Andric     if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
45fe6060f1SDimitry Andric       dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
4668d75effSDimitry Andric     return nullptr;
4768d75effSDimitry Andric   }
4868d75effSDimitry Andric   return reinterpret_cast<void *>(Data->VmarBase);
4968d75effSDimitry Andric }
5068d75effSDimitry Andric 
5168d75effSDimitry Andric void *map(void *Addr, uptr Size, const char *Name, uptr Flags,
5268d75effSDimitry Andric           MapPlatformData *Data) {
53fe6060f1SDimitry Andric   DCHECK_EQ(Size % getPageSizeCached(), 0);
5468d75effSDimitry Andric   const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM);
5568d75effSDimitry Andric 
5668d75effSDimitry Andric   // For MAP_NOACCESS, just allocate a Vmar and return.
5768d75effSDimitry Andric   if (Flags & MAP_NOACCESS)
5868d75effSDimitry Andric     return allocateVmar(Size, Data, AllowNoMem);
5968d75effSDimitry Andric 
6068d75effSDimitry Andric   const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
6168d75effSDimitry Andric   CHECK_NE(Vmar, ZX_HANDLE_INVALID);
6268d75effSDimitry Andric 
6368d75effSDimitry Andric   zx_status_t Status;
6468d75effSDimitry Andric   zx_handle_t Vmo;
6568d75effSDimitry Andric   uint64_t VmoSize = 0;
6668d75effSDimitry Andric   if (Data && Data->Vmo != ZX_HANDLE_INVALID) {
6768d75effSDimitry Andric     // If a Vmo was specified, it's a resize operation.
6868d75effSDimitry Andric     CHECK(Addr);
6968d75effSDimitry Andric     DCHECK(Flags & MAP_RESIZABLE);
7068d75effSDimitry Andric     Vmo = Data->Vmo;
7168d75effSDimitry Andric     VmoSize = Data->VmoSize;
7268d75effSDimitry Andric     Status = _zx_vmo_set_size(Vmo, VmoSize + Size);
7368d75effSDimitry Andric     if (Status != ZX_OK) {
7468d75effSDimitry Andric       if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
75fe6060f1SDimitry Andric         dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
7668d75effSDimitry Andric       return nullptr;
7768d75effSDimitry Andric     }
7868d75effSDimitry Andric   } else {
7968d75effSDimitry Andric     // Otherwise, create a Vmo and set its name.
8068d75effSDimitry Andric     Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo);
8168d75effSDimitry Andric     if (UNLIKELY(Status != ZX_OK)) {
8268d75effSDimitry Andric       if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
83fe6060f1SDimitry Andric         dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
8468d75effSDimitry Andric       return nullptr;
8568d75effSDimitry Andric     }
8668d75effSDimitry Andric     _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name));
8768d75effSDimitry Andric   }
8868d75effSDimitry Andric 
8968d75effSDimitry Andric   uintptr_t P;
9068d75effSDimitry Andric   zx_vm_option_t MapFlags =
9168d75effSDimitry Andric       ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS;
92*81ad6265SDimitry Andric   if (Addr)
93*81ad6265SDimitry Andric     DCHECK(Data);
9468d75effSDimitry Andric   const uint64_t Offset =
9568d75effSDimitry Andric       Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0;
9668d75effSDimitry Andric   if (Offset)
9768d75effSDimitry Andric     MapFlags |= ZX_VM_SPECIFIC;
9868d75effSDimitry Andric   Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P);
9968d75effSDimitry Andric   // No need to track the Vmo if we don't intend on resizing it. Close it.
10068d75effSDimitry Andric   if (Flags & MAP_RESIZABLE) {
10168d75effSDimitry Andric     DCHECK(Data);
102fe6060f1SDimitry Andric     if (Data->Vmo == ZX_HANDLE_INVALID)
10368d75effSDimitry Andric       Data->Vmo = Vmo;
104fe6060f1SDimitry Andric     else
105fe6060f1SDimitry Andric       DCHECK_EQ(Data->Vmo, Vmo);
10668d75effSDimitry Andric   } else {
10768d75effSDimitry Andric     CHECK_EQ(_zx_handle_close(Vmo), ZX_OK);
10868d75effSDimitry Andric   }
10968d75effSDimitry Andric   if (UNLIKELY(Status != ZX_OK)) {
11068d75effSDimitry Andric     if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem)
111fe6060f1SDimitry Andric       dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY ? Size : 0);
11268d75effSDimitry Andric     return nullptr;
11368d75effSDimitry Andric   }
11468d75effSDimitry Andric   if (Data)
11568d75effSDimitry Andric     Data->VmoSize += Size;
11668d75effSDimitry Andric 
11768d75effSDimitry Andric   return reinterpret_cast<void *>(P);
11868d75effSDimitry Andric }
11968d75effSDimitry Andric 
12068d75effSDimitry Andric void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) {
12168d75effSDimitry Andric   if (Flags & UNMAP_ALL) {
12268d75effSDimitry Andric     DCHECK_NE(Data, nullptr);
12368d75effSDimitry Andric     const zx_handle_t Vmar = Data->Vmar;
12468d75effSDimitry Andric     DCHECK_NE(Vmar, _zx_vmar_root_self());
12568d75effSDimitry Andric     // Destroying the vmar effectively unmaps the whole mapping.
12668d75effSDimitry Andric     CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK);
12768d75effSDimitry Andric     CHECK_EQ(_zx_handle_close(Vmar), ZX_OK);
12868d75effSDimitry Andric   } else {
12968d75effSDimitry Andric     const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self();
13068d75effSDimitry Andric     const zx_status_t Status =
13168d75effSDimitry Andric         _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size);
13268d75effSDimitry Andric     if (UNLIKELY(Status != ZX_OK))
13368d75effSDimitry Andric       dieOnMapUnmapError();
13468d75effSDimitry Andric   }
13568d75effSDimitry Andric   if (Data) {
13668d75effSDimitry Andric     if (Data->Vmo != ZX_HANDLE_INVALID)
13768d75effSDimitry Andric       CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK);
13868d75effSDimitry Andric     memset(Data, 0, sizeof(*Data));
13968d75effSDimitry Andric   }
14068d75effSDimitry Andric }
14168d75effSDimitry Andric 
142fe6060f1SDimitry Andric void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags,
143fe6060f1SDimitry Andric                          UNUSED MapPlatformData *Data) {
144fe6060f1SDimitry Andric   const zx_vm_option_t Prot =
145fe6060f1SDimitry Andric       (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE);
146fe6060f1SDimitry Andric   DCHECK(Data);
147fe6060f1SDimitry Andric   DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
148fe6060f1SDimitry Andric   if (_zx_vmar_protect(Data->Vmar, Prot, Addr, Size) != ZX_OK)
149fe6060f1SDimitry Andric     dieOnMapUnmapError();
150fe6060f1SDimitry Andric }
151fe6060f1SDimitry Andric 
15268d75effSDimitry Andric void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size,
15368d75effSDimitry Andric                       MapPlatformData *Data) {
15468d75effSDimitry Andric   DCHECK(Data);
15568d75effSDimitry Andric   DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID);
15668d75effSDimitry Andric   DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID);
15768d75effSDimitry Andric   const zx_status_t Status =
15868d75effSDimitry Andric       _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0);
15968d75effSDimitry Andric   CHECK_EQ(Status, ZX_OK);
16068d75effSDimitry Andric }
16168d75effSDimitry Andric 
16268d75effSDimitry Andric const char *getEnv(const char *Name) { return getenv(Name); }
16368d75effSDimitry Andric 
16468d75effSDimitry Andric // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS
16568d75effSDimitry Andric // because the Fuchsia implementation of sync_mutex_t has clang thread safety
16668d75effSDimitry Andric // annotations. Were we to apply proper capability annotations to the top level
16768d75effSDimitry Andric // HybridMutex class itself, they would not be needed. As it stands, the
16868d75effSDimitry Andric // thread analysis thinks that we are locking the mutex and accidentally leaving
16968d75effSDimitry Andric // it locked on the way out.
17068d75effSDimitry Andric bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS {
17168d75effSDimitry Andric   // Size and alignment must be compatible between both types.
17268d75effSDimitry Andric   return sync_mutex_trylock(&M) == ZX_OK;
17368d75effSDimitry Andric }
17468d75effSDimitry Andric 
17568d75effSDimitry Andric void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS {
17668d75effSDimitry Andric   sync_mutex_lock(&M);
17768d75effSDimitry Andric }
17868d75effSDimitry Andric 
17968d75effSDimitry Andric void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS {
18068d75effSDimitry Andric   sync_mutex_unlock(&M);
18168d75effSDimitry Andric }
18268d75effSDimitry Andric 
18368d75effSDimitry Andric u64 getMonotonicTime() { return _zx_clock_get_monotonic(); }
18468d75effSDimitry Andric 
18568d75effSDimitry Andric u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); }
18668d75effSDimitry Andric 
1875ffd83dbSDimitry Andric u32 getThreadID() { return 0; }
1885ffd83dbSDimitry Andric 
18968d75effSDimitry Andric bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) {
190480093f4SDimitry Andric   static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, "");
19168d75effSDimitry Andric   if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength))
19268d75effSDimitry Andric     return false;
19368d75effSDimitry Andric   _zx_cprng_draw(Buffer, Length);
19468d75effSDimitry Andric   return true;
19568d75effSDimitry Andric }
19668d75effSDimitry Andric 
19768d75effSDimitry Andric void outputRaw(const char *Buffer) {
19868d75effSDimitry Andric   __sanitizer_log_write(Buffer, strlen(Buffer));
19968d75effSDimitry Andric }
20068d75effSDimitry Andric 
20168d75effSDimitry Andric void setAbortMessage(const char *Message) {}
20268d75effSDimitry Andric 
20368d75effSDimitry Andric } // namespace scudo
20468d75effSDimitry Andric 
20568d75effSDimitry Andric #endif // SCUDO_FUCHSIA
206