1 /*
2 ** $Id: lstate.c,v 2.99.1.2 2013/11/08 17:45:31 roberto Exp $
3 ** Global State
4 ** See Copyright Notice in lua.h
5 */
6 
7 
8 #include <stddef.h>
9 #include <string.h>
10 
11 #define lstate_c
12 #define LUA_CORE
13 
14 #include "lua.h"
15 
16 #include "lapi.h"
17 #include "ldebug.h"
18 #include "ldo.h"
19 #include "lfunc.h"
20 #include "lgc.h"
21 #include "llex.h"
22 #include "lmem.h"
23 #include "lstate.h"
24 #include "lstring.h"
25 #include "ltable.h"
26 #include "ltm.h"
27 
28 
29 #if !defined(LUAI_GCPAUSE)
30 #define LUAI_GCPAUSE	200  /* 200% */
31 #endif
32 
33 #if !defined(LUAI_GCMAJOR)
34 #define LUAI_GCMAJOR	200  /* 200% */
35 #endif
36 
37 #if !defined(LUAI_GCMUL)
38 #define LUAI_GCMUL	200 /* GC runs 'twice the speed' of memory allocation */
39 #endif
40 
41 
42 #define MEMERRMSG	"not enough memory"
43 
44 
45 /*
46 ** a macro to help the creation of a unique random seed when a state is
47 ** created; the seed is used to randomize hashes.
48 */
49 #if !defined(luai_makeseed)
50 #ifndef SYSLINUX
51 #include <time.h>
52 #define luai_makeseed()		cast(unsigned int, time(NULL))
53 #else
54 #include <sys/times.h>
55 #define luai_makeseed()		cast(unsigned int, times(NULL))
56 #endif /* SYSLINUX */
57 #endif
58 
59 
60 
61 /*
62 ** thread state + extra space
63 */
64 typedef struct LX {
65 #if defined(LUAI_EXTRASPACE)
66   char buff[LUAI_EXTRASPACE];
67 #endif
68   lua_State l;
69 } LX;
70 
71 
72 /*
73 ** Main thread combines a thread state and the global state
74 */
75 typedef struct LG {
76   LX l;
77   global_State g;
78 } LG;
79 
80 
81 
82 #define fromstate(L)	(cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
83 
84 
85 /*
86 ** Compute an initial seed as random as possible. In ANSI, rely on
87 ** Address Space Layout Randomization (if present) to increase
88 ** randomness..
89 */
90 #define addbuff(b,p,e) \
91   { size_t t = cast(size_t, e); \
92     memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); }
93 
makeseed(lua_State * L)94 static unsigned int makeseed (lua_State *L) {
95   char buff[4 * sizeof(size_t)];
96   unsigned int h = luai_makeseed();
97   int p = 0;
98   addbuff(buff, p, L);  /* heap variable */
99   addbuff(buff, p, &h);  /* local variable */
100   addbuff(buff, p, luaO_nilobject);  /* global variable */
101   addbuff(buff, p, &lua_newstate);  /* public function */
102   lua_assert(p == sizeof(buff));
103   return luaS_hash(buff, p, h);
104 }
105 
106 
107 /*
108 ** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
109 ** invariant
110 */
luaE_setdebt(global_State * g,l_mem debt)111 void luaE_setdebt (global_State *g, l_mem debt) {
112   g->totalbytes -= (debt - g->GCdebt);
113   g->GCdebt = debt;
114 }
115 
116 
luaE_extendCI(lua_State * L)117 CallInfo *luaE_extendCI (lua_State *L) {
118   CallInfo *ci = luaM_new(L, CallInfo);
119   lua_assert(L->ci->next == NULL);
120   L->ci->next = ci;
121   ci->previous = L->ci;
122   ci->next = NULL;
123   return ci;
124 }
125 
126 
luaE_freeCI(lua_State * L)127 void luaE_freeCI (lua_State *L) {
128   CallInfo *ci = L->ci;
129   CallInfo *next = ci->next;
130   ci->next = NULL;
131   while ((ci = next) != NULL) {
132     next = ci->next;
133     luaM_free(L, ci);
134   }
135 }
136 
137 
stack_init(lua_State * L1,lua_State * L)138 static void stack_init (lua_State *L1, lua_State *L) {
139   int i; CallInfo *ci;
140   /* initialize stack array */
141   L1->stack = luaM_newvector(L, BASIC_STACK_SIZE, TValue);
142   L1->stacksize = BASIC_STACK_SIZE;
143   for (i = 0; i < BASIC_STACK_SIZE; i++)
144     setnilvalue(L1->stack + i);  /* erase new stack */
145   L1->top = L1->stack;
146   L1->stack_last = L1->stack + L1->stacksize - EXTRA_STACK;
147   /* initialize first ci */
148   ci = &L1->base_ci;
149   ci->next = ci->previous = NULL;
150   ci->callstatus = 0;
151   ci->func = L1->top;
152   setnilvalue(L1->top++);  /* 'function' entry for this 'ci' */
153   ci->top = L1->top + LUA_MINSTACK;
154   L1->ci = ci;
155 }
156 
157 
freestack(lua_State * L)158 static void freestack (lua_State *L) {
159   if (L->stack == NULL)
160     return;  /* stack not completely built yet */
161   L->ci = &L->base_ci;  /* free the entire 'ci' list */
162   luaE_freeCI(L);
163   luaM_freearray(L, L->stack, L->stacksize);  /* free stack array */
164 }
165 
166 
167 /*
168 ** Create registry table and its predefined values
169 */
init_registry(lua_State * L,global_State * g)170 static void init_registry (lua_State *L, global_State *g) {
171   TValue mt;
172   /* create registry */
173   Table *registry = luaH_new(L);
174   sethvalue(L, &g->l_registry, registry);
175   luaH_resize(L, registry, LUA_RIDX_LAST, 0);
176   /* registry[LUA_RIDX_MAINTHREAD] = L */
177   setthvalue(L, &mt, L);
178   luaH_setint(L, registry, LUA_RIDX_MAINTHREAD, &mt);
179   /* registry[LUA_RIDX_GLOBALS] = table of globals */
180   sethvalue(L, &mt, luaH_new(L));
181   luaH_setint(L, registry, LUA_RIDX_GLOBALS, &mt);
182 }
183 
184 
185 /*
186 ** open parts of the state that may cause memory-allocation errors
187 */
f_luaopen(lua_State * L,void * ud)188 static void f_luaopen (lua_State *L, void *ud) {
189   global_State *g = G(L);
190   UNUSED(ud);
191   stack_init(L, L);  /* init stack */
192   init_registry(L, g);
193   luaS_resize(L, MINSTRTABSIZE);  /* initial size of string table */
194   luaT_init(L);
195   luaX_init(L);
196   /* pre-create memory-error message */
197   g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
198   luaS_fix(g->memerrmsg);  /* it should never be collected */
199   g->gcrunning = 1;  /* allow gc */
200   g->version = lua_version(NULL);
201   luai_userstateopen(L);
202 }
203 
204 
205 /*
206 ** preinitialize a state with consistent values without allocating
207 ** any memory (to avoid errors)
208 */
preinit_state(lua_State * L,global_State * g)209 static void preinit_state (lua_State *L, global_State *g) {
210   G(L) = g;
211   L->stack = NULL;
212   L->ci = NULL;
213   L->stacksize = 0;
214   L->errorJmp = NULL;
215   L->nCcalls = 0;
216   L->hook = NULL;
217   L->hookmask = 0;
218   L->basehookcount = 0;
219   L->allowhook = 1;
220   resethookcount(L);
221   L->openupval = NULL;
222   L->nny = 1;
223   L->status = LUA_OK;
224   L->errfunc = 0;
225 }
226 
227 
close_state(lua_State * L)228 static void close_state (lua_State *L) {
229   global_State *g = G(L);
230   luaF_close(L, L->stack);  /* close all upvalues for this thread */
231   luaC_freeallobjects(L);  /* collect all objects */
232   if (g->version)  /* closing a fully built state? */
233     luai_userstateclose(L);
234   luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
235   luaZ_freebuffer(L, &g->buff);
236   freestack(L);
237   lua_assert(gettotalbytes(g) == sizeof(LG));
238   (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);  /* free main block */
239 }
240 
241 
lua_newthread(lua_State * L)242 LUA_API lua_State *lua_newthread (lua_State *L) {
243   lua_State *L1;
244   lua_lock(L);
245   luaC_checkGC(L);
246   L1 = &luaC_newobj(L, LUA_TTHREAD, sizeof(LX), NULL, offsetof(LX, l))->th;
247   setthvalue(L, L->top, L1);
248   api_incr_top(L);
249   preinit_state(L1, G(L));
250   L1->hookmask = L->hookmask;
251   L1->basehookcount = L->basehookcount;
252   L1->hook = L->hook;
253   resethookcount(L1);
254   luai_userstatethread(L, L1);
255   stack_init(L1, L);  /* init stack */
256   lua_unlock(L);
257   return L1;
258 }
259 
260 
luaE_freethread(lua_State * L,lua_State * L1)261 void luaE_freethread (lua_State *L, lua_State *L1) {
262   LX *l = fromstate(L1);
263   luaF_close(L1, L1->stack);  /* close all upvalues for this thread */
264   lua_assert(L1->openupval == NULL);
265   luai_userstatefree(L, L1);
266   freestack(L1);
267   luaM_free(L, l);
268 }
269 
270 
lua_newstate(lua_Alloc f,void * ud)271 LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) {
272   int i;
273   lua_State *L;
274   global_State *g;
275   LG *l = cast(LG *, (*f)(ud, NULL, LUA_TTHREAD, sizeof(LG)));
276   if (l == NULL) return NULL;
277   L = &l->l.l;
278   g = &l->g;
279   L->next = NULL;
280   L->tt = LUA_TTHREAD;
281   g->currentwhite = bit2mask(WHITE0BIT, FIXEDBIT);
282   L->marked = luaC_white(g);
283   g->gckind = KGC_NORMAL;
284   preinit_state(L, g);
285   g->frealloc = f;
286   g->ud = ud;
287   g->mainthread = L;
288   g->seed = makeseed(L);
289   g->uvhead.u.l.prev = &g->uvhead;
290   g->uvhead.u.l.next = &g->uvhead;
291   g->gcrunning = 0;  /* no GC while building state */
292   g->GCestimate = 0;
293   g->strt.size = 0;
294   g->strt.nuse = 0;
295   g->strt.hash = NULL;
296   setnilvalue(&g->l_registry);
297   luaZ_initbuffer(L, &g->buff);
298   g->panic = NULL;
299   g->version = NULL;
300   g->gcstate = GCSpause;
301   g->allgc = NULL;
302   g->finobj = NULL;
303   g->tobefnz = NULL;
304   g->sweepgc = g->sweepfin = NULL;
305   g->gray = g->grayagain = NULL;
306   g->weak = g->ephemeron = g->allweak = NULL;
307   g->totalbytes = sizeof(LG);
308   g->GCdebt = 0;
309   g->gcpause = LUAI_GCPAUSE;
310   g->gcmajorinc = LUAI_GCMAJOR;
311   g->gcstepmul = LUAI_GCMUL;
312   for (i=0; i < LUA_NUMTAGS; i++) g->mt[i] = NULL;
313   if (luaD_rawrunprotected(L, f_luaopen, NULL) != LUA_OK) {
314     /* memory allocation error: free partial state */
315     close_state(L);
316     L = NULL;
317   }
318   return L;
319 }
320 
321 
lua_close(lua_State * L)322 LUA_API void lua_close (lua_State *L) {
323   L = G(L)->mainthread;  /* only the main thread can be closed */
324   lua_lock(L);
325   close_state(L);
326 }
327 
328 
329