1 //===-- guarded_pool_allocator_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 "gwp_asan/guarded_pool_allocator.h" 10 #include "gwp_asan/utilities.h" 11 12 #include <assert.h> 13 #include <stdint.h> 14 #include <string.h> 15 #include <zircon/limits.h> 16 #include <zircon/process.h> 17 #include <zircon/syscalls.h> 18 19 namespace gwp_asan { 20 void GuardedPoolAllocator::initPRNG() { 21 _zx_cprng_draw(&getThreadLocals()->RandomState, sizeof(uint32_t)); 22 } 23 24 void *GuardedPoolAllocator::map(size_t Size, const char *Name) const { 25 assert((Size % State.PageSize) == 0); 26 zx_handle_t Vmo; 27 zx_status_t Status = _zx_vmo_create(Size, 0, &Vmo); 28 Check(Status == ZX_OK, "Failed to create Vmo"); 29 _zx_object_set_property(Vmo, ZX_PROP_NAME, Name, strlen(Name)); 30 zx_vaddr_t Addr; 31 Status = _zx_vmar_map(_zx_vmar_root_self(), 32 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | ZX_VM_ALLOW_FAULTS, 33 0, Vmo, 0, Size, &Addr); 34 Check(Status == ZX_OK, "Vmo mapping failed"); 35 _zx_handle_close(Vmo); 36 return reinterpret_cast<void *>(Addr); 37 } 38 39 void GuardedPoolAllocator::unmap(void *Ptr, size_t Size) const { 40 assert((reinterpret_cast<uintptr_t>(Ptr) % State.PageSize) == 0); 41 assert((Size % State.PageSize) == 0); 42 zx_status_t Status = _zx_vmar_unmap(_zx_vmar_root_self(), 43 reinterpret_cast<zx_vaddr_t>(Ptr), Size); 44 Check(Status == ZX_OK, "Vmo unmapping failed"); 45 } 46 47 void *GuardedPoolAllocator::reserveGuardedPool(size_t Size) { 48 assert((Size % State.PageSize) == 0); 49 zx_vaddr_t Addr; 50 const zx_status_t Status = _zx_vmar_allocate( 51 _zx_vmar_root_self(), 52 ZX_VM_CAN_MAP_READ | ZX_VM_CAN_MAP_WRITE | ZX_VM_CAN_MAP_SPECIFIC, 0, 53 Size, &GuardedPagePoolPlatformData.Vmar, &Addr); 54 Check(Status == ZX_OK, "Failed to reserve guarded pool allocator memory"); 55 _zx_object_set_property(GuardedPagePoolPlatformData.Vmar, ZX_PROP_NAME, 56 kGwpAsanGuardPageName, strlen(kGwpAsanGuardPageName)); 57 return reinterpret_cast<void *>(Addr); 58 } 59 60 void GuardedPoolAllocator::unreserveGuardedPool() { 61 const zx_handle_t Vmar = GuardedPagePoolPlatformData.Vmar; 62 assert(Vmar != ZX_HANDLE_INVALID && Vmar != _zx_vmar_root_self()); 63 Check(_zx_vmar_destroy(Vmar) == ZX_OK, "Failed to destroy a vmar"); 64 Check(_zx_handle_close(Vmar) == ZX_OK, "Failed to close a vmar"); 65 GuardedPagePoolPlatformData.Vmar = ZX_HANDLE_INVALID; 66 } 67 68 void GuardedPoolAllocator::allocateInGuardedPool(void *Ptr, size_t Size) const { 69 assert((reinterpret_cast<uintptr_t>(Ptr) % State.PageSize) == 0); 70 assert((Size % State.PageSize) == 0); 71 zx_handle_t Vmo; 72 zx_status_t Status = _zx_vmo_create(Size, 0, &Vmo); 73 Check(Status == ZX_OK, "Failed to create vmo"); 74 _zx_object_set_property(Vmo, ZX_PROP_NAME, kGwpAsanAliveSlotName, 75 strlen(kGwpAsanAliveSlotName)); 76 const zx_handle_t Vmar = GuardedPagePoolPlatformData.Vmar; 77 assert(Vmar != ZX_HANDLE_INVALID && Vmar != _zx_vmar_root_self()); 78 const size_t Offset = 79 reinterpret_cast<uintptr_t>(Ptr) - State.GuardedPagePool; 80 zx_vaddr_t P; 81 Status = _zx_vmar_map(Vmar, 82 ZX_VM_PERM_READ | ZX_VM_PERM_WRITE | 83 ZX_VM_ALLOW_FAULTS | ZX_VM_SPECIFIC, 84 Offset, Vmo, 0, Size, &P); 85 Check(Status == ZX_OK, "Vmo mapping failed"); 86 _zx_handle_close(Vmo); 87 } 88 89 void GuardedPoolAllocator::deallocateInGuardedPool(void *Ptr, 90 size_t Size) const { 91 assert((reinterpret_cast<uintptr_t>(Ptr) % State.PageSize) == 0); 92 assert((Size % State.PageSize) == 0); 93 const zx_handle_t Vmar = GuardedPagePoolPlatformData.Vmar; 94 assert(Vmar != ZX_HANDLE_INVALID && Vmar != _zx_vmar_root_self()); 95 const zx_status_t Status = 96 _zx_vmar_unmap(Vmar, reinterpret_cast<zx_vaddr_t>(Ptr), Size); 97 Check(Status == ZX_OK, "Vmar unmapping failed"); 98 } 99 100 size_t GuardedPoolAllocator::getPlatformPageSize() { return ZX_PAGE_SIZE; } 101 102 void GuardedPoolAllocator::installAtFork() {} 103 } // namespace gwp_asan 104