OSX/iOS: Fix SDK incompatibility.
[luajit-2.0.git] / src / lj_err.c
blob414ef477a43b2ef9515982ff63d08ab8fdfeab06
1 /*
2 ** Error handling.
3 ** Copyright (C) 2005-2023 Mike Pall. See Copyright Notice in luajit.h
4 */
6 #define lj_err_c
7 #define LUA_CORE
9 #include "lj_obj.h"
10 #include "lj_err.h"
11 #include "lj_debug.h"
12 #include "lj_str.h"
13 #include "lj_func.h"
14 #include "lj_state.h"
15 #include "lj_frame.h"
16 #include "lj_ff.h"
17 #include "lj_trace.h"
18 #include "lj_vm.h"
19 #include "lj_strfmt.h"
22 ** LuaJIT can either use internal or external frame unwinding:
24 ** - Internal frame unwinding (INT) is free-standing and doesn't require
25 ** any OS or library support.
27 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
29 ** Pros and Cons:
31 ** - EXT requires unwind tables for *all* functions on the C stack between
32 ** the pcall/catch and the error/throw. C modules used by Lua code can
33 ** throw errors, so these need to have unwind tables, too. Transitively
34 ** this applies to all system libraries used by C modules -- at least
35 ** when they have callbacks which may throw an error.
37 ** - INT is faster when actually throwing errors, but this happens rarely.
38 ** Setting up error handlers is zero-cost in any case.
40 ** - INT needs to save *all* callee-saved registers when entering the
41 ** interpreter. EXT only needs to save those actually used inside the
42 ** interpreter. JIT-compiled code may need to save some more.
44 ** - EXT provides full interoperability with C++ exceptions. You can throw
45 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
46 ** C++ destructors are called as needed. C++ exceptions caught by pcall
47 ** are converted to the string "C++ exception". Lua errors can be caught
48 ** with catch (...) in C++.
50 ** - INT has only limited support for automatically catching C++ exceptions
51 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
52 ** the wrapper function feature. Lua errors thrown through C++ frames
53 ** cannot be caught by C++ code and C++ destructors are not run.
55 ** - EXT can handle errors from internal helper functions that are called
56 ** from JIT-compiled code (except for Windows/x86 and 32 bit ARM).
57 ** INT has no choice but to call the panic handler, if this happens.
58 ** Note: this is mainly relevant for out-of-memory errors.
60 ** EXT is the default on all systems where the toolchain produces unwind
61 ** tables by default (*). This is hard-coded and/or detected in src/Makefile.
62 ** You can thwart the detection with: TARGET_XCFLAGS=-DLUAJIT_UNWIND_INTERNAL
64 ** INT is the default on all other systems.
66 ** EXT can be manually enabled for toolchains that are able to produce
67 ** conforming unwind tables:
68 ** "TARGET_XCFLAGS=-funwind-tables -DLUAJIT_UNWIND_EXTERNAL"
69 ** As explained above, *all* C code used directly or indirectly by LuaJIT
70 ** must be compiled with -funwind-tables (or -fexceptions). C++ code must
71 ** *not* be compiled with -fno-exceptions.
73 ** If you're unsure whether error handling inside the VM works correctly,
74 ** try running this and check whether it prints "OK":
76 ** luajit -e "print(select(2, load('OK')):match('OK'))"
78 ** (*) Originally, toolchains only generated unwind tables for C++ code. For
79 ** interoperability reasons, this can be manually enabled for plain C code,
80 ** too (with -funwind-tables). With the introduction of the x64 architecture,
81 ** the corresponding POSIX and Windows ABIs mandated unwind tables for all
82 ** code. Over the following years most desktop and server platforms have
83 ** enabled unwind tables by default on all architectures. OTOH mobile and
84 ** embedded platforms do not consistently mandate unwind tables.
87 /* -- Error messages ------------------------------------------------------ */
89 /* Error message strings. */
90 LJ_DATADEF const char *lj_err_allmsg =
91 #define ERRDEF(name, msg) msg "\0"
92 #include "lj_errmsg.h"
95 /* -- Internal frame unwinding -------------------------------------------- */
97 /* Unwind Lua stack and move error message to new top. */
98 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
100 lj_func_closeuv(L, top);
101 if (top < L->top-1) {
102 copyTV(L, top, L->top-1);
103 L->top = top+1;
105 lj_state_relimitstack(L);
108 /* Unwind until stop frame. Optionally cleanup frames. */
109 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
111 TValue *frame = L->base-1;
112 void *cf = L->cframe;
113 while (cf) {
114 int32_t nres = cframe_nres(cframe_raw(cf));
115 if (nres < 0) { /* C frame without Lua frame? */
116 TValue *top = restorestack(L, -nres);
117 if (frame < top) { /* Frame reached? */
118 if (errcode) {
119 L->base = frame+1;
120 L->cframe = cframe_prev(cf);
121 unwindstack(L, top);
123 return cf;
126 if (frame <= tvref(L->stack)+LJ_FR2)
127 break;
128 switch (frame_typep(frame)) {
129 case FRAME_LUA: /* Lua frame. */
130 case FRAME_LUAP:
131 frame = frame_prevl(frame);
132 break;
133 case FRAME_C: /* C frame. */
134 unwind_c:
135 #if LJ_UNWIND_EXT
136 if (errcode) {
137 L->base = frame_prevd(frame) + 1;
138 L->cframe = cframe_prev(cf);
139 unwindstack(L, frame - LJ_FR2);
140 } else if (cf != stopcf) {
141 cf = cframe_prev(cf);
142 frame = frame_prevd(frame);
143 break;
145 return NULL; /* Continue unwinding. */
146 #else
147 UNUSED(stopcf);
148 cf = cframe_prev(cf);
149 frame = frame_prevd(frame);
150 break;
151 #endif
152 case FRAME_CP: /* Protected C frame. */
153 if (cframe_canyield(cf)) { /* Resume? */
154 if (errcode) {
155 hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */
156 L->cframe = NULL;
157 L->status = (uint8_t)errcode;
159 return cf;
161 if (errcode) {
162 L->base = frame_prevd(frame) + 1;
163 L->cframe = cframe_prev(cf);
164 unwindstack(L, frame - LJ_FR2);
166 return cf;
167 case FRAME_CONT: /* Continuation frame. */
168 if (frame_iscont_fficb(frame))
169 goto unwind_c;
170 /* fallthrough */
171 case FRAME_VARG: /* Vararg frame. */
172 frame = frame_prevd(frame);
173 break;
174 case FRAME_PCALL: /* FF pcall() frame. */
175 case FRAME_PCALLH: /* FF pcall() frame inside hook. */
176 if (errcode) {
177 global_State *g;
178 if (errcode == LUA_YIELD) {
179 frame = frame_prevd(frame);
180 break;
182 g = G(L);
183 setgcref(g->cur_L, obj2gco(L));
184 if (frame_typep(frame) == FRAME_PCALL)
185 hook_leave(g);
186 L->base = frame_prevd(frame) + 1;
187 L->cframe = cf;
188 unwindstack(L, L->base);
190 return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
193 /* No C frame. */
194 if (errcode) {
195 L->base = tvref(L->stack)+1+LJ_FR2;
196 L->cframe = NULL;
197 unwindstack(L, L->base);
198 if (G(L)->panic)
199 G(L)->panic(L);
200 exit(EXIT_FAILURE);
202 return L; /* Anything non-NULL will do. */
205 /* -- External frame unwinding -------------------------------------------- */
207 #if LJ_ABI_WIN
210 ** Someone in Redmond owes me several days of my life. A lot of this is
211 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
212 ** from 3rd party docs or must be found by trial-and-error. They really
213 ** don't want you to write your own language-specific exception handler
214 ** or to interact gracefully with MSVC. :-(
217 #define WIN32_LEAN_AND_MEAN
218 #include <windows.h>
220 #if LJ_TARGET_X86
221 typedef void *UndocumentedDispatcherContext; /* Unused on x86. */
222 #else
223 /* Taken from: http://www.nynaeve.net/?p=99 */
224 typedef struct UndocumentedDispatcherContext {
225 ULONG64 ControlPc;
226 ULONG64 ImageBase;
227 PRUNTIME_FUNCTION FunctionEntry;
228 ULONG64 EstablisherFrame;
229 ULONG64 TargetIp;
230 PCONTEXT ContextRecord;
231 void (*LanguageHandler)(void);
232 PVOID HandlerData;
233 PUNWIND_HISTORY_TABLE HistoryTable;
234 ULONG ScopeIndex;
235 ULONG Fill0;
236 } UndocumentedDispatcherContext;
237 #endif
239 /* Another wild guess. */
240 extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
242 #if LJ_TARGET_X64 && defined(MINGW_SDK_INIT)
243 /* Workaround for broken MinGW64 declaration. */
244 VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");
245 #define RtlUnwindEx RtlUnwindEx_FIXED
246 #endif
248 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
249 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
251 #define LJ_EXCODE ((DWORD)0xe24c4a00)
252 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
253 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
254 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
256 /* Windows exception handler for interpreter frame. */
257 LJ_FUNCA int lj_err_unwind_win(EXCEPTION_RECORD *rec,
258 void *f, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
260 #if LJ_TARGET_X86
261 void *cf = (char *)f - CFRAME_OFS_SEH;
262 #elif LJ_TARGET_ARM64
263 void *cf = (char *)f - CFRAME_SIZE;
264 #else
265 void *cf = f;
266 #endif
267 lua_State *L = cframe_L(cf);
268 int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
269 LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
270 if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
271 if (rec->ExceptionCode == STATUS_LONGJUMP &&
272 rec->ExceptionRecord &&
273 LJ_EXCODE_CHECK(rec->ExceptionRecord->ExceptionCode)) {
274 errcode = LJ_EXCODE_ERRCODE(rec->ExceptionRecord->ExceptionCode);
275 if ((rec->ExceptionFlags & 0x20)) { /* EH_TARGET_UNWIND */
276 /* Unwinding is about to finish; revert the ExceptionCode so that
277 ** RtlRestoreContext does not try to restore from a _JUMP_BUFFER.
279 rec->ExceptionCode = 0;
282 /* Unwind internal frames. */
283 err_unwind(L, cf, errcode);
284 } else {
285 void *cf2 = err_unwind(L, cf, 0);
286 if (cf2) { /* We catch it, so start unwinding the upper frames. */
287 #if !LJ_TARGET_X86
288 EXCEPTION_RECORD rec2;
289 #endif
290 if (rec->ExceptionCode == LJ_MSVC_EXCODE ||
291 rec->ExceptionCode == LJ_GCC_EXCODE) {
292 #if !LJ_TARGET_CYGWIN
293 __DestructExceptionObject(rec, 1);
294 #endif
295 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
296 } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
297 /* Don't catch access violations etc. */
298 return 1; /* ExceptionContinueSearch */
300 #if LJ_TARGET_X86
301 UNUSED(ctx);
302 UNUSED(dispatch);
303 /* Call all handlers for all lower C frames (including ourselves) again
304 ** with EH_UNWINDING set. Then call the specified function, passing cf
305 ** and errcode.
307 lj_vm_rtlunwind(cf, (void *)rec,
308 (cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
309 (void *)lj_vm_unwind_ff : (void *)lj_vm_unwind_c, errcode);
310 /* lj_vm_rtlunwind does not return. */
311 #else
312 if (LJ_EXCODE_CHECK(rec->ExceptionCode)) {
313 /* For unwind purposes, wrap the EXCEPTION_RECORD in something that
314 ** looks like a longjmp, so that MSVC will execute C++ destructors in
315 ** the frames we unwind over. ExceptionInformation[0] should really
316 ** contain a _JUMP_BUFFER*, but hopefully nobody is looking too closely
317 ** at this point.
319 rec2.ExceptionCode = STATUS_LONGJUMP;
320 rec2.ExceptionRecord = rec;
321 rec2.ExceptionAddress = 0;
322 rec2.NumberParameters = 1;
323 rec2.ExceptionInformation[0] = (ULONG_PTR)ctx;
324 rec = &rec2;
326 /* Unwind the stack and call all handlers for all lower C frames
327 ** (including ourselves) again with EH_UNWINDING set. Then set
328 ** stack pointer = f, result = errcode and jump to the specified target.
330 RtlUnwindEx(f, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
331 lj_vm_unwind_ff_eh :
332 lj_vm_unwind_c_eh),
333 rec, (void *)(uintptr_t)errcode, dispatch->ContextRecord,
334 dispatch->HistoryTable);
335 /* RtlUnwindEx should never return. */
336 #endif
339 return 1; /* ExceptionContinueSearch */
342 #if LJ_UNWIND_JIT
344 #if LJ_TARGET_X64
345 #define CONTEXT_REG_PC Rip
346 #elif LJ_TARGET_ARM64
347 #define CONTEXT_REG_PC Pc
348 #else
349 #error "NYI: Windows arch-specific unwinder for JIT-compiled code"
350 #endif
352 /* Windows unwinder for JIT-compiled code. */
353 static void err_unwind_win_jit(global_State *g, int errcode)
355 CONTEXT ctx;
356 UNWIND_HISTORY_TABLE hist;
358 memset(&hist, 0, sizeof(hist));
359 RtlCaptureContext(&ctx);
360 while (1) {
361 DWORD64 frame, base, addr = ctx.CONTEXT_REG_PC;
362 void *hdata;
363 PRUNTIME_FUNCTION func = RtlLookupFunctionEntry(addr, &base, &hist);
364 if (!func) { /* Found frame without .pdata: must be JIT-compiled code. */
365 ExitNo exitno;
366 uintptr_t stub = lj_trace_unwind(G2J(g), (uintptr_t)(addr - sizeof(MCode)), &exitno);
367 if (stub) { /* Jump to side exit to unwind the trace. */
368 ctx.CONTEXT_REG_PC = stub;
369 G2J(g)->exitcode = errcode;
370 RtlRestoreContext(&ctx, NULL); /* Does not return. */
372 break;
374 RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, addr, func,
375 &ctx, &hdata, &frame, NULL);
376 if (!addr) break;
378 /* Unwinding failed, if we end up here. */
380 #endif
382 /* Raise Windows exception. */
383 static void err_raise_ext(global_State *g, int errcode)
385 #if LJ_UNWIND_JIT
386 if (tvref(g->jit_base)) {
387 err_unwind_win_jit(g, errcode);
388 return; /* Unwinding failed. */
390 #elif LJ_HASJIT
391 /* Cannot catch on-trace errors for Windows/x86 SEH. Unwind to interpreter. */
392 setmref(g->jit_base, NULL);
393 #endif
394 UNUSED(g);
395 RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
398 #elif !LJ_NO_UNWIND && (defined(__GNUC__) || defined(__clang__))
401 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
402 ** since various OS, distros and compilers mess up the header installation.
405 typedef struct _Unwind_Context _Unwind_Context;
407 #define _URC_OK 0
408 #define _URC_FATAL_PHASE2_ERROR 2
409 #define _URC_FATAL_PHASE1_ERROR 3
410 #define _URC_HANDLER_FOUND 6
411 #define _URC_INSTALL_CONTEXT 7
412 #define _URC_CONTINUE_UNWIND 8
413 #define _URC_FAILURE 9
415 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
416 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
417 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
418 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
420 #if !LJ_TARGET_ARM
422 typedef struct _Unwind_Exception
424 uint64_t exclass;
425 void (*excleanup)(int, struct _Unwind_Exception *);
426 uintptr_t p1, p2;
427 } __attribute__((__aligned__)) _Unwind_Exception;
428 #define UNWIND_EXCEPTION_TYPE _Unwind_Exception
430 extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
431 extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);
432 extern uintptr_t _Unwind_GetIP(_Unwind_Context *);
433 extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);
434 extern void _Unwind_DeleteException(_Unwind_Exception *);
435 extern int _Unwind_RaiseException(_Unwind_Exception *);
437 #define _UA_SEARCH_PHASE 1
438 #define _UA_CLEANUP_PHASE 2
439 #define _UA_HANDLER_FRAME 4
440 #define _UA_FORCE_UNWIND 8
442 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
443 LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
444 uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
446 void *cf;
447 lua_State *L;
448 if (version != 1)
449 return _URC_FATAL_PHASE1_ERROR;
450 cf = (void *)_Unwind_GetCFA(ctx);
451 L = cframe_L(cf);
452 if ((actions & _UA_SEARCH_PHASE)) {
453 #if LJ_UNWIND_EXT
454 if (err_unwind(L, cf, 0) == NULL)
455 return _URC_CONTINUE_UNWIND;
456 #endif
457 if (!LJ_UEXCLASS_CHECK(uexclass)) {
458 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
460 return _URC_HANDLER_FOUND;
462 if ((actions & _UA_CLEANUP_PHASE)) {
463 int errcode;
464 if (LJ_UEXCLASS_CHECK(uexclass)) {
465 errcode = LJ_UEXCLASS_ERRCODE(uexclass);
466 } else {
467 if ((actions & _UA_HANDLER_FRAME))
468 _Unwind_DeleteException(uex);
469 errcode = LUA_ERRRUN;
471 #if LJ_UNWIND_EXT
472 cf = err_unwind(L, cf, errcode);
473 if ((actions & _UA_FORCE_UNWIND)) {
474 return _URC_CONTINUE_UNWIND;
475 } else if (cf) {
476 ASMFunction ip;
477 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
478 ip = cframe_unwind_ff(cf) ? lj_vm_unwind_ff_eh : lj_vm_unwind_c_eh;
479 _Unwind_SetIP(ctx, (uintptr_t)lj_ptr_strip(ip));
480 return _URC_INSTALL_CONTEXT;
482 #if LJ_TARGET_X86ORX64
483 else if ((actions & _UA_HANDLER_FRAME)) {
484 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
485 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
487 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
488 _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
489 return _URC_INSTALL_CONTEXT;
491 #endif
492 #else
493 /* This is not the proper way to escape from the unwinder. We get away with
494 ** it on non-x64 because the interpreter restores all callee-saved regs.
496 lj_err_throw(L, errcode);
497 #if LJ_TARGET_X64
498 #error "Broken build system -- only use the provided Makefiles!"
499 #endif
500 #endif
502 return _URC_CONTINUE_UNWIND;
505 #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
506 struct dwarf_eh_bases { void *tbase, *dbase, *func; };
507 extern const void *_Unwind_Find_FDE(void *pc, struct dwarf_eh_bases *bases);
509 /* Verify that external error handling actually has a chance to work. */
510 void lj_err_verify(void)
512 #if !LJ_TARGET_OSX
513 /* Check disabled on MacOS due to brilliant software engineering at Apple. */
514 struct dwarf_eh_bases ehb;
515 lj_assertX(_Unwind_Find_FDE((void *)lj_err_throw, &ehb), "broken build: external frame unwinding enabled, but missing -funwind-tables");
516 #endif
517 /* Check disabled, because of broken Fedora/ARM64. See #722.
518 lj_assertX(_Unwind_Find_FDE((void *)_Unwind_RaiseException, &ehb), "broken build: external frame unwinding enabled, but system libraries have no unwind tables");
521 #endif
523 #if LJ_UNWIND_JIT
524 /* DWARF2 personality handler for JIT-compiled code. */
525 static int err_unwind_jit(int version, int actions,
526 uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
528 /* NYI: FFI C++ exception interoperability. */
529 if (version != 1 || !LJ_UEXCLASS_CHECK(uexclass))
530 return _URC_FATAL_PHASE1_ERROR;
531 if ((actions & _UA_SEARCH_PHASE)) {
532 return _URC_HANDLER_FOUND;
534 if ((actions & _UA_CLEANUP_PHASE)) {
535 global_State *g = *(global_State **)(uex+1);
536 ExitNo exitno;
537 uintptr_t addr = _Unwind_GetIP(ctx); /* Return address _after_ call. */
538 uintptr_t stub = lj_trace_unwind(G2J(g), addr - sizeof(MCode), &exitno);
539 lj_assertG(tvref(g->jit_base), "unexpected throw across mcode frame");
540 if (stub) { /* Jump to side exit to unwind the trace. */
541 G2J(g)->exitcode = LJ_UEXCLASS_ERRCODE(uexclass);
542 #ifdef LJ_TARGET_MIPS
543 _Unwind_SetGR(ctx, 4, stub);
544 _Unwind_SetGR(ctx, 5, exitno);
545 _Unwind_SetIP(ctx, (uintptr_t)(void *)lj_vm_unwind_stub);
546 #else
547 _Unwind_SetIP(ctx, stub);
548 #endif
549 return _URC_INSTALL_CONTEXT;
551 return _URC_FATAL_PHASE2_ERROR;
553 return _URC_FATAL_PHASE1_ERROR;
556 /* DWARF2 template frame info for JIT-compiled code.
558 ** After copying the template to the start of the mcode segment,
559 ** the frame handler function and the code size is patched.
560 ** The frame handler always installs a new context to jump to the exit,
561 ** so don't bother to add any unwind opcodes.
563 static const uint8_t err_frame_jit_template[] = {
564 #if LJ_BE
565 0,0,0,
566 #endif
567 LJ_64 ? 0x1c : 0x14, /* CIE length. */
568 #if LJ_LE
569 0,0,0,
570 #endif
571 0,0,0,0, 1, 'z','P','R',0, /* CIE mark, CIE version, augmentation. */
572 1, LJ_64 ? 0x78 : 0x7c, LJ_TARGET_EHRAREG, /* Code/data align, RA. */
573 #if LJ_64
574 10, 0, 0,0,0,0,0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
575 0,0,0,0,0, /* Alignment. */
576 #else
577 6, 0, 0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
578 0, /* Alignment. */
579 #endif
580 #if LJ_BE
581 0,0,0,
582 #endif
583 LJ_64 ? 0x14 : 0x10, /* FDE length. */
584 0,0,0,
585 LJ_64 ? 0x24 : 0x1c, /* CIE offset. */
586 0,0,0,
587 LJ_64 ? 0x14 : 0x10, /* Code offset. After Final FDE. */
588 #if LJ_LE
589 0,0,0,
590 #endif
591 0,0,0,0, 0, 0,0,0, /* Code size, augmentation length, alignment. */
592 #if LJ_64
593 0,0,0,0, /* Alignment. */
594 #endif
595 0,0,0,0 /* Final FDE. */
598 #define ERR_FRAME_JIT_OFS_HANDLER 0x12
599 #define ERR_FRAME_JIT_OFS_FDE (LJ_64 ? 0x20 : 0x18)
600 #define ERR_FRAME_JIT_OFS_CODE_SIZE (LJ_64 ? 0x2c : 0x24)
601 #if LJ_TARGET_OSX
602 #define ERR_FRAME_JIT_OFS_REGISTER ERR_FRAME_JIT_OFS_FDE
603 #else
604 #define ERR_FRAME_JIT_OFS_REGISTER 0
605 #endif
607 extern void __register_frame(const void *);
608 extern void __deregister_frame(const void *);
610 uint8_t *lj_err_register_mcode(void *base, size_t sz, uint8_t *info)
612 ASMFunction handler = (ASMFunction)err_unwind_jit;
613 memcpy(info, err_frame_jit_template, sizeof(err_frame_jit_template));
614 #if LJ_ABI_PAUTH
615 #if LJ_TARGET_ARM64
616 handler = ptrauth_auth_and_resign(handler,
617 ptrauth_key_function_pointer, 0,
618 ptrauth_key_process_independent_code, info + ERR_FRAME_JIT_OFS_HANDLER);
619 #else
620 #error "missing pointer authentication support for this architecture"
621 #endif
622 #endif
623 memcpy(info + ERR_FRAME_JIT_OFS_HANDLER, &handler, sizeof(handler));
624 *(uint32_t *)(info + ERR_FRAME_JIT_OFS_CODE_SIZE) =
625 (uint32_t)(sz - sizeof(err_frame_jit_template) - (info - (uint8_t *)base));
626 __register_frame(info + ERR_FRAME_JIT_OFS_REGISTER);
627 #ifdef LUA_USE_ASSERT
629 struct dwarf_eh_bases ehb;
630 lj_assertX(_Unwind_Find_FDE(info + sizeof(err_frame_jit_template)+1, &ehb),
631 "bad JIT unwind table registration");
633 #endif
634 return info + sizeof(err_frame_jit_template);
637 void lj_err_deregister_mcode(void *base, size_t sz, uint8_t *info)
639 UNUSED(base); UNUSED(sz);
640 __deregister_frame(info + ERR_FRAME_JIT_OFS_REGISTER);
642 #endif
644 #else /* LJ_TARGET_ARM */
646 #define _US_VIRTUAL_UNWIND_FRAME 0
647 #define _US_UNWIND_FRAME_STARTING 1
648 #define _US_ACTION_MASK 3
649 #define _US_FORCE_UNWIND 8
651 typedef struct _Unwind_Control_Block _Unwind_Control_Block;
652 #define UNWIND_EXCEPTION_TYPE _Unwind_Control_Block
654 struct _Unwind_Control_Block {
655 uint64_t exclass;
656 uint32_t misc[20];
659 extern int _Unwind_RaiseException(_Unwind_Control_Block *);
660 extern int __gnu_unwind_frame(_Unwind_Control_Block *, _Unwind_Context *);
661 extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);
662 extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);
664 static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
666 uint32_t v;
667 _Unwind_VRS_Get(ctx, 0, r, 0, &v);
668 return v;
671 static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
673 _Unwind_VRS_Set(ctx, 0, r, 0, &v);
676 extern void lj_vm_unwind_ext(void);
678 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
679 LJ_FUNCA int lj_err_unwind_arm(int state, _Unwind_Control_Block *ucb,
680 _Unwind_Context *ctx)
682 void *cf = (void *)_Unwind_GetGR(ctx, 13);
683 lua_State *L = cframe_L(cf);
684 int errcode;
686 switch ((state & _US_ACTION_MASK)) {
687 case _US_VIRTUAL_UNWIND_FRAME:
688 if ((state & _US_FORCE_UNWIND)) break;
689 return _URC_HANDLER_FOUND;
690 case _US_UNWIND_FRAME_STARTING:
691 if (LJ_UEXCLASS_CHECK(ucb->exclass)) {
692 errcode = LJ_UEXCLASS_ERRCODE(ucb->exclass);
693 } else {
694 errcode = LUA_ERRRUN;
695 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
697 cf = err_unwind(L, cf, errcode);
698 if ((state & _US_FORCE_UNWIND) || cf == NULL) break;
699 _Unwind_SetGR(ctx, 15, (uint32_t)lj_vm_unwind_ext);
700 _Unwind_SetGR(ctx, 0, (uint32_t)ucb);
701 _Unwind_SetGR(ctx, 1, (uint32_t)errcode);
702 _Unwind_SetGR(ctx, 2, cframe_unwind_ff(cf) ?
703 (uint32_t)lj_vm_unwind_ff_eh :
704 (uint32_t)lj_vm_unwind_c_eh);
705 return _URC_INSTALL_CONTEXT;
706 default:
707 return _URC_FAILURE;
709 if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
710 return _URC_FAILURE;
711 #ifdef LUA_USE_ASSERT
712 /* We should never get here unless this is a forced unwind aka backtrace. */
713 if (_Unwind_GetGR(ctx, 0) == 0xff33aa77) {
714 _Unwind_SetGR(ctx, 0, 0xff33aa88);
716 #endif
717 return _URC_CONTINUE_UNWIND;
720 #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
721 typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *);
722 extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
724 static int err_verify_bt(_Unwind_Context *ctx, int *got)
726 if (_Unwind_GetGR(ctx, 0) == 0xff33aa88) { *got = 2; }
727 else if (*got == 0) { *got = 1; _Unwind_SetGR(ctx, 0, 0xff33aa77); }
728 return _URC_OK;
731 /* Verify that external error handling actually has a chance to work. */
732 void lj_err_verify(void)
734 int got = 0;
735 _Unwind_Backtrace((_Unwind_Trace_Fn)err_verify_bt, &got);
736 lj_assertX(got == 2, "broken build: external frame unwinding enabled, but missing -funwind-tables");
738 #endif
741 ** Note: LJ_UNWIND_JIT is not implemented for 32 bit ARM.
743 ** The quirky ARM unwind API doesn't have __register_frame().
744 ** A potential workaround might involve _Unwind_Backtrace.
745 ** But most 32 bit ARM targets don't qualify for LJ_UNWIND_EXT, anyway,
746 ** since they are built without unwind tables by default.
749 #endif /* LJ_TARGET_ARM */
752 #if LJ_UNWIND_EXT
753 static __thread struct {
754 UNWIND_EXCEPTION_TYPE ex;
755 global_State *g;
756 } static_uex;
758 /* Raise external exception. */
759 static void err_raise_ext(global_State *g, int errcode)
761 memset(&static_uex, 0, sizeof(static_uex));
762 static_uex.ex.exclass = LJ_UEXCLASS_MAKE(errcode);
763 static_uex.g = g;
764 _Unwind_RaiseException(&static_uex.ex);
767 #endif
769 #endif
771 /* -- Error handling ------------------------------------------------------ */
773 /* Throw error. Find catch frame, unwind stack and continue. */
774 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
776 global_State *g = G(L);
777 lj_trace_abort(g);
778 L->status = LUA_OK;
779 #if LJ_UNWIND_EXT
780 err_raise_ext(g, errcode);
782 ** A return from this function signals a corrupt C stack that cannot be
783 ** unwound. We have no choice but to call the panic function and exit.
785 ** Usually this is caused by a C function without unwind information.
786 ** This may happen if you've manually enabled LUAJIT_UNWIND_EXTERNAL
787 ** and forgot to recompile *every* non-C++ file with -funwind-tables.
789 if (G(L)->panic)
790 G(L)->panic(L);
791 #else
792 #if LJ_HASJIT
793 setmref(g->jit_base, NULL);
794 #endif
796 void *cf = err_unwind(L, NULL, errcode);
797 if (cframe_unwind_ff(cf))
798 lj_vm_unwind_ff(cframe_raw(cf));
799 else
800 lj_vm_unwind_c(cframe_raw(cf), errcode);
802 #endif
803 exit(EXIT_FAILURE);
806 /* Return string object for error message. */
807 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
809 return lj_str_newz(L, err2msg(em));
812 /* Out-of-memory error. */
813 LJ_NOINLINE void lj_err_mem(lua_State *L)
815 if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */
816 lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
817 if (LJ_HASJIT) {
818 TValue *base = tvref(G(L)->jit_base);
819 if (base) L->base = base;
821 if (curr_funcisL(L)) {
822 L->top = curr_topL(L);
823 if (LJ_UNLIKELY(L->top > tvref(L->maxstack))) {
824 /* The current Lua frame violates the stack. Replace it with a dummy. */
825 L->top = L->base;
826 setframe_gc(L->base - 1 - LJ_FR2, obj2gco(L), LJ_TTHREAD);
829 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
830 lj_err_throw(L, LUA_ERRMEM);
833 /* Find error function for runtime errors. Requires an extra stack traversal. */
834 static ptrdiff_t finderrfunc(lua_State *L)
836 cTValue *frame = L->base-1, *bot = tvref(L->stack)+LJ_FR2;
837 void *cf = L->cframe;
838 while (frame > bot && cf) {
839 while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */
840 if (frame >= restorestack(L, -cframe_nres(cf)))
841 break;
842 if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */
843 return cframe_errfunc(cf);
844 cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */
845 if (cf == NULL)
846 return 0;
848 switch (frame_typep(frame)) {
849 case FRAME_LUA:
850 case FRAME_LUAP:
851 frame = frame_prevl(frame);
852 break;
853 case FRAME_C:
854 cf = cframe_prev(cf);
855 /* fallthrough */
856 case FRAME_VARG:
857 frame = frame_prevd(frame);
858 break;
859 case FRAME_CONT:
860 if (frame_iscont_fficb(frame))
861 cf = cframe_prev(cf);
862 frame = frame_prevd(frame);
863 break;
864 case FRAME_CP:
865 if (cframe_canyield(cf)) return 0;
866 if (cframe_errfunc(cf) >= 0)
867 return cframe_errfunc(cf);
868 cf = cframe_prev(cf);
869 frame = frame_prevd(frame);
870 break;
871 case FRAME_PCALL:
872 case FRAME_PCALLH:
873 if (frame_func(frame_prevd(frame))->c.ffid == FF_xpcall)
874 return savestack(L, frame_prevd(frame)+1); /* xpcall's errorfunc. */
875 return 0;
876 default:
877 lj_assertL(0, "bad frame type");
878 return 0;
881 return 0;
884 /* Runtime error. */
885 LJ_NOINLINE void LJ_FASTCALL lj_err_run(lua_State *L)
887 ptrdiff_t ef = (LJ_HASJIT && tvref(G(L)->jit_base)) ? 0 : finderrfunc(L);
888 if (ef) {
889 TValue *errfunc, *top;
890 lj_state_checkstack(L, LUA_MINSTACK * 2); /* Might raise new error. */
891 lj_trace_abort(G(L));
892 errfunc = restorestack(L, ef);
893 top = L->top;
894 if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
895 setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
896 lj_err_throw(L, LUA_ERRERR);
898 L->status = LUA_ERRERR;
899 copyTV(L, top+LJ_FR2, top-1);
900 copyTV(L, top-1, errfunc);
901 if (LJ_FR2) setnilV(top++);
902 L->top = top+1;
903 lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */
905 lj_err_throw(L, LUA_ERRRUN);
908 /* Stack overflow error. */
909 void LJ_FASTCALL lj_err_stkov(lua_State *L)
911 lj_debug_addloc(L, err2msg(LJ_ERR_STKOV), L->base-1, NULL);
912 lj_err_run(L);
915 #if LJ_HASJIT
916 /* Rethrow error after doing a trace exit. */
917 LJ_NOINLINE void LJ_FASTCALL lj_err_trace(lua_State *L, int errcode)
919 if (errcode == LUA_ERRRUN)
920 lj_err_run(L);
921 else
922 lj_err_throw(L, errcode);
924 #endif
926 /* Formatted runtime error message. */
927 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
929 const char *msg;
930 va_list argp;
931 va_start(argp, em);
932 if (LJ_HASJIT) {
933 TValue *base = tvref(G(L)->jit_base);
934 if (base) L->base = base;
936 if (curr_funcisL(L)) L->top = curr_topL(L);
937 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
938 va_end(argp);
939 lj_debug_addloc(L, msg, L->base-1, NULL);
940 lj_err_run(L);
943 /* Non-vararg variant for better calling conventions. */
944 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
946 err_msgv(L, em);
949 /* Lexer error. */
950 LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
951 BCLine line, ErrMsg em, va_list argp)
953 char buff[LUA_IDSIZE];
954 const char *msg;
955 lj_debug_shortname(buff, src, line);
956 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
957 msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);
958 if (tok)
959 lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
960 lj_err_throw(L, LUA_ERRSYNTAX);
963 /* Typecheck error for operands. */
964 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
966 const char *tname = lj_typename(o);
967 const char *opname = err2msg(opm);
968 if (curr_funcisL(L)) {
969 GCproto *pt = curr_proto(L);
970 const BCIns *pc = cframe_Lpc(L) - 1;
971 const char *oname = NULL;
972 const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
973 if (kind)
974 err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
976 err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
979 /* Typecheck error for ordered comparisons. */
980 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
982 const char *t1 = lj_typename(o1);
983 const char *t2 = lj_typename(o2);
984 err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
985 /* This assumes the two "boolean" entries are commoned by the C compiler. */
988 /* Typecheck error for __call. */
989 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
991 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
992 ** L->base still points to the caller. So add a dummy frame with L instead
993 ** of a function. See lua_getstack().
995 const BCIns *pc = cframe_Lpc(L);
996 if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
997 const char *tname = lj_typename(o);
998 setframe_gc(o, obj2gco(L), LJ_TTHREAD);
999 if (LJ_FR2) o++;
1000 setframe_pc(o, pc);
1001 L->top = L->base = o+1;
1002 err_msgv(L, LJ_ERR_BADCALL, tname);
1004 lj_err_optype(L, o, LJ_ERR_OPCALL);
1007 /* Error in context of caller. */
1008 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
1010 TValue *frame = NULL, *pframe = NULL;
1011 if (!(LJ_HASJIT && tvref(G(L)->jit_base))) {
1012 frame = L->base-1;
1013 if (frame_islua(frame)) {
1014 pframe = frame_prevl(frame);
1015 } else if (frame_iscont(frame)) {
1016 if (frame_iscont_fficb(frame)) {
1017 pframe = frame;
1018 frame = NULL;
1019 } else {
1020 pframe = frame_prevd(frame);
1021 #if LJ_HASFFI
1022 /* Remove frame for FFI metamethods. */
1023 if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
1024 frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
1025 L->base = pframe+1;
1026 L->top = frame;
1027 setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));
1029 #endif
1033 lj_debug_addloc(L, msg, pframe, frame);
1034 lj_err_run(L);
1037 /* Formatted error in context of caller. */
1038 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
1040 const char *msg;
1041 va_list argp;
1042 va_start(argp, em);
1043 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
1044 va_end(argp);
1045 lj_err_callermsg(L, msg);
1048 /* Error in context of caller. */
1049 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
1051 lj_err_callermsg(L, err2msg(em));
1054 /* Argument error message. */
1055 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
1056 const char *msg)
1058 const char *fname = "?";
1059 const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
1060 if (narg < 0 && narg > LUA_REGISTRYINDEX)
1061 narg = (int)(L->top - L->base) + narg + 1;
1062 if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
1063 msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
1064 else
1065 msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
1066 lj_err_callermsg(L, msg);
1069 /* Formatted argument error. */
1070 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
1072 const char *msg;
1073 va_list argp;
1074 va_start(argp, em);
1075 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
1076 va_end(argp);
1077 err_argmsg(L, narg, msg);
1080 /* Argument error. */
1081 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
1083 err_argmsg(L, narg, err2msg(em));
1086 /* Typecheck error for arguments. */
1087 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
1089 const char *tname, *msg;
1090 if (narg <= LUA_REGISTRYINDEX) {
1091 if (narg >= LUA_GLOBALSINDEX) {
1092 tname = lj_obj_itypename[~LJ_TTAB];
1093 } else {
1094 GCfunc *fn = curr_func(L);
1095 int idx = LUA_GLOBALSINDEX - narg;
1096 if (idx <= fn->c.nupvalues)
1097 tname = lj_typename(&fn->c.upvalue[idx-1]);
1098 else
1099 tname = lj_obj_typename[0];
1101 } else {
1102 TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
1103 tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
1105 msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
1106 err_argmsg(L, narg, msg);
1109 /* Typecheck error for arguments. */
1110 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
1112 lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
1115 /* -- Public error handling API ------------------------------------------- */
1117 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
1119 lua_CFunction old = G(L)->panic;
1120 G(L)->panic = panicf;
1121 return old;
1124 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
1125 LUA_API int lua_error(lua_State *L)
1127 lj_err_run(L);
1128 return 0; /* unreachable */
1131 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
1133 err_argmsg(L, narg, msg);
1134 return 0; /* unreachable */
1137 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
1139 lj_err_argtype(L, narg, xname);
1140 return 0; /* unreachable */
1143 LUALIB_API void luaL_where(lua_State *L, int level)
1145 int size;
1146 cTValue *frame = lj_debug_frame(L, level, &size);
1147 lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
1150 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
1152 const char *msg;
1153 va_list argp;
1154 va_start(argp, fmt);
1155 msg = lj_strfmt_pushvf(L, fmt, argp);
1156 va_end(argp);
1157 lj_err_callermsg(L, msg);
1158 return 0; /* unreachable */