From 2e0159fe467ac3ea9b89394ff2e8be77263a01d2 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 23 Jun 2021 20:53:11 -0700 Subject: New: stack overflow protection. * configure: detect getrlimit, producing HAVE_RLIMIT in config.h. * eval.c (do_eval, do_expand): Call gc_stack_check inline function to check stack pointer against limit. * gc.c (gc_stack_bottom): Static becomes extern, so inline function in gc.h can refer to it. (gc_stack_limit): New global variable. (gc_init): If we have rlimit, then probe RLIMIT_STACK. If the stack is sufficiently large, then enable the stack overflow protection, which kicks in when the stack pointer appears to be within a certain percentage of the limit. (set_stack_limit, get_stack_limit): New static functions. (gc_late_init): Register set-stack-limit and get-stack-limit intrinsics. (gc_stack_overflow): New function. * gc.h (gc_stack_bottom, gc_stack_limit, gc_stack_overflow): Declared. (gc_stack_check): New inline function. * lib.c (stack_overflow_s): New symbol variable. (obj_print_impl): Call gc_stack_check to protect recursive printing againts overflow. * lib.h (stack_overflow_s): Declared. * unwind.c (uw_init): Register stack-overflow symbol as a an exception symbol subtyped from error. (uw_unwind_to_exit_point): When dealing with an unhandled exception, turn off the stack limit, so we can print the messages without triggering it in a loop. * vm.c (vm_execute_closure, vm_funcall_common): Insert gc_stack_check to the top of the execution of every VM function. * txr.1: Documented. * share/txr/stdlib/doc-syms.tl: Updated. --- configure | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'configure') diff --git a/configure b/configure index 26a0f225..0f2b97ff 100755 --- a/configure +++ b/configure @@ -3721,6 +3721,25 @@ else printf "no\n" fi +printf "Checking for getrlimit ... " +cat > conftest.c < + +int main(void) +{ + struct rlimit rl; + int res = getrlimit(RLIMIT_STACK, &rl); + return 0; +} +! + +if conftest ; then + printf "yes\n" + printf "#define HAVE_RLIMIT 1\n" >> config.h +else + printf "no\n" +fi + # # Dependent variables # -- cgit v1.2.3