diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-02-01 19:57:15 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2012-02-01 19:57:15 -0200 |
commit | 678c1255c92eed9c2c7564d340a5563b17395158 (patch) | |
tree | ca35d119d770835b59d34cdec93805190d3155d1 /lstate.c | |
parent | a4b96ce9a3305ae3585c0bb143fa7342c140f20b (diff) | |
download | lua-678c1255c92eed9c2c7564d340a5563b17395158.tar.gz lua-678c1255c92eed9c2c7564d340a5563b17395158.tar.bz2 lua-678c1255c92eed9c2c7564d340a5563b17395158.zip |
random seed used in the hash of all strings to avoid intentional
collisions
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 37 |
1 files changed, 36 insertions, 1 deletions
@@ -1,11 +1,12 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstate.c,v 2.91 2011/08/23 17:24:34 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.92 2011/10/03 17:54:25 roberto Exp roberto $ |
3 | ** Global State | 3 | ** Global State |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
6 | 6 | ||
7 | 7 | ||
8 | #include <stddef.h> | 8 | #include <stddef.h> |
9 | #include <string.h> | ||
9 | 10 | ||
10 | #define lstate_c | 11 | #define lstate_c |
11 | #define LUA_CORE | 12 | #define LUA_CORE |
@@ -42,6 +43,17 @@ | |||
42 | 43 | ||
43 | 44 | ||
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 | #include <time.h> | ||
51 | #define luai_makeseed(L) cast(size_t, time(NULL)) | ||
52 | #endif | ||
53 | |||
54 | |||
55 | |||
56 | /* | ||
45 | ** thread state + extra space | 57 | ** thread state + extra space |
46 | */ | 58 | */ |
47 | typedef struct LX { | 59 | typedef struct LX { |
@@ -66,6 +78,28 @@ typedef struct LG { | |||
66 | 78 | ||
67 | 79 | ||
68 | /* | 80 | /* |
81 | ** Compute an initial seed as random as possible. In ANSI, rely on | ||
82 | ** Address Space Layour Randomization (if present) to increase | ||
83 | ** randomness.. | ||
84 | */ | ||
85 | #define addbuff(b,p,e) \ | ||
86 | { size_t t = cast(size_t, e); \ | ||
87 | memcpy(buff + p, &t, sizeof(t)); p += sizeof(t); } | ||
88 | |||
89 | static unsigned int makeseed (lua_State *L) { | ||
90 | char buff[4 * sizeof(size_t)]; | ||
91 | unsigned int h = luai_makeseed(); | ||
92 | int p = 0; | ||
93 | addbuff(buff, p, L); /* heap variable */ | ||
94 | addbuff(buff, p, &h); /* local variable */ | ||
95 | addbuff(buff, p, luaO_nilobject); /* global variable */ | ||
96 | addbuff(buff, p, &lua_newstate); /* public function */ | ||
97 | lua_assert(p == sizeof(buff)); | ||
98 | return luaS_hash(buff, p, h); | ||
99 | } | ||
100 | |||
101 | |||
102 | /* | ||
69 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) | 103 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
70 | ** invariant | 104 | ** invariant |
71 | */ | 105 | */ |
@@ -242,6 +276,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
242 | g->frealloc = f; | 276 | g->frealloc = f; |
243 | g->ud = ud; | 277 | g->ud = ud; |
244 | g->mainthread = L; | 278 | g->mainthread = L; |
279 | g->seed = makeseed(L); | ||
245 | g->uvhead.u.l.prev = &g->uvhead; | 280 | g->uvhead.u.l.prev = &g->uvhead; |
246 | g->uvhead.u.l.next = &g->uvhead; | 281 | g->uvhead.u.l.next = &g->uvhead; |
247 | g->gcrunning = 0; /* no GC while building state */ | 282 | g->gcrunning = 0; /* no GC while building state */ |