lj_mcode.c 10.0 KB

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