mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-08-07 12:28:24 +00:00
[Support] Fix sys::GetRandomNumber() to always use a high quality seed.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@156414 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -12,6 +12,8 @@
|
|||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
#include "Unix.h"
|
#include "Unix.h"
|
||||||
|
#include "llvm/ADT/Hashing.h"
|
||||||
|
#include "llvm/Support/TimeValue.h"
|
||||||
#ifdef HAVE_SYS_TIME_H
|
#ifdef HAVE_SYS_TIME_H
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#endif
|
#endif
|
||||||
@@ -300,13 +302,21 @@ const char *Process::ResetColor() {
|
|||||||
|
|
||||||
#if !defined(HAVE_ARC4RANDOM)
|
#if !defined(HAVE_ARC4RANDOM)
|
||||||
static unsigned GetRandomNumberSeed() {
|
static unsigned GetRandomNumberSeed() {
|
||||||
unsigned seed = ::time(NULL); // FIXME: It might not provide unique seed.
|
// Attempt to get the initial seed from /dev/urandom, if possible.
|
||||||
FILE *RandomSource = ::fopen("/dev/urandom", "r");
|
if (FILE *RandomSource = ::fopen("/dev/urandom", "r")) {
|
||||||
if (RandomSource) {
|
unsigned seed;
|
||||||
::fread((void *)&seed, sizeof(seed), 1, RandomSource);
|
int count = ::fread((void *)&seed, sizeof(seed), 1, RandomSource);
|
||||||
::fclose(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
|
#endif
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user