From 23668698cba9bf861c697e65e3d07f9433396b8b Mon Sep 17 00:00:00 2001
From: Paul Sokolovsky <pfalcon@users.sourceforge.net>
Date: Wed, 25 Jun 2014 03:03:34 +0300
Subject: [PATCH] py: Add portable framework to query/check C stack usage.

Such mechanism is important to get stable Python functioning, because Python
function calling is handled with C stack. The idea is to sprinkle
STACK_CHECK() calls in places where there can be C recursion.

TODO: Add more STACK_CHECK()'s.
---
 py/mpconfig.h  |  6 +++++
 py/objfun.c    |  6 +++--
 py/py.mk       |  1 +
 py/stackctrl.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
 py/stackctrl.h | 41 ++++++++++++++++++++++++++++++++
 unix/main.c    | 11 ++++-----
 6 files changed, 119 insertions(+), 9 deletions(-)
 create mode 100644 py/stackctrl.c
 create mode 100644 py/stackctrl.h

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/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);
 
-- 
GitLab