2 ** load.c - mruby binary loader
4 ** See Copyright Notice in mruby.h
11 #include <mruby/dump.h>
12 #include <mruby/irep.h>
13 #include <mruby/proc.h>
14 #include <mruby/string.h>
15 #include <mruby/debug.h>
16 #include <mruby/error.h>
18 #if SIZE_MAX < UINT32_MAX
19 # error size_t must be at least 32 bits wide
22 #define FLAG_BYTEORDER_BIG 2
23 #define FLAG_BYTEORDER_LIL 4
24 #define FLAG_BYTEORDER_NATIVE 8
25 #define FLAG_SRC_MALLOC 1
26 #define FLAG_SRC_STATIC 0
28 #define SIZE_ERROR_MUL(nmemb, size) ((size_t)(nmemb) > SIZE_MAX / (size))
31 skip_padding(const uint8_t *buf)
33 const size_t align = MRB_DUMP_ALIGNMENT;
34 return -(intptr_t)buf & (align-1);
40 struct rite_binary_header header;
41 return ((uint8_t *)header.binary_crc - (uint8_t *)&header) + sizeof(header.binary_crc);
44 #ifndef MRB_WITHOUT_FLOAT
46 str_to_double(mrb_state *mrb, mrb_value str)
48 const char *p = RSTRING_PTR(str);
49 mrb_int len = RSTRING_LEN(str);
51 /* `i`, `inf`, `infinity` */
52 if (len > 0 && p[0] == 'i') return INFINITY;
54 /* `I`, `-inf`, `-infinity` */
55 if (p[0] == 'I' || (len > 1 && p[0] == '-' && p[1] == 'i')) return -INFINITY;
57 return mrb_str_to_dbl(mrb, str, TRUE);
62 read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags)
65 const uint8_t *src = bin;
67 uint16_t tt, pool_data_len, snl;
69 int ai = mrb_gc_arena_save(mrb);
70 mrb_irep *irep = mrb_add_irep(mrb);
72 /* skip record size */
73 src += sizeof(uint32_t);
75 /* number of local variable */
76 irep->nlocals = bin_to_uint16(src);
77 src += sizeof(uint16_t);
79 /* number of register variable */
80 irep->nregs = bin_to_uint16(src);
81 src += sizeof(uint16_t);
83 /* number of child irep */
84 irep->rlen = (size_t)bin_to_uint16(src);
85 src += sizeof(uint16_t);
87 /* Binary Data Section */
89 irep->ilen = (uint16_t)bin_to_uint32(src);
90 src += sizeof(uint32_t);
91 src += skip_padding(src);
94 if (SIZE_ERROR_MUL(irep->ilen, sizeof(mrb_code))) {
97 if ((flags & FLAG_SRC_MALLOC) == 0 &&
98 (flags & FLAG_BYTEORDER_NATIVE)) {
99 irep->iseq = (mrb_code*)src;
100 src += sizeof(mrb_code) * irep->ilen;
101 irep->flags |= MRB_ISEQ_NO_FREE;
104 size_t data_len = sizeof(mrb_code) * irep->ilen;
105 irep->iseq = (mrb_code *)mrb_malloc(mrb, data_len);
106 memcpy(irep->iseq, src, data_len);
112 plen = bin_to_uint32(src); /* number of pool */
113 src += sizeof(uint32_t);
115 if (SIZE_ERROR_MUL(plen, sizeof(mrb_value))) {
118 irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen);
120 for (i = 0; i < plen; i++) {
123 tt = *src++; /* pool TT */
124 pool_data_len = bin_to_uint16(src); /* pool data length */
125 src += sizeof(uint16_t);
126 if (flags & FLAG_SRC_MALLOC) {
127 s = mrb_str_new(mrb, (char *)src, pool_data_len);
130 s = mrb_str_new_static(mrb, (char *)src, pool_data_len);
132 src += pool_data_len;
133 switch (tt) { /* pool data */
134 case IREP_TT_FIXNUM: {
135 mrb_value num = mrb_str_to_inum(mrb, s, 10, FALSE);
136 #ifdef MRB_WITHOUT_FLOAT
139 irep->pool[i] = mrb_float_p(num)? mrb_float_pool(mrb, mrb_float(num)) : num;
144 #ifndef MRB_WITHOUT_FLOAT
146 irep->pool[i] = mrb_float_pool(mrb, str_to_double(mrb, s));
151 irep->pool[i] = mrb_str_pool(mrb, s);
155 /* should not happen */
156 irep->pool[i] = mrb_nil_value();
160 mrb_gc_arena_restore(mrb, ai);
165 irep->slen = (uint16_t)bin_to_uint32(src); /* syms length */
166 src += sizeof(uint32_t);
167 if (irep->slen > 0) {
168 if (SIZE_ERROR_MUL(irep->slen, sizeof(mrb_sym))) {
171 irep->syms = (mrb_sym *)mrb_malloc(mrb, sizeof(mrb_sym) * irep->slen);
173 for (i = 0; i < irep->slen; i++) {
174 snl = bin_to_uint16(src); /* symbol name length */
175 src += sizeof(uint16_t);
177 if (snl == MRB_DUMP_NULL_SYM_LEN) {
182 if (flags & FLAG_SRC_MALLOC) {
183 irep->syms[i] = mrb_intern(mrb, (char *)src, snl);
186 irep->syms[i] = mrb_intern_static(mrb, (char *)src, snl);
190 mrb_gc_arena_restore(mrb, ai);
194 irep->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*irep->rlen);
197 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
204 read_irep_record(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags)
206 mrb_irep *irep = read_irep_record_1(mrb, bin, len, flags);
214 for (i=0; i<irep->rlen; i++) {
217 irep->reps[i] = read_irep_record(mrb, bin, &rlen, flags);
218 if (irep->reps[i] == NULL) {
228 read_section_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
232 bin += sizeof(struct rite_section_irep_header);
233 return read_irep_record(mrb, bin, &len, flags);
236 /* ignore lineno record */
238 read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t *len)
240 size_t i, fname_len, niseq;
243 bin += sizeof(uint32_t); /* record size */
244 *len += sizeof(uint32_t);
245 fname_len = bin_to_uint16(bin);
246 bin += sizeof(uint16_t);
247 *len += sizeof(uint16_t);
251 niseq = (size_t)bin_to_uint32(bin);
252 bin += sizeof(uint32_t); /* niseq */
253 *len += sizeof(uint32_t);
255 if (SIZE_ERROR_MUL(niseq, sizeof(uint16_t))) {
256 return MRB_DUMP_GENERAL_FAILURE;
258 for (i = 0; i < niseq; i++) {
259 bin += sizeof(uint16_t); /* niseq */
260 *len += sizeof(uint16_t);
267 read_lineno_record(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t *lenp)
269 int result = read_lineno_record_1(mrb, bin, irep, lenp);
272 if (result != MRB_DUMP_OK) return result;
273 for (i = 0; i < irep->rlen; i++) {
276 result = read_lineno_record(mrb, bin, irep->reps[i], &len);
277 if (result != MRB_DUMP_OK) break;
285 read_section_lineno(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep)
290 bin += sizeof(struct rite_section_lineno_header);
292 /* Read Binary Data Section */
293 return read_lineno_record(mrb, bin, irep, &len);
297 read_debug_record(mrb_state *mrb, const uint8_t *start, mrb_irep* irep, size_t *record_len, const mrb_sym *filenames, size_t filenames_len)
299 const uint8_t *bin = start;
305 if (irep->debug_info) { return MRB_DUMP_INVALID_IREP; }
307 irep->debug_info = (mrb_irep_debug_info*)mrb_malloc(mrb, sizeof(mrb_irep_debug_info));
308 irep->debug_info->pc_count = (uint32_t)irep->ilen;
310 record_size = (size_t)bin_to_uint32(bin);
311 bin += sizeof(uint32_t);
313 irep->debug_info->flen = bin_to_uint16(bin);
314 irep->debug_info->files = (mrb_irep_debug_info_file**)mrb_malloc(mrb, sizeof(mrb_irep_debug_info*) * irep->debug_info->flen);
315 bin += sizeof(uint16_t);
317 for (f_idx = 0; f_idx < irep->debug_info->flen; ++f_idx) {
318 mrb_irep_debug_info_file *file;
319 uint16_t filename_idx;
321 file = (mrb_irep_debug_info_file *)mrb_malloc(mrb, sizeof(*file));
322 irep->debug_info->files[f_idx] = file;
324 file->start_pos = bin_to_uint32(bin);
325 bin += sizeof(uint32_t);
328 filename_idx = bin_to_uint16(bin);
329 bin += sizeof(uint16_t);
330 mrb_assert(filename_idx < filenames_len);
331 file->filename_sym = filenames[filename_idx];
333 file->line_entry_count = bin_to_uint32(bin);
334 bin += sizeof(uint32_t);
335 file->line_type = (mrb_debug_line_type)bin_to_uint8(bin);
336 bin += sizeof(uint8_t);
337 switch (file->line_type) {
338 case mrb_debug_line_ary: {
341 file->lines.ary = (uint16_t *)mrb_malloc(mrb, sizeof(uint16_t) * (size_t)(file->line_entry_count));
342 for (l = 0; l < file->line_entry_count; ++l) {
343 file->lines.ary[l] = bin_to_uint16(bin);
344 bin += sizeof(uint16_t);
348 case mrb_debug_line_flat_map: {
351 file->lines.flat_map = (mrb_irep_debug_info_line*)mrb_malloc(
352 mrb, sizeof(mrb_irep_debug_info_line) * (size_t)(file->line_entry_count));
353 for (l = 0; l < file->line_entry_count; ++l) {
354 file->lines.flat_map[l].start_pos = bin_to_uint32(bin);
355 bin += sizeof(uint32_t);
356 file->lines.flat_map[l].line = bin_to_uint16(bin);
357 bin += sizeof(uint16_t);
361 default: return MRB_DUMP_GENERAL_FAILURE;
366 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
368 if (record_size != (size_t)diff) {
369 return MRB_DUMP_GENERAL_FAILURE;
372 for (i = 0; i < irep->rlen; i++) {
376 ret = read_debug_record(mrb, bin, irep->reps[i], &len, filenames, filenames_len);
377 if (ret != MRB_DUMP_OK) return ret;
382 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
383 *record_len = (size_t)diff;
389 read_section_debug(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, uint8_t flags)
393 struct rite_section_debug_header *header;
397 uint16_t filenames_len;
401 header = (struct rite_section_debug_header *)bin;
402 bin += sizeof(struct rite_section_debug_header);
404 filenames_len = bin_to_uint16(bin);
405 bin += sizeof(uint16_t);
406 filenames = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * (size_t)filenames_len);
407 for (i = 0; i < filenames_len; ++i) {
408 uint16_t f_len = bin_to_uint16(bin);
409 bin += sizeof(uint16_t);
410 if (flags & FLAG_SRC_MALLOC) {
411 filenames[i] = mrb_intern(mrb, (const char *)bin, (size_t)f_len);
414 filenames[i] = mrb_intern_static(mrb, (const char *)bin, (size_t)f_len);
419 result = read_debug_record(mrb, bin, irep, &len, filenames, filenames_len);
420 if (result != MRB_DUMP_OK) goto debug_exit;
424 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
425 if ((uint32_t)diff != bin_to_uint32(header->section_size)) {
426 result = MRB_DUMP_GENERAL_FAILURE;
430 mrb_free(mrb, filenames);
435 read_lv_record(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, size_t *record_len, mrb_sym const *syms, uint32_t syms_len)
437 const uint8_t *bin = start;
441 irep->lv = (struct mrb_locals*)mrb_malloc(mrb, sizeof(struct mrb_locals) * (irep->nlocals - 1));
443 for (i = 0; i + 1< irep->nlocals; ++i) {
444 uint16_t const sym_idx = bin_to_uint16(bin);
445 bin += sizeof(uint16_t);
446 if (sym_idx == RITE_LV_NULL_MARK) {
447 irep->lv[i].name = 0;
451 if (sym_idx >= syms_len) {
452 return MRB_DUMP_GENERAL_FAILURE;
454 irep->lv[i].name = syms[sym_idx];
456 irep->lv[i].r = bin_to_uint16(bin);
458 bin += sizeof(uint16_t);
461 for (i = 0; i < irep->rlen; ++i) {
465 ret = read_lv_record(mrb, bin, irep->reps[i], &len, syms, syms_len);
466 if (ret != MRB_DUMP_OK) return ret;
471 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
472 *record_len = (size_t)diff;
478 read_section_lv(mrb_state *mrb, const uint8_t *start, mrb_irep *irep, uint8_t flags)
482 struct rite_section_lv_header const *header;
488 mrb_sym (*intern_func)(mrb_state*, const char*, size_t) =
489 (flags & FLAG_SRC_MALLOC)? mrb_intern : mrb_intern_static;
492 header = (struct rite_section_lv_header const*)bin;
493 bin += sizeof(struct rite_section_lv_header);
495 syms_len = bin_to_uint32(bin);
496 bin += sizeof(uint32_t);
497 syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym) * (size_t)syms_len);
498 for (i = 0; i < syms_len; ++i) {
499 uint16_t const str_len = bin_to_uint16(bin);
500 bin += sizeof(uint16_t);
502 syms[i] = intern_func(mrb, (const char*)bin, str_len);
506 result = read_lv_record(mrb, bin, irep, &len, syms, syms_len);
507 if (result != MRB_DUMP_OK) goto lv_exit;
511 mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
512 if ((uint32_t)diff != bin_to_uint32(header->section_size)) {
513 result = MRB_DUMP_GENERAL_FAILURE;
522 read_binary_header(const uint8_t *bin, size_t *bin_size, uint16_t *crc, uint8_t *flags)
524 const struct rite_binary_header *header = (const struct rite_binary_header *)bin;
526 if (memcmp(header->binary_ident, RITE_BINARY_IDENT, sizeof(header->binary_ident)) == 0) {
528 *flags |= FLAG_BYTEORDER_NATIVE;
530 *flags |= FLAG_BYTEORDER_BIG;
532 else if (memcmp(header->binary_ident, RITE_BINARY_IDENT_LIL, sizeof(header->binary_ident)) == 0) {
534 *flags |= FLAG_BYTEORDER_LIL;
536 *flags |= FLAG_BYTEORDER_NATIVE;
539 return MRB_DUMP_INVALID_FILE_HEADER;
543 *crc = bin_to_uint16(header->binary_crc);
545 *bin_size = (size_t)bin_to_uint32(header->binary_size);
551 read_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
554 mrb_irep *irep = NULL;
555 const struct rite_section_header *section_header;
560 if ((mrb == NULL) || (bin == NULL)) {
564 result = read_binary_header(bin, &bin_size, &crc, &flags);
565 if (result != MRB_DUMP_OK) {
569 n = offset_crc_body();
570 if (crc != calc_crc_16_ccitt(bin + n, bin_size - n, 0)) {
574 bin += sizeof(struct rite_binary_header);
576 section_header = (const struct rite_section_header *)bin;
577 if (memcmp(section_header->section_ident, RITE_SECTION_IREP_IDENT, sizeof(section_header->section_ident)) == 0) {
578 irep = read_section_irep(mrb, bin, flags);
579 if (!irep) return NULL;
581 else if (memcmp(section_header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(section_header->section_ident)) == 0) {
582 if (!irep) return NULL; /* corrupted data */
583 result = read_section_lineno(mrb, bin, irep);
584 if (result < MRB_DUMP_OK) {
588 else if (memcmp(section_header->section_ident, RITE_SECTION_DEBUG_IDENT, sizeof(section_header->section_ident)) == 0) {
589 if (!irep) return NULL; /* corrupted data */
590 result = read_section_debug(mrb, bin, irep, flags);
591 if (result < MRB_DUMP_OK) {
595 else if (memcmp(section_header->section_ident, RITE_SECTION_LV_IDENT, sizeof(section_header->section_ident)) == 0) {
596 if (!irep) return NULL;
597 result = read_section_lv(mrb, bin, irep, flags);
598 if (result < MRB_DUMP_OK) {
602 bin += bin_to_uint32(section_header->section_size);
603 } while (memcmp(section_header->section_ident, RITE_BINARY_EOF, sizeof(section_header->section_ident)) != 0);
609 mrb_read_irep(mrb_state *mrb, const uint8_t *bin)
611 #ifdef MRB_USE_ETEXT_EDATA
612 uint8_t flags = mrb_ro_data_p((char*)bin) ? FLAG_SRC_STATIC : FLAG_SRC_MALLOC;
614 uint8_t flags = FLAG_SRC_STATIC;
617 return read_irep(mrb, bin, flags);
620 void mrb_exc_set(mrb_state *mrb, mrb_value exc);
623 irep_error(mrb_state *mrb)
625 mrb_exc_set(mrb, mrb_exc_new_str_lit(mrb, E_SCRIPT_ERROR, "irep load error"));
628 void mrb_codedump_all(mrb_state*, struct RProc*);
631 load_irep(mrb_state *mrb, mrb_irep *irep, mrbc_context *c)
637 return mrb_nil_value();
639 proc = mrb_proc_new(mrb, irep);
641 mrb_irep_decref(mrb, irep);
642 if (c && c->dump_result) mrb_codedump_all(mrb, proc);
643 if (c && c->no_exec) return mrb_obj_value(proc);
644 return mrb_top_run(mrb, proc, mrb_top_self(mrb), 0);
648 mrb_load_irep_cxt(mrb_state *mrb, const uint8_t *bin, mrbc_context *c)
650 return load_irep(mrb, mrb_read_irep(mrb, bin), c);
654 mrb_load_irep(mrb_state *mrb, const uint8_t *bin)
656 return mrb_load_irep_cxt(mrb, bin, NULL);
659 #ifndef MRB_DISABLE_STDIO
662 mrb_read_irep_file(mrb_state *mrb, FILE* fp)
664 mrb_irep *irep = NULL;
666 const size_t header_size = sizeof(struct rite_binary_header);
671 if ((mrb == NULL) || (fp == NULL)) {
675 buf = (uint8_t*)mrb_malloc(mrb, header_size);
676 if (fread(buf, header_size, 1, fp) == 0) {
679 result = read_binary_header(buf, &buf_size, NULL, &flags);
680 if (result != MRB_DUMP_OK || buf_size <= header_size) {
684 buf = (uint8_t*)mrb_realloc(mrb, buf, buf_size);
685 if (fread(buf+header_size, buf_size-header_size, 1, fp) == 0) {
688 irep = read_irep(mrb, buf, FLAG_SRC_MALLOC);
696 mrb_load_irep_file_cxt(mrb_state *mrb, FILE* fp, mrbc_context *c)
698 return load_irep(mrb, mrb_read_irep_file(mrb, fp), c);
702 mrb_load_irep_file(mrb_state *mrb, FILE* fp)
704 return mrb_load_irep_file_cxt(mrb, fp, NULL);
706 #endif /* MRB_DISABLE_STDIO */