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 #include "mem_map_fuchsia.h"
25 #include "mem_map_linux.h"
26 
27 namespace scudo {
28 
29 // This will be deprecated when every allocator has been supported by each
30 // platform's `MemMap` implementation.
31 class MemMapDefault final : public MemMapBase<MemMapDefault> {
32 public:
33   constexpr MemMapDefault() = default;
34   MemMapDefault(uptr Base, uptr Capacity) : Base(Base), Capacity(Capacity) {}
35 
36   // Impls for base functions.
37   bool mapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
38   void unmapImpl(uptr Addr, uptr Size);
39   bool remapImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
40   void setMemoryPermissionImpl(uptr Addr, uptr Size, uptr Flags);
41   void releasePagesToOSImpl(uptr From, uptr Size) {
42     return releaseAndZeroPagesToOSImpl(From, Size);
43   }
44   void releaseAndZeroPagesToOSImpl(uptr From, uptr Size);
45   uptr getBaseImpl() { return Base; }
46   uptr getCapacityImpl() { return Capacity; }
47 
48   void setMapPlatformData(MapPlatformData &NewData) { Data = NewData; }
49 
50 private:
51   uptr Base = 0;
52   uptr Capacity = 0;
53   uptr MappedBase = 0;
54   MapPlatformData Data = {};
55 };
56 
57 // This will be deprecated when every allocator has been supported by each
58 // platform's `MemMap` implementation.
59 class ReservedMemoryDefault final
60     : public ReservedMemory<ReservedMemoryDefault, MemMapDefault> {
61 public:
62   constexpr ReservedMemoryDefault() = default;
63 
64   bool createImpl(uptr Addr, uptr Size, const char *Name, uptr Flags);
65   void releaseImpl();
66   MemMapT dispatchImpl(uptr Addr, uptr Size);
67   uptr getBaseImpl() { return Base; }
68   uptr getCapacityImpl() { return Capacity; }
69 
70 private:
71   uptr Base = 0;
72   uptr Capacity = 0;
73   MapPlatformData Data = {};
74 };
75 
76 #if SCUDO_LINUX
77 using ReservedMemoryT = ReservedMemoryLinux;
78 using MemMapT = ReservedMemoryT::MemMapT;
79 #elif SCUDO_FUCHSIA
80 using ReservedMemoryT = ReservedMemoryFuchsia;
81 using MemMapT = ReservedMemoryT::MemMapT;
82 #elif SCUDO_TRUSTY
83 using ReservedMemoryT = ReservedMemoryDefault;
84 using MemMapT = ReservedMemoryT::MemMapT;
85 #else
86 #error                                                                         \
87     "Unsupported platform, please implement the ReservedMemory for your platform!"
88 #endif
89 
90 } // namespace scudo
91 
92 #endif // SCUDO_MEM_MAP_H_
93