summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2014-04-10 16:21:34 +0000
committerDamien George <damien.p.george@gmail.com>2014-04-10 16:21:34 +0000
commite90be0ddf578605eeda9224ae169d09952a66419 (patch)
treef9e50e17a0d325ddb89f896c416a66d3e04c97ab /py
parentc90717a3e4eae703d8b2a111e80fb8957f63b348 (diff)
downloadmicropython-e90be0ddf578605eeda9224ae169d09952a66419.tar.gz
micropython-e90be0ddf578605eeda9224ae169d09952a66419.zip
py: Add option to VM to detect stack overflow.
Diffstat (limited to 'py')
-rw-r--r--py/vm.c16
1 files changed, 16 insertions, 0 deletions
diff --git a/py/vm.c b/py/vm.c
index b9147d1d32..d7d3241d71 100644
--- a/py/vm.c
+++ b/py/vm.c
@@ -12,6 +12,8 @@
#include "bc.h"
#include "objgenerator.h"
+#define DETECT_VM_STACK_OVERFLOW (0)
+
// 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
// (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
mp_obj_t temp_state[10];
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) {
state = m_new(mp_obj_t, n_state);
}
+#endif
mp_obj_t *sp = &state[0] - 1;
// 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
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) {
case MP_VM_RETURN_NORMAL:
*ret = *sp;