random seed used in the hash of all strings to avoid intentional

collisions
This commit is contained in:
Roberto Ierusalimschy
2012-02-01 19:57:15 -02:00
parent a4b96ce9a3
commit 678c1255c9
5 changed files with 49 additions and 12 deletions

View File

@@ -1,11 +1,12 @@
/*
** $Id: lstate.c,v 2.91 2011/08/23 17:24:34 roberto Exp roberto $
** $Id: lstate.c,v 2.92 2011/10/03 17:54:25 roberto Exp roberto $
** Global State
** See Copyright Notice in lua.h
*/
#include <stddef.h>
#include <string.h>
#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 <time.h>
#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 */