Merge branch 'master' into v2.1
[luajit-2.0.git] / src / lj_err.c
blobd37df310eada0fc2306044539baddacf2a51ea0d
1 /*
2 ** Error handling.
3 ** Copyright (C) 2005-2014 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. This is the default on x64,
33 ** but needs to be manually enabled on x86/PPC for non-C++ code.
35 ** - INT is faster when actually throwing errors (but this happens rarely).
36 ** Setting up error handlers is zero-cost in any case.
38 ** - EXT provides full interoperability with C++ exceptions. You can throw
39 ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
40 ** C++ destructors are called as needed. C++ exceptions caught by pcall
41 ** are converted to the string "C++ exception". Lua errors can be caught
42 ** with catch (...) in C++.
44 ** - INT has only limited support for automatically catching C++ exceptions
45 ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
46 ** the wrapper function feature. Lua errors thrown through C++ frames
47 ** cannot be caught by C++ code and C++ destructors are not run.
49 ** EXT is the default on x64 systems, INT is the default on all other systems.
51 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
52 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
53 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
54 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
55 ** and all C libraries that have callbacks which may be used to call back
56 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
58 ** EXT cannot be enabled on WIN32 since system exceptions use code-driven SEH.
59 ** EXT is mandatory on WIN64 since the calling convention has an abundance
60 ** of callee-saved registers (rbx, rbp, rsi, rdi, r12-r15, xmm6-xmm15).
61 ** EXT is mandatory on POSIX/x64 since the interpreter doesn't save r12/r13.
64 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL))
65 #define LJ_UNWIND_EXT 1
66 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
67 #define LJ_UNWIND_EXT 1
68 #endif
70 /* -- Error messages ------------------------------------------------------ */
72 /* Error message strings. */
73 LJ_DATADEF const char *lj_err_allmsg =
74 #define ERRDEF(name, msg) msg "\0"
75 #include "lj_errmsg.h"
78 /* -- Internal frame unwinding -------------------------------------------- */
80 /* Unwind Lua stack and move error message to new top. */
81 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
83 lj_func_closeuv(L, top);
84 if (top < L->top-1) {
85 copyTV(L, top, L->top-1);
86 L->top = top+1;
88 lj_state_relimitstack(L);
91 /* Unwind until stop frame. Optionally cleanup frames. */
92 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
94 TValue *frame = L->base-1;
95 void *cf = L->cframe;
96 while (cf) {
97 int32_t nres = cframe_nres(cframe_raw(cf));
98 if (nres < 0) { /* C frame without Lua frame? */
99 TValue *top = restorestack(L, -nres);
100 if (frame < top) { /* Frame reached? */
101 if (errcode) {
102 L->base = frame+1;
103 L->cframe = cframe_prev(cf);
104 unwindstack(L, top);
106 return cf;
109 if (frame <= tvref(L->stack))
110 break;
111 switch (frame_typep(frame)) {
112 case FRAME_LUA: /* Lua frame. */
113 case FRAME_LUAP:
114 frame = frame_prevl(frame);
115 break;
116 case FRAME_C: /* C frame. */
117 #if LJ_HASFFI
118 unwind_c:
119 #endif
120 #if LJ_UNWIND_EXT
121 if (errcode) {
122 L->base = frame_prevd(frame) + 1;
123 L->cframe = cframe_prev(cf);
124 unwindstack(L, frame);
125 } else if (cf != stopcf) {
126 cf = cframe_prev(cf);
127 frame = frame_prevd(frame);
128 break;
130 return NULL; /* Continue unwinding. */
131 #else
132 UNUSED(stopcf);
133 cf = cframe_prev(cf);
134 frame = frame_prevd(frame);
135 break;
136 #endif
137 case FRAME_CP: /* Protected C frame. */
138 if (cframe_canyield(cf)) { /* Resume? */
139 if (errcode) {
140 hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */
141 L->cframe = NULL;
142 L->status = (uint8_t)errcode;
144 return cf;
146 if (errcode) {
147 L->base = frame_prevd(frame) + 1;
148 L->cframe = cframe_prev(cf);
149 unwindstack(L, frame);
151 return cf;
152 case FRAME_CONT: /* Continuation frame. */
153 #if LJ_HASFFI
154 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK)
155 goto unwind_c;
156 #endif
157 case FRAME_VARG: /* Vararg frame. */
158 frame = frame_prevd(frame);
159 break;
160 case FRAME_PCALL: /* FF pcall() frame. */
161 case FRAME_PCALLH: /* FF pcall() frame inside hook. */
162 if (errcode) {
163 if (errcode == LUA_YIELD) {
164 frame = frame_prevd(frame);
165 break;
167 if (frame_typep(frame) == FRAME_PCALL)
168 hook_leave(G(L));
169 L->base = frame_prevd(frame) + 1;
170 L->cframe = cf;
171 unwindstack(L, L->base);
173 return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
176 /* No C frame. */
177 if (errcode) {
178 L->base = tvref(L->stack)+1;
179 L->cframe = NULL;
180 unwindstack(L, L->base);
181 if (G(L)->panic)
182 G(L)->panic(L);
183 exit(EXIT_FAILURE);
185 return L; /* Anything non-NULL will do. */
188 /* -- External frame unwinding -------------------------------------------- */
190 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_TARGET_WINDOWS
193 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
194 ** since various OS, distros and compilers mess up the header installation.
197 typedef struct _Unwind_Exception
199 uint64_t exclass;
200 void (*excleanup)(int, struct _Unwind_Exception *);
201 uintptr_t p1, p2;
202 } __attribute__((__aligned__)) _Unwind_Exception;
204 typedef struct _Unwind_Context _Unwind_Context;
206 #define _URC_OK 0
207 #define _URC_FATAL_PHASE1_ERROR 3
208 #define _URC_HANDLER_FOUND 6
209 #define _URC_INSTALL_CONTEXT 7
210 #define _URC_CONTINUE_UNWIND 8
211 #define _URC_FAILURE 9
213 #if !LJ_TARGET_ARM
215 extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
216 extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);
217 extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);
218 extern void _Unwind_DeleteException(_Unwind_Exception *);
219 extern int _Unwind_RaiseException(_Unwind_Exception *);
221 #define _UA_SEARCH_PHASE 1
222 #define _UA_CLEANUP_PHASE 2
223 #define _UA_HANDLER_FRAME 4
224 #define _UA_FORCE_UNWIND 8
226 #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
227 #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
228 #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
229 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
231 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
232 LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
233 uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
235 void *cf;
236 lua_State *L;
237 if (version != 1)
238 return _URC_FATAL_PHASE1_ERROR;
239 UNUSED(uexclass);
240 cf = (void *)_Unwind_GetCFA(ctx);
241 L = cframe_L(cf);
242 if ((actions & _UA_SEARCH_PHASE)) {
243 #if LJ_UNWIND_EXT
244 if (err_unwind(L, cf, 0) == NULL)
245 return _URC_CONTINUE_UNWIND;
246 #endif
247 if (!LJ_UEXCLASS_CHECK(uexclass)) {
248 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
250 return _URC_HANDLER_FOUND;
252 if ((actions & _UA_CLEANUP_PHASE)) {
253 int errcode;
254 if (LJ_UEXCLASS_CHECK(uexclass)) {
255 errcode = LJ_UEXCLASS_ERRCODE(uexclass);
256 } else {
257 if ((actions & _UA_HANDLER_FRAME))
258 _Unwind_DeleteException(uex);
259 errcode = LUA_ERRRUN;
261 #if LJ_UNWIND_EXT
262 cf = err_unwind(L, cf, errcode);
263 if ((actions & _UA_FORCE_UNWIND)) {
264 return _URC_CONTINUE_UNWIND;
265 } else if (cf) {
266 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
267 _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ?
268 lj_vm_unwind_ff_eh :
269 lj_vm_unwind_c_eh));
270 return _URC_INSTALL_CONTEXT;
272 #if LJ_TARGET_X86ORX64
273 else if ((actions & _UA_HANDLER_FRAME)) {
274 /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
275 ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
277 _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
278 _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
279 return _URC_INSTALL_CONTEXT;
281 #endif
282 #else
283 /* This is not the proper way to escape from the unwinder. We get away with
284 ** it on non-x64 because the interpreter restores all callee-saved regs.
286 lj_err_throw(L, errcode);
287 #endif
289 return _URC_CONTINUE_UNWIND;
292 #if LJ_UNWIND_EXT
293 #if LJ_TARGET_OSX || defined(__OpenBSD__)
294 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
295 static _Unwind_Exception static_uex;
296 #else
297 static __thread _Unwind_Exception static_uex;
298 #endif
300 /* Raise DWARF2 exception. */
301 static void err_raise_ext(int errcode)
303 static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);
304 static_uex.excleanup = NULL;
305 _Unwind_RaiseException(&static_uex);
307 #endif
309 #else
311 extern void _Unwind_DeleteException(void *);
312 extern int __gnu_unwind_frame (void *, _Unwind_Context *);
313 extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);
314 extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);
316 static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
318 uint32_t v;
319 _Unwind_VRS_Get(ctx, 0, r, 0, &v);
320 return v;
323 static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
325 _Unwind_VRS_Set(ctx, 0, r, 0, &v);
328 #define _US_VIRTUAL_UNWIND_FRAME 0
329 #define _US_UNWIND_FRAME_STARTING 1
330 #define _US_ACTION_MASK 3
331 #define _US_FORCE_UNWIND 8
333 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
334 LJ_FUNCA int lj_err_unwind_arm(int state, void *ucb, _Unwind_Context *ctx)
336 void *cf = (void *)_Unwind_GetGR(ctx, 13);
337 lua_State *L = cframe_L(cf);
338 if ((state & _US_ACTION_MASK) == _US_VIRTUAL_UNWIND_FRAME) {
339 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
340 return _URC_HANDLER_FOUND;
342 if ((state&(_US_ACTION_MASK|_US_FORCE_UNWIND)) == _US_UNWIND_FRAME_STARTING) {
343 _Unwind_DeleteException(ucb);
344 _Unwind_SetGR(ctx, 15, (uint32_t)(void *)lj_err_throw);
345 _Unwind_SetGR(ctx, 0, (uint32_t)L);
346 _Unwind_SetGR(ctx, 1, (uint32_t)LUA_ERRRUN);
347 return _URC_INSTALL_CONTEXT;
349 if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
350 return _URC_FAILURE;
351 return _URC_CONTINUE_UNWIND;
354 #endif
356 #elif LJ_TARGET_X64 && LJ_TARGET_WINDOWS
359 ** Someone in Redmond owes me several days of my life. A lot of this is
360 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
361 ** from 3rd party docs or must be found by trial-and-error. They really
362 ** don't want you to write your own language-specific exception handler
363 ** or to interact gracefully with MSVC. :-(
365 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
366 ** unless you compile your C++ code with /EHa. Unfortunately this means
367 ** catch (...) also catches things like access violations. The use of
368 ** _set_se_translator doesn't really help, because it requires /EHa, too.
371 #define WIN32_LEAN_AND_MEAN
372 #include <windows.h>
374 /* Taken from: http://www.nynaeve.net/?p=99 */
375 typedef struct UndocumentedDispatcherContext {
376 ULONG64 ControlPc;
377 ULONG64 ImageBase;
378 PRUNTIME_FUNCTION FunctionEntry;
379 ULONG64 EstablisherFrame;
380 ULONG64 TargetIp;
381 PCONTEXT ContextRecord;
382 void (*LanguageHandler)(void);
383 PVOID HandlerData;
384 PUNWIND_HISTORY_TABLE HistoryTable;
385 ULONG ScopeIndex;
386 ULONG Fill0;
387 } UndocumentedDispatcherContext;
389 /* Another wild guess. */
390 extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
392 #ifdef MINGW_SDK_INIT
393 /* Workaround for broken MinGW64 declaration. */
394 VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");
395 #define RtlUnwindEx RtlUnwindEx_FIXED
396 #endif
398 #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
399 #define LJ_GCC_EXCODE ((DWORD)0x20474343)
401 #define LJ_EXCODE ((DWORD)0xe24c4a00)
402 #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
403 #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
404 #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
406 /* Win64 exception handler for interpreter frame. */
407 LJ_FUNCA EXCEPTION_DISPOSITION lj_err_unwind_win64(EXCEPTION_RECORD *rec,
408 void *cf, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
410 lua_State *L = cframe_L(cf);
411 int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
412 LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
413 if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
414 /* Unwind internal frames. */
415 err_unwind(L, cf, errcode);
416 } else {
417 void *cf2 = err_unwind(L, cf, 0);
418 if (cf2) { /* We catch it, so start unwinding the upper frames. */
419 if (rec->ExceptionCode == LJ_MSVC_EXCODE ||
420 rec->ExceptionCode == LJ_GCC_EXCODE) {
421 __DestructExceptionObject(rec, 1);
422 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
423 } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
424 /* Don't catch access violations etc. */
425 return ExceptionContinueSearch;
427 /* Unwind the stack and call all handlers for all lower C frames
428 ** (including ourselves) again with EH_UNWINDING set. Then set
429 ** rsp = cf, rax = errcode and jump to the specified target.
431 RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
432 lj_vm_unwind_ff_eh :
433 lj_vm_unwind_c_eh),
434 rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);
435 /* RtlUnwindEx should never return. */
438 return ExceptionContinueSearch;
441 /* Raise Windows exception. */
442 static void err_raise_ext(int errcode)
444 RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
447 #endif
449 /* -- Error handling ------------------------------------------------------ */
451 /* Throw error. Find catch frame, unwind stack and continue. */
452 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
454 global_State *g = G(L);
455 lj_trace_abort(g);
456 setmref(g->jit_base, NULL);
457 L->status = 0;
458 #if LJ_UNWIND_EXT
459 err_raise_ext(errcode);
461 ** A return from this function signals a corrupt C stack that cannot be
462 ** unwound. We have no choice but to call the panic function and exit.
464 ** Usually this is caused by a C function without unwind information.
465 ** This should never happen on x64, but may happen if you've manually
466 ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
467 ** non-C++ file with -funwind-tables.
469 if (G(L)->panic)
470 G(L)->panic(L);
471 #else
473 void *cf = err_unwind(L, NULL, errcode);
474 if (cframe_unwind_ff(cf))
475 lj_vm_unwind_ff(cframe_raw(cf));
476 else
477 lj_vm_unwind_c(cframe_raw(cf), errcode);
479 #endif
480 exit(EXIT_FAILURE);
483 /* Return string object for error message. */
484 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
486 return lj_str_newz(L, err2msg(em));
489 /* Out-of-memory error. */
490 LJ_NOINLINE void lj_err_mem(lua_State *L)
492 if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */
493 lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
494 setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
495 lj_err_throw(L, LUA_ERRMEM);
498 /* Find error function for runtime errors. Requires an extra stack traversal. */
499 static ptrdiff_t finderrfunc(lua_State *L)
501 cTValue *frame = L->base-1, *bot = tvref(L->stack);
502 void *cf = L->cframe;
503 while (frame > bot) {
504 lua_assert(cf != NULL);
505 while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */
506 if (frame >= restorestack(L, -cframe_nres(cf)))
507 break;
508 if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */
509 return cframe_errfunc(cf);
510 cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */
511 if (cf == NULL)
512 return 0;
514 switch (frame_typep(frame)) {
515 case FRAME_LUA:
516 case FRAME_LUAP:
517 frame = frame_prevl(frame);
518 break;
519 case FRAME_C:
520 cf = cframe_prev(cf);
521 /* fallthrough */
522 case FRAME_VARG:
523 frame = frame_prevd(frame);
524 break;
525 case FRAME_CONT:
526 #if LJ_HASFFI
527 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK)
528 cf = cframe_prev(cf);
529 #endif
530 frame = frame_prevd(frame);
531 break;
532 case FRAME_CP:
533 if (cframe_canyield(cf)) return 0;
534 if (cframe_errfunc(cf) >= 0)
535 return cframe_errfunc(cf);
536 frame = frame_prevd(frame);
537 break;
538 case FRAME_PCALL:
539 case FRAME_PCALLH:
540 if (frame_ftsz(frame) >= (ptrdiff_t)(2*sizeof(TValue))) /* xpcall? */
541 return savestack(L, frame-1); /* Point to xpcall's errorfunc. */
542 return 0;
543 default:
544 lua_assert(0);
545 return 0;
548 return 0;
551 /* Runtime error. */
552 LJ_NOINLINE void lj_err_run(lua_State *L)
554 ptrdiff_t ef = finderrfunc(L);
555 if (ef) {
556 TValue *errfunc = restorestack(L, ef);
557 TValue *top = L->top;
558 lj_trace_abort(G(L));
559 if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
560 setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
561 lj_err_throw(L, LUA_ERRERR);
563 L->status = LUA_ERRERR;
564 copyTV(L, top, top-1);
565 copyTV(L, top-1, errfunc);
566 L->top = top+1;
567 lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */
569 lj_err_throw(L, LUA_ERRRUN);
572 /* Formatted runtime error message. */
573 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
575 const char *msg;
576 va_list argp;
577 va_start(argp, em);
578 if (curr_funcisL(L)) L->top = curr_topL(L);
579 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
580 va_end(argp);
581 lj_debug_addloc(L, msg, L->base-1, NULL);
582 lj_err_run(L);
585 /* Non-vararg variant for better calling conventions. */
586 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
588 err_msgv(L, em);
591 /* Lexer error. */
592 LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
593 BCLine line, ErrMsg em, va_list argp)
595 char buff[LUA_IDSIZE];
596 const char *msg;
597 lj_debug_shortname(buff, src, line);
598 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
599 msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);
600 if (tok)
601 lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
602 lj_err_throw(L, LUA_ERRSYNTAX);
605 /* Typecheck error for operands. */
606 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
608 const char *tname = lj_typename(o);
609 const char *opname = err2msg(opm);
610 if (curr_funcisL(L)) {
611 GCproto *pt = curr_proto(L);
612 const BCIns *pc = cframe_Lpc(L) - 1;
613 const char *oname = NULL;
614 const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
615 if (kind)
616 err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
618 err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
621 /* Typecheck error for ordered comparisons. */
622 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
624 const char *t1 = lj_typename(o1);
625 const char *t2 = lj_typename(o2);
626 err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
627 /* This assumes the two "boolean" entries are commoned by the C compiler. */
630 /* Typecheck error for __call. */
631 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
633 /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
634 ** L->base still points to the caller. So add a dummy frame with L instead
635 ** of a function. See lua_getstack().
637 const BCIns *pc = cframe_Lpc(L);
638 if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
639 const char *tname = lj_typename(o);
640 setframe_pc(o, pc);
641 setframe_gc(o, obj2gco(L));
642 L->top = L->base = o+1;
643 err_msgv(L, LJ_ERR_BADCALL, tname);
645 lj_err_optype(L, o, LJ_ERR_OPCALL);
648 /* Error in context of caller. */
649 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
651 TValue *frame = L->base-1;
652 TValue *pframe = NULL;
653 if (frame_islua(frame)) {
654 pframe = frame_prevl(frame);
655 } else if (frame_iscont(frame)) {
656 #if LJ_HASFFI
657 if ((frame-1)->u32.lo == LJ_CONT_FFI_CALLBACK) {
658 pframe = frame;
659 frame = NULL;
660 } else
661 #endif
663 pframe = frame_prevd(frame);
664 #if LJ_HASFFI
665 /* Remove frame for FFI metamethods. */
666 if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
667 frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
668 L->base = pframe+1;
669 L->top = frame;
670 setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));
672 #endif
675 lj_debug_addloc(L, msg, pframe, frame);
676 lj_err_run(L);
679 /* Formatted error in context of caller. */
680 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
682 const char *msg;
683 va_list argp;
684 va_start(argp, em);
685 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
686 va_end(argp);
687 lj_err_callermsg(L, msg);
690 /* Error in context of caller. */
691 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
693 lj_err_callermsg(L, err2msg(em));
696 /* Argument error message. */
697 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
698 const char *msg)
700 const char *fname = "?";
701 const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
702 if (narg < 0 && narg > LUA_REGISTRYINDEX)
703 narg = (int)(L->top - L->base) + narg + 1;
704 if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
705 msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
706 else
707 msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
708 lj_err_callermsg(L, msg);
711 /* Formatted argument error. */
712 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
714 const char *msg;
715 va_list argp;
716 va_start(argp, em);
717 msg = lj_strfmt_pushvf(L, err2msg(em), argp);
718 va_end(argp);
719 err_argmsg(L, narg, msg);
722 /* Argument error. */
723 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
725 err_argmsg(L, narg, err2msg(em));
728 /* Typecheck error for arguments. */
729 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
731 TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
732 const char *tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
733 const char *msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
734 err_argmsg(L, narg, msg);
737 /* Typecheck error for arguments. */
738 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
740 lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
743 /* -- Public error handling API ------------------------------------------- */
745 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
747 lua_CFunction old = G(L)->panic;
748 G(L)->panic = panicf;
749 return old;
752 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
753 LUA_API int lua_error(lua_State *L)
755 lj_err_run(L);
756 return 0; /* unreachable */
759 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
761 err_argmsg(L, narg, msg);
762 return 0; /* unreachable */
765 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
767 lj_err_argtype(L, narg, xname);
768 return 0; /* unreachable */
771 LUALIB_API void luaL_where(lua_State *L, int level)
773 int size;
774 cTValue *frame = lj_debug_frame(L, level, &size);
775 lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
778 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
780 const char *msg;
781 va_list argp;
782 va_start(argp, fmt);
783 msg = lj_strfmt_pushvf(L, fmt, argp);
784 va_end(argp);
785 lj_err_callermsg(L, msg);
786 return 0; /* unreachable */