Skip to content

Commit

Permalink
vm.c: check call stack depth before pushing the frame.
Browse files Browse the repository at this point in the history
Existing call stack depth checks are unified into this check in
`cipush()`. The maximum depth is now specified by `MRB_CALL_LEVEL_MAX`
(the default is 512).  The older `MRB_FUNCALL_DEPTH_MAX` is no longer
used.
  • Loading branch information
matz committed Oct 8, 2021
1 parent fd6d911 commit bec074e
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions src/vm.c
Expand Up @@ -39,9 +39,9 @@ void abort(void);
#define MRB_STACK_GROWTH 128
#endif

/* Maximum mrb_funcall() depth. Should be set lower on memory constrained systems. */
#ifndef MRB_FUNCALL_DEPTH_MAX
#define MRB_FUNCALL_DEPTH_MAX 512
/* Maximum recursive depth. Should be set lower on memory constrained systems. */
#ifndef MRB_CALL_LEVEL_MAX
#define MRB_CALL_LEVEL_MAX 512
#endif

/* Maximum stack depth. Should be set lower on memory constrained systems.
Expand Down Expand Up @@ -251,6 +251,9 @@ cipush(mrb_state *mrb, mrb_int push_stacks, uint8_t cci,
if (ci + 1 == c->ciend) {
ptrdiff_t size = ci - c->cibase;

if (size > MRB_CALL_LEVEL_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
c->cibase = (mrb_callinfo *)mrb_realloc(mrb, c->cibase, sizeof(mrb_callinfo)*size*2);
c->ci = c->cibase + size;
c->ciend = c->cibase + size * 2;
Expand Down Expand Up @@ -464,9 +467,6 @@ mrb_funcall_with_block(mrb_state *mrb, mrb_value self, mrb_sym mid, mrb_int argc
mrb->c->ci->stack[n+1] = args;
argc = -1;
}
if (mrb->c->ci - mrb->c->cibase > MRB_FUNCALL_DEPTH_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
ci = cipush(mrb, n, 0, c, NULL, mid, argc);
if (argc < 0) argc = 1;
if (mrb->c->stbase <= argv && argv < mrb->c->stend) {
Expand Down Expand Up @@ -740,9 +740,6 @@ mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const mrb_value
}
ci = mrb->c->ci;
n = ci_nregs(ci);
if (ci - mrb->c->cibase > MRB_FUNCALL_DEPTH_MAX) {
mrb_exc_raise(mrb, mrb_obj_value(mrb->stack_err));
}
p = mrb_proc_ptr(b);
ci = cipush(mrb, n, CINFO_SKIP, c, p, mid, 0 /* dummy */);
if (argc >= CALL_MAXARGS) {
Expand Down

0 comments on commit bec074e

Please sign in to comment.