| 1 |
/* |
| 2 |
* mmap.c - memory mapping |
| 3 |
* |
| 4 |
* Copyright (c) 2022-2024 Shiro Kawai <[email protected]> |
| 5 |
* |
| 6 |
* Redistribution and use in source and binary forms, with or without |
| 7 |
* modification, are permitted provided that the following conditions |
| 8 |
* are met: |
| 9 |
* |
| 10 |
* 1. Redistributions of source code must retain the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer. |
| 12 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
/* |
| 35 |
* Abstraction of memory mapping |
| 36 |
* Scheme API and class definition of <memory-region> is in libsys.scm |
| 37 |
*/ |
| 38 |
|
| 39 |
#define LIBGAUCHE_BODY |
| 40 |
#include "gauche.h" |
| 41 |
#include "gauche/priv/configP.h" |
| 42 |
#include "gauche/priv/atomicP.h" |
| 43 |
#include "gauche/priv/mmapP.h" |
| 44 |
|
| 45 |
#ifdef HAVE_SYS_MMAN_H |
| 46 |
#include <sys/mman.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
static void mem_finalize(ScmObj obj, void *data SCM_UNUSED) |
| 50 |
{ |
| 51 |
ScmMemoryRegion *m = SCM_MEMORY_REGION(obj); |
| 52 |
if (m->ptr != NULL) { |
| 53 |
#if !defined(GAUCHE_WINDOWS) |
| 54 |
int r; |
| 55 |
SCM_SYSCALL(r, munmap(m->ptr, m->size)); |
| 56 |
if (r < 0) Scm_Warn("munmap failed"); |
| 57 |
m->ptr = NULL; |
| 58 |
#else /*GAUCHE_WINDOWS*/ |
| 59 |
if (!UnmapViewOfFile(m->ptr)) { |
| 60 |
Scm_SysError("UnmapViewOfFile failed"); |
| 61 |
} |
| 62 |
m->ptr = NULL; |
| 63 |
if (!CloseHandle(m->fileMapping)) { |
| 64 |
Scm_SysError("CloseHandle failed"); |
| 65 |
} |
| 66 |
m->fileMapping = INVALID_HANDLE_VALUE; |
| 67 |
#endif /*GAUCHE_WINDOWS*/ |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
static ScmObj make_memory_region(void *ptr, size_t size, int prot, int flags |
| 72 |
#if defined(GAUCHE_WINDOWS) |
| 73 |
, HANDLE fileMapping |
| 74 |
#endif |
| 75 |
) |
| 76 |
{ |
| 77 |
ScmMemoryRegion *m = SCM_NEW(ScmMemoryRegion); |
| 78 |
SCM_SET_CLASS(m, SCM_CLASS_MEMORY_REGION); |
| 79 |
m->ptr = ptr; |
| 80 |
m->size = size; |
| 81 |
m->prot = prot; |
| 82 |
m->flags = flags; |
| 83 |
#if defined(GAUCHE_WINDOWS) |
| 84 |
m->fileMapping = fileMapping; |
| 85 |
#endif |
| 86 |
Scm_RegisterFinalizer(SCM_OBJ(m), mem_finalize, NULL); |
| 87 |
return SCM_OBJ(m); |
| 88 |
} |
| 89 |
|
| 90 |
static void *mmap_int(void *addrhint, size_t len, int prot, |
| 91 |
int flags SCM_UNUSED, /* MinGW version ignores this */ |
| 92 |
int fd, size_t off |
| 93 |
#if defined(GAUCHE_WINDOWS) |
| 94 |
, HANDLE *pmapping |
| 95 |
#endif |
| 96 |
) |
| 97 |
{ |
| 98 |
#if !defined(GAUCHE_WINDOWS) |
| 99 |
void *ptr; |
| 100 |
SCM_SYSCALL3(ptr, mmap(addrhint, len, prot, flags, fd, off), |
| 101 |
ptr == MAP_FAILED); |
| 102 |
if (ptr == MAP_FAILED) Scm_SysError("mmap failed"); |
| 103 |
return ptr; |
| 104 |
#else /*GAUCHE_WINDOWS*/ |
| 105 |
HANDLE fhandle = INVALID_HANDLE_VALUE; |
| 106 |
if (fd >= 0) { |
| 107 |
fhandle = (HANDLE)_get_osfhandle(fd); |
| 108 |
if (fhandle == INVALID_HANDLE_VALUE) { |
| 109 |
Scm_Error("fd is not associated to a file: %d", fd); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
DWORD wprot = 0; |
| 114 |
if (prot & PROT_EXEC) { |
| 115 |
if (prot & PROT_WRITE) wprot = PAGE_EXECUTE_READWRITE; |
| 116 |
else wprot = PAGE_EXECUTE_READ; |
| 117 |
} else { |
| 118 |
if (prot & PROT_WRITE) wprot = PAGE_READWRITE; |
| 119 |
else wprot = PAGE_READONLY; |
| 120 |
} |
| 121 |
if (wprot == 0) { |
| 122 |
Scm_Error("PROT_NONE mmap protection isn't allowed on MinGW"); |
| 123 |
} |
| 124 |
|
| 125 |
#if SIZEOF_SIZE_T > 4 |
| 126 |
DWORD size_hi = len >> 32; |
| 127 |
DWORD size_lo = len & 0xffffffffULL; |
| 128 |
#else |
| 129 |
DWORD size_hi = 0; |
| 130 |
DWORD size_lo = len; |
| 131 |
#endif |
| 132 |
HANDLE mapping = CreateFileMappingA(fhandle, NULL, wprot, |
| 133 |
size_hi, size_lo, NULL); |
| 134 |
if (mapping == NULL) { |
| 135 |
Scm_SysError("CreateFileMapping failed"); |
| 136 |
} |
| 137 |
|
| 138 |
#if SIZEOF_SIZE_T > 4 |
| 139 |
DWORD off_hi = off >> 32; |
| 140 |
DWORD off_lo = off & 0xffffffffULL; |
| 141 |
#else |
| 142 |
DWORD off_hi = 0; |
| 143 |
DWORD off_lo = off; |
| 144 |
#endif |
| 145 |
DWORD accmode = 0; |
| 146 |
if (prot & PROT_WRITE) accmode = FILE_MAP_WRITE; |
| 147 |
else accmode = FILE_MAP_READ; |
| 148 |
if (prot & PROT_EXEC) accmode |= FILE_MAP_EXECUTE; |
| 149 |
LPVOID ptr = MapViewOfFileEx(mapping, accmode, off_hi, off_lo, |
| 150 |
0, addrhint); |
| 151 |
if (ptr == NULL) { |
| 152 |
DWORD errcode = GetLastError(); |
| 153 |
(void)CloseHandle(mapping); |
| 154 |
SetLastError(errcode); |
| 155 |
Scm_SysError("MapViewOfFileEx failed"); |
| 156 |
} |
| 157 |
|
| 158 |
*pmapping = mapping; |
| 159 |
return ptr; |
| 160 |
#endif /*GAUCHE_WINDOWS*/ |
| 161 |
} |
| 162 |
|
| 163 |
/* Check if PaX mprotect is active. */ |
| 164 |
#if !defined(GAUCHE_WINDOWS) |
| 165 |
static int pax_active_p() |
| 166 |
{ |
| 167 |
static ScmAtomicVar result_cache = -1; /* -1: unknown */ |
| 168 |
|
| 169 |
/* No MT hazard - operation is idempotent*/ |
| 170 |
if (result_cache < 0) { |
| 171 |
size_t pagesize = sysconf(_SC_PAGESIZE); |
| 172 |
SCM_ASSERT(pagesize > 0); |
| 173 |
void *ptr = mmap(NULL, pagesize, PROT_WRITE, |
| 174 |
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); |
| 175 |
SCM_ASSERT(ptr != NULL); |
| 176 |
int r = mprotect(ptr, pagesize, PROT_WRITE|PROT_EXEC); |
| 177 |
(void)munmap(ptr, pagesize); |
| 178 |
Scm_AtomicStoreFull(&result_cache, (r < 0)); |
| 179 |
} |
| 180 |
return result_cache; |
| 181 |
} |
| 182 |
#endif /*!GAUCHE_WINDOWS*/ |
| 183 |
|
| 184 |
/* |
| 185 |
* API |
| 186 |
*/ |
| 187 |
|
| 188 |
/* Straight mmap() interface. Scheme's sys-mmap calls this. */ |
| 189 |
ScmObj Scm_SysMmap(void *addrhint, int fd, size_t len, off_t off, |
| 190 |
int prot, int flags) |
| 191 |
{ |
| 192 |
#if !defined(GAUCHE_WINDOWS) |
| 193 |
void *ptr = mmap_int(addrhint, len, prot, flags, fd, off); |
| 194 |
return make_memory_region(ptr, len, prot, flags); |
| 195 |
#else /*GAUCHE_WINDOWS*/ |
| 196 |
HANDLE mapping = INVALID_HANDLE_VALUE; |
| 197 |
void *ptr = mmap_int(addrhint, len, prot, flags, fd, off, &mapping); |
| 198 |
return make_memory_region(ptr, len, prot, flags, mapping); |
| 199 |
#endif /*GAUCHE_WINDOWS*/ |
| 200 |
} |
| 201 |
|
| 202 |
/* Mmap for runtime code generation. Returns two ScmMemoryRegions |
| 203 |
of the same size, one for write and one for execute. If the system |
| 204 |
allows a page being both writable and executable, two regions may |
| 205 |
be the same. */ |
| 206 |
void Scm_SysMmapWX(size_t len, |
| 207 |
ScmMemoryRegion **writable, /* ret */ |
| 208 |
ScmMemoryRegion **executable) /* ret */ |
| 209 |
{ |
| 210 |
#if !defined(GAUCHE_WINDOWS) |
| 211 |
if (!pax_active_p()) goto fallback; |
| 212 |
/* TODO */ |
| 213 |
Scm_Error("System has Pax MPROTECT activated. We haven't supported it yet."); |
| 214 |
#endif /*GAUCHE_WINDOWS*/ |
| 215 |
fallback: |
| 216 |
{ |
| 217 |
ScmMemoryRegion *m = |
| 218 |
SCM_MEMORY_REGION(Scm_SysMmap(NULL, -1, len, 0, |
| 219 |
PROT_READ|PROT_WRITE|PROT_EXEC, |
| 220 |
MAP_PRIVATE|MAP_ANONYMOUS)); |
| 221 |
*writable = m; |
| 222 |
*executable = m; |
| 223 |
} |
| 224 |
} |