Skip to content

Commit 75466f9

Browse files
committed
mruby-compiler: use mrb_basic_alloc_func() directly from compiler
As a side effect, memory allocation failure may cause segmentation fault. Maybe we have to add error detection later.
1 parent 9ef2f55 commit 75466f9

2 files changed

Lines changed: 40 additions & 41 deletions

File tree

mrbgems/mruby-compiler/core/codegen.c

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
#include <string.h>
2121
#include <mruby/internal.h>
2222

23+
#define mrbc_malloc(s) mrb_basic_alloc_func(NULL,(s))
24+
#define mrbc_realloc(p,s) mrb_basic_alloc_func((p),(s))
25+
#define mrbc_free(p) mrb_basic_alloc_func((p),0)
26+
2327
#ifndef MRB_CODEGEN_LEVEL_MAX
2428
#define MRB_CODEGEN_LEVEL_MAX 256
2529
#endif
@@ -159,15 +163,6 @@ codegen_palloc(codegen_scope *s, size_t len)
159163
return p;
160164
}
161165

162-
static void*
163-
codegen_realloc(codegen_scope *s, void *p, size_t len)
164-
{
165-
p = mrb_realloc_simple(s->mrb, p, len);
166-
167-
if (!p && len > 0) codegen_error(s, "mrb_realloc");
168-
return p;
169-
}
170-
171166
static void
172167
check_no_ext_ops(codegen_scope *s, uint16_t a, uint16_t b)
173168
{
@@ -195,9 +190,9 @@ emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
195190
else {
196191
s->icapa *= 2;
197192
}
198-
s->iseq = (mrb_code*)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa);
193+
s->iseq = (mrb_code*)mrbc_realloc(s->iseq, sizeof(mrb_code)*s->icapa);
199194
if (s->lines) {
200-
s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->icapa);
195+
s->lines = (uint16_t*)mrbc_realloc(s->lines, sizeof(uint16_t)*s->icapa);
201196
}
202197
}
203198
if (s->lines) {
@@ -805,11 +800,11 @@ realloc_pool_str(codegen_scope *s, mrb_irep_pool *p, mrb_int len)
805800
{
806801
char *str;
807802
if ((p->tt & 3) == IREP_TT_SSTR) {
808-
str = (char*)codegen_realloc(s, NULL, len+1);
803+
str = (char*)mrbc_malloc(len+1);
809804
}
810805
else {
811806
str = (char*)p->u.str;
812-
str = (char*)codegen_realloc(s, str, len+1);
807+
str = (char*)mrbc_realloc(str, len+1);
813808
}
814809
p->tt = (uint32_t)(len<<2 | IREP_TT_STR);
815810
str[len] = '\0';
@@ -820,7 +815,7 @@ static void
820815
free_pool_str(codegen_scope *s, mrb_irep_pool *p)
821816
{
822817
if ((p->tt & 3) != IREP_TT_SSTR) {
823-
codegen_realloc(s, (char*)p->u.str, 0);
818+
mrbc_free((char*)p->u.str);
824819
}
825820
p->u.str = NULL;
826821
s->irep->plen--;
@@ -1095,7 +1090,7 @@ lit_pool_extend(codegen_scope *s)
10951090
{
10961091
if (s->irep->plen == s->pcapa) {
10971092
s->pcapa *= 2;
1098-
s->pool = (mrb_irep_pool*)codegen_realloc(s, s->pool, sizeof(mrb_irep_pool)*s->pcapa);
1093+
s->pool = (mrb_irep_pool*)mrbc_realloc(s->pool, sizeof(mrb_irep_pool)*s->pcapa);
10991094
}
11001095

11011096
return &s->pool[s->irep->plen++];
@@ -1125,7 +1120,7 @@ new_litbint(codegen_scope *s, const char *p, int base)
11251120

11261121
char *buf;
11271122
pv->tt = IREP_TT_BIGINT;
1128-
buf = (char*)codegen_realloc(s, NULL, plen+3);
1123+
buf = (char*)mrbc_malloc(plen+3);
11291124
buf[0] = (char)plen;
11301125
buf[1] = base;
11311126
memcpy(buf+2, p, plen);
@@ -1172,7 +1167,7 @@ new_lit_str2(codegen_scope *s, const char *str1, mrb_int len1, const char *str2,
11721167
else {
11731168
char *p;
11741169
pool->tt = (uint32_t)(len<<2) | IREP_TT_STR;
1175-
p = (char*)codegen_realloc(s, NULL, len+1);
1170+
p = (char*)mrbc_malloc(len+1);
11761171
memcpy(p, str1, len1);
11771172
if (str2) memcpy(p+len1, str2, len2);
11781173
p[len] = '\0';
@@ -1266,7 +1261,7 @@ new_sym(codegen_scope *s, mrb_sym sym)
12661261
if (s->scapa > 0xffff) {
12671262
codegen_error(s, "too many symbols");
12681263
}
1269-
s->syms = (mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*s->scapa);
1264+
s->syms = (mrb_sym*)mrbc_realloc(s->syms, sizeof(mrb_sym)*s->scapa);
12701265
}
12711266
s->syms[s->irep->slen] = sym;
12721267
return s->irep->slen++;
@@ -3968,7 +3963,7 @@ scope_add_irep(codegen_scope *s)
39683963
s->irep = irep = mrb_add_irep(s->mrb);
39693964
if (prev->irep->rlen == prev->rcapa) {
39703965
prev->rcapa *= 2;
3971-
prev->reps = (mrb_irep**)codegen_realloc(s, prev->reps, sizeof(mrb_irep*)*prev->rcapa);
3966+
prev->reps = (mrb_irep**)mrbc_realloc(prev->reps, sizeof(mrb_irep*)*prev->rcapa);
39723967
}
39733968
prev->reps[prev->irep->rlen] = irep;
39743969
prev->irep->rlen++;
@@ -3998,16 +3993,16 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
39983993
scope_add_irep(s);
39993994

40003995
s->rcapa = 8;
4001-
s->reps = (mrb_irep**)mrb_malloc(mrb, sizeof(mrb_irep*)*s->rcapa);
3996+
s->reps = (mrb_irep**)mrbc_malloc(sizeof(mrb_irep*)*s->rcapa);
40023997

40033998
s->icapa = 1024;
4004-
s->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*s->icapa);
3999+
s->iseq = (mrb_code*)mrbc_malloc(sizeof(mrb_code)*s->icapa);
40054000

40064001
s->pcapa = 32;
4007-
s->pool = (mrb_irep_pool*)mrb_malloc(mrb, sizeof(mrb_irep_pool)*s->pcapa);
4002+
s->pool = (mrb_irep_pool*)mrbc_malloc(sizeof(mrb_irep_pool)*s->pcapa);
40084003

40094004
s->scapa = 256;
4010-
s->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*s->scapa);
4005+
s->syms = (mrb_sym*)mrbc_malloc(sizeof(mrb_sym)*s->scapa);
40114006

40124007
s->lv = nlv;
40134008
s->sp += node_len(nlv)+1; /* add self */
@@ -4017,7 +4012,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
40174012
node *n = nlv;
40184013
size_t i = 0;
40194014

4020-
s->irep->lv = lv = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*(s->nlocals-1));
4015+
s->irep->lv = lv = (mrb_sym*)mrbc_malloc(sizeof(mrb_sym)*(s->nlocals-1));
40214016
for (i=0, n=nlv; n; i++,n=n->cdr) {
40224017
lv[i] = lv_name(n);
40234018
}
@@ -4027,7 +4022,7 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *nlv)
40274022

40284023
s->filename_sym = prev->filename_sym;
40294024
if (s->filename_sym) {
4030-
s->lines = (uint16_t*)mrb_malloc(mrb, sizeof(short)*s->icapa);
4025+
s->lines = (uint16_t*)mrbc_malloc(sizeof(short)*s->icapa);
40314026
}
40324027
s->lineno = prev->lineno;
40334028

@@ -4059,7 +4054,7 @@ scope_finish(codegen_scope *s)
40594054
irep->flags = 0;
40604055
if (s->iseq) {
40614056
size_t catchsize = sizeof(struct mrb_irep_catch_handler) * irep->clen;
4062-
irep->iseq = (const mrb_code*)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc + catchsize);
4057+
irep->iseq = (const mrb_code*)mrbc_realloc(s->iseq, sizeof(mrb_code)*s->pc + catchsize);
40634058
irep->ilen = s->pc;
40644059
if (irep->clen > 0) {
40654060
memcpy((void*)(irep->iseq + irep->ilen), s->catch_table, catchsize);
@@ -4068,11 +4063,11 @@ scope_finish(codegen_scope *s)
40684063
else {
40694064
irep->clen = 0;
40704065
}
4071-
mrb_free(s->mrb, s->catch_table);
4066+
mrbc_free(s->catch_table);
40724067
s->catch_table = NULL;
4073-
irep->pool = (const mrb_irep_pool*)codegen_realloc(s, s->pool, sizeof(mrb_irep_pool)*irep->plen);
4074-
irep->syms = (const mrb_sym*)codegen_realloc(s, s->syms, sizeof(mrb_sym)*irep->slen);
4075-
irep->reps = (const mrb_irep**)codegen_realloc(s, s->reps, sizeof(mrb_irep*)*irep->rlen);
4068+
irep->pool = (const mrb_irep_pool*)mrbc_realloc(s->pool, sizeof(mrb_irep_pool)*irep->plen);
4069+
irep->syms = (const mrb_sym*)mrbc_realloc(s->syms, sizeof(mrb_sym)*irep->slen);
4070+
irep->reps = (const mrb_irep**)mrbc_realloc(s->reps, sizeof(mrb_irep*)*irep->rlen);
40764071
if (s->filename_sym) {
40774072
mrb_sym fname = mrb_parser_get_filename(s->parser, s->filename_index);
40784073
const char *filename = mrb_sym_name_len(s->mrb, fname, NULL);
@@ -4177,7 +4172,7 @@ static int
41774172
catch_handler_new(codegen_scope *s)
41784173
{
41794174
size_t newsize = sizeof(struct mrb_irep_catch_handler) * (s->irep->clen + 1);
4180-
s->catch_table = (struct mrb_irep_catch_handler*)codegen_realloc(s, (void*)s->catch_table, newsize);
4175+
s->catch_table = (struct mrb_irep_catch_handler*)mrbc_realloc((void*)s->catch_table, newsize);
41814176
return s->irep->clen++;
41824177
}
41834178

mrbgems/mruby-compiler/core/parse.y

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727

2828
#define YYLEX_PARAM p
2929

30+
#define mrbc_malloc(s) mrb_basic_alloc_func(NULL,(s))
31+
#define mrbc_realloc(p,s) mrb_basic_alloc_func((p),(s))
32+
#define mrbc_free(p) mrb_basic_alloc_func((p),0)
33+
3034
typedef mrb_ast_node node;
3135
typedef struct mrb_parser_state parser_state;
3236
typedef struct mrb_parser_heredoc_info parser_heredoc_info;
@@ -4489,7 +4493,7 @@ static int
44894493
newtok(parser_state *p)
44904494
{
44914495
if (p->tokbuf != p->buf) {
4492-
mrb_free(p->mrb, p->tokbuf);
4496+
mrbc_free(p->tokbuf);
44934497
p->tokbuf = p->buf;
44944498
p->tsiz = MRB_PARSER_TOKBUF_SIZE;
44954499
}
@@ -4542,11 +4546,11 @@ tokadd(parser_state *p, int32_t c)
45424546
}
45434547
p->tsiz *= 2;
45444548
if (p->tokbuf == p->buf) {
4545-
p->tokbuf = (char*)mrb_malloc(p->mrb, p->tsiz);
4549+
p->tokbuf = (char*)mrbc_malloc(p->tsiz);
45464550
memcpy(p->tokbuf, p->buf, MRB_PARSER_TOKBUF_SIZE);
45474551
}
45484552
else {
4549-
p->tokbuf = (char*)mrb_realloc(p->mrb, p->tokbuf, p->tsiz);
4553+
p->tokbuf = (char*)mrbc_realloc(p->tokbuf, p->tsiz);
45504554
}
45514555
}
45524556
for (i = 0; i < len; i++) {
@@ -6633,7 +6637,7 @@ parser_update_cxt(parser_state *p, mrb_ccontext *cxt)
66336637
i++;
66346638
n = n->cdr;
66356639
}
6636-
cxt->syms = (mrb_sym*)mrb_realloc(p->mrb, cxt->syms, i*sizeof(mrb_sym));
6640+
cxt->syms = (mrb_sym*)mrbc_realloc(cxt->syms, i*sizeof(mrb_sym));
66376641
cxt->slen = i;
66386642
for (i=0, n=n0; n; i++,n=n->cdr) {
66396643
cxt->syms[i] = sym(n->car);
@@ -6726,7 +6730,7 @@ mrb_parser_new(mrb_state *mrb)
67266730
MRB_API void
67276731
mrb_parser_free(parser_state *p) {
67286732
if (p->tokbuf != p->buf) {
6729-
mrb_free(p->mrb, p->tokbuf);
6733+
mrbc_free(p->tokbuf);
67306734
}
67316735
mempool_close(p->pool);
67326736
}
@@ -6740,22 +6744,22 @@ mrb_ccontext_new(mrb_state *mrb)
67406744
MRB_API void
67416745
mrb_ccontext_free(mrb_state *mrb, mrb_ccontext *cxt)
67426746
{
6743-
mrb_free(mrb, cxt->filename);
6744-
mrb_free(mrb, cxt->syms);
6745-
mrb_free(mrb, cxt);
6747+
mrbc_free(cxt->filename);
6748+
mrbc_free(cxt->syms);
6749+
mrbc_free(cxt);
67466750
}
67476751

67486752
MRB_API const char*
67496753
mrb_ccontext_filename(mrb_state *mrb, mrb_ccontext *c, const char *s)
67506754
{
67516755
if (s) {
67526756
size_t len = strlen(s);
6753-
char *p = (char*)mrb_malloc_simple(mrb, len + 1);
6757+
char *p = (char*)mrbc_malloc(len + 1);
67546758

67556759
if (p == NULL) return NULL;
67566760
memcpy(p, s, len + 1);
67576761
if (c->filename) {
6758-
mrb_free(mrb, c->filename);
6762+
mrbc_free(c->filename);
67596763
}
67606764
c->filename = p;
67616765
}

0 commit comments

Comments
 (0)