2 % (c) The AQUA Project, Glasgow University, 1995
4 %************************************************************************
6 \section[mprotect.lc]{Memory Protection}
8 %************************************************************************
10 Is @mprotect@ POSIX now? [Yup, POSIX.4 -- sof]
13 #if STACK_CHECK_BY_PAGE_FAULT
15 /* #define STK_CHK_DEBUG */
19 # ifdef HAVE_SYS_TYPES_H
20 # include <sys/types.h>
23 # ifdef HAVE_SYS_MMAN_H
24 # include <sys/mman.h>
27 # if defined(_SC_PAGE_SIZE) && !defined(_SC_PAGESIZE)
28 /* Death to HP-UX. What are standards for, anyway??? */
29 # define _SC_PAGESIZE _SC_PAGE_SIZE
32 # if defined(_SC_PAGESIZE)
33 # define GETPAGESIZE() sysconf(_SC_PAGESIZE)
35 # if defined(HAVE_GETPAGESIZE)
36 # define GETPAGESIZE() getpagesize()
38 # if defined(linux_TARGET_OS) || defined(linuxaout_TARGET_OS)
39 # /* it has it, but it is in BSD land; easier to just say so */
40 # define GETPAGESIZE() getpagesize()
47 #if defined(sunos4_TARGET_OS)
48 int getpagesize PROTO((void));
49 int mprotect PROTO((caddr_t, size_t, int));
52 #if defined(aix_TARGET_OS)
53 /* PROT_NONE doesn't work on aix, PROT_READ works and should suit the job */
54 #define PROT_NONE PROT_READ
57 /* Needed for FreeBSD (SDM, 96/03) */
62 /* For VirtualProtect() and its flags */
63 #if defined(cygwin32_TARGET_OS)
68 unmapMiddleStackPage(addr, size)
69 char * /*caddr_t*/ addr;
72 int pagesize = GETPAGESIZE();
73 char * middle = (caddr_t) (((W_) (addr + size / 2)) / pagesize * pagesize);
74 #if defined(cygwin32_TARGET_OS)
75 unsigned int old_prot;
79 fprintf(stderr, "pagesize: %x\nstack start: %08lx\nstack size: %08lx\nstack middle: %08lx\n",
80 pagesize, addr, size, middle);
83 if (middle < addr || middle + pagesize > addr + size) {
84 fprintf(stderr, "Stack too small; stack overflow trap disabled.\n");
87 /* mprotect() is broken in beta18, so we use the native Win32
90 #if defined(cygwin32_TARGET_OS)
91 if (VirtualProtect(middle, pagesize, PAGE_NOACCESS, &old_prot) == 0) {
93 if (mprotect(middle, pagesize, PROT_NONE) == -1) {
98 if (install_segv_handler()) {
99 fprintf(stderr, "Can't install SIGSEGV handler for stack overflow check.\n");
104 #endif /* STACK_CHECK_BY_PAGE_FAULT */