From 0b089e4fb0e6ec1887e54d60a10dea4b0b463c12 Mon Sep 17 00:00:00 2001 From: "Guillermo J. Rozas" Date: Wed, 8 Sep 1993 04:41:06 +0000 Subject: [PATCH] Add ability to detect stack overflow in NT/Windows. --- v7/src/microcode/bchmmg.c | 3 +- v7/src/microcode/memmag.c | 3 +- v7/src/microcode/nttrap.c | 73 ++++++++++++++++++++++++++++++++++++++- v7/src/microcode/stack.h | 20 ++++++----- 4 files changed, 87 insertions(+), 12 deletions(-) diff --git a/v7/src/microcode/bchmmg.c b/v7/src/microcode/bchmmg.c index 5d49dd8aa..390e13655 100644 --- a/v7/src/microcode/bchmmg.c +++ b/v7/src/microcode/bchmmg.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: bchmmg.c,v 9.80 1993/09/03 18:35:46 gjr Exp $ +$Id: bchmmg.c,v 9.81 1993/09/08 04:39:30 gjr Exp $ Copyright (c) 1987-1993 Massachusetts Institute of Technology @@ -2065,6 +2065,7 @@ DEFUN (Clear_Memory, (heap_size, stack_size, constant_space_size), Free = Heap_Bottom; Constant_Top = (Constant_Space + constant_space_size); Initialize_Stack (); + STACK_RESET (); Free_Constant = Constant_Space; SET_CONSTANT_TOP (); return; diff --git a/v7/src/microcode/memmag.c b/v7/src/microcode/memmag.c index 6e64edde7..15afe0f3c 100644 --- a/v7/src/microcode/memmag.c +++ b/v7/src/microcode/memmag.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: memmag.c,v 9.54 1993/08/22 22:39:03 gjr Exp $ +$Id: memmag.c,v 9.55 1993/09/08 04:39:01 gjr Exp $ Copyright (c) 1987-1993 Massachusetts Institute of Technology @@ -103,6 +103,7 @@ DEFUN (Clear_Memory, Free = Heap_Bottom; Constant_Top = (Constant_Space + Our_Constant_Size); Initialize_Stack (); + STACK_RESET (); Free_Constant = Constant_Space; SET_CONSTANT_TOP (); return; diff --git a/v7/src/microcode/nttrap.c b/v7/src/microcode/nttrap.c index e2b9c2a1d..cd1248c62 100644 --- a/v7/src/microcode/nttrap.c +++ b/v7/src/microcode/nttrap.c @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: nttrap.c,v 1.5 1993/08/21 03:48:49 gjr Exp $ +$Id: nttrap.c,v 1.6 1993/09/08 04:41:06 gjr Exp $ Copyright (c) 1992-1993 Massachusetts Institute of Technology @@ -1214,6 +1214,68 @@ DEFUN (tinyexcpdebug, (code, info), } #endif /* W32_TRAP_DEBUG */ +#ifndef PAGE_SIZE +# define PAGE_SIZE 0x1000 +#endif + +extern void EXFUN (winnt_stack_reset, (void)); +extern void EXFUN (winnt_protect_stack, (void)); +extern void EXFUN (winnt_unprotect_stack, (void)); + +static Boolean stack_protected = FALSE; +unsigned long protected_stack_base; +unsigned long protected_stack_end; + +void +DEFUN_VOID (winnt_unprotect_stack) +{ + DWORD old_protection; + + if ((stack_protected) + && (VirtualProtect (((LPVOID) protected_stack_base), + PAGE_SIZE, + PAGE_READWRITE, + &old_protection))) + stack_protected = FALSE; + return; +} + +void +DEFUN_VOID (winnt_protect_stack) +{ + DWORD old_protection; + + if ((! stack_protected) + && (VirtualProtect (((LPVOID) protected_stack_base), + PAGE_SIZE, + (PAGE_GUARD | PAGE_READWRITE), + &old_protection))) + stack_protected = TRUE; + return; +} + +void +DEFUN_VOID (winnt_stack_reset) +{ + unsigned long boundary; + + /* This presumes that the distance between Absolute_Stack_Base and + Stack_Guard is at least a page. + */ + + boundary = (((unsigned long) Stack_Guard) + & (~ ((unsigned long) (PAGE_SIZE - 1)))); + if (stack_protected && (protected_stack_base == boundary)) + return; + winnt_unprotect_stack (); + protected_stack_base = boundary; + protected_stack_end = (boundary + PAGE_SIZE); + winnt_protect_stack (); + return; +} + +#define EXCEPTION_CODE_GUARDED_PAGE_ACCESS 0x80000001L + static int DEFUN (WinntException, (code, info), DWORD code AND LPEXCEPTION_POINTERS info) @@ -1233,6 +1295,15 @@ DEFUN (WinntException, (code, info), MB_OK); trap_immediate_termination (); } + else if ((code == EXCEPTION_CODE_GUARDED_PAGE_ACCESS) + && stack_protected + && (context->Esp >= protected_stack_base) + && (context->Esp <= protected_stack_end)) + { + stack_protected = FALSE; + REQUEST_INTERRUPT (INT_Stack_Overflow); + return (EXCEPTION_CONTINUE_EXECUTION); + } else { #ifdef W32_TRAP_DEBUG diff --git a/v7/src/microcode/stack.h b/v7/src/microcode/stack.h index 6369a1765..2db397022 100644 --- a/v7/src/microcode/stack.h +++ b/v7/src/microcode/stack.h @@ -1,6 +1,6 @@ /* -*-C-*- -$Id: stack.h,v 9.34 1993/06/24 06:22:52 gjr Exp $ +$Id: stack.h,v 9.35 1993/09/08 04:38:21 gjr Exp $ Copyright (c) 1987-1993 Massachusetts Institute of Technology @@ -35,17 +35,19 @@ MIT in each case. */ /* This file contains macros for manipulating stacks and stacklets. */ #ifdef DOS386 + extern void EXFUN (dos386_stack_reset, (void)); +# define STACK_RESET() dos386_stack_reset() +#endif /* DOS386 */ -extern void EXFUN (dos386_stack_reset, (void)); -#define STACK_RESET() dos386_stack_reset() +#ifdef WINNT + extern void EXFUN (winnt_stack_reset, (void)); +# define STACK_RESET() winnt_stack_reset() +#endif /* WINNT */ -#else +#ifndef STACK_RESET +# define STACK_RESET() do {} while (0) +#endif /* STACK_RESET */ -#define STACK_RESET() do \ -{ \ -} while (0) - -#endif #ifdef USE_STACKLETS -- 2.25.1