Skip to content

Commit 891f002

Browse files
authored
[orc-rt] Add SimpleNativeMemoryMap. (#162584)
SimpleNativeMemoryMap is a memory allocation backend for use with ORC. It can... 1. Reserve slabs of address space. 2. Finalize regions of memory within a reserved slab: copying content into requested addresses, applying memory protections, running finalization actions, and storing deallocation actions to be run by deallocate. 3. Deallocate finalized memory regions: running deallocate actions and, if possible, making memory in these regions available for use by future finalization operations. (Some systems prohibit reuse of executable memory. On these systems deallocated memory is no longer usable within the process). 4. Release reserved slabs. This runs deallocate for any not-yet-deallocated finalized regions, and then (if possible) returns the address space to system. (On systems that prohibit reuse of executable memory parts of the released address space may be permanently unusable by the process). SimpleNativeMemoryMap is intended primarily for use by llvm::orc::JITLinkMemoryManager implementations to allocate JIT'd code and data.
1 parent 3ccc888 commit 891f002

File tree

8 files changed

+880
-0
lines changed

8 files changed

+880
-0
lines changed

orc-rt/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ set(ORC_RT_HEADERS
1515
orc-rt/ResourceManager.h
1616
orc-rt/RTTI.h
1717
orc-rt/ScopeExit.h
18+
orc-rt/SimpleNativeMemoryMap.h
1819
orc-rt/SimplePackedSerialization.h
1920
orc-rt/SPSAllocAction.h
2021
orc-rt/SPSMemoryFlags.h

orc-rt/include/orc-rt/SPSWrapperFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,12 @@
1414
#ifndef ORC_RT_SPSWRAPPERFUNCTION_H
1515
#define ORC_RT_SPSWRAPPERFUNCTION_H
1616

17+
#include "orc-rt/Compiler.h"
1718
#include "orc-rt/SimplePackedSerialization.h"
1819
#include "orc-rt/WrapperFunction.h"
1920

21+
#define ORC_RT_SPS_INTERFACE ORC_RT_INTERFACE
22+
2023
namespace orc_rt {
2124
namespace detail {
2225

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//===- SimpleNativeMemoryMap.h -- Mem via standard host OS APIs -*- 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+
// SimpleNativeMemoryMap and related APIs.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef ORC_RT_SIMPLENATIVEMEMORYMAP_H
14+
#define ORC_RT_SIMPLENATIVEMEMORYMAP_H
15+
16+
#include "orc-rt/AllocAction.h"
17+
#include "orc-rt/Error.h"
18+
#include "orc-rt/MemoryFlags.h"
19+
#include "orc-rt/ResourceManager.h"
20+
#include "orc-rt/SPSWrapperFunction.h"
21+
#include "orc-rt/move_only_function.h"
22+
23+
#include <map>
24+
#include <mutex>
25+
#include <unordered_map>
26+
#include <vector>
27+
28+
namespace orc_rt {
29+
30+
/// JIT'd memory management backend.
31+
///
32+
/// Intances can:
33+
/// 1. Reserve address space.
34+
/// 2. Finalize memory regions within reserved memory (copying content,
35+
/// applying permissions, running finalize actions, and recording
36+
/// deallocate actions).
37+
/// 3. Deallocate memory regions within reserved memory (running
38+
/// deallocate actions and making memory available for future
39+
/// finalize calls (if the system permits this).
40+
/// 4. Release address space, deallocating any not-yet-deallocated finalized
41+
/// regions, and returning the address space to the system for reuse (if
42+
/// the system permits).
43+
class SimpleNativeMemoryMap : public ResourceManager {
44+
public:
45+
/// Reserves a slab of contiguous address space for allocation.
46+
///
47+
/// Returns the base address of the allocated memory.
48+
using OnReserveCompleteFn = move_only_function<void(Expected<void *>)>;
49+
void reserve(OnReserveCompleteFn &&OnComplete, size_t Size);
50+
51+
/// Release a slab of contiguous address space back to the system.
52+
using OnReleaseCompleteFn = move_only_function<void(Error)>;
53+
void release(OnReleaseCompleteFn &&OnComplete, void *Addr);
54+
55+
struct FinalizeRequest {
56+
struct Segment {
57+
enum class ContentType : uint8_t { Uninitialized, ZeroFill, Regular };
58+
59+
Segment() = default;
60+
Segment(void *Address, size_t Size, AllocGroup G, ContentType C)
61+
: Address(Address), Size(Size), G(G), C(C) {}
62+
63+
void *Address = nullptr;
64+
size_t Size = 0;
65+
AllocGroup G;
66+
ContentType C = ContentType::Uninitialized;
67+
char *data() { return reinterpret_cast<char *>(Address); }
68+
size_t size() const { return Size; }
69+
};
70+
71+
std::vector<Segment> Segments;
72+
std::vector<AllocActionPair> AAPs;
73+
};
74+
75+
/// Writes content into the requested ranges, applies permissions, and
76+
/// performs allocation actions.
77+
using OnFinalizeCompleteFn = move_only_function<void(Expected<void *>)>;
78+
void finalize(OnFinalizeCompleteFn &&OnComplete, FinalizeRequest FR);
79+
80+
/// Runs deallocation actions and resets memory permissions for the requested
81+
/// memory.
82+
using OnDeallocateCompleteFn = move_only_function<void(Error)>;
83+
void deallocate(OnDeallocateCompleteFn &&OnComplete, void *Base);
84+
85+
void detach(ResourceManager::OnCompleteFn OnComplete) override;
86+
void shutdown(ResourceManager::OnCompleteFn OnComplete) override;
87+
88+
private:
89+
struct SlabInfo {
90+
SlabInfo(size_t Size) : Size(Size) {}
91+
size_t Size;
92+
std::unordered_map<void *, std::vector<AllocAction>> DeallocActions;
93+
};
94+
95+
void shutdownNext(OnCompleteFn OnComplete, std::vector<void *> Bases);
96+
Error makeBadSlabError(void *Base, const char *Op);
97+
SlabInfo *findSlabInfoFor(void *Base);
98+
Error recordDeallocActions(void *Base,
99+
std::vector<AllocAction> DeallocActions);
100+
101+
std::mutex M;
102+
std::map<void *, SlabInfo> Slabs;
103+
};
104+
105+
} // namespace orc_rt
106+
107+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_reserve_sps_wrapper(
108+
orc_rt_SessionRef Session, void *CallCtx,
109+
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
110+
111+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_release_sps_wrapper(
112+
orc_rt_SessionRef Session, void *CallCtx,
113+
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
114+
115+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_finalize_sps_wrapper(
116+
orc_rt_SessionRef Session, void *CallCtx,
117+
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
118+
119+
ORC_RT_SPS_INTERFACE void orc_rt_SimpleNativeMemoryMap_deallocate_sps_wrapper(
120+
orc_rt_SessionRef Session, void *CallCtx,
121+
orc_rt_WrapperFunctionReturn Return, orc_rt_WrapperFunctionBuffer ArgBytes);
122+
123+
#endif // ORC_RT_SIMPLENATIVEMEMORYMAP_H

orc-rt/lib/executor/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ set(files
33
AllocAction.cpp
44
ResourceManager.cpp
55
RTTI.cpp
6+
SimpleNativeMemoryMap.cpp
67
)
78

89
add_library(orc-rt-executor STATIC ${files})

0 commit comments

Comments
 (0)