sljitWXExecAllocator.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. This file contains a simple W^X executable memory allocator for POSIX
  28. like systems and Windows
  29. In *NIX, MAP_ANON is required (that is considered a feature) so make
  30. sure to set the right availability macros for your system or the code
  31. will fail to build.
  32. If your system doesn't support mapping of anonymous pages (ex: IRIX) it
  33. is also likely that it doesn't need this allocator and should be using
  34. the standard one instead.
  35. It allocates a separate map for each code block and may waste a lot of
  36. memory, because whatever was requested, will be rounded up to the page
  37. size (minimum 4KB, but could be even bigger).
  38. It changes the page permissions (RW <-> RX) as needed and therefore, if you
  39. will be updating the code after it has been generated, need to make sure to
  40. block any concurrent execution, or could result in a SIGBUS, that could
  41. even manifest itself at a different address than the one that was being
  42. modified.
  43. Only use if you are unable to use the regular allocator because of security
  44. restrictions and adding exceptions to your application or the system are
  45. not possible.
  46. */
  47. #define SLJIT_UPDATE_WX_FLAGS(from, to, enable_exec) \
  48. sljit_update_wx_flags((from), (to), (enable_exec))
  49. #ifndef _WIN32
  50. #include <sys/types.h>
  51. #include <sys/mman.h>
  52. #ifdef __NetBSD__
  53. #if defined(PROT_MPROTECT)
  54. #define check_se_protected(ptr, size) (0)
  55. #define SLJIT_PROT_WX PROT_MPROTECT(PROT_EXEC)
  56. #else /* !PROT_MPROTECT */
  57. #ifdef _NETBSD_SOURCE
  58. #include <sys/param.h>
  59. #else /* !_NETBSD_SOURCE */
  60. typedef unsigned int u_int;
  61. #define devmajor_t sljit_s32
  62. #endif /* _NETBSD_SOURCE */
  63. #include <sys/sysctl.h>
  64. #include <unistd.h>
  65. #define check_se_protected(ptr, size) netbsd_se_protected()
  66. static SLJIT_INLINE int netbsd_se_protected(void)
  67. {
  68. int mib[3];
  69. int paxflags;
  70. size_t len = sizeof(paxflags);
  71. mib[0] = CTL_PROC;
  72. mib[1] = getpid();
  73. mib[2] = PROC_PID_PAXFLAGS;
  74. if (SLJIT_UNLIKELY(sysctl(mib, 3, &paxflags, &len, NULL, 0) < 0))
  75. return -1;
  76. return (paxflags & CTL_PROC_PAXFLAGS_MPROTECT) ? -1 : 0;
  77. }
  78. #endif /* PROT_MPROTECT */
  79. #else /* POSIX */
  80. #define check_se_protected(ptr, size) generic_se_protected(ptr, size)
  81. static SLJIT_INLINE int generic_se_protected(void *ptr, sljit_uw size)
  82. {
  83. if (SLJIT_LIKELY(!mprotect(ptr, size, PROT_EXEC)))
  84. return mprotect(ptr, size, PROT_READ | PROT_WRITE);
  85. return -1;
  86. }
  87. #endif /* NetBSD */
  88. #if defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED
  89. #define SLJIT_SE_LOCK()
  90. #define SLJIT_SE_UNLOCK()
  91. #else /* !SLJIT_SINGLE_THREADED */
  92. #include <pthread.h>
  93. #define SLJIT_SE_LOCK() pthread_mutex_lock(&se_lock)
  94. #define SLJIT_SE_UNLOCK() pthread_mutex_unlock(&se_lock)
  95. #endif /* SLJIT_SINGLE_THREADED */
  96. #ifndef SLJIT_PROT_WX
  97. #define SLJIT_PROT_WX 0
  98. #endif /* !SLJIT_PROT_WX */
  99. SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
  100. {
  101. #if !(defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
  102. static pthread_mutex_t se_lock = PTHREAD_MUTEX_INITIALIZER;
  103. #endif
  104. static int se_protected = !SLJIT_PROT_WX;
  105. int prot = PROT_READ | PROT_WRITE | SLJIT_PROT_WX;
  106. sljit_uw* ptr;
  107. if (SLJIT_UNLIKELY(se_protected < 0))
  108. return NULL;
  109. #ifdef PROT_MAX
  110. prot |= PROT_MAX(PROT_READ | PROT_WRITE | PROT_EXEC);
  111. #endif
  112. size += sizeof(sljit_uw);
  113. ptr = (sljit_uw*)mmap(NULL, size, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
  114. if (ptr == MAP_FAILED)
  115. return NULL;
  116. if (SLJIT_UNLIKELY(se_protected > 0)) {
  117. SLJIT_SE_LOCK();
  118. se_protected = check_se_protected(ptr, size);
  119. SLJIT_SE_UNLOCK();
  120. if (SLJIT_UNLIKELY(se_protected < 0)) {
  121. munmap((void *)ptr, size);
  122. return NULL;
  123. }
  124. }
  125. *ptr++ = size;
  126. return ptr;
  127. }
  128. #undef SLJIT_PROT_WX
  129. #undef SLJIT_SE_UNLOCK
  130. #undef SLJIT_SE_LOCK
  131. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr)
  132. {
  133. sljit_uw *start_ptr = ((sljit_uw*)ptr) - 1;
  134. munmap((void*)start_ptr, *start_ptr);
  135. }
  136. static void sljit_update_wx_flags(void *from, void *to, sljit_s32 enable_exec)
  137. {
  138. sljit_uw page_mask = (sljit_uw)get_page_alignment();
  139. sljit_uw start = (sljit_uw)from;
  140. sljit_uw end = (sljit_uw)to;
  141. int prot = PROT_READ | (enable_exec ? PROT_EXEC : PROT_WRITE);
  142. SLJIT_ASSERT(start < end);
  143. start &= ~page_mask;
  144. end = (end + page_mask) & ~page_mask;
  145. mprotect((void*)start, end - start, prot);
  146. }
  147. #else /* windows */
  148. SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
  149. {
  150. sljit_uw *ptr;
  151. size += sizeof(sljit_uw);
  152. ptr = (sljit_uw*)VirtualAlloc(NULL, size,
  153. MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
  154. if (!ptr)
  155. return NULL;
  156. *ptr++ = size;
  157. return ptr;
  158. }
  159. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr)
  160. {
  161. sljit_uw start = (sljit_uw)ptr - sizeof(sljit_uw);
  162. #if defined(SLJIT_DEBUG) && SLJIT_DEBUG
  163. sljit_uw page_mask = (sljit_uw)get_page_alignment();
  164. SLJIT_ASSERT(!(start & page_mask));
  165. #endif
  166. VirtualFree((void*)start, 0, MEM_RELEASE);
  167. }
  168. static void sljit_update_wx_flags(void *from, void *to, sljit_s32 enable_exec)
  169. {
  170. DWORD oldprot;
  171. sljit_uw page_mask = (sljit_uw)get_page_alignment();
  172. sljit_uw start = (sljit_uw)from;
  173. sljit_uw end = (sljit_uw)to;
  174. DWORD prot = enable_exec ? PAGE_EXECUTE : PAGE_READWRITE;
  175. SLJIT_ASSERT(start < end);
  176. start &= ~page_mask;
  177. end = (end + page_mask) & ~page_mask;
  178. VirtualProtect((void*)start, end - start, prot, &oldprot);
  179. }
  180. #endif /* !windows */
  181. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void)
  182. {
  183. /* This allocator does not keep unused memory for future allocations. */
  184. }