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> 2206c3fb27SDimitry Andric #include <zircon/status.h> 2368d75effSDimitry Andric #include <zircon/syscalls.h> 2468d75effSDimitry Andric 2568d75effSDimitry Andric namespace scudo { 2668d75effSDimitry Andric 27fe6060f1SDimitry Andric uptr getPageSize() { return _zx_system_get_page_size(); } 2868d75effSDimitry Andric 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 3506c3fb27SDimitry Andric static void NORETURN dieOnError(zx_status_t Status, const char *FnName, 3606c3fb27SDimitry Andric uptr Size) { 37*0fca6ea1SDimitry Andric ScopedString Error; 38*0fca6ea1SDimitry Andric Error.append("SCUDO ERROR: %s failed with size %zuKB (%s)", FnName, 3906c3fb27SDimitry Andric Size >> 10, zx_status_get_string(Status)); 40*0fca6ea1SDimitry Andric outputRaw(Error.data()); 4106c3fb27SDimitry Andric die(); 4206c3fb27SDimitry Andric } 4306c3fb27SDimitry Andric 4468d75effSDimitry Andric static void *allocateVmar(uptr Size, MapPlatformData *Data, bool AllowNoMem) { 4568d75effSDimitry Andric // Only scenario so far. 4668d75effSDimitry Andric DCHECK(Data); 4768d75effSDimitry Andric DCHECK_EQ(Data->Vmar, ZX_HANDLE_INVALID); 4868d75effSDimitry Andric 4968d75effSDimitry Andric const zx_status_t Status = _zx_vmar_allocate( 5068d75effSDimitry Andric _zx_vmar_root_self(), 5168d75effSDimitry Andric ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0, 5268d75effSDimitry Andric Size, &Data->Vmar, &Data->VmarBase); 5368d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 5468d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 5506c3fb27SDimitry Andric dieOnError(Status, "zx_vmar_allocate", Size); 5668d75effSDimitry Andric return nullptr; 5768d75effSDimitry Andric } 5868d75effSDimitry Andric return reinterpret_cast<void *>(Data->VmarBase); 5968d75effSDimitry Andric } 6068d75effSDimitry Andric 6168d75effSDimitry Andric void *map(void *Addr, uptr Size, const char *Name, uptr Flags, 6268d75effSDimitry Andric MapPlatformData *Data) { 63fe6060f1SDimitry Andric DCHECK_EQ(Size % getPageSizeCached(), 0); 6468d75effSDimitry Andric const bool AllowNoMem = !!(Flags & MAP_ALLOWNOMEM); 6568d75effSDimitry Andric 6668d75effSDimitry Andric // For MAP_NOACCESS, just allocate a Vmar and return. 6768d75effSDimitry Andric if (Flags & MAP_NOACCESS) 6868d75effSDimitry Andric return allocateVmar(Size, Data, AllowNoMem); 6968d75effSDimitry Andric 70753f127fSDimitry Andric const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID) 71753f127fSDimitry Andric ? Data->Vmar 72753f127fSDimitry Andric : _zx_vmar_root_self(); 7368d75effSDimitry Andric 7468d75effSDimitry Andric zx_status_t Status; 7568d75effSDimitry Andric zx_handle_t Vmo; 7668d75effSDimitry Andric uint64_t VmoSize = 0; 7768d75effSDimitry Andric if (Data && Data->Vmo != ZX_HANDLE_INVALID) { 7868d75effSDimitry Andric // If a Vmo was specified, it's a resize operation. 7968d75effSDimitry Andric CHECK(Addr); 8068d75effSDimitry Andric DCHECK(Flags & MAP_RESIZABLE); 8168d75effSDimitry Andric Vmo = Data->Vmo; 8268d75effSDimitry Andric VmoSize = Data->VmoSize; 8368d75effSDimitry Andric Status = _zx_vmo_set_size(Vmo, VmoSize + Size); 8468d75effSDimitry Andric if (Status != ZX_OK) { 8568d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 8606c3fb27SDimitry Andric dieOnError(Status, "zx_vmo_set_size", VmoSize + Size); 8768d75effSDimitry Andric return nullptr; 8868d75effSDimitry Andric } 8968d75effSDimitry Andric } else { 9068d75effSDimitry Andric // Otherwise, create a Vmo and set its name. 9168d75effSDimitry Andric Status = _zx_vmo_create(Size, ZX_VMO_RESIZABLE, &Vmo); 9268d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 9368d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 9406c3fb27SDimitry Andric dieOnError(Status, "zx_vmo_create", Size); 9568d75effSDimitry Andric return nullptr; 9668d75effSDimitry Andric } 9768d75effSDimitry Andric _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name)); 9868d75effSDimitry Andric } 9968d75effSDimitry Andric 10068d75effSDimitry Andric uintptr_t P; 10168d75effSDimitry Andric zx_vm_option_t MapFlags = 10268d75effSDimitry Andric ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS; 10381ad6265SDimitry Andric if (Addr) 10481ad6265SDimitry Andric DCHECK(Data); 10568d75effSDimitry Andric const uint64_t Offset = 10668d75effSDimitry Andric Addr ? reinterpret_cast<uintptr_t>(Addr) - Data->VmarBase : 0; 10768d75effSDimitry Andric if (Offset) 10868d75effSDimitry Andric MapFlags |= ZX_VM_SPECIFIC; 10968d75effSDimitry Andric Status = _zx_vmar_map(Vmar, MapFlags, Offset, Vmo, VmoSize, Size, &P); 110bdd1243dSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 111bdd1243dSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 11206c3fb27SDimitry Andric dieOnError(Status, "zx_vmar_map", Size); 113bdd1243dSDimitry Andric return nullptr; 114bdd1243dSDimitry Andric } 115bdd1243dSDimitry Andric 116bdd1243dSDimitry Andric if (Flags & MAP_PRECOMMIT) { 117bdd1243dSDimitry Andric Status = _zx_vmar_op_range(Vmar, ZX_VMAR_OP_COMMIT, P, Size, 118bdd1243dSDimitry Andric /*buffer=*/nullptr, /*buffer_size=*/0); 119bdd1243dSDimitry Andric } 120bdd1243dSDimitry Andric 12168d75effSDimitry Andric // No need to track the Vmo if we don't intend on resizing it. Close it. 12268d75effSDimitry Andric if (Flags & MAP_RESIZABLE) { 12368d75effSDimitry Andric DCHECK(Data); 124fe6060f1SDimitry Andric if (Data->Vmo == ZX_HANDLE_INVALID) 12568d75effSDimitry Andric Data->Vmo = Vmo; 126fe6060f1SDimitry Andric else 127fe6060f1SDimitry Andric DCHECK_EQ(Data->Vmo, Vmo); 12868d75effSDimitry Andric } else { 12968d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmo), ZX_OK); 13068d75effSDimitry Andric } 13168d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) { 13268d75effSDimitry Andric if (Status != ZX_ERR_NO_MEMORY || !AllowNoMem) 13306c3fb27SDimitry Andric dieOnError(Status, "zx_vmar_op_range", Size); 13468d75effSDimitry Andric return nullptr; 13568d75effSDimitry Andric } 136bdd1243dSDimitry Andric 13768d75effSDimitry Andric if (Data) 13868d75effSDimitry Andric Data->VmoSize += Size; 13968d75effSDimitry Andric 14068d75effSDimitry Andric return reinterpret_cast<void *>(P); 14168d75effSDimitry Andric } 14268d75effSDimitry Andric 14368d75effSDimitry Andric void unmap(void *Addr, uptr Size, uptr Flags, MapPlatformData *Data) { 14468d75effSDimitry Andric if (Flags & UNMAP_ALL) { 14568d75effSDimitry Andric DCHECK_NE(Data, nullptr); 14668d75effSDimitry Andric const zx_handle_t Vmar = Data->Vmar; 14768d75effSDimitry Andric DCHECK_NE(Vmar, _zx_vmar_root_self()); 14868d75effSDimitry Andric // Destroying the vmar effectively unmaps the whole mapping. 14968d75effSDimitry Andric CHECK_EQ(_zx_vmar_destroy(Vmar), ZX_OK); 15068d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Vmar), ZX_OK); 15168d75effSDimitry Andric } else { 152753f127fSDimitry Andric const zx_handle_t Vmar = (Data && Data->Vmar != ZX_HANDLE_INVALID) 153753f127fSDimitry Andric ? Data->Vmar 154753f127fSDimitry Andric : _zx_vmar_root_self(); 15568d75effSDimitry Andric const zx_status_t Status = 15668d75effSDimitry Andric _zx_vmar_unmap(Vmar, reinterpret_cast<uintptr_t>(Addr), Size); 15768d75effSDimitry Andric if (UNLIKELY(Status != ZX_OK)) 15806c3fb27SDimitry Andric dieOnError(Status, "zx_vmar_unmap", Size); 15968d75effSDimitry Andric } 16068d75effSDimitry Andric if (Data) { 16168d75effSDimitry Andric if (Data->Vmo != ZX_HANDLE_INVALID) 16268d75effSDimitry Andric CHECK_EQ(_zx_handle_close(Data->Vmo), ZX_OK); 16368d75effSDimitry Andric memset(Data, 0, sizeof(*Data)); 16468d75effSDimitry Andric } 16568d75effSDimitry Andric } 16668d75effSDimitry Andric 167fe6060f1SDimitry Andric void setMemoryPermission(UNUSED uptr Addr, UNUSED uptr Size, UNUSED uptr Flags, 168fe6060f1SDimitry Andric UNUSED MapPlatformData *Data) { 169fe6060f1SDimitry Andric const zx_vm_option_t Prot = 170fe6060f1SDimitry Andric (Flags & MAP_NOACCESS) ? 0 : (ZX_VM_PERM_READ | ZX_VM_PERM_WRITE); 171fe6060f1SDimitry Andric DCHECK(Data); 172fe6060f1SDimitry Andric DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); 17306c3fb27SDimitry Andric const zx_status_t Status = _zx_vmar_protect(Data->Vmar, Prot, Addr, Size); 17406c3fb27SDimitry Andric if (Status != ZX_OK) 17506c3fb27SDimitry Andric dieOnError(Status, "zx_vmar_protect", Size); 176fe6060f1SDimitry Andric } 177fe6060f1SDimitry Andric 17868d75effSDimitry Andric void releasePagesToOS(UNUSED uptr BaseAddress, uptr Offset, uptr Size, 17968d75effSDimitry Andric MapPlatformData *Data) { 18006c3fb27SDimitry Andric // TODO: DCHECK the BaseAddress is consistent with the data in 18106c3fb27SDimitry Andric // MapPlatformData. 18268d75effSDimitry Andric DCHECK(Data); 18368d75effSDimitry Andric DCHECK_NE(Data->Vmar, ZX_HANDLE_INVALID); 18468d75effSDimitry Andric DCHECK_NE(Data->Vmo, ZX_HANDLE_INVALID); 18568d75effSDimitry Andric const zx_status_t Status = 18668d75effSDimitry Andric _zx_vmo_op_range(Data->Vmo, ZX_VMO_OP_DECOMMIT, Offset, Size, NULL, 0); 18768d75effSDimitry Andric CHECK_EQ(Status, ZX_OK); 18868d75effSDimitry Andric } 18968d75effSDimitry Andric 19068d75effSDimitry Andric const char *getEnv(const char *Name) { return getenv(Name); } 19168d75effSDimitry Andric 19268d75effSDimitry Andric // Note: we need to flag these methods with __TA_NO_THREAD_SAFETY_ANALYSIS 19368d75effSDimitry Andric // because the Fuchsia implementation of sync_mutex_t has clang thread safety 19468d75effSDimitry Andric // annotations. Were we to apply proper capability annotations to the top level 19568d75effSDimitry Andric // HybridMutex class itself, they would not be needed. As it stands, the 19668d75effSDimitry Andric // thread analysis thinks that we are locking the mutex and accidentally leaving 19768d75effSDimitry Andric // it locked on the way out. 19868d75effSDimitry Andric bool HybridMutex::tryLock() __TA_NO_THREAD_SAFETY_ANALYSIS { 19968d75effSDimitry Andric // Size and alignment must be compatible between both types. 20068d75effSDimitry Andric return sync_mutex_trylock(&M) == ZX_OK; 20168d75effSDimitry Andric } 20268d75effSDimitry Andric 20368d75effSDimitry Andric void HybridMutex::lockSlow() __TA_NO_THREAD_SAFETY_ANALYSIS { 20468d75effSDimitry Andric sync_mutex_lock(&M); 20568d75effSDimitry Andric } 20668d75effSDimitry Andric 20768d75effSDimitry Andric void HybridMutex::unlock() __TA_NO_THREAD_SAFETY_ANALYSIS { 20868d75effSDimitry Andric sync_mutex_unlock(&M); 20968d75effSDimitry Andric } 21068d75effSDimitry Andric 21106c3fb27SDimitry Andric void HybridMutex::assertHeldImpl() __TA_NO_THREAD_SAFETY_ANALYSIS {} 21206c3fb27SDimitry Andric 21368d75effSDimitry Andric u64 getMonotonicTime() { return _zx_clock_get_monotonic(); } 21406c3fb27SDimitry Andric u64 getMonotonicTimeFast() { return _zx_clock_get_monotonic(); } 21568d75effSDimitry Andric 21668d75effSDimitry Andric u32 getNumberOfCPUs() { return _zx_system_get_num_cpus(); } 21768d75effSDimitry Andric 2185ffd83dbSDimitry Andric u32 getThreadID() { return 0; } 2195ffd83dbSDimitry Andric 22068d75effSDimitry Andric bool getRandom(void *Buffer, uptr Length, UNUSED bool Blocking) { 221480093f4SDimitry Andric static_assert(MaxRandomLength <= ZX_CPRNG_DRAW_MAX_LEN, ""); 22268d75effSDimitry Andric if (UNLIKELY(!Buffer || !Length || Length > MaxRandomLength)) 22368d75effSDimitry Andric return false; 22468d75effSDimitry Andric _zx_cprng_draw(Buffer, Length); 22568d75effSDimitry Andric return true; 22668d75effSDimitry Andric } 22768d75effSDimitry Andric 22868d75effSDimitry Andric void outputRaw(const char *Buffer) { 22968d75effSDimitry Andric __sanitizer_log_write(Buffer, strlen(Buffer)); 23068d75effSDimitry Andric } 23168d75effSDimitry Andric 23268d75effSDimitry Andric void setAbortMessage(const char *Message) {} 23368d75effSDimitry Andric 23468d75effSDimitry Andric } // namespace scudo 23568d75effSDimitry Andric 23668d75effSDimitry Andric #endif // SCUDO_FUCHSIA 237