Add ability to detect stack overflow in NT/Windows.
authorGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Wed, 8 Sep 1993 04:41:06 +0000 (04:41 +0000)
committerGuillermo J. Rozas <edu/mit/csail/zurich/gjr>
Wed, 8 Sep 1993 04:41:06 +0000 (04:41 +0000)
v7/src/microcode/bchmmg.c
v7/src/microcode/memmag.c
v7/src/microcode/nttrap.c
v7/src/microcode/stack.h

index 5d49dd8aa6501dd05b44bc0d9cae2f7ded6c6278..390e13655635332b005a9f56ff7b8a6ad4ce1e6b 100644 (file)
@@ -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;
index 6e64edde789341ff6e781e08292049e0011bdf8a..15afe0f3cc72471ff50307e2e8c309636ab7d66a 100644 (file)
@@ -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;
index e2b9c2a1da0af572e0e2098ba7be42fa82087e56..cd1248c621843939846dbea773665feb46121744 100644 (file)
@@ -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 */
 \f
+#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;
+}
+\f
+#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
index 6369a176571fb006fbb0a564d66d5fbee79122eb..2db3970225b369ed0b45bb1b09a437b155ec7bb5 100644 (file)
@@ -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. */
 \f
 #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