mem_map.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===-- mem_map.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_H_
  9. #define SCUDO_MEM_MAP_H_
  10. #include "mem_map_base.h"
  11. #include "common.h"
  12. #include "internal_defs.h"
  13. // TODO: This is only used for `MapPlatformData`. Remove these includes when we
  14. // have all three platform specific `MemMap` and `ReservedMemory`
  15. // implementations.
  16. #include "fuchsia.h"
  17. #include "linux.h"
  18. #include "trusty.h"
  19. #include "mem_map_fuchsia.h"
  20. #include "mem_map_linux.h"
  21. namespace scudo {
  22. // This will be deprecated when every allocator has been supported by each
  23. // platform's `MemMap` implementation.
  24. class MemMapDefault final : public MemMapBase<MemMapDefault> {
  25. public:
  26. constexpr MemMapDefault() = default;
  27. MemMapDefault(uptr Base, uptr Capacity) : Base(Base), Capacity(Capacity) {}
  28. // Impls for base functions.
  29. bool mapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
  30. void unmapImpl(uptr Addr, uptr Size);
  31. bool remapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
  32. void setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags);
  33. void releasePagesToOSImpl(uptr From, uptr Size) {
  34. return releaseAndZeroPagesToOSImpl(From, Size);
  35. }
  36. void releaseAndZeroPagesToOSImpl(uptr From, uptr Size);
  37. uptr getBaseImpl() { return Base; }
  38. uptr getCapacityImpl() { return Capacity; }
  39. void setMapPlatformData(MapPlatformData &NewData) { Data = NewData; }
  40. private:
  41. uptr Base = 0;
  42. uptr Capacity = 0;
  43. uptr MappedBase = 0;
  44. MapPlatformData Data = {};
  45. };
  46. // This will be deprecated when every allocator has been supported by each
  47. // platform's `MemMap` implementation.
  48. class ReservedMemoryDefault final
  49. : public ReservedMemory<ReservedMemoryDefault, MemMapDefault> {
  50. public:
  51. constexpr ReservedMemoryDefault() = default;
  52. bool createImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
  53. void releaseImpl();
  54. MemMapT dispatchImpl(uptr Addr, uptr Size);
  55. uptr getBaseImpl() { return Base; }
  56. uptr getCapacityImpl() { return Capacity; }
  57. private:
  58. uptr Base = 0;
  59. uptr Capacity = 0;
  60. MapPlatformData Data = {};
  61. };
  62. #if SCUDO_LINUX
  63. using ReservedMemoryT = ReservedMemoryLinux;
  64. using MemMapT = ReservedMemoryT::MemMapT;
  65. #elif SCUDO_FUCHSIA
  66. using ReservedMemoryT = ReservedMemoryFuchsia;
  67. using MemMapT = ReservedMemoryT::MemMapT;
  68. #elif SCUDO_TRUSTY
  69. using ReservedMemoryT = ReservedMemoryDefault;
  70. using MemMapT = ReservedMemoryT::MemMapT;
  71. #else
  72. #error \
  73. "Unsupported platform, please implement the ReservedMemory for your platform!"
  74. #endif
  75. } // namespace scudo
  76. #endif // SCUDO_MEM_MAP_H_