diff options
Diffstat (limited to 'lstate.c')
| -rw-r--r-- | lstate.c | 34 |
1 files changed, 17 insertions, 17 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lstate.c,v 2.150 2018/01/28 15:13:26 roberto Exp roberto $ | 2 | ** $Id: lstate.c,v 2.151 2018/02/05 17:11:37 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 | */ |
| @@ -30,17 +30,6 @@ | |||
| 30 | 30 | ||
| 31 | 31 | ||
| 32 | /* | 32 | /* |
| 33 | ** a macro to help the creation of a unique random seed when a state is | ||
| 34 | ** created; the seed is used to randomize hashes. | ||
| 35 | */ | ||
| 36 | #if !defined(luai_makeseed) | ||
| 37 | #include <time.h> | ||
| 38 | #define luai_makeseed() cast_uint(time(NULL)) | ||
| 39 | #endif | ||
| 40 | |||
| 41 | |||
| 42 | |||
| 43 | /* | ||
| 44 | ** thread state + extra space | 33 | ** thread state + extra space |
| 45 | */ | 34 | */ |
| 46 | typedef struct LX { | 35 | typedef struct LX { |
| @@ -63,16 +52,25 @@ typedef struct LG { | |||
| 63 | 52 | ||
| 64 | 53 | ||
| 65 | /* | 54 | /* |
| 66 | ** Compute an initial seed as random as possible. Rely on Address Space | 55 | ** A macro to create a "random" seed when a state is created; |
| 67 | ** Layout Randomization (if present) to increase randomness.. | 56 | ** the seed is used to randomize string hashes. |
| 57 | */ | ||
| 58 | #if !defined(luai_makeseed) | ||
| 59 | |||
| 60 | #include <time.h> | ||
| 61 | |||
| 62 | /* | ||
| 63 | ** Compute an initial seed with some level of randomness. | ||
| 64 | ** Rely on Address Space Layout Randomization (if present) and | ||
| 65 | ** current time. | ||
| 68 | */ | 66 | */ |
| 69 | #define addbuff(b,p,e) \ | 67 | #define addbuff(b,p,e) \ |
| 70 | { size_t t = cast_sizet(e); \ | 68 | { size_t t = cast_sizet(e); \ |
| 71 | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } | 69 | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } |
| 72 | 70 | ||
| 73 | static unsigned int makeseed (lua_State *L) { | 71 | static unsigned int luai_makeseed (lua_State *L) { |
| 74 | char buff[4 * sizeof(size_t)]; | 72 | char buff[4 * sizeof(size_t)]; |
| 75 | unsigned int h = luai_makeseed(); | 73 | unsigned int h = cast_uint(time(NULL)); |
| 76 | int p = 0; | 74 | int p = 0; |
| 77 | addbuff(buff, p, L); /* heap variable */ | 75 | addbuff(buff, p, L); /* heap variable */ |
| 78 | addbuff(buff, p, &h); /* local variable */ | 76 | addbuff(buff, p, &h); /* local variable */ |
| @@ -82,6 +80,8 @@ static unsigned int makeseed (lua_State *L) { | |||
| 82 | return luaS_hash(buff, p, h); | 80 | return luaS_hash(buff, p, h); |
| 83 | } | 81 | } |
| 84 | 82 | ||
| 83 | #endif | ||
| 84 | |||
| 85 | 85 | ||
| 86 | /* | 86 | /* |
| 87 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) | 87 | ** set GCdebt to a new value keeping the value (totalbytes + GCdebt) |
| @@ -327,7 +327,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
| 327 | g->frealloc = f; | 327 | g->frealloc = f; |
| 328 | g->ud = ud; | 328 | g->ud = ud; |
| 329 | g->mainthread = L; | 329 | g->mainthread = L; |
| 330 | g->seed = makeseed(L); | 330 | g->seed = luai_makeseed(L); |
| 331 | g->gcrunning = 0; /* no GC while building state */ | 331 | g->gcrunning = 0; /* no GC while building state */ |
| 332 | g->strt.size = g->strt.nuse = 0; | 332 | g->strt.size = g->strt.nuse = 0; |
| 333 | g->strt.hash = NULL; | 333 | g->strt.hash = NULL; |
