This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RLBox_Process.h
290 lines (251 loc) · 7.85 KB
/
RLBox_Process.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#ifndef RLBOX_API_PROCESS
#define RLBOX_API_PROCESS
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <utility>
#include <stdint.h>
#include <mutex>
#include <limits>
#include <map>
#include <algorithm>
#include "ProcessSandbox.h"
namespace RLBox_Process_detail {
//https://stackoverflow.com/questions/6512019/can-we-get-the-type-of-a-lambda-argument
template<typename Ret, typename... Rest>
Ret return_argument_helper(Ret(*) (Rest...));
template<typename Ret, typename F, typename... Rest>
Ret return_argument_helper(Ret(F::*) (Rest...));
template<typename Ret, typename F, typename... Rest>
Ret return_argument_helper(Ret(F::*) (Rest...) const);
template <typename F>
decltype(return_argument_helper(&F::operator())) return_argument_helper(F);
template <typename T>
using return_argument = decltype(return_argument_helper(std::declval<T>()));
template<typename TProcSandbox, typename Ret, typename... Rest>
Ret(*injectSandboxParamInFnType_helper(Ret(*) (Rest...)))(TProcSandbox*, Rest...);
template <typename TProcSandbox, typename T>
using injectSandboxParamInFnType = decltype(injectSandboxParamInFnType_helper<TProcSandbox>(std::declval<T>()));
};
#define ENABLE_IF(...) typename std::enable_if<__VA_ARGS__>::type* = nullptr
template<typename TProcSandbox>
class RLBox_Process
{
private:
static thread_local RLBox_Process* dynLib_SavedState;
static std::mutex sandboxListMutex;
static std::vector<TProcSandbox*> sandboxList;
std::mutex callbackMutex;
std::map<void*, void*> callbackKVMap;
void* libHandle = nullptr;
TProcSandbox* procSandbox = nullptr;
int pushPopCount = 0;
static inline size_t getTotalMemoryHelper()
{
#if defined(_M_IX86) || defined(__i386__)
return 1024ull * 1024ull * 256ull;
#elif defined(_M_X64) || defined(__x86_64__)
return 1024ull * 1024ull * 1024ull * 4ull;
#else
#error Unsupported platform!
#endif
}
public:
inline void impl_CreateSandbox(const char* sandboxRuntimePath, const char* libraryPath)
{
//dlopen with null pointer points to the current app
libHandle = dlopen(nullptr, RTLD_LAZY);
if(!libHandle)
{
printf("Could not open symbol table of my app\n");
abort();
}
procSandbox = new TProcSandbox(libraryPath, 9999 /* maincore: special marker for don't change */, 3 /* sbox_process_core */);
std::lock_guard<std::mutex> lock(sandboxListMutex);
sandboxList.push_back(procSandbox);
}
inline void impl_DestroySandbox()
{
std::lock_guard<std::mutex> lock(sandboxListMutex);
sandboxList.erase(std::remove(sandboxList.begin(), sandboxList.end(), procSandbox), sandboxList.end());
procSandbox->destroySandbox();
}
inline TProcSandbox* impl_getSandbox()
{
return procSandbox;
}
inline void* impl_mallocInSandbox(size_t size)
{
return procSandbox->mallocInSandbox(size);
}
//parameter val is a sandboxed pointer
inline void impl_freeInSandbox(void* val)
{
procSandbox->freeInSandbox(val);
}
inline size_t impl_getTotalMemory()
{
return getTotalMemoryHelper();
}
inline char* impl_getMaxPointer()
{
auto base = (uintptr_t) procSandbox->getSandboxMemoryBase();
auto ending = base + getTotalMemoryHelper() - 1;
return (char*)ending;
}
inline void* impl_pushStackArr(size_t size)
{
pushPopCount++;
return procSandbox->mallocInSandbox(size);
}
inline void impl_popStackArr(void* ptr, size_t size)
{
pushPopCount--;
if(pushPopCount < 0)
{
printf("Error - RLBox_Process popCount was negative.\n");
abort();
}
return procSandbox->freeInSandbox(ptr);
}
template<typename T>
static inline void* impl_GetUnsandboxedPointer(T* p, void* exampleUnsandboxedPtr)
{
return const_cast<void*>((const void*)p);
}
template<typename T>
static inline void* impl_GetSandboxedPointer(T* p, void* exampleUnsandboxedPtr)
{
return const_cast<void*>((const void*)p);
}
template<typename T>
inline void* impl_GetUnsandboxedPointer(T* p)
{
if (impl_isPointerInSandboxMemoryOrNull((const void*)p)) {
return const_cast<void*>((const void*)p);
} else {
return nullptr;
}
}
template<typename T>
inline void* impl_GetSandboxedPointer(T* p)
{
return const_cast<void*>((const void*)p);
}
inline bool impl_isValidSandboxedPointer(const void* p, bool isFuncPtr)
{
if(isFuncPtr) {
//Hard to find if this is a valid func ptr in process, so just be conservative
return false;
} else {
return impl_isPointerInSandboxMemoryOrNull(p);
}
}
inline bool impl_isPointerInSandboxMemoryOrNull(const void* p)
{
auto base = (uintptr_t) procSandbox->getSandboxMemoryBase();
auto ending = base + getTotalMemoryHelper() - 1;
auto pVal = (uintptr_t) p;
return (pVal == 0) || (base <= pVal && pVal <= ending);
}
inline bool impl_isPointerInAppMemoryOrNull(const void* p)
{
return (p == nullptr) || (!impl_isPointerInSandboxMemoryOrNull(p));
}
template<typename T>
static inline T* impl_pointerIncrement(T* p, int64_t increment)
{
std::lock_guard<std::mutex> lock(sandboxListMutex);
for(TProcSandbox* sandbox : sandboxList)
{
size_t memSize = getTotalMemoryHelper();
uintptr_t base = (uintptr_t) sandbox->getSandboxMemoryBase();
uintptr_t pVal = (uintptr_t) const_cast<void*>((const void*)p);
if(pVal >= base && pVal < (base + memSize))
{
auto ret = p + increment;
uintptr_t retv = (uintptr_t) const_cast<void*>((const void*)ret);
if(retv >= base && retv < (base + memSize))
{
return ret;
}
else
{
printf("Incrementing address %p resulted in an out of bounds\n", (void*)retv);
abort();
}
}
}
printf("Could not find sandbox for address: %p\n", const_cast<void*>((const void*)p));
abort();
}
template<typename TRet, typename... TArgs>
inline void* impl_RegisterCallback(void* key, void* callback, void* state)
{
using FuncType = TRet(*)(TArgs...);
auto ret = procSandbox->template registerCallback<FuncType>((FuncType)callback, state);
{
std::lock_guard<std::mutex> lock(callbackMutex);
callbackKVMap[key] = const_cast<void*>((const void*)ret);
}
return const_cast<void*>((const void*)ret);
}
template<typename TFunc>
inline void impl_UnregisterCallback(void* key)
{
void* cb = nullptr;
{
std::lock_guard<std::mutex> lock(callbackMutex);
auto iter = callbackKVMap.find(key);
if(iter == callbackKVMap.end())
{
abort();
}
cb = iter->second;
callbackKVMap.erase(iter);
}
auto cbCast = (TFunc*) cb;
procSandbox->unregisterCallback(cbCast);
}
inline void* impl_LookupSymbol(const char* name, bool forSandboxFunction)
{
if(!forSandboxFunction) {
std::string convertedName = "ProcessSandbox_";
convertedName += name;
auto ret = dlsym(libHandle, convertedName.c_str());
if(!ret)
{
printf("Symbol not found: %s. Error %s.\n", name, dlerror());
abort();
}
return ret;
} else {
size_t len = strlen(name) + 1;
auto copiedName = (char*) procSandbox->mallocInSandbox(len);
strcpy(copiedName, name);
void* ret = procSandbox->inv_invokeDlSym(copiedName);
procSandbox->freeInSandbox(copiedName);
return ret;
}
}
template <typename T, typename ... TArgs>
RLBox_Process_detail::return_argument<T> impl_InvokeFunction(T* fnPtr, TArgs... params)
{
auto castPointer = (RLBox_Process_detail::injectSandboxParamInFnType<TProcSandbox, T*>) (uintptr_t) fnPtr;
dynLib_SavedState = this;
return (*castPointer)(procSandbox, params...);
}
template <typename T, typename ... TArgs>
RLBox_Process_detail::return_argument<T> impl_InvokeFunctionReturnAppPtr(T* fnPtr, TArgs... params)
{
return impl_InvokeFunction(fnPtr, params...);
}
};
template<typename TProcSandbox>
thread_local RLBox_Process<TProcSandbox>* RLBox_Process<TProcSandbox>::dynLib_SavedState = nullptr;
template<typename TProcSandbox>
std::mutex RLBox_Process<TProcSandbox>::sandboxListMutex __attribute__((weak));
template<typename TProcSandbox>
std::vector<TProcSandbox*> RLBox_Process<TProcSandbox>::sandboxList __attribute__((weak));
#undef ENABLE_IF
#endif