From 5a04f1851e0d42b4bcbb0af103490bc964e985aa Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 20 Mar 2023 16:13:17 -0300 Subject: 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. --- lauxlib.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 49 insertions(+), 1 deletion(-) (limited to 'lauxlib.c') diff --git a/lauxlib.c b/lauxlib.c index 4ca6c654..64b325d3 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1091,8 +1091,56 @@ static void warnfon (void *ud, const char *message, int tocont) { } + +/* +** A function to compute an unsigned int with some level of +** randomness. Rely on Address Space Layout Randomization (if present), +** current time, and clock. +*/ +#if !defined(luai_makeseed) + +#include + + +/* +** Size of 'e' measured in number of 'unsigned int's. (In the weird +** case that the division truncates, we just lose some part of the +** value, no big deal.) +*/ +#define sof(e) (sizeof(e) / sizeof(unsigned int)) + + +#define addbuff(b,v) \ + (memcpy(b, &(v), sof(v) * sizeof(unsigned int)), b += sof(v)) + + +static unsigned int luai_makeseed (void) { + unsigned int buff[sof(void*) + sof(clock_t) + sof(time_t)]; + unsigned int res; + unsigned int *b = buff; + clock_t c = clock(); + time_t t = time(NULL); + void *h = buff; + addbuff(b, h); /* local variable's address */ + addbuff(b, c); /* clock */ + addbuff(b, t); /* time */ + res = buff[0]; + for (b = buff + 1; b < buff + sof(buff); b++) + res += *b; + return res; +} + +#endif + + +LUALIB_API unsigned int luaL_makeseed (lua_State *L) { + (void)L; /* unused */ + return luai_makeseed(); +} + + LUALIB_API lua_State *luaL_newstate (void) { - lua_State *L = lua_newstate(l_alloc, NULL); + lua_State *L = lua_newstate(l_alloc, NULL, luai_makeseed()); if (l_likely(L)) { lua_atpanic(L, &panic); lua_setwarnf(L, warnfoff, L); /* default is warnings off */ -- cgit v1.2.3-55-g6feb