xref: /freebsd/sys/contrib/openzfs/module/lua/ldo.c (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: MIT
2 /*
3 ** $Id: ldo.c,v 2.108.1.3 2013/11/08 18:22:50 roberto Exp $
4 ** Stack and Call structure of Lua
5 ** See Copyright Notice in lua.h
6 */
7 
8 
9 #define ldo_c
10 #define LUA_CORE
11 
12 #include <sys/lua/lua.h>
13 #include <sys/asm_linkage.h>
14 
15 #include "lapi.h"
16 #include "ldebug.h"
17 #include "ldo.h"
18 #include "lfunc.h"
19 #include "lgc.h"
20 #include "lmem.h"
21 #include "lobject.h"
22 #include "lopcodes.h"
23 #include "lparser.h"
24 #include "lstate.h"
25 #include "lstring.h"
26 #include "ltable.h"
27 #include "ltm.h"
28 #include "lvm.h"
29 #include "lzio.h"
30 
31 
32 /* Return the number of bytes available on the stack. */
33 #if defined (_KERNEL) && defined(__linux__)
34 #include <asm/current.h>
stack_remaining(void)35 static intptr_t stack_remaining(void) {
36   intptr_t local;
37   local = (intptr_t)&local - (intptr_t)current->stack;
38   return local;
39 }
40 #elif defined (_KERNEL) && defined(__FreeBSD__)
41 #include <sys/pcpu.h>
stack_remaining(void)42 static intptr_t stack_remaining(void) {
43   intptr_t local;
44   local = (intptr_t)&local - (intptr_t)curthread->td_kstack;
45   return local;
46 }
47 #else
stack_remaining(void)48 static intptr_t stack_remaining(void) {
49   return INTPTR_MAX;
50 }
51 #endif
52 
53 /*
54 ** {======================================================
55 ** Error-recovery functions
56 ** =======================================================
57 */
58 
59 /*
60 ** LUAI_THROW/LUAI_TRY define how Lua does exception handling. By
61 ** default, Lua handles errors with exceptions when compiling as
62 ** C++ code, with _longjmp/_setjmp when asked to use them, and with
63 ** longjmp/setjmp otherwise.
64 */
65 #if !defined(LUAI_THROW)
66 
67 #ifdef _KERNEL
68 
69 #ifdef __linux__
70 #if defined(__i386__)
71 #define	JMP_BUF_CNT	6
72 #elif defined(__x86_64__)
73 #define	JMP_BUF_CNT	8
74 #elif defined(__sparc__) && defined(__arch64__)
75 #define	JMP_BUF_CNT	6
76 #elif defined(__powerpc__)
77 #define	JMP_BUF_CNT	26
78 #elif defined(__aarch64__)
79 #define	JMP_BUF_CNT	64
80 #elif defined(__arm__)
81 #define	JMP_BUF_CNT	65
82 #elif defined(__mips__)
83 #define JMP_BUF_CNT	12
84 #elif defined(__s390x__)
85 #define JMP_BUF_CNT	18
86 #elif defined(__riscv)
87 #define JMP_BUF_CNT     64
88 #elif defined(__loongarch_lp64)
89 #define JMP_BUF_CNT     64
90 #else
91 #define	JMP_BUF_CNT	1
92 #endif
93 
94 typedef	struct _label_t { long long unsigned val[JMP_BUF_CNT]; } label_t;
95 
96 int ASMABI setjmp(label_t *) __attribute__ ((__nothrow__));
97 extern __attribute__((noreturn)) void ASMABI longjmp(label_t *);
98 
99 #define LUAI_THROW(L,c)		longjmp(&(c)->b)
100 #define LUAI_TRY(L,c,a)		if (setjmp(&(c)->b) == 0) { a }
101 #define luai_jmpbuf		label_t
102 
103 /* unsupported arches will build but not be able to run lua programs */
104 #if JMP_BUF_CNT == 1
setjmp(label_t * buf)105 int setjmp (label_t *buf) {
106 	return 1;
107 }
108 
longjmp(label_t * buf)109 void longjmp (label_t * buf) {
110 	for (;;);
111 }
112 #endif
113 #else
114 #define LUAI_THROW(L,c)		longjmp((c)->b, 1)
115 #define LUAI_TRY(L,c,a)		if (setjmp((c)->b) == 0) { a }
116 #define luai_jmpbuf		jmp_buf
117 #endif
118 
119 #else /* _KERNEL */
120 
121 #if defined(__cplusplus) && !defined(LUA_USE_LONGJMP)
122 /* C++ exceptions */
123 #define LUAI_THROW(L,c)		throw(c)
124 #define LUAI_TRY(L,c,a) \
125 	try { a } catch(...) { if ((c)->status == 0) (c)->status = -1; }
126 #define luai_jmpbuf		int  /* dummy variable */
127 
128 #elif defined(LUA_USE_ULONGJMP)
129 /* in Unix, try _longjmp/_setjmp (more efficient) */
130 #define LUAI_THROW(L,c)		_longjmp((c)->b, 1)
131 #define LUAI_TRY(L,c,a)		if (_setjmp((c)->b) == 0) { a }
132 #define luai_jmpbuf		jmp_buf
133 
134 #else
135 /* default handling with long jumps */
136 #define LUAI_THROW(L,c)		longjmp((c)->b, 1)
137 #define LUAI_TRY(L,c,a)		if (setjmp((c)->b) == 0) { a }
138 #define luai_jmpbuf		jmp_buf
139 
140 #endif
141 
142 #endif /* _KERNEL */
143 
144 #endif /* LUAI_THROW */
145 
146 
147 /* chain list of long jump buffers */
148 struct lua_longjmp {
149   struct lua_longjmp *previous;
150   luai_jmpbuf b;
151   volatile int status;  /* error code */
152 };
153 
154 
seterrorobj(lua_State * L,int errcode,StkId oldtop)155 static void seterrorobj (lua_State *L, int errcode, StkId oldtop) {
156   switch (errcode) {
157     case LUA_ERRMEM: {  /* memory error? */
158       setsvalue2s(L, oldtop, G(L)->memerrmsg); /* reuse preregistered msg. */
159       break;
160     }
161     case LUA_ERRERR: {
162       setsvalue2s(L, oldtop, luaS_newliteral(L, "error in error handling"));
163       break;
164     }
165     default: {
166       setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
167       break;
168     }
169   }
170   L->top = oldtop + 1;
171 }
172 
173 /*
174  * Silence infinite recursion warning which was added to -Wall in gcc 12.1
175  */
176 #if defined(__GNUC__) && !defined(__clang__) && \
177 	defined(HAVE_KERNEL_INFINITE_RECURSION)
178 #pragma GCC diagnostic push
179 #pragma GCC diagnostic ignored "-Winfinite-recursion"
180 #endif
181 
luaD_throw(lua_State * L,int errcode)182 l_noret luaD_throw (lua_State *L, int errcode) {
183   if (L->errorJmp) {  /* thread has an error handler? */
184     L->errorJmp->status = errcode;  /* set status */
185     LUAI_THROW(L, L->errorJmp);  /* jump to it */
186   }
187   else {  /* thread has no error handler */
188     L->status = cast_byte(errcode);  /* mark it as dead */
189     if (G(L)->mainthread->errorJmp) {  /* main thread has a handler? */
190       setobjs2s(L, G(L)->mainthread->top++, L->top - 1);  /* copy error obj. */
191       luaD_throw(G(L)->mainthread, errcode);  /* re-throw in main thread */
192     }
193     else {  /* no handler at all; abort */
194       if (G(L)->panic) {  /* panic function? */
195         lua_unlock(L);
196         G(L)->panic(L);  /* call it (last chance to jump out) */
197       }
198       panic("no error handler");
199     }
200   }
201 }
202 
203 #if defined(__GNUC__) && !defined(__clang__) && \
204 	defined(HAVE_INFINITE_RECURSION)
205 #pragma GCC diagnostic pop
206 #endif
207 
208 
luaD_rawrunprotected(lua_State * L,Pfunc f,void * ud)209 int luaD_rawrunprotected (lua_State *L, Pfunc f, void *ud) {
210   unsigned short oldnCcalls = L->nCcalls;
211   struct lua_longjmp lj;
212   lj.status = LUA_OK;
213   lj.previous = L->errorJmp;  /* chain new error handler */
214   L->errorJmp = &lj;
215   LUAI_TRY(L, &lj,
216     (*f)(L, ud);
217   );
218   L->errorJmp = lj.previous;  /* restore old error handler */
219   L->nCcalls = oldnCcalls;
220   return lj.status;
221 }
222 
223 /* }====================================================== */
224 
225 
correctstack(lua_State * L,TValue * oldstack)226 static void correctstack (lua_State *L, TValue *oldstack) {
227   CallInfo *ci;
228   GCObject *up;
229   L->top = (L->top - oldstack) + L->stack;
230   for (up = L->openupval; up != NULL; up = up->gch.next)
231     gco2uv(up)->v = (gco2uv(up)->v - oldstack) + L->stack;
232   for (ci = L->ci; ci != NULL; ci = ci->previous) {
233     ci->top = (ci->top - oldstack) + L->stack;
234     ci->func = (ci->func - oldstack) + L->stack;
235     if (isLua(ci))
236       ci->u.l.base = (ci->u.l.base - oldstack) + L->stack;
237   }
238 }
239 
240 
241 /* some space for error handling */
242 #define ERRORSTACKSIZE	(LUAI_MAXSTACK + 200)
243 
244 
luaD_reallocstack(lua_State * L,int newsize)245 void luaD_reallocstack (lua_State *L, int newsize) {
246   TValue *oldstack = L->stack;
247   int lim = L->stacksize;
248   lua_assert(newsize <= LUAI_MAXSTACK || newsize == ERRORSTACKSIZE);
249   lua_assert(L->stack_last - L->stack == L->stacksize - EXTRA_STACK);
250   luaM_reallocvector(L, L->stack, L->stacksize, newsize, TValue);
251   for (; lim < newsize; lim++)
252     setnilvalue(L->stack + lim); /* erase new segment */
253   L->stacksize = newsize;
254   L->stack_last = L->stack + newsize - EXTRA_STACK;
255   correctstack(L, oldstack);
256 }
257 
258 
luaD_growstack(lua_State * L,int n)259 void luaD_growstack (lua_State *L, int n) {
260   int size = L->stacksize;
261   if (size > LUAI_MAXSTACK)  /* error after extra size? */
262     luaD_throw(L, LUA_ERRERR);
263   else {
264     int needed = cast_int(L->top - L->stack) + n + EXTRA_STACK;
265     int newsize = 2 * size;
266     if (newsize > LUAI_MAXSTACK) newsize = LUAI_MAXSTACK;
267     if (newsize < needed) newsize = needed;
268     if (newsize > LUAI_MAXSTACK) {  /* stack overflow? */
269       luaD_reallocstack(L, ERRORSTACKSIZE);
270       luaG_runerror(L, "stack overflow");
271     }
272     else
273       luaD_reallocstack(L, newsize);
274   }
275 }
276 
277 
stackinuse(lua_State * L)278 static int stackinuse (lua_State *L) {
279   CallInfo *ci;
280   StkId lim = L->top;
281   for (ci = L->ci; ci != NULL; ci = ci->previous) {
282     lua_assert(ci->top <= L->stack_last);
283     if (lim < ci->top) lim = ci->top;
284   }
285   return cast_int(lim - L->stack) + 1;  /* part of stack in use */
286 }
287 
288 
luaD_shrinkstack(lua_State * L)289 void luaD_shrinkstack (lua_State *L) {
290   int inuse = stackinuse(L);
291   int goodsize = inuse + (inuse / 8) + 2*EXTRA_STACK;
292   if (goodsize > LUAI_MAXSTACK) goodsize = LUAI_MAXSTACK;
293   if (inuse > LUAI_MAXSTACK ||  /* handling stack overflow? */
294       goodsize >= L->stacksize)  /* would grow instead of shrink? */
295     condmovestack(L);  /* don't change stack (change only for debugging) */
296   else
297     luaD_reallocstack(L, goodsize);  /* shrink it */
298 }
299 
300 
luaD_hook(lua_State * L,int event,int line)301 void luaD_hook (lua_State *L, int event, int line) {
302   lua_Hook hook = L->hook;
303   if (hook && L->allowhook) {
304     CallInfo *ci = L->ci;
305     ptrdiff_t top = savestack(L, L->top);
306     ptrdiff_t ci_top = savestack(L, ci->top);
307     lua_Debug ar;
308     ar.event = event;
309     ar.currentline = line;
310     ar.i_ci = ci;
311     luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
312     ci->top = L->top + LUA_MINSTACK;
313     lua_assert(ci->top <= L->stack_last);
314     L->allowhook = 0;  /* cannot call hooks inside a hook */
315     ci->callstatus |= CIST_HOOKED;
316     lua_unlock(L);
317     (*hook)(L, &ar);
318     lua_lock(L);
319     lua_assert(!L->allowhook);
320     L->allowhook = 1;
321     ci->top = restorestack(L, ci_top);
322     L->top = restorestack(L, top);
323     ci->callstatus &= ~CIST_HOOKED;
324   }
325 }
326 
327 
callhook(lua_State * L,CallInfo * ci)328 static void callhook (lua_State *L, CallInfo *ci) {
329   int hook = LUA_HOOKCALL;
330   ci->u.l.savedpc++;  /* hooks assume 'pc' is already incremented */
331   if (isLua(ci->previous) &&
332       GET_OPCODE(*(ci->previous->u.l.savedpc - 1)) == OP_TAILCALL) {
333     ci->callstatus |= CIST_TAIL;
334     hook = LUA_HOOKTAILCALL;
335   }
336   luaD_hook(L, hook, -1);
337   ci->u.l.savedpc--;  /* correct 'pc' */
338 }
339 
340 
adjust_varargs(lua_State * L,Proto * p,int actual)341 static StkId adjust_varargs (lua_State *L, Proto *p, int actual) {
342   int i;
343   int nfixargs = p->numparams;
344   StkId base, fixed;
345   lua_assert(actual >= nfixargs);
346   /* move fixed parameters to final position */
347   luaD_checkstack(L, p->maxstacksize);  /* check again for new 'base' */
348   fixed = L->top - actual;  /* first fixed argument */
349   base = L->top;  /* final position of first argument */
350   for (i=0; i<nfixargs; i++) {
351     setobjs2s(L, L->top++, fixed + i);
352     setnilvalue(fixed + i);
353   }
354   return base;
355 }
356 
357 
tryfuncTM(lua_State * L,StkId func)358 static StkId tryfuncTM (lua_State *L, StkId func) {
359   const TValue *tm = luaT_gettmbyobj(L, func, TM_CALL);
360   StkId p;
361   ptrdiff_t funcr = savestack(L, func);
362   if (!ttisfunction(tm))
363     luaG_typeerror(L, func, "call");
364   /* Open a hole inside the stack at `func' */
365   for (p = L->top; p > func; p--) setobjs2s(L, p, p-1);
366   incr_top(L);
367   func = restorestack(L, funcr);  /* previous call may change stack */
368   setobj2s(L, func, tm);  /* tag method is the new function to be called */
369   return func;
370 }
371 
372 
373 
374 #define next_ci(L) (L->ci = (L->ci->next ? L->ci->next : luaE_extendCI(L)))
375 
376 
377 /*
378 ** returns true if function has been executed (C function)
379 */
luaD_precall(lua_State * L,StkId func,int nresults)380 int luaD_precall (lua_State *L, StkId func, int nresults) {
381   lua_CFunction f;
382   CallInfo *ci;
383   int n;  /* number of arguments (Lua) or returns (C) */
384   ptrdiff_t funcr = savestack(L, func);
385   switch (ttype(func)) {
386     case LUA_TLCF:  /* light C function */
387       f = fvalue(func);
388       goto Cfunc;
389     case LUA_TCCL: {  /* C closure */
390       f = clCvalue(func)->f;
391      Cfunc:
392       luaD_checkstack(L, LUA_MINSTACK);  /* ensure minimum stack size */
393       ci = next_ci(L);  /* now 'enter' new function */
394       ci->nresults = nresults;
395       ci->func = restorestack(L, funcr);
396       ci->top = L->top + LUA_MINSTACK;
397       lua_assert(ci->top <= L->stack_last);
398       ci->callstatus = 0;
399       luaC_checkGC(L);  /* stack grow uses memory */
400       if (L->hookmask & LUA_MASKCALL)
401         luaD_hook(L, LUA_HOOKCALL, -1);
402       lua_unlock(L);
403       n = (*f)(L);  /* do the actual call */
404       lua_lock(L);
405       api_checknelems(L, n);
406       luaD_poscall(L, L->top - n);
407       return 1;
408     }
409     case LUA_TLCL: {  /* Lua function: prepare its call */
410       StkId base;
411       Proto *p = clLvalue(func)->p;
412       n = cast_int(L->top - func) - 1;  /* number of real arguments */
413       luaD_checkstack(L, p->maxstacksize + p->numparams);
414       for (; n < p->numparams; n++)
415         setnilvalue(L->top++);  /* complete missing arguments */
416       if (!p->is_vararg) {
417         func = restorestack(L, funcr);
418         base = func + 1;
419       }
420       else {
421         base = adjust_varargs(L, p, n);
422         func = restorestack(L, funcr);  /* previous call can change stack */
423       }
424       ci = next_ci(L);  /* now 'enter' new function */
425       ci->nresults = nresults;
426       ci->func = func;
427       ci->u.l.base = base;
428       ci->top = base + p->maxstacksize;
429       lua_assert(ci->top <= L->stack_last);
430       ci->u.l.savedpc = p->code;  /* starting point */
431       ci->callstatus = CIST_LUA;
432       L->top = ci->top;
433       luaC_checkGC(L);  /* stack grow uses memory */
434       if (L->hookmask & LUA_MASKCALL)
435         callhook(L, ci);
436       return 0;
437     }
438     default: {  /* not a function */
439       func = tryfuncTM(L, func);  /* retry with 'function' tag method */
440       return luaD_precall(L, func, nresults);  /* now it must be a function */
441     }
442   }
443 }
444 
445 
luaD_poscall(lua_State * L,StkId firstResult)446 int luaD_poscall (lua_State *L, StkId firstResult) {
447   StkId res;
448   int wanted, i;
449   CallInfo *ci = L->ci;
450   if (L->hookmask & (LUA_MASKRET | LUA_MASKLINE)) {
451     if (L->hookmask & LUA_MASKRET) {
452       ptrdiff_t fr = savestack(L, firstResult);  /* hook may change stack */
453       luaD_hook(L, LUA_HOOKRET, -1);
454       firstResult = restorestack(L, fr);
455     }
456     L->oldpc = ci->previous->u.l.savedpc;  /* 'oldpc' for caller function */
457   }
458   res = ci->func;  /* res == final position of 1st result */
459   wanted = ci->nresults;
460   L->ci = ci->previous;  /* back to caller */
461   /* move results to correct place */
462   for (i = wanted; i != 0 && firstResult < L->top; i--)
463     setobjs2s(L, res++, firstResult++);
464   while (i-- > 0)
465     setnilvalue(res++);
466   L->top = res;
467   return (wanted - LUA_MULTRET);  /* 0 iff wanted == LUA_MULTRET */
468 }
469 
470 
471 /*
472 ** Call a function (C or Lua). The function to be called is at *func.
473 ** The arguments are on the stack, right after the function.
474 ** When returns, all the results are on the stack, starting at the original
475 ** function position.
476 */
luaD_call(lua_State * L,StkId func,int nResults,int allowyield)477 void luaD_call (lua_State *L, StkId func, int nResults, int allowyield) {
478   if (++L->nCcalls >= LUAI_MAXCCALLS) {
479     if (L->nCcalls == LUAI_MAXCCALLS)
480       luaG_runerror(L, "C stack overflow");
481     else if (L->nCcalls >= (LUAI_MAXCCALLS + (LUAI_MAXCCALLS>>3)))
482       luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
483   }
484   intptr_t remaining = stack_remaining();
485   if (L->runerror == 0 && remaining < LUAI_MINCSTACK)
486     luaG_runerror(L, "C stack overflow");
487   if (L->runerror != 0 && remaining < LUAI_MINCSTACK / 2)
488     luaD_throw(L, LUA_ERRERR);  /* error while handling stack error */
489   if (!allowyield) L->nny++;
490   if (!luaD_precall(L, func, nResults))  /* is a Lua function? */
491     luaV_execute(L);  /* call it */
492   if (!allowyield) L->nny--;
493   L->nCcalls--;
494 }
495 
496 
finishCcall(lua_State * L)497 static void finishCcall (lua_State *L) {
498   CallInfo *ci = L->ci;
499   int n;
500   lua_assert(ci->u.c.k != NULL);  /* must have a continuation */
501   lua_assert(L->nny == 0);
502   if (ci->callstatus & CIST_YPCALL) {  /* was inside a pcall? */
503     ci->callstatus &= ~CIST_YPCALL;  /* finish 'lua_pcall' */
504     L->errfunc = ci->u.c.old_errfunc;
505   }
506   /* finish 'lua_callk'/'lua_pcall' */
507   adjustresults(L, ci->nresults);
508   /* call continuation function */
509   if (!(ci->callstatus & CIST_STAT))  /* no call status? */
510     ci->u.c.status = LUA_YIELD;  /* 'default' status */
511   lua_assert(ci->u.c.status != LUA_OK);
512   ci->callstatus = (ci->callstatus & ~(CIST_YPCALL | CIST_STAT)) | CIST_YIELDED;
513   lua_unlock(L);
514   n = (*ci->u.c.k)(L);
515   lua_lock(L);
516   api_checknelems(L, n);
517   /* finish 'luaD_precall' */
518   luaD_poscall(L, L->top - n);
519 }
520 
521 
unroll(lua_State * L,void * ud)522 static void unroll (lua_State *L, void *ud) {
523   UNUSED(ud);
524   for (;;) {
525     if (L->ci == &L->base_ci)  /* stack is empty? */
526       return;  /* coroutine finished normally */
527     if (!isLua(L->ci))  /* C function? */
528       finishCcall(L);
529     else {  /* Lua function */
530       luaV_finishOp(L);  /* finish interrupted instruction */
531       luaV_execute(L);  /* execute down to higher C 'boundary' */
532     }
533   }
534 }
535 
536 
537 /*
538 ** check whether thread has a suspended protected call
539 */
findpcall(lua_State * L)540 static CallInfo *findpcall (lua_State *L) {
541   CallInfo *ci;
542   for (ci = L->ci; ci != NULL; ci = ci->previous) {  /* search for a pcall */
543     if (ci->callstatus & CIST_YPCALL)
544       return ci;
545   }
546   return NULL;  /* no pending pcall */
547 }
548 
549 
recover(lua_State * L,int status)550 static int recover (lua_State *L, int status) {
551   StkId oldtop;
552   CallInfo *ci = findpcall(L);
553   if (ci == NULL) return 0;  /* no recovery point */
554   /* "finish" luaD_pcall */
555   oldtop = restorestack(L, ci->extra);
556   luaF_close(L, oldtop);
557   seterrorobj(L, status, oldtop);
558   L->ci = ci;
559   L->allowhook = ci->u.c.old_allowhook;
560   L->nny = 0;  /* should be zero to be yieldable */
561   luaD_shrinkstack(L);
562   L->errfunc = ci->u.c.old_errfunc;
563   ci->callstatus |= CIST_STAT;  /* call has error status */
564   ci->u.c.status = status;  /* (here it is) */
565   return 1;  /* continue running the coroutine */
566 }
567 
568 
569 /*
570 ** signal an error in the call to 'resume', not in the execution of the
571 ** coroutine itself. (Such errors should not be handled by any coroutine
572 ** error handler and should not kill the coroutine.)
573 */
resume_error(lua_State * L,const char * msg,StkId firstArg)574 static l_noret resume_error (lua_State *L, const char *msg, StkId firstArg) {
575   L->top = firstArg;  /* remove args from the stack */
576   setsvalue2s(L, L->top, luaS_new(L, msg));  /* push error message */
577   api_incr_top(L);
578   luaD_throw(L, -1);  /* jump back to 'lua_resume' */
579 }
580 
581 
582 /*
583 ** do the work for 'lua_resume' in protected mode
584 */
resume_cb(lua_State * L,void * ud)585 static void resume_cb (lua_State *L, void *ud) {
586   int nCcalls = L->nCcalls;
587   StkId firstArg = cast(StkId, ud);
588   CallInfo *ci = L->ci;
589   if (nCcalls >= LUAI_MAXCCALLS)
590     resume_error(L, "C stack overflow", firstArg);
591   if (L->status == LUA_OK) {  /* may be starting a coroutine */
592     if (ci != &L->base_ci)  /* not in base level? */
593       resume_error(L, "cannot resume non-suspended coroutine", firstArg);
594     /* coroutine is in base level; start running it */
595     if (!luaD_precall(L, firstArg - 1, LUA_MULTRET))  /* Lua function? */
596       luaV_execute(L);  /* call it */
597   }
598   else if (L->status != LUA_YIELD)
599     resume_error(L, "cannot resume dead coroutine", firstArg);
600   else {  /* resuming from previous yield */
601     L->status = LUA_OK;
602     ci->func = restorestack(L, ci->extra);
603     if (isLua(ci))  /* yielded inside a hook? */
604       luaV_execute(L);  /* just continue running Lua code */
605     else {  /* 'common' yield */
606       if (ci->u.c.k != NULL) {  /* does it have a continuation? */
607         int n;
608         ci->u.c.status = LUA_YIELD;  /* 'default' status */
609         ci->callstatus |= CIST_YIELDED;
610         lua_unlock(L);
611         n = (*ci->u.c.k)(L);  /* call continuation */
612         lua_lock(L);
613         api_checknelems(L, n);
614         firstArg = L->top - n;  /* yield results come from continuation */
615       }
616       luaD_poscall(L, firstArg);  /* finish 'luaD_precall' */
617     }
618     unroll(L, NULL);
619   }
620   lua_assert(nCcalls == L->nCcalls);
621 }
622 
623 
lua_resume(lua_State * L,lua_State * from,int nargs)624 LUA_API int lua_resume (lua_State *L, lua_State *from, int nargs) {
625   int status;
626   int oldnny = L->nny;  /* save 'nny' */
627   lua_lock(L);
628   luai_userstateresume(L, nargs);
629   L->nCcalls = (from) ? from->nCcalls + 1 : 1;
630   L->nny = 0;  /* allow yields */
631   api_checknelems(L, (L->status == LUA_OK) ? nargs + 1 : nargs);
632   status = luaD_rawrunprotected(L, resume_cb, L->top - nargs);
633   if (status == -1)  /* error calling 'lua_resume'? */
634     status = LUA_ERRRUN;
635   else {  /* yield or regular error */
636     while (status != LUA_OK && status != LUA_YIELD) {  /* error? */
637       if (recover(L, status))  /* recover point? */
638         status = luaD_rawrunprotected(L, unroll, NULL);  /* run continuation */
639       else {  /* unrecoverable error */
640         L->status = cast_byte(status);  /* mark thread as `dead' */
641         seterrorobj(L, status, L->top);
642         L->ci->top = L->top;
643         break;
644       }
645     }
646     lua_assert(status == L->status);
647   }
648   L->nny = oldnny;  /* restore 'nny' */
649   L->nCcalls--;
650   lua_assert(L->nCcalls == ((from) ? from->nCcalls : 0));
651   lua_unlock(L);
652   return status;
653 }
654 
655 
lua_yieldk(lua_State * L,int nresults,int ctx,lua_CFunction k)656 LUA_API int lua_yieldk (lua_State *L, int nresults, int ctx, lua_CFunction k) {
657   CallInfo *ci = L->ci;
658   luai_userstateyield(L, nresults);
659   lua_lock(L);
660   api_checknelems(L, nresults);
661   if (L->nny > 0) {
662     if (L != G(L)->mainthread)
663       luaG_runerror(L, "attempt to yield across a C-call boundary");
664     else
665       luaG_runerror(L, "attempt to yield from outside a coroutine");
666   }
667   L->status = LUA_YIELD;
668   ci->extra = savestack(L, ci->func);  /* save current 'func' */
669   if (isLua(ci)) {  /* inside a hook? */
670     api_check(L, k == NULL, "hooks cannot continue after yielding");
671   }
672   else {
673     if ((ci->u.c.k = k) != NULL)  /* is there a continuation? */
674       ci->u.c.ctx = ctx;  /* save context */
675     ci->func = L->top - nresults - 1;  /* protect stack below results */
676     luaD_throw(L, LUA_YIELD);
677   }
678   lua_assert(ci->callstatus & CIST_HOOKED);  /* must be inside a hook */
679   lua_unlock(L);
680   return 0;  /* return to 'luaD_hook' */
681 }
682 
683 
luaD_pcall(lua_State * L,Pfunc func,void * u,ptrdiff_t old_top,ptrdiff_t ef)684 int luaD_pcall (lua_State *L, Pfunc func, void *u,
685                 ptrdiff_t old_top, ptrdiff_t ef) {
686   int status;
687   CallInfo *old_ci = L->ci;
688   lu_byte old_allowhooks = L->allowhook;
689   unsigned short old_nny = L->nny;
690   ptrdiff_t old_errfunc = L->errfunc;
691   L->errfunc = ef;
692   status = luaD_rawrunprotected(L, func, u);
693   if (status != LUA_OK) {  /* an error occurred? */
694     StkId oldtop = restorestack(L, old_top);
695     luaF_close(L, oldtop);  /* close possible pending closures */
696     seterrorobj(L, status, oldtop);
697     L->ci = old_ci;
698     L->allowhook = old_allowhooks;
699     L->nny = old_nny;
700     luaD_shrinkstack(L);
701   }
702   L->errfunc = old_errfunc;
703   return status;
704 }
705 
706 
707 
708 /*
709 ** Execute a protected parser.
710 */
711 struct SParser {  /* data to `f_parser' */
712   ZIO *z;
713   Mbuffer buff;  /* dynamic structure used by the scanner */
714   Dyndata dyd;  /* dynamic structures used by the parser */
715   const char *mode;
716   const char *name;
717 };
718 
719 
checkmode(lua_State * L,const char * mode,const char * x)720 static void checkmode (lua_State *L, const char *mode, const char *x) {
721   if (mode && strchr(mode, x[0]) == NULL) {
722     luaO_pushfstring(L,
723        "attempt to load a %s chunk (mode is " LUA_QS ")", x, mode);
724     luaD_throw(L, LUA_ERRSYNTAX);
725   }
726 }
727 
728 
f_parser(lua_State * L,void * ud)729 static void f_parser (lua_State *L, void *ud) {
730   int i;
731   Closure *cl;
732   struct SParser *p = cast(struct SParser *, ud);
733   int c = zgetc(p->z);  /* read first character */
734   lua_assert(c != LUA_SIGNATURE[0]);	/* binary not supported */
735   checkmode(L, p->mode, "text");
736   cl = luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
737   lua_assert(cl->l.nupvalues == cl->l.p->sizeupvalues);
738   for (i = 0; i < cl->l.nupvalues; i++) {  /* initialize upvalues */
739     UpVal *up = luaF_newupval(L);
740     cl->l.upvals[i] = up;
741     luaC_objbarrier(L, cl, up);
742   }
743 }
744 
745 
luaD_protectedparser(lua_State * L,ZIO * z,const char * name,const char * mode)746 int luaD_protectedparser (lua_State *L, ZIO *z, const char *name,
747                                         const char *mode) {
748   struct SParser p;
749   int status;
750   L->nny++;  /* cannot yield during parsing */
751   p.z = z; p.name = name; p.mode = mode;
752   p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
753   p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
754   p.dyd.label.arr = NULL; p.dyd.label.size = 0;
755   luaZ_initbuffer(L, &p.buff);
756   status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
757   luaZ_freebuffer(L, &p.buff);
758   luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
759   luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
760   luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
761   L->nny--;
762   return status;
763 }
764