From 1cb937cf2d4583482aadaff45b6628b39fdcd91e Mon Sep 17 00:00:00 2001 From: Lua Team Date: Wed, 21 Mar 2012 12:00:00 +0000 Subject: Lua 5.2.1-work1 --- src/lstate.c | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'src/lstate.c') 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 +#include #define lstate_c #define LUA_CORE @@ -41,6 +42,17 @@ #define MEMERRMSG "not enough memory" +/* +** 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 +#define luai_makeseed(L) cast(size_t, time(NULL)) +#endif + + + /* ** thread state + extra space */ @@ -65,6 +77,28 @@ typedef struct LG { #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l))) +/* +** 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 */ -- cgit v1.2.1