pool_urbg.cc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "y_absl/random/internal/pool_urbg.h"
  15. #include <algorithm>
  16. #include <atomic>
  17. #include <cstdint>
  18. #include <cstring>
  19. #include <iterator>
  20. #include "y_absl/base/attributes.h"
  21. #include "y_absl/base/call_once.h"
  22. #include "y_absl/base/config.h"
  23. #include "y_absl/base/internal/endian.h"
  24. #include "y_absl/base/internal/raw_logging.h"
  25. #include "y_absl/base/internal/spinlock.h"
  26. #include "y_absl/base/internal/sysinfo.h"
  27. #include "y_absl/base/internal/unaligned_access.h"
  28. #include "y_absl/base/optimization.h"
  29. #include "y_absl/random/internal/randen.h"
  30. #include "y_absl/random/internal/seed_material.h"
  31. #include "y_absl/random/seed_gen_exception.h"
  32. using y_absl::base_internal::SpinLock;
  33. using y_absl::base_internal::SpinLockHolder;
  34. namespace y_absl {
  35. Y_ABSL_NAMESPACE_BEGIN
  36. namespace random_internal {
  37. namespace {
  38. // RandenPoolEntry is a thread-safe pseudorandom bit generator, implementing a
  39. // single generator within a RandenPool<T>. It is an internal implementation
  40. // detail, and does not aim to conform to [rand.req.urng].
  41. //
  42. // NOTE: There are alignment issues when used on ARM, for instance.
  43. // See the allocation code in PoolAlignedAlloc().
  44. class RandenPoolEntry {
  45. public:
  46. static constexpr size_t kState = RandenTraits::kStateBytes / sizeof(uint32_t);
  47. static constexpr size_t kCapacity =
  48. RandenTraits::kCapacityBytes / sizeof(uint32_t);
  49. void Init(y_absl::Span<const uint32_t> data) {
  50. SpinLockHolder l(&mu_); // Always uncontested.
  51. std::copy(data.begin(), data.end(), std::begin(state_));
  52. next_ = kState;
  53. }
  54. // Copy bytes into out.
  55. void Fill(uint8_t* out, size_t bytes) Y_ABSL_LOCKS_EXCLUDED(mu_);
  56. // Returns random bits from the buffer in units of T.
  57. template <typename T>
  58. inline T Generate() Y_ABSL_LOCKS_EXCLUDED(mu_);
  59. inline void MaybeRefill() Y_ABSL_EXCLUSIVE_LOCKS_REQUIRED(mu_) {
  60. if (next_ >= kState) {
  61. next_ = kCapacity;
  62. impl_.Generate(state_);
  63. }
  64. }
  65. private:
  66. // Randen URBG state.
  67. uint32_t state_[kState] Y_ABSL_GUARDED_BY(mu_); // First to satisfy alignment.
  68. SpinLock mu_;
  69. const Randen impl_;
  70. size_t next_ Y_ABSL_GUARDED_BY(mu_);
  71. };
  72. template <>
  73. inline uint8_t RandenPoolEntry::Generate<uint8_t>() {
  74. SpinLockHolder l(&mu_);
  75. MaybeRefill();
  76. return static_cast<uint8_t>(state_[next_++]);
  77. }
  78. template <>
  79. inline uint16_t RandenPoolEntry::Generate<uint16_t>() {
  80. SpinLockHolder l(&mu_);
  81. MaybeRefill();
  82. return static_cast<uint16_t>(state_[next_++]);
  83. }
  84. template <>
  85. inline uint32_t RandenPoolEntry::Generate<uint32_t>() {
  86. SpinLockHolder l(&mu_);
  87. MaybeRefill();
  88. return state_[next_++];
  89. }
  90. template <>
  91. inline uint64_t RandenPoolEntry::Generate<uint64_t>() {
  92. SpinLockHolder l(&mu_);
  93. if (next_ >= kState - 1) {
  94. next_ = kCapacity;
  95. impl_.Generate(state_);
  96. }
  97. auto p = state_ + next_;
  98. next_ += 2;
  99. uint64_t result;
  100. std::memcpy(&result, p, sizeof(result));
  101. return result;
  102. }
  103. void RandenPoolEntry::Fill(uint8_t* out, size_t bytes) {
  104. SpinLockHolder l(&mu_);
  105. while (bytes > 0) {
  106. MaybeRefill();
  107. size_t remaining = (kState - next_) * sizeof(state_[0]);
  108. size_t to_copy = std::min(bytes, remaining);
  109. std::memcpy(out, &state_[next_], to_copy);
  110. out += to_copy;
  111. bytes -= to_copy;
  112. next_ += (to_copy + sizeof(state_[0]) - 1) / sizeof(state_[0]);
  113. }
  114. }
  115. // Number of pooled urbg entries.
  116. static constexpr size_t kPoolSize = 8;
  117. // Shared pool entries.
  118. static y_absl::once_flag pool_once;
  119. Y_ABSL_CACHELINE_ALIGNED static RandenPoolEntry* shared_pools[kPoolSize];
  120. // Returns an id in the range [0 ... kPoolSize), which indexes into the
  121. // pool of random engines.
  122. //
  123. // Each thread to access the pool is assigned a sequential ID (without reuse)
  124. // from the pool-id space; the id is cached in a thread_local variable.
  125. // This id is assigned based on the arrival-order of the thread to the
  126. // GetPoolID call; this has no binary, CL, or runtime stability because
  127. // on subsequent runs the order within the same program may be significantly
  128. // different. However, as other thread IDs are not assigned sequentially,
  129. // this is not expected to matter.
  130. size_t GetPoolID() {
  131. static_assert(kPoolSize >= 1,
  132. "At least one urbg instance is required for PoolURBG");
  133. Y_ABSL_CONST_INIT static std::atomic<uint64_t> sequence{0};
  134. #ifdef Y_ABSL_HAVE_THREAD_LOCAL
  135. static thread_local size_t my_pool_id = kPoolSize;
  136. if (Y_ABSL_PREDICT_FALSE(my_pool_id == kPoolSize)) {
  137. my_pool_id = (sequence++ % kPoolSize);
  138. }
  139. return my_pool_id;
  140. #else
  141. static pthread_key_t tid_key = [] {
  142. pthread_key_t tmp_key;
  143. int err = pthread_key_create(&tmp_key, nullptr);
  144. if (err) {
  145. Y_ABSL_RAW_LOG(FATAL, "pthread_key_create failed with %d", err);
  146. }
  147. return tmp_key;
  148. }();
  149. // Store the value in the pthread_{get/set}specific. However an uninitialized
  150. // value is 0, so add +1 to distinguish from the null value.
  151. uintptr_t my_pool_id =
  152. reinterpret_cast<uintptr_t>(pthread_getspecific(tid_key));
  153. if (Y_ABSL_PREDICT_FALSE(my_pool_id == 0)) {
  154. // No allocated ID, allocate the next value, cache it, and return.
  155. my_pool_id = (sequence++ % kPoolSize) + 1;
  156. int err = pthread_setspecific(tid_key, reinterpret_cast<void*>(my_pool_id));
  157. if (err) {
  158. Y_ABSL_RAW_LOG(FATAL, "pthread_setspecific failed with %d", err);
  159. }
  160. }
  161. return my_pool_id - 1;
  162. #endif
  163. }
  164. // Allocate a RandenPoolEntry with at least 32-byte alignment, which is required
  165. // by ARM platform code.
  166. RandenPoolEntry* PoolAlignedAlloc() {
  167. constexpr size_t kAlignment =
  168. Y_ABSL_CACHELINE_SIZE > 32 ? Y_ABSL_CACHELINE_SIZE : 32;
  169. // Not all the platforms that we build for have std::aligned_alloc, however
  170. // since we never free these objects, we can over allocate and munge the
  171. // pointers to the correct alignment.
  172. uintptr_t x = reinterpret_cast<uintptr_t>(
  173. new char[sizeof(RandenPoolEntry) + kAlignment]);
  174. auto y = x % kAlignment;
  175. void* aligned = reinterpret_cast<void*>(y == 0 ? x : (x + kAlignment - y));
  176. return new (aligned) RandenPoolEntry();
  177. }
  178. // Allocate and initialize kPoolSize objects of type RandenPoolEntry.
  179. //
  180. // The initialization strategy is to initialize one object directly from
  181. // OS entropy, then to use that object to seed all of the individual
  182. // pool instances.
  183. void InitPoolURBG() {
  184. static constexpr size_t kSeedSize =
  185. RandenTraits::kStateBytes / sizeof(uint32_t);
  186. // Read the seed data from OS entropy once.
  187. uint32_t seed_material[kPoolSize * kSeedSize];
  188. if (!random_internal::ReadSeedMaterialFromOSEntropy(
  189. y_absl::MakeSpan(seed_material))) {
  190. random_internal::ThrowSeedGenException();
  191. }
  192. for (size_t i = 0; i < kPoolSize; i++) {
  193. shared_pools[i] = PoolAlignedAlloc();
  194. shared_pools[i]->Init(
  195. y_absl::MakeSpan(&seed_material[i * kSeedSize], kSeedSize));
  196. }
  197. }
  198. // Returns the pool entry for the current thread.
  199. RandenPoolEntry* GetPoolForCurrentThread() {
  200. y_absl::call_once(pool_once, InitPoolURBG);
  201. return shared_pools[GetPoolID()];
  202. }
  203. } // namespace
  204. template <typename T>
  205. typename RandenPool<T>::result_type RandenPool<T>::Generate() {
  206. auto* pool = GetPoolForCurrentThread();
  207. return pool->Generate<T>();
  208. }
  209. template <typename T>
  210. void RandenPool<T>::Fill(y_absl::Span<result_type> data) {
  211. auto* pool = GetPoolForCurrentThread();
  212. pool->Fill(reinterpret_cast<uint8_t*>(data.data()),
  213. data.size() * sizeof(result_type));
  214. }
  215. template class RandenPool<uint8_t>;
  216. template class RandenPool<uint16_t>;
  217. template class RandenPool<uint32_t>;
  218. template class RandenPool<uint64_t>;
  219. } // namespace random_internal
  220. Y_ABSL_NAMESPACE_END
  221. } // namespace y_absl