[Mesa-dev] [PATCH v2] util/rand_xor: seed with getentropy when available

Nicolai Hähnle nhaehnle at gmail.com
Thu Mar 23 11:42:39 UTC 2017


On 23.03.2017 07:19, Jonathan Gray wrote:
> On Thu, Mar 23, 2017 at 03:24:16PM +1100, Jonathan Gray wrote:
>> Instead of using using /dev/urandom on Linux and time(NULL) elsewhere
>> for a seed first use getentropy() for systems that have a kernel
>> interface to get a seed such as OpenBSD.  This interface is also
>> present in other systems such as Solaris and even Linux with a recent
>> version of glibc.
>>
>> v2: check for/use the different header Solaris and glibc use
>>
>> Signed-off-by: Jonathan Gray <jsg at jsg.id.au>
>
> Even just changing the original to have the following would be an
> improvement.

I like this suggestion and the first patch you sent (the getentropy 
one). Perhaps you can provide a combined patch or a sequence of two 
patches for that?

Thanks
Nicolai

>
> diff --git a/src/util/rand_xor.c b/src/util/rand_xor.c
> index de05fa64b3..9780907368 100644
> --- a/src/util/rand_xor.c
> +++ b/src/util/rand_xor.c
> @@ -22,12 +22,9 @@
>   *
>   */
>
> -#if defined(__linux__)
> -#include <sys/file.h>
> +#include <fcntl.h>
>  #include <unistd.h>
> -#else
>  #include <time.h>
> -#endif
>
>  #include "rand_xor.h"
>
> @@ -53,29 +50,34 @@ rand_xorshift128plus(uint64_t *seed)
>  void
>  s_rand_xorshift128plus(uint64_t *seed, bool randomised_seed)
>  {
> +   time_t now;
> +
>     if (!randomised_seed)
>        goto fixed_seed;
>
> -#if defined(__linux__)
>     int fd = open("/dev/urandom", O_RDONLY);
>     if (fd < 0)
> -      goto fixed_seed;
> +      goto time_seed;
>
>     size_t seed_size = sizeof(uint64_t) * 2;
>     if (read(fd, seed, seed_size) != seed_size) {
>        close(fd);
> -      goto fixed_seed;
> +      goto time_seed;
>     }
>
>     close(fd);
>     return;
>
> -#else
> +time_seed:
> +
> +   now = time(NULL);
> +   if (now == -1)
> +      goto fixed_seed;
> +
>     seed[0] = 0x3bffb83978e24f88;
> -   seed[1] = time(NULL);
> +   seed[1] = now;
>
>     return;
> -#endif
>
>  fixed_seed:
>
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev
>


-- 
Lerne, wie die Welt wirklich ist,
Aber vergiss niemals, wie sie sein sollte.


More information about the mesa-dev mailing list