aboutsummaryrefslogtreecommitdiffstats
path: root/mps/code
diff options
context:
space:
mode:
authorDavid Jones2007-03-08 21:50:28 +0000
committerDavid Jones2007-03-08 21:50:28 +0000
commit76e9f4ae0697135e94df6744d5fde99cb637fc66 (patch)
tree4e4bb527f8fe94cd902cde8c89f5f2090e238647 /mps/code
parentb44f32fa08581d6365bb318b43274cdea6ffa6bb (diff)
downloademacs-76e9f4ae0697135e94df6744d5fde99cb637fc66.tar.gz
emacs-76e9f4ae0697135e94df6744d5fde99cb637fc66.zip
Mps: protection for intel darwin. hopefully cross-platform.
Copied from Perforce Change: 161902 ServerID: perforce.ravenbrook.com
Diffstat (limited to 'mps/code')
-rw-r--r--mps/code/config.h20
-rw-r--r--mps/code/protix.c147
-rw-r--r--mps/code/protsgix.c183
-rw-r--r--mps/code/xci3gc.gmk2
4 files changed, 351 insertions, 1 deletions
diff --git a/mps/code/config.h b/mps/code/config.h
index 69ea603605e..2a518c48620 100644
--- a/mps/code/config.h
+++ b/mps/code/config.h
@@ -226,6 +226,26 @@
226#define VMANPageALIGNMENT ((Align)4096) 226#define VMANPageALIGNMENT ((Align)4096)
227#define VMJunkBYTE ((unsigned char)0xA9) 227#define VMJunkBYTE ((unsigned char)0xA9)
228 228
229/* Protection Configuration see <code/prot*.c>
230
231 For each architecture/OS that uses protix.c or protsgix.c, we need to
232 define what signal number to use, and what si_code value to check.
233*/
234
235#if defined(MPS_OS_O1) || defined(MPS_OS_SO)
236#define PROT_SIGNAL (SIGSEGV)
237#elif defined(MPS_OS_FR) || defined(MPS_OS_XC)
238#define PROT_SIGNAL (SIGBUS)
239#endif
240
241#if defined(MPS_OS_XC)
242#define PROT_SIGINFO_GOOD(info) (1)
243#elif defined(MPS_OS_O1)
244#define PROT_SIGINFO_GOOD(info) ((info)->si_code == SEGV_ACCERR)
245#elif define(MPS_OS_FR)
246#define PROT_SIGINFO_GOOD(info) ((info)->si_code == BUS_PAGE_FAULT)
247#endif
248
229 249
230/* Tracer Configuration -- see <code/trace.c> */ 250/* Tracer Configuration -- see <code/trace.c> */
231 251
diff --git a/mps/code/protix.c b/mps/code/protix.c
new file mode 100644
index 00000000000..48c432a0a28
--- /dev/null
+++ b/mps/code/protix.c
@@ -0,0 +1,147 @@
1/* protix.c: PROTECTION FOR UNIX
2 *
3 * $Id$
4 * Copyright (c) 2001,2007 Ravenbrook Limited. See end of file for license.
5 *
6 * Somewhat generic across different Unix systems. Shared between
7 * Darwin (OS X), OSF/1 (DIGITAL UNIX), and FreeBSD.
8 *
9 * May not actually work on OSF/1 due to lack of available machines.
10 *
11 * This file does not contain a signal handler. That's in protsgix.c
12 * (for FreeBSD and Darwin on Intel); in protxcpp.c (for Darwin on
13 * PowerPC).
14 */
15
16
17/* open sesame magic, see standards(5) */
18#define _POSIX_C_SOURCE 199309L
19#define _XOPEN_SOURCE_EXTENDED 1
20
21#include "mpm.h"
22
23#if !defined(MPS_OS_O1) && !defined(MPS_OS_FR) && !defined(MPS_OS_XC)
24#error "protix.c is Unix-specific, currently for MPS_OS_O1 FR or XC"
25#endif
26#ifndef PROTECTION
27#error "protix.c implements protection, but PROTECTION is not set"
28#endif
29
30#include <limits.h>
31#include <stddef.h>
32
33#include <sys/mman.h>
34#include <sys/types.h>
35
36SRCID(protix, "$Id$");
37
38/* ProtSet -- set protection
39 *
40 * This is just a thin veneer on top of mprotect(2).
41 */
42
43void ProtSet(Addr base, Addr limit, AccessSet mode)
44{
45 int flags;
46
47 AVER(sizeof(size_t) == sizeof(Addr));
48 AVER(base < limit);
49 AVER(base != 0);
50 AVER(AddrOffset(base, limit) <= INT_MAX); /* should be redundant */
51
52 /* Convert between MPS AccessSet and UNIX PROT thingies.
53 In this function, AccessREAD means protect against read accesses
54 (disallow them). PROT_READ means allow read accesses.
55 */
56 switch(mode) {
57 case AccessWRITE | AccessREAD:
58 case AccessREAD: /* forbids writes as well */
59 flags = PROT_NONE;
60 break;
61 case AccessWRITE:
62 flags = PROT_READ | PROT_EXEC;
63 break;
64 case AccessSetEMPTY:
65 flags = PROT_READ | PROT_WRITE | PROT_EXEC;
66 break;
67 default:
68 NOTREACHED;
69 flags = PROT_NONE;
70 }
71
72 if(mprotect((caddr_t)base, (size_t)AddrOffset(base, limit), flags) != 0)
73 NOTREACHED;
74}
75
76
77/* ProtSync -- synchronize protection settings with hardware
78 *
79 * This does nothing under Solaris.
80 */
81
82void ProtSync(Arena arena)
83{
84 UNUSED(arena);
85 NOOP;
86}
87
88
89/* ProtTramp -- protection trampoline
90 *
91 * The protection trampoline is trivial under Unix, as there is
92 * nothing that needs to be done in the dynamic context of the mutator in
93 * order to catch faults. (Contrast this with Win32 Structured Exception
94 * Handling.)
95 */
96
97void ProtTramp(void **resultReturn, void *(*f)(void *, size_t),
98 void *p, size_t s)
99{
100 AVER(resultReturn != NULL);
101 AVER(FUNCHECK(f));
102 /* Can't check p and s as they are interpreted by the client */
103
104 *resultReturn = (*f)(p, s);
105}
106
107
108/* C. COPYRIGHT AND LICENSE
109 *
110 * Copyright (C) 2001-2007 Ravenbrook Limited <http://www.ravenbrook.com/>.
111 * All rights reserved. This is an open source license. Contact
112 * Ravenbrook for commercial licensing options.
113 *
114 * Redistribution and use in source and binary forms, with or without
115 * modification, are permitted provided that the following conditions are
116 * met:
117 *
118 * 1. Redistributions of source code must retain the above copyright
119 * notice, this list of conditions and the following disclaimer.
120 *
121 * 2. Redistributions in binary form must reproduce the above copyright
122 * notice, this list of conditions and the following disclaimer in the
123 * documentation and/or other materials provided with the distribution.
124 *
125 * 3. Redistributions in any form must be accompanied by information on how
126 * to obtain complete source code for this software and any accompanying
127 * software that uses this software. The source code must either be
128 * included in the distribution or be available for no more than the cost
129 * of distribution plus a nominal fee, and must be freely redistributable
130 * under reasonable conditions. For an executable file, complete source
131 * code means the source code for all modules it contains. It does not
132 * include source code for modules or files that typically accompany the
133 * major components of the operating system on which the executable file
134 * runs.
135 *
136 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
137 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
138 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
139 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
140 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
141 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
142 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
143 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
144 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
145 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
146 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
147 */
diff --git a/mps/code/protsgix.c b/mps/code/protsgix.c
new file mode 100644
index 00000000000..94b0dae6aa1
--- /dev/null
+++ b/mps/code/protsgix.c
@@ -0,0 +1,183 @@
1/* protsgix.c: PROTECTION (SIGNAL HANDLER) FOR UNIX
2 *
3 * $Id$
4 * Copyright (c) 2001-2007 Ravenbrook Limited. See end of file for license.
5 *
6 * Would ordinarily be part of protix.c (as the code is common to more
7 * than one Unix-like operating system), but PowerPC Darwin requires a
8 * different implementation of this module.
9 *
10 * SOURCES
11 *
12 * .source.man: sigaction(2): FreeBSD System Calls Manual.
13 *
14 * .source.merge: A blend from primarily the FreeBSD version (protfri3.c)
15 * and the OSF/1 (DIGITAL UNIX) version (proto1.c); influenced by other
16 * Unix versions.
17 */
18
19#include "mpm.h"
20
21#if !defined(MPS_OS_XC) && !defined(MPS_OS_FR) && !defined(MPS_OS_O1)
22#error "protsgix.c is Unix-specific, currently for MPS_OS_O1 FR or XC"
23#endif
24#if defined(MPS_OS_XC) && defined(MPS_ARCH_PP)
25#error "protsgix.c does not work on Darwin on PowerPC. Use protxcpp.c"
26#endif
27#ifndef PROTECTION
28#error "protsgix.c implements protection, but PROTECTION is not set"
29#endif
30
31#include <signal.h> /* for many functions */
32#include <sys/types.h> /* for getpid */
33#include <unistd.h> /* for getpid */
34
35SRCID(protsgix, "$Id$");
36
37
38/* The previously-installed signal action, as returned by */
39/* sigaction(3). See ProtSetup. */
40
41static struct sigaction sigNext;
42
43/* sigHandle -- protection signal handler
44 *
45 * This is the signal handler installed by ProtSetup to deal with
46 * protection faults. It is installed on the PROT_SIGNAL (a macro
47 * defined according to the platform in config.h) signal. It
48 * decodes the protection fault details from the signal context and
49 * passes them to ArenaAccess, which attempts to handle the fault and
50 * remove its cause. If the fault is handled, then the handler
51 * returns and execution resumes. If it isn't handled, then
52 * sigHandle does its best to pass the signal on to the previously
53 * installed signal handler (sigNext); which it does by signalling
54 * itself using kill(2).
55 *
56 * .sigh.args: The sigaction manual page .source.man documents three
57 * different handler prototypes: ANSI C sa_handler, traditional BSD
58 * sa_handler, and POSIX SA_SIGINFO sa_sigaction. The ANSI C
59 * prototype isn't powerful enough for us (can't get addresses), and
60 * the manual page deprecates the BSD sa_handler in favour of the
61 * POSIX SA_SIGINFO sa_sigaction. In that prototype, the arguments
62 * are: signal number, pointer to signal info structure, pointer to
63 * signal context structure.
64 *
65 * .sigh.context: We use the PROT_SIGINFO_GOOD macro to (usually) check
66 * the info->si_code. The macro is platform dependent and defined in
67 * config.h. We assume that info->si_addr is the fault address. This
68 * assumption turns out to fail for PowerPC Darwin (we use protxcpp.c
69 * there).
70 *
71 * .sigh.mode: The fault type (read/write) does not appear to be
72 * available to the signal handler (see mail archive).
73 */
74
75static void sigHandle(int sig, siginfo_t *info, void *context) /* .sigh.args */
76{
77 int e;
78 /* sigset renamed to asigset due to clash with global on Darwin. */
79 sigset_t asigset, oldset;
80 struct sigaction sa;
81
82 AVER(sig == PROT_SIGNAL);
83
84 /* .sigh.context */
85 if(PROT_SIGINFO_GOOD(info)) {
86 AccessSet mode;
87 Addr base, limit;
88
89 mode = AccessREAD | AccessWRITE; /* .sigh.mode */
90
91 /* We assume that the access is for one word at the address. */
92 base = (Addr)info->si_addr; /* .sigh.context */
93 limit = AddrAdd(base, (Size)sizeof(Addr));
94
95 /* Offer each protection structure the opportunity to handle the */
96 /* exception. If it succeeds, then allow the mutator to continue. */
97 if(ArenaAccess(base, mode, NULL))
98 return;
99 }
100
101 /* The exception was not handled by any known protection structure, */
102 /* so throw it to the previously installed handler. */
103
104 e = sigaction(PROT_SIGNAL, &sigNext, &sa);
105 AVER(e == 0);
106 sigemptyset(&asigset);
107 sigaddset(&asigset, PROT_SIGNAL);
108 e = sigprocmask(SIG_UNBLOCK, &asigset, &oldset);
109 AVER(e == 0);
110 kill(getpid(), PROT_SIGNAL);
111 e = sigprocmask(SIG_SETMASK, &oldset, NULL);
112 AVER(e == 0);
113 e = sigaction(PROT_SIGNAL, &sa, NULL);
114 AVER(e == 0);
115}
116
117
118/* ProtSetup -- global protection setup
119 *
120 * Under Unix, the global setup involves installing a signal
121 * handler on PROT_SIGNAL to catch and handle page faults (see
122 * sigHandle). The previous handler is recorded so that it can be
123 * reached from sigHandle if it fails to handle the fault.
124 *
125 * NOTE: There are problems with this approach:
126 * 1. we can't honor the sa_flags for the previous handler,
127 * 2. what if this thread is suspended just after calling signal(3)?
128 * The sigNext variable will never be initialized! */
129
130void ProtSetup(void)
131{
132 struct sigaction sa;
133 int result;
134
135 sa.sa_sigaction = sigHandle;
136 sigemptyset(&sa.sa_mask);
137 sa.sa_flags = SA_SIGINFO;
138
139 result = sigaction(PROT_SIGNAL, &sa, &sigNext);
140 AVER(result == 0);
141}
142
143
144/* C. COPYRIGHT AND LICENSE
145 *
146 * Copyright (C) 2001-2007 Ravenbrook Limited <http://www.ravenbrook.com/>.
147 * All rights reserved. This is an open source license. Contact
148 * Ravenbrook for commercial licensing options.
149 *
150 * Redistribution and use in source and binary forms, with or without
151 * modification, are permitted provided that the following conditions are
152 * met:
153 *
154 * 1. Redistributions of source code must retain the above copyright
155 * notice, this list of conditions and the following disclaimer.
156 *
157 * 2. Redistributions in binary form must reproduce the above copyright
158 * notice, this list of conditions and the following disclaimer in the
159 * documentation and/or other materials provided with the distribution.
160 *
161 * 3. Redistributions in any form must be accompanied by information on how
162 * to obtain complete source code for this software and any accompanying
163 * software that uses this software. The source code must either be
164 * included in the distribution or be available for no more than the cost
165 * of distribution plus a nominal fee, and must be freely redistributable
166 * under reasonable conditions. For an executable file, complete source
167 * code means the source code for all modules it contains. It does not
168 * include source code for modules or files that typically accompany the
169 * major components of the operating system on which the executable file
170 * runs.
171 *
172 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
173 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
174 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
175 * PURPOSE, OR NON-INFRINGEMENT, ARE DISCLAIMED. IN NO EVENT SHALL THE
176 * COPYRIGHT HOLDERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
177 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
178 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
179 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
180 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
181 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
182 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
183 */
diff --git a/mps/code/xci3gc.gmk b/mps/code/xci3gc.gmk
index 97a80e039df..35fd73b7223 100644
--- a/mps/code/xci3gc.gmk
+++ b/mps/code/xci3gc.gmk
@@ -8,7 +8,7 @@
8PFM = xci3gc 8PFM = xci3gc
9 9
10MPMPF = lockan.c than.c vmxc.c \ 10MPMPF = lockan.c than.c vmxc.c \
11 protan.c prmcan.c span.c ssixi3.c 11 protix.c protsgix.c prmcan.c span.c ssixi3.c
12SWPF = than.c vmxc.c protsw.c prmcan.c ssan.c 12SWPF = than.c vmxc.c protsw.c prmcan.c ssan.c
13 13
14LIBS = 14LIBS =