3 ** Copyright (C) 2005-2025 Mike Pall. See Copyright Notice in luajit.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.
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);
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
;
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? */
120 L
->cframe
= cframe_prev(cf
);
126 if (frame
<= tvref(L
->stack
)+LJ_FR2
)
128 switch (frame_typep(frame
)) {
129 case FRAME_LUA
: /* Lua frame. */
131 frame
= frame_prevl(frame
);
133 case FRAME_C
: /* C frame. */
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
);
145 return NULL
; /* Continue unwinding. */
148 cf
= cframe_prev(cf
);
149 frame
= frame_prevd(frame
);
152 case FRAME_CP
: /* Protected C frame. */
153 if (cframe_canyield(cf
)) { /* Resume? */
155 hook_leave(G(L
)); /* Assumes nobody uses coroutines inside hooks. */
157 L
->status
= (uint8_t)errcode
;
162 L
->base
= frame_prevd(frame
) + 1;
163 L
->cframe
= cframe_prev(cf
);
164 unwindstack(L
, frame
- LJ_FR2
);
167 case FRAME_CONT
: /* Continuation frame. */
168 if (frame_iscont_fficb(frame
))
171 case FRAME_VARG
: /* Vararg frame. */
172 frame
= frame_prevd(frame
);
174 case FRAME_PCALL
: /* FF pcall() frame. */
175 case FRAME_PCALLH
: /* FF pcall() frame inside hook. */
178 if (errcode
== LUA_YIELD
) {
179 frame
= frame_prevd(frame
);
183 setgcref(g
->cur_L
, obj2gco(L
));
184 if (frame_typep(frame
) == FRAME_PCALL
)
186 L
->base
= frame_prevd(frame
) + 1;
188 unwindstack(L
, L
->base
);
190 return (void *)((intptr_t)cf
| CFRAME_UNWIND_FF
);
195 L
->base
= tvref(L
->stack
)+1+LJ_FR2
;
197 unwindstack(L
, L
->base
);
202 return L
; /* Anything non-NULL will do. */
205 /* -- External frame unwinding -------------------------------------------- */
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
221 typedef void *UndocumentedDispatcherContext
; /* Unused on x86. */
223 /* Taken from: http://www.nynaeve.net/?p=99 */
224 typedef struct UndocumentedDispatcherContext
{
227 PRUNTIME_FUNCTION FunctionEntry
;
228 ULONG64 EstablisherFrame
;
230 PCONTEXT ContextRecord
;
231 void (*LanguageHandler
)(void);
233 PUNWIND_HISTORY_TABLE HistoryTable
;
236 } UndocumentedDispatcherContext
;
239 /* Another wild guess. */
240 extern void __DestructExceptionObject(EXCEPTION_RECORD
*rec
, int nothrow
);
242 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
243 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
245 #define LJ_EXCODE ((DWORD)0xe24c4a00)
246 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
247 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
248 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
250 /* Windows exception handler for interpreter frame. */
251 LJ_FUNCA
int lj_err_unwind_win(EXCEPTION_RECORD
*rec
,
252 void *f
, CONTEXT
*ctx
, UndocumentedDispatcherContext
*dispatch
)
255 void *cf
= (char *)f
- CFRAME_OFS_SEH
;
256 #elif LJ_TARGET_ARM64
257 void *cf
= (char *)f
- CFRAME_SIZE
;
261 lua_State
*L
= cframe_L(cf
);
262 int errcode
= LJ_EXCODE_CHECK(rec
->ExceptionCode
) ?
263 LJ_EXCODE_ERRCODE(rec
->ExceptionCode
) : LUA_ERRRUN
;
264 if ((rec
->ExceptionFlags
& 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
265 if (rec
->ExceptionCode
== STATUS_LONGJUMP
&&
266 rec
->ExceptionRecord
&&
267 LJ_EXCODE_CHECK(rec
->ExceptionRecord
->ExceptionCode
)) {
268 errcode
= LJ_EXCODE_ERRCODE(rec
->ExceptionRecord
->ExceptionCode
);
269 if ((rec
->ExceptionFlags
& 0x20)) { /* EH_TARGET_UNWIND */
270 /* Unwinding is about to finish; revert the ExceptionCode so that
271 ** RtlRestoreContext does not try to restore from a _JUMP_BUFFER.
273 rec
->ExceptionCode
= 0;
276 /* Unwind internal frames. */
277 err_unwind(L
, cf
, errcode
);
279 void *cf2
= err_unwind(L
, cf
, 0);
280 if (cf2
) { /* We catch it, so start unwinding the upper frames. */
282 EXCEPTION_RECORD rec2
;
284 if (rec
->ExceptionCode
== LJ_MSVC_EXCODE
||
285 rec
->ExceptionCode
== LJ_GCC_EXCODE
) {
286 #if !LJ_TARGET_CYGWIN
287 __DestructExceptionObject(rec
, 1);
289 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
290 } else if (!LJ_EXCODE_CHECK(rec
->ExceptionCode
)) {
291 /* Don't catch access violations etc. */
292 return 1; /* ExceptionContinueSearch */
297 /* Call all handlers for all lower C frames (including ourselves) again
298 ** with EH_UNWINDING set. Then call the specified function, passing cf
301 lj_vm_rtlunwind(cf
, (void *)rec
,
302 (cframe_unwind_ff(cf2
) && errcode
!= LUA_YIELD
) ?
303 (void *)lj_vm_unwind_ff
: (void *)lj_vm_unwind_c
, errcode
);
304 /* lj_vm_rtlunwind does not return. */
306 if (LJ_EXCODE_CHECK(rec
->ExceptionCode
)) {
307 /* For unwind purposes, wrap the EXCEPTION_RECORD in something that
308 ** looks like a longjmp, so that MSVC will execute C++ destructors in
309 ** the frames we unwind over. ExceptionInformation[0] should really
310 ** contain a _JUMP_BUFFER*, but hopefully nobody is looking too closely
313 rec2
.ExceptionCode
= STATUS_LONGJUMP
;
314 rec2
.ExceptionRecord
= rec
;
315 rec2
.ExceptionAddress
= 0;
316 rec2
.NumberParameters
= 1;
317 rec2
.ExceptionInformation
[0] = (ULONG_PTR
)ctx
;
320 /* Unwind the stack and call all handlers for all lower C frames
321 ** (including ourselves) again with EH_UNWINDING set. Then set
322 ** stack pointer = f, result = errcode and jump to the specified target.
324 RtlUnwindEx(f
, (void *)((cframe_unwind_ff(cf2
) && errcode
!= LUA_YIELD
) ?
327 rec
, (void *)(uintptr_t)errcode
, dispatch
->ContextRecord
,
328 dispatch
->HistoryTable
);
329 /* RtlUnwindEx should never return. */
333 return 1; /* ExceptionContinueSearch */
339 #define CONTEXT_REG_PC Rip
340 #elif LJ_TARGET_ARM64
341 #define CONTEXT_REG_PC Pc
343 #error "NYI: Windows arch-specific unwinder for JIT-compiled code"
346 /* Windows unwinder for JIT-compiled code. */
347 static void err_unwind_win_jit(global_State
*g
, int errcode
)
350 UNWIND_HISTORY_TABLE hist
;
352 memset(&hist
, 0, sizeof(hist
));
353 RtlCaptureContext(&ctx
);
355 DWORD64 frame
, base
, addr
= ctx
.CONTEXT_REG_PC
;
357 PRUNTIME_FUNCTION func
= RtlLookupFunctionEntry(addr
, &base
, &hist
);
358 if (!func
) { /* Found frame without .pdata: must be JIT-compiled code. */
360 uintptr_t stub
= lj_trace_unwind(G2J(g
), (uintptr_t)(addr
- sizeof(MCode
)), &exitno
);
361 if (stub
) { /* Jump to side exit to unwind the trace. */
362 ctx
.CONTEXT_REG_PC
= stub
;
363 G2J(g
)->exitcode
= errcode
;
364 RtlRestoreContext(&ctx
, NULL
); /* Does not return. */
368 RtlVirtualUnwind(UNW_FLAG_NHANDLER
, base
, addr
, func
,
369 &ctx
, &hdata
, &frame
, NULL
);
372 /* Unwinding failed, if we end up here. */
376 /* Raise Windows exception. */
377 static void err_raise_ext(global_State
*g
, int errcode
)
380 if (tvref(g
->jit_base
)) {
381 err_unwind_win_jit(g
, errcode
);
382 return; /* Unwinding failed. */
385 /* Cannot catch on-trace errors for Windows/x86 SEH. Unwind to interpreter. */
386 setmref(g
->jit_base
, NULL
);
389 RaiseException(LJ_EXCODE_MAKE(errcode
), 1 /* EH_NONCONTINUABLE */, 0, NULL
);
392 #elif !LJ_NO_UNWIND && (defined(__GNUC__) || defined(__clang__))
395 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
396 ** since various OS, distros and compilers mess up the header installation.
399 typedef struct _Unwind_Context _Unwind_Context
;
402 #define _URC_FATAL_PHASE2_ERROR 2
403 #define _URC_FATAL_PHASE1_ERROR 3
404 #define _URC_HANDLER_FOUND 6
405 #define _URC_INSTALL_CONTEXT 7
406 #define _URC_CONTINUE_UNWIND 8
407 #define _URC_FAILURE 9
409 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
410 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
411 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
412 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
416 typedef struct _Unwind_Exception
419 void (*excleanup
)(int, struct _Unwind_Exception
*);
421 } __attribute__((__aligned__
)) _Unwind_Exception
;
422 #define UNWIND_EXCEPTION_TYPE _Unwind_Exception
424 extern uintptr_t _Unwind_GetCFA(_Unwind_Context
*);
425 extern void _Unwind_SetGR(_Unwind_Context
*, int, uintptr_t);
426 extern uintptr_t _Unwind_GetIP(_Unwind_Context
*);
427 extern void _Unwind_SetIP(_Unwind_Context
*, uintptr_t);
428 extern void _Unwind_DeleteException(_Unwind_Exception
*);
429 extern int _Unwind_RaiseException(_Unwind_Exception
*);
431 #define _UA_SEARCH_PHASE 1
432 #define _UA_CLEANUP_PHASE 2
433 #define _UA_HANDLER_FRAME 4
434 #define _UA_FORCE_UNWIND 8
436 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
437 LJ_FUNCA
int lj_err_unwind_dwarf(int version
, int actions
,
438 uint64_t uexclass
, _Unwind_Exception
*uex
, _Unwind_Context
*ctx
)
443 return _URC_FATAL_PHASE1_ERROR
;
444 cf
= (void *)_Unwind_GetCFA(ctx
);
446 if ((actions
& _UA_SEARCH_PHASE
)) {
448 if (err_unwind(L
, cf
, 0) == NULL
)
449 return _URC_CONTINUE_UNWIND
;
451 if (!LJ_UEXCLASS_CHECK(uexclass
)) {
452 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
454 return _URC_HANDLER_FOUND
;
456 if ((actions
& _UA_CLEANUP_PHASE
)) {
458 if (LJ_UEXCLASS_CHECK(uexclass
)) {
459 errcode
= LJ_UEXCLASS_ERRCODE(uexclass
);
461 if ((actions
& _UA_HANDLER_FRAME
))
462 _Unwind_DeleteException(uex
);
463 errcode
= LUA_ERRRUN
;
466 cf
= err_unwind(L
, cf
, errcode
);
467 if ((actions
& _UA_FORCE_UNWIND
)) {
468 return _URC_CONTINUE_UNWIND
;
471 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
472 ip
= cframe_unwind_ff(cf
) ? lj_vm_unwind_ff_eh
: lj_vm_unwind_c_eh
;
473 _Unwind_SetIP(ctx
, (uintptr_t)lj_ptr_strip(ip
));
474 return _URC_INSTALL_CONTEXT
;
476 #if LJ_TARGET_X86ORX64
477 else if ((actions
& _UA_HANDLER_FRAME
)) {
478 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
479 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
481 _Unwind_SetGR(ctx
, LJ_TARGET_EHRETREG
, errcode
);
482 _Unwind_SetIP(ctx
, (uintptr_t)lj_vm_unwind_rethrow
);
483 return _URC_INSTALL_CONTEXT
;
487 /* This is not the proper way to escape from the unwinder. We get away with
488 ** it on non-x64 because the interpreter restores all callee-saved regs.
490 lj_err_throw(L
, errcode
);
492 #error "Broken build system -- only use the provided Makefiles!"
496 return _URC_CONTINUE_UNWIND
;
499 #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
500 struct dwarf_eh_bases
{ void *tbase
, *dbase
, *func
; };
501 extern const void *_Unwind_Find_FDE(void *pc
, struct dwarf_eh_bases
*bases
);
503 /* Verify that external error handling actually has a chance to work. */
504 void lj_err_verify(void)
507 /* Check disabled on MacOS due to brilliant software engineering at Apple. */
508 struct dwarf_eh_bases ehb
;
509 lj_assertX(_Unwind_Find_FDE((void *)lj_err_throw
, &ehb
), "broken build: external frame unwinding enabled, but missing -funwind-tables");
511 /* Check disabled, because of broken Fedora/ARM64. See #722.
512 lj_assertX(_Unwind_Find_FDE((void *)_Unwind_RaiseException, &ehb), "broken build: external frame unwinding enabled, but system libraries have no unwind tables");
518 /* DWARF2 personality handler for JIT-compiled code. */
519 static int err_unwind_jit(int version
, int actions
,
520 uint64_t uexclass
, _Unwind_Exception
*uex
, _Unwind_Context
*ctx
)
522 /* NYI: FFI C++ exception interoperability. */
523 if (version
!= 1 || !LJ_UEXCLASS_CHECK(uexclass
))
524 return _URC_FATAL_PHASE1_ERROR
;
525 if ((actions
& _UA_SEARCH_PHASE
)) {
526 return _URC_HANDLER_FOUND
;
528 if ((actions
& _UA_CLEANUP_PHASE
)) {
529 global_State
*g
= *(global_State
**)(uex
+1);
531 uintptr_t addr
= _Unwind_GetIP(ctx
); /* Return address _after_ call. */
532 uintptr_t stub
= lj_trace_unwind(G2J(g
), addr
- sizeof(MCode
), &exitno
);
533 lj_assertG(tvref(g
->jit_base
), "unexpected throw across mcode frame");
534 if (stub
) { /* Jump to side exit to unwind the trace. */
535 G2J(g
)->exitcode
= LJ_UEXCLASS_ERRCODE(uexclass
);
536 #ifdef LJ_TARGET_MIPS
537 _Unwind_SetGR(ctx
, 4, stub
);
538 _Unwind_SetGR(ctx
, 5, exitno
);
539 _Unwind_SetIP(ctx
, (uintptr_t)(void *)lj_vm_unwind_stub
);
541 _Unwind_SetIP(ctx
, stub
);
543 return _URC_INSTALL_CONTEXT
;
545 return _URC_FATAL_PHASE2_ERROR
;
547 return _URC_FATAL_PHASE1_ERROR
;
550 /* DWARF2 template frame info for JIT-compiled code.
552 ** After copying the template to the start of the mcode segment,
553 ** the frame handler function and the code size is patched.
554 ** The frame handler always installs a new context to jump to the exit,
555 ** so don't bother to add any unwind opcodes.
557 static const uint8_t err_frame_jit_template
[] = {
561 LJ_64
? 0x1c : 0x14, /* CIE length. */
565 0,0,0,0, 1, 'z','P','R',0, /* CIE mark, CIE version, augmentation. */
566 1, LJ_64
? 0x78 : 0x7c, LJ_TARGET_EHRAREG
, /* Code/data align, RA. */
568 10, 0, 0,0,0,0,0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
569 0,0,0,0,0, /* Alignment. */
571 6, 0, 0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
577 LJ_64
? 0x14 : 0x10, /* FDE length. */
579 LJ_64
? 0x24 : 0x1c, /* CIE offset. */
581 LJ_64
? 0x14 : 0x10, /* Code offset. After Final FDE. */
585 0,0,0,0, 0, 0,0,0, /* Code size, augmentation length, alignment. */
587 0,0,0,0, /* Alignment. */
589 0,0,0,0 /* Final FDE. */
592 #define ERR_FRAME_JIT_OFS_HANDLER 0x12
593 #define ERR_FRAME_JIT_OFS_FDE (LJ_64 ? 0x20 : 0x18)
594 #define ERR_FRAME_JIT_OFS_CODE_SIZE (LJ_64 ? 0x2c : 0x24)
596 #define ERR_FRAME_JIT_OFS_REGISTER ERR_FRAME_JIT_OFS_FDE
598 #define ERR_FRAME_JIT_OFS_REGISTER 0
601 extern void __register_frame(const void *);
602 extern void __deregister_frame(const void *);
604 uint8_t *lj_err_register_mcode(void *base
, size_t sz
, uint8_t *info
)
606 ASMFunction handler
= (ASMFunction
)err_unwind_jit
;
607 memcpy(info
, err_frame_jit_template
, sizeof(err_frame_jit_template
));
610 handler
= ptrauth_auth_and_resign(handler
,
611 ptrauth_key_function_pointer
, 0,
612 ptrauth_key_process_independent_code
, info
+ ERR_FRAME_JIT_OFS_HANDLER
);
614 #error "missing pointer authentication support for this architecture"
617 memcpy(info
+ ERR_FRAME_JIT_OFS_HANDLER
, &handler
, sizeof(handler
));
618 *(uint32_t *)(info
+ ERR_FRAME_JIT_OFS_CODE_SIZE
) =
619 (uint32_t)(sz
- sizeof(err_frame_jit_template
) - (info
- (uint8_t *)base
));
620 __register_frame(info
+ ERR_FRAME_JIT_OFS_REGISTER
);
621 #ifdef LUA_USE_ASSERT
623 struct dwarf_eh_bases ehb
;
624 lj_assertX(_Unwind_Find_FDE(info
+ sizeof(err_frame_jit_template
)+1, &ehb
),
625 "bad JIT unwind table registration");
628 return info
+ sizeof(err_frame_jit_template
);
631 void lj_err_deregister_mcode(void *base
, size_t sz
, uint8_t *info
)
633 UNUSED(base
); UNUSED(sz
);
634 __deregister_frame(info
+ ERR_FRAME_JIT_OFS_REGISTER
);
638 #else /* LJ_TARGET_ARM */
640 #define _US_VIRTUAL_UNWIND_FRAME 0
641 #define _US_UNWIND_FRAME_STARTING 1
642 #define _US_ACTION_MASK 3
643 #define _US_FORCE_UNWIND 8
645 typedef struct _Unwind_Control_Block _Unwind_Control_Block
;
646 #define UNWIND_EXCEPTION_TYPE _Unwind_Control_Block
648 struct _Unwind_Control_Block
{
653 extern int _Unwind_RaiseException(_Unwind_Control_Block
*);
654 extern int __gnu_unwind_frame(_Unwind_Control_Block
*, _Unwind_Context
*);
655 extern int _Unwind_VRS_Set(_Unwind_Context
*, int, uint32_t, int, void *);
656 extern int _Unwind_VRS_Get(_Unwind_Context
*, int, uint32_t, int, void *);
658 static inline uint32_t _Unwind_GetGR(_Unwind_Context
*ctx
, int r
)
661 _Unwind_VRS_Get(ctx
, 0, r
, 0, &v
);
665 static inline void _Unwind_SetGR(_Unwind_Context
*ctx
, int r
, uint32_t v
)
667 _Unwind_VRS_Set(ctx
, 0, r
, 0, &v
);
670 extern void lj_vm_unwind_ext(void);
672 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
673 LJ_FUNCA
int lj_err_unwind_arm(int state
, _Unwind_Control_Block
*ucb
,
674 _Unwind_Context
*ctx
)
676 void *cf
= (void *)_Unwind_GetGR(ctx
, 13);
677 lua_State
*L
= cframe_L(cf
);
680 switch ((state
& _US_ACTION_MASK
)) {
681 case _US_VIRTUAL_UNWIND_FRAME
:
682 if ((state
& _US_FORCE_UNWIND
)) break;
683 return _URC_HANDLER_FOUND
;
684 case _US_UNWIND_FRAME_STARTING
:
685 if (LJ_UEXCLASS_CHECK(ucb
->exclass
)) {
686 errcode
= LJ_UEXCLASS_ERRCODE(ucb
->exclass
);
688 errcode
= LUA_ERRRUN
;
689 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRCPP
));
691 cf
= err_unwind(L
, cf
, errcode
);
692 if ((state
& _US_FORCE_UNWIND
) || cf
== NULL
) break;
693 _Unwind_SetGR(ctx
, 15, (uint32_t)lj_vm_unwind_ext
);
694 _Unwind_SetGR(ctx
, 0, (uint32_t)ucb
);
695 _Unwind_SetGR(ctx
, 1, (uint32_t)errcode
);
696 _Unwind_SetGR(ctx
, 2, cframe_unwind_ff(cf
) ?
697 (uint32_t)lj_vm_unwind_ff_eh
:
698 (uint32_t)lj_vm_unwind_c_eh
);
699 return _URC_INSTALL_CONTEXT
;
703 if (__gnu_unwind_frame(ucb
, ctx
) != _URC_OK
)
705 #ifdef LUA_USE_ASSERT
706 /* We should never get here unless this is a forced unwind aka backtrace. */
707 if (_Unwind_GetGR(ctx
, 0) == 0xff33aa77) {
708 _Unwind_SetGR(ctx
, 0, 0xff33aa88);
711 return _URC_CONTINUE_UNWIND
;
714 #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
715 typedef int (*_Unwind_Trace_Fn
)(_Unwind_Context
*, void *);
716 extern int _Unwind_Backtrace(_Unwind_Trace_Fn
, void *);
718 static int err_verify_bt(_Unwind_Context
*ctx
, int *got
)
720 if (_Unwind_GetGR(ctx
, 0) == 0xff33aa88) { *got
= 2; }
721 else if (*got
== 0) { *got
= 1; _Unwind_SetGR(ctx
, 0, 0xff33aa77); }
725 /* Verify that external error handling actually has a chance to work. */
726 void lj_err_verify(void)
729 _Unwind_Backtrace((_Unwind_Trace_Fn
)err_verify_bt
, &got
);
730 lj_assertX(got
== 2, "broken build: external frame unwinding enabled, but missing -funwind-tables");
735 ** Note: LJ_UNWIND_JIT is not implemented for 32 bit ARM.
737 ** The quirky ARM unwind API doesn't have __register_frame().
738 ** A potential workaround might involve _Unwind_Backtrace.
739 ** But most 32 bit ARM targets don't qualify for LJ_UNWIND_EXT, anyway,
740 ** since they are built without unwind tables by default.
743 #endif /* LJ_TARGET_ARM */
747 static __thread
struct {
748 UNWIND_EXCEPTION_TYPE ex
;
752 /* Raise external exception. */
753 static void err_raise_ext(global_State
*g
, int errcode
)
755 memset(&static_uex
, 0, sizeof(static_uex
));
756 static_uex
.ex
.exclass
= LJ_UEXCLASS_MAKE(errcode
);
758 _Unwind_RaiseException(&static_uex
.ex
);
765 /* -- Error handling ------------------------------------------------------ */
767 /* Throw error. Find catch frame, unwind stack and continue. */
768 LJ_NOINLINE
void LJ_FASTCALL
lj_err_throw(lua_State
*L
, int errcode
)
770 global_State
*g
= G(L
);
774 err_raise_ext(g
, errcode
);
776 ** A return from this function signals a corrupt C stack that cannot be
777 ** unwound. We have no choice but to call the panic function and exit.
779 ** Usually this is caused by a C function without unwind information.
780 ** This may happen if you've manually enabled LUAJIT_UNWIND_EXTERNAL
781 ** and forgot to recompile *every* non-C++ file with -funwind-tables.
787 setmref(g
->jit_base
, NULL
);
790 void *cf
= err_unwind(L
, NULL
, errcode
);
791 if (cframe_unwind_ff(cf
))
792 lj_vm_unwind_ff(cframe_raw(cf
));
794 lj_vm_unwind_c(cframe_raw(cf
), errcode
);
800 /* Return string object for error message. */
801 LJ_NOINLINE GCstr
*lj_err_str(lua_State
*L
, ErrMsg em
)
803 return lj_str_newz(L
, err2msg(em
));
806 /* Out-of-memory error. */
807 LJ_NOINLINE
void lj_err_mem(lua_State
*L
)
809 if (L
->status
== LUA_ERRERR
+1) /* Don't touch the stack during lua_open. */
810 lj_vm_unwind_c(L
->cframe
, LUA_ERRMEM
);
812 TValue
*base
= tvref(G(L
)->jit_base
);
813 if (base
) L
->base
= base
;
815 if (curr_funcisL(L
)) {
816 L
->top
= curr_topL(L
);
817 if (LJ_UNLIKELY(L
->top
> tvref(L
->maxstack
))) {
818 /* The current Lua frame violates the stack. Replace it with a dummy. */
820 setframe_gc(L
->base
- 1 - LJ_FR2
, obj2gco(L
), LJ_TTHREAD
);
823 setstrV(L
, L
->top
++, lj_err_str(L
, LJ_ERR_ERRMEM
));
824 lj_err_throw(L
, LUA_ERRMEM
);
827 /* Find error function for runtime errors. Requires an extra stack traversal. */
828 static ptrdiff_t finderrfunc(lua_State
*L
)
830 cTValue
*frame
= L
->base
-1, *bot
= tvref(L
->stack
)+LJ_FR2
;
831 void *cf
= L
->cframe
;
832 while (frame
> bot
&& cf
) {
833 while (cframe_nres(cframe_raw(cf
)) < 0) { /* cframe without frame? */
834 if (frame
>= restorestack(L
, -cframe_nres(cf
)))
836 if (cframe_errfunc(cf
) >= 0) /* Error handler not inherited (-1)? */
837 return cframe_errfunc(cf
);
838 cf
= cframe_prev(cf
); /* Else unwind cframe and continue searching. */
842 switch (frame_typep(frame
)) {
845 frame
= frame_prevl(frame
);
848 cf
= cframe_prev(cf
);
851 frame
= frame_prevd(frame
);
854 if (frame_iscont_fficb(frame
))
855 cf
= cframe_prev(cf
);
856 frame
= frame_prevd(frame
);
859 if (cframe_canyield(cf
)) return 0;
860 if (cframe_errfunc(cf
) >= 0)
861 return cframe_errfunc(cf
);
862 cf
= cframe_prev(cf
);
863 frame
= frame_prevd(frame
);
867 if (frame_func(frame_prevd(frame
))->c
.ffid
== FF_xpcall
)
868 return savestack(L
, frame_prevd(frame
)+1); /* xpcall's errorfunc. */
871 lj_assertL(0, "bad frame type");
879 LJ_NOINLINE
void LJ_FASTCALL
lj_err_run(lua_State
*L
)
881 ptrdiff_t ef
= (LJ_HASJIT
&& tvref(G(L
)->jit_base
)) ? 0 : finderrfunc(L
);
883 TValue
*errfunc
, *top
;
884 lj_state_checkstack(L
, LUA_MINSTACK
* 2); /* Might raise new error. */
885 lj_trace_abort(G(L
));
886 errfunc
= restorestack(L
, ef
);
888 if (!tvisfunc(errfunc
) || L
->status
== LUA_ERRERR
) {
889 setstrV(L
, top
-1, lj_err_str(L
, LJ_ERR_ERRERR
));
890 lj_err_throw(L
, LUA_ERRERR
);
892 L
->status
= LUA_ERRERR
;
893 copyTV(L
, top
+LJ_FR2
, top
-1);
894 copyTV(L
, top
-1, errfunc
);
895 if (LJ_FR2
) setnilV(top
++);
897 lj_vm_call(L
, top
, 1+1); /* Stack: |errfunc|msg| -> |msg| */
899 lj_err_throw(L
, LUA_ERRRUN
);
902 /* Stack overflow error. */
903 void LJ_FASTCALL
lj_err_stkov(lua_State
*L
)
905 lj_debug_addloc(L
, err2msg(LJ_ERR_STKOV
), L
->base
-1, NULL
);
910 /* Rethrow error after doing a trace exit. */
911 LJ_NOINLINE
void LJ_FASTCALL
lj_err_trace(lua_State
*L
, int errcode
)
913 if (errcode
== LUA_ERRRUN
)
916 lj_err_throw(L
, errcode
);
920 /* Formatted runtime error message. */
921 LJ_NORET LJ_NOINLINE
static void err_msgv(lua_State
*L
, ErrMsg em
, ...)
927 TValue
*base
= tvref(G(L
)->jit_base
);
928 if (base
) L
->base
= base
;
930 if (curr_funcisL(L
)) L
->top
= curr_topL(L
);
931 msg
= lj_strfmt_pushvf(L
, err2msg(em
), argp
);
933 lj_debug_addloc(L
, msg
, L
->base
-1, NULL
);
937 /* Non-vararg variant for better calling conventions. */
938 LJ_NOINLINE
void lj_err_msg(lua_State
*L
, ErrMsg em
)
944 LJ_NOINLINE
void lj_err_lex(lua_State
*L
, GCstr
*src
, const char *tok
,
945 BCLine line
, ErrMsg em
, va_list argp
)
947 char buff
[LUA_IDSIZE
];
949 lj_debug_shortname(buff
, src
, line
);
950 msg
= lj_strfmt_pushvf(L
, err2msg(em
), argp
);
951 msg
= lj_strfmt_pushf(L
, "%s:%d: %s", buff
, line
, msg
);
953 lj_strfmt_pushf(L
, err2msg(LJ_ERR_XNEAR
), msg
, tok
);
954 lj_err_throw(L
, LUA_ERRSYNTAX
);
957 /* Typecheck error for operands. */
958 LJ_NOINLINE
void lj_err_optype(lua_State
*L
, cTValue
*o
, ErrMsg opm
)
960 const char *tname
= lj_typename(o
);
961 const char *opname
= err2msg(opm
);
962 if (curr_funcisL(L
)) {
963 GCproto
*pt
= curr_proto(L
);
964 const BCIns
*pc
= cframe_Lpc(L
) - 1;
965 const char *oname
= NULL
;
966 const char *kind
= lj_debug_slotname(pt
, pc
, (BCReg
)(o
-L
->base
), &oname
);
968 err_msgv(L
, LJ_ERR_BADOPRT
, opname
, kind
, oname
, tname
);
970 err_msgv(L
, LJ_ERR_BADOPRV
, opname
, tname
);
973 /* Typecheck error for ordered comparisons. */
974 LJ_NOINLINE
void lj_err_comp(lua_State
*L
, cTValue
*o1
, cTValue
*o2
)
976 const char *t1
= lj_typename(o1
);
977 const char *t2
= lj_typename(o2
);
978 err_msgv(L
, t1
== t2
? LJ_ERR_BADCMPV
: LJ_ERR_BADCMPT
, t1
, t2
);
979 /* This assumes the two "boolean" entries are commoned by the C compiler. */
982 /* Typecheck error for __call. */
983 LJ_NOINLINE
void lj_err_optype_call(lua_State
*L
, TValue
*o
)
985 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
986 ** L->base still points to the caller. So add a dummy frame with L instead
987 ** of a function. See lua_getstack().
989 const BCIns
*pc
= cframe_Lpc(L
);
990 if (((ptrdiff_t)pc
& FRAME_TYPE
) != FRAME_LUA
) {
991 const char *tname
= lj_typename(o
);
992 setframe_gc(o
, obj2gco(L
), LJ_TTHREAD
);
995 L
->top
= L
->base
= o
+1;
996 err_msgv(L
, LJ_ERR_BADCALL
, tname
);
998 lj_err_optype(L
, o
, LJ_ERR_OPCALL
);
1001 /* Error in context of caller. */
1002 LJ_NOINLINE
void lj_err_callermsg(lua_State
*L
, const char *msg
)
1004 TValue
*frame
= NULL
, *pframe
= NULL
;
1005 if (!(LJ_HASJIT
&& tvref(G(L
)->jit_base
))) {
1007 if (frame_islua(frame
)) {
1008 pframe
= frame_prevl(frame
);
1009 } else if (frame_iscont(frame
)) {
1010 if (frame_iscont_fficb(frame
)) {
1014 pframe
= frame_prevd(frame
);
1016 /* Remove frame for FFI metamethods. */
1017 if (frame_func(frame
)->c
.ffid
>= FF_ffi_meta___index
&&
1018 frame_func(frame
)->c
.ffid
<= FF_ffi_meta___tostring
) {
1021 setcframe_pc(cframe_raw(L
->cframe
), frame_contpc(frame
));
1027 lj_debug_addloc(L
, msg
, pframe
, frame
);
1031 /* Formatted error in context of caller. */
1032 LJ_NOINLINE
void lj_err_callerv(lua_State
*L
, ErrMsg em
, ...)
1037 msg
= lj_strfmt_pushvf(L
, err2msg(em
), argp
);
1039 lj_err_callermsg(L
, msg
);
1042 /* Error in context of caller. */
1043 LJ_NOINLINE
void lj_err_caller(lua_State
*L
, ErrMsg em
)
1045 lj_err_callermsg(L
, err2msg(em
));
1048 /* Argument error message. */
1049 LJ_NORET LJ_NOINLINE
static void err_argmsg(lua_State
*L
, int narg
,
1052 const char *fname
= "?";
1053 const char *ftype
= lj_debug_funcname(L
, L
->base
- 1, &fname
);
1054 if (narg
< 0 && narg
> LUA_REGISTRYINDEX
)
1055 narg
= (int)(L
->top
- L
->base
) + narg
+ 1;
1056 if (ftype
&& ftype
[3] == 'h' && --narg
== 0) /* Check for "method". */
1057 msg
= lj_strfmt_pushf(L
, err2msg(LJ_ERR_BADSELF
), fname
, msg
);
1059 msg
= lj_strfmt_pushf(L
, err2msg(LJ_ERR_BADARG
), narg
, fname
, msg
);
1060 lj_err_callermsg(L
, msg
);
1063 /* Formatted argument error. */
1064 LJ_NOINLINE
void lj_err_argv(lua_State
*L
, int narg
, ErrMsg em
, ...)
1069 msg
= lj_strfmt_pushvf(L
, err2msg(em
), argp
);
1071 err_argmsg(L
, narg
, msg
);
1074 /* Argument error. */
1075 LJ_NOINLINE
void lj_err_arg(lua_State
*L
, int narg
, ErrMsg em
)
1077 err_argmsg(L
, narg
, err2msg(em
));
1080 /* Typecheck error for arguments. */
1081 LJ_NOINLINE
void lj_err_argtype(lua_State
*L
, int narg
, const char *xname
)
1083 const char *tname
, *msg
;
1084 if (narg
<= LUA_REGISTRYINDEX
) {
1085 if (narg
>= LUA_GLOBALSINDEX
) {
1086 tname
= lj_obj_itypename
[~LJ_TTAB
];
1088 GCfunc
*fn
= curr_func(L
);
1089 int idx
= LUA_GLOBALSINDEX
- narg
;
1090 if (idx
<= fn
->c
.nupvalues
)
1091 tname
= lj_typename(&fn
->c
.upvalue
[idx
-1]);
1093 tname
= lj_obj_typename
[0];
1096 TValue
*o
= narg
< 0 ? L
->top
+ narg
: L
->base
+ narg
-1;
1097 tname
= o
< L
->top
? lj_typename(o
) : lj_obj_typename
[0];
1099 msg
= lj_strfmt_pushf(L
, err2msg(LJ_ERR_BADTYPE
), xname
, tname
);
1100 err_argmsg(L
, narg
, msg
);
1103 /* Typecheck error for arguments. */
1104 LJ_NOINLINE
void lj_err_argt(lua_State
*L
, int narg
, int tt
)
1106 lj_err_argtype(L
, narg
, lj_obj_typename
[tt
+1]);
1109 /* -- Public error handling API ------------------------------------------- */
1111 LUA_API lua_CFunction
lua_atpanic(lua_State
*L
, lua_CFunction panicf
)
1113 lua_CFunction old
= G(L
)->panic
;
1114 G(L
)->panic
= panicf
;
1118 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
1119 LUA_API
int lua_error(lua_State
*L
)
1122 return 0; /* unreachable */
1125 LUALIB_API
int luaL_argerror(lua_State
*L
, int narg
, const char *msg
)
1127 err_argmsg(L
, narg
, msg
);
1128 return 0; /* unreachable */
1131 LUALIB_API
int luaL_typerror(lua_State
*L
, int narg
, const char *xname
)
1133 lj_err_argtype(L
, narg
, xname
);
1134 return 0; /* unreachable */
1137 LUALIB_API
void luaL_where(lua_State
*L
, int level
)
1140 cTValue
*frame
= lj_debug_frame(L
, level
, &size
);
1141 lj_debug_addloc(L
, "", frame
, size
? frame
+size
: NULL
);
1144 LUALIB_API
int luaL_error(lua_State
*L
, const char *fmt
, ...)
1148 va_start(argp
, fmt
);
1149 msg
= lj_strfmt_pushvf(L
, fmt
, argp
);
1151 lj_err_callermsg(L
, msg
);
1152 return 0; /* unreachable */