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. err_raise_ext
  9. lj_err_unwind_win
  10. err_raise_ext
  11. lj_err_throw
  12. lj_err_str
  13. lj_err_mem
  14. finderrfunc
  15. lj_err_run
  16. err_msgv
  17. lj_err_msg
  18. lj_err_lex
  19. lj_err_optype
  20. lj_err_comp
  21. lj_err_optype_call
  22. lj_err_callermsg
  23. lj_err_callerv
  24. lj_err_caller
  25. err_argmsg
  26. lj_err_argv
  27. lj_err_arg
  28. lj_err_argtype
  29. lj_err_argt
  30. lua_atpanic
  31. lua_error
  32. luaL_argerror
  33. luaL_typerror
  34. luaL_where
  35. 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 #include "lj_strfmt.h"
  20 
  21 /*
  22 ** LuaJIT can either use internal or external frame unwinding:
  23 **
  24 ** - Internal frame unwinding (INT) is free-standing and doesn't require
  25 **   any OS or library support.
  26 **
  27 ** - External frame unwinding (EXT) uses the system-provided unwind handler.
  28 **
  29 ** Pros and Cons:
  30 **
  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.
  34 **
  35 ** - INT is faster when actually throwing errors (but this happens rarely).
  36 **   Setting up error handlers is zero-cost in any case.
  37 **
  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++.
  43 **
  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.
  48 **
  49 ** EXT is the default on x64 systems and on Windows, INT is the default on all
  50 ** other systems.
  51 **
  52 ** EXT can be manually enabled on POSIX systems using GCC and DWARF2 stack
  53 ** unwinding with -DLUAJIT_UNWIND_EXTERNAL. *All* C code must be compiled
  54 ** with -funwind-tables (or -fexceptions). This includes LuaJIT itself (set
  55 ** TARGET_CFLAGS), all of your C/Lua binding code, all loadable C modules
  56 ** and all C libraries that have callbacks which may be used to call back
  57 ** into Lua. C++ code must *not* be compiled with -fno-exceptions.
  58 **
  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 ** The POSIX/x64 interpreter only saves r12/r13 for INT (e.g. PS4).
  62 */
  63 
  64 #if defined(__GNUC__) && (LJ_TARGET_X64 || defined(LUAJIT_UNWIND_EXTERNAL)) && !LJ_NO_UNWIND
  65 #define LJ_UNWIND_EXT   1
  66 #elif LJ_TARGET_WINDOWS
  67 #define LJ_UNWIND_EXT   1
  68 #endif
  69 
  70 /* -- Error messages ------------------------------------------------------ */
  71 
  72 /* Error message strings. */
  73 LJ_DATADEF const char *lj_err_allmsg =
  74 #define ERRDEF(name, msg)       msg "\0"
  75 #include "lj_errmsg.h"
  76 ;
  77 
  78 /* -- Internal frame unwinding -------------------------------------------- */
  79 
  80 /* Unwind Lua stack and move error message to new top. */
  81 LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
  82 {
  83   lj_func_closeuv(L, top);
  84   if (top < L->top-1) {
  85     copyTV(L, top, L->top-1);
  86     L->top = top+1;
  87   }
  88   lj_state_relimitstack(L);
  89 }
  90 
  91 /* Unwind until stop frame. Optionally cleanup frames. */
  92 static void *err_unwind(lua_State *L, void *stopcf, int errcode)
  93 {
  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);
 105         }
 106         return cf;
 107       }
 108     }
 109     if (frame <= tvref(L->stack)+LJ_FR2)
 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     unwind_c:
 118 #if LJ_UNWIND_EXT
 119       if (errcode) {
 120         L->base = frame_prevd(frame) + 1;
 121         L->cframe = cframe_prev(cf);
 122         unwindstack(L, frame - LJ_FR2);
 123       } else if (cf != stopcf) {
 124         cf = cframe_prev(cf);
 125         frame = frame_prevd(frame);
 126         break;
 127       }
 128       return NULL;  /* Continue unwinding. */
 129 #else
 130       UNUSED(stopcf);
 131       cf = cframe_prev(cf);
 132       frame = frame_prevd(frame);
 133       break;
 134 #endif
 135     case FRAME_CP:  /* Protected C frame. */
 136       if (cframe_canyield(cf)) {  /* Resume? */
 137         if (errcode) {
 138           hook_leave(G(L));  /* Assumes nobody uses coroutines inside hooks. */
 139           L->cframe = NULL;
 140           L->status = (uint8_t)errcode;
 141         }
 142         return cf;
 143       }
 144       if (errcode) {
 145         L->base = frame_prevd(frame) + 1;
 146         L->cframe = cframe_prev(cf);
 147         unwindstack(L, frame - LJ_FR2);
 148       }
 149       return cf;
 150     case FRAME_CONT:  /* Continuation frame. */
 151       if (frame_iscont_fficb(frame))
 152         goto unwind_c;
 153       /* fallthrough */
 154     case FRAME_VARG:  /* Vararg frame. */
 155       frame = frame_prevd(frame);
 156       break;
 157     case FRAME_PCALL:  /* FF pcall() frame. */
 158     case FRAME_PCALLH:  /* FF pcall() frame inside hook. */
 159       if (errcode) {
 160         if (errcode == LUA_YIELD) {
 161           frame = frame_prevd(frame);
 162           break;
 163         }
 164         if (frame_typep(frame) == FRAME_PCALL)
 165           hook_leave(G(L));
 166         L->base = frame_prevd(frame) + 1;
 167         L->cframe = cf;
 168         unwindstack(L, L->base);
 169       }
 170       return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
 171     }
 172   }
 173   /* No C frame. */
 174   if (errcode) {
 175     L->base = tvref(L->stack)+1+LJ_FR2;
 176     L->cframe = NULL;
 177     unwindstack(L, L->base);
 178     if (G(L)->panic)
 179       G(L)->panic(L);
 180     exit(EXIT_FAILURE);
 181   }
 182   return L;  /* Anything non-NULL will do. */
 183 }
 184 
 185 /* -- External frame unwinding -------------------------------------------- */
 186 
 187 #if defined(__GNUC__) && !LJ_NO_UNWIND && !LJ_ABI_WIN
 188 
 189 /*
 190 ** We have to use our own definitions instead of the mandatory (!) unwind.h,
 191 ** since various OS, distros and compilers mess up the header installation.
 192 */
 193 
 194 typedef struct _Unwind_Context _Unwind_Context;
 195 
 196 #define _URC_OK                 0
 197 #define _URC_FATAL_PHASE1_ERROR 3
 198 #define _URC_HANDLER_FOUND      6
 199 #define _URC_INSTALL_CONTEXT    7
 200 #define _URC_CONTINUE_UNWIND    8
 201 #define _URC_FAILURE            9
 202 
 203 #define LJ_UEXCLASS             0x4c55414a49543200ULL   /* LUAJIT2\0 */
 204 #define LJ_UEXCLASS_MAKE(c)     (LJ_UEXCLASS | (uint64_t)(c))
 205 #define LJ_UEXCLASS_CHECK(cl)   (((cl) ^ LJ_UEXCLASS) <= 0xff)
 206 #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
 207 
 208 #if !LJ_TARGET_ARM
 209 
 210 typedef struct _Unwind_Exception
 211 {
 212   uint64_t exclass;
 213   void (*excleanup)(int, struct _Unwind_Exception *);
 214   uintptr_t p1, p2;
 215 } __attribute__((__aligned__)) _Unwind_Exception;
 216 
 217 extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
 218 extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);
 219 extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);
 220 extern void _Unwind_DeleteException(_Unwind_Exception *);
 221 extern int _Unwind_RaiseException(_Unwind_Exception *);
 222 
 223 #define _UA_SEARCH_PHASE        1
 224 #define _UA_CLEANUP_PHASE       2
 225 #define _UA_HANDLER_FRAME       4
 226 #define _UA_FORCE_UNWIND        8
 227 
 228 /* DWARF2 personality handler referenced from interpreter .eh_frame. */
 229 LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
 230   uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
 231 {
 232   void *cf;
 233   lua_State *L;
 234   if (version != 1)
 235     return _URC_FATAL_PHASE1_ERROR;
 236   UNUSED(uexclass);
 237   cf = (void *)_Unwind_GetCFA(ctx);
 238   L = cframe_L(cf);
 239   if ((actions & _UA_SEARCH_PHASE)) {
 240 #if LJ_UNWIND_EXT
 241     if (err_unwind(L, cf, 0) == NULL)
 242       return _URC_CONTINUE_UNWIND;
 243 #endif
 244     if (!LJ_UEXCLASS_CHECK(uexclass)) {
 245       setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
 246     }
 247     return _URC_HANDLER_FOUND;
 248   }
 249   if ((actions & _UA_CLEANUP_PHASE)) {
 250     int errcode;
 251     if (LJ_UEXCLASS_CHECK(uexclass)) {
 252       errcode = LJ_UEXCLASS_ERRCODE(uexclass);
 253     } else {
 254       if ((actions & _UA_HANDLER_FRAME))
 255         _Unwind_DeleteException(uex);
 256       errcode = LUA_ERRRUN;
 257     }
 258 #if LJ_UNWIND_EXT
 259     cf = err_unwind(L, cf, errcode);
 260     if ((actions & _UA_FORCE_UNWIND)) {
 261       return _URC_CONTINUE_UNWIND;
 262     } else if (cf) {
 263       _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
 264       _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ?
 265                                      lj_vm_unwind_ff_eh :
 266                                      lj_vm_unwind_c_eh));
 267       return _URC_INSTALL_CONTEXT;
 268     }
 269 #if LJ_TARGET_X86ORX64
 270     else if ((actions & _UA_HANDLER_FRAME)) {
 271       /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
 272       ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
 273       */
 274       _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
 275       _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
 276       return _URC_INSTALL_CONTEXT;
 277     }
 278 #endif
 279 #else
 280     /* This is not the proper way to escape from the unwinder. We get away with
 281     ** it on non-x64 because the interpreter restores all callee-saved regs.
 282     */
 283     lj_err_throw(L, errcode);
 284 #endif
 285   }
 286   return _URC_CONTINUE_UNWIND;
 287 }
 288 
 289 #if LJ_UNWIND_EXT
 290 #if LJ_TARGET_OSX || defined(__OpenBSD__)
 291 /* Sorry, no thread safety for OSX. Complain to Apple, not me. */
 292 static _Unwind_Exception static_uex;
 293 #else
 294 static __thread _Unwind_Exception static_uex;
 295 #endif
 296 
 297 /* Raise DWARF2 exception. */
 298 static void err_raise_ext(int errcode)
 299 {
 300   static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);
 301   static_uex.excleanup = NULL;
 302   _Unwind_RaiseException(&static_uex);
 303 }
 304 #endif
 305 
 306 #else /* LJ_TARGET_ARM */
 307 
 308 #define _US_VIRTUAL_UNWIND_FRAME        0
 309 #define _US_UNWIND_FRAME_STARTING       1
 310 #define _US_ACTION_MASK                 3
 311 #define _US_FORCE_UNWIND                8
 312 
 313 typedef struct _Unwind_Control_Block _Unwind_Control_Block;
 314 
 315 struct _Unwind_Control_Block {
 316   uint64_t exclass;
 317   uint32_t misc[20];
 318 };
 319 
 320 extern int _Unwind_RaiseException(_Unwind_Control_Block *);
 321 extern int __gnu_unwind_frame(_Unwind_Control_Block *, _Unwind_Context *);
 322 extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);
 323 extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);
 324 
 325 static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
 326 {
 327   uint32_t v;
 328   _Unwind_VRS_Get(ctx, 0, r, 0, &v);
 329   return v;
 330 }
 331 
 332 static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
 333 {
 334   _Unwind_VRS_Set(ctx, 0, r, 0, &v);
 335 }
 336 
 337 extern void lj_vm_unwind_ext(void);
 338 
 339 /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
 340 LJ_FUNCA int lj_err_unwind_arm(int state, _Unwind_Control_Block *ucb,
 341                                _Unwind_Context *ctx)
 342 {
 343   void *cf = (void *)_Unwind_GetGR(ctx, 13);
 344   lua_State *L = cframe_L(cf);
 345   int errcode;
 346 
 347   switch ((state & _US_ACTION_MASK)) {
 348   case _US_VIRTUAL_UNWIND_FRAME:
 349     if ((state & _US_FORCE_UNWIND)) break;
 350     return _URC_HANDLER_FOUND;
 351   case _US_UNWIND_FRAME_STARTING:
 352     if (LJ_UEXCLASS_CHECK(ucb->exclass)) {
 353       errcode = LJ_UEXCLASS_ERRCODE(ucb->exclass);
 354     } else {
 355       errcode = LUA_ERRRUN;
 356       setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
 357     }
 358     cf = err_unwind(L, cf, errcode);
 359     if ((state & _US_FORCE_UNWIND) || cf == NULL) break;
 360     _Unwind_SetGR(ctx, 15, (uint32_t)lj_vm_unwind_ext);
 361     _Unwind_SetGR(ctx, 0, (uint32_t)ucb);
 362     _Unwind_SetGR(ctx, 1, (uint32_t)errcode);
 363     _Unwind_SetGR(ctx, 2, cframe_unwind_ff(cf) ?
 364                             (uint32_t)lj_vm_unwind_ff_eh :
 365                             (uint32_t)lj_vm_unwind_c_eh);
 366     return _URC_INSTALL_CONTEXT;
 367   default:
 368     return _URC_FAILURE;
 369   }
 370   if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
 371     return _URC_FAILURE;
 372   return _URC_CONTINUE_UNWIND;
 373 }
 374 
 375 #if LJ_UNWIND_EXT
 376 static __thread _Unwind_Control_Block static_uex;
 377 
 378 static void err_raise_ext(int errcode)
 379 {
 380   memset(&static_uex, 0, sizeof(static_uex));
 381   static_uex.exclass = LJ_UEXCLASS_MAKE(errcode);
 382   _Unwind_RaiseException(&static_uex);
 383 }
 384 #endif
 385 
 386 #endif /* LJ_TARGET_ARM */
 387 
 388 #elif LJ_ABI_WIN
 389 
 390 /*
 391 ** Someone in Redmond owes me several days of my life. A lot of this is
 392 ** undocumented or just plain wrong on MSDN. Some of it can be gathered
 393 ** from 3rd party docs or must be found by trial-and-error. They really
 394 ** don't want you to write your own language-specific exception handler
 395 ** or to interact gracefully with MSVC. :-(
 396 **
 397 ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
 398 ** unless you compile your C++ code with /EHa. Unfortunately this means
 399 ** catch (...) also catches things like access violations. The use of
 400 ** _set_se_translator doesn't really help, because it requires /EHa, too.
 401 */
 402 
 403 #define WIN32_LEAN_AND_MEAN
 404 #include <windows.h>
 405 
 406 #if LJ_TARGET_X64
 407 /* Taken from: http://www.nynaeve.net/?p=99 */
 408 typedef struct UndocumentedDispatcherContext {
 409   ULONG64 ControlPc;
 410   ULONG64 ImageBase;
 411   PRUNTIME_FUNCTION FunctionEntry;
 412   ULONG64 EstablisherFrame;
 413   ULONG64 TargetIp;
 414   PCONTEXT ContextRecord;
 415   void (*LanguageHandler)(void);
 416   PVOID HandlerData;
 417   PUNWIND_HISTORY_TABLE HistoryTable;
 418   ULONG ScopeIndex;
 419   ULONG Fill0;
 420 } UndocumentedDispatcherContext;
 421 #else
 422 typedef void *UndocumentedDispatcherContext;
 423 #endif
 424 
 425 /* Another wild guess. */
 426 extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
 427 
 428 #if LJ_TARGET_X64 && defined(MINGW_SDK_INIT)
 429 /* Workaround for broken MinGW64 declaration. */
 430 VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");
 431 #define RtlUnwindEx RtlUnwindEx_FIXED
 432 #endif
 433 
 434 #define LJ_MSVC_EXCODE          ((DWORD)0xe06d7363)
 435 #define LJ_GCC_EXCODE           ((DWORD)0x20474343)
 436 
 437 #define LJ_EXCODE               ((DWORD)0xe24c4a00)
 438 #define LJ_EXCODE_MAKE(c)       (LJ_EXCODE | (DWORD)(c))
 439 #define LJ_EXCODE_CHECK(cl)     (((cl) ^ LJ_EXCODE) <= 0xff)
 440 #define LJ_EXCODE_ERRCODE(cl)   ((int)((cl) & 0xff))
 441 
 442 /* Windows exception handler for interpreter frame. */
 443 LJ_FUNCA int lj_err_unwind_win(EXCEPTION_RECORD *rec,
 444   void *f, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
 445 {
 446 #if LJ_TARGET_X64
 447   void *cf = f;
 448 #else
 449   void *cf = (char *)f - CFRAME_OFS_SEH;
 450 #endif
 451   lua_State *L = cframe_L(cf);
 452   int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
 453                 LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
 454   if ((rec->ExceptionFlags & 6)) {  /* EH_UNWINDING|EH_EXIT_UNWIND */
 455     /* Unwind internal frames. */
 456     err_unwind(L, cf, errcode);
 457   } else {
 458     void *cf2 = err_unwind(L, cf, 0);
 459     if (cf2) {  /* We catch it, so start unwinding the upper frames. */
 460       if (rec->ExceptionCode == LJ_MSVC_EXCODE ||
 461           rec->ExceptionCode == LJ_GCC_EXCODE) {
 462 #if LJ_TARGET_WINDOWS
 463         __DestructExceptionObject(rec, 1);
 464 #endif
 465         setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
 466       } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
 467         /* Don't catch access violations etc. */
 468         return 1;  /* ExceptionContinueSearch */
 469       }
 470 #if LJ_TARGET_X64
 471       /* Unwind the stack and call all handlers for all lower C frames
 472       ** (including ourselves) again with EH_UNWINDING set. Then set
 473       ** rsp = cf, rax = errcode and jump to the specified target.
 474       */
 475       RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
 476                                lj_vm_unwind_ff_eh :
 477                                lj_vm_unwind_c_eh),
 478                   rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);
 479       /* RtlUnwindEx should never return. */
 480 #else
 481       UNUSED(ctx);
 482       UNUSED(dispatch);
 483       /* Call all handlers for all lower C frames (including ourselves) again
 484       ** with EH_UNWINDING set. Then call the specified function, passing cf
 485       ** and errcode.
 486       */
 487       lj_vm_rtlunwind(cf, (void *)rec,
 488         (cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
 489         (void *)lj_vm_unwind_ff : (void *)lj_vm_unwind_c, errcode);
 490       /* lj_vm_rtlunwind does not return. */
 491 #endif
 492     }
 493   }
 494   return 1;  /* ExceptionContinueSearch */
 495 }
 496 
 497 /* Raise Windows exception. */
 498 static void err_raise_ext(int errcode)
 499 {
 500   RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
 501 }
 502 
 503 #endif
 504 
 505 /* -- Error handling ------------------------------------------------------ */
 506 
 507 /* Throw error. Find catch frame, unwind stack and continue. */
 508 LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
 509 {
 510   global_State *g = G(L);
 511   lj_trace_abort(g);
 512   setmref(g->jit_base, NULL);
 513   L->status = LUA_OK;
 514 #if LJ_UNWIND_EXT
 515   err_raise_ext(errcode);
 516   /*
 517   ** A return from this function signals a corrupt C stack that cannot be
 518   ** unwound. We have no choice but to call the panic function and exit.
 519   **
 520   ** Usually this is caused by a C function without unwind information.
 521   ** This should never happen on x64, but may happen if you've manually
 522   ** enabled LUAJIT_UNWIND_EXTERNAL and forgot to recompile *every*
 523   ** non-C++ file with -funwind-tables.
 524   */
 525   if (G(L)->panic)
 526     G(L)->panic(L);
 527 #else
 528   {
 529     void *cf = err_unwind(L, NULL, errcode);
 530     if (cframe_unwind_ff(cf))
 531       lj_vm_unwind_ff(cframe_raw(cf));
 532     else
 533       lj_vm_unwind_c(cframe_raw(cf), errcode);
 534   }
 535 #endif
 536   exit(EXIT_FAILURE);
 537 }
 538 
 539 /* Return string object for error message. */
 540 LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
 541 {
 542   return lj_str_newz(L, err2msg(em));
 543 }
 544 
 545 /* Out-of-memory error. */
 546 LJ_NOINLINE void lj_err_mem(lua_State *L)
 547 {
 548   if (L->status == LUA_ERRERR+1)  /* Don't touch the stack during lua_open. */
 549     lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
 550   setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
 551   lj_err_throw(L, LUA_ERRMEM);
 552 }
 553 
 554 /* Find error function for runtime errors. Requires an extra stack traversal. */
 555 static ptrdiff_t finderrfunc(lua_State *L)
 556 {
 557   cTValue *frame = L->base-1, *bot = tvref(L->stack)+LJ_FR2;
 558   void *cf = L->cframe;
 559   while (frame > bot && cf) {
 560     while (cframe_nres(cframe_raw(cf)) < 0) {  /* cframe without frame? */
 561       if (frame >= restorestack(L, -cframe_nres(cf)))
 562         break;
 563       if (cframe_errfunc(cf) >= 0)  /* Error handler not inherited (-1)? */
 564         return cframe_errfunc(cf);
 565       cf = cframe_prev(cf);  /* Else unwind cframe and continue searching. */
 566       if (cf == NULL)
 567         return 0;
 568     }
 569     switch (frame_typep(frame)) {
 570     case FRAME_LUA:
 571     case FRAME_LUAP:
 572       frame = frame_prevl(frame);
 573       break;
 574     case FRAME_C:
 575       cf = cframe_prev(cf);
 576       /* fallthrough */
 577     case FRAME_VARG:
 578       frame = frame_prevd(frame);
 579       break;
 580     case FRAME_CONT:
 581       if (frame_iscont_fficb(frame))
 582         cf = cframe_prev(cf);
 583       frame = frame_prevd(frame);
 584       break;
 585     case FRAME_CP:
 586       if (cframe_canyield(cf)) return 0;
 587       if (cframe_errfunc(cf) >= 0)
 588         return cframe_errfunc(cf);
 589       frame = frame_prevd(frame);
 590       break;
 591     case FRAME_PCALL:
 592     case FRAME_PCALLH:
 593       if (frame_func(frame_prevd(frame))->c.ffid == FF_xpcall)
 594         return savestack(L, frame_prevd(frame)+1);  /* xpcall's errorfunc. */
 595       return 0;
 596     default:
 597       lua_assert(0);
 598       return 0;
 599     }
 600   }
 601   return 0;
 602 }
 603 
 604 /* Runtime error. */
 605 LJ_NOINLINE void lj_err_run(lua_State *L)
 606 {
 607   ptrdiff_t ef = finderrfunc(L);
 608   if (ef) {
 609     TValue *errfunc = restorestack(L, ef);
 610     TValue *top = L->top;
 611     lj_trace_abort(G(L));
 612     if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
 613       setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
 614       lj_err_throw(L, LUA_ERRERR);
 615     }
 616     L->status = LUA_ERRERR;
 617     copyTV(L, top+LJ_FR2, top-1);
 618     copyTV(L, top-1, errfunc);
 619     if (LJ_FR2) setnilV(top++);
 620     L->top = top+1;
 621     lj_vm_call(L, top, 1+1);  /* Stack: |errfunc|msg| -> |msg| */
 622   }
 623   lj_err_throw(L, LUA_ERRRUN);
 624 }
 625 
 626 /* Formatted runtime error message. */
 627 LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
 628 {
 629   const char *msg;
 630   va_list argp;
 631   va_start(argp, em);
 632   if (curr_funcisL(L)) L->top = curr_topL(L);
 633   msg = lj_strfmt_pushvf(L, err2msg(em), argp);
 634   va_end(argp);
 635   lj_debug_addloc(L, msg, L->base-1, NULL);
 636   lj_err_run(L);
 637 }
 638 
 639 /* Non-vararg variant for better calling conventions. */
 640 LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
 641 {
 642   err_msgv(L, em);
 643 }
 644 
 645 /* Lexer error. */
 646 LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
 647                             BCLine line, ErrMsg em, va_list argp)
 648 {
 649   char buff[LUA_IDSIZE];
 650   const char *msg;
 651   lj_debug_shortname(buff, src, line);
 652   msg = lj_strfmt_pushvf(L, err2msg(em), argp);
 653   msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);
 654   if (tok)
 655     lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
 656   lj_err_throw(L, LUA_ERRSYNTAX);
 657 }
 658 
 659 /* Typecheck error for operands. */
 660 LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
 661 {
 662   const char *tname = lj_typename(o);
 663   const char *opname = err2msg(opm);
 664   if (curr_funcisL(L)) {
 665     GCproto *pt = curr_proto(L);
 666     const BCIns *pc = cframe_Lpc(L) - 1;
 667     const char *oname = NULL;
 668     const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
 669     if (kind)
 670       err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
 671   }
 672   err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
 673 }
 674 
 675 /* Typecheck error for ordered comparisons. */
 676 LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
 677 {
 678   const char *t1 = lj_typename(o1);
 679   const char *t2 = lj_typename(o2);
 680   err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
 681   /* This assumes the two "boolean" entries are commoned by the C compiler. */
 682 }
 683 
 684 /* Typecheck error for __call. */
 685 LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
 686 {
 687   /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
 688   ** L->base still points to the caller. So add a dummy frame with L instead
 689   ** of a function. See lua_getstack().
 690   */
 691   const BCIns *pc = cframe_Lpc(L);
 692   if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
 693     const char *tname = lj_typename(o);
 694     if (LJ_FR2) o++;
 695     setframe_pc(o, pc);
 696     setframe_gc(o, obj2gco(L), LJ_TTHREAD);
 697     L->top = L->base = o+1;
 698     err_msgv(L, LJ_ERR_BADCALL, tname);
 699   }
 700   lj_err_optype(L, o, LJ_ERR_OPCALL);
 701 }
 702 
 703 /* Error in context of caller. */
 704 LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
 705 {
 706   TValue *frame = L->base-1;
 707   TValue *pframe = NULL;
 708   if (frame_islua(frame)) {
 709     pframe = frame_prevl(frame);
 710   } else if (frame_iscont(frame)) {
 711     if (frame_iscont_fficb(frame)) {
 712       pframe = frame;
 713       frame = NULL;
 714     } else {
 715       pframe = frame_prevd(frame);
 716 #if LJ_HASFFI
 717       /* Remove frame for FFI metamethods. */
 718       if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
 719           frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
 720         L->base = pframe+1;
 721         L->top = frame;
 722         setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));
 723       }
 724 #endif
 725     }
 726   }
 727   lj_debug_addloc(L, msg, pframe, frame);
 728   lj_err_run(L);
 729 }
 730 
 731 /* Formatted error in context of caller. */
 732 LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
 733 {
 734   const char *msg;
 735   va_list argp;
 736   va_start(argp, em);
 737   msg = lj_strfmt_pushvf(L, err2msg(em), argp);
 738   va_end(argp);
 739   lj_err_callermsg(L, msg);
 740 }
 741 
 742 /* Error in context of caller. */
 743 LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
 744 {
 745   lj_err_callermsg(L, err2msg(em));
 746 }
 747 
 748 /* Argument error message. */
 749 LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
 750                                             const char *msg)
 751 {
 752   const char *fname = "?";
 753   const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
 754   if (narg < 0 && narg > LUA_REGISTRYINDEX)
 755     narg = (int)(L->top - L->base) + narg + 1;
 756   if (ftype && ftype[3] == 'h' && --narg == 0)  /* Check for "method". */
 757     msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
 758   else
 759     msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
 760   lj_err_callermsg(L, msg);
 761 }
 762 
 763 /* Formatted argument error. */
 764 LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
 765 {
 766   const char *msg;
 767   va_list argp;
 768   va_start(argp, em);
 769   msg = lj_strfmt_pushvf(L, err2msg(em), argp);
 770   va_end(argp);
 771   err_argmsg(L, narg, msg);
 772 }
 773 
 774 /* Argument error. */
 775 LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
 776 {
 777   err_argmsg(L, narg, err2msg(em));
 778 }
 779 
 780 /* Typecheck error for arguments. */
 781 LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
 782 {
 783   const char *tname, *msg;
 784   if (narg <= LUA_REGISTRYINDEX) {
 785     if (narg >= LUA_GLOBALSINDEX) {
 786       tname = lj_obj_itypename[~LJ_TTAB];
 787     } else {
 788       GCfunc *fn = curr_func(L);
 789       int idx = LUA_GLOBALSINDEX - narg;
 790       if (idx <= fn->c.nupvalues)
 791         tname = lj_typename(&fn->c.upvalue[idx-1]);
 792       else
 793         tname = lj_obj_typename[0];
 794     }
 795   } else {
 796     TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
 797     tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
 798   }
 799   msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
 800   err_argmsg(L, narg, msg);
 801 }
 802 
 803 /* Typecheck error for arguments. */
 804 LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
 805 {
 806   lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
 807 }
 808 
 809 /* -- Public error handling API ------------------------------------------- */
 810 
 811 LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
 812 {
 813   lua_CFunction old = G(L)->panic;
 814   G(L)->panic = panicf;
 815   return old;
 816 }
 817 
 818 /* Forwarders for the public API (C calling convention and no LJ_NORET). */
 819 LUA_API int lua_error(lua_State *L)
 820 {
 821   lj_err_run(L);
 822   return 0;  /* unreachable */
 823 }
 824 
 825 LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
 826 {
 827   err_argmsg(L, narg, msg);
 828   return 0;  /* unreachable */
 829 }
 830 
 831 LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
 832 {
 833   lj_err_argtype(L, narg, xname);
 834   return 0;  /* unreachable */
 835 }
 836 
 837 LUALIB_API void luaL_where(lua_State *L, int level)
 838 {
 839   int size;
 840   cTValue *frame = lj_debug_frame(L, level, &size);
 841   lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
 842 }
 843 
 844 LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
 845 {
 846   const char *msg;
 847   va_list argp;
 848   va_start(argp, fmt);
 849   msg = lj_strfmt_pushvf(L, fmt, argp);
 850   va_end(argp);
 851   lj_err_callermsg(L, msg);
 852   return 0;  /* unreachable */
 853 }
 854 

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