132 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef SCUDO_MEM_MAP_BASE_H_10#define SCUDO_MEM_MAP_BASE_H_11 12#include "common.h"13 14namespace scudo {15 16// In Scudo, every memory operation will be fulfilled through a17// platform-specific `MemMap` instance. The essential APIs are listed in the18// `MemMapBase` below. This is implemented in CRTP, so for each implementation,19// it has to implement all of the 'Impl' named functions.20template <class Derived> class MemMapBase {21public:22 constexpr MemMapBase() = default;23 24 // This is used to map a new set of contiguous pages. Note that the `Addr` is25 // 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.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.39 void unmap() { unmap(getBase(), getCapacity()); }40 41 // This is used to remap a mapped range (either from map() or dispatched from42 // ReservedMemory). For example, we have reserved several pages and then we43 // 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, mark51 // pages as no read/write permission.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. Note59 // that only physical pages are supposed to be released. Any release of60 // virtual pages may lead to undefined behavior.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 the67 // released pages will return with zero-filled pages.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 74 uptr getBase() { return invokeImpl(&Derived::getBaseImpl); }75 uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); }76 77 bool isAllocated() { return getBase() != 0U; }78 79protected:80 template <typename R, typename... 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 page87// allocator. `ReservedMemory` will reserve a contiguous pages and the later88// page request can be fulfilled at the designated address. This is used when89// we want to ensure the virtual address of the MemMap will be in a known range.90// This is implemented in CRTP, so for each91// implementation, it has to implement all of the 'Impl' named functions.92template <class Derived, typename MemMapTy> class ReservedMemory {93public: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.104 void release() {105 DCHECK(isCreated());106 invokeImpl(&Derived::releaseImpl);107 }108 109 // Dispatch a sub-range of reserved memory. Note that any fragmentation of110 // the reserved pages is managed by each implementation.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 117 uptr getBase() { return invokeImpl(&Derived::getBaseImpl); }118 uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); }119 120 bool isCreated() { return getBase() != 0U; }121 122protected:123 template <typename R, typename... Args>124 R invokeImpl(R (Derived::*MemFn)(Args...), Args... args) {125 return (static_cast<Derived *>(this)->*MemFn)(args...);126 }127};128 129} // namespace scudo130 131#endif // SCUDO_MEM_MAP_BASE_H_132