| 1 | n/a | /* ----------------------------------------------------------------------- |
|---|
| 2 | n/a | closures.c - Copyright (c) 2007, 2009, 2010 Red Hat, Inc. |
|---|
| 3 | n/a | Copyright (C) 2007, 2009, 2010 Free Software Foundation, Inc |
|---|
| 4 | n/a | Copyright (c) 2011 Plausible Labs Cooperative, Inc. |
|---|
| 5 | n/a | |
|---|
| 6 | n/a | Code to allocate and deallocate memory for closures. |
|---|
| 7 | n/a | |
|---|
| 8 | n/a | Permission is hereby granted, free of charge, to any person obtaining |
|---|
| 9 | n/a | a copy of this software and associated documentation files (the |
|---|
| 10 | n/a | ``Software''), to deal in the Software without restriction, including |
|---|
| 11 | n/a | without limitation the rights to use, copy, modify, merge, publish, |
|---|
| 12 | n/a | distribute, sublicense, and/or sell copies of the Software, and to |
|---|
| 13 | n/a | permit persons to whom the Software is furnished to do so, subject to |
|---|
| 14 | n/a | the following conditions: |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | The above copyright notice and this permission notice shall be included |
|---|
| 17 | n/a | in all copies or substantial portions of the Software. |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND, |
|---|
| 20 | n/a | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|---|
| 21 | n/a | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
|---|
| 22 | n/a | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
|---|
| 23 | n/a | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
|---|
| 24 | n/a | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 25 | n/a | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
|---|
| 26 | n/a | DEALINGS IN THE SOFTWARE. |
|---|
| 27 | n/a | ----------------------------------------------------------------------- */ |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | #if defined __linux__ && !defined _GNU_SOURCE |
|---|
| 30 | n/a | #define _GNU_SOURCE 1 |
|---|
| 31 | n/a | #endif |
|---|
| 32 | n/a | |
|---|
| 33 | n/a | #include <ffi.h> |
|---|
| 34 | n/a | #include <ffi_common.h> |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | #if !FFI_MMAP_EXEC_WRIT && !FFI_EXEC_TRAMPOLINE_TABLE |
|---|
| 37 | n/a | # if __gnu_linux__ && !defined(__ANDROID__) |
|---|
| 38 | n/a | /* This macro indicates it may be forbidden to map anonymous memory |
|---|
| 39 | n/a | with both write and execute permission. Code compiled when this |
|---|
| 40 | n/a | option is defined will attempt to map such pages once, but if it |
|---|
| 41 | n/a | fails, it falls back to creating a temporary file in a writable and |
|---|
| 42 | n/a | executable filesystem and mapping pages from it into separate |
|---|
| 43 | n/a | locations in the virtual memory space, one location writable and |
|---|
| 44 | n/a | another executable. */ |
|---|
| 45 | n/a | # define FFI_MMAP_EXEC_WRIT 1 |
|---|
| 46 | n/a | # define HAVE_MNTENT 1 |
|---|
| 47 | n/a | # endif |
|---|
| 48 | n/a | # if defined(X86_WIN32) || defined(X86_WIN64) || defined(__OS2__) |
|---|
| 49 | n/a | /* Windows systems may have Data Execution Protection (DEP) enabled, |
|---|
| 50 | n/a | which requires the use of VirtualMalloc/VirtualFree to alloc/free |
|---|
| 51 | n/a | executable memory. */ |
|---|
| 52 | n/a | # define FFI_MMAP_EXEC_WRIT 1 |
|---|
| 53 | n/a | # endif |
|---|
| 54 | n/a | #endif |
|---|
| 55 | n/a | |
|---|
| 56 | n/a | #if FFI_MMAP_EXEC_WRIT && !defined FFI_MMAP_EXEC_SELINUX |
|---|
| 57 | n/a | # ifdef __linux__ |
|---|
| 58 | n/a | /* When defined to 1 check for SELinux and if SELinux is active, |
|---|
| 59 | n/a | don't attempt PROT_EXEC|PROT_WRITE mapping at all, as that |
|---|
| 60 | n/a | might cause audit messages. */ |
|---|
| 61 | n/a | # define FFI_MMAP_EXEC_SELINUX 1 |
|---|
| 62 | n/a | # endif |
|---|
| 63 | n/a | #endif |
|---|
| 64 | n/a | |
|---|
| 65 | n/a | #if FFI_CLOSURES |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | # if FFI_EXEC_TRAMPOLINE_TABLE |
|---|
| 68 | n/a | |
|---|
| 69 | n/a | // Per-target implementation; It's unclear what can reasonable be shared between two OS/architecture implementations. |
|---|
| 70 | n/a | |
|---|
| 71 | n/a | # elif FFI_MMAP_EXEC_WRIT /* !FFI_EXEC_TRAMPOLINE_TABLE */ |
|---|
| 72 | n/a | |
|---|
| 73 | n/a | #define USE_LOCKS 1 |
|---|
| 74 | n/a | #define USE_DL_PREFIX 1 |
|---|
| 75 | n/a | #ifdef __GNUC__ |
|---|
| 76 | n/a | #ifndef USE_BUILTIN_FFS |
|---|
| 77 | n/a | #define USE_BUILTIN_FFS 1 |
|---|
| 78 | n/a | #endif |
|---|
| 79 | n/a | #endif |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | /* We need to use mmap, not sbrk. */ |
|---|
| 82 | n/a | #define HAVE_MORECORE 0 |
|---|
| 83 | n/a | |
|---|
| 84 | n/a | /* We could, in theory, support mremap, but it wouldn't buy us anything. */ |
|---|
| 85 | n/a | #define HAVE_MREMAP 0 |
|---|
| 86 | n/a | |
|---|
| 87 | n/a | /* We have no use for this, so save some code and data. */ |
|---|
| 88 | n/a | #define NO_MALLINFO 1 |
|---|
| 89 | n/a | |
|---|
| 90 | n/a | /* We need all allocations to be in regular segments, otherwise we |
|---|
| 91 | n/a | lose track of the corresponding code address. */ |
|---|
| 92 | n/a | #define DEFAULT_MMAP_THRESHOLD MAX_SIZE_T |
|---|
| 93 | n/a | |
|---|
| 94 | n/a | /* Don't allocate more than a page unless needed. */ |
|---|
| 95 | n/a | #define DEFAULT_GRANULARITY ((size_t)malloc_getpagesize) |
|---|
| 96 | n/a | |
|---|
| 97 | n/a | #if FFI_CLOSURE_TEST |
|---|
| 98 | n/a | /* Don't release single pages, to avoid a worst-case scenario of |
|---|
| 99 | n/a | continuously allocating and releasing single pages, but release |
|---|
| 100 | n/a | pairs of pages, which should do just as well given that allocations |
|---|
| 101 | n/a | are likely to be small. */ |
|---|
| 102 | n/a | #define DEFAULT_TRIM_THRESHOLD ((size_t)malloc_getpagesize) |
|---|
| 103 | n/a | #endif |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | #include <sys/types.h> |
|---|
| 106 | n/a | #include <sys/stat.h> |
|---|
| 107 | n/a | #include <fcntl.h> |
|---|
| 108 | n/a | #include <errno.h> |
|---|
| 109 | n/a | #ifndef _MSC_VER |
|---|
| 110 | n/a | #include <unistd.h> |
|---|
| 111 | n/a | #endif |
|---|
| 112 | n/a | #include <string.h> |
|---|
| 113 | n/a | #include <stdio.h> |
|---|
| 114 | n/a | #if !defined(X86_WIN32) && !defined(X86_WIN64) |
|---|
| 115 | n/a | #ifdef HAVE_MNTENT |
|---|
| 116 | n/a | #include <mntent.h> |
|---|
| 117 | n/a | #endif /* HAVE_MNTENT */ |
|---|
| 118 | n/a | #include <sys/param.h> |
|---|
| 119 | n/a | #include <pthread.h> |
|---|
| 120 | n/a | |
|---|
| 121 | n/a | /* We don't want sys/mman.h to be included after we redefine mmap and |
|---|
| 122 | n/a | dlmunmap. */ |
|---|
| 123 | n/a | #include <sys/mman.h> |
|---|
| 124 | n/a | #define LACKS_SYS_MMAN_H 1 |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | #if FFI_MMAP_EXEC_SELINUX |
|---|
| 127 | n/a | #include <sys/statfs.h> |
|---|
| 128 | n/a | #include <stdlib.h> |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | static int selinux_enabled = -1; |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | static int |
|---|
| 133 | n/a | selinux_enabled_check (void) |
|---|
| 134 | n/a | { |
|---|
| 135 | n/a | struct statfs sfs; |
|---|
| 136 | n/a | FILE *f; |
|---|
| 137 | n/a | char *buf = NULL; |
|---|
| 138 | n/a | size_t len = 0; |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | if (statfs ("/selinux", &sfs) >= 0 |
|---|
| 141 | n/a | && (unsigned int) sfs.f_type == 0xf97cff8cU) |
|---|
| 142 | n/a | return 1; |
|---|
| 143 | n/a | f = fopen ("/proc/mounts", "r"); |
|---|
| 144 | n/a | if (f == NULL) |
|---|
| 145 | n/a | return 0; |
|---|
| 146 | n/a | while (getline (&buf, &len, f) >= 0) |
|---|
| 147 | n/a | { |
|---|
| 148 | n/a | char *p = strchr (buf, ' '); |
|---|
| 149 | n/a | if (p == NULL) |
|---|
| 150 | n/a | break; |
|---|
| 151 | n/a | p = strchr (p + 1, ' '); |
|---|
| 152 | n/a | if (p == NULL) |
|---|
| 153 | n/a | break; |
|---|
| 154 | n/a | if (strncmp (p + 1, "selinuxfs ", 10) == 0) |
|---|
| 155 | n/a | { |
|---|
| 156 | n/a | free (buf); |
|---|
| 157 | n/a | fclose (f); |
|---|
| 158 | n/a | return 1; |
|---|
| 159 | n/a | } |
|---|
| 160 | n/a | } |
|---|
| 161 | n/a | free (buf); |
|---|
| 162 | n/a | fclose (f); |
|---|
| 163 | n/a | return 0; |
|---|
| 164 | n/a | } |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | #define is_selinux_enabled() (selinux_enabled >= 0 ? selinux_enabled \ |
|---|
| 167 | n/a | : (selinux_enabled = selinux_enabled_check ())) |
|---|
| 168 | n/a | |
|---|
| 169 | n/a | #else |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | #define is_selinux_enabled() 0 |
|---|
| 172 | n/a | |
|---|
| 173 | n/a | #endif /* !FFI_MMAP_EXEC_SELINUX */ |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | /* On PaX enable kernels that have MPROTECT enable we can't use PROT_EXEC. */ |
|---|
| 176 | n/a | #ifdef FFI_MMAP_EXEC_EMUTRAMP_PAX |
|---|
| 177 | n/a | #include <stdlib.h> |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | static int emutramp_enabled = -1; |
|---|
| 180 | n/a | |
|---|
| 181 | n/a | static int |
|---|
| 182 | n/a | emutramp_enabled_check (void) |
|---|
| 183 | n/a | { |
|---|
| 184 | n/a | char *buf = NULL; |
|---|
| 185 | n/a | size_t len = 0; |
|---|
| 186 | n/a | FILE *f; |
|---|
| 187 | n/a | int ret; |
|---|
| 188 | n/a | f = fopen ("/proc/self/status", "r"); |
|---|
| 189 | n/a | if (f == NULL) |
|---|
| 190 | n/a | return 0; |
|---|
| 191 | n/a | ret = 0; |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | while (getline (&buf, &len, f) != -1) |
|---|
| 194 | n/a | if (!strncmp (buf, "PaX:", 4)) |
|---|
| 195 | n/a | { |
|---|
| 196 | n/a | char emutramp; |
|---|
| 197 | n/a | if (sscanf (buf, "%*s %*c%c", &emutramp) == 1) |
|---|
| 198 | n/a | ret = (emutramp == 'E'); |
|---|
| 199 | n/a | break; |
|---|
| 200 | n/a | } |
|---|
| 201 | n/a | free (buf); |
|---|
| 202 | n/a | fclose (f); |
|---|
| 203 | n/a | return ret; |
|---|
| 204 | n/a | } |
|---|
| 205 | n/a | |
|---|
| 206 | n/a | #define is_emutramp_enabled() (emutramp_enabled >= 0 ? emutramp_enabled \ |
|---|
| 207 | n/a | : (emutramp_enabled = emutramp_enabled_check ())) |
|---|
| 208 | n/a | #endif /* FFI_MMAP_EXEC_EMUTRAMP_PAX */ |
|---|
| 209 | n/a | |
|---|
| 210 | n/a | #elif defined (__CYGWIN__) || defined(__INTERIX) |
|---|
| 211 | n/a | |
|---|
| 212 | n/a | #include <sys/mman.h> |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | /* Cygwin is Linux-like, but not quite that Linux-like. */ |
|---|
| 215 | n/a | #define is_selinux_enabled() 0 |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | #endif /* !defined(X86_WIN32) && !defined(X86_WIN64) */ |
|---|
| 218 | n/a | |
|---|
| 219 | n/a | #ifndef FFI_MMAP_EXEC_EMUTRAMP_PAX |
|---|
| 220 | n/a | #define is_emutramp_enabled() 0 |
|---|
| 221 | n/a | #endif /* FFI_MMAP_EXEC_EMUTRAMP_PAX */ |
|---|
| 222 | n/a | |
|---|
| 223 | n/a | /* Declare all functions defined in dlmalloc.c as static. */ |
|---|
| 224 | n/a | static void *dlmalloc(size_t); |
|---|
| 225 | n/a | static void dlfree(void*); |
|---|
| 226 | n/a | static void *dlcalloc(size_t, size_t) MAYBE_UNUSED; |
|---|
| 227 | n/a | static void *dlrealloc(void *, size_t) MAYBE_UNUSED; |
|---|
| 228 | n/a | static void *dlmemalign(size_t, size_t) MAYBE_UNUSED; |
|---|
| 229 | n/a | static void *dlvalloc(size_t) MAYBE_UNUSED; |
|---|
| 230 | n/a | static int dlmallopt(int, int) MAYBE_UNUSED; |
|---|
| 231 | n/a | static size_t dlmalloc_footprint(void) MAYBE_UNUSED; |
|---|
| 232 | n/a | static size_t dlmalloc_max_footprint(void) MAYBE_UNUSED; |
|---|
| 233 | n/a | static void** dlindependent_calloc(size_t, size_t, void**) MAYBE_UNUSED; |
|---|
| 234 | n/a | static void** dlindependent_comalloc(size_t, size_t*, void**) MAYBE_UNUSED; |
|---|
| 235 | n/a | static void *dlpvalloc(size_t) MAYBE_UNUSED; |
|---|
| 236 | n/a | static int dlmalloc_trim(size_t) MAYBE_UNUSED; |
|---|
| 237 | n/a | static size_t dlmalloc_usable_size(void*) MAYBE_UNUSED; |
|---|
| 238 | n/a | static void dlmalloc_stats(void) MAYBE_UNUSED; |
|---|
| 239 | n/a | |
|---|
| 240 | n/a | #if !(defined(X86_WIN32) || defined(X86_WIN64) || defined(__OS2__)) || defined (__CYGWIN__) || defined(__INTERIX) |
|---|
| 241 | n/a | /* Use these for mmap and munmap within dlmalloc.c. */ |
|---|
| 242 | n/a | static void *dlmmap(void *, size_t, int, int, int, off_t); |
|---|
| 243 | n/a | static int dlmunmap(void *, size_t); |
|---|
| 244 | n/a | #endif /* !(defined(X86_WIN32) || defined(X86_WIN64) || defined(__OS2__)) || defined (__CYGWIN__) || defined(__INTERIX) */ |
|---|
| 245 | n/a | |
|---|
| 246 | n/a | #define mmap dlmmap |
|---|
| 247 | n/a | #define munmap dlmunmap |
|---|
| 248 | n/a | |
|---|
| 249 | n/a | #include "dlmalloc.c" |
|---|
| 250 | n/a | |
|---|
| 251 | n/a | #undef mmap |
|---|
| 252 | n/a | #undef munmap |
|---|
| 253 | n/a | |
|---|
| 254 | n/a | #if !(defined(X86_WIN32) || defined(X86_WIN64) || defined(__OS2__)) || defined (__CYGWIN__) || defined(__INTERIX) |
|---|
| 255 | n/a | |
|---|
| 256 | n/a | /* A mutex used to synchronize access to *exec* variables in this file. */ |
|---|
| 257 | n/a | static pthread_mutex_t open_temp_exec_file_mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 258 | n/a | |
|---|
| 259 | n/a | /* A file descriptor of a temporary file from which we'll map |
|---|
| 260 | n/a | executable pages. */ |
|---|
| 261 | n/a | static int execfd = -1; |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | /* The amount of space already allocated from the temporary file. */ |
|---|
| 264 | n/a | static size_t execsize = 0; |
|---|
| 265 | n/a | |
|---|
| 266 | n/a | /* Open a temporary file name, and immediately unlink it. */ |
|---|
| 267 | n/a | static int |
|---|
| 268 | n/a | open_temp_exec_file_name (char *name) |
|---|
| 269 | n/a | { |
|---|
| 270 | n/a | int fd = mkstemp (name); |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | if (fd != -1) |
|---|
| 273 | n/a | unlink (name); |
|---|
| 274 | n/a | |
|---|
| 275 | n/a | return fd; |
|---|
| 276 | n/a | } |
|---|
| 277 | n/a | |
|---|
| 278 | n/a | /* Open a temporary file in the named directory. */ |
|---|
| 279 | n/a | static int |
|---|
| 280 | n/a | open_temp_exec_file_dir (const char *dir) |
|---|
| 281 | n/a | { |
|---|
| 282 | n/a | static const char suffix[] = "/ffiXXXXXX"; |
|---|
| 283 | n/a | size_t lendir = strlen (dir); |
|---|
| 284 | n/a | char *tempname = __builtin_alloca (lendir + sizeof (suffix)); |
|---|
| 285 | n/a | |
|---|
| 286 | n/a | if (!tempname) |
|---|
| 287 | n/a | return -1; |
|---|
| 288 | n/a | |
|---|
| 289 | n/a | memcpy (tempname, dir, lendir); |
|---|
| 290 | n/a | memcpy (tempname + lendir, suffix, sizeof (suffix)); |
|---|
| 291 | n/a | |
|---|
| 292 | n/a | return open_temp_exec_file_name (tempname); |
|---|
| 293 | n/a | } |
|---|
| 294 | n/a | |
|---|
| 295 | n/a | /* Open a temporary file in the directory in the named environment |
|---|
| 296 | n/a | variable. */ |
|---|
| 297 | n/a | static int |
|---|
| 298 | n/a | open_temp_exec_file_env (const char *envvar) |
|---|
| 299 | n/a | { |
|---|
| 300 | n/a | const char *value = getenv (envvar); |
|---|
| 301 | n/a | |
|---|
| 302 | n/a | if (!value) |
|---|
| 303 | n/a | return -1; |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | return open_temp_exec_file_dir (value); |
|---|
| 306 | n/a | } |
|---|
| 307 | n/a | |
|---|
| 308 | n/a | #ifdef HAVE_MNTENT |
|---|
| 309 | n/a | /* Open a temporary file in an executable and writable mount point |
|---|
| 310 | n/a | listed in the mounts file. Subsequent calls with the same mounts |
|---|
| 311 | n/a | keep searching for mount points in the same file. Providing NULL |
|---|
| 312 | n/a | as the mounts file closes the file. */ |
|---|
| 313 | n/a | static int |
|---|
| 314 | n/a | open_temp_exec_file_mnt (const char *mounts) |
|---|
| 315 | n/a | { |
|---|
| 316 | n/a | static const char *last_mounts; |
|---|
| 317 | n/a | static FILE *last_mntent; |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | if (mounts != last_mounts) |
|---|
| 320 | n/a | { |
|---|
| 321 | n/a | if (last_mntent) |
|---|
| 322 | n/a | endmntent (last_mntent); |
|---|
| 323 | n/a | |
|---|
| 324 | n/a | last_mounts = mounts; |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | if (mounts) |
|---|
| 327 | n/a | last_mntent = setmntent (mounts, "r"); |
|---|
| 328 | n/a | else |
|---|
| 329 | n/a | last_mntent = NULL; |
|---|
| 330 | n/a | } |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | if (!last_mntent) |
|---|
| 333 | n/a | return -1; |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | for (;;) |
|---|
| 336 | n/a | { |
|---|
| 337 | n/a | int fd; |
|---|
| 338 | n/a | struct mntent mnt; |
|---|
| 339 | n/a | char buf[MAXPATHLEN * 3]; |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | if (getmntent_r (last_mntent, &mnt, buf, sizeof (buf)) == NULL) |
|---|
| 342 | n/a | return -1; |
|---|
| 343 | n/a | |
|---|
| 344 | n/a | if (hasmntopt (&mnt, "ro") |
|---|
| 345 | n/a | || hasmntopt (&mnt, "noexec") |
|---|
| 346 | n/a | || access (mnt.mnt_dir, W_OK)) |
|---|
| 347 | n/a | continue; |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | fd = open_temp_exec_file_dir (mnt.mnt_dir); |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | if (fd != -1) |
|---|
| 352 | n/a | return fd; |
|---|
| 353 | n/a | } |
|---|
| 354 | n/a | } |
|---|
| 355 | n/a | #endif /* HAVE_MNTENT */ |
|---|
| 356 | n/a | |
|---|
| 357 | n/a | /* Instructions to look for a location to hold a temporary file that |
|---|
| 358 | n/a | can be mapped in for execution. */ |
|---|
| 359 | n/a | static struct |
|---|
| 360 | n/a | { |
|---|
| 361 | n/a | int (*func)(const char *); |
|---|
| 362 | n/a | const char *arg; |
|---|
| 363 | n/a | int repeat; |
|---|
| 364 | n/a | } open_temp_exec_file_opts[] = { |
|---|
| 365 | n/a | { open_temp_exec_file_env, "TMPDIR", 0 }, |
|---|
| 366 | n/a | { open_temp_exec_file_dir, "/tmp", 0 }, |
|---|
| 367 | n/a | { open_temp_exec_file_dir, "/var/tmp", 0 }, |
|---|
| 368 | n/a | { open_temp_exec_file_dir, "/dev/shm", 0 }, |
|---|
| 369 | n/a | { open_temp_exec_file_env, "HOME", 0 }, |
|---|
| 370 | n/a | #ifdef HAVE_MNTENT |
|---|
| 371 | n/a | { open_temp_exec_file_mnt, "/etc/mtab", 1 }, |
|---|
| 372 | n/a | { open_temp_exec_file_mnt, "/proc/mounts", 1 }, |
|---|
| 373 | n/a | #endif /* HAVE_MNTENT */ |
|---|
| 374 | n/a | }; |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | /* Current index into open_temp_exec_file_opts. */ |
|---|
| 377 | n/a | static int open_temp_exec_file_opts_idx = 0; |
|---|
| 378 | n/a | |
|---|
| 379 | n/a | /* Reset a current multi-call func, then advances to the next entry. |
|---|
| 380 | n/a | If we're at the last, go back to the first and return nonzero, |
|---|
| 381 | n/a | otherwise return zero. */ |
|---|
| 382 | n/a | static int |
|---|
| 383 | n/a | open_temp_exec_file_opts_next (void) |
|---|
| 384 | n/a | { |
|---|
| 385 | n/a | if (open_temp_exec_file_opts[open_temp_exec_file_opts_idx].repeat) |
|---|
| 386 | n/a | open_temp_exec_file_opts[open_temp_exec_file_opts_idx].func (NULL); |
|---|
| 387 | n/a | |
|---|
| 388 | n/a | open_temp_exec_file_opts_idx++; |
|---|
| 389 | n/a | if (open_temp_exec_file_opts_idx |
|---|
| 390 | n/a | == (sizeof (open_temp_exec_file_opts) |
|---|
| 391 | n/a | / sizeof (*open_temp_exec_file_opts))) |
|---|
| 392 | n/a | { |
|---|
| 393 | n/a | open_temp_exec_file_opts_idx = 0; |
|---|
| 394 | n/a | return 1; |
|---|
| 395 | n/a | } |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | return 0; |
|---|
| 398 | n/a | } |
|---|
| 399 | n/a | |
|---|
| 400 | n/a | /* Return a file descriptor of a temporary zero-sized file in a |
|---|
| 401 | n/a | writable and executable filesystem. */ |
|---|
| 402 | n/a | static int |
|---|
| 403 | n/a | open_temp_exec_file (void) |
|---|
| 404 | n/a | { |
|---|
| 405 | n/a | int fd; |
|---|
| 406 | n/a | |
|---|
| 407 | n/a | do |
|---|
| 408 | n/a | { |
|---|
| 409 | n/a | fd = open_temp_exec_file_opts[open_temp_exec_file_opts_idx].func |
|---|
| 410 | n/a | (open_temp_exec_file_opts[open_temp_exec_file_opts_idx].arg); |
|---|
| 411 | n/a | |
|---|
| 412 | n/a | if (!open_temp_exec_file_opts[open_temp_exec_file_opts_idx].repeat |
|---|
| 413 | n/a | || fd == -1) |
|---|
| 414 | n/a | { |
|---|
| 415 | n/a | if (open_temp_exec_file_opts_next ()) |
|---|
| 416 | n/a | break; |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | } |
|---|
| 419 | n/a | while (fd == -1); |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | return fd; |
|---|
| 422 | n/a | } |
|---|
| 423 | n/a | |
|---|
| 424 | n/a | /* Map in a chunk of memory from the temporary exec file into separate |
|---|
| 425 | n/a | locations in the virtual memory address space, one writable and one |
|---|
| 426 | n/a | executable. Returns the address of the writable portion, after |
|---|
| 427 | n/a | storing an offset to the corresponding executable portion at the |
|---|
| 428 | n/a | last word of the requested chunk. */ |
|---|
| 429 | n/a | static void * |
|---|
| 430 | n/a | dlmmap_locked (void *start, size_t length, int prot, int flags, off_t offset) |
|---|
| 431 | n/a | { |
|---|
| 432 | n/a | void *ptr; |
|---|
| 433 | n/a | |
|---|
| 434 | n/a | if (execfd == -1) |
|---|
| 435 | n/a | { |
|---|
| 436 | n/a | open_temp_exec_file_opts_idx = 0; |
|---|
| 437 | n/a | retry_open: |
|---|
| 438 | n/a | execfd = open_temp_exec_file (); |
|---|
| 439 | n/a | if (execfd == -1) |
|---|
| 440 | n/a | return MFAIL; |
|---|
| 441 | n/a | } |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | offset = execsize; |
|---|
| 444 | n/a | |
|---|
| 445 | n/a | if (ftruncate (execfd, offset + length)) |
|---|
| 446 | n/a | return MFAIL; |
|---|
| 447 | n/a | |
|---|
| 448 | n/a | flags &= ~(MAP_PRIVATE | MAP_ANONYMOUS); |
|---|
| 449 | n/a | flags |= MAP_SHARED; |
|---|
| 450 | n/a | |
|---|
| 451 | n/a | ptr = mmap (NULL, length, (prot & ~PROT_WRITE) | PROT_EXEC, |
|---|
| 452 | n/a | flags, execfd, offset); |
|---|
| 453 | n/a | if (ptr == MFAIL) |
|---|
| 454 | n/a | { |
|---|
| 455 | n/a | if (!offset) |
|---|
| 456 | n/a | { |
|---|
| 457 | n/a | close (execfd); |
|---|
| 458 | n/a | goto retry_open; |
|---|
| 459 | n/a | } |
|---|
| 460 | n/a | ftruncate (execfd, offset); |
|---|
| 461 | n/a | return MFAIL; |
|---|
| 462 | n/a | } |
|---|
| 463 | n/a | else if (!offset |
|---|
| 464 | n/a | && open_temp_exec_file_opts[open_temp_exec_file_opts_idx].repeat) |
|---|
| 465 | n/a | open_temp_exec_file_opts_next (); |
|---|
| 466 | n/a | |
|---|
| 467 | n/a | start = mmap (start, length, prot, flags, execfd, offset); |
|---|
| 468 | n/a | |
|---|
| 469 | n/a | if (start == MFAIL) |
|---|
| 470 | n/a | { |
|---|
| 471 | n/a | munmap (ptr, length); |
|---|
| 472 | n/a | ftruncate (execfd, offset); |
|---|
| 473 | n/a | return start; |
|---|
| 474 | n/a | } |
|---|
| 475 | n/a | |
|---|
| 476 | n/a | mmap_exec_offset ((char *)start, length) = (char*)ptr - (char*)start; |
|---|
| 477 | n/a | |
|---|
| 478 | n/a | execsize += length; |
|---|
| 479 | n/a | |
|---|
| 480 | n/a | return start; |
|---|
| 481 | n/a | } |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | /* Map in a writable and executable chunk of memory if possible. |
|---|
| 484 | n/a | Failing that, fall back to dlmmap_locked. */ |
|---|
| 485 | n/a | static void * |
|---|
| 486 | n/a | dlmmap (void *start, size_t length, int prot, |
|---|
| 487 | n/a | int flags, int fd, off_t offset) |
|---|
| 488 | n/a | { |
|---|
| 489 | n/a | void *ptr; |
|---|
| 490 | n/a | |
|---|
| 491 | n/a | assert (start == NULL && length % malloc_getpagesize == 0 |
|---|
| 492 | n/a | && prot == (PROT_READ | PROT_WRITE) |
|---|
| 493 | n/a | && flags == (MAP_PRIVATE | MAP_ANONYMOUS) |
|---|
| 494 | n/a | && fd == -1 && offset == 0); |
|---|
| 495 | n/a | |
|---|
| 496 | n/a | #if FFI_CLOSURE_TEST |
|---|
| 497 | n/a | printf ("mapping in %zi\n", length); |
|---|
| 498 | n/a | #endif |
|---|
| 499 | n/a | |
|---|
| 500 | n/a | if (execfd == -1 && is_emutramp_enabled ()) |
|---|
| 501 | n/a | { |
|---|
| 502 | n/a | ptr = mmap (start, length, prot & ~PROT_EXEC, flags, fd, offset); |
|---|
| 503 | n/a | return ptr; |
|---|
| 504 | n/a | } |
|---|
| 505 | n/a | |
|---|
| 506 | n/a | if (execfd == -1 && !is_selinux_enabled ()) |
|---|
| 507 | n/a | { |
|---|
| 508 | n/a | ptr = mmap (start, length, prot | PROT_EXEC, flags, fd, offset); |
|---|
| 509 | n/a | |
|---|
| 510 | n/a | if (ptr != MFAIL || (errno != EPERM && errno != EACCES)) |
|---|
| 511 | n/a | /* Cool, no need to mess with separate segments. */ |
|---|
| 512 | n/a | return ptr; |
|---|
| 513 | n/a | |
|---|
| 514 | n/a | /* If MREMAP_DUP is ever introduced and implemented, try mmap |
|---|
| 515 | n/a | with ((prot & ~PROT_WRITE) | PROT_EXEC) and mremap with |
|---|
| 516 | n/a | MREMAP_DUP and prot at this point. */ |
|---|
| 517 | n/a | } |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | if (execsize == 0 || execfd == -1) |
|---|
| 520 | n/a | { |
|---|
| 521 | n/a | pthread_mutex_lock (&open_temp_exec_file_mutex); |
|---|
| 522 | n/a | ptr = dlmmap_locked (start, length, prot, flags, offset); |
|---|
| 523 | n/a | pthread_mutex_unlock (&open_temp_exec_file_mutex); |
|---|
| 524 | n/a | |
|---|
| 525 | n/a | return ptr; |
|---|
| 526 | n/a | } |
|---|
| 527 | n/a | |
|---|
| 528 | n/a | return dlmmap_locked (start, length, prot, flags, offset); |
|---|
| 529 | n/a | } |
|---|
| 530 | n/a | |
|---|
| 531 | n/a | /* Release memory at the given address, as well as the corresponding |
|---|
| 532 | n/a | executable page if it's separate. */ |
|---|
| 533 | n/a | static int |
|---|
| 534 | n/a | dlmunmap (void *start, size_t length) |
|---|
| 535 | n/a | { |
|---|
| 536 | n/a | /* We don't bother decreasing execsize or truncating the file, since |
|---|
| 537 | n/a | we can't quite tell whether we're unmapping the end of the file. |
|---|
| 538 | n/a | We don't expect frequent deallocation anyway. If we did, we |
|---|
| 539 | n/a | could locate pages in the file by writing to the pages being |
|---|
| 540 | n/a | deallocated and checking that the file contents change. |
|---|
| 541 | n/a | Yuck. */ |
|---|
| 542 | n/a | msegmentptr seg = segment_holding (gm, start); |
|---|
| 543 | n/a | void *code; |
|---|
| 544 | n/a | |
|---|
| 545 | n/a | #if FFI_CLOSURE_TEST |
|---|
| 546 | n/a | printf ("unmapping %zi\n", length); |
|---|
| 547 | n/a | #endif |
|---|
| 548 | n/a | |
|---|
| 549 | n/a | if (seg && (code = add_segment_exec_offset (start, seg)) != start) |
|---|
| 550 | n/a | { |
|---|
| 551 | n/a | int ret = munmap (code, length); |
|---|
| 552 | n/a | if (ret) |
|---|
| 553 | n/a | return ret; |
|---|
| 554 | n/a | } |
|---|
| 555 | n/a | |
|---|
| 556 | n/a | return munmap (start, length); |
|---|
| 557 | n/a | } |
|---|
| 558 | n/a | |
|---|
| 559 | n/a | #if FFI_CLOSURE_FREE_CODE |
|---|
| 560 | n/a | /* Return segment holding given code address. */ |
|---|
| 561 | n/a | static msegmentptr |
|---|
| 562 | n/a | segment_holding_code (mstate m, char* addr) |
|---|
| 563 | n/a | { |
|---|
| 564 | n/a | msegmentptr sp = &m->seg; |
|---|
| 565 | n/a | for (;;) { |
|---|
| 566 | n/a | if (addr >= add_segment_exec_offset (sp->base, sp) |
|---|
| 567 | n/a | && addr < add_segment_exec_offset (sp->base, sp) + sp->size) |
|---|
| 568 | n/a | return sp; |
|---|
| 569 | n/a | if ((sp = sp->next) == 0) |
|---|
| 570 | n/a | return 0; |
|---|
| 571 | n/a | } |
|---|
| 572 | n/a | } |
|---|
| 573 | n/a | #endif |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | #endif /* !(defined(X86_WIN32) || defined(X86_WIN64) || defined(__OS2__)) || defined (__CYGWIN__) || defined(__INTERIX) */ |
|---|
| 576 | n/a | |
|---|
| 577 | n/a | /* Allocate a chunk of memory with the given size. Returns a pointer |
|---|
| 578 | n/a | to the writable address, and sets *CODE to the executable |
|---|
| 579 | n/a | corresponding virtual address. */ |
|---|
| 580 | n/a | void * |
|---|
| 581 | n/a | ffi_closure_alloc (size_t size, void **code) |
|---|
| 582 | n/a | { |
|---|
| 583 | n/a | void *ptr; |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | if (!code) |
|---|
| 586 | n/a | return NULL; |
|---|
| 587 | n/a | |
|---|
| 588 | n/a | ptr = dlmalloc (size); |
|---|
| 589 | n/a | |
|---|
| 590 | n/a | if (ptr) |
|---|
| 591 | n/a | { |
|---|
| 592 | n/a | msegmentptr seg = segment_holding (gm, ptr); |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | *code = add_segment_exec_offset (ptr, seg); |
|---|
| 595 | n/a | } |
|---|
| 596 | n/a | |
|---|
| 597 | n/a | return ptr; |
|---|
| 598 | n/a | } |
|---|
| 599 | n/a | |
|---|
| 600 | n/a | /* Release a chunk of memory allocated with ffi_closure_alloc. If |
|---|
| 601 | n/a | FFI_CLOSURE_FREE_CODE is nonzero, the given address can be the |
|---|
| 602 | n/a | writable or the executable address given. Otherwise, only the |
|---|
| 603 | n/a | writable address can be provided here. */ |
|---|
| 604 | n/a | void |
|---|
| 605 | n/a | ffi_closure_free (void *ptr) |
|---|
| 606 | n/a | { |
|---|
| 607 | n/a | #if FFI_CLOSURE_FREE_CODE |
|---|
| 608 | n/a | msegmentptr seg = segment_holding_code (gm, ptr); |
|---|
| 609 | n/a | |
|---|
| 610 | n/a | if (seg) |
|---|
| 611 | n/a | ptr = sub_segment_exec_offset (ptr, seg); |
|---|
| 612 | n/a | #endif |
|---|
| 613 | n/a | |
|---|
| 614 | n/a | dlfree (ptr); |
|---|
| 615 | n/a | } |
|---|
| 616 | n/a | |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | #if FFI_CLOSURE_TEST |
|---|
| 619 | n/a | /* Do some internal sanity testing to make sure allocation and |
|---|
| 620 | n/a | deallocation of pages are working as intended. */ |
|---|
| 621 | n/a | int main () |
|---|
| 622 | n/a | { |
|---|
| 623 | n/a | void *p[3]; |
|---|
| 624 | n/a | #define GET(idx, len) do { p[idx] = dlmalloc (len); printf ("allocated %zi for p[%i]\n", (len), (idx)); } while (0) |
|---|
| 625 | n/a | #define PUT(idx) do { printf ("freeing p[%i]\n", (idx)); dlfree (p[idx]); } while (0) |
|---|
| 626 | n/a | GET (0, malloc_getpagesize / 2); |
|---|
| 627 | n/a | GET (1, 2 * malloc_getpagesize - 64 * sizeof (void*)); |
|---|
| 628 | n/a | PUT (1); |
|---|
| 629 | n/a | GET (1, 2 * malloc_getpagesize); |
|---|
| 630 | n/a | GET (2, malloc_getpagesize / 2); |
|---|
| 631 | n/a | PUT (1); |
|---|
| 632 | n/a | PUT (0); |
|---|
| 633 | n/a | PUT (2); |
|---|
| 634 | n/a | return 0; |
|---|
| 635 | n/a | } |
|---|
| 636 | n/a | #endif /* FFI_CLOSURE_TEST */ |
|---|
| 637 | n/a | # else /* ! FFI_MMAP_EXEC_WRIT */ |
|---|
| 638 | n/a | |
|---|
| 639 | n/a | /* On many systems, memory returned by malloc is writable and |
|---|
| 640 | n/a | executable, so just use it. */ |
|---|
| 641 | n/a | |
|---|
| 642 | n/a | #include <stdlib.h> |
|---|
| 643 | n/a | |
|---|
| 644 | n/a | void * |
|---|
| 645 | n/a | ffi_closure_alloc (size_t size, void **code) |
|---|
| 646 | n/a | { |
|---|
| 647 | n/a | if (!code) |
|---|
| 648 | n/a | return NULL; |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | return *code = malloc (size); |
|---|
| 651 | n/a | } |
|---|
| 652 | n/a | |
|---|
| 653 | n/a | void |
|---|
| 654 | n/a | ffi_closure_free (void *ptr) |
|---|
| 655 | n/a | { |
|---|
| 656 | n/a | free (ptr); |
|---|
| 657 | n/a | } |
|---|
| 658 | n/a | |
|---|
| 659 | n/a | # endif /* ! FFI_MMAP_EXEC_WRIT */ |
|---|
| 660 | n/a | #endif /* FFI_CLOSURES */ |
|---|