1 //===-- mem_map_base.h ------------------------------------------*- 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 #ifndef SCUDO_MEM_MAP_BASE_H_ 10 #define SCUDO_MEM_MAP_BASE_H_ 11 12 #include "common.h" 13 14 namespace scudo { 15 16 // In Scudo, every memory operation will be fulfilled through a 17 // platform-specific `MemMap` instance. The essential APIs are listed in the 18 // `MemMapBase` below. This is implemented in CRTP, so for each implementation, 19 // it has to implement all of the 'Impl' named functions. 20 template <class Derived> class MemMapBase { 21 public: 22 constexpr MemMapBase() = default; 23 24 // This is used to map a new set of contiguous pages. Note that the `Addr` is 25 // only a suggestion to the system. 26 bool map(uptr Addr, uptr Size, const char *Name, uptr Flags = 0) { 27 DCHECK(!isAllocated()); 28 return invokeImpl(&Derived::mapImpl, Addr, Size, Name, Flags); 29 } 30 31 // This is used to unmap partial/full pages from the beginning or the end. 32 // I.e., the result pages are expected to be still contiguous. unmap(uptr Addr,uptr Size)33 void unmap(uptr Addr, uptr Size) { 34 DCHECK(isAllocated()); 35 DCHECK((Addr == getBase()) || (Addr + Size == getBase() + getCapacity())); 36 invokeImpl(&Derived::unmapImpl, Addr, Size); 37 } 38 // A default implementation to unmap all pages. unmap()39 void unmap() { unmap(getBase(), getCapacity()); } 40 41 // This is used to remap a mapped range (either from map() or dispatched from 42 // ReservedMemory). For example, we have reserved several pages and then we 43 // want to remap them with different accessibility. 44 bool remap(uptr Addr, uptr Size, const char *Name, uptr Flags = 0) { 45 DCHECK(isAllocated()); 46 DCHECK((Addr >= getBase()) && (Addr + Size <= getBase() + getCapacity())); 47 return invokeImpl(&Derived::remapImpl, Addr, Size, Name, Flags); 48 } 49 50 // This is used to update the pages' access permission. For example, mark 51 // pages as no read/write permission. setMemoryPermission(uptr Addr,uptr Size,uptr Flags)52 void setMemoryPermission(uptr Addr, uptr Size, uptr Flags) { 53 DCHECK(isAllocated()); 54 DCHECK((Addr >= getBase()) && (Addr + Size <= getBase() + getCapacity())); 55 return invokeImpl(&Derived::setMemoryPermissionImpl, Addr, Size, Flags); 56 } 57 58 // Suggest releasing a set of contiguous physical pages back to the OS. Note 59 // that only physical pages are supposed to be released. Any release of 60 // virtual pages may lead to undefined behavior. releasePagesToOS(uptr From,uptr Size)61 void releasePagesToOS(uptr From, uptr Size) { 62 DCHECK(isAllocated()); 63 DCHECK((From >= getBase()) && (From + Size <= getBase() + getCapacity())); 64 invokeImpl(&Derived::releasePagesToOSImpl, From, Size); 65 } 66 // This is similar to the above one except that any subsequent access to the 67 // released pages will return with zero-filled pages. releaseAndZeroPagesToOS(uptr From,uptr Size)68 void releaseAndZeroPagesToOS(uptr From, uptr Size) { 69 DCHECK(isAllocated()); 70 DCHECK((From >= getBase()) && (From + Size <= getBase() + getCapacity())); 71 invokeImpl(&Derived::releaseAndZeroPagesToOSImpl, From, Size); 72 } 73 getBase()74 uptr getBase() { return invokeImpl(&Derived::getBaseImpl); } getCapacity()75 uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); } 76 isAllocated()77 bool isAllocated() { return getBase() != 0U; } 78 79 protected: 80 template <typename R, typename... Args> invokeImpl(R (Derived::* MemFn)(Args...),Args...args)81 R invokeImpl(R (Derived::*MemFn)(Args...), Args... args) { 82 return (static_cast<Derived *>(this)->*MemFn)(args...); 83 } 84 }; 85 86 // `ReservedMemory` is a special memory handle which can be viewed as a page 87 // allocator. `ReservedMemory` will reserve a contiguous pages and the later 88 // page request can be fulfilled at the designated address. This is used when 89 // we want to ensure the virtual address of the MemMap will be in a known range. 90 // This is implemented in CRTP, so for each 91 // implementation, it has to implement all of the 'Impl' named functions. 92 template <class Derived, typename MemMapTy> class ReservedMemory { 93 public: 94 using MemMapT = MemMapTy; 95 constexpr ReservedMemory() = default; 96 97 // Reserve a chunk of memory at a suggested address. 98 bool create(uptr Addr, uptr Size, const char *Name, uptr Flags = 0) { 99 DCHECK(!isCreated()); 100 return invokeImpl(&Derived::createImpl, Addr, Size, Name, Flags); 101 } 102 103 // Release the entire reserved memory. release()104 void release() { 105 DCHECK(isCreated()); 106 invokeImpl(&Derived::releaseImpl); 107 } 108 109 // Dispatch a sub-range of reserved memory. Note that any fragmentation of 110 // the reserved pages is managed by each implementation. dispatch(uptr Addr,uptr Size)111 MemMapT dispatch(uptr Addr, uptr Size) { 112 DCHECK(isCreated()); 113 DCHECK((Addr >= getBase()) && (Addr + Size <= getBase() + getCapacity())); 114 return invokeImpl(&Derived::dispatchImpl, Addr, Size); 115 } 116 getBase()117 uptr getBase() { return invokeImpl(&Derived::getBaseImpl); } getCapacity()118 uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); } 119 isCreated()120 bool isCreated() { return getBase() != 0U; } 121 122 protected: 123 template <typename R, typename... Args> invokeImpl(R (Derived::* MemFn)(Args...),Args...args)124 R invokeImpl(R (Derived::*MemFn)(Args...), Args... args) { 125 return (static_cast<Derived *>(this)->*MemFn)(args...); 126 } 127 }; 128 129 } // namespace scudo 130 131 #endif // SCUDO_MEM_MAP_BASE_H_ 132