0.2.0-dev Download ZIP
102 lines ·3.7 KB Raw
| 1 | /* |
| 2 | * Stack-less Just-In-Time compiler |
| 3 | * |
| 4 | * Copyright Zoltan Herczeg (hzmester@freemail.hu). All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without modification, are |
| 7 | * permitted provided that the following conditions are met: |
| 8 | * |
| 9 | * 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | * conditions and the following disclaimer. |
| 11 | * |
| 12 | * 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | * of conditions and the following disclaimer in the documentation and/or other materials |
| 14 | * provided with the distribution. |
| 15 | * |
| 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) AND CONTRIBUTORS ``AS IS'' AND ANY |
| 17 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 18 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 19 | * SHALL THE COPYRIGHT HOLDER(S) OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 20 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 21 | * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 22 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 24 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 | */ |
| 26 | |
| 27 | /* |
| 28 | This file contains a simple W^X executable memory allocator |
| 29 | |
| 30 | In *NIX, MAP_ANON is required (that is considered a feature) so make |
| 31 | sure to set the right availability macros for your system or the code |
| 32 | will fail to build. |
| 33 | |
| 34 | If your system doesn't support mapping of anonymous pages (ex: IRIX) it |
| 35 | is also likely that it doesn't need this allocator and should be using |
| 36 | the standard one instead. |
| 37 | |
| 38 | It allocates a separate map for each code block and may waste a lot of |
| 39 | memory, because whatever was requested, will be rounded up to the page |
| 40 | size (minimum 4KB, but could be even bigger). |
| 41 | |
| 42 | It changes the page permissions (RW <-> RX) as needed and therefore, if you |
| 43 | will be updating the code after it has been generated, need to make sure to |
| 44 | block any concurrent execution, or could result in a SIGBUS, that could |
| 45 | even manifest itself at a different address than the one that was being |
| 46 | modified. |
| 47 | |
| 48 | Only use if you are unable to use the regular allocator because of security |
| 49 | restrictions and adding exceptions to your application or the system are |
| 50 | not possible. |
| 51 | */ |
| 52 | |
| 53 | #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec) \ |
| 54 | sljit_update_wx_flags((from), (to), (enable_exec)) |
| 55 | |
| 56 | SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size) |
| 57 | { |
| 58 | sljit_uw *ptr; |
| 59 | |
| 60 | size += sizeof(sljit_uw); |
| 61 | ptr = (sljit_uw*)VirtualAlloc(NULL, size, |
| 62 | MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); |
| 63 | |
| 64 | if (!ptr) |
| 65 | return NULL; |
| 66 | |
| 67 | *ptr++ = size; |
| 68 | |
| 69 | return ptr; |
| 70 | } |
| 71 | |
| 72 | SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr) |
| 73 | { |
| 74 | sljit_uw start = (sljit_uw)ptr - sizeof(sljit_uw); |
| 75 | #if defined(SLJIT_DEBUG) && SLJIT_DEBUG |
| 76 | sljit_uw page_mask = (sljit_uw)get_page_alignment(); |
| 77 | |
| 78 | SLJIT_ASSERT(!(start & page_mask)); |
| 79 | #endif |
| 80 | VirtualFree((void*)start, 0, MEM_RELEASE); |
| 81 | } |
| 82 | |
| 83 | static void sljit_update_wx_flags(void *from, void *to, sljit_s32 enable_exec) |
| 84 | { |
| 85 | DWORD oldprot; |
| 86 | sljit_uw page_mask = (sljit_uw)get_page_alignment(); |
| 87 | sljit_uw start = (sljit_uw)from; |
| 88 | sljit_uw end = (sljit_uw)to; |
| 89 | DWORD prot = enable_exec ? PAGE_EXECUTE : PAGE_READWRITE; |
| 90 | |
| 91 | SLJIT_ASSERT(start < end); |
| 92 | |
| 93 | start &= ~page_mask; |
| 94 | end = (end + page_mask) & ~page_mask; |
| 95 | |
| 96 | VirtualProtect((void*)start, end - start, prot, &oldprot); |
| 97 | } |
| 98 | |
| 99 | SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void) |
| 100 | { |
| 101 | /* This allocator does not keep unused memory for future allocations. */ |
| 102 | } |