summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.travis.yml2
-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
-rw-r--r--stmhal/boards/HYDRABUS/mpconfigboard.h1
-rw-r--r--stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h1
-rw-r--r--stmhal/boards/PYBV10/mpconfigboard.h1
-rw-r--r--stmhal/boards/PYBV3/mpconfigboard.h1
-rw-r--r--stmhal/boards/PYBV4/mpconfigboard.h1
-rw-r--r--stmhal/boards/STM32F4DISC/mpconfigboard.h1
-rw-r--r--stmhal/main.c6
-rw-r--r--stmhal/pin.c8
-rw-r--r--stmhal/pyexec.c2
-rw-r--r--tests/basics/struct1.py4
-rw-r--r--tests/misc/recursion.py7
-rw-r--r--unix/main.c11
20 files changed, 176 insertions, 36 deletions
diff --git a/.travis.yml b/.travis.yml
index e6c12b967..489cceaa2 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -20,4 +20,4 @@ script:
- (cd tests && MICROPY_CPYTHON3=python3.3 ./run-tests)
after_failure:
- - (cd tests && for exp in *.exp; do testbase=$(basename $exp .exp); echo -e "\nFAILURE $testbase"; diff $testbase.exp $testbase.out; done)
+ - (cd tests && for exp in *.exp; do testbase=$(basename $exp .exp); echo -e "\nFAILURE $testbase"; diff -u $testbase.exp $testbase.out; done)
diff --git a/py/binary.c b/py/binary.c
index 9eab01c4e..d755bc86e 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 f15a2fd7f..63ea5d741 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 4a3288a3d..d7504c140 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 29363129b..f75e9142a 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 2b74f252c..374bb11d6 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 000000000..aef9bad9e
--- /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 000000000..a9a8d2e2d
--- /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
diff --git a/stmhal/boards/HYDRABUS/mpconfigboard.h b/stmhal/boards/HYDRABUS/mpconfigboard.h
index f87a14142..8480c5bf0 100644
--- a/stmhal/boards/HYDRABUS/mpconfigboard.h
+++ b/stmhal/boards/HYDRABUS/mpconfigboard.h
@@ -1,6 +1,7 @@
#define HYDRABUSV10
#define MICROPY_HW_BOARD_NAME "HydraBus1.0"
+#define MICROPY_HW_MICRO_NAME "STM32F4"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_SDCARD (1)
diff --git a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h
index 0e4054525..d7ee97ac4 100644
--- a/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h
+++ b/stmhal/boards/NETDUINO_PLUS_2/mpconfigboard.h
@@ -1,6 +1,7 @@
#define NETDUINO_PLUS_2
#define MICROPY_HW_BOARD_NAME "NetduinoPlus2"
+#define MICROPY_HW_MICRO_NAME "STM32F405RG"
#define MICROPY_HW_HAS_SWITCH (1)
diff --git a/stmhal/boards/PYBV10/mpconfigboard.h b/stmhal/boards/PYBV10/mpconfigboard.h
index 3def53123..8c31d4388 100644
--- a/stmhal/boards/PYBV10/mpconfigboard.h
+++ b/stmhal/boards/PYBV10/mpconfigboard.h
@@ -1,6 +1,7 @@
#define PYBV10
#define MICROPY_HW_BOARD_NAME "PYBv1.0"
+#define MICROPY_HW_MICRO_NAME "STM32F405RG"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_SDCARD (1)
diff --git a/stmhal/boards/PYBV3/mpconfigboard.h b/stmhal/boards/PYBV3/mpconfigboard.h
index ac0d84ca2..e0e4dd11c 100644
--- a/stmhal/boards/PYBV3/mpconfigboard.h
+++ b/stmhal/boards/PYBV3/mpconfigboard.h
@@ -1,6 +1,7 @@
#define PYBV3
#define MICROPY_HW_BOARD_NAME "PYBv3"
+#define MICROPY_HW_MICRO_NAME "STM32F405RG"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_SDCARD (1)
diff --git a/stmhal/boards/PYBV4/mpconfigboard.h b/stmhal/boards/PYBV4/mpconfigboard.h
index 9fedb7013..220e3b4f6 100644
--- a/stmhal/boards/PYBV4/mpconfigboard.h
+++ b/stmhal/boards/PYBV4/mpconfigboard.h
@@ -1,6 +1,7 @@
#define PYBV4
#define MICROPY_HW_BOARD_NAME "PYBv4"
+#define MICROPY_HW_MICRO_NAME "STM32F405RG"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_SDCARD (1)
diff --git a/stmhal/boards/STM32F4DISC/mpconfigboard.h b/stmhal/boards/STM32F4DISC/mpconfigboard.h
index e6780eacb..7a553c5e8 100644
--- a/stmhal/boards/STM32F4DISC/mpconfigboard.h
+++ b/stmhal/boards/STM32F4DISC/mpconfigboard.h
@@ -1,6 +1,7 @@
#define STM32F4DISC
#define MICROPY_HW_BOARD_NAME "F4DISC"
+#define MICROPY_HW_MICRO_NAME "STM32F407"
#define MICROPY_HW_HAS_SWITCH (1)
#define MICROPY_HW_HAS_SDCARD (0)
diff --git a/stmhal/main.c b/stmhal/main.c
index e264fdf4b..4e50daba4 100644
--- a/stmhal/main.c
+++ b/stmhal/main.c
@@ -40,6 +40,7 @@
#include "parse.h"
#include "obj.h"
#include "runtime.h"
+#include "stackctrl.h"
#include "gc.h"
#include "gccollect.h"
#include "pybstdio.h"
@@ -186,6 +187,11 @@ static const char fresh_readme_txt[] =
int main(void) {
// TODO disable JTAG
+ stack_ctrl_init();
+ // Stack limit should be less than real stack size, so we
+ // had chance to recover from limit hit.
+ stack_set_limit(&_ram_end - &_heap_end - 512);
+
/* STM32F4xx HAL library initialization:
- Configure the Flash prefetch, instruction and Data caches
- Configure the Systick to generate an interrupt each 1 msec
diff --git a/stmhal/pin.c b/stmhal/pin.c
index 9806a8c49..449b5074f 100644
--- a/stmhal/pin.c
+++ b/stmhal/pin.c
@@ -100,8 +100,8 @@ STATIC mp_obj_t pin_class_map_dict;
STATIC bool pin_class_debug;
void pin_init(void) {
- pin_class_mapper = MP_OBJ_NULL;
- pin_class_map_dict = MP_OBJ_NULL;
+ pin_class_mapper = mp_const_none;
+ pin_class_map_dict = mp_const_none;
pin_class_debug = false;
}
@@ -120,7 +120,7 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) {
return pin_obj;
}
- if (pin_class_mapper != MP_OBJ_NULL) {
+ if (pin_class_mapper != mp_const_none) {
pin_obj = mp_call_function_1(pin_class_mapper, user_obj);
if (pin_obj != mp_const_none) {
if (!MP_OBJ_IS_TYPE(pin_obj, &pin_type)) {
@@ -139,7 +139,7 @@ const pin_obj_t *pin_find(mp_obj_t user_obj) {
// other lookup methods.
}
- if (pin_class_map_dict != MP_OBJ_NULL) {
+ if (pin_class_map_dict != mp_const_none) {
mp_map_t *pin_map_map = mp_obj_dict_get_map(pin_class_map_dict);
mp_map_elem_t *elem = mp_map_lookup(pin_map_map, user_obj, MP_MAP_LOOKUP);
if (elem != NULL && elem->value != NULL) {
diff --git a/stmhal/pyexec.c b/stmhal/pyexec.c
index 45928427e..6dfc00776 100644
--- a/stmhal/pyexec.c
+++ b/stmhal/pyexec.c
@@ -185,7 +185,7 @@ int pyexec_friendly_repl(void) {
#endif
friendly_repl_reset:
- stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with STM32F405RG\r\n");
+ stdout_tx_str("Micro Python " MICROPY_GIT_TAG " on " MICROPY_BUILD_DATE "; " MICROPY_HW_BOARD_NAME " with " MICROPY_HW_MICRO_NAME "\r\n");
stdout_tx_str("Type \"help()\" for more information.\r\n");
// to test ctrl-C
diff --git a/tests/basics/struct1.py b/tests/basics/struct1.py
index b114a789b..c3049c55d 100644
--- a/tests/basics/struct1.py
+++ b/tests/basics/struct1.py
@@ -21,3 +21,7 @@ print(struct.calcsize("100sI"))
print(struct.calcsize("97sI"))
print(struct.unpack("<6sH", b"foo\0\0\0\x12\x34"))
print(struct.pack("<6sH", b"foo", 10000))
+
+s = struct.pack("BHBI", 10, 100, 200, 300)
+v = struct.unpack("BHBI", s)
+print(v == (10, 100, 200, 300))
diff --git a/tests/misc/recursion.py b/tests/misc/recursion.py
new file mode 100644
index 000000000..227f48396
--- /dev/null
+++ b/tests/misc/recursion.py
@@ -0,0 +1,7 @@
+def foo():
+ foo()
+
+try:
+ foo()
+except RuntimeError:
+ print("RuntimeError")
diff --git a/unix/main.c b/unix/main.c
index 552df9b80..a08661339 100644
--- a/unix/main.c
+++ b/unix/main.c
@@ -51,6 +51,7 @@
#include "gc.h"
#include "genhdr/py-version.h"
#include "input.h"
+#include "stackctrl.h"
// Command line options, with their defaults
bool compile_only = false;
@@ -63,9 +64,6 @@ uint mp_verbose_flag;
long heap_size = 128*1024 * (sizeof(machine_uint_t) / 4);
#endif
-// Stack top at the start of program
-char *stack_top;
-
void microsocket_init();
void time_init();
void ffi_init();
@@ -214,10 +212,9 @@ int usage(char **argv) {
}
mp_obj_t mem_info(void) {
- volatile int stack_dummy;
printf("mem: total=%d, current=%d, peak=%d\n",
m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated());
- printf("stack: " INT_FMT "\n", stack_top - (char*)&stack_dummy);
+ printf("stack: %u\n", stack_usage());
#if MICROPY_ENABLE_GC
gc_dump_info();
#endif
@@ -268,8 +265,8 @@ void pre_process_options(int argc, char **argv) {
#endif
int main(int argc, char **argv) {
- volatile int stack_dummy;
- stack_top = (char*)&stack_dummy;
+ stack_ctrl_init();
+ stack_set_limit(32768);
pre_process_options(argc, argv);