|
| 1 | +/* |
| 2 | + * This program is free software; you can redistribute it and/or modify |
| 3 | + * it under the terms of the GNU General Public License as published by |
| 4 | + * the Free Software Foundation; either version 2 of the License, or |
| 5 | + * (at your option) any later version. |
| 6 | + * |
| 7 | + * This program is distributed in the hope that it will be useful, |
| 8 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | + * GNU General Public License for more details. |
| 11 | + * |
| 12 | + * You should have received a copy of the GNU General Public License |
| 13 | + * along with this program; if not, write to the Free Software |
| 14 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Purpose: dlmalloc's memory spaces support |
| 19 | + * and circumvent W^X restrictions. |
| 20 | + * |
| 21 | + * Author: @stsp |
| 22 | + * |
| 23 | + */ |
| 24 | +#include <stddef.h> |
| 25 | +#include <assert.h> |
| 26 | +#include <sys/mman.h> |
| 27 | +#include "emu86.h" |
| 28 | +#include "misc/dlmalloc.h" |
| 29 | + |
| 30 | +#ifndef MAP_JIT |
| 31 | +#define MAP_JIT 0 |
| 32 | +#endif |
| 33 | +#define CODEBUF_SZ (128 * 1024 * 1024) |
| 34 | +static mspace cspace; |
| 35 | +static unsigned char *wbase; |
| 36 | +static unsigned char *xbase; |
| 37 | + |
| 38 | +void InitGenCodeBuf(void) |
| 39 | +{ |
| 40 | + void *addr; |
| 41 | + |
| 42 | +#if HAVE_DECL_MREMAP_MAYMOVE && 0 |
| 43 | + int err; |
| 44 | + |
| 45 | + addr = mmap(NULL, CODEBUF_SZ, PROT_NONE, |
| 46 | + MAP_SHARED | MAP_ANONYMOUS, -1, 0); |
| 47 | + assert(addr != MAP_FAILED); |
| 48 | + xbase = mremap(addr, 0, CODEBUF_SZ, MREMAP_MAYMOVE); |
| 49 | + assert(xbase != MAP_FAILED && xbase != addr); |
| 50 | + err = mprotect(addr, CODEBUF_SZ, PROT_READ | PROT_WRITE); |
| 51 | + assert(!err); |
| 52 | + err = mprotect(xbase, CODEBUF_SZ, PROT_EXEC); |
| 53 | + assert(!err); |
| 54 | +#else |
| 55 | + addr = mmap(NULL, CODEBUF_SZ, PROT_READ | PROT_WRITE | PROT_EXEC, |
| 56 | + MAP_PRIVATE | MAP_ANONYMOUS | MAP_JIT, -1, 0); |
| 57 | + assert(addr != MAP_FAILED); |
| 58 | + xbase = addr; |
| 59 | +#endif |
| 60 | + wbase = addr; |
| 61 | + cspace = create_mspace_with_base(addr, CODEBUF_SZ, 0); |
| 62 | + assert(cspace); |
| 63 | +} |
| 64 | + |
| 65 | +void EndGenCodeBuf(void) |
| 66 | +{ |
| 67 | + destroy_mspace(cspace); |
| 68 | + if (xbase != wbase) |
| 69 | + munmap(xbase, CODEBUF_SZ); |
| 70 | + munmap(wbase, CODEBUF_SZ); |
| 71 | +} |
| 72 | + |
| 73 | +struct cbptr AllocGenCodeBuf(size_t size) |
| 74 | +{ |
| 75 | + struct cbptr ret; |
| 76 | + |
| 77 | + ret.ptr = mspace_malloc(cspace, size); |
| 78 | + assert(ret.ptr); |
| 79 | + ret.xptr = xbase + (ret.ptr - wbase); |
| 80 | + return ret; |
| 81 | +} |
| 82 | + |
| 83 | +void ShrinkGenCodeBuf(struct cbptr ptr, size_t size) |
| 84 | +{ |
| 85 | + void *ptr2 = mspace_realloc(cspace, ptr.ptr, size); |
| 86 | + assert(ptr2 == ptr.ptr); |
| 87 | +} |
| 88 | + |
| 89 | +void FreeGenCodeBuf(void *ptr) |
| 90 | +{ |
| 91 | + if (!ptr) |
| 92 | + return; |
| 93 | + mspace_free(cspace, ptr); |
| 94 | +} |
| 95 | + |
| 96 | +unsigned char *GetGenCodeBuf(const unsigned char *eip) |
| 97 | +{ |
| 98 | + assert(eip >= xbase && eip < xbase + CODEBUF_SZ); |
| 99 | + return wbase + (eip - xbase); |
| 100 | +} |
| 101 | + |
| 102 | +unsigned char *GetExecCodeBuf(const unsigned char *ptr) |
| 103 | +{ |
| 104 | + assert(ptr >= wbase && ptr < wbase + CODEBUF_SZ); |
| 105 | + return xbase + (ptr - wbase); |
| 106 | +} |
0 commit comments