X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FRandom.cpp;h=87da68b6b5216469591efd17ad68466fd1c00068;hb=0347a79f57ee6e1179c0482adacf3a7e6ff890ff;hp=e69a4452564e0fb32744cbf3e05d28b5dd37b089;hpb=52fceaf485e63d459d2df5858ffdbeaafe851dbf;p=folly.git diff --git a/folly/Random.cpp b/folly/Random.cpp index e69a4452..87da68b6 100644 --- a/folly/Random.cpp +++ b/folly/Random.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,7 +14,7 @@ * limitations under the License. */ -#include "folly/Random.h" +#include #include #include @@ -22,61 +22,96 @@ #include #include -#if __GNUC_PREREQ(4, 8) -#include -#define USE_SIMD_PRNG -#endif +#include +#include +#include namespace folly { namespace { -std::atomic seedInput(0); -} -uint32_t randomNumberSeed() { - struct timeval tv; - gettimeofday(&tv, nullptr); - const uint32_t kPrime0 = 51551; - const uint32_t kPrime1 = 61631; - const uint32_t kPrime2 = 64997; - const uint32_t kPrime3 = 111857; - return kPrime0 * (seedInput++) - + kPrime1 * static_cast(getpid()) - + kPrime2 * static_cast(tv.tv_sec) - + kPrime3 * static_cast(tv.tv_usec); +void readRandomDevice(void* data, size_t size) { + // Keep the random device open for the duration of the program. + static int randomFd = ::open("/dev/urandom", O_RDONLY); + PCHECK(randomFd >= 0); + auto bytesRead = readFull(randomFd, data, size); + PCHECK(bytesRead >= 0 && size_t(bytesRead) == size); } +class BufferedRandomDevice { + public: + static constexpr size_t kDefaultBufferSize = 128; -folly::ThreadLocalPtr -ThreadLocalPRNG::localInstance; + explicit BufferedRandomDevice(size_t bufferSize = kDefaultBufferSize); -class ThreadLocalPRNG::LocalInstancePRNG { -#ifdef USE_SIMD_PRNG - typedef __gnu_cxx::sfmt19937 RNG; -#else - typedef std::mt19937 RNG; -#endif - - static RNG makeRng() { - std::array seed_data; - std::random_device r; - std::generate_n(seed_data.data(), seed_data.size(), std::ref(r)); - std::seed_seq seq(std::begin(seed_data), std::end(seed_data)); - return RNG(seq); + void get(void* data, size_t size) { + if (LIKELY(size <= remaining())) { + memcpy(data, ptr_, size); + ptr_ += size; + } else { + getSlow(static_cast(data), size); + } } - public: - LocalInstancePRNG() : rng(std::move(makeRng())) {} + private: + void getSlow(unsigned char* data, size_t size); + + inline size_t remaining() const { + return buffer_.get() + bufferSize_ - ptr_; + } - RNG rng; + const size_t bufferSize_; + std::unique_ptr buffer_; + unsigned char* ptr_; }; -ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() { - auto ret = new LocalInstancePRNG; - localInstance.reset(ret); - return ret; +BufferedRandomDevice::BufferedRandomDevice(size_t bufferSize) + : bufferSize_(bufferSize), + buffer_(new unsigned char[bufferSize]), + ptr_(buffer_.get() + bufferSize) { // refill on first use +} + +void BufferedRandomDevice::getSlow(unsigned char* data, size_t size) { + DCHECK_GT(size, remaining()); + if (size >= bufferSize_) { + // Just read directly. + readRandomDevice(data, size); + return; + } + + size_t copied = remaining(); + memcpy(data, ptr_, copied); + data += copied; + size -= copied; + + // refill + readRandomDevice(buffer_.get(), bufferSize_); + ptr_ = buffer_.get(); + + memcpy(data, ptr_, size); + ptr_ += size; } + +} // namespace + +void Random::secureRandom(void* data, size_t size) { + static ThreadLocal bufferedRandomDevice; + bufferedRandomDevice->get(data, size); +} + +ThreadLocalPRNG::ThreadLocalPRNG() { + static folly::ThreadLocal localInstance; + local_ = localInstance.get(); +} + +class ThreadLocalPRNG::LocalInstancePRNG { + public: + LocalInstancePRNG() : rng(Random::create()) { } + + Random::DefaultGenerator rng; +}; + uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) { return local->rng(); }