diff options
Diffstat (limited to 'src/lstate.c')
-rw-r--r-- | src/lstate.c | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/src/lstate.c b/src/lstate.c index 6e2801c4..e3aec6a3 100644 --- a/src/lstate.c +++ b/src/lstate.c @@ -1,11 +1,12 @@ /* -** $Id: lstate.c,v 2.92 2011/10/03 17:54:25 roberto Exp $ +** $Id: lstate.c,v 2.93 2012/02/01 21:57:15 roberto Exp $ ** Global State ** See Copyright Notice in lua.h */ #include <stddef.h> +#include <string.h> #define lstate_c #define LUA_CORE @@ -42,6 +43,17 @@ /* +** a macro to help the creation of a unique random seed when a state is +** created; the seed is used to randomize hashes. +*/ +#if !defined(luai_makeseed) +#include <time.h> +#define luai_makeseed(L) cast(size_t, time(NULL)) +#endif + + + +/* ** thread state + extra space */ typedef struct LX { @@ -66,6 +78,28 @@ typedef struct LG { /* +** Compute an initial seed as random as possible. In ANSI, rely on +** Address Space Layour Randomization (if present) to increase +** randomness.. +*/ +#define addbuff(b,p,e) \ + { size_t t = cast(size_t, e); \ + memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } + +static unsigned int makeseed (lua_State *L) { + char buff[4 * sizeof(size_t)]; + unsigned int h = luai_makeseed(); + int p = 0; + addbuff(buff, p, L); /* heap variable */ + addbuff(buff, p, &h); /* local variable */ + addbuff(buff, p, luaO_nilobject); /* global variable */ + addbuff(buff, p, &lua_newstate); /* public function */ + lua_assert(p == sizeof(buff)); + return luaS_hash(buff, p, h); +} + + +/* ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) ** invariant */ @@ -242,6 +276,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { g->frealloc = f; g->ud = ud; g->mainthread = L; + g->seed = makeseed(L); g->uvhead.u.l.prev = &g->uvhead; g->uvhead.u.l.next = &g->uvhead; g->gcrunning = 0; /* no GC while building state */ |