forked from shdown/lua-shm-state-poc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
401 lines (354 loc) · 8.63 KB
/
main.c
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#include "dumb_alloc.h"
#include "align.h"
#include "mapping_kind.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <pthread.h>
#include <unistd.h>
#include <signal.h>
#include <time.h>
//------------------------------------------------------------------------------
#define PTH_CHECK(Expr_) pth_check_impl(Expr_, #Expr_, __FILE__, __LINE__)
static
void
pth_check_impl(int ret, const char *expr, const char *file, int line)
{
if (ret == 0) {
return;
}
fprintf(stderr, "PTH_CHECK(%s) failed at %s:%d: %s\n", expr, file, line, strerror(ret));
abort();
}
//------------------------------------------------------------------------------
typedef struct {
pthread_mutex_t mtx;
pthread_cond_t cond;
bool parentsturn;
} Preface;
static
void
preface_init(Preface *p)
{
pthread_mutexattr_t mtx_attr;
PTH_CHECK(pthread_mutexattr_init(&mtx_attr));
PTH_CHECK(pthread_mutexattr_setpshared(&mtx_attr, PTHREAD_PROCESS_SHARED));
PTH_CHECK(pthread_mutex_init(&p->mtx, &mtx_attr));
pthread_condattr_t cond_attr;
PTH_CHECK(pthread_condattr_init(&cond_attr));
PTH_CHECK(pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED));
PTH_CHECK(pthread_cond_init(&p->cond, &cond_attr));
p->parentsturn = true;
}
//------------------------------------------------------------------------------
static size_t pagesize;
static inline
void
memmgr_init(void)
{
#ifdef _SC_PAGESIZE
long r = sysconf(_SC_PAGESIZE);
#else
long r = sysconf(_SC_PAGE_SIZE);
#endif
if (r < 0) {
perror("sysconf");
abort();
}
pagesize = r;
}
typedef struct {
void *addr;
size_t len;
int (*reclaim_pages)(void *addr, size_t len);
} Mapping;
static inline
void
memshrink(Mapping *m, size_t newnmem)
{
if (!m->reclaim_pages) {
return;
}
const size_t addr_off = ALIGN_TO(newnmem, pagesize);
const size_t len = m->len;
if (addr_off > len) {
return;
}
const size_t reclaim_len = len - addr_off;
if (reclaim_len < pagesize) {
return;
}
if (m->reclaim_pages(m->addr + addr_off, reclaim_len) < 0) {
perror("reclaim_pages");
abort();
}
}
static
bool
setnmem(void *userdata, size_t oldnmem, size_t newnmem)
{
Mapping *m = userdata;
if (oldnmem > newnmem) {
memshrink(m, newnmem);
return true;
} else {
return m->len - ALIGN_TO_DUMB(sizeof(Preface)) >= newnmem;
}
}
static inline
DumbAllocData
make_dumb_alloc_data(Mapping *m)
{
return (DumbAllocData) {
.mem = m->addr + ALIGN_TO_DUMB(sizeof(Preface)),
.userdata = m,
.setnmem = setnmem,
};
}
static
void *
l_realloc(void *userdata, void *ptr, size_t oldn, size_t newn)
{
(void) oldn;
return dumb_realloc(make_dumb_alloc_data(userdata), ptr, newn);
}
//------------------------------------------------------------------------------
static
const char *
get_lua_errmsg(lua_State *L, int pos)
{
const char *msg = lua_tostring(L, pos);
return msg ? msg : "(threw an error object that cannot be converted to string)";
}
static
bool
check_lua_error(lua_State *L, int ret)
{
if (ret == 0) {
return true;
}
fprintf(stderr, "Lua: %s\n", get_lua_errmsg(L, -1));
return false;
}
static
int
l_panicf(lua_State *L)
{
fprintf(stderr, "Lua PANIC: %s\n", get_lua_errmsg(L, -1));
return 0;
}
static
int
l_getpid(lua_State *L)
{
lua_pushinteger(L, getpid());
return 1;
}
static
int
l_sleep(lua_State *L)
{
const double arg = luaL_checknumber(L, 1);
struct timespec req = {.tv_sec = arg};
req.tv_nsec = (arg - req.tv_sec) * 1e9;
struct timespec rem;
int r;
while ((r = nanosleep(&req, &rem)) < 0 && errno == EINTR) {
req = rem;
}
if (r < 0) {
perror("nanosleep");
}
return 0;
}
static
void
inject_lib(lua_State *L)
{
lua_newtable(L); // L: table
lua_pushcfunction(L, l_getpid); // L: table l_getpid
lua_setfield(L, -2, "getpid"); // L: table
lua_pushcfunction(L, l_sleep); // L: table l_sleep
lua_setfield(L, -2, "sleep"); // L: table
lua_setglobal(L, "shm_poc"); // L: -
}
//------------------------------------------------------------------------------
static volatile int sibling;
static
void
handle_signal(int signo)
{
int saved_errno = errno;
kill(sibling, signo);
raise(signo);
errno = saved_errno;
}
static
void
signals_setup(void)
{
sibling = getpid();
struct sigaction siga = {.sa_handler = handle_signal, .sa_flags = SA_RESETHAND};
if (sigemptyset(&siga.sa_mask) < 0) {
perror("sigemptyset");
abort();
}
if (sigaction(SIGHUP, &siga, NULL) < 0 ||
sigaction(SIGINT, &siga, NULL) < 0 ||
sigaction(SIGQUIT, &siga, NULL) < 0 ||
sigaction(SIGTERM, &siga, NULL) < 0)
{
perror("sigaction");
abort();
}
}
static inline
void
signals_onfork(pid_t res)
{
if (res > 0) {
sibling = res;
}
}
//------------------------------------------------------------------------------
static
size_t
parse_mapping_len(const char *arg)
{
char *endptr;
errno = 0;
size_t r = strtoull(arg, &endptr, 10);
if (errno || endptr == arg) {
return 0;
}
switch (*endptr) {
case 'G':
r *= 1024;
// fall through
case 'M':
r *= 1024;
// fall through
case 'K':
r *= 1024;
// fall through
case '\0':
break;
default:
return 0;
}
if (*endptr && endptr[1]) {
return 0;
}
return r;
}
static
void
usage(void)
{
fprintf(stderr, "USAGE: %s [{ -p | -o }] [-n MAPPING_LENGTH]\n"
"\n"
"OPTIONS:\n"
" -p: create portable /dev/zero mapping.\n"
" -o: create Linux-specific on-demand mapping.\n"
" -n MAPPING_LENGTH: set mapping length. Supported suffixes:\n"
" K for kilobytes;\n"
" M for megabytes;\n"
" G for gigabytes.\n"
,
PROGRAM_NAME);
exit(2);
}
int
main(int argc, char **argv)
{
memmgr_init();
MappingKind m_kind = *mapping_kind_pdefault;
Mapping m = {.len = 0};
for (int c; (c = getopt(argc, argv, "n:po")) != -1; ) {
switch (c) {
case 'n':
if (!(m.len = parse_mapping_len(optarg))) {
fprintf(stderr, "E: invalid mapping length.\n");
usage();
}
break;
case 'p':
m_kind = MAPPING_KIND_PORTABLE;
break;
case 'o':
m_kind = MAPPING_KIND_ONDEMAND;
break;
case '?':
usage();
}
}
if (optind != argc) {
usage();
}
if (!m_kind.create) {
fprintf(stderr, "E: specified mapping kind is not available on your system.\n");
return 1;
}
m.reclaim_pages = m_kind.reclaim_pages;
if (!m.len) {
m.len = m_kind.default_len;
}
m.len += ALIGN_TO_DUMB(sizeof(Preface));
if (!(m.addr = m_kind.create(m.len))) {
perror("mmap");
return 1;
}
Preface *preface = m.addr;
preface_init(preface);
if (!dumb_alloc_init(make_dumb_alloc_data(&m))) {
fprintf(stderr, "E: allocated shared mapping is too small.\n");
return 1;
}
lua_State *L = lua_newstate(l_realloc, &m);
if (!L) {
fprintf(stderr, "E: lua_newstate() failed (out of memory?)\n");
return 1;
}
lua_atpanic(L, l_panicf);
luaL_openlibs(L);
inject_lib(L);
if (!check_lua_error(L, luaL_loadfile(L, "demo.lua")) ||
!check_lua_error(L, lua_pcall(L, 0, 0, 0)))
{
return 1;
}
signals_setup();
pid_t pid = fork();
if (pid < 0) {
perror("fork");
return 1;
}
signals_onfork(pid);
bool isparent;
const char *funcname;
if (pid) {
// parent
isparent = true;
funcname = "f";
} else {
// child
isparent = false;
funcname = "g";
}
while (1) {
PTH_CHECK(pthread_mutex_lock(&preface->mtx));
while (preface->parentsturn != isparent) {
PTH_CHECK(pthread_cond_wait(&preface->cond, &preface->mtx));
}
lua_getglobal(L, funcname);
check_lua_error(L, lua_pcall(L, 0, 0, 0));
preface->parentsturn = !isparent;
PTH_CHECK(pthread_cond_signal(&preface->cond));
PTH_CHECK(pthread_mutex_unlock(&preface->mtx));
}
}