diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
commit | 5a04f1851e0d42b4bcbb0af103490bc964e985aa (patch) | |
tree | 227f98d4fff3f1bd1eea4d20ddd4aa0ec7562ddd /lstate.c | |
parent | 8c064fdc23bd745bbd3456a58cc9e2521f8e4263 (diff) | |
download | lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.gz lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.bz2 lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.zip |
New function 'luaL_makeseed'
This function unifies code from 'lua_newstate', 'math.randomseed',
and 'table.sort' that tries to create a value with a minimum level
of randomness.
Diffstat (limited to 'lstate.c')
-rw-r--r-- | lstate.c | 35 |
1 files changed, 2 insertions, 33 deletions
@@ -52,37 +52,6 @@ typedef struct LG { | |||
52 | 52 | ||
53 | 53 | ||
54 | /* | 54 | /* |
55 | ** A macro to create a "random" seed when a state is created; | ||
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. | ||
66 | */ | ||
67 | #define addbuff(b,p,e) \ | ||
68 | { size_t t = cast_sizet(e); \ | ||
69 | memcpy(b + p, &t, sizeof(t)); p += sizeof(t); } | ||
70 | |||
71 | static unsigned int luai_makeseed (lua_State *L) { | ||
72 | char buff[3 * sizeof(size_t)]; | ||
73 | unsigned int h = cast_uint(time(NULL)); | ||
74 | int p = 0; | ||
75 | addbuff(buff, p, L); /* heap variable */ | ||
76 | addbuff(buff, p, &h); /* local variable */ | ||
77 | addbuff(buff, p, &lua_newstate); /* public function */ | ||
78 | lua_assert(p == sizeof(buff)); | ||
79 | return luaS_hash(buff, p, h); | ||
80 | } | ||
81 | |||
82 | #endif | ||
83 | |||
84 | |||
85 | /* | ||
86 | ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) | 55 | ** set GCdebt to a new value keeping the value (totalobjs + GCdebt) |
87 | ** invariant (and avoiding underflows in 'totalobjs') | 56 | ** invariant (and avoiding underflows in 'totalobjs') |
88 | */ | 57 | */ |
@@ -350,7 +319,7 @@ LUA_API int lua_resetthread (lua_State *L, lua_State *from) { | |||
350 | } | 319 | } |
351 | 320 | ||
352 | 321 | ||
353 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | 322 | LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud, unsigned int seed) { |
354 | int i; | 323 | int i; |
355 | lua_State *L; | 324 | lua_State *L; |
356 | global_State *g; | 325 | global_State *g; |
@@ -370,7 +339,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { | |||
370 | g->warnf = NULL; | 339 | g->warnf = NULL; |
371 | g->ud_warn = NULL; | 340 | g->ud_warn = NULL; |
372 | g->mainthread = L; | 341 | g->mainthread = L; |
373 | g->seed = luai_makeseed(L); | 342 | g->seed = seed; |
374 | g->gcstp = GCSTPGC; /* no GC while building state */ | 343 | g->gcstp = GCSTPGC; /* no GC while building state */ |
375 | g->strt.size = g->strt.nuse = 0; | 344 | g->strt.size = g->strt.nuse = 0; |
376 | g->strt.hash = NULL; | 345 | g->strt.hash = NULL; |