More completely seed UUID::Random()
Generate enuogh randomness with std::random_device to fully seed the
internal state of an std::mt19937. This ensures that even the first
UUID::Random() call after boot has full entropy, so long as
`std::random_device` is non-deterministic.
Also, initialize the random number generator during AOS initialization.
Change-Id: Ie09d85f09f7969bcd47576a577b4415d39186233
Signed-off-by: James Kuszmaul <james.kuszmaul@bluerivertech.com>
diff --git a/aos/uuid.h b/aos/uuid.h
index 1b63ac3..371eb6d 100644
--- a/aos/uuid.h
+++ b/aos/uuid.h
@@ -3,6 +3,7 @@
#include <array>
#include <ostream>
+#include <random>
#include <string>
#include "absl/types/span.h"
@@ -20,6 +21,9 @@
static constexpr size_t kDataSize = 16;
// Returns a randomly generated UUID. This is known as a UUID4.
+ // The first Random() call in a thread will tend to be slightly slower than
+ // the rest so that it can seed the pseudo-random number generator used
+ // internally.
static UUID Random();
// Returns a uuid with all '0's.
@@ -95,6 +99,13 @@
std::ostream &operator<<(std::ostream &os, const UUID &uuid);
+namespace internal {
+// Initializes a mt19937 with as much entropy as it can take (rather than just a
+// 32-bit value from std::random_device).
+// Exposed for testing purposes.
+std::mt19937 FullySeededRandomGenerator();
+} // namespace internal
+
} // namespace aos
#endif // AOS_EVENTS_LOGGING_UUID_H_