00001
00002
00003
00004
00005
00006
00007
00008 #include <setjmp.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011
00012 #define ldo_c
00013 #define LUA_CORE
00014
00015 #include "../lua_jailbreak_exception.hpp"
00016 #include <exception>
00017 #if !defined(__cplusplus)
00018 #error "Exception support requires a C++ compiler."
00019 #endif
00020
00021 #include "lua.h"
00022
00023 #include "lapi.h"
00024 #include "ldebug.h"
00025 #include "ldo.h"
00026 #include "lfunc.h"
00027 #include "lgc.h"
00028 #include "lmem.h"
00029 #include "lobject.h"
00030 #include "lopcodes.h"
00031 #include "lparser.h"
00032 #include "lstate.h"
00033 #include "lstring.h"
00034 #include "ltable.h"
00035 #include "ltm.h"
00036 #include "lundump.h"
00037 #include "lvm.h"
00038 #include "lzio.h"
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055 #if defined(__cplusplus)
00056
00057 #define LUAI_THROW(L,c) throw(c)
00058 #define LUAI_TRY(L,c,a) \
00059 try { \
00060 try { \
00061 a \
00062 } catch(const tlua_jailbreak_exception &e) { \
00063 e.store(); \
00064 throw; \
00065 } catch(const std::exception &e) { \
00066 lua_pushstring(L, e.what()); \
00067 luaG_errormsg(L); \
00068 throw; \
00069 } \
00070 } catch(...) { \
00071 if((c)->status == 0) \
00072 (c)->status = -1;\
00073 }
00074 #define luai_jmpbuf int
00075
00076
00077 #elif defined(LUA_USE_ULONGJMP)
00078
00079 #define LUAI_THROW(L,c) _longjmp((c)->b, 1)
00080 #define LUAI_TRY(L,c,a) if (_setjmp((c)->b) == 0) { a }
00081 #define luai_jmpbuf jmp_buf
00082
00083 #else
00084
00085 #define LUAI_THROW(L,c) longjmp((c)->b, 1)
00086 #define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
00087 #define luai_jmpbuf jmp_buf
00088
00089 #endif
00090
00091
00092
00093
00094 struct lua_longjmp {
00095 struct lua_longjmp *previous;
00096 luai_jmpbuf b;
00097 volatile int status;
00098 };
00099
00100
00101 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
00102 switch (errcode) {
00103 case LUA_ERRMEM: {
00104 setsvalue2s(L, oldtop, G(L)->memerrmsg);
00105 break;
00106 }
00107 case LUA_ERRERR: {
00108 setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
00109 break;
00110 }
00111 default: {
00112 setobjs2s(L, oldtop, L->top - 1);
00113 break;
00114 }
00115 }
00116 L->top = oldtop + 1;
00117 }
00118
00119
00120 l_noret luaD_throw (lua_State *L, int errcode) {
00121 if (L->errorJmp) {
00122 L->errorJmp->status = errcode;
00123 LUAI_THROW(L, L->errorJmp);
00124 }
00125 else {
00126 L->status = cast_byte(errcode);
00127 if (G(L)->mainthread->errorJmp) {
00128 setobjs2s(L, G(L)->mainthread->top++, L->top - 1);
00129 luaD_throw(G(L)->mainthread, errcode);
00130 }
00131 else {
00132 if (G(L)->panic) {
00133 lua_unlock(L);
00134 G(L)->panic(L);
00135 }
00136 abort();
00137 }
00138 }
00139 }
00140
00141
00142 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
00143 unsigned short oldnCcalls = L->nCcalls;
00144 struct lua_longjmp lj;
00145 lj.status = LUA_OK;
00146 lj.previous = L->errorJmp;
00147 L->errorJmp = &lj;
00148 LUAI_TRY(L, &lj,
00149 (*f)(L, ud);
00150 );
00151 L->errorJmp = lj.previous;
00152 L->nCcalls = oldnCcalls;
00153 return lj.status;
00154 }
00155
00156
00157
00158
00159 static void correctstack (lua_State *L, TValue *oldstack) {
00160 CallInfo *ci;
00161 GCObject *up;
00162 L->top = (L->top - oldstack) + L->stack;
00163 for (up = L->openupval; up != NULL; up = up->gch.next)
00164 gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
00165 for (ci = L->ci; ci != NULL; ci = ci->previous) {
00166 ci->top = (ci->top - oldstack) + L->stack;
00167 ci->func = (ci->func - oldstack) + L->stack;
00168 if (isLua(ci))
00169 ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
00170 }
00171 }
00172
00173
00174
00175 #define ERRORSTACKSIZE (LUAI_MAXSTACK + 200)
00176
00177
00178 void luaD_reallocstack (lua_State *L, int newsize) {
00179 TValue *oldstack = L->stack;
00180 int lim = L->stacksize;
00181 lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
00182 lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
00183 luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
00184 for (; lim < newsize; lim++)
00185 setnilvalue(L->stack + lim);
00186 L->stacksize = newsize;
00187 L->stack_last = L->stack + newsize - EXTRA_STACK;
00188 correctstack(L, oldstack);
00189 }
00190
00191
00192 void luaD_growstack (lua_State *L, int n) {
00193 int size = L->stacksize;
00194 if (size > LUAI_MAXSTACK)
00195 luaD_throw(L, LUA_ERRERR);
00196 else {
00197 int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
00198 int newsize = 2 * size;
00199 if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
00200 if (newsize < needed) newsize = needed;
00201 if (newsize > LUAI_MAXSTACK) {
00202 luaD_reallocstack(L, ERRORSTACKSIZE);
00203 luaG_runerror(L, "stack overflow");
00204 }
00205 else
00206 luaD_reallocstack(L, newsize);
00207 }
00208 }
00209
00210
00211 static int stackinuse (lua_State *L) {
00212 CallInfo *ci;
00213 StkId lim = L->top;
00214 for (ci = L->ci; ci != NULL; ci = ci->previous) {
00215 lua_assert(ci->top <= L->stack_last);
00216 if (lim < ci->top) lim = ci->top;
00217 }
00218 return cast_int(lim - L->stack) + 1;
00219 }
00220
00221
00222 void luaD_shrinkstack (lua_State *L) {
00223 int inuse = stackinuse(L);
00224 int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
00225 if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
00226 if (inuse > LUAI_MAXSTACK ||
00227 goodsize >= L->stacksize)
00228 condmovestack(L);
00229 else
00230 luaD_reallocstack(L, goodsize);
00231 }
00232
00233
00234 void luaD_hook (lua_State *L, int event, int line) {
00235 lua_Hook hook = L->hook;
00236 if (hook && L->allowhook) {
00237 CallInfo *ci = L->ci;
00238 ptrdiff_t top = savestack(L, L->top);
00239 ptrdiff_t ci_top = savestack(L, ci->top);
00240 lua_Debug ar;
00241 ar.event = event;
00242 ar.currentline = line;
00243 ar.i_ci = ci;
00244 luaD_checkstack(L, LUA_MINSTACK);
00245 ci->top = L->top + LUA_MINSTACK;
00246 lua_assert(ci->top <= L->stack_last);
00247 L->allowhook = 0;
00248 ci->callstatus |= CIST_HOOKED;
00249 lua_unlock(L);
00250 (*hook)(L, &ar);
00251 lua_lock(L);
00252 lua_assert(!L->allowhook);
00253 L->allowhook = 1;
00254 ci->top = restorestack(L, ci_top);
00255 L->top = restorestack(L, top);
00256 ci->callstatus &= ~CIST_HOOKED;
00257 }
00258 }
00259
00260
00261 static void callhook (lua_State *L, CallInfo *ci) {
00262 int hook = LUA_HOOKCALL;
00263 ci->u.l.savedpc++;
00264 if (isLua(ci->previous) &&
00265 GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
00266 ci->callstatus |= CIST_TAIL;
00267 hook = LUA_HOOKTAILCALL;
00268 }
00269 luaD_hook(L, hook, -1);
00270 ci->u.l.savedpc--;
00271 }
00272
00273
00274 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
00275 int i;
00276 int nfixargs = p->numparams;
00277 StkId base, fixed;
00278 lua_assert(actual >= nfixargs);
00279
00280 fixed = L->top - actual;
00281 base = L->top;
00282 for (i=0; i<nfixargs; i++) {
00283 setobjs2s(L, L->top++, fixed + i);
00284 setnilvalue(fixed + i);
00285 }
00286 return base;
00287 }
00288
00289
00290 static StkId tryfuncTM (lua_State *L, StkId func) {
00291 const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
00292 StkId p;
00293 ptrdiff_t funcr = savestack(L, func);
00294 if (!ttisfunction(tm))
00295 luaG_typeerror(L, func, "call");
00296
00297 for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
00298 incr_top(L);
00299 func = restorestack(L, funcr);
00300 setobj2s(L, func, tm);
00301 return func;
00302 }
00303
00304
00305
00306 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
00307
00308
00309
00310
00311
00312 int luaD_precall (lua_State *L, StkId func, int nresults) {
00313 lua_CFunction f;
00314 CallInfo *ci;
00315 int n;
00316 ptrdiff_t funcr = savestack(L, func);
00317 switch (ttype(func)) {
00318 case LUA_TLCF:
00319 f = fvalue(func);
00320 goto Cfunc;
00321 case LUA_TCCL: {
00322 f = clCvalue(func)->f;
00323 Cfunc:
00324 luaD_checkstack(L, LUA_MINSTACK);
00325 ci = next_ci(L);
00326 ci->nresults = nresults;
00327 ci->func = restorestack(L, funcr);
00328 ci->top = L->top + LUA_MINSTACK;
00329 lua_assert(ci->top <= L->stack_last);
00330 ci->callstatus = 0;
00331 if (L->hookmask & LUA_MASKCALL)
00332 luaD_hook(L, LUA_HOOKCALL, -1);
00333 lua_unlock(L);
00334 n = (*f)(L);
00335 lua_lock(L);
00336 api_checknelems(L, n);
00337 luaD_poscall(L, L->top - n);
00338 return 1;
00339 }
00340 case LUA_TLCL: {
00341 StkId base;
00342 Proto *p = clLvalue(func)->p;
00343 luaD_checkstack(L, p->maxstacksize);
00344 func = restorestack(L, funcr);
00345 n = cast_int(L->top - func) - 1;
00346 for (; n < p->numparams; n++)
00347 setnilvalue(L->top++);
00348 base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
00349 ci = next_ci(L);
00350 ci->nresults = nresults;
00351 ci->func = func;
00352 ci->u.l.base = base;
00353 ci->top = base + p->maxstacksize;
00354 lua_assert(ci->top <= L->stack_last);
00355 ci->u.l.savedpc = p->code;
00356 ci->callstatus = CIST_LUA;
00357 L->top = ci->top;
00358 if (L->hookmask & LUA_MASKCALL)
00359 callhook(L, ci);
00360 return 0;
00361 }
00362 default: {
00363 func = tryfuncTM(L, func);
00364 return luaD_precall(L, func, nresults);
00365 }
00366 }
00367 }
00368
00369
00370 int luaD_poscall (lua_State *L, StkId firstResult) {
00371 StkId res;
00372 int wanted, i;
00373 CallInfo *ci = L->ci;
00374 if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
00375 if (L->hookmask & LUA_MASKRET) {
00376 ptrdiff_t fr = savestack(L, firstResult);
00377 luaD_hook(L, LUA_HOOKRET, -1);
00378 firstResult = restorestack(L, fr);
00379 }
00380 L->oldpc = ci->previous->u.l.savedpc;
00381 }
00382 res = ci->func;
00383 wanted = ci->nresults;
00384 L->ci = ci = ci->previous;
00385
00386 for (i = wanted; i != 0 && firstResult < L->top; i--)
00387 setobjs2s(L, res++, firstResult++);
00388 while (i-- > 0)
00389 setnilvalue(res++);
00390 L->top = res;
00391 return (wanted - LUA_MULTRET);
00392 }
00393
00394
00395
00396
00397
00398
00399
00400
00401 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
00402 if (++L->nCcalls >= LUAI_MAXCCALLS) {
00403 if (L->nCcalls == LUAI_MAXCCALLS)
00404 luaG_runerror(L, "C stack overflow");
00405 else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
00406 luaD_throw(L, LUA_ERRERR);
00407 }
00408 if (!allowyield) L->nny++;
00409 if (!luaD_precall(L, func, nResults))
00410 luaV_execute(L);
00411 if (!allowyield) L->nny--;
00412 L->nCcalls--;
00413 luaC_checkGC(L);
00414 }
00415
00416
00417 static void finishCcall (lua_State *L) {
00418 CallInfo *ci = L->ci;
00419 int n;
00420 lua_assert(ci->u.c.k != NULL);
00421 lua_assert(L->nny == 0);
00422
00423 L->nCcalls--;
00424
00425 adjustresults(L, ci->nresults);
00426
00427 if (!(ci->callstatus & CIST_STAT))
00428 ci->u.c.status = LUA_YIELD;
00429 lua_assert(ci->u.c.status != LUA_OK);
00430 ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
00431 lua_unlock(L);
00432 n = (*ci->u.c.k)(L);
00433 lua_lock(L);
00434 api_checknelems(L, n);
00435
00436 luaD_poscall(L, L->top - n);
00437 }
00438
00439
00440 static void unroll (lua_State *L, void *ud) {
00441 UNUSED(ud);
00442 for (;;) {
00443 if (L->ci == &L->base_ci)
00444 return;
00445 if (!isLua(L->ci))
00446 finishCcall(L);
00447 else {
00448 luaV_finishOp(L);
00449 luaV_execute(L);
00450 }
00451 }
00452 }
00453
00454
00455
00456
00457
00458 static CallInfo *findpcall (lua_State *L) {
00459 CallInfo *ci;
00460 for (ci = L->ci; ci != NULL; ci = ci->previous) {
00461 if (ci->callstatus & CIST_YPCALL)
00462 return ci;
00463 }
00464 return NULL;
00465 }
00466
00467
00468 static int recover (lua_State *L, int status) {
00469 StkId oldtop;
00470 CallInfo *ci = findpcall(L);
00471 if (ci == NULL) return 0;
00472
00473 oldtop = restorestack(L, ci->u.c.extra);
00474 luaF_close(L, oldtop);
00475 seterrorobj(L, status, oldtop);
00476 L->ci = ci;
00477 L->allowhook = ci->u.c.old_allowhook;
00478 L->nny = 0;
00479 luaD_shrinkstack(L);
00480 L->errfunc = ci->u.c.old_errfunc;
00481 ci->callstatus |= CIST_STAT;
00482 ci->u.c.status = status;
00483 return 1;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
00493 L->top = firstArg;
00494 setsvalue2s(L, L->top, luaS_new(L, msg));
00495 incr_top(L);
00496 luaD_throw(L, -1);
00497 }
00498
00499
00500
00501
00502
00503 static void resume (lua_State *L, void *ud) {
00504 StkId firstArg = cast(StkId, ud);
00505 CallInfo *ci = L->ci;
00506 if (L->nCcalls >= LUAI_MAXCCALLS)
00507 resume_error(L, "C stack overflow", firstArg);
00508 if (L->status == LUA_OK) {
00509 if (ci != &L->base_ci)
00510 resume_error(L, "cannot resume non-suspended coroutine", firstArg);
00511
00512 if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))
00513 luaV_execute(L);
00514 }
00515 else if (L->status != LUA_YIELD)
00516 resume_error(L, "cannot resume dead coroutine", firstArg);
00517 else {
00518 L->status = LUA_OK;
00519 if (isLua(ci))
00520 luaV_execute(L);
00521 else {
00522 ci->func = restorestack(L, ci->u.c.extra);
00523 if (ci->u.c.k != NULL) {
00524 int n;
00525 ci->u.c.status = LUA_YIELD;
00526 ci->callstatus |= CIST_YIELDED;
00527 lua_unlock(L);
00528 n = (*ci->u.c.k)(L);
00529 lua_lock(L);
00530 api_checknelems(L, n);
00531 firstArg = L->top - n;
00532 }
00533 L->nCcalls--;
00534 luaD_poscall(L, firstArg);
00535 }
00536 unroll(L, NULL);
00537 }
00538 }
00539
00540
00541 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
00542 int status;
00543 lua_lock(L);
00544 luai_userstateresume(L, nargs);
00545 L->nCcalls = (from) ? from->nCcalls + 1 : 1;
00546 L->nny = 0;
00547 api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
00548 status = luaD_rawrunprotected(L, resume, L->top - nargs);
00549 if (status == -1)
00550 status = LUA_ERRRUN;
00551 else {
00552 while (status != LUA_OK && status != LUA_YIELD) {
00553 if (recover(L, status))
00554 status = luaD_rawrunprotected(L, unroll, NULL);
00555 else {
00556 L->status = cast_byte(status);
00557 seterrorobj(L, status, L->top);
00558 L->ci->top = L->top;
00559 break;
00560 }
00561 }
00562 lua_assert(status == L->status);
00563 }
00564 L->nny = 1;
00565 L->nCcalls--;
00566 lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
00567 lua_unlock(L);
00568 return status;
00569 }
00570
00571
00572 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
00573 CallInfo *ci = L->ci;
00574 luai_userstateyield(L, nresults);
00575 lua_lock(L);
00576 api_checknelems(L, nresults);
00577 if (L->nny > 0) {
00578 if (L != G(L)->mainthread)
00579 luaG_runerror(L, "attempt to yield across metamethod/C-call boundary");
00580 else
00581 luaG_runerror(L, "attempt to yield from outside a coroutine");
00582 }
00583 L->status = LUA_YIELD;
00584 if (isLua(ci)) {
00585 api_check(L, k == NULL, "hooks cannot continue after yielding");
00586 }
00587 else {
00588 if ((ci->u.c.k = k) != NULL)
00589 ci->u.c.ctx = ctx;
00590 ci->u.c.extra = savestack(L, ci->func);
00591 ci->func = L->top - nresults - 1;
00592 luaD_throw(L, LUA_YIELD);
00593 }
00594 lua_assert(ci->callstatus & CIST_HOOKED);
00595 lua_unlock(L);
00596 return 0;
00597 }
00598
00599
00600 int luaD_pcall (lua_State *L, Pfunc func, void *u,
00601 ptrdiff_t old_top, ptrdiff_t ef) {
00602 int status;
00603 CallInfo *old_ci = L->ci;
00604 lu_byte old_allowhooks = L->allowhook;
00605 unsigned short old_nny = L->nny;
00606 ptrdiff_t old_errfunc = L->errfunc;
00607 L->errfunc = ef;
00608 status = luaD_rawrunprotected(L, func, u);
00609 if (status != LUA_OK) {
00610 StkId oldtop = restorestack(L, old_top);
00611 luaF_close(L, oldtop);
00612 seterrorobj(L, status, oldtop);
00613 L->ci = old_ci;
00614 L->allowhook = old_allowhooks;
00615 L->nny = old_nny;
00616 luaD_shrinkstack(L);
00617 }
00618 L->errfunc = old_errfunc;
00619 return status;
00620 }
00621
00622
00623
00624
00625
00626
00627 struct SParser {
00628 ZIO *z;
00629 Mbuffer buff;
00630 Dyndata dyd;
00631 const char *mode;
00632 const char *name;
00633 };
00634
00635
00636 static void checkmode (lua_State *L, const char *mode, const char *x) {
00637 if (mode && strchr(mode, x[0]) == NULL) {
00638 luaO_pushfstring(L,
00639 "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
00640 luaD_throw(L, LUA_ERRSYNTAX);
00641 }
00642 }
00643
00644
00645 static void f_parser (lua_State *L, void *ud) {
00646 int i;
00647 Proto *tf;
00648 Closure *cl;
00649 struct SParser *p = cast(struct SParser *, ud);
00650 int c = zgetc(p->z);
00651 if (c == LUA_SIGNATURE[0]) {
00652 checkmode(L, p->mode, "binary");
00653 tf = luaU_undump(L, p->z, &p->buff, p->name);
00654 }
00655 else {
00656 checkmode(L, p->mode, "text");
00657 tf = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
00658 }
00659 setptvalue2s(L, L->top, tf);
00660 incr_top(L);
00661 cl = luaF_newLclosure(L, tf);
00662 setclLvalue(L, L->top - 1, cl);
00663 for (i = 0; i < tf->sizeupvalues; i++)
00664 cl->l.upvals[i] = luaF_newupval(L);
00665 }
00666
00667
00668 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
00669 const char *mode) {
00670 struct SParser p;
00671 int status;
00672 L->nny++;
00673 p.z = z; p.name = name; p.mode = mode;
00674 p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
00675 p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
00676 p.dyd.label.arr = NULL; p.dyd.label.size = 0;
00677 luaZ_initbuffer(L, &p.buff);
00678 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
00679 luaZ_freebuffer(L, &p.buff);
00680 luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
00681 luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
00682 luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
00683 L->nny--;
00684 return status;
00685 }
00686
00687