sljitUtils.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. /* Locks */
  28. /* ------------------------------------------------------------------------ */
  29. /* Executable Allocator */
  30. #if (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) \
  31. && !(defined SLJIT_WX_EXECUTABLE_ALLOCATOR && SLJIT_WX_EXECUTABLE_ALLOCATOR)
  32. #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
  33. #define SLJIT_ALLOCATOR_LOCK()
  34. #define SLJIT_ALLOCATOR_UNLOCK()
  35. #elif !(defined _WIN32)
  36. #include <pthread.h>
  37. static pthread_mutex_t allocator_lock = PTHREAD_MUTEX_INITIALIZER;
  38. #define SLJIT_ALLOCATOR_LOCK() pthread_mutex_lock(&allocator_lock)
  39. #define SLJIT_ALLOCATOR_UNLOCK() pthread_mutex_unlock(&allocator_lock)
  40. #else /* windows */
  41. static HANDLE allocator_lock;
  42. static SLJIT_INLINE void allocator_grab_lock(void)
  43. {
  44. HANDLE lock;
  45. if (SLJIT_UNLIKELY(!InterlockedCompareExchangePointer(&allocator_lock, NULL, NULL))) {
  46. lock = CreateMutex(NULL, FALSE, NULL);
  47. if (InterlockedCompareExchangePointer(&allocator_lock, lock, NULL))
  48. CloseHandle(lock);
  49. }
  50. WaitForSingleObject(allocator_lock, INFINITE);
  51. }
  52. #define SLJIT_ALLOCATOR_LOCK() allocator_grab_lock()
  53. #define SLJIT_ALLOCATOR_UNLOCK() ReleaseMutex(allocator_lock)
  54. #endif /* thread implementation */
  55. #endif /* SLJIT_EXECUTABLE_ALLOCATOR && !SLJIT_WX_EXECUTABLE_ALLOCATOR */
  56. /* ------------------------------------------------------------------------ */
  57. /* Stack */
  58. /* ------------------------------------------------------------------------ */
  59. #if ((defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) \
  60. && !(defined SLJIT_UTIL_SIMPLE_STACK_ALLOCATION && SLJIT_UTIL_SIMPLE_STACK_ALLOCATION)) \
  61. || ((defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR) \
  62. && !((defined SLJIT_PROT_EXECUTABLE_ALLOCATOR && SLJIT_PROT_EXECUTABLE_ALLOCATOR) \
  63. || (defined SLJIT_WX_EXECUTABLE_ALLOCATOR && SLJIT_WX_EXECUTABLE_ALLOCATOR)))
  64. #ifndef _WIN32
  65. /* Provides mmap function. */
  66. #include <sys/types.h>
  67. #include <sys/mman.h>
  68. #ifndef MAP_ANON
  69. #ifdef MAP_ANONYMOUS
  70. #define MAP_ANON MAP_ANONYMOUS
  71. #endif /* MAP_ANONYMOUS */
  72. #endif /* !MAP_ANON */
  73. #ifndef MAP_ANON
  74. #include <fcntl.h>
  75. #ifdef O_CLOEXEC
  76. #define SLJIT_CLOEXEC O_CLOEXEC
  77. #else /* !O_CLOEXEC */
  78. #define SLJIT_CLOEXEC 0
  79. #endif /* O_CLOEXEC */
  80. /* Some old systems do not have MAP_ANON. */
  81. static int dev_zero = -1;
  82. #if (defined SLJIT_SINGLE_THREADED && SLJIT_SINGLE_THREADED)
  83. static SLJIT_INLINE int open_dev_zero(void)
  84. {
  85. dev_zero = open("/dev/zero", O_RDWR | SLJIT_CLOEXEC);
  86. return dev_zero < 0;
  87. }
  88. #else /* !SLJIT_SINGLE_THREADED */
  89. #include <pthread.h>
  90. static pthread_mutex_t dev_zero_mutex = PTHREAD_MUTEX_INITIALIZER;
  91. static SLJIT_INLINE int open_dev_zero(void)
  92. {
  93. pthread_mutex_lock(&dev_zero_mutex);
  94. if (SLJIT_UNLIKELY(dev_zero < 0))
  95. dev_zero = open("/dev/zero", O_RDWR | SLJIT_CLOEXEC);
  96. pthread_mutex_unlock(&dev_zero_mutex);
  97. return dev_zero < 0;
  98. }
  99. #endif /* SLJIT_SINGLE_THREADED */
  100. #undef SLJIT_CLOEXEC
  101. #endif /* !MAP_ANON */
  102. #endif /* !_WIN32 */
  103. #endif /* open_dev_zero */
  104. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK) \
  105. || (defined SLJIT_EXECUTABLE_ALLOCATOR && SLJIT_EXECUTABLE_ALLOCATOR)
  106. #ifdef _WIN32
  107. static SLJIT_INLINE sljit_sw get_page_alignment(void) {
  108. SYSTEM_INFO si;
  109. static sljit_sw sljit_page_align;
  110. if (!sljit_page_align) {
  111. GetSystemInfo(&si);
  112. sljit_page_align = si.dwPageSize - 1;
  113. }
  114. return sljit_page_align;
  115. }
  116. #else
  117. #include <unistd.h>
  118. static SLJIT_INLINE sljit_sw get_page_alignment(void) {
  119. static sljit_sw sljit_page_align = -1;
  120. if (sljit_page_align < 0) {
  121. #ifdef _SC_PAGESIZE
  122. sljit_page_align = sysconf(_SC_PAGESIZE);
  123. #else
  124. sljit_page_align = getpagesize();
  125. #endif
  126. /* Should never happen. */
  127. if (sljit_page_align < 0)
  128. sljit_page_align = 4096;
  129. sljit_page_align--;
  130. }
  131. return sljit_page_align;
  132. }
  133. #endif /* _WIN32 */
  134. #endif /* get_page_alignment() */
  135. #if (defined SLJIT_UTIL_STACK && SLJIT_UTIL_STACK)
  136. #if (defined SLJIT_UTIL_SIMPLE_STACK_ALLOCATION && SLJIT_UTIL_SIMPLE_STACK_ALLOCATION)
  137. SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
  138. {
  139. struct sljit_stack *stack;
  140. void *ptr;
  141. SLJIT_UNUSED_ARG(allocator_data);
  142. if (start_size > max_size || start_size < 1)
  143. return NULL;
  144. stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack), allocator_data);
  145. if (stack == NULL)
  146. return NULL;
  147. ptr = SLJIT_MALLOC(max_size, allocator_data);
  148. if (ptr == NULL) {
  149. SLJIT_FREE(stack, allocator_data);
  150. return NULL;
  151. }
  152. stack->min_start = (sljit_u8 *)ptr;
  153. stack->end = stack->min_start + max_size;
  154. stack->start = stack->end - start_size;
  155. stack->top = stack->end;
  156. return stack;
  157. }
  158. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
  159. {
  160. SLJIT_UNUSED_ARG(allocator_data);
  161. SLJIT_FREE((void*)stack->min_start, allocator_data);
  162. SLJIT_FREE(stack, allocator_data);
  163. }
  164. SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
  165. {
  166. if ((new_start < stack->min_start) || (new_start >= stack->end))
  167. return NULL;
  168. stack->start = new_start;
  169. return new_start;
  170. }
  171. #else /* !SLJIT_UTIL_SIMPLE_STACK_ALLOCATION */
  172. #ifdef _WIN32
  173. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
  174. {
  175. SLJIT_UNUSED_ARG(allocator_data);
  176. VirtualFree((void*)stack->min_start, 0, MEM_RELEASE);
  177. SLJIT_FREE(stack, allocator_data);
  178. }
  179. #else /* !_WIN32 */
  180. SLJIT_API_FUNC_ATTRIBUTE void SLJIT_FUNC sljit_free_stack(struct sljit_stack *stack, void *allocator_data)
  181. {
  182. SLJIT_UNUSED_ARG(allocator_data);
  183. munmap((void*)stack->min_start, stack->end - stack->min_start);
  184. SLJIT_FREE(stack, allocator_data);
  185. }
  186. #endif /* _WIN32 */
  187. SLJIT_API_FUNC_ATTRIBUTE struct sljit_stack* SLJIT_FUNC sljit_allocate_stack(sljit_uw start_size, sljit_uw max_size, void *allocator_data)
  188. {
  189. struct sljit_stack *stack;
  190. void *ptr;
  191. sljit_sw page_align;
  192. SLJIT_UNUSED_ARG(allocator_data);
  193. if (start_size > max_size || start_size < 1)
  194. return NULL;
  195. stack = (struct sljit_stack*)SLJIT_MALLOC(sizeof(struct sljit_stack), allocator_data);
  196. if (stack == NULL)
  197. return NULL;
  198. /* Align max_size. */
  199. page_align = get_page_alignment();
  200. max_size = (max_size + page_align) & ~page_align;
  201. #ifdef _WIN32
  202. ptr = VirtualAlloc(NULL, max_size, MEM_RESERVE, PAGE_READWRITE);
  203. if (!ptr) {
  204. SLJIT_FREE(stack, allocator_data);
  205. return NULL;
  206. }
  207. stack->min_start = (sljit_u8 *)ptr;
  208. stack->end = stack->min_start + max_size;
  209. stack->start = stack->end;
  210. if (sljit_stack_resize(stack, stack->end - start_size) == NULL) {
  211. sljit_free_stack(stack, allocator_data);
  212. return NULL;
  213. }
  214. #else /* !_WIN32 */
  215. #ifdef MAP_ANON
  216. ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, -1, 0);
  217. #else /* !MAP_ANON */
  218. if (SLJIT_UNLIKELY((dev_zero < 0) && open_dev_zero())) {
  219. SLJIT_FREE(stack, allocator_data);
  220. return NULL;
  221. }
  222. ptr = mmap(NULL, max_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, dev_zero, 0);
  223. #endif /* MAP_ANON */
  224. if (ptr == MAP_FAILED) {
  225. SLJIT_FREE(stack, allocator_data);
  226. return NULL;
  227. }
  228. stack->min_start = (sljit_u8 *)ptr;
  229. stack->end = stack->min_start + max_size;
  230. stack->start = stack->end - start_size;
  231. #endif /* _WIN32 */
  232. stack->top = stack->end;
  233. return stack;
  234. }
  235. SLJIT_API_FUNC_ATTRIBUTE sljit_u8 *SLJIT_FUNC sljit_stack_resize(struct sljit_stack *stack, sljit_u8 *new_start)
  236. {
  237. #if defined _WIN32 || defined(POSIX_MADV_DONTNEED)
  238. sljit_uw aligned_old_start;
  239. sljit_uw aligned_new_start;
  240. sljit_sw page_align;
  241. #endif
  242. if ((new_start < stack->min_start) || (new_start >= stack->end))
  243. return NULL;
  244. #ifdef _WIN32
  245. page_align = get_page_alignment();
  246. aligned_new_start = (sljit_uw)new_start & ~page_align;
  247. aligned_old_start = ((sljit_uw)stack->start) & ~page_align;
  248. if (aligned_new_start != aligned_old_start) {
  249. if (aligned_new_start < aligned_old_start) {
  250. if (!VirtualAlloc((void*)aligned_new_start, aligned_old_start - aligned_new_start, MEM_COMMIT, PAGE_READWRITE))
  251. return NULL;
  252. }
  253. else {
  254. if (!VirtualFree((void*)aligned_old_start, aligned_new_start - aligned_old_start, MEM_DECOMMIT))
  255. return NULL;
  256. }
  257. }
  258. #elif defined(POSIX_MADV_DONTNEED)
  259. if (stack->start < new_start) {
  260. page_align = get_page_alignment();
  261. aligned_new_start = (sljit_uw)new_start & ~page_align;
  262. aligned_old_start = ((sljit_uw)stack->start) & ~page_align;
  263. if (aligned_new_start > aligned_old_start) {
  264. posix_madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, POSIX_MADV_DONTNEED);
  265. #ifdef MADV_FREE
  266. madvise((void*)aligned_old_start, aligned_new_start - aligned_old_start, MADV_FREE);
  267. #endif /* MADV_FREE */
  268. }
  269. }
  270. #endif /* _WIN32 */
  271. stack->start = new_start;
  272. return new_start;
  273. }
  274. #endif /* SLJIT_UTIL_SIMPLE_STACK_ALLOCATION */
  275. #endif /* SLJIT_UTIL_STACK */