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_MyApp.h
230 lines (193 loc) · 5.57 KB
/
RLBox_MyApp.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
#ifndef RLBOX_API_MYAPP
#define RLBOX_API_MYAPP
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>
#include <utility>
#include <stdint.h>
#include <mutex>
#include <limits>
namespace RLBox_MyApp_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>()));
};
#define ENABLE_IF(...) typename std::enable_if<__VA_ARGS__>::type* = nullptr
class RLBox_MyApp
{
private:
static thread_local RLBox_MyApp* dynLib_SavedState;
std::mutex callbackMutex;
static const unsigned int CALLBACK_SLOT_COUNT = 32;
void* allowedFunctions[CALLBACK_SLOT_COUNT];
void* functionState[CALLBACK_SLOT_COUNT];
void* callbackUniqueKey[CALLBACK_SLOT_COUNT];
void* libHandle = nullptr;
int pushPopCount = 0;
template<unsigned int N, typename TRet, typename... TArgs>
static TRet impl_CallbackReceiver(TArgs... params)
{
using fnType = TRet(*)(TArgs..., void*);
fnType fnPtr = (fnType)(uintptr_t) dynLib_SavedState->allowedFunctions[N];
return fnPtr(params..., dynLib_SavedState->functionState[N]);
}
template<unsigned int I, unsigned int N, typename TRet, typename... TArgs, typename std::enable_if<!(I < N)>::type* = nullptr>
void impl_RegisterCallback_helper(void* key, void* callback, void* state, void*& result)
{
}
template<unsigned int I, unsigned int N, typename TRet, typename... TArgs, typename std::enable_if<(I < N)>::type* = nullptr>
void impl_RegisterCallback_helper(void* key, void* callback, void* state, void*& result)
{
if(!result && callbackUniqueKey[I] == nullptr)
{
callbackUniqueKey[I] = key;
allowedFunctions[I] = callback;
functionState[I] = state;
result = (void*)(uintptr_t) impl_CallbackReceiver<I, TRet, TArgs...>;
}
impl_RegisterCallback_helper<I+1, N, TRet, TArgs...>(key, callback, state, result);
}
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();
}
}
inline void impl_DestroySandbox()
{
}
inline void* impl_getSandbox()
{
return libHandle;
}
inline void* impl_mallocInSandbox(size_t size)
{
return malloc(size);
}
//parameter val is a sandboxed pointer
inline void impl_freeInSandbox(void* val)
{
free(val);
}
inline size_t impl_getTotalMemory()
{
return std::numeric_limits<size_t>::max();
}
inline char* impl_getMaxPointer()
{
return (char*)std::numeric_limits<uintptr_t>::max();
}
inline void* impl_pushStackArr(size_t size)
{
pushPopCount++;
return malloc(size);
}
inline void impl_popStackArr(void* ptr, size_t size)
{
pushPopCount--;
if(pushPopCount < 0)
{
printf("Error - RLBox_MyApp popCount was negative.\n");
abort();
}
return free(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)
{
return const_cast<void*>((const void*)p);
}
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)
{
return true;
}
inline bool impl_isPointerInSandboxMemoryOrNull(const void* p)
{
return true;
}
inline bool impl_isPointerInAppMemoryOrNull(const void* p)
{
return true;
}
template<typename T>
static inline T* impl_pointerIncrement(T* p, int64_t increment)
{
return p + increment;
}
template<typename TRet, typename... TArgs>
inline void* impl_RegisterCallback(void* key, void* callback, void* state)
{
std::lock_guard<std::mutex> lock(callbackMutex);
void* result = nullptr;
impl_RegisterCallback_helper<0, CALLBACK_SLOT_COUNT, TRet, TArgs...>(key, callback, state, result);
return result;
}
template<typename TFunc>
inline void impl_UnregisterCallback(void* key)
{
std::lock_guard<std::mutex> lock(callbackMutex);
for(unsigned int i = 0; i < CALLBACK_SLOT_COUNT; i++)
{
if(callbackUniqueKey[i] == key)
{
callbackUniqueKey[i] = nullptr;
allowedFunctions[i] = nullptr;
functionState[i] = nullptr;
break;
}
}
}
inline void* impl_LookupSymbol(const char* name, bool forSandboxFunction)
{
auto ret = dlsym(libHandle, name);
if(!ret)
{
printf("Symbol not found: %s. Error %s.\n", name, dlerror());
abort();
}
return ret;
}
template <typename T, typename ... TArgs>
RLBox_MyApp_detail::return_argument<T> impl_InvokeFunction(T* fnPtr, TArgs... params)
{
dynLib_SavedState = this;
return (*fnPtr)(params...);
}
template <typename T, typename ... TArgs>
RLBox_MyApp_detail::return_argument<T> impl_InvokeFunctionReturnAppPtr(T* fnPtr, TArgs... params)
{
return impl_InvokeFunction(fnPtr, params...);
}
};
__attribute__((weak))
thread_local RLBox_MyApp* RLBox_MyApp::dynLib_SavedState = nullptr;
#undef ENABLE_IF
#endif