root/lj_err.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. unwindstack
  2. err_unwind
  3. lj_err_unwind_dwarf
  4. err_raise_ext
  5. _Unwind_GetGR
  6. _Unwind_SetGR
  7. lj_err_unwind_arm
  8. lj_err_unwind_win64
  9. err_raise_ext
  10. lj_err_throw
  11. lj_err_str
  12. lj_err_mem
  13. finderrfunc
  14. lj_err_run
  15. err_msgv
  16. lj_err_msg
  17. lj_err_lex
  18. lj_err_optype
  19. lj_err_comp
  20. lj_err_optype_call
  21. lj_err_callermsg
  22. lj_err_callerv
  23. lj_err_caller
  24. err_argmsg
  25. lj_err_argv
  26. lj_err_arg
  27. lj_err_argtype
  28. lj_err_argt
  29. lua_atpanic
  30. lua_error
  31. luaL_argerror
  32. luaL_typerror
  33. luaL_where
  34. luaL_error

   1 /*
   2 ** Error handling.
   3 ** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
   4 */
   5 
   6 #define lj_err_c
   7 #define LUA_CORE
   8 
   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 
  20 /*
  21 ** LuaJIT can either use internal or external frame unwinding:
  22 **
  23 ** - Internal frame unwinding (INT) is free-standing and doesn't require
  24 **   any OS or library support.
  25 **
  26 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
  27 **
  28 ** Pros and Cons:
  29 **
  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.
  33 **
  34 ** - INT is faster when actually throwing errors (but this happens rarely).
  35 **   Setting up error handlers is zero-cost in any case.
  36 **
  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++.
  42 **
  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.
  47 **
  48 ** EXT is the default on x64 systems, INT is the default on all other systems.
  49 **
  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.
  56 **
  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).
  61 */
  62 
  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
  68 
  69 /* -- Error messages ------------------------------------------------------ */
  70 
  71 /* Error message strings. */
  72 LJ_DATADEF const char *lj_err_allmsg =
  73 #define ERRDEF(name, msg)       msg "\0"
  74 #include "lj_errmsg.h"
  75 ;
  76 
  77 /* -- Internal frame unwinding -------------------------------------------- */
  78 
  79 /* Unwind Lua stack and move error message to new top. */
  80 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
  81 {
  82   lj_func_closeuv(L, top);
  83   if (top < L->top-1) {
  84     copyTV(L, top, L->top-1);
  85     L->top = top+1;
  86   }
  87   lj_state_relimitstack(L);
  88 }
  89 
  90 /* Unwind until stop frame. Optionally cleanup frames. */
  91 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
  92 {
  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);
 104         }
 105         return cf;
 106       }
 107     }
 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;
 128       }
 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;
 142         }
 143         return cf;
 144       }
 145       if (errcode) {
 146         L->cframe = cframe_prev(cf);
 147         L->base = frame_prevd(frame) + 1;
 148         unwindstack(L, frame);
 149       }
 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;
 165         }
 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);
 171       }
 172       return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
 173     }
 174   }
 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);
 183   }
 184   return L;  /* Anything non-NULL will do. */
 185 }
 186 
 187 /* -- External frame unwinding -------------------------------------------- */
 188 
 189 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_ABI_WIN
 190 
 191 /*
 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.
 194 */
 195 
 196 typedef struct _Unwind_Exception
 197 {
 198   uint64_t exclass;
 199   void (*excleanup)(int, struct _Unwind_Exception *);
 200   uintptr_t p1, p2;
 201 } __attribute__((__aligned__)) _Unwind_Exception;
 202 
 203 typedef struct _Unwind_Context _Unwind_Context;
 204 
 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
 211 
 212 #if !LJ_TARGET_ARM
 213 
 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 *);
 219 
 220 #define _UA_SEARCH_PHASE        1
 221 #define _UA_CLEANUP_PHASE       2
 222 #define _UA_HANDLER_FRAME       4
 223 #define _UA_FORCE_UNWIND        8
 224 
 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))
 229 
 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)
 233 {
 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));
 248     }
 249     return _URC_HANDLER_FOUND;
 250   }
 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;
 259     }
 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;
 270     }
 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
 275       */
 276       _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
 277       _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
 278       return _URC_INSTALL_CONTEXT;
 279     }
 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.
 284     */
 285     lj_err_throw(L, errcode);
 286 #endif
 287   }
 288   return _URC_CONTINUE_UNWIND;
 289 }
 290 
 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
 298 
 299 /* Raise DWARF2 exception. */
 300 static void err_raise_ext(int errcode)
 301 {
 302   static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);
 303   static_uex.excleanup = NULL;
 304   _Unwind_RaiseException(&static_uex);
 305 }
 306 #endif
 307 
 308 #else
 309 
 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 *);
 314 
 315 static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
 316 {
 317   uint32_t v;
 318   _Unwind_VRS_Get(ctx, 0, r, 0, &v);
 319   return v;
 320 }
 321 
 322 static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
 323 {
 324   _Unwind_VRS_Set(ctx, 0, r, 0, &v);
 325 }
 326 
 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
 331 
 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)
 334 {
 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;
 340   }
 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;
 347   }
 348   if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
 349     return _URC_FAILURE;
 350   return _URC_CONTINUE_UNWIND;
 351 }
 352 
 353 #endif
 354 
 355 #elif LJ_TARGET_X64 && LJ_ABI_WIN
 356 
 357 /*
 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. :-(
 363 **
 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.
 368 */
 369 
 370 #define WIN32_LEAN_AND_MEAN
 371 #include <windows.h>
 372 
 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;
 387 
 388 /* Another wild guess. */
 389 extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
 390 
 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
 396 
 397 #define LJ_MSVC_EXCODE          ((DWORD)0xe06d7363)
 398 #define LJ_GCC_EXCODE           ((DWORD)0x20474343)
 399 
 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))
 404 
 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)
 408 {
 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 #if LJ_TARGET_WINDOWS
 421         __DestructExceptionObject(rec, 1);
 422 #endif
 423         setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
 424       } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
 425         /* Don't catch access violations etc. */
 426         return ExceptionContinueSearch;
 427       }
 428       /* Unwind the stack and call all handlers for all lower C frames
 429       ** (including ourselves) again with EH_UNWINDING set. Then set
 430       ** rsp = cf, rax = errcode and jump to the specified target.
 431       */
 432       RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
 433                                lj_vm_unwind_ff_eh :
 434                                lj_vm_unwind_c_eh),
 435                   rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);
 436       /* RtlUnwindEx should never return. */
 437     }
 438   }
 439   return ExceptionContinueSearch;
 440 }
 441 
 442 /* Raise Windows exception. */
 443 static void err_raise_ext(int errcode)
 444 {
 445   RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
 446 }
 447 
 448 #endif
 449 
 450 /* -- Error handling ------------------------------------------------------ */
 451 
 452 /* Throw error. Find catch frame, unwind stack and continue. */
 453 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
 454 {
 455   global_State *g = G(L);
 456   lj_trace_abort(g);
 457   setgcrefnull(g->jit_L);
 458   L->status = 0;
 459 #if LJ_UNWIND_EXT
 460   err_raise_ext(errcode);
 461   /*
 462   ** A return from this function signals a corrupt C stack that cannot be
 463   ** unwound. We have no choice but to call the panic function and exit.
 464   **
 465   ** Usually this is caused by a C function without unwind information.
 466   ** This should never happen on x64, but may happen if you've manually
 467   ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
 468   ** non-C++ file with -funwind-tables.
 469   */
 470   if (G(L)->panic)
 471     G(L)->panic(L);
 472 #else
 473   {
 474     void *cf = err_unwind(L, NULL, errcode);
 475     if (cframe_unwind_ff(cf))
 476       lj_vm_unwind_ff(cframe_raw(cf));
 477     else
 478       lj_vm_unwind_c(cframe_raw(cf), errcode);
 479   }
 480 #endif
 481   exit(EXIT_FAILURE);
 482 }
 483 
 484 /* Return string object for error message. */
 485 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
 486 {
 487   return lj_str_newz(L, err2msg(em));
 488 }
 489 
 490 /* Out-of-memory error. */
 491 LJ_NOINLINE void lj_err_mem(lua_State *L)
 492 {
 493   if (L->status == LUA_ERRERR+1)  /* Don't touch the stack during lua_open. */
 494     lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
 495   setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
 496   lj_err_throw(L, LUA_ERRMEM);
 497 }
 498 
 499 /* Find error function for runtime errors. Requires an extra stack traversal. */
 500 static ptrdiff_t finderrfunc(lua_State *L)
 501 {
 502   cTValue *frame = L->base-1, *bot = tvref(L->stack);
 503   void *cf = L->cframe;
 504   while (frame > bot && cf) {
 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;
 513     }
 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;
 546     }
 547   }
 548   return 0;
 549 }
 550 
 551 /* Runtime error. */
 552 LJ_NOINLINE void lj_err_run(lua_State *L)
 553 {
 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);
 562     }
 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| */
 568   }
 569   lj_err_throw(L, LUA_ERRRUN);
 570 }
 571 
 572 /* Formatted runtime error message. */
 573 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
 574 {
 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_str_pushvf(L, err2msg(em), argp);
 580   va_end(argp);
 581   lj_debug_addloc(L, msg, L->base-1, NULL);
 582   lj_err_run(L);
 583 }
 584 
 585 /* Non-vararg variant for better calling conventions. */
 586 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
 587 {
 588   err_msgv(L, em);
 589 }
 590 
 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)
 594 {
 595   char buff[LUA_IDSIZE];
 596   const char *msg;
 597   lj_debug_shortname(buff, src);
 598   msg = lj_str_pushvf(L, err2msg(em), argp);
 599   msg = lj_str_pushf(L, "%s:%d: %s", buff, line, msg);
 600   if (tok)
 601     lj_str_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
 602   lj_err_throw(L, LUA_ERRSYNTAX);
 603 }
 604 
 605 /* Typecheck error for operands. */
 606 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
 607 {
 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);
 617   }
 618   err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
 619 }
 620 
 621 /* Typecheck error for ordered comparisons. */
 622 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
 623 {
 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. */
 628 }
 629 
 630 /* Typecheck error for __call. */
 631 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
 632 {
 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().
 636   */
 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);
 644   }
 645   lj_err_optype(L, o, LJ_ERR_OPCALL);
 646 }
 647 
 648 /* Error in context of caller. */
 649 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
 650 {
 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
 662     {
 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));
 671       }
 672 #endif
 673     }
 674   }
 675   lj_debug_addloc(L, msg, pframe, frame);
 676   lj_err_run(L);
 677 }
 678 
 679 /* Formatted error in context of caller. */
 680 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
 681 {
 682   const char *msg;
 683   va_list argp;
 684   va_start(argp, em);
 685   msg = lj_str_pushvf(L, err2msg(em), argp);
 686   va_end(argp);
 687   lj_err_callermsg(L, msg);
 688 }
 689 
 690 /* Error in context of caller. */
 691 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
 692 {
 693   lj_err_callermsg(L, err2msg(em));
 694 }
 695 
 696 /* Argument error message. */
 697 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
 698                                             const char *msg)
 699 {
 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_str_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
 706   else
 707     msg = lj_str_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
 708   lj_err_callermsg(L, msg);
 709 }
 710 
 711 /* Formatted argument error. */
 712 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
 713 {
 714   const char *msg;
 715   va_list argp;
 716   va_start(argp, em);
 717   msg = lj_str_pushvf(L, err2msg(em), argp);
 718   va_end(argp);
 719   err_argmsg(L, narg, msg);
 720 }
 721 
 722 /* Argument error. */
 723 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
 724 {
 725   err_argmsg(L, narg, err2msg(em));
 726 }
 727 
 728 /* Typecheck error for arguments. */
 729 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
 730 {
 731   const char *tname, *msg;
 732   if (narg <= LUA_REGISTRYINDEX) {
 733     if (narg >= LUA_GLOBALSINDEX) {
 734       tname = lj_obj_itypename[~LJ_TTAB];
 735     } else {
 736       GCfunc *fn = curr_func(L);
 737       int idx = LUA_GLOBALSINDEX - narg;
 738       if (idx <= fn->c.nupvalues)
 739         tname = lj_typename(&fn->c.upvalue[idx-1]);
 740       else
 741         tname = lj_obj_typename[0];
 742     }
 743   } else {
 744     TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
 745     tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
 746   }
 747   msg = lj_str_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
 748   err_argmsg(L, narg, msg);
 749 }
 750 
 751 /* Typecheck error for arguments. */
 752 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
 753 {
 754   lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
 755 }
 756 
 757 /* -- Public error handling API ------------------------------------------- */
 758 
 759 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
 760 {
 761   lua_CFunction old = G(L)->panic;
 762   G(L)->panic = panicf;
 763   return old;
 764 }
 765 
 766 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
 767 LUA_API int lua_error(lua_State *L)
 768 {
 769   lj_err_run(L);
 770   return 0;  /* unreachable */
 771 }
 772 
 773 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
 774 {
 775   err_argmsg(L, narg, msg);
 776   return 0;  /* unreachable */
 777 }
 778 
 779 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
 780 {
 781   lj_err_argtype(L, narg, xname);
 782   return 0;  /* unreachable */
 783 }
 784 
 785 LUALIB_API void luaL_where(lua_State *L, int level)
 786 {
 787   int size;
 788   cTValue *frame = lj_debug_frame(L, level, &size);
 789   lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
 790 }
 791 
 792 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
 793 {
 794   const char *msg;
 795   va_list argp;
 796   va_start(argp, fmt);
 797   msg = lj_str_pushvf(L, fmt, argp);
 798   va_end(argp);
 799   lj_err_callermsg(L, msg);
 800   return 0;  /* unreachable */
 801 }
 802 

/* [<][>][^][v][top][bottom][index][help] */