lj_err.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094
  1. /*
  2. ** Error handling.
  3. ** Copyright (C) 2005-2021 Mike Pall. See Copyright Notice in luajit.h
  4. */
  5. #define lj_err_c
  6. #define LUA_CORE
  7. #include "lj_obj.h"
  8. #include "lj_err.h"
  9. #include "lj_debug.h"
  10. #include "lj_str.h"
  11. #include "lj_func.h"
  12. #include "lj_state.h"
  13. #include "lj_frame.h"
  14. #include "lj_ff.h"
  15. #include "lj_trace.h"
  16. #include "lj_vm.h"
  17. #include "lj_strfmt.h"
  18. /*
  19. ** LuaJIT can either use internal or external frame unwinding:
  20. **
  21. ** - Internal frame unwinding (INT) is free-standing and doesn't require
  22. ** any OS or library support.
  23. **
  24. ** - External frame unwinding (EXT) uses the system-provided unwind handler.
  25. **
  26. ** Pros and Cons:
  27. **
  28. ** - EXT requires unwind tables for *all* functions on the C stack between
  29. ** the pcall/catch and the error/throw. C modules used by Lua code can
  30. ** throw errors, so these need to have unwind tables, too. Transitively
  31. ** this applies to all system libraries used by C modules -- at least
  32. ** when they have callbacks which may throw an error.
  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. ** - INT needs to save *all* callee-saved registers when entering the
  38. ** interpreter. EXT only needs to save those actually used inside the
  39. ** interpreter. JIT-compiled code may need to save some more.
  40. **
  41. ** - EXT provides full interoperability with C++ exceptions. You can throw
  42. ** Lua errors or C++ exceptions through a mix of Lua frames and C++ frames.
  43. ** C++ destructors are called as needed. C++ exceptions caught by pcall
  44. ** are converted to the string "C++ exception". Lua errors can be caught
  45. ** with catch (...) in C++.
  46. **
  47. ** - INT has only limited support for automatically catching C++ exceptions
  48. ** on POSIX systems using DWARF2 stack unwinding. Other systems may use
  49. ** the wrapper function feature. Lua errors thrown through C++ frames
  50. ** cannot be caught by C++ code and C++ destructors are not run.
  51. **
  52. ** - EXT can handle errors from internal helper functions that are called
  53. ** from JIT-compiled code (except for Windows/x86 and 32 bit ARM).
  54. ** INT has no choice but to call the panic handler, if this happens.
  55. ** Note: this is mainly relevant for out-of-memory errors.
  56. **
  57. ** EXT is the default on all systems where the toolchain produces unwind
  58. ** tables by default (*). This is hard-coded and/or detected in src/Makefile.
  59. ** You can thwart the detection with: TARGET_XCFLAGS=-DLUAJIT_UNWIND_INTERNAL
  60. **
  61. ** INT is the default on all other systems.
  62. **
  63. ** EXT can be manually enabled for toolchains that are able to produce
  64. ** conforming unwind tables:
  65. ** "TARGET_XCFLAGS=-funwind-tables -DLUAJIT_UNWIND_EXTERNAL"
  66. ** As explained above, *all* C code used directly or indirectly by LuaJIT
  67. ** must be compiled with -funwind-tables (or -fexceptions). C++ code must
  68. ** *not* be compiled with -fno-exceptions.
  69. **
  70. ** If you're unsure whether error handling inside the VM works correctly,
  71. ** try running this and check whether it prints "OK":
  72. **
  73. ** luajit -e "print(select(2, load('OK')):match('OK'))"
  74. **
  75. ** (*) Originally, toolchains only generated unwind tables for C++ code. For
  76. ** interoperability reasons, this can be manually enabled for plain C code,
  77. ** too (with -funwind-tables). With the introduction of the x64 architecture,
  78. ** the corresponding POSIX and Windows ABIs mandated unwind tables for all
  79. ** code. Over the following years most desktop and server platforms have
  80. ** enabled unwind tables by default on all architectures. OTOH mobile and
  81. ** embedded platforms do not consistently mandate unwind tables.
  82. */
  83. /* -- Error messages ------------------------------------------------------ */
  84. /* Error message strings. */
  85. LJ_DATADEF const char *lj_err_allmsg =
  86. #define ERRDEF(name, msg) msg "\0"
  87. #include "lj_errmsg.h"
  88. ;
  89. /* -- Internal frame unwinding -------------------------------------------- */
  90. /* Unwind Lua stack and move error message to new top. */
  91. LJ_NOINLINE static void unwindstack(lua_State *L, TValue *top)
  92. {
  93. lj_func_closeuv(L, top);
  94. if (top < L->top-1) {
  95. copyTV(L, top, L->top-1);
  96. L->top = top+1;
  97. }
  98. lj_state_relimitstack(L);
  99. }
  100. /* Unwind until stop frame. Optionally cleanup frames. */
  101. static void *err_unwind(lua_State *L, void *stopcf, int errcode)
  102. {
  103. TValue *frame = L->base-1;
  104. void *cf = L->cframe;
  105. while (cf) {
  106. int32_t nres = cframe_nres(cframe_raw(cf));
  107. if (nres < 0) { /* C frame without Lua frame? */
  108. TValue *top = restorestack(L, -nres);
  109. if (frame < top) { /* Frame reached? */
  110. if (errcode) {
  111. L->base = frame+1;
  112. L->cframe = cframe_prev(cf);
  113. unwindstack(L, top);
  114. }
  115. return cf;
  116. }
  117. }
  118. if (frame <= tvref(L->stack)+LJ_FR2)
  119. break;
  120. switch (frame_typep(frame)) {
  121. case FRAME_LUA: /* Lua frame. */
  122. case FRAME_LUAP:
  123. frame = frame_prevl(frame);
  124. break;
  125. case FRAME_C: /* C frame. */
  126. unwind_c:
  127. #if LJ_UNWIND_EXT
  128. if (errcode) {
  129. L->base = frame_prevd(frame) + 1;
  130. L->cframe = cframe_prev(cf);
  131. unwindstack(L, frame - LJ_FR2);
  132. } else if (cf != stopcf) {
  133. cf = cframe_prev(cf);
  134. frame = frame_prevd(frame);
  135. break;
  136. }
  137. return NULL; /* Continue unwinding. */
  138. #else
  139. UNUSED(stopcf);
  140. cf = cframe_prev(cf);
  141. frame = frame_prevd(frame);
  142. break;
  143. #endif
  144. case FRAME_CP: /* Protected C frame. */
  145. if (cframe_canyield(cf)) { /* Resume? */
  146. if (errcode) {
  147. hook_leave(G(L)); /* Assumes nobody uses coroutines inside hooks. */
  148. L->cframe = NULL;
  149. L->status = (uint8_t)errcode;
  150. }
  151. return cf;
  152. }
  153. if (errcode) {
  154. L->base = frame_prevd(frame) + 1;
  155. L->cframe = cframe_prev(cf);
  156. unwindstack(L, frame - LJ_FR2);
  157. }
  158. return cf;
  159. case FRAME_CONT: /* Continuation frame. */
  160. if (frame_iscont_fficb(frame))
  161. goto unwind_c;
  162. /* fallthrough */
  163. case FRAME_VARG: /* Vararg frame. */
  164. frame = frame_prevd(frame);
  165. break;
  166. case FRAME_PCALL: /* FF pcall() frame. */
  167. case FRAME_PCALLH: /* FF pcall() frame inside hook. */
  168. if (errcode) {
  169. if (errcode == LUA_YIELD) {
  170. frame = frame_prevd(frame);
  171. break;
  172. }
  173. if (frame_typep(frame) == FRAME_PCALL)
  174. hook_leave(G(L));
  175. L->base = frame_prevd(frame) + 1;
  176. L->cframe = cf;
  177. unwindstack(L, L->base);
  178. }
  179. return (void *)((intptr_t)cf | CFRAME_UNWIND_FF);
  180. }
  181. }
  182. /* No C frame. */
  183. if (errcode) {
  184. L->base = tvref(L->stack)+1+LJ_FR2;
  185. L->cframe = NULL;
  186. unwindstack(L, L->base);
  187. if (G(L)->panic)
  188. G(L)->panic(L);
  189. exit(EXIT_FAILURE);
  190. }
  191. return L; /* Anything non-NULL will do. */
  192. }
  193. /* -- External frame unwinding -------------------------------------------- */
  194. #if LJ_ABI_WIN
  195. /*
  196. ** Someone in Redmond owes me several days of my life. A lot of this is
  197. ** undocumented or just plain wrong on MSDN. Some of it can be gathered
  198. ** from 3rd party docs or must be found by trial-and-error. They really
  199. ** don't want you to write your own language-specific exception handler
  200. ** or to interact gracefully with MSVC. :-(
  201. **
  202. ** Apparently MSVC doesn't call C++ destructors for foreign exceptions
  203. ** unless you compile your C++ code with /EHa. Unfortunately this means
  204. ** catch (...) also catches things like access violations. The use of
  205. ** _set_se_translator doesn't really help, because it requires /EHa, too.
  206. */
  207. #define WIN32_LEAN_AND_MEAN
  208. #include <windows.h>
  209. #if LJ_TARGET_X86
  210. typedef void *UndocumentedDispatcherContext; /* Unused on x86. */
  211. #else
  212. /* Taken from: http://www.nynaeve.net/?p=99 */
  213. typedef struct UndocumentedDispatcherContext {
  214. ULONG64 ControlPc;
  215. ULONG64 ImageBase;
  216. PRUNTIME_FUNCTION FunctionEntry;
  217. ULONG64 EstablisherFrame;
  218. ULONG64 TargetIp;
  219. PCONTEXT ContextRecord;
  220. void (*LanguageHandler)(void);
  221. PVOID HandlerData;
  222. PUNWIND_HISTORY_TABLE HistoryTable;
  223. ULONG ScopeIndex;
  224. ULONG Fill0;
  225. } UndocumentedDispatcherContext;
  226. #endif
  227. /* Another wild guess. */
  228. extern void __DestructExceptionObject(EXCEPTION_RECORD *rec, int nothrow);
  229. #if LJ_TARGET_X64 && defined(MINGW_SDK_INIT)
  230. /* Workaround for broken MinGW64 declaration. */
  231. VOID RtlUnwindEx_FIXED(PVOID,PVOID,PVOID,PVOID,PVOID,PVOID) asm("RtlUnwindEx");
  232. #define RtlUnwindEx RtlUnwindEx_FIXED
  233. #endif
  234. #define LJ_MSVC_EXCODE ((DWORD)0xe06d7363)
  235. #define LJ_GCC_EXCODE ((DWORD)0x20474343)
  236. #define LJ_EXCODE ((DWORD)0xe24c4a00)
  237. #define LJ_EXCODE_MAKE(c) (LJ_EXCODE | (DWORD)(c))
  238. #define LJ_EXCODE_CHECK(cl) (((cl) ^ LJ_EXCODE) <= 0xff)
  239. #define LJ_EXCODE_ERRCODE(cl) ((int)((cl) & 0xff))
  240. /* Windows exception handler for interpreter frame. */
  241. LJ_FUNCA int lj_err_unwind_win(EXCEPTION_RECORD *rec,
  242. void *f, CONTEXT *ctx, UndocumentedDispatcherContext *dispatch)
  243. {
  244. #if LJ_TARGET_X86
  245. void *cf = (char *)f - CFRAME_OFS_SEH;
  246. #else
  247. void *cf = f;
  248. #endif
  249. lua_State *L = cframe_L(cf);
  250. int errcode = LJ_EXCODE_CHECK(rec->ExceptionCode) ?
  251. LJ_EXCODE_ERRCODE(rec->ExceptionCode) : LUA_ERRRUN;
  252. if ((rec->ExceptionFlags & 6)) { /* EH_UNWINDING|EH_EXIT_UNWIND */
  253. /* Unwind internal frames. */
  254. err_unwind(L, cf, errcode);
  255. } else {
  256. void *cf2 = err_unwind(L, cf, 0);
  257. if (cf2) { /* We catch it, so start unwinding the upper frames. */
  258. if (rec->ExceptionCode == LJ_MSVC_EXCODE ||
  259. rec->ExceptionCode == LJ_GCC_EXCODE) {
  260. #if !LJ_TARGET_CYGWIN
  261. __DestructExceptionObject(rec, 1);
  262. #endif
  263. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
  264. } else if (!LJ_EXCODE_CHECK(rec->ExceptionCode)) {
  265. /* Don't catch access violations etc. */
  266. return 1; /* ExceptionContinueSearch */
  267. }
  268. #if LJ_TARGET_X86
  269. UNUSED(ctx);
  270. UNUSED(dispatch);
  271. /* Call all handlers for all lower C frames (including ourselves) again
  272. ** with EH_UNWINDING set. Then call the specified function, passing cf
  273. ** and errcode.
  274. */
  275. lj_vm_rtlunwind(cf, (void *)rec,
  276. (cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
  277. (void *)lj_vm_unwind_ff : (void *)lj_vm_unwind_c, errcode);
  278. /* lj_vm_rtlunwind does not return. */
  279. #else
  280. /* Unwind the stack and call all handlers for all lower C frames
  281. ** (including ourselves) again with EH_UNWINDING set. Then set
  282. ** stack pointer = cf, result = errcode and jump to the specified target.
  283. */
  284. RtlUnwindEx(cf, (void *)((cframe_unwind_ff(cf2) && errcode != LUA_YIELD) ?
  285. lj_vm_unwind_ff_eh :
  286. lj_vm_unwind_c_eh),
  287. rec, (void *)(uintptr_t)errcode, ctx, dispatch->HistoryTable);
  288. /* RtlUnwindEx should never return. */
  289. #endif
  290. }
  291. }
  292. return 1; /* ExceptionContinueSearch */
  293. }
  294. #if LJ_UNWIND_JIT
  295. #if LJ_TARGET_X64
  296. #define CONTEXT_REG_PC Rip
  297. #elif LJ_TARGET_ARM64
  298. #define CONTEXT_REG_PC Pc
  299. #else
  300. #error "NYI: Windows arch-specific unwinder for JIT-compiled code"
  301. #endif
  302. /* Windows unwinder for JIT-compiled code. */
  303. static void err_unwind_win_jit(global_State *g, int errcode)
  304. {
  305. CONTEXT ctx;
  306. UNWIND_HISTORY_TABLE hist;
  307. memset(&hist, 0, sizeof(hist));
  308. RtlCaptureContext(&ctx);
  309. while (1) {
  310. uintptr_t frame, base, addr = ctx.CONTEXT_REG_PC;
  311. void *hdata;
  312. PRUNTIME_FUNCTION func = RtlLookupFunctionEntry(addr, &base, &hist);
  313. if (!func) { /* Found frame without .pdata: must be JIT-compiled code. */
  314. ExitNo exitno;
  315. uintptr_t stub = lj_trace_unwind(G2J(g), addr - sizeof(MCode), &exitno);
  316. if (stub) { /* Jump to side exit to unwind the trace. */
  317. ctx.CONTEXT_REG_PC = stub;
  318. G2J(g)->exitcode = errcode;
  319. RtlRestoreContext(&ctx, NULL); /* Does not return. */
  320. }
  321. break;
  322. }
  323. RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, addr, func,
  324. &ctx, &hdata, &frame, NULL);
  325. if (!addr) break;
  326. }
  327. /* Unwinding failed, if we end up here. */
  328. }
  329. #endif
  330. /* Raise Windows exception. */
  331. static void err_raise_ext(global_State *g, int errcode)
  332. {
  333. #if LJ_UNWIND_JIT
  334. if (tvref(g->jit_base)) {
  335. err_unwind_win_jit(g, errcode);
  336. return; /* Unwinding failed. */
  337. }
  338. #elif LJ_HASJIT
  339. /* Cannot catch on-trace errors for Windows/x86 SEH. Unwind to interpreter. */
  340. setmref(g->jit_base, NULL);
  341. #endif
  342. UNUSED(g);
  343. RaiseException(LJ_EXCODE_MAKE(errcode), 1 /* EH_NONCONTINUABLE */, 0, NULL);
  344. }
  345. #elif !LJ_NO_UNWIND && (defined(__GNUC__) || defined(__clang__))
  346. /*
  347. ** We have to use our own definitions instead of the mandatory (!) unwind.h,
  348. ** since various OS, distros and compilers mess up the header installation.
  349. */
  350. typedef struct _Unwind_Context _Unwind_Context;
  351. #define _URC_OK 0
  352. #define _URC_FATAL_PHASE2_ERROR 2
  353. #define _URC_FATAL_PHASE1_ERROR 3
  354. #define _URC_HANDLER_FOUND 6
  355. #define _URC_INSTALL_CONTEXT 7
  356. #define _URC_CONTINUE_UNWIND 8
  357. #define _URC_FAILURE 9
  358. #define LJ_UEXCLASS 0x4c55414a49543200ULL /* LUAJIT2\0 */
  359. #define LJ_UEXCLASS_MAKE(c) (LJ_UEXCLASS | (uint64_t)(c))
  360. #define LJ_UEXCLASS_CHECK(cl) (((cl) ^ LJ_UEXCLASS) <= 0xff)
  361. #define LJ_UEXCLASS_ERRCODE(cl) ((int)((cl) & 0xff))
  362. #if !LJ_TARGET_ARM
  363. typedef struct _Unwind_Exception
  364. {
  365. uint64_t exclass;
  366. void (*excleanup)(int, struct _Unwind_Exception *);
  367. uintptr_t p1, p2;
  368. } __attribute__((__aligned__)) _Unwind_Exception;
  369. #define UNWIND_EXCEPTION_TYPE _Unwind_Exception
  370. extern uintptr_t _Unwind_GetCFA(_Unwind_Context *);
  371. extern void _Unwind_SetGR(_Unwind_Context *, int, uintptr_t);
  372. extern uintptr_t _Unwind_GetIP(_Unwind_Context *);
  373. extern void _Unwind_SetIP(_Unwind_Context *, uintptr_t);
  374. extern void _Unwind_DeleteException(_Unwind_Exception *);
  375. extern int _Unwind_RaiseException(_Unwind_Exception *);
  376. #define _UA_SEARCH_PHASE 1
  377. #define _UA_CLEANUP_PHASE 2
  378. #define _UA_HANDLER_FRAME 4
  379. #define _UA_FORCE_UNWIND 8
  380. /* DWARF2 personality handler referenced from interpreter .eh_frame. */
  381. LJ_FUNCA int lj_err_unwind_dwarf(int version, int actions,
  382. uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
  383. {
  384. void *cf;
  385. lua_State *L;
  386. if (version != 1)
  387. return _URC_FATAL_PHASE1_ERROR;
  388. cf = (void *)_Unwind_GetCFA(ctx);
  389. L = cframe_L(cf);
  390. if ((actions & _UA_SEARCH_PHASE)) {
  391. #if LJ_UNWIND_EXT
  392. if (err_unwind(L, cf, 0) == NULL)
  393. return _URC_CONTINUE_UNWIND;
  394. #endif
  395. if (!LJ_UEXCLASS_CHECK(uexclass)) {
  396. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
  397. }
  398. return _URC_HANDLER_FOUND;
  399. }
  400. if ((actions & _UA_CLEANUP_PHASE)) {
  401. int errcode;
  402. if (LJ_UEXCLASS_CHECK(uexclass)) {
  403. errcode = LJ_UEXCLASS_ERRCODE(uexclass);
  404. } else {
  405. if ((actions & _UA_HANDLER_FRAME))
  406. _Unwind_DeleteException(uex);
  407. errcode = LUA_ERRRUN;
  408. }
  409. #if LJ_UNWIND_EXT
  410. cf = err_unwind(L, cf, errcode);
  411. if ((actions & _UA_FORCE_UNWIND)) {
  412. return _URC_CONTINUE_UNWIND;
  413. } else if (cf) {
  414. _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
  415. _Unwind_SetIP(ctx, (uintptr_t)(cframe_unwind_ff(cf) ?
  416. lj_vm_unwind_ff_eh :
  417. lj_vm_unwind_c_eh));
  418. return _URC_INSTALL_CONTEXT;
  419. }
  420. #if LJ_TARGET_X86ORX64
  421. else if ((actions & _UA_HANDLER_FRAME)) {
  422. /* Workaround for ancient libgcc bug. Still present in RHEL 5.5. :-/
  423. ** Real fix: http://gcc.gnu.org/viewcvs/trunk/gcc/unwind-dw2.c?r1=121165&r2=124837&pathrev=153877&diff_format=h
  424. */
  425. _Unwind_SetGR(ctx, LJ_TARGET_EHRETREG, errcode);
  426. _Unwind_SetIP(ctx, (uintptr_t)lj_vm_unwind_rethrow);
  427. return _URC_INSTALL_CONTEXT;
  428. }
  429. #endif
  430. #else
  431. /* This is not the proper way to escape from the unwinder. We get away with
  432. ** it on non-x64 because the interpreter restores all callee-saved regs.
  433. */
  434. lj_err_throw(L, errcode);
  435. #if LJ_TARGET_X64
  436. #error "Broken build system -- only use the provided Makefiles!"
  437. #endif
  438. #endif
  439. }
  440. return _URC_CONTINUE_UNWIND;
  441. }
  442. #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
  443. struct dwarf_eh_bases { void *tbase, *dbase, *func; };
  444. extern const void *_Unwind_Find_FDE(void *pc, struct dwarf_eh_bases *bases);
  445. /* Verify that external error handling actually has a chance to work. */
  446. void lj_err_verify(void)
  447. {
  448. struct dwarf_eh_bases ehb;
  449. lj_assertX(_Unwind_Find_FDE((void *)lj_err_throw, &ehb), "broken build: external frame unwinding enabled, but missing -funwind-tables");
  450. /* Check disabled, because of broken Fedora/ARM64. See #722.
  451. lj_assertX(_Unwind_Find_FDE((void *)_Unwind_RaiseException, &ehb), "broken build: external frame unwinding enabled, but system libraries have no unwind tables");
  452. */
  453. }
  454. #endif
  455. #if LJ_UNWIND_JIT
  456. /* DWARF2 personality handler for JIT-compiled code. */
  457. static int err_unwind_jit(int version, int actions,
  458. uint64_t uexclass, _Unwind_Exception *uex, _Unwind_Context *ctx)
  459. {
  460. /* NYI: FFI C++ exception interoperability. */
  461. if (version != 1 || !LJ_UEXCLASS_CHECK(uexclass))
  462. return _URC_FATAL_PHASE1_ERROR;
  463. if ((actions & _UA_SEARCH_PHASE)) {
  464. return _URC_HANDLER_FOUND;
  465. }
  466. if ((actions & _UA_CLEANUP_PHASE)) {
  467. global_State *g = *(global_State **)(uex+1);
  468. ExitNo exitno;
  469. uintptr_t addr = _Unwind_GetIP(ctx); /* Return address _after_ call. */
  470. uintptr_t stub = lj_trace_unwind(G2J(g), addr - sizeof(MCode), &exitno);
  471. lj_assertG(tvref(g->jit_base), "unexpected throw across mcode frame");
  472. if (stub) { /* Jump to side exit to unwind the trace. */
  473. G2J(g)->exitcode = LJ_UEXCLASS_ERRCODE(uexclass);
  474. #ifdef LJ_TARGET_MIPS
  475. _Unwind_SetGR(ctx, 4, stub);
  476. _Unwind_SetGR(ctx, 5, exitno);
  477. _Unwind_SetIP(ctx, (uintptr_t)(void *)lj_vm_unwind_stub);
  478. #else
  479. _Unwind_SetIP(ctx, stub);
  480. #endif
  481. return _URC_INSTALL_CONTEXT;
  482. }
  483. return _URC_FATAL_PHASE2_ERROR;
  484. }
  485. return _URC_FATAL_PHASE1_ERROR;
  486. }
  487. /* DWARF2 template frame info for JIT-compiled code.
  488. **
  489. ** After copying the template to the start of the mcode segment,
  490. ** the frame handler function and the code size is patched.
  491. ** The frame handler always installs a new context to jump to the exit,
  492. ** so don't bother to add any unwind opcodes.
  493. */
  494. static const uint8_t err_frame_jit_template[] = {
  495. #if LJ_BE
  496. 0,0,0,
  497. #endif
  498. LJ_64 ? 0x1c : 0x14, /* CIE length. */
  499. #if LJ_LE
  500. 0,0,0,
  501. #endif
  502. 0,0,0,0, 1, 'z','P','R',0, /* CIE mark, CIE version, augmentation. */
  503. 1, LJ_64 ? 0x78 : 0x7c, LJ_TARGET_EHRAREG, /* Code/data align, RA. */
  504. #if LJ_64
  505. 10, 0, 0,0,0,0,0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
  506. 0,0,0,0,0, /* Alignment. */
  507. #else
  508. 6, 0, 0,0,0,0, 0x1b, /* Aug. data ABS handler, PCREL|SDATA4 code. */
  509. 0, /* Alignment. */
  510. #endif
  511. #if LJ_BE
  512. 0,0,0,
  513. #endif
  514. LJ_64 ? 0x14 : 0x10, /* FDE length. */
  515. 0,0,0,
  516. LJ_64 ? 0x24 : 0x1c, /* CIE offset. */
  517. 0,0,0,
  518. LJ_64 ? 0x14 : 0x10, /* Code offset. After Final FDE. */
  519. #if LJ_LE
  520. 0,0,0,
  521. #endif
  522. 0,0,0,0, 0, 0,0,0, /* Code size, augmentation length, alignment. */
  523. #if LJ_64
  524. 0,0,0,0, /* Alignment. */
  525. #endif
  526. 0,0,0,0 /* Final FDE. */
  527. };
  528. #define ERR_FRAME_JIT_OFS_HANDLER 0x12
  529. #define ERR_FRAME_JIT_OFS_FDE (LJ_64 ? 0x20 : 0x18)
  530. #define ERR_FRAME_JIT_OFS_CODE_SIZE (LJ_64 ? 0x2c : 0x24)
  531. #if LJ_TARGET_OSX
  532. #define ERR_FRAME_JIT_OFS_REGISTER ERR_FRAME_JIT_OFS_FDE
  533. #else
  534. #define ERR_FRAME_JIT_OFS_REGISTER 0
  535. #endif
  536. extern void __register_frame(const void *);
  537. extern void __deregister_frame(const void *);
  538. uint8_t *lj_err_register_mcode(void *base, size_t sz, uint8_t *info)
  539. {
  540. void **handler;
  541. memcpy(info, err_frame_jit_template, sizeof(err_frame_jit_template));
  542. handler = (void *)err_unwind_jit;
  543. memcpy(info + ERR_FRAME_JIT_OFS_HANDLER, &handler, sizeof(handler));
  544. *(uint32_t *)(info + ERR_FRAME_JIT_OFS_CODE_SIZE) =
  545. (uint32_t)(sz - sizeof(err_frame_jit_template) - (info - (uint8_t *)base));
  546. __register_frame(info + ERR_FRAME_JIT_OFS_REGISTER);
  547. #ifdef LUA_USE_ASSERT
  548. {
  549. struct dwarf_eh_bases ehb;
  550. lj_assertX(_Unwind_Find_FDE(info + sizeof(err_frame_jit_template)+1, &ehb),
  551. "bad JIT unwind table registration");
  552. }
  553. #endif
  554. return info + sizeof(err_frame_jit_template);
  555. }
  556. void lj_err_deregister_mcode(void *base, size_t sz, uint8_t *info)
  557. {
  558. UNUSED(base); UNUSED(sz);
  559. __deregister_frame(info + ERR_FRAME_JIT_OFS_REGISTER);
  560. }
  561. #endif
  562. #else /* LJ_TARGET_ARM */
  563. #define _US_VIRTUAL_UNWIND_FRAME 0
  564. #define _US_UNWIND_FRAME_STARTING 1
  565. #define _US_ACTION_MASK 3
  566. #define _US_FORCE_UNWIND 8
  567. typedef struct _Unwind_Control_Block _Unwind_Control_Block;
  568. #define UNWIND_EXCEPTION_TYPE _Unwind_Control_Block
  569. struct _Unwind_Control_Block {
  570. uint64_t exclass;
  571. uint32_t misc[20];
  572. };
  573. extern int _Unwind_RaiseException(_Unwind_Control_Block *);
  574. extern int __gnu_unwind_frame(_Unwind_Control_Block *, _Unwind_Context *);
  575. extern int _Unwind_VRS_Set(_Unwind_Context *, int, uint32_t, int, void *);
  576. extern int _Unwind_VRS_Get(_Unwind_Context *, int, uint32_t, int, void *);
  577. static inline uint32_t _Unwind_GetGR(_Unwind_Context *ctx, int r)
  578. {
  579. uint32_t v;
  580. _Unwind_VRS_Get(ctx, 0, r, 0, &v);
  581. return v;
  582. }
  583. static inline void _Unwind_SetGR(_Unwind_Context *ctx, int r, uint32_t v)
  584. {
  585. _Unwind_VRS_Set(ctx, 0, r, 0, &v);
  586. }
  587. extern void lj_vm_unwind_ext(void);
  588. /* ARM unwinder personality handler referenced from interpreter .ARM.extab. */
  589. LJ_FUNCA int lj_err_unwind_arm(int state, _Unwind_Control_Block *ucb,
  590. _Unwind_Context *ctx)
  591. {
  592. void *cf = (void *)_Unwind_GetGR(ctx, 13);
  593. lua_State *L = cframe_L(cf);
  594. int errcode;
  595. switch ((state & _US_ACTION_MASK)) {
  596. case _US_VIRTUAL_UNWIND_FRAME:
  597. if ((state & _US_FORCE_UNWIND)) break;
  598. return _URC_HANDLER_FOUND;
  599. case _US_UNWIND_FRAME_STARTING:
  600. if (LJ_UEXCLASS_CHECK(ucb->exclass)) {
  601. errcode = LJ_UEXCLASS_ERRCODE(ucb->exclass);
  602. } else {
  603. errcode = LUA_ERRRUN;
  604. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRCPP));
  605. }
  606. cf = err_unwind(L, cf, errcode);
  607. if ((state & _US_FORCE_UNWIND) || cf == NULL) break;
  608. _Unwind_SetGR(ctx, 15, (uint32_t)lj_vm_unwind_ext);
  609. _Unwind_SetGR(ctx, 0, (uint32_t)ucb);
  610. _Unwind_SetGR(ctx, 1, (uint32_t)errcode);
  611. _Unwind_SetGR(ctx, 2, cframe_unwind_ff(cf) ?
  612. (uint32_t)lj_vm_unwind_ff_eh :
  613. (uint32_t)lj_vm_unwind_c_eh);
  614. return _URC_INSTALL_CONTEXT;
  615. default:
  616. return _URC_FAILURE;
  617. }
  618. if (__gnu_unwind_frame(ucb, ctx) != _URC_OK)
  619. return _URC_FAILURE;
  620. #ifdef LUA_USE_ASSERT
  621. /* We should never get here unless this is a forced unwind aka backtrace. */
  622. if (_Unwind_GetGR(ctx, 0) == 0xff33aa77) {
  623. _Unwind_SetGR(ctx, 0, 0xff33aa88);
  624. }
  625. #endif
  626. return _URC_CONTINUE_UNWIND;
  627. }
  628. #if LJ_UNWIND_EXT && defined(LUA_USE_ASSERT)
  629. typedef int (*_Unwind_Trace_Fn)(_Unwind_Context *, void *);
  630. extern int _Unwind_Backtrace(_Unwind_Trace_Fn, void *);
  631. static int err_verify_bt(_Unwind_Context *ctx, int *got)
  632. {
  633. if (_Unwind_GetGR(ctx, 0) == 0xff33aa88) { *got = 2; }
  634. else if (*got == 0) { *got = 1; _Unwind_SetGR(ctx, 0, 0xff33aa77); }
  635. return _URC_OK;
  636. }
  637. /* Verify that external error handling actually has a chance to work. */
  638. void lj_err_verify(void)
  639. {
  640. int got = 0;
  641. _Unwind_Backtrace((_Unwind_Trace_Fn)err_verify_bt, &got);
  642. lj_assertX(got == 2, "broken build: external frame unwinding enabled, but missing -funwind-tables");
  643. }
  644. #endif
  645. /*
  646. ** Note: LJ_UNWIND_JIT is not implemented for 32 bit ARM.
  647. **
  648. ** The quirky ARM unwind API doesn't have __register_frame().
  649. ** A potential workaround might involve _Unwind_Backtrace.
  650. ** But most 32 bit ARM targets don't qualify for LJ_UNWIND_EXT, anyway,
  651. ** since they are built without unwind tables by default.
  652. */
  653. #endif /* LJ_TARGET_ARM */
  654. #if LJ_UNWIND_EXT
  655. static __thread struct {
  656. UNWIND_EXCEPTION_TYPE ex;
  657. global_State *g;
  658. } static_uex;
  659. /* Raise external exception. */
  660. static void err_raise_ext(global_State *g, int errcode)
  661. {
  662. memset(&static_uex, 0, sizeof(static_uex));
  663. static_uex.ex.exclass = LJ_UEXCLASS_MAKE(errcode);
  664. static_uex.g = g;
  665. _Unwind_RaiseException(&static_uex.ex);
  666. }
  667. #endif
  668. #endif
  669. /* -- Error handling ------------------------------------------------------ */
  670. /* Throw error. Find catch frame, unwind stack and continue. */
  671. LJ_NOINLINE void LJ_FASTCALL lj_err_throw(lua_State *L, int errcode)
  672. {
  673. global_State *g = G(L);
  674. lj_trace_abort(g);
  675. L->status = LUA_OK;
  676. #if LJ_UNWIND_EXT
  677. err_raise_ext(g, errcode);
  678. /*
  679. ** A return from this function signals a corrupt C stack that cannot be
  680. ** unwound. We have no choice but to call the panic function and exit.
  681. **
  682. ** Usually this is caused by a C function without unwind information.
  683. ** This may happen if you've manually enabled LUAJIT_UNWIND_EXTERNAL
  684. ** and forgot to recompile *every* non-C++ file with -funwind-tables.
  685. */
  686. if (G(L)->panic)
  687. G(L)->panic(L);
  688. #else
  689. #if LJ_HASJIT
  690. setmref(g->jit_base, NULL);
  691. #endif
  692. {
  693. void *cf = err_unwind(L, NULL, errcode);
  694. if (cframe_unwind_ff(cf))
  695. lj_vm_unwind_ff(cframe_raw(cf));
  696. else
  697. lj_vm_unwind_c(cframe_raw(cf), errcode);
  698. }
  699. #endif
  700. exit(EXIT_FAILURE);
  701. }
  702. /* Return string object for error message. */
  703. LJ_NOINLINE GCstr *lj_err_str(lua_State *L, ErrMsg em)
  704. {
  705. return lj_str_newz(L, err2msg(em));
  706. }
  707. /* Out-of-memory error. */
  708. LJ_NOINLINE void lj_err_mem(lua_State *L)
  709. {
  710. if (L->status == LUA_ERRERR+1) /* Don't touch the stack during lua_open. */
  711. lj_vm_unwind_c(L->cframe, LUA_ERRMEM);
  712. setstrV(L, L->top++, lj_err_str(L, LJ_ERR_ERRMEM));
  713. lj_err_throw(L, LUA_ERRMEM);
  714. }
  715. /* Find error function for runtime errors. Requires an extra stack traversal. */
  716. static ptrdiff_t finderrfunc(lua_State *L)
  717. {
  718. cTValue *frame = L->base-1, *bot = tvref(L->stack)+LJ_FR2;
  719. void *cf = L->cframe;
  720. while (frame > bot && cf) {
  721. while (cframe_nres(cframe_raw(cf)) < 0) { /* cframe without frame? */
  722. if (frame >= restorestack(L, -cframe_nres(cf)))
  723. break;
  724. if (cframe_errfunc(cf) >= 0) /* Error handler not inherited (-1)? */
  725. return cframe_errfunc(cf);
  726. cf = cframe_prev(cf); /* Else unwind cframe and continue searching. */
  727. if (cf == NULL)
  728. return 0;
  729. }
  730. switch (frame_typep(frame)) {
  731. case FRAME_LUA:
  732. case FRAME_LUAP:
  733. frame = frame_prevl(frame);
  734. break;
  735. case FRAME_C:
  736. cf = cframe_prev(cf);
  737. /* fallthrough */
  738. case FRAME_VARG:
  739. frame = frame_prevd(frame);
  740. break;
  741. case FRAME_CONT:
  742. if (frame_iscont_fficb(frame))
  743. cf = cframe_prev(cf);
  744. frame = frame_prevd(frame);
  745. break;
  746. case FRAME_CP:
  747. if (cframe_canyield(cf)) return 0;
  748. if (cframe_errfunc(cf) >= 0)
  749. return cframe_errfunc(cf);
  750. cf = cframe_prev(cf);
  751. frame = frame_prevd(frame);
  752. break;
  753. case FRAME_PCALL:
  754. case FRAME_PCALLH:
  755. if (frame_func(frame_prevd(frame))->c.ffid == FF_xpcall)
  756. return savestack(L, frame_prevd(frame)+1); /* xpcall's errorfunc. */
  757. return 0;
  758. default:
  759. lj_assertL(0, "bad frame type");
  760. return 0;
  761. }
  762. }
  763. return 0;
  764. }
  765. /* Runtime error. */
  766. LJ_NOINLINE void LJ_FASTCALL lj_err_run(lua_State *L)
  767. {
  768. ptrdiff_t ef = (LJ_HASJIT && tvref(G(L)->jit_base)) ? 0 : finderrfunc(L);
  769. if (ef) {
  770. TValue *errfunc = restorestack(L, ef);
  771. TValue *top = L->top;
  772. lj_trace_abort(G(L));
  773. if (!tvisfunc(errfunc) || L->status == LUA_ERRERR) {
  774. setstrV(L, top-1, lj_err_str(L, LJ_ERR_ERRERR));
  775. lj_err_throw(L, LUA_ERRERR);
  776. }
  777. L->status = LUA_ERRERR;
  778. copyTV(L, top+LJ_FR2, top-1);
  779. copyTV(L, top-1, errfunc);
  780. if (LJ_FR2) setnilV(top++);
  781. L->top = top+1;
  782. lj_vm_call(L, top, 1+1); /* Stack: |errfunc|msg| -> |msg| */
  783. }
  784. lj_err_throw(L, LUA_ERRRUN);
  785. }
  786. #if LJ_HASJIT
  787. LJ_NOINLINE void LJ_FASTCALL lj_err_trace(lua_State *L, int errcode)
  788. {
  789. if (errcode == LUA_ERRRUN)
  790. lj_err_run(L);
  791. else
  792. lj_err_throw(L, errcode);
  793. }
  794. #endif
  795. /* Formatted runtime error message. */
  796. LJ_NORET LJ_NOINLINE static void err_msgv(lua_State *L, ErrMsg em, ...)
  797. {
  798. const char *msg;
  799. va_list argp;
  800. va_start(argp, em);
  801. if (curr_funcisL(L)) L->top = curr_topL(L);
  802. msg = lj_strfmt_pushvf(L, err2msg(em), argp);
  803. va_end(argp);
  804. lj_debug_addloc(L, msg, L->base-1, NULL);
  805. lj_err_run(L);
  806. }
  807. /* Non-vararg variant for better calling conventions. */
  808. LJ_NOINLINE void lj_err_msg(lua_State *L, ErrMsg em)
  809. {
  810. err_msgv(L, em);
  811. }
  812. /* Lexer error. */
  813. LJ_NOINLINE void lj_err_lex(lua_State *L, GCstr *src, const char *tok,
  814. BCLine line, ErrMsg em, va_list argp)
  815. {
  816. char buff[LUA_IDSIZE];
  817. const char *msg;
  818. lj_debug_shortname(buff, src, line);
  819. msg = lj_strfmt_pushvf(L, err2msg(em), argp);
  820. msg = lj_strfmt_pushf(L, "%s:%d: %s", buff, line, msg);
  821. if (tok)
  822. lj_strfmt_pushf(L, err2msg(LJ_ERR_XNEAR), msg, tok);
  823. lj_err_throw(L, LUA_ERRSYNTAX);
  824. }
  825. /* Typecheck error for operands. */
  826. LJ_NOINLINE void lj_err_optype(lua_State *L, cTValue *o, ErrMsg opm)
  827. {
  828. const char *tname = lj_typename(o);
  829. const char *opname = err2msg(opm);
  830. if (curr_funcisL(L)) {
  831. GCproto *pt = curr_proto(L);
  832. const BCIns *pc = cframe_Lpc(L) - 1;
  833. const char *oname = NULL;
  834. const char *kind = lj_debug_slotname(pt, pc, (BCReg)(o-L->base), &oname);
  835. if (kind)
  836. err_msgv(L, LJ_ERR_BADOPRT, opname, kind, oname, tname);
  837. }
  838. err_msgv(L, LJ_ERR_BADOPRV, opname, tname);
  839. }
  840. /* Typecheck error for ordered comparisons. */
  841. LJ_NOINLINE void lj_err_comp(lua_State *L, cTValue *o1, cTValue *o2)
  842. {
  843. const char *t1 = lj_typename(o1);
  844. const char *t2 = lj_typename(o2);
  845. err_msgv(L, t1 == t2 ? LJ_ERR_BADCMPV : LJ_ERR_BADCMPT, t1, t2);
  846. /* This assumes the two "boolean" entries are commoned by the C compiler. */
  847. }
  848. /* Typecheck error for __call. */
  849. LJ_NOINLINE void lj_err_optype_call(lua_State *L, TValue *o)
  850. {
  851. /* Gross hack if lua_[p]call or pcall/xpcall fail for a non-callable object:
  852. ** L->base still points to the caller. So add a dummy frame with L instead
  853. ** of a function. See lua_getstack().
  854. */
  855. const BCIns *pc = cframe_Lpc(L);
  856. if (((ptrdiff_t)pc & FRAME_TYPE) != FRAME_LUA) {
  857. const char *tname = lj_typename(o);
  858. setframe_gc(o, obj2gco(L), LJ_TTHREAD);
  859. if (LJ_FR2) o++;
  860. setframe_pc(o, pc);
  861. L->top = L->base = o+1;
  862. err_msgv(L, LJ_ERR_BADCALL, tname);
  863. }
  864. lj_err_optype(L, o, LJ_ERR_OPCALL);
  865. }
  866. /* Error in context of caller. */
  867. LJ_NOINLINE void lj_err_callermsg(lua_State *L, const char *msg)
  868. {
  869. TValue *frame = NULL, *pframe = NULL;
  870. if (!(LJ_HASJIT && tvref(G(L)->jit_base))) {
  871. frame = L->base-1;
  872. if (frame_islua(frame)) {
  873. pframe = frame_prevl(frame);
  874. } else if (frame_iscont(frame)) {
  875. if (frame_iscont_fficb(frame)) {
  876. pframe = frame;
  877. frame = NULL;
  878. } else {
  879. pframe = frame_prevd(frame);
  880. #if LJ_HASFFI
  881. /* Remove frame for FFI metamethods. */
  882. if (frame_func(frame)->c.ffid >= FF_ffi_meta___index &&
  883. frame_func(frame)->c.ffid <= FF_ffi_meta___tostring) {
  884. L->base = pframe+1;
  885. L->top = frame;
  886. setcframe_pc(cframe_raw(L->cframe), frame_contpc(frame));
  887. }
  888. #endif
  889. }
  890. }
  891. }
  892. lj_debug_addloc(L, msg, pframe, frame);
  893. lj_err_run(L);
  894. }
  895. /* Formatted error in context of caller. */
  896. LJ_NOINLINE void lj_err_callerv(lua_State *L, ErrMsg em, ...)
  897. {
  898. const char *msg;
  899. va_list argp;
  900. va_start(argp, em);
  901. msg = lj_strfmt_pushvf(L, err2msg(em), argp);
  902. va_end(argp);
  903. lj_err_callermsg(L, msg);
  904. }
  905. /* Error in context of caller. */
  906. LJ_NOINLINE void lj_err_caller(lua_State *L, ErrMsg em)
  907. {
  908. lj_err_callermsg(L, err2msg(em));
  909. }
  910. /* Argument error message. */
  911. LJ_NORET LJ_NOINLINE static void err_argmsg(lua_State *L, int narg,
  912. const char *msg)
  913. {
  914. const char *fname = "?";
  915. const char *ftype = lj_debug_funcname(L, L->base - 1, &fname);
  916. if (narg < 0 && narg > LUA_REGISTRYINDEX)
  917. narg = (int)(L->top - L->base) + narg + 1;
  918. if (ftype && ftype[3] == 'h' && --narg == 0) /* Check for "method". */
  919. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADSELF), fname, msg);
  920. else
  921. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADARG), narg, fname, msg);
  922. lj_err_callermsg(L, msg);
  923. }
  924. /* Formatted argument error. */
  925. LJ_NOINLINE void lj_err_argv(lua_State *L, int narg, ErrMsg em, ...)
  926. {
  927. const char *msg;
  928. va_list argp;
  929. va_start(argp, em);
  930. msg = lj_strfmt_pushvf(L, err2msg(em), argp);
  931. va_end(argp);
  932. err_argmsg(L, narg, msg);
  933. }
  934. /* Argument error. */
  935. LJ_NOINLINE void lj_err_arg(lua_State *L, int narg, ErrMsg em)
  936. {
  937. err_argmsg(L, narg, err2msg(em));
  938. }
  939. /* Typecheck error for arguments. */
  940. LJ_NOINLINE void lj_err_argtype(lua_State *L, int narg, const char *xname)
  941. {
  942. const char *tname, *msg;
  943. if (narg <= LUA_REGISTRYINDEX) {
  944. if (narg >= LUA_GLOBALSINDEX) {
  945. tname = lj_obj_itypename[~LJ_TTAB];
  946. } else {
  947. GCfunc *fn = curr_func(L);
  948. int idx = LUA_GLOBALSINDEX - narg;
  949. if (idx <= fn->c.nupvalues)
  950. tname = lj_typename(&fn->c.upvalue[idx-1]);
  951. else
  952. tname = lj_obj_typename[0];
  953. }
  954. } else {
  955. TValue *o = narg < 0 ? L->top + narg : L->base + narg-1;
  956. tname = o < L->top ? lj_typename(o) : lj_obj_typename[0];
  957. }
  958. msg = lj_strfmt_pushf(L, err2msg(LJ_ERR_BADTYPE), xname, tname);
  959. err_argmsg(L, narg, msg);
  960. }
  961. /* Typecheck error for arguments. */
  962. LJ_NOINLINE void lj_err_argt(lua_State *L, int narg, int tt)
  963. {
  964. lj_err_argtype(L, narg, lj_obj_typename[tt+1]);
  965. }
  966. /* -- Public error handling API ------------------------------------------- */
  967. LUA_API lua_CFunction lua_atpanic(lua_State *L, lua_CFunction panicf)
  968. {
  969. lua_CFunction old = G(L)->panic;
  970. G(L)->panic = panicf;
  971. return old;
  972. }
  973. /* Forwarders for the public API (C calling convention and no LJ_NORET). */
  974. LUA_API int lua_error(lua_State *L)
  975. {
  976. lj_err_run(L);
  977. return 0; /* unreachable */
  978. }
  979. LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *msg)
  980. {
  981. err_argmsg(L, narg, msg);
  982. return 0; /* unreachable */
  983. }
  984. LUALIB_API int luaL_typerror(lua_State *L, int narg, const char *xname)
  985. {
  986. lj_err_argtype(L, narg, xname);
  987. return 0; /* unreachable */
  988. }
  989. LUALIB_API void luaL_where(lua_State *L, int level)
  990. {
  991. int size;
  992. cTValue *frame = lj_debug_frame(L, level, &size);
  993. lj_debug_addloc(L, "", frame, size ? frame+size : NULL);
  994. }
  995. LUALIB_API int luaL_error(lua_State *L, const char *fmt, ...)
  996. {
  997. const char *msg;
  998. va_list argp;
  999. va_start(argp, fmt);
  1000. msg = lj_strfmt_pushvf(L, fmt, argp);
  1001. va_end(argp);
  1002. lj_err_callermsg(L, msg);
  1003. return 0; /* unreachable */
  1004. }