diff options
| author | David Jones | 2007-03-08 21:50:28 +0000 |
|---|---|---|
| committer | David Jones | 2007-03-08 21:50:28 +0000 |
| commit | 76e9f4ae0697135e94df6744d5fde99cb637fc66 (patch) | |
| tree | 4e4bb527f8fe94cd902cde8c89f5f2090e238647 /mps/code/protix.c | |
| parent | b44f32fa08581d6365bb318b43274cdea6ffa6bb (diff) | |
| download | emacs-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/protix.c')
| -rw-r--r-- | mps/code/protix.c | 147 |
1 files changed, 147 insertions, 0 deletions
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 | |||
| 36 | SRCID(protix, "$Id$"); | ||
| 37 | |||
| 38 | /* ProtSet -- set protection | ||
| 39 | * | ||
| 40 | * This is just a thin veneer on top of mprotect(2). | ||
| 41 | */ | ||
| 42 | |||
| 43 | void 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 | |||
| 82 | void 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 | |||
| 97 | void 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 | */ | ||