• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
39   // This is used to remap a mapped range (either from map() or dispatched from
40   // ReservedMemory). For example, we have reserved several pages and then we
41   // want to remap them with different accessibility.
42   bool remap(uptr Addr, uptr Size, const char *Name, uptr Flags = 0) {
43     DCHECK(isAllocated());
44     DCHECK((Addr >= getBase()) || (Addr + Size <= getBase() + getCapacity()));
45     return invokeImpl(&Derived::remapImpl, Addr, Size, Name, Flags);
46   }
47 
48   // This is used to update the pages' access permission. For example, mark
49   // pages as no read/write permission.
setMemoryPermission(uptr Addr,uptr Size,uptr Flags)50   void setMemoryPermission(uptr Addr, uptr Size, uptr Flags) {
51     DCHECK(isAllocated());
52     DCHECK((Addr >= getBase()) || (Addr + Size <= getBase() + getCapacity()));
53     return static_cast<Derived *>(this)->setMemoryPermissionImpl(Addr, Size,
54                                                                  Flags);
55   }
56 
57   // Suggest releasing a set of contiguous physical pages back to the OS. Note
58   // that only physical pages are supposed to be released. Any release of
59   // virtual pages may lead to undefined behavior.
releasePagesToOS(uptr From,uptr Size)60   void releasePagesToOS(uptr From, uptr Size) {
61     DCHECK(isAllocated());
62     DCHECK((From >= getBase()) || (From + Size <= getBase() + getCapacity()));
63     invokeImpl(&Derived::releasePagesToOSImpl, From, Size);
64   }
65   // This is similar to the above one except that any subsequent access to the
66   // released pages will return with zero-filled pages.
releaseAndZeroPagesToOS(uptr From,uptr Size)67   void releaseAndZeroPagesToOS(uptr From, uptr Size) {
68     DCHECK(isAllocated());
69     DCHECK((From >= getBase()) || (From + Size <= getBase() + getCapacity()));
70     invokeImpl(&Derived::releaseAndZeroPagesToOSImpl, From, Size);
71   }
72 
getBase()73   uptr getBase() { return invokeImpl(&Derived::getBaseImpl); }
getCapacity()74   uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); }
75 
isAllocated()76   bool isAllocated() { return getBase() != 0U; }
77 
78 protected:
79   template <typename R, typename... Args>
invokeImpl(R (Derived::* MemFn)(Args...),Args...args)80   R invokeImpl(R (Derived::*MemFn)(Args...), Args... args) {
81     return (static_cast<Derived *>(this)->*MemFn)(args...);
82   }
83 };
84 
85 // `ReservedMemory` is a special memory handle which can be viewed as a page
86 // allocator. `ReservedMemory` will reserve a contiguous pages and the later
87 // page request can be fulfilled at the designated address. This is used when
88 // we want to ensure the virtual address of the MemMap will be in a known range.
89 // This is implemented in CRTP, so for each
90 // implementation, it has to implement all of the 'Impl' named functions.
91 template <class Derived, typename MemMapTy> class ReservedMemory {
92 public:
93   using MemMapT = MemMapTy;
94   constexpr ReservedMemory() = default;
95 
96   // Reserve a chunk of memory at a suggested address.
97   bool create(uptr Addr, uptr Size, const char *Name, uptr Flags = 0) {
98     DCHECK(!isCreated());
99     return invokeImpl(&Derived::createImpl, Addr, Size, Name, Flags);
100   }
101 
102   // Release the entire reserved memory.
release()103   void release() {
104     DCHECK(isCreated());
105     invokeImpl(&Derived::releaseImpl);
106   }
107 
108   // Dispatch a sub-range of reserved memory. Note that any fragmentation of
109   // the reserved pages is managed by each implementation.
dispatch(uptr Addr,uptr Size)110   MemMapT dispatch(uptr Addr, uptr Size) {
111     DCHECK(isCreated());
112     DCHECK((Addr >= getBase()) || (Addr + Size <= getBase() + getCapacity()));
113     return invokeImpl(&Derived::dispatchImpl, Addr, Size);
114   }
115 
getBase()116   uptr getBase() { return invokeImpl(&Derived::getBaseImpl); }
getCapacity()117   uptr getCapacity() { return invokeImpl(&Derived::getCapacityImpl); }
118 
isCreated()119   bool isCreated() { return getBase() != 0U; }
120 
121 protected:
122   template <typename R, typename... Args>
invokeImpl(R (Derived::* MemFn)(Args...),Args...args)123   R invokeImpl(R (Derived::*MemFn)(Args...), Args... args) {
124     return (static_cast<Derived *>(this)->*MemFn)(args...);
125   }
126 };
127 
128 } // namespace scudo
129 
130 #endif // SCUDO_MEM_MAP_BASE_H_
131