sljitExecAllocator.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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 executable memory allocator
  28. It is assumed, that executable code blocks are usually medium (or sometimes
  29. large) memory blocks, and the allocator is not too frequently called (less
  30. optimized than other allocators). Thus, using it as a generic allocator is
  31. not suggested.
  32. How does it work:
  33. Memory is allocated in continuous memory areas called chunks by alloc_chunk()
  34. Chunk format:
  35. [ block ][ block ] ... [ block ][ block terminator ]
  36. All blocks and the block terminator is started with block_header. The block
  37. header contains the size of the previous and the next block. These sizes
  38. can also contain special values.
  39. Block size:
  40. 0 - The block is a free_block, with a different size member.
  41. 1 - The block is a block terminator.
  42. n - The block is used at the moment, and the value contains its size.
  43. Previous block size:
  44. 0 - This is the first block of the memory chunk.
  45. n - The size of the previous block.
  46. Using these size values we can go forward or backward on the block chain.
  47. The unused blocks are stored in a chain list pointed by free_blocks. This
  48. list is useful if we need to find a suitable memory area when the allocator
  49. is called.
  50. When a block is freed, the new free block is connected to its adjacent free
  51. blocks if possible.
  52. [ free block ][ used block ][ free block ]
  53. and "used block" is freed, the three blocks are connected together:
  54. [ one big free block ]
  55. */
  56. /* --------------------------------------------------------------------- */
  57. /* System (OS) functions */
  58. /* --------------------------------------------------------------------- */
  59. /* 64 KByte. */
  60. #define CHUNK_SIZE 0x10000
  61. /*
  62. alloc_chunk / free_chunk :
  63. * allocate executable system memory chunks
  64. * the size is always divisible by CHUNK_SIZE
  65. allocator_grab_lock / allocator_release_lock :
  66. * make the allocator thread safe
  67. * can be empty if the OS (or the application) does not support threading
  68. * only the allocator requires this lock, sljit is fully thread safe
  69. as it only uses local variables
  70. */
  71. #ifdef _WIN32
  72. static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
  73. {
  74. return VirtualAlloc(NULL, size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
  75. }
  76. static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
  77. {
  78. SLJIT_UNUSED_ARG(size);
  79. VirtualFree(chunk, 0, MEM_RELEASE);
  80. }
  81. #else
  82. #ifdef __APPLE__
  83. /* Configures TARGET_OS_OSX when appropriate */
  84. #include <TargetConditionals.h>
  85. #if TARGET_OS_OSX && defined(MAP_JIT)
  86. #include <sys/utsname.h>
  87. #endif /* TARGET_OS_OSX && MAP_JIT */
  88. #ifdef MAP_JIT
  89. static SLJIT_INLINE int get_map_jit_flag()
  90. {
  91. #if TARGET_OS_OSX
  92. /* On macOS systems, returns MAP_JIT if it is defined _and_ we're running on a version
  93. of macOS where it's OK to have more than one JIT block. On non-macOS systems, returns
  94. MAP_JIT if it is defined. */
  95. static int map_jit_flag = -1;
  96. /* The following code is thread safe because multiple initialization
  97. sets map_jit_flag to the same value and the code has no side-effects.
  98. Changing the kernel version witout system restart is (very) unlikely. */
  99. if (map_jit_flag == -1) {
  100. struct utsname name;
  101. map_jit_flag = 0;
  102. uname(&name);
  103. /* Kernel version for 10.14.0 (Mojave) */
  104. if (atoi(name.release) >= 18) {
  105. /* Only use MAP_JIT if a hardened runtime is used, because MAP_JIT is incompatible with fork(). */
  106. void *ptr = mmap(NULL, getpagesize(), PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  107. if (ptr == MAP_FAILED) {
  108. map_jit_flag = MAP_JIT;
  109. } else {
  110. munmap(ptr, getpagesize());
  111. }
  112. }
  113. }
  114. return map_jit_flag;
  115. #else /* !TARGET_OS_OSX */
  116. return MAP_JIT;
  117. #endif /* TARGET_OS_OSX */
  118. }
  119. #endif /* MAP_JIT */
  120. #endif /* __APPLE__ */
  121. static SLJIT_INLINE void* alloc_chunk(sljit_uw size)
  122. {
  123. void *retval;
  124. const int prot = PROT_READ | PROT_WRITE | PROT_EXEC;
  125. #ifdef MAP_ANON
  126. int flags = MAP_PRIVATE | MAP_ANON;
  127. #ifdef MAP_JIT
  128. flags |= get_map_jit_flag();
  129. #endif
  130. retval = mmap(NULL, size, prot, flags, -1, 0);
  131. #else /* !MAP_ANON */
  132. if (dev_zero < 0) {
  133. if (open_dev_zero())
  134. return NULL;
  135. }
  136. retval = mmap(NULL, size, prot, MAP_PRIVATE, dev_zero, 0);
  137. #endif /* MAP_ANON */
  138. if (retval == MAP_FAILED)
  139. retval = NULL;
  140. else {
  141. if (mprotect(retval, size, prot) < 0) {
  142. munmap(retval, size);
  143. retval = NULL;
  144. }
  145. }
  146. return retval;
  147. }
  148. static SLJIT_INLINE void free_chunk(void *chunk, sljit_uw size)
  149. {
  150. munmap(chunk, size);
  151. }
  152. #endif
  153. /* --------------------------------------------------------------------- */
  154. /* Common functions */
  155. /* --------------------------------------------------------------------- */
  156. #define CHUNK_MASK (~(CHUNK_SIZE - 1))
  157. struct block_header {
  158. sljit_uw size;
  159. sljit_uw prev_size;
  160. };
  161. struct free_block {
  162. struct block_header header;
  163. struct free_block *next;
  164. struct free_block *prev;
  165. sljit_uw size;
  166. };
  167. #define AS_BLOCK_HEADER(base, offset) \
  168. ((struct block_header*)(((sljit_u8*)base) + offset))
  169. #define AS_FREE_BLOCK(base, offset) \
  170. ((struct free_block*)(((sljit_u8*)base) + offset))
  171. #define MEM_START(base) ((void*)(((sljit_u8*)base) + sizeof(struct block_header)))
  172. #define ALIGN_SIZE(size) (((size) + sizeof(struct block_header) + 7) & ~7)
  173. static struct free_block* free_blocks;
  174. static sljit_uw allocated_size;
  175. static sljit_uw total_size;
  176. static SLJIT_INLINE void sljit_insert_free_block(struct free_block *free_block, sljit_uw size)
  177. {
  178. free_block->header.size = 0;
  179. free_block->size = size;
  180. free_block->next = free_blocks;
  181. free_block->prev = NULL;
  182. if (free_blocks)
  183. free_blocks->prev = free_block;
  184. free_blocks = free_block;
  185. }
  186. static SLJIT_INLINE void sljit_remove_free_block(struct free_block *free_block)
  187. {
  188. if (free_block->next)
  189. free_block->next->prev = free_block->prev;
  190. if (free_block->prev)
  191. free_block->prev->next = free_block->next;
  192. else {
  193. SLJIT_ASSERT(free_blocks == free_block);
  194. free_blocks = free_block->next;
  195. }
  196. }
  197. SLJIT_API_FUNC_ATTRIBUTE void* sljit_malloc_exec(sljit_uw size)
  198. {
  199. struct block_header *header;
  200. struct block_header *next_header;
  201. struct free_block *free_block;
  202. sljit_uw chunk_size;
  203. allocator_grab_lock();
  204. if (size < (64 - sizeof(struct block_header)))
  205. size = (64 - sizeof(struct block_header));
  206. size = ALIGN_SIZE(size);
  207. free_block = free_blocks;
  208. while (free_block) {
  209. if (free_block->size >= size) {
  210. chunk_size = free_block->size;
  211. if (chunk_size > size + 64) {
  212. /* We just cut a block from the end of the free block. */
  213. chunk_size -= size;
  214. free_block->size = chunk_size;
  215. header = AS_BLOCK_HEADER(free_block, chunk_size);
  216. header->prev_size = chunk_size;
  217. AS_BLOCK_HEADER(header, size)->prev_size = size;
  218. }
  219. else {
  220. sljit_remove_free_block(free_block);
  221. header = (struct block_header*)free_block;
  222. size = chunk_size;
  223. }
  224. allocated_size += size;
  225. header->size = size;
  226. allocator_release_lock();
  227. return MEM_START(header);
  228. }
  229. free_block = free_block->next;
  230. }
  231. chunk_size = (size + sizeof(struct block_header) + CHUNK_SIZE - 1) & CHUNK_MASK;
  232. header = (struct block_header*)alloc_chunk(chunk_size);
  233. if (!header) {
  234. allocator_release_lock();
  235. return NULL;
  236. }
  237. chunk_size -= sizeof(struct block_header);
  238. total_size += chunk_size;
  239. header->prev_size = 0;
  240. if (chunk_size > size + 64) {
  241. /* Cut the allocated space into a free and a used block. */
  242. allocated_size += size;
  243. header->size = size;
  244. chunk_size -= size;
  245. free_block = AS_FREE_BLOCK(header, size);
  246. free_block->header.prev_size = size;
  247. sljit_insert_free_block(free_block, chunk_size);
  248. next_header = AS_BLOCK_HEADER(free_block, chunk_size);
  249. }
  250. else {
  251. /* All space belongs to this allocation. */
  252. allocated_size += chunk_size;
  253. header->size = chunk_size;
  254. next_header = AS_BLOCK_HEADER(header, chunk_size);
  255. }
  256. next_header->size = 1;
  257. next_header->prev_size = chunk_size;
  258. allocator_release_lock();
  259. return MEM_START(header);
  260. }
  261. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_exec(void* ptr)
  262. {
  263. struct block_header *header;
  264. struct free_block* free_block;
  265. allocator_grab_lock();
  266. header = AS_BLOCK_HEADER(ptr, -(sljit_sw)sizeof(struct block_header));
  267. allocated_size -= header->size;
  268. /* Connecting free blocks together if possible. */
  269. /* If header->prev_size == 0, free_block will equal to header.
  270. In this case, free_block->header.size will be > 0. */
  271. free_block = AS_FREE_BLOCK(header, -(sljit_sw)header->prev_size);
  272. if (SLJIT_UNLIKELY(!free_block->header.size)) {
  273. free_block->size += header->size;
  274. header = AS_BLOCK_HEADER(free_block, free_block->size);
  275. header->prev_size = free_block->size;
  276. }
  277. else {
  278. free_block = (struct free_block*)header;
  279. sljit_insert_free_block(free_block, header->size);
  280. }
  281. header = AS_BLOCK_HEADER(free_block, free_block->size);
  282. if (SLJIT_UNLIKELY(!header->size)) {
  283. free_block->size += ((struct free_block*)header)->size;
  284. sljit_remove_free_block((struct free_block*)header);
  285. header = AS_BLOCK_HEADER(free_block, free_block->size);
  286. header->prev_size = free_block->size;
  287. }
  288. /* The whole chunk is free. */
  289. if (SLJIT_UNLIKELY(!free_block->header.prev_size && header->size == 1)) {
  290. /* If this block is freed, we still have (allocated_size / 2) free space. */
  291. if (total_size - free_block->size > (allocated_size * 3 / 2)) {
  292. total_size -= free_block->size;
  293. sljit_remove_free_block(free_block);
  294. free_chunk(free_block, free_block->size + sizeof(struct block_header));
  295. }
  296. }
  297. allocator_release_lock();
  298. }
  299. SLJIT_API_FUNC_ATTRIBUTE void sljit_free_unused_memory_exec(void)
  300. {
  301. struct free_block* free_block;
  302. struct free_block* next_free_block;
  303. allocator_grab_lock();
  304. free_block = free_blocks;
  305. while (free_block) {
  306. next_free_block = free_block->next;
  307. if (!free_block->header.prev_size &&
  308. AS_BLOCK_HEADER(free_block, free_block->size)->size == 1) {
  309. total_size -= free_block->size;
  310. sljit_remove_free_block(free_block);
  311. free_chunk(free_block, free_block->size + sizeof(struct block_header));
  312. }
  313. free_block = next_free_block;
  314. }
  315. SLJIT_ASSERT((total_size && free_blocks) || (!total_size && !free_blocks));
  316. allocator_release_lock();
  317. }