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 <limits.h> // for PAGE_SIZE 1968d75effSDimitry Andric #include <stdlib.h> // for getenv() 2068d75effSDimitry Andric #include <zircon/compiler.h> 2168d75effSDimitry Andric #include <zircon/sanitizer.h> 2268d75effSDimitry Andric #include <zircon/syscalls.h> 2368d75effSDimitry Andric 2468d75effSDimitry Andric namespace scudo { 2568d75effSDimitry Andric 2668d75effSDimitry Andric uptr getPageSize() { return 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) 4568d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 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) { 5368d75effSDimitry Andric DCHECK_EQ(Size % PAGE_SIZE, 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) 7568d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 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) 8368d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 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; 9268d75effSDimitry Andric const uint64_t Offset = 9368d75effSDimitry Andric Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0; 9468d75effSDimitry Andric if (Offset) 9568d75effSDimitry Andric MapFlags |= ZX_VM_SPECIFIC; 9668d75effSDimitry Andric Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P); 9768d75effSDimitry Andric // No need to track the Vmo if we don't intend on resizing it. Close it. 9868d75effSDimitry Andric if (Flags & MAP_RESIZABLE) { 9968d75effSDimitry Andric DCHECK(Data); 10068d75effSDimitry Andric DCHECK_EQ(Data->Vmo, ZX_HANDLE_INVALID); 10168d75effSDimitry Andric Data->Vmo = Vmo; 10268d75effSDimitry Andric } else { 10368d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmo), ZX_OK); 10468d75effSDimitry Andric } 10568d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 10668d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 10768d75effSDimitry Andric dieOnMapUnmapError(Status == ZX_ERR_NO_MEMORY); 10868d75effSDimitry Andric return nullptr; 10968d75effSDimitry Andric } 11068d75effSDimitry Andric if (Data) 11168d75effSDimitry Andric Data->VmoSize += Size; 11268d75effSDimitry Andric 11368d75effSDimitry Andric return reinterpret_cast<void *>(P); 11468d75effSDimitry Andric } 11568d75effSDimitry Andric 11668d75effSDimitry Andric void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) { 11768d75effSDimitry Andric if (Flags & UNMAP_ALL) { 11868d75effSDimitry Andric DCHECK_NE(Data, nullptr); 11968d75effSDimitry Andric const zx_handle_t Vmar = Data->Vmar; 12068d75effSDimitry Andric DCHECK_NE(Vmar, _zx_vmar_root_self()); 12168d75effSDimitry Andric // Destroying the vmar effectively unmaps the whole mapping. 12268d75effSDimitry Andric CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK); 12368d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmar), ZX_OK); 12468d75effSDimitry Andric } else { 12568d75effSDimitry Andric const zx_handle_t Vmar = Data ? Data->Vmar : _zx_vmar_root_self(); 12668d75effSDimitry Andric const zx_status_t Status = 12768d75effSDimitry Andric _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size); 12868d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) 12968d75effSDimitry Andric dieOnMapUnmapError(); 13068d75effSDimitry Andric } 13168d75effSDimitry Andric if (Data) { 13268d75effSDimitry Andric if (Data->Vmo != ZX_HANDLE_INVALID) 13368d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK); 13468d75effSDimitry Andric memset(Data, 0, sizeof(*Data)); 13568d75effSDimitry Andric } 13668d75effSDimitry Andric } 13768d75effSDimitry Andric 13868d75effSDimitry Andric void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size, 13968d75effSDimitry Andric MapPlatformData *Data) { 14068d75effSDimitry Andric DCHECK(Data); 14168d75effSDimitry Andric DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); 14268d75effSDimitry Andric DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID); 14368d75effSDimitry Andric const zx_status_t Status = 14468d75effSDimitry Andric _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0); 14568d75effSDimitry Andric CHECK_EQ(Status, ZX_OK); 14668d75effSDimitry Andric } 14768d75effSDimitry Andric 14868d75effSDimitry Andric const char *getEnv(const char *Name) { return getenv(Name); } 14968d75effSDimitry Andric 15068d75effSDimitry Andric // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS 15168d75effSDimitry Andric // because the Fuchsia implementation of sync_mutex_t has clang thread safety 15268d75effSDimitry Andric // annotations. Were we to apply proper capability annotations to the top level 15368d75effSDimitry Andric // HybridMutex class itself, they would not be needed. As it stands, the 15468d75effSDimitry Andric // thread analysis thinks that we are locking the mutex and accidentally leaving 15568d75effSDimitry Andric // it locked on the way out. 15668d75effSDimitry Andric bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS { 15768d75effSDimitry Andric // Size and alignment must be compatible between both types. 15868d75effSDimitry Andric return sync_mutex_trylock(&M) == ZX_OK; 15968d75effSDimitry Andric } 16068d75effSDimitry Andric 16168d75effSDimitry Andric void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS { 16268d75effSDimitry Andric sync_mutex_lock(&M); 16368d75effSDimitry Andric } 16468d75effSDimitry Andric 16568d75effSDimitry Andric void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { 16668d75effSDimitry Andric sync_mutex_unlock(&M); 16768d75effSDimitry Andric } 16868d75effSDimitry Andric 16968d75effSDimitry Andric u64 getMonotonicTime() { return _zx_clock_get_monotonic(); } 17068d75effSDimitry Andric 17168d75effSDimitry Andric u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); } 17268d75effSDimitry Andric 173*5ffd83dbSDimitry Andric u32 getThreadID() { return 0; } 174*5ffd83dbSDimitry Andric 17568d75effSDimitry Andric bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) { 176480093f4SDimitry Andric static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, ""); 17768d75effSDimitry Andric if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength)) 17868d75effSDimitry Andric return false; 17968d75effSDimitry Andric _zx_cprng_draw(Buffer, Length); 18068d75effSDimitry Andric return true; 18168d75effSDimitry Andric } 18268d75effSDimitry Andric 18368d75effSDimitry Andric void outputRaw(const char *Buffer) { 18468d75effSDimitry Andric __sanitizer_log_write(Buffer, strlen(Buffer)); 18568d75effSDimitry Andric } 18668d75effSDimitry Andric 18768d75effSDimitry Andric void setAbortMessage(const char *Message) {} 18868d75effSDimitry Andric 18968d75effSDimitry Andric } // namespace scudo 19068d75effSDimitry Andric 19168d75effSDimitry Andric #endif // SCUDO_FUCHSIA 192