Skip to content

Commit 4a3c6dd

Browse files
sipaxanimo
authored andcommitted
Test that GetPerformanceCounter() increments
1 parent 930acbf commit 4a3c6dd

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/random.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdlib.h>
1818
#include <limits>
1919
#include <chrono>
20+
#include <thread>
2021

2122
#ifndef WIN32
2223
#include <sys/time.h>
@@ -294,6 +295,8 @@ FastRandomContext::FastRandomContext(const uint256& seed) : requires_seed(false)
294295

295296
bool Random_SanityCheck()
296297
{
298+
uint64_t start = GetPerformanceCounter();
299+
297300
/* This does not measure the quality of randomness, but it does test that
298301
* OSRandom() overwrites all 32 bytes of the output given a maximum
299302
* number of tries.
@@ -320,7 +323,14 @@ bool Random_SanityCheck()
320323

321324
tries += 1;
322325
} while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
323-
return (num_overwritten == NUM_OS_RANDOM_BYTES); /* If this failed, bailed out after too many tries */
326+
if (num_overwritten != NUM_OS_RANDOM_BYTES) return false; /* If this failed, bailed out after too many tries */
327+
328+
// Check that GetPerformanceCounter increases at least during a GetOSRand() call + 1ms sleep.
329+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
330+
uint64_t stop = GetPerformanceCounter();
331+
if (stop == start) return false;
332+
333+
return true;
324334
}
325335

326336
FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)

0 commit comments

Comments
 (0)