mem_map.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //===-- mem_map.cpp ---------------------------------------------*- 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. #include "mem_map.h"
  9. #include "common.h"
  10. namespace scudo {
  11. bool MemMapDefault::mapImpl(uptr Addr, uptr Size, const char *Name,
  12. uptr Flags) {
  13. void *MappedAddr =
  14. ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name, Flags, &Data);
  15. if (MappedAddr == nullptr)
  16. return false;
  17. Base = reinterpret_cast<uptr>(MappedAddr);
  18. MappedBase = Base;
  19. Capacity = Size;
  20. return true;
  21. }
  22. void MemMapDefault::unmapImpl(uptr Addr, uptr Size) {
  23. if (Size == Capacity) {
  24. Base = MappedBase = Capacity = 0;
  25. } else {
  26. if (Base == Addr) {
  27. Base = Addr + Size;
  28. MappedBase = MappedBase == 0 ? Base : Max(MappedBase, Base);
  29. }
  30. Capacity -= Size;
  31. }
  32. ::scudo::unmap(reinterpret_cast<void *>(Addr), Size, UNMAP_ALL, &Data);
  33. }
  34. bool MemMapDefault::remapImpl(uptr Addr, uptr Size, const char *Name,
  35. uptr Flags) {
  36. void *RemappedPtr =
  37. ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name, Flags, &Data);
  38. const uptr RemappedAddr = reinterpret_cast<uptr>(RemappedPtr);
  39. MappedBase = MappedBase == 0 ? RemappedAddr : Min(MappedBase, RemappedAddr);
  40. return RemappedAddr == Addr;
  41. }
  42. void MemMapDefault::releaseAndZeroPagesToOSImpl(uptr From, uptr Size) {
  43. DCHECK_NE(MappedBase, 0U);
  44. DCHECK_GE(From, MappedBase);
  45. return ::scudo::releasePagesToOS(MappedBase, From - MappedBase, Size, &Data);
  46. }
  47. void MemMapDefault::setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags) {
  48. return ::scudo::setMemoryPermission(Addr, Size, Flags);
  49. }
  50. void ReservedMemoryDefault::releaseImpl() {
  51. ::scudo::unmap(reinterpret_cast<void *>(Base), Capacity, UNMAP_ALL, &Data);
  52. }
  53. bool ReservedMemoryDefault::createImpl(uptr Addr, uptr Size, const char *Name,
  54. uptr Flags) {
  55. void *Reserved = ::scudo::map(reinterpret_cast<void *>(Addr), Size, Name,
  56. Flags | MAP_NOACCESS, &Data);
  57. if (Reserved == nullptr)
  58. return false;
  59. Base = reinterpret_cast<uptr>(Reserved);
  60. Capacity = Size;
  61. return true;
  62. }
  63. ReservedMemoryDefault::MemMapT ReservedMemoryDefault::dispatchImpl(uptr Addr,
  64. uptr Size) {
  65. ReservedMemoryDefault::MemMapT NewMap(Addr, Size);
  66. NewMap.setMapPlatformData(Data);
  67. return NewMap;
  68. }
  69. } // namespace scudo