lj_mcode.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. ** Machine code management.
  3. ** Copyright (C) 2005-2014 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_mcode_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #if LJ_HASJIT
  9. #include "lj_gc.h"
  10. #include "lj_err.h"
  11. #include "lj_jit.h"
  12. #include "lj_mcode.h"
  13. #include "lj_trace.h"
  14. #include "lj_dispatch.h"
  15. #endif
  16. #if LJ_HASJIT || LJ_HASFFI
  17. #include "lj_vm.h"
  18. #endif
  19. /* -- OS-specific functions ----------------------------------------------- */
  20. #if LJ_HASJIT || LJ_HASFFI
  21. /* Define this if you want to run LuaJIT with Valgrind. */
  22. #ifdef LUAJIT_USE_VALGRIND
  23. #include <valgrind/valgrind.h>
  24. #endif
  25. #if LJ_TARGET_IOS
  26. void sys_icache_invalidate(void *start, size_t len);
  27. #endif
  28. /* Synchronize data/instruction cache. */
  29. void lj_mcode_sync(void *start, void *end)
  30. {
  31. #ifdef LUAJIT_USE_VALGRIND
  32. VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start);
  33. #endif
  34. #if LJ_TARGET_X86ORX64
  35. UNUSED(start); UNUSED(end);
  36. #elif LJ_TARGET_IOS
  37. sys_icache_invalidate(start, (char *)end-(char *)start);
  38. #elif LJ_TARGET_PPC
  39. lj_vm_cachesync(start, end);
  40. #elif defined(__GNUC__)
  41. __clear_cache(start, end);
  42. #else
  43. #error "Missing builtin to flush instruction cache"
  44. #endif
  45. }
  46. #endif
  47. #if LJ_HASJIT
  48. #if LJ_TARGET_WINDOWS
  49. #define WIN32_LEAN_AND_MEAN
  50. #include <windows.h>
  51. #define MCPROT_RW PAGE_READWRITE
  52. #define MCPROT_RX PAGE_EXECUTE_READ
  53. #define MCPROT_RWX PAGE_EXECUTE_READWRITE
  54. static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, DWORD prot)
  55. {
  56. void *p = VirtualAlloc((void *)hint, sz,
  57. MEM_RESERVE|MEM_COMMIT|MEM_TOP_DOWN, prot);
  58. if (!p && !hint)
  59. lj_trace_err(J, LJ_TRERR_MCODEAL);
  60. return p;
  61. }
  62. static void mcode_free(jit_State *J, void *p, size_t sz)
  63. {
  64. UNUSED(J); UNUSED(sz);
  65. VirtualFree(p, 0, MEM_RELEASE);
  66. }
  67. static int mcode_setprot(void *p, size_t sz, DWORD prot)
  68. {
  69. DWORD oprot;
  70. return !VirtualProtect(p, sz, prot, &oprot);
  71. }
  72. #elif LJ_TARGET_POSIX
  73. #include <sys/mman.h>
  74. #ifndef MAP_ANONYMOUS
  75. #define MAP_ANONYMOUS MAP_ANON
  76. #endif
  77. #define MCPROT_RW (PROT_READ|PROT_WRITE)
  78. #define MCPROT_RX (PROT_READ|PROT_EXEC)
  79. #define MCPROT_RWX (PROT_READ|PROT_WRITE|PROT_EXEC)
  80. static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
  81. {
  82. void *p = mmap((void *)hint, sz, prot, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
  83. if (p == MAP_FAILED) {
  84. if (!hint) lj_trace_err(J, LJ_TRERR_MCODEAL);
  85. p = NULL;
  86. }
  87. return p;
  88. }
  89. static void mcode_free(jit_State *J, void *p, size_t sz)
  90. {
  91. UNUSED(J);
  92. munmap(p, sz);
  93. }
  94. static int mcode_setprot(void *p, size_t sz, int prot)
  95. {
  96. return mprotect(p, sz, prot);
  97. }
  98. #elif LJ_64
  99. #error "Missing OS support for explicit placement of executable memory"
  100. #else
  101. /* Fallback allocator. This will fail if memory is not executable by default. */
  102. #define LUAJIT_UNPROTECT_MCODE
  103. #define MCPROT_RW 0
  104. #define MCPROT_RX 0
  105. #define MCPROT_RWX 0
  106. static void *mcode_alloc_at(jit_State *J, uintptr_t hint, size_t sz, int prot)
  107. {
  108. UNUSED(hint); UNUSED(prot);
  109. return lj_mem_new(J->L, sz);
  110. }
  111. static void mcode_free(jit_State *J, void *p, size_t sz)
  112. {
  113. lj_mem_free(J2G(J), p, sz);
  114. }
  115. #endif
  116. /* -- MCode area protection ----------------------------------------------- */
  117. /* Define this ONLY if the page protection twiddling becomes a bottleneck. */
  118. #ifdef LUAJIT_UNPROTECT_MCODE
  119. /* It's generally considered to be a potential security risk to have
  120. ** pages with simultaneous write *and* execute access in a process.
  121. **
  122. ** Do not even think about using this mode for server processes or
  123. ** apps handling untrusted external data (such as a browser).
  124. **
  125. ** The security risk is not in LuaJIT itself -- but if an adversary finds
  126. ** any *other* flaw in your C application logic, then any RWX memory page
  127. ** simplifies writing an exploit considerably.
  128. */
  129. #define MCPROT_GEN MCPROT_RWX
  130. #define MCPROT_RUN MCPROT_RWX
  131. static void mcode_protect(jit_State *J, int prot)
  132. {
  133. UNUSED(J); UNUSED(prot);
  134. }
  135. #else
  136. /* This is the default behaviour and much safer:
  137. **
  138. ** Most of the time the memory pages holding machine code are executable,
  139. ** but NONE of them is writable.
  140. **
  141. ** The current memory area is marked read-write (but NOT executable) only
  142. ** during the short time window while the assembler generates machine code.
  143. */
  144. #define MCPROT_GEN MCPROT_RW
  145. #define MCPROT_RUN MCPROT_RX
  146. /* Protection twiddling failed. Probably due to kernel security. */
  147. static LJ_NOINLINE void mcode_protfail(jit_State *J)
  148. {
  149. lua_CFunction panic = J2G(J)->panic;
  150. if (panic) {
  151. lua_State *L = J->L;
  152. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_JITPROT));
  153. panic(L);
  154. }
  155. }
  156. /* Change protection of MCode area. */
  157. static void mcode_protect(jit_State *J, int prot)
  158. {
  159. if (J->mcprot != prot) {
  160. if (LJ_UNLIKELY(mcode_setprot(J->mcarea, J->szmcarea, prot)))
  161. mcode_protfail(J);
  162. J->mcprot = prot;
  163. }
  164. }
  165. #endif
  166. /* -- MCode area allocation ----------------------------------------------- */
  167. #if LJ_TARGET_X64
  168. #define mcode_validptr(p) ((p) && (uintptr_t)(p) < (uintptr_t)1<<47)
  169. #else
  170. #define mcode_validptr(p) ((p) && (uintptr_t)(p) < 0xffff0000)
  171. #endif
  172. #ifdef LJ_TARGET_JUMPRANGE
  173. /* Get memory within relative jump distance of our code in 64 bit mode. */
  174. static void *mcode_alloc(jit_State *J, size_t sz)
  175. {
  176. /* Target an address in the static assembler code (64K aligned).
  177. ** Try addresses within a distance of target-range/2+1MB..target+range/2-1MB.
  178. ** Use half the jump range so every address in the range can reach any other.
  179. */
  180. #if LJ_TARGET_MIPS
  181. /* Use the middle of the 256MB-aligned region. */
  182. uintptr_t target = ((uintptr_t)(void *)lj_vm_exit_handler & 0xf0000000u) +
  183. 0x08000000u;
  184. #else
  185. uintptr_t target = (uintptr_t)(void *)lj_vm_exit_handler & ~(uintptr_t)0xffff;
  186. #endif
  187. const uintptr_t range = (1u << (LJ_TARGET_JUMPRANGE-1)) - (1u << 21);
  188. /* First try a contiguous area below the last one. */
  189. uintptr_t hint = J->mcarea ? (uintptr_t)J->mcarea - sz : 0;
  190. int i;
  191. for (i = 0; i < 32; i++) { /* 32 attempts ought to be enough ... */
  192. if (mcode_validptr(hint)) {
  193. void *p = mcode_alloc_at(J, hint, sz, MCPROT_GEN);
  194. if (mcode_validptr(p) &&
  195. ((uintptr_t)p + sz - target < range || target - (uintptr_t)p < range))
  196. return p;
  197. if (p) mcode_free(J, p, sz); /* Free badly placed area. */
  198. }
  199. /* Next try probing pseudo-random addresses. */
  200. do {
  201. hint = (0x78fb ^ LJ_PRNG_BITS(J, 15)) << 16; /* 64K aligned. */
  202. } while (!(hint + sz < range));
  203. hint = target + hint - (range>>1);
  204. }
  205. lj_trace_err(J, LJ_TRERR_MCODEAL); /* Give up. OS probably ignores hints? */
  206. return NULL;
  207. }
  208. #else
  209. /* All memory addresses are reachable by relative jumps. */
  210. #define mcode_alloc(J, sz) mcode_alloc_at((J), 0, (sz), MCPROT_GEN)
  211. #endif
  212. /* -- MCode area management ----------------------------------------------- */
  213. /* Linked list of MCode areas. */
  214. typedef struct MCLink {
  215. MCode *next; /* Next area. */
  216. size_t size; /* Size of current area. */
  217. } MCLink;
  218. /* Allocate a new MCode area. */
  219. static void mcode_allocarea(jit_State *J)
  220. {
  221. MCode *oldarea = J->mcarea;
  222. size_t sz = (size_t)J->param[JIT_P_sizemcode] << 10;
  223. sz = (sz + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1);
  224. J->mcarea = (MCode *)mcode_alloc(J, sz);
  225. J->szmcarea = sz;
  226. J->mcprot = MCPROT_GEN;
  227. J->mctop = (MCode *)((char *)J->mcarea + J->szmcarea);
  228. J->mcbot = (MCode *)((char *)J->mcarea + sizeof(MCLink));
  229. ((MCLink *)J->mcarea)->next = oldarea;
  230. ((MCLink *)J->mcarea)->size = sz;
  231. J->szallmcarea += sz;
  232. }
  233. /* Free all MCode areas. */
  234. void lj_mcode_free(jit_State *J)
  235. {
  236. MCode *mc = J->mcarea;
  237. J->mcarea = NULL;
  238. J->szallmcarea = 0;
  239. while (mc) {
  240. MCode *next = ((MCLink *)mc)->next;
  241. mcode_free(J, mc, ((MCLink *)mc)->size);
  242. mc = next;
  243. }
  244. }
  245. /* -- MCode transactions -------------------------------------------------- */
  246. /* Reserve the remainder of the current MCode area. */
  247. MCode *lj_mcode_reserve(jit_State *J, MCode **lim)
  248. {
  249. if (!J->mcarea)
  250. mcode_allocarea(J);
  251. else
  252. mcode_protect(J, MCPROT_GEN);
  253. *lim = J->mcbot;
  254. return J->mctop;
  255. }
  256. /* Commit the top part of the current MCode area. */
  257. void lj_mcode_commit(jit_State *J, MCode *top)
  258. {
  259. J->mctop = top;
  260. mcode_protect(J, MCPROT_RUN);
  261. }
  262. /* Abort the reservation. */
  263. void lj_mcode_abort(jit_State *J)
  264. {
  265. if (J->mcarea)
  266. mcode_protect(J, MCPROT_RUN);
  267. }
  268. /* Set/reset protection to allow patching of MCode areas. */
  269. MCode *lj_mcode_patch(jit_State *J, MCode *ptr, int finish)
  270. {
  271. #ifdef LUAJIT_UNPROTECT_MCODE
  272. UNUSED(J); UNUSED(ptr); UNUSED(finish);
  273. return NULL;
  274. #else
  275. if (finish) {
  276. if (J->mcarea == ptr)
  277. mcode_protect(J, MCPROT_RUN);
  278. else if (LJ_UNLIKELY(mcode_setprot(ptr, ((MCLink *)ptr)->size, MCPROT_RUN)))
  279. mcode_protfail(J);
  280. return NULL;
  281. } else {
  282. MCode *mc = J->mcarea;
  283. /* Try current area first to use the protection cache. */
  284. if (ptr >= mc && ptr < (MCode *)((char *)mc + J->szmcarea)) {
  285. mcode_protect(J, MCPROT_GEN);
  286. return mc;
  287. }
  288. /* Otherwise search through the list of MCode areas. */
  289. for (;;) {
  290. mc = ((MCLink *)mc)->next;
  291. lua_assert(mc != NULL);
  292. if (ptr >= mc && ptr < (MCode *)((char *)mc + ((MCLink *)mc)->size)) {
  293. if (LJ_UNLIKELY(mcode_setprot(mc, ((MCLink *)mc)->size, MCPROT_GEN)))
  294. mcode_protfail(J);
  295. return mc;
  296. }
  297. }
  298. }
  299. #endif
  300. }
  301. /* Limit of MCode reservation reached. */
  302. void lj_mcode_limiterr(jit_State *J, size_t need)
  303. {
  304. size_t sizemcode, maxmcode;
  305. lj_mcode_abort(J);
  306. sizemcode = (size_t)J->param[JIT_P_sizemcode] << 10;
  307. sizemcode = (sizemcode + LJ_PAGESIZE-1) & ~(size_t)(LJ_PAGESIZE - 1);
  308. maxmcode = (size_t)J->param[JIT_P_maxmcode] << 10;
  309. if ((size_t)need > sizemcode)
  310. lj_trace_err(J, LJ_TRERR_MCODEOV); /* Too long for any area. */
  311. if (J->szallmcarea + sizemcode > maxmcode)
  312. lj_trace_err(J, LJ_TRERR_MCODEAL);
  313. mcode_allocarea(J);
  314. lj_trace_err(J, LJ_TRERR_MCODELM); /* Retry with new area. */
  315. }
  316. #endif