From e510674aeef05e37b13b6ebb8af7a98a0bb16211 Mon Sep 17 00:00:00 2001
From: sof <unknown>
Date: Thu, 2 Oct 1997 19:40:41 +0000
Subject: [PATCH] [project @ 1997-10-02 19:40:41 by sof] Use Win32 calls for
 mprotecting under cygwin32

---
 ghc/runtime/storage/mprotect.lc | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/ghc/runtime/storage/mprotect.lc b/ghc/runtime/storage/mprotect.lc
index 316572da633a..b9b14f5fb674 100644
--- a/ghc/runtime/storage/mprotect.lc
+++ b/ghc/runtime/storage/mprotect.lc
@@ -7,7 +7,7 @@
 %*									*
 %************************************************************************
 
-Is @mprotect@ POSIX now?
+Is @mprotect@ POSIX now? [Yup, POSIX.4 -- sof]
 
 \begin{code}
 #if STACK_CHECK_BY_PAGE_FAULT
@@ -59,6 +59,11 @@ int mprotect PROTO((caddr_t, size_t, int));
 #define PROT_NONE 0
 #endif
 
+/* For VirtualProtect() and its flags */
+#if defined(cygwin32_TARGET_OS)
+#include <windows.h>
+#endif
+
 void 
 unmapMiddleStackPage(addr_, size)
 char * /*caddr_t*/ addr_;
@@ -67,6 +72,9 @@ int size;
     int pagesize = GETPAGESIZE();
     caddr_t addr = addr_;
     caddr_t middle = (caddr_t) (((W_) (addr + size / 2)) / pagesize * pagesize);
+#if defined(cygwin32_TARGET_OS)
+    unsigned int old_prot;
+#endif
 
 # ifdef STK_CHK_DEBUG
     fprintf(stderr, "pagesize: %x\nstack start: %08lx\nstack size: %08lx\nstack middle: %08lx\n",
@@ -77,7 +85,14 @@ int size;
 	fprintf(stderr, "Stack too small; stack overflow trap disabled.\n");
 	return;
     }
+/* mprotect() is broken in beta18, so we use the native Win32
+   call instead
+*/
+#if defined(cygwin32_TARGET_OS)
+    if (VirtualProtect(middle, pagesize, PAGE_NOACCESS, &old_prot) == 0) {
+#else
     if (mprotect(middle, pagesize, PROT_NONE) == -1) {
+#endif
 	perror("mprotect");
 	EXIT(EXIT_FAILURE);
     }
-- 
GitLab