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