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.
pull/715/merge
Paul Sokolovsky 2014-06-25 03:03:34 +03:00
rodzic 91b576d147
commit 23668698cb
6 zmienionych plików z 119 dodań i 9 usunięć

Wyświetl plik

@ -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)

Wyświetl plik

@ -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: ");

Wyświetl plik

@ -43,6 +43,7 @@ PY_O_BASENAME = \
parsenum.o \
emitglue.o \
runtime.o \
stackctrl.o \
argcheck.o \
map.o \
obj.o \

63
py/stackctrl.c 100644
Wyświetl plik

@ -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

41
py/stackctrl.h 100644
Wyświetl plik

@ -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

Wyświetl plik

@ -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);