-
Notifications
You must be signed in to change notification settings - Fork 5
/
scripting.cc
305 lines (256 loc) · 8.03 KB
/
scripting.cc
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <sstream>
#include <ios>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <assert.h>
#include "labrea.h"
#include "gen_invoker.hh"
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
namespace labrea {
pthread_mutex_t luamutex = PTHREAD_MUTEX_INITIALIZER;
pthread_key_t invocation_state_key;
pthread_t timer_thread;
static pthread_key_t lua_thread_key;
static lua_State *luaStateProto(NULL);
static int cstring_tolstring(lua_State *ls) {
if (lua_gettop(ls) == 1) {
char *addr = reinterpret_cast<char *>(lua_tointeger(ls, -1));
lua_pushstring(ls, addr);
} else if (lua_gettop(ls) == 2) {
char *addr = reinterpret_cast<char *>(lua_tointeger(ls, -2));
size_t n = static_cast<size_t>(lua_tointeger(ls, -1));
lua_pushlstring(ls, addr, n);
} else {
lua_pushstring(ls, "labrea.tostring takes one or two args");
lua_error(ls);
}
return 1;
}
static int do_malloc(lua_State *ls) {
size_t size = static_cast<size_t>(lua_tointeger(ls, -1));
assert(size > 0);
void *p = calloc(1, size);
assert(p);
lua_pushinteger(ls, reinterpret_cast<lua_Integer>(p));
return 1;
}
static int do_free(lua_State *ls) {
void *addr = reinterpret_cast<void *>(lua_tointeger(ls, -1));
free(addr);
return 0;
}
static int do_memcpy(lua_State *ls) {
char *addr = reinterpret_cast<char *>(lua_tointeger(ls, -3));
size_t len(0);
const char *src = lua_tolstring(ls, -2, &len);
size_t n = static_cast<size_t>(lua_tointeger(ls, -1));
memcpy(addr, src, n);
return 0;
}
static int do_fileno(lua_State *ls) {
FILE *f = reinterpret_cast<FILE *>(lua_tointeger(ls, -1));
lua_pushinteger(ls, fileno(f));
return 1;
}
static int do_usleep(lua_State *ls) {
int howlong = lua_tointeger(ls, -1);
// There's a cost associated with this call. Don't do it if it's
// unnecessary.
if (howlong < 1) {
return 0;
}
// This is mildly gross, but we want to release the lock to sleep.
// The lock has automatic cleanup in the caller.
AntiLockHolder alh(&luamutex);
usleep(howlong);
return 0;
}
static int do_invoke(lua_State *ls) {
int n_args = lua_gettop(ls) - 1; // function is an argument.
int offset = lua_tointeger(ls, 1);
struct ftype fun = functions[offset];
assert(fun.pos == offset);
// Capture the arguments.
union farg args[MAX_ARGS];
for (int i = 0; i < n_args; ++i) {
args[i].i64 = lua_tointeger(ls, i + 2); // 2 == starting point of args
}
// Similar to sleep, invoking from around_advice should be done
// without a lock.
lua_Integer rv;
{
AntiLockHolder alh(&luamutex);
rv = abstractInvoke(&fun, args);
}
lua_settop(ls, 0);
lua_pushinteger(ls, rv);
return 1;
}
static int set_errno(lua_State *ls) {
errno = lua_tointeger(ls, -1);
return 0;
}
static bool hasFunction(lua_State *state, const char *fname) {
lua_settop(state, 0);
lua_getfield(state, LUA_GLOBALSINDEX, fname);
return !lua_isnil(state, -1);
}
static int do_reinit(lua_State *s) {
(void)s;
LockHolder lh(&luamutex);
char fname[64];
for (size_t i = 0; functions[i].name; ++i) {
snprintf(fname, sizeof(fname)-1, "before_%s", functions[i].name);
functions[i].has_before = hasFunction(s, fname);
snprintf(fname, sizeof(fname)-1, "around_%s", functions[i].name);
functions[i].has_around = hasFunction(s, fname);
snprintf(fname, sizeof(fname)-1, "after_%s", functions[i].name);
functions[i].has_after = hasFunction(s, fname);
}
return 0;
}
static const luaL_Reg labrea_funcs[] = {
{"fileno", do_fileno},
{"free", do_free},
{"invoke", do_invoke},
{"malloc", do_malloc},
{"memcpy", do_memcpy},
{"reinit", do_reinit},
{"set_errno", set_errno},
{"tostring", cstring_tolstring},
{"usleep", do_usleep},
{NULL, NULL}
};
static void *l_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize;
if (nsize == 0) {
labreafree(ptr);
return NULL;
} else {
return labrearealloc(ptr, nsize);
}
}
static void* timerLoop(void* a) {
(void)a;
for (int i = 0;; ++i) {
sleep(1);
LockHolder lh(&luamutex);
lua_getglobal(luaStateProto, "labrea_periodic");
lua_pushinteger(luaStateProto, i);
if (lua_pcall(luaStateProto, 1, 0, 0) != 0) {
const char *errmsg(lua_tostring(luaStateProto, -1));
std::cerr << "Error running exit function: " << errmsg << std::endl;
}
}
return NULL;
}
static void startTimerThread() {
pthread_attr_t attr;
if (pthread_attr_init(&attr) != 0 ||
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) != 0) {
perror("pthread attr init");
exit(1);
}
if (pthread_create(&timer_thread, &attr, timerLoop, NULL) != 0) {
perror("pthread_create");
exit(1);
}
}
void initPthreadKeys() {
if (pthread_key_create(&lua_thread_key, NULL) != 0) {
perror("pthread_key_create");
abort();
}
if (pthread_key_create(&invocation_state_key, labreafree) != 0) {
perror("pthread_key_create");
abort();
}
}
static void recordFunctions() {
lua_settop(luaStateProto, 0);
for (size_t i = 0; functions[i].name; ++i) {
lua_getglobal(luaStateProto, "labrea_record_function");
lua_pushstring(luaStateProto, functions[i].name);
if (lua_pcall(luaStateProto, 1, 0, 0) != 0) {
const char *errmsg(lua_tostring(luaStateProto, -1));
std::cerr << "Error recording functions function: " << errmsg << std::endl;
abort();
}
}
}
void initScriptingState() {
luaStateProto = lua_newstate(l_alloc, NULL);
luaL_openlibs(luaStateProto);
luaL_register(luaStateProto, "labrea", labrea_funcs);
const char *path = getenv("LABREA_INIT");
if (path != NULL) {
int status = luaL_dofile(luaStateProto, path);
if (status) {
std::cerr << "Error processing labrea script from "
<< (path == NULL ? "stdin" : path)
<< ": " << lua_tostring(luaStateProto, -1) << std::endl;
exit(1);
}
}
recordFunctions();
lua_getglobal(luaStateProto, "labrea_load_user_script");
if (lua_pcall(luaStateProto, 0, 0, 0) != 0) {
const char *errmsg(lua_tostring(luaStateProto, -1));
std::cerr << "Error loading user script: " << errmsg << std::endl;
abort();
}
startTimerThread();
}
void destroyScriptingState() {
LockHolder lh(&luamutex);
lua_getglobal(luaStateProto, "labrea_exiting");
if (lua_pcall(luaStateProto, 0, 0, 0) != 0) {
const char *errmsg(lua_tostring(luaStateProto, -1));
std::cerr << "Error running exit function: " << errmsg << std::endl;
}
}
lua_State* getLuaState() {
lua_State *rv = reinterpret_cast<lua_State*>(pthread_getspecific(lua_thread_key));
if (rv == NULL) {
LockHolder lh(&luamutex);
rv = lua_newthread(luaStateProto);
if (pthread_setspecific(lua_thread_key, rv) != 0) {
perror("pthread_setspecific");
abort();
}
}
return rv;
}
void add_arg(lua_State *state, const unsigned int val) {
lua_pushinteger(state, val);
}
void add_arg(lua_State *state, const int val) {
lua_pushinteger(state, val);
}
void add_arg(lua_State *state, const long val) {
lua_pushinteger(state, static_cast<int64_t>(val));
}
void add_arg(lua_State *state, const long unsigned val) {
lua_pushinteger(state, static_cast<uint64_t>(val));
}
void add_arg(lua_State *state, const long long val) {
lua_pushinteger(state, static_cast<int64_t>(val));
}
void add_arg(lua_State *state, const unsigned long long val) {
lua_pushinteger(state, static_cast<uint64_t>(val));
}
void add_arg(lua_State *state, const void* val) {
union {
const void* p;
uint64_t i;
} hack;
hack.p = val;
lua_pushinteger(state, hack.i);
}
}