summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/binary.c48
-rw-r--r--py/binary.h1
-rw-r--r--py/mpconfig.h6
-rw-r--r--py/objfun.c6
-rw-r--r--py/py.mk1
-rw-r--r--py/stackctrl.c63
-rw-r--r--py/stackctrl.h41
7 files changed, 143 insertions, 23 deletions
diff --git a/py/binary.c b/py/binary.c
index 9eab01c4eb..d755bc86e0 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -125,24 +125,9 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
-#define is_signed(typecode) (typecode > 'Z')
-mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
- byte *p = *ptr;
- uint align;
-
- int size = mp_binary_get_size(struct_type, val_type, &align);
- if (struct_type == '@') {
- // Make pointer aligned
- p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
- #if MP_ENDIANNESS_LITTLE
- struct_type = '<';
- #else
- struct_type = '>';
- #endif
- }
-
+machine_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p) {
int delta;
- if (struct_type == '<') {
+ if (!big_endian) {
delta = -1;
p += size - 1;
} else {
@@ -150,7 +135,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
}
machine_int_t val = 0;
- if (is_signed(val_type) && *p & 0x80) {
+ if (is_signed && *p & 0x80) {
val = -1;
}
for (uint i = 0; i < size; i++) {
@@ -159,7 +144,28 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
p += delta;
}
- *ptr += size;
+ return val;
+}
+
+#define is_signed(typecode) (typecode > 'Z')
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
+ byte *p = *ptr;
+ uint align;
+
+ int size = mp_binary_get_size(struct_type, val_type, &align);
+ if (struct_type == '@') {
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~((machine_uint_t)align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
+ }
+ *ptr = p + size;
+
+ machine_int_t val = mp_binary_get_int(size, is_signed(val_type), (struct_type == '>'), p);
+
if (val_type == 'O') {
return (mp_obj_t)val;
} else if (val_type == 'S') {
@@ -178,13 +184,14 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
int size = mp_binary_get_size(struct_type, val_type, &align);
if (struct_type == '@') {
// Make pointer aligned
- p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~((machine_uint_t)align - 1));
#if MP_ENDIANNESS_LITTLE
struct_type = '<';
#else
struct_type = '>';
#endif
}
+ *ptr = p + size;
#if MP_ENDIANNESS_BIG
#error Not implemented
@@ -215,7 +222,6 @@ void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **
in += in_delta;
}
- *ptr += size;
}
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
diff --git a/py/binary.h b/py/binary.h
index f15a2fd7fb..63ea5d741e 100644
--- a/py/binary.h
+++ b/py/binary.h
@@ -34,3 +34,4 @@ void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in)
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val);
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr);
+machine_int_t mp_binary_get_int(uint size, bool is_signed, bool big_endian, byte *p);
diff --git a/py/mpconfig.h b/py/mpconfig.h
index 4a3288a3d1..d7504c1402 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -157,6 +157,12 @@
#define MICROPY_ENABLE_GC_FINALISER (0)
#endif
+// Whether to check C stack usage. C stack used for calling Python functions,
+// etc. Not checking means segfault on overflow.
+#ifndef MICROPY_STACK_CHECK
+#define MICROPY_STACK_CHECK (1)
+#endif
+
// Whether to include REPL helper function
#ifndef MICROPY_HELPER_REPL
#define MICROPY_HELPER_REPL (0)
diff --git a/py/objfun.c b/py/objfun.c
index 29363129b2..f75e9142a2 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -39,6 +39,7 @@
#include "runtime0.h"
#include "runtime.h"
#include "bc.h"
+#include "stackctrl.h"
#if 0 // print debugging info
#define DEBUG_PRINT (1)
@@ -204,6 +205,8 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, uint expected, ui
// code_state should have ->ip filled in (pointing past code info block),
// as well as ->n_state.
void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
+ // This function is pretty complicated. It's main aim is to be efficient in speed and RAM
+ // usage for the common case of positional only args.
mp_obj_fun_bc_t *self = self_in;
machine_uint_t n_state = code_state->n_state;
const byte *ip = code_state->ip;
@@ -353,8 +356,7 @@ continue2:;
STATIC mp_obj_t fun_bc_call(mp_obj_t self_in, uint n_args, uint n_kw, const mp_obj_t *args) {
- // This function is pretty complicated. It's main aim is to be efficient in speed and RAM
- // usage for the common case of positional only args.
+ STACK_CHECK();
DEBUG_printf("Input n_args: %d, n_kw: %d\n", n_args, n_kw);
DEBUG_printf("Input pos args: ");
diff --git a/py/py.mk b/py/py.mk
index 2b74f252cf..374bb11d61 100644
--- a/py/py.mk
+++ b/py/py.mk
@@ -43,6 +43,7 @@ PY_O_BASENAME = \
parsenum.o \
emitglue.o \
runtime.o \
+ stackctrl.o \
argcheck.o \
map.o \
obj.o \
diff --git a/py/stackctrl.c b/py/stackctrl.c
new file mode 100644
index 0000000000..aef9bad9e9
--- /dev/null
+++ b/py/stackctrl.c
@@ -0,0 +1,63 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "mpconfig.h"
+#include "misc.h"
+#include "nlr.h"
+#include "qstr.h"
+#include "obj.h"
+#include "runtime.h"
+#include "stackctrl.h"
+
+// Stack top at the start of program
+char *stack_top;
+
+void stack_ctrl_init() {
+ volatile int stack_dummy;
+ stack_top = (char*)&stack_dummy;
+}
+
+uint stack_usage() {
+ // Assumes descending stack
+ volatile int stack_dummy;
+ return stack_top - (char*)&stack_dummy;
+}
+
+#if MICROPY_STACK_CHECK
+
+uint stack_limit = 10240;
+
+void stack_set_limit(uint limit) {
+ stack_limit = limit;
+}
+
+void stack_check() {
+ if (stack_usage() >= stack_limit) {
+ nlr_raise(mp_obj_new_exception_msg(&mp_type_RuntimeError, "maximum recursion depth exceeded"));
+ }
+}
+
+#endif // MICROPY_STACK_CHECK
diff --git a/py/stackctrl.h b/py/stackctrl.h
new file mode 100644
index 0000000000..a9a8d2e2d8
--- /dev/null
+++ b/py/stackctrl.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2014 Paul Sokolovsky
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+void stack_ctrl_init();
+uint stack_usage();
+
+#if MICROPY_STACK_CHECK
+
+void stack_set_limit(uint limit);
+void stack_check();
+#define STACK_CHECK() stack_check()
+
+#else
+
+#define stack_set_limit(limit)
+#define STACK_CHECK()
+
+#endif