lj_mcode.c 9.8 KB

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