py: Add option to VM to detect stack overflow.

This commit is contained in:
Damien George 2014-04-10 16:21:34 +00:00
parent c90717a3e4
commit e90be0ddf5
1 changed files with 16 additions and 0 deletions

16
py/vm.c
View File

@ -12,6 +12,8 @@
#include "bc.h" #include "bc.h"
#include "objgenerator.h" #include "objgenerator.h"
#define DETECT_VM_STACK_OVERFLOW (0)
// Value stack grows up (this makes it incompatible with native C stack, but // Value stack grows up (this makes it incompatible with native C stack, but
// makes sure that arguments to functions are in natural order arg1..argN // makes sure that arguments to functions are in natural order arg1..argN
// (Python semantics mandates left-to-right evaluation order, including for // (Python semantics mandates left-to-right evaluation order, including for
@ -74,9 +76,16 @@ mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args,
// allocate state for locals and stack // allocate state for locals and stack
mp_obj_t temp_state[10]; mp_obj_t temp_state[10];
mp_obj_t *state = &temp_state[0]; mp_obj_t *state = &temp_state[0];
#if DETECT_VM_STACK_OVERFLOW
if (n_state + 1 > 10) {
state = m_new(mp_obj_t, n_state + 1);
}
state[n_state - n_args - n_args2] = (void*)0xdeadbeef;
#else
if (n_state > 10) { if (n_state > 10) {
state = m_new(mp_obj_t, n_state); state = m_new(mp_obj_t, n_state);
} }
#endif
mp_obj_t *sp = &state[0] - 1; mp_obj_t *sp = &state[0] - 1;
// allocate state for exceptions // allocate state for exceptions
@ -109,6 +118,13 @@ mp_vm_return_kind_t mp_execute_byte_code(const byte *code, const mp_obj_t *args,
// execute the byte code // execute the byte code
mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp, exc_stack, &exc_sp, MP_OBJ_NULL); mp_vm_return_kind_t vm_return_kind = mp_execute_byte_code_2(code, &ip, &state[n_state - 1], &sp, exc_stack, &exc_sp, MP_OBJ_NULL);
#if DETECT_VM_STACK_OVERFLOW
if (state[n_state - n_args - n_args2] != (void*)0xdeadbeef) {
printf("VM stack overflow\n");
assert(0);
}
#endif
switch (vm_return_kind) { switch (vm_return_kind) {
case MP_VM_RETURN_NORMAL: case MP_VM_RETURN_NORMAL:
*ret = *sp; *ret = *sp;