diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-03-31 09:34:08 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-03-31 09:34:08 (GMT) |
commit | f0480750796df39b6c356fbfe1cdcb395725654c (patch) | |
tree | c084fe744e891d53ee9a995cbd65d5d4f9a07a93 /Modules | |
parent | 702624ecf7fa0b7247b2bd07962db30d3e53885d (diff) | |
download | cpython-f0480750796df39b6c356fbfe1cdcb395725654c.zip cpython-f0480750796df39b6c356fbfe1cdcb395725654c.tar.gz cpython-f0480750796df39b6c356fbfe1cdcb395725654c.tar.bz2 |
Issue #11393: limit stack overflow test to 100 MB
Stop if the stack overflow doesn't occur after allocating 100 MB on the stack.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/faulthandler.c | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/Modules/faulthandler.c b/Modules/faulthandler.c index 8ef998d..e8ed3e8 100644 --- a/Modules/faulthandler.c +++ b/Modules/faulthandler.c @@ -16,6 +16,9 @@ # define FAULTHANDLER_USER #endif +/* Allocate at maximum 100 MB of the stack to raise the stack overflow */ +#define STACK_OVERFLOW_MAX_SIZE (100*1024*1024) + #define PUTS(fd, str) write(fd, str, strlen(str)) #ifdef HAVE_SIGACTION @@ -742,15 +745,39 @@ faulthandler_fatal_error_py(PyObject *self, PyObject *args) } #if defined(HAVE_SIGALTSTACK) && defined(HAVE_SIGACTION) -static PyObject * -faulthandler_stack_overflow(PyObject *self) +void* +stack_overflow(void *min_sp, void *max_sp, size_t *depth) { /* allocate 4096 bytes on the stack at each call */ unsigned char buffer[4096]; + void *sp = &buffer; + *depth += 1; + if (sp < min_sp || max_sp < sp) + return sp; buffer[0] = 1; - buffer[4095] = 2; - faulthandler_stack_overflow(self); - return PyLong_FromLong(buffer[0] + buffer[4095]); + buffer[4095] = 0; + return stack_overflow(min_sp, max_sp, depth); +} + +static PyObject * +faulthandler_stack_overflow(PyObject *self) +{ + size_t depth, size; + void *sp = &depth, *stop; + + depth = 0; + stop = stack_overflow(sp - STACK_OVERFLOW_MAX_SIZE, + sp + STACK_OVERFLOW_MAX_SIZE, + &depth); + if (sp < stop) + size = stop - sp; + else + size = sp - stop; + PyErr_Format(PyExc_RuntimeError, + "unable to raise a stack overflow (allocated %zu bytes " + "on the stack, %zu recursive calls)", + size, depth); + return NULL; } #endif |