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