-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathffi-glue.cc
613 lines (558 loc) · 15.4 KB
/
ffi-glue.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
#include <vector>
#include <ffi.h>
#include <dlfcn.h>
struct Value {
ffi_type *type;
union {
uint8_t u8;
uint16_t u16;
uint32_t u32;
uint64_t u64;
int8_t s8;
int16_t s16;
int32_t s32;
int64_t s64;
float f;
double d;
void *ptr;
} value;
};
std::ostream &operator<<(std::ostream &out, const Value &v) {
if (v.type == &ffi_type_uint8) {
out << static_cast<unsigned int>(v.value.u8);
} else if (v.type == &ffi_type_uint16) {
out << v.value.u16;
} else if (v.type == &ffi_type_uint32) {
out << v.value.u32;
} else if (v.type == &ffi_type_uint64) {
out << v.value.u64;
} else if (v.type == &ffi_type_sint8) {
out << static_cast<int>(v.value.s8);
} else if (v.type == &ffi_type_sint16) {
out << v.value.s16;
} else if (v.type == &ffi_type_sint32) {
out << v.value.s32;
} else if (v.type == &ffi_type_sint64) {
out << v.value.s64;
} else if (v.type == &ffi_type_float) {
float value = v.value.f;
if(std::isnan(value)) {
out << "0.0e+NaN";
} else if(std::isinf(value)) {
if(value < 0) {
out << "-1.0e+INF";
} else {
out << "1.0e+INF";
}
} else {
out << value;
}
} else if (v.type == &ffi_type_double) {
double value = v.value.d;
if(std::isnan(value)) {
out << "0.0e+NaN";
} else if(std::isinf(value)) {
if(value < 0) {
out << "-1.0e+INF";
} else {
out << "1.0e+INF";
}
} else {
out << value;
}
} else if (v.type == &ffi_type_pointer) {
out << "\\" << v.value.ptr;
} else if (v.type == &ffi_type_void) {
out << ":void";
}
out << "\n$";
return out;
}
class FFIStack {
public:
void push(uint8_t v) {
stack_.push_back(Value{&ffi_type_uint8, {.u8 = v}});
}
void push(uint16_t v) {
stack_.push_back(Value{&ffi_type_uint16, {.u16 = v}});
}
void push(uint32_t v) {
stack_.push_back(Value{&ffi_type_uint32, {.u32 = v}});
}
void push(uint64_t v) {
stack_.push_back(Value{&ffi_type_uint64, {.u64 = v}});
}
void push(int8_t v) {
stack_.push_back(Value{&ffi_type_sint8, {.s8 = v}});
}
void push(int16_t v) {
stack_.push_back(Value{&ffi_type_sint16, {.s16 = v}});
}
void push(int32_t v) {
stack_.push_back(Value{&ffi_type_sint32, {.s32 = v}});
}
void push(int64_t v) {
stack_.push_back(Value{&ffi_type_sint64, {.s64 = v}});
}
void push(double v) {
stack_.push_back(Value{&ffi_type_double, {.d = v}});
}
void push(float v) {
stack_.push_back(Value{&ffi_type_float, {.f = v}});
}
void push(void *v) {
stack_.push_back(Value{&ffi_type_pointer, {.ptr = v}});
}
void push(Value &v) { stack_.push_back(v); }
void push() {
stack_.push_back(Value{&ffi_type_void, {0}});
}
Value pop() {
Value v = stack_.back();
stack_.pop_back();
return v;
}
Value peek() { return stack_.back(); }
Value &operator[](int i) { return stack_[stack_.size() - i - 1]; }
void dup() {
stack_.push_back(stack_.back());
}
size_t size() {
return stack_.size();
}
private:
std::vector<Value> stack_;
};
class Machine {
public:
Machine(std::ostream &out) : out_(out) {
out.precision(std::numeric_limits<double>::digits10);
};
~Machine() {
for (auto &i : cifs_) {
delete i->arg_types;
delete i;
}
for (auto &i : libs_) {
if (i) dlclose(i);
}
};
FFIStack stack;
void pop() { out_ << stack.pop(); }
void peek() { out_ << stack.peek(); }
void dlopen() {
const char *name = static_cast<const char *>(stack.pop().value.ptr);
void *handle = ::dlopen(name, RTLD_LAZY);
stack.push(handle);
libs_.push_back(handle);
}
void dlsym() {
const char *name = static_cast<const char *>(stack.pop().value.ptr);
void *handle = stack.pop().value.ptr;
void *ptr = ::dlsym(handle, name);
stack.push(ptr);
}
void cif() {
ffi_cif *cif = new ffi_cif;
uint32_t nargs = stack.pop().value.u32;
ffi_type *rtype = stack.pop().type;
ffi_type **types = new ffi_type*[nargs];
for (uint32_t i = 0; i < nargs; i++) {
types[i] = stack.pop().type;
}
ffi_prep_cif(cif, FFI_DEFAULT_ABI, nargs, rtype, types);
stack.push(cif);
cifs_.push_back(cif);
}
void call(bool get_errno) {
void (*function)() = reinterpret_cast<void (*)()>(stack.pop().value.ptr);
ffi_cif *cif = static_cast<ffi_cif *>(stack.pop().value.ptr);
int nargs = cif->nargs;
void *args[nargs];
Value result {cif->rtype, {0}};
for (int i = 0; i < nargs; i++) {
args[i] = &stack[i].value;
}
if(get_errno)
errno = 0;
ffi_call(cif, function, &result.value, args);
for (int i = 0; i < nargs; i++) {
stack.pop();
}
if(get_errno)
stack.push(errno);
stack.push(result);
}
void strlen() {
char *str = static_cast<char *>(stack.peek().value.ptr);
uint32_t size = std::strlen(str);
stack.push(size);
}
void dump() {
uint32_t size = stack.pop().value.u32;
char *data = static_cast<char *>(stack.pop().value.ptr);
out_.write(data, size);
}
void read_mem() {
int type = std::cin.get();
Value out;
uint8_t* ptr_bytes = static_cast<uint8_t*>(stack.pop().value.ptr);
size_t offset = stack.pop().value.u64;
void* ptr = ptr_bytes + offset;
switch(type) {
case 'u':
out.type = &ffi_type_uint8;
out.value.u8 = *static_cast<uint8_t*>(ptr);
break;
case 'v':
out.type = &ffi_type_uint16;
out.value.u16 = *static_cast<uint16_t*>(ptr);
break;
case 'w':
out.type = &ffi_type_uint32;
out.value.u32 = *static_cast<uint32_t*>(ptr);
break;
case 'x':
out.type = &ffi_type_uint64;
out.value.u64 = *static_cast<uint64_t*>(ptr);
break;
case 'i':
out.type = &ffi_type_sint8;
out.value.s8 = *static_cast<int8_t*>(ptr);
break;
case 'j':
out.type = &ffi_type_sint16;
out.value.s16 = *static_cast<int16_t*>(ptr);
break;
case 'k':
out.type = &ffi_type_sint32;
out.value.s32 = *static_cast<int32_t*>(ptr);
break;
case 'l':
out.type = &ffi_type_sint64;
out.value.s64 = *static_cast<int64_t*>(ptr);
break;
case 'f':
out.type = &ffi_type_float;
out.value.f = *static_cast<float*>(ptr);
break;
case 'd':
out.type = &ffi_type_double;
out.value.d = *static_cast<double*>(ptr);
break;
case 'p':
out.type = &ffi_type_pointer;
out.value.ptr = *static_cast<void**>(ptr);
break;
default:
std::cerr << "invalid type in read_mem: " << static_cast<char>(type) << std::endl;
return;
}
stack.push(out);
}
void read_array() {
int type = std::cin.get();
void* raw_ptr = stack.pop().value.ptr;
size_t length = stack.pop().value.u64;
out_ << '(';
switch(type) {
case 'u': {
uint8_t* ptr = static_cast<uint8_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << static_cast<unsigned int>(ptr[i]) << ' ';
}
break;
case 'v': {
uint16_t* ptr = static_cast<uint16_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'w': {
uint32_t* ptr = static_cast<uint32_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'x': {
uint64_t* ptr = static_cast<uint64_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'i': {
int8_t* ptr = static_cast<int8_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << static_cast<int>(ptr[i]) << ' ';
}
break;
case 'j': {
int16_t* ptr = static_cast<int16_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'k': {
int32_t* ptr = static_cast<int32_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'l': {
int64_t* ptr = static_cast<int64_t*>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
case 'f': {
float* ptr = static_cast<float*>(raw_ptr);
for(size_t i = 0; i < length; i++) {
float value = ptr[i];
if(std::isnan(value)) {
out_ << "0.0e+NaN";
} else if(std::isinf(value)) {
if(value < 0) {
out_ << "-1.0e+INF";
} else {
out_ << "1.0e+INF";
}
} else {
out_ << value;
}
out_ << ' ';
}}
break;
case 'd': {
double* ptr = static_cast<double*>(raw_ptr);
for(size_t i = 0; i < length; i++) {
double value = ptr[i];
if(std::isnan(value)) {
out_ << "0.0e+NaN";
} else if(std::isinf(value)) {
if(value < 0) {
out_ << "-1.0e+INF";
} else {
out_ << "1.0e+INF";
}
} else {
out_ << value;
}
out_ << ' ';
}}
break;
case 'p': {
void** ptr = static_cast<void**>(raw_ptr);
for(size_t i = 0; i < length; i++)
out_ << ptr[i] << ' ';
}
break;
default:
std::cerr << "invalid type in read_array: " << static_cast<char>(type) << std::endl;
return;
}
out_ << ")\n$";
}
void free() { delete static_cast<char *>(stack.pop().value.ptr); }
void size() {
uint32_t size = stack.size();
stack.push(size);
}
private:
std::ostream &out_;
std::vector<ffi_cif *> cifs_;
std::vector<void *> libs_;
};
class Reader {
public:
Reader(Machine &vm, std::istream &in) : vm_(vm), in_(in) {};
void read_uint8() {
uint32_t i;
in_ >> i;
vm_.stack.push(static_cast<uint8_t>(i));
}
void read_uint16() {
uint16_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_uint32() {
uint32_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_uint64() {
uint64_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_sint8() {
int32_t i;
in_ >> i;
vm_.stack.push(static_cast<int8_t>(i));
}
void read_sint16() {
int16_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_sint32() {
int32_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_sint64() {
int64_t i;
in_ >> i;
vm_.stack.push(i);
}
void read_float() {
float f;
in_ >> f;
vm_.stack.push(f);
}
void read_double() {
double d;
in_ >> d;
vm_.stack.push(d);
}
void read_pointer() {
void *ptr = nullptr;
in_ >> ptr;
vm_.stack.push(ptr);
}
void read(uint32_t size) {
char *buffer = new char[size + 1];
buffer[size] = '\0';
in_.read(buffer, size);
vm_.stack.push(buffer);
}
private:
Machine &vm_;
std::istream &in_;
};
int main() {
Machine vm{std::cout};
Reader reader{vm, std::cin};
while (!std::cin.eof()) {
int command = std::cin.get();
switch (command) {
/* push signed integer */
case 'i' + 0: /* i */
reader.read_sint8();
break;
case 'i' + 1: /* j */
reader.read_sint16();
break;
case 'i' + 2: /* k */
reader.read_sint32();
break;
case 'i' + 3: /* l */
reader.read_sint64();
break;
/* push unsigned integer */
case 'u' + 0: /* u */
reader.read_uint8();
break;
case 'u' + 1: /* v */
reader.read_uint16();
break;
case 'u' + 2: /* w */
reader.read_uint32();
break;
case 'u' + 3: /* x */
reader.read_uint64();
break;
/* push float */
case 'f':
reader.read_float();
break;
case 'd':
reader.read_double();
break;
/* push pointer */
case 'p':
reader.read_pointer();
break;
/* push void */
case 'V':
vm.stack.push();
break;
/* peek */
case 'e':
vm.peek();
break;
/* pop */
case 'o':
vm.pop();
break;
/* call */
case 'c':
vm.call(false);
break;
/* call with errno */
case 'E':
vm.call(true);
break;
/* call */
case 'C':
vm.cif();
break;
/* "malloc" */
case 'M':
reader.read(vm.stack.pop().value.u32);
break;
/* free */
case 'F':
vm.free();
break;
/* dl calls */
case 'O':
vm.dlopen();
break;
case 'S':
vm.dlsym();
break;
/* strlen */
case 'L':
vm.strlen();
break;
/* dump */
case 'D':
vm.dump();
break;
/* read from memory */
case 'r':
vm.read_mem();
break;
/* array read from memory */
case 'A':
vm.read_array();
break;
/* duplicate top value */
case '!':
vm.stack.dup();
break;
/* get stack size */
case '?':
vm.size();
break;
/* no-op */
case ' ':
case '\n':
case '\r':
case -1:
break;
default:
std::cerr << "invalid op: " << static_cast<char>(command) << std::endl;
break;
}
std::cout.flush();
}
std::cout << std::endl;
return 0;
}