mem_map_base.h 5.0 KB

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