Properly handle OOM in trace_save().
[luajit-2.0.git] / src / lj_err.c
blob11b07b5624b2245d56b0a33faa2b376d923972d0
1 /*
2 ** Error handling.
3 ** Copyright (C) 2005-2015 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"
21 ** LuaJIT can either use internal or external frame unwinding:
23 ** - Internal frame unwinding (INT) is free-standing and doesn't require
24 ** any OS or library support.
26 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
28 ** Pros and Cons:
30 ** - EXT requires unwind tables for *all* functions on the C stack between
31 ** the pcall/catch and the error/throw. This is the default on x64,
32 ** but needs to be manually enabled on x86/PPC for non-C++ code.
34 ** - INT is faster when actually throwing errors (but this happens rarely).
35 ** Setting up error handlers is zero-cost in any case.
37 ** - EXT provides full interoperability with C++ exceptions. You can throw
38 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
39 ** C++ destructors are called as needed. C++ exceptions caught by pcall
40 ** are converted to the string "C++ exception". Lua errors can be caught
41 ** with catch (...) in C++.
43 ** - INT has only limited support for automatically catching C++ exceptions
44 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
45 ** the wrapper function feature. Lua errors thrown through C++ frames
46 ** cannot be caught by C++ code and C++ destructors are not run.
48 ** EXT is the default on x64 systems, INT is the default on all other systems.
50 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
51 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
52 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
53 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
54 ** and all C libraries that have callbacks which may be used to call back
55 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
57 ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
58 ** EXT is mandatory on WIN64 since the calling convention has an abundance
59 ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
60 ** The POSIX/x64 interpreter only saves r12/r13 for INT (e.g. PS4).
63 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND
64 #define LJ_UNWIND_EXT 1
65 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
66 #define LJ_UNWIND_EXT 1
67 #endif
69 /* -- Error messages ------------------------------------------------------ */
71 /* Error message strings. */
72 LJ_DATADEF const char *lj_err_allmsg =
73 #define ERRDEF(name, msg) msg "\0"
74 #include "lj_errmsg.h"
77 /* -- Internal frame unwinding -------------------------------------------- */
79 /* Unwind Lua stack and move error message to new top. */
80 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
82 lj_func_closeuv(L, top);
83 if (top < L->top-1) {
84 copyTV(L, top, L->top-1);
85 L->top = top+1;
87 lj_state_relimitstack(L);
90 /* Unwind until stop frame. Optionally cleanup frames. */
91 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
93 TValue *frame = L->base-1;
94 void *cf = L->cframe;
95 while (cf) {
96 int32_t nres = cframe_nres(cframe_raw(cf));
97 if (nres < 0) { /* C frame without Lua frame? */
98 TValue *top = restorestack(L, -nres);
99 if (frame < top) { /* Frame reached? */
100 if (errcode) {
101 L->cframe = cframe_prev(cf);
102 L->base = frame+1;
103 unwindstack(L, top);
105 return cf;
108 if (frame <= tvref(L->stack))
109 break;
110 switch (frame_typep(frame)) {
111 case FRAME_LUA: /* Lua frame. */
112 case FRAME_LUAP:
113 frame = frame_prevl(frame);
114 break;
115 case FRAME_C: /* C frame. */
116 #if LJ_HASFFI
117 unwind_c:
118 #endif
119 #if LJ_UNWIND_EXT
120 if (errcode) {
121 L->cframe = cframe_prev(cf);
122 L->base = frame_prevd(frame) + 1;
123 unwindstack(L, frame);
124 } else if (cf != stopcf) {
125 cf = cframe_prev(cf);
126 frame = frame_prevd(frame);
127 break;
129 return NULL; /* Continue unwinding. */
130 #else
131 UNUSED(stopcf);
132 cf = cframe_prev(cf);
133 frame = frame_prevd(frame);
134 break;
135 #endif
136 case FRAME_CP: /* Protected C frame. */
137 if (cframe_canyield(cf)) { /* Resume? */
138 if (errcode) {
139 hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */
140 L->cframe = NULL;
141 L->status = (uint8_t)errcode;
143 return cf;
145 if (errcode) {
146 L->cframe = cframe_prev(cf);
147 L->base = frame_prevd(frame) + 1;
148 unwindstack(L, frame);
150 return cf;
151 case FRAME_CONT: /* Continuation frame. */
152 #if LJ_HASFFI
153 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK)
154 goto unwind_c;
155 #endif
156 case FRAME_VARG: /* Vararg frame. */
157 frame = frame_prevd(frame);
158 break;
159 case FRAME_PCALL: /* FF pcall() frame. */
160 case FRAME_PCALLH: /* FF pcall() frame inside hook. */
161 if (errcode) {
162 if (errcode == LUA_YIELD) {
163 frame = frame_prevd(frame);
164 break;
166 if (frame_typep(frame) == FRAME_PCALL)
167 hook_leave(G(L));
168 L->cframe = cf;
169 L->base = frame_prevd(frame) + 1;
170 unwindstack(L, L->base);
172 return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
175 /* No C frame. */
176 if (errcode) {
177 L->cframe = NULL;
178 L->base = tvref(L->stack)+1;
179 unwindstack(L, L->base);
180 if (G(L)->panic)
181 G(L)->panic(L);
182 exit(EXIT_FAILURE);
184 return L; /* Anything non-NULL will do. */
187 /* -- External frame unwinding -------------------------------------------- */
189 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS
192 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
193 ** since various OS, distros and compilers mess up the header installation.
196 typedef struct _Unwind_Exception
198 uint64_t exclass;
199 void (*excleanup)(int, struct _Unwind_Exception *);
200 uintptr_t p1, p2;
201 } __attribute__((__aligned__)) _Unwind_Exception;
203 typedef struct _Unwind_Context _Unwind_Context;
205 #define _URC_OK 0
206 #define _URC_FATAL_PHASE1_ERROR 3
207 #define _URC_HANDLER_FOUND 6
208 #define _URC_INSTALL_CONTEXT 7
209 #define _URC_CONTINUE_UNWIND 8
210 #define _URC_FAILURE 9
212 #if !LJ_TARGET_ARM
214 extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
215 extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);
216 extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);
217 extern void _Unwind_DeleteException(_Unwind_Exception *);
218 extern int _Unwind_RaiseException(_Unwind_Exception *);
220 #define _UA_SEARCH_PHASE 1
221 #define _UA_CLEANUP_PHASE 2
222 #define _UA_HANDLER_FRAME 4
223 #define _UA_FORCE_UNWIND 8
225 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
226 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
227 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
228 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
230 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
231 LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
232 uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
234 void *cf;
235 lua_State *L;
236 if (version != 1)
237 return _URC_FATAL_PHASE1_ERROR;
238 UNUSED(uexclass);
239 cf = (void *)_Unwind_GetCFA(ctx);
240 L = cframe_L(cf);
241 if ((actions & _UA_SEARCH_PHASE)) {
242 #if LJ_UNWIND_EXT
243 if (err_unwind(L, cf, 0) == NULL)
244 return _URC_CONTINUE_UNWIND;
245 #endif
246 if (!LJ_UEXCLASS_CHECK(uexclass)) {
247 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
249 return _URC_HANDLER_FOUND;
251 if ((actions & _UA_CLEANUP_PHASE)) {
252 int errcode;
253 if (LJ_UEXCLASS_CHECK(uexclass)) {
254 errcode = LJ_UEXCLASS_ERRCODE(uexclass);
255 } else {
256 if ((actions & _UA_HANDLER_FRAME))
257 _Unwind_DeleteException(uex);
258 errcode = LUA_ERRRUN;
260 #if LJ_UNWIND_EXT
261 cf = err_unwind(L, cf, errcode);
262 if ((actions & _UA_FORCE_UNWIND)) {
263 return _URC_CONTINUE_UNWIND;
264 } else if (cf) {
265 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
266 _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ?
267 lj_vm_unwind_ff_eh :
268 lj_vm_unwind_c_eh));
269 return _URC_INSTALL_CONTEXT;
271 #if LJ_TARGET_X86ORX64
272 else if ((actions & _UA_HANDLER_FRAME)) {
273 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
274 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
276 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
277 _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
278 return _URC_INSTALL_CONTEXT;
280 #endif
281 #else
282 /* This is not the proper way to escape from the unwinder. We get away with
283 ** it on non-x64 because the interpreter restores all callee-saved regs.
285 lj_err_throw(L, errcode);
286 #endif
288 return _URC_CONTINUE_UNWIND;
291 #if LJ_UNWIND_EXT
292 #if LJ_TARGET_OSX || defined(__OpenBSD__)
293 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
294 static _Unwind_Exception static_uex;
295 #else
296 static __thread _Unwind_Exception static_uex;
297 #endif
299 /* Raise DWARF2 exception. */
300 static void err_raise_ext(int errcode)
302 static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);
303 static_uex.excleanup = NULL;
304 _Unwind_RaiseException(&static_uex);
306 #endif
308 #else
310 extern void _Unwind_DeleteException(void *);
311 extern int __gnu_unwind_frame (void *, _Unwind_Context *);
312 extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);
313 extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);
315 static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
317 uint32_t v;
318 _Unwind_VRS_Get(ctx, 0, r, 0, &v);
319 return v;
322 static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
324 _Unwind_VRS_Set(ctx, 0, r, 0, &v);
327 #define _US_VIRTUAL_UNWIND_FRAME 0
328 #define _US_UNWIND_FRAME_STARTING 1
329 #define _US_ACTION_MASK 3
330 #define _US_FORCE_UNWIND 8
332 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
333 LJ_FUNCA int lj_err_unwind_arm(int state, void *ucb, _Unwind_Context *ctx)
335 void *cf = (void *)_Unwind_GetGR(ctx, 13);
336 lua_State *L = cframe_L(cf);
337 if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) {
338 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
339 return _URC_HANDLER_FOUND;
341 if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) {
342 _Unwind_DeleteException(ucb);
343 _Unwind_SetGR(ctx, 15, (uint32_t)(void *)lj_err_throw);
344 _Unwind_SetGR(ctx, 0, (uint32_t)L);
345 _Unwind_SetGR(ctx, 1, (uint32_t)LUA_ERRRUN);
346 return _URC_INSTALL_CONTEXT;
348 if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
349 return _URC_FAILURE;
350 return _URC_CONTINUE_UNWIND;
353 #endif
355 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
358 ** Someone in Redmond owes me several days of my life. A lot of this is
359 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
360 ** from 3rd party docs or must be found by trial-and-error. They really
361 ** don't want you to write your own language-specific exception handler
362 ** or to interact gracefully with MSVC. :-(
364 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
365 ** unless you compile your C++ code with /EHa. Unfortunately this means
366 ** catch (...) also catches things like access violations. The use of
367 ** _set_se_translator doesn't really help, because it requires /EHa, too.
370 #define WIN32_LEAN_AND_MEAN
371 #include <windows.h>
373 /* Taken from: http://www.nynaeve.net/?p=99 */
374 typedef struct UndocumentedDispatcherContext {
375 ULONG64 ControlPc;
376 ULONG64 ImageBase;
377 PRUNTIME_FUNCTION FunctionEntry;
378 ULONG64 EstablisherFrame;
379 ULONG64 TargetIp;
380 PCONTEXT ContextRecord;
381 void (*LanguageHandler)(void);
382 PVOID HandlerData;
383 PUNWIND_HISTORY_TABLE HistoryTable;
384 ULONG ScopeIndex;
385 ULONG Fill0;
386 } UndocumentedDispatcherContext;
388 /* Another wild guess. */
389 extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
391 #ifdef MINGW_SDK_INIT
392 /* Workaround for broken MinGW64 declaration. */
393 VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");
394 #define RtlUnwindEx RtlUnwindEx_FIXED
395 #endif
397 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
398 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
400 #define LJ_EXCODE ((DWORD)0xe24c4a00)
401 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
402 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
403 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
405 /* Win64 exception handler for interpreter frame. */
406 LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec,
407 void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
409 lua_State *L = cframe_L(cf);
410 int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
411 LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
412 if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
413 /* Unwind internal frames. */
414 err_unwind(L, cf, errcode);
415 } else {
416 void *cf2 = err_unwind(L, cf, 0);
417 if (cf2) { /* We catch it, so start unwinding the upper frames. */
418 if (rec->ExceptionCode == LJ_MSVC_EXCODE ||
419 rec->ExceptionCode == LJ_GCC_EXCODE) {
420 __DestructExceptionObject(rec, 1);
421 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
422 } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
423 /* Don't catch access violations etc. */
424 return ExceptionContinueSearch;
426 /* Unwind the stack and call all handlers for all lower C frames
427 ** (including ourselves) again with EH_UNWINDING set. Then set
428 ** rsp = cf, rax = errcode and jump to the specified target.
430 RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
431 lj_vm_unwind_ff_eh :
432 lj_vm_unwind_c_eh),
433 rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);
434 /* RtlUnwindEx should never return. */
437 return ExceptionContinueSearch;
440 /* Raise Windows exception. */
441 static void err_raise_ext(int errcode)
443 RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
446 #endif
448 /* -- Error handling ------------------------------------------------------ */
450 /* Throw error. Find catch frame, unwind stack and continue. */
451 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
453 global_State *g = G(L);
454 lj_trace_abort(g);
455 setgcrefnull(g->jit_L);
456 L->status = 0;
457 #if LJ_UNWIND_EXT
458 err_raise_ext(errcode);
460 ** A return from this function signals a corrupt C stack that cannot be
461 ** unwound. We have no choice but to call the panic function and exit.
463 ** Usually this is caused by a C function without unwind information.
464 ** This should never happen on x64, but may happen if you've manually
465 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
466 ** non-C++ file with -funwind-tables.
468 if (G(L)->panic)
469 G(L)->panic(L);
470 #else
472 void *cf = err_unwind(L, NULL, errcode);
473 if (cframe_unwind_ff(cf))
474 lj_vm_unwind_ff(cframe_raw(cf));
475 else
476 lj_vm_unwind_c(cframe_raw(cf), errcode);
478 #endif
479 exit(EXIT_FAILURE);
482 /* Return string object for error message. */
483 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
485 return lj_str_newz(L, err2msg(em));
488 /* Out-of-memory error. */
489 LJ_NOINLINE void lj_err_mem(lua_State *L)
491 if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */
492 lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
493 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
494 lj_err_throw(L, LUA_ERRMEM);
497 /* Find error function for runtime errors. Requires an extra stack traversal. */
498 static ptrdiff_t finderrfunc(lua_State *L)
500 cTValue *frame = L->base-1, *bot = tvref(L->stack);
501 void *cf = L->cframe;
502 while (frame > bot && cf) {
503 while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */
504 if (frame >= restorestack(L, -cframe_nres(cf)))
505 break;
506 if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */
507 return cframe_errfunc(cf);
508 cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */
509 if (cf == NULL)
510 return 0;
512 switch (frame_typep(frame)) {
513 case FRAME_LUA:
514 case FRAME_LUAP:
515 frame = frame_prevl(frame);
516 break;
517 case FRAME_C:
518 cf = cframe_prev(cf);
519 /* fallthrough */
520 case FRAME_VARG:
521 frame = frame_prevd(frame);
522 break;
523 case FRAME_CONT:
524 #if LJ_HASFFI
525 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK)
526 cf = cframe_prev(cf);
527 #endif
528 frame = frame_prevd(frame);
529 break;
530 case FRAME_CP:
531 if (cframe_canyield(cf)) return 0;
532 if (cframe_errfunc(cf) >= 0)
533 return cframe_errfunc(cf);
534 frame = frame_prevd(frame);
535 break;
536 case FRAME_PCALL:
537 case FRAME_PCALLH:
538 if (frame_ftsz(frame) >= (ptrdiff_t)(2*sizeof(TValue))) /* xpcall? */
539 return savestack(L, frame-1); /* Point to xpcall's errorfunc. */
540 return 0;
541 default:
542 lua_assert(0);
543 return 0;
546 return 0;
549 /* Runtime error. */
550 LJ_NOINLINE void lj_err_run(lua_State *L)
552 ptrdiff_t ef = finderrfunc(L);
553 if (ef) {
554 TValue *errfunc = restorestack(L, ef);
555 TValue *top = L->top;
556 lj_trace_abort(G(L));
557 if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
558 setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
559 lj_err_throw(L, LUA_ERRERR);
561 L->status = LUA_ERRERR;
562 copyTV(L, top, top-1);
563 copyTV(L, top-1, errfunc);
564 L->top = top+1;
565 lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */
567 lj_err_throw(L, LUA_ERRRUN);
570 /* Formatted runtime error message. */
571 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
573 const char *msg;
574 va_list argp;
575 va_start(argp, em);
576 if (curr_funcisL(L)) L->top = curr_topL(L);
577 msg = lj_str_pushvf(L, err2msg(em), argp);
578 va_end(argp);
579 lj_debug_addloc(L, msg, L->base-1, NULL);
580 lj_err_run(L);
583 /* Non-vararg variant for better calling conventions. */
584 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
586 err_msgv(L, em);
589 /* Lexer error. */
590 LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
591 BCLine line, ErrMsg em, va_list argp)
593 char buff[LUA_IDSIZE];
594 const char *msg;
595 lj_debug_shortname(buff, src);
596 msg = lj_str_pushvf(L, err2msg(em), argp);
597 msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
598 if (tok)
599 lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
600 lj_err_throw(L, LUA_ERRSYNTAX);
603 /* Typecheck error for operands. */
604 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
606 const char *tname = lj_typename(o);
607 const char *opname = err2msg(opm);
608 if (curr_funcisL(L)) {
609 GCproto *pt = curr_proto(L);
610 const BCIns *pc = cframe_Lpc(L) - 1;
611 const char *oname = NULL;
612 const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
613 if (kind)
614 err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
616 err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
619 /* Typecheck error for ordered comparisons. */
620 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
622 const char *t1 = lj_typename(o1);
623 const char *t2 = lj_typename(o2);
624 err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
625 /* This assumes the two "boolean" entries are commoned by the C compiler. */
628 /* Typecheck error for __call. */
629 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
631 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
632 ** L->base still points to the caller. So add a dummy frame with L instead
633 ** of a function. See lua_getstack().
635 const BCIns *pc = cframe_Lpc(L);
636 if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
637 const char *tname = lj_typename(o);
638 setframe_pc(o, pc);
639 setframe_gc(o, obj2gco(L));
640 L->top = L->base = o+1;
641 err_msgv(L, LJ_ERR_BADCALL, tname);
643 lj_err_optype(L, o, LJ_ERR_OPCALL);
646 /* Error in context of caller. */
647 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
649 TValue *frame = L->base-1;
650 TValue *pframe = NULL;
651 if (frame_islua(frame)) {
652 pframe = frame_prevl(frame);
653 } else if (frame_iscont(frame)) {
654 #if LJ_HASFFI
655 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) {
656 pframe = frame;
657 frame = NULL;
658 } else
659 #endif
661 pframe = frame_prevd(frame);
662 #if LJ_HASFFI
663 /* Remove frame for FFI metamethods. */
664 if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
665 frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
666 L->base = pframe+1;
667 L->top = frame;
668 setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));
670 #endif
673 lj_debug_addloc(L, msg, pframe, frame);
674 lj_err_run(L);
677 /* Formatted error in context of caller. */
678 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
680 const char *msg;
681 va_list argp;
682 va_start(argp, em);
683 msg = lj_str_pushvf(L, err2msg(em), argp);
684 va_end(argp);
685 lj_err_callermsg(L, msg);
688 /* Error in context of caller. */
689 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
691 lj_err_callermsg(L, err2msg(em));
694 /* Argument error message. */
695 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
696 const char *msg)
698 const char *fname = "?";
699 const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
700 if (narg < 0 && narg > LUA_REGISTRYINDEX)
701 narg = (int)(L->top - L->base) + narg + 1;
702 if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
703 msg = lj_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
704 else
705 msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
706 lj_err_callermsg(L, msg);
709 /* Formatted argument error. */
710 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
712 const char *msg;
713 va_list argp;
714 va_start(argp, em);
715 msg = lj_str_pushvf(L, err2msg(em), argp);
716 va_end(argp);
717 err_argmsg(L, narg, msg);
720 /* Argument error. */
721 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
723 err_argmsg(L, narg, err2msg(em));
726 /* Typecheck error for arguments. */
727 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
729 const char *tname, *msg;
730 if (narg <= LUA_REGISTRYINDEX) {
731 if (narg >= LUA_GLOBALSINDEX) {
732 tname = lj_obj_itypename[~LJ_TTAB];
733 } else {
734 GCfunc *fn = curr_func(L);
735 int idx = LUA_GLOBALSINDEX - narg;
736 if (idx <= fn->c.nupvalues)
737 tname = lj_typename(&fn->c.upvalue[idx-1]);
738 else
739 tname = lj_obj_typename[0];
741 } else {
742 TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
743 tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
745 msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
746 err_argmsg(L, narg, msg);
749 /* Typecheck error for arguments. */
750 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
752 lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
755 /* -- Public error handling API ------------------------------------------- */
757 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
759 lua_CFunction old = G(L)->panic;
760 G(L)->panic = panicf;
761 return old;
764 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
765 LUA_API int lua_error(lua_State *L)
767 lj_err_run(L);
768 return 0; /* unreachable */
771 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
773 err_argmsg(L, narg, msg);
774 return 0; /* unreachable */
777 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
779 lj_err_argtype(L, narg, xname);
780 return 0; /* unreachable */
783 LUALIB_API void luaL_where(lua_State *L, int level)
785 int size;
786 cTValue *frame = lj_debug_frame(L, level, &size);
787 lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
790 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
792 const char *msg;
793 va_list argp;
794 va_start(argp, fmt);
795 msg = lj_str_pushvf(L, fmt, argp);
796 va_end(argp);
797 lj_err_callermsg(L, msg);
798 return 0; /* unreachable */