-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmruby_engine.c
More file actions
462 lines (394 loc) · 12.8 KB
/
mruby_engine.c
File metadata and controls
462 lines (394 loc) · 12.8 KB
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
#include "mruby_engine_private.h"
#include "platform.h"
#include <mruby.h>
#include <mruby/array.h>
#include <mruby/class.h>
#include <mruby/dump.h>
#include <mruby/error.h>
#include <mruby/hash.h>
#include <mruby/opcode.h>
#include <mruby/string.h>
#include <mruby/throw.h>
#include <mruby/variable.h>
#include <stdlib.h>
#define ME_EXIT_EXCEPTION_CLASS_VARIABLE "_me_exit_exception_class_"
static struct RClass *get_exit_exception_class(struct mrb_state *state) {
mrb_value c = mrb_gv_get(state, mrb_intern_lit(state, ME_EXIT_EXCEPTION_CLASS_VARIABLE));
mrb_check_type(state, c, MRB_TT_CLASS);
return mrb_class_ptr(c);
}
static const uint64_t COMPILER_INSTRUCTION_QUOTA = 100000;
static const struct timespec COMPILER_TIME_QUOTA = { 1, 0 };
struct me_iseq {
size_t size;
uint8_t *data;
};
static void mruby_engine_signal_memory_quota_reached(struct me_mruby_engine *self, size_t size) {
struct me_eval_err err = (struct me_eval_err){
.type = ME_EVAL_MEMORY_QUOTA_REACHED,
.memory_quota_reached = {
.size = size,
.allocation = me_memory_pool_info(self->allocator).uordblks,
.capacity = me_memory_pool_get_capacity(self->allocator),
},
};
me_mruby_engine_eval_leave(self, err);
}
static void mruby_engine_signal_instruction_quota_reached(struct me_mruby_engine *self) {
struct me_eval_err err = (struct me_eval_err){
.type = ME_EVAL_INSTRUCTION_QUOTA_REACHED,
.instruction_quota_reached = {
.instruction_quota = self->instruction_quota,
},
};
me_mruby_engine_eval_leave(self, err);
}
#ifdef ME_EVAL_MONITORED_P
static void mruby_engine_signal_stack_exhausted(
struct me_mruby_engine *self)
{
struct me_eval_err err = (struct me_eval_err){
.type = ME_EVAL_STACK_EXHAUSTED,
};
me_mruby_engine_eval_leave(self, err);
}
#endif
static void *mruby_engine_allocf(struct mrb_state *state, void *block, size_t size, void *data) {
(void)state;
struct me_mruby_engine *engine = data;
if (size == 0) {
if (block != NULL) {
me_memory_pool_free(engine->allocator, block);
}
return NULL;
}
if (block == NULL) {
block = me_memory_pool_malloc(engine->allocator, size);
} else {
block = me_memory_pool_realloc(engine->allocator, block, size);
}
if (block == NULL) {
mruby_engine_signal_memory_quota_reached(engine, size);
}
return block;
}
bool me_mruby_engine_get_quota_exception_raised(struct me_mruby_engine *self) {
return self->quota_error_raised;
}
me_host_exception_t me_mruby_engine_get_exception(struct me_mruby_engine *self) {
if (self->state->exc == NULL)
return ME_HOST_NIL;
mrb_value exception = mrb_obj_value(self->state->exc);
self->state->exc = NULL;
if (mrb_obj_is_kind_of(self->state, exception, get_exit_exception_class(self->state))) {
return ME_HOST_NIL;
}
intptr_t host_backtrace = me_host_backtrace_new();
mrb_value backtrace = mrb_exc_backtrace(self->state, exception);
mrb_int backtrace_len = RARRAY_LEN(backtrace);
for (int i = 0; i < backtrace_len; ++i) {
mrb_value location = mrb_ary_entry(backtrace, i);
me_host_backtrace_push_location(host_backtrace, mrb_string_value_cstr(self->state, &location));
}
struct RClass *class = mrb_class(self->state, exception);
mrb_value class_name_obj = mrb_obj_as_string(self->state, mrb_obj_value(class));
struct RString *class_name = mrb_str_ptr(class_name_obj);
mrb_value message = mrb_obj_as_string(self->state, exception);
me_host_exception_t err = me_host_runtime_error_new(
RSTR_PTR(class_name),
RSTR_LEN(class_name),
mrb_string_value_cstr(self->state, &message),
host_backtrace);
self->state->exc = NULL;
return err;
}
#ifdef ME_EVAL_MONITORED_P
static const ptrdiff_t STACK_MINIMUM = 0x10000;
static void mruby_engine_check_stack(
struct me_mruby_engine *self)
{
ptrdiff_t stack_remaining =
(uint8_t *)&stack_remaining - (uint8_t *)self->eval_state.stack_base;
if (stack_remaining < STACK_MINIMUM) {
mruby_engine_signal_stack_exhausted(self);
}
}
#endif
static void mruby_engine_code_fetch_hook(
struct mrb_state* mrb,
struct mrb_irep *irep,
mrb_code *pc,
mrb_value *regs)
{
(void)irep;
(void)regs;
struct me_mruby_engine *engine = mrb->allocf_ud;
if (engine->instruction_count >= engine->instruction_quota) {
mruby_engine_signal_instruction_quota_reached(engine);
}
engine->instruction_count++;
#ifdef ME_EVAL_MONITORED_P
switch (GET_OPCODE(*pc)) {
case OP_SEND:
case OP_SENDB:
case OP_FSEND:
{
mruby_engine_check_stack(engine);
break;
}
}
#else
(void)pc;
#endif
}
static mrb_value mruby_engine_exit(struct mrb_state *state, mrb_value rvalue) {
struct RClass *c = get_exit_exception_class(state);
mrb_raise(state, c, "exit exception");
return rvalue;
}
struct me_mruby_engine *me_mruby_engine_new(
struct me_memory_pool *allocator,
uint64_t instruction_quota,
struct timespec time_quota)
{
struct RClass *eExitException_class;
struct me_mruby_engine *self = me_memory_pool_malloc(allocator, sizeof(struct me_mruby_engine));
self->allocator = allocator;
self->state = mrb_open_allocf(mruby_engine_allocf, self);
if (self->state == NULL) {
me_memory_pool_free(allocator, self);
return NULL;
}
eExitException_class = mrb_define_class(self->state, "ExitException", self->state->eException_class);
mrb_gv_set(self->state, mrb_intern_lit(self->state, ME_EXIT_EXCEPTION_CLASS_VARIABLE), mrb_obj_value(eExitException_class));
mrb_define_method(self->state, self->state->kernel_module, "exit", mruby_engine_exit, 1);
self->instruction_quota = instruction_quota;
self->instruction_count = 0;
self->quota_error_raised = false;
self->state->code_fetch_hook = mruby_engine_code_fetch_hook;
self->time_quota = time_quota;
self->ctx_switches_v = -1;
self->ctx_switches_iv = -1;
self->cpu_time_ns = 0;
return self;
}
void me_mruby_engine_destroy(struct me_mruby_engine *self) {
struct me_memory_pool *allocator = me_mruby_engine_get_allocator(self);
mrb_close(self->state);
me_memory_pool_free(allocator, self);
}
struct me_memory_pool *me_mruby_engine_get_allocator(struct me_mruby_engine *self) {
return self->allocator;
}
static struct RProc *generate_code(
struct mrb_state *state,
struct mrbc_context *context,
const char *source,
me_host_exception_t *err)
{
struct mrb_jmpbuf *previous = state->jmp;
struct mrb_jmpbuf p_jmp;
state->jmp = &p_jmp;
struct mrb_parser_state *parser_state = mrb_parser_new(state);
parser_state->s = source;
parser_state->send = source + strlen(source);
MRB_TRY(state->jmp) {
mrb_parser_parse(parser_state, context);
}
MRB_CATCH(state->jmp) {
state->exc = NULL;
if (parser_state->nerr < 1) {
*err = me_host_internal_error_new("code parsing failed");
mrb_parser_free(parser_state);
state->jmp = previous;
return NULL;
}
}
MRB_END_EXC(state->jmp);
state->jmp = previous;
if (parser_state->nerr > 0) {
*err = me_host_syntax_error_new(
parser_state->filename,
parser_state->error_buffer[0].lineno,
parser_state->error_buffer[0].column,
parser_state->error_buffer[0].message);
mrb_parser_free(parser_state);
return NULL;
}
struct RProc *proc = mrb_generate_code(state, parser_state);
mrb_parser_free(parser_state);
if (proc == NULL) {
*err = me_host_internal_error_new("code generation failed");
return NULL;
}
return proc;
}
struct me_proc *me_mruby_engine_generate_code(
struct me_mruby_engine *self,
const char *path,
const char *source,
me_host_exception_t *err)
{
struct mrbc_context *context = mrbc_context_new(self->state);
context->capture_errors = true;
mrbc_filename(self->state, context, path);
struct RProc *proc = generate_code(self->state, context, source, err);
mrbc_context_free(self->state, context);
return (struct me_proc *)proc;
}
void me_mruby_engine_iseq_load(
struct me_mruby_engine *self,
const struct me_iseq *iseq,
me_host_exception_t *err)
{
mrb_irep *irep = mrb_read_irep(self->state, iseq->data);
if(!irep) {
*err = me_host_internal_error_new("mrb_read_irep returned invalid");
return;
}
struct RProc *proc = mrb_proc_new(self->state, irep);
me_mruby_engine_eval(self,(struct me_proc *)proc, err);
}
void me_mruby_engine_inject(
struct me_mruby_engine *self,
const char *ivar_name,
me_host_value_t value,
struct me_value_err *err)
{
mrb_sym ivar_name_mrb = mrb_intern_cstr(self->state, ivar_name);
mrb_value value_mrb = (mrb_value){ .w = me_value_to_guest(self, value, err) };
if (err->type != ME_VALUE_NO_ERR) {
return;
}
mrb_iv_set(self->state, mrb_top_self(self->state), ivar_name_mrb, value_mrb);
}
me_host_value_t me_mruby_engine_extract(
struct me_mruby_engine *self,
const char *ivar_name,
struct me_value_err *err)
{
if (self == NULL) {
me_host_raise(me_host_internal_error_new("invalid parameter: self == NULL"));
}
if (ivar_name == NULL) {
me_host_raise(me_host_internal_error_new("invalid parameter: ivar_name == NULL"));
}
if (err == NULL) {
me_host_raise(me_host_internal_error_new("invalid parameter: err == NULL"));
}
mrb_sym ivar_name_mrb = mrb_intern_cstr(self->state, ivar_name);
mrb_value value = mrb_iv_get(self->state, mrb_top_self(self->state), ivar_name_mrb);
return me_value_to_host(self, value.w, err);
}
uint64_t me_mruby_engine_get_instruction_count(struct me_mruby_engine *self) {
return self->instruction_count;
}
struct meminfo me_mruby_engine_get_memory_info(struct me_mruby_engine *self) {
return me_memory_pool_info(self->allocator);
}
int64_t me_mruby_engine_get_ctx_switches_voluntary(struct me_mruby_engine *self) {
return self->ctx_switches_v;
}
int64_t me_mruby_engine_get_ctx_switches_involuntary(struct me_mruby_engine *self) {
return self->ctx_switches_iv;
}
int64_t me_mruby_engine_get_cpu_time(struct me_mruby_engine *self) {
return self->cpu_time_ns;
}
static int next_source(struct mrb_parser_state *parser_state) {
struct mrbc_context *context = parser_state->cxt;
struct me_source *sources = context->partial_data;
if (sources->source == NULL) {
return -1;
}
parser_state->s = sources->source;
parser_state->send = sources->source + strlen(sources->source);
mrb_parser_set_filename(parser_state, sources->path);
context->partial_data = sources + 1;
return 0;
}
struct me_iseq *me_iseq_new(
struct me_source sources[],
struct me_memory_pool *allocator,
struct me_iseq_err *err)
{
struct me_mruby_engine *engine = me_mruby_engine_new(
allocator,
COMPILER_INSTRUCTION_QUOTA,
COMPILER_TIME_QUOTA);
struct mrbc_context *context = mrbc_context_new(engine->state);
context->no_exec = TRUE;
context->capture_errors = true;
mrbc_partial_hook(engine->state, context, next_source, sources + 1);
mrbc_filename(engine->state, context, sources->path);
me_host_exception_t host_err = ME_HOST_NIL;
struct RProc *proc = generate_code(engine->state, context, sources->source, &host_err);
mrbc_context_free(engine->state, context);
if (proc == NULL) {
me_host_raise(host_err);
}
mrb_irep *irep = proc->body.irep;
uint8_t *irep_data;
size_t irep_data_size;
int status = mrb_dump_irep(
engine->state,
irep,
DUMP_DEBUG_INFO,
&irep_data,
&irep_data_size);
if (status == MRB_DUMP_OK) {
err->type = ME_ISEQ_NO_ERR;
} else {
err->type = ME_ISEQ_GENERAL_FAILURE;
me_mruby_engine_destroy(engine);
return NULL;
}
struct me_iseq *iseq = me_host_malloc(sizeof(struct me_iseq));
*iseq = (struct me_iseq){
.data = me_host_malloc(irep_data_size),
.size = irep_data_size,
};
memcpy(iseq->data, irep_data, irep_data_size);
me_memory_pool_free(allocator, irep_data);
me_mruby_engine_destroy(engine);
return iseq;
}
void me_iseq_destroy(struct me_iseq *iseq) {
free(iseq->data);
free(iseq);
}
size_t me_iseq_size(struct me_iseq *iseq) {
return iseq->size;
}
void *me_iseq_data(struct me_iseq *iseq) {
return iseq->data;
}
static const uint32_t HASH_FACTOR = 65599;
uint32_t me_iseq_hash(struct me_iseq *iseq) {
uint32_t hash = 0;
for (size_t i = 0; i < iseq->size; ++i) {
hash = hash * HASH_FACTOR + iseq->data[i];
}
return hash;
}
me_host_exception_t me_eval_err_to_host(struct me_eval_err *err) {
switch (err->type) {
case ME_EVAL_NO_ERR:
return ME_HOST_NIL;
case ME_EVAL_INSTRUCTION_QUOTA_REACHED:
return me_host_instruction_quota_error_new(err->instruction_quota_reached.instruction_quota);
case ME_EVAL_MEMORY_QUOTA_REACHED:
return me_host_memory_quota_error_new(
err->memory_quota_reached.size,
err->memory_quota_reached.allocation,
err->memory_quota_reached.capacity);
case ME_EVAL_STACK_EXHAUSTED:
return me_host_stack_exhausted_error_new();
case ME_EVAL_SYSTEM_ERROR:
return me_host_internal_error_new_from_err_no(
err->system_error.err_source,
err->system_error.err_no);
default:
return me_host_internal_error_new("unknown %d", err->type);
}
}