diff options
author | Daniel Dunbar <daniel@zuster.org> | 2012-05-08 20:38:00 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2012-05-08 20:38:00 +0000 |
commit | 5f1c956eb0239b15fc5ec5cebd4194dfb7b2a8ec (patch) | |
tree | 97f492bb73d14733cc19af5bb84c096c0955c558 /llvm/lib/Support/Unix | |
parent | 5bad4799a3d355724cd9ef277629a8fffc996016 (diff) | |
download | bcm5719-llvm-5f1c956eb0239b15fc5ec5cebd4194dfb7b2a8ec.tar.gz bcm5719-llvm-5f1c956eb0239b15fc5ec5cebd4194dfb7b2a8ec.zip |
[Support] Fix sys::GetRandomNumber() to always use a high quality seed.
llvm-svn: 156414
Diffstat (limited to 'llvm/lib/Support/Unix')
-rw-r--r-- | llvm/lib/Support/Unix/Process.inc | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/llvm/lib/Support/Unix/Process.inc b/llvm/lib/Support/Unix/Process.inc index dd855b0d3ff..4e1bd5db142 100644 --- a/llvm/lib/Support/Unix/Process.inc +++ b/llvm/lib/Support/Unix/Process.inc @@ -12,6 +12,8 @@ //===----------------------------------------------------------------------===// #include "Unix.h" +#include "llvm/ADT/Hashing.h" +#include "llvm/Support/TimeValue.h" #ifdef HAVE_SYS_TIME_H #include <sys/time.h> #endif @@ -300,13 +302,21 @@ const char *Process::ResetColor() { #if !defined(HAVE_ARC4RANDOM) static unsigned GetRandomNumberSeed() { - unsigned seed = ::time(NULL); // FIXME: It might not provide unique seed. - FILE *RandomSource = ::fopen("/dev/urandom", "r"); - if (RandomSource) { - ::fread((void *)&seed, sizeof(seed), 1, RandomSource); + // Attempt to get the initial seed from /dev/urandom, if possible. + if (FILE *RandomSource = ::fopen("/dev/urandom", "r")) { + unsigned seed; + int count = ::fread((void *)&seed, sizeof(seed), 1, RandomSource); ::fclose(RandomSource); + + // Return the seed if the read was successful. + if (count == 1) + return seed; } - return seed; + + // Otherwise, swizzle the current time and the process ID to form a reasonable + // seed. + TimeValue Now = llvm::TimeValue::now(); + return hash_combine(Now.seconds(), Now.nanoseconds(), ::getpid()); } #endif |