// Copyright 2017 The Abseil Authors. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. #include "absl/random/internal/pool_urbg.h" #include <algorithm> #include <atomic> #include <cstdint> #include <cstring> #include <iterator> #include "absl/base/attributes.h" #include "absl/base/call_once.h" #include "absl/base/config.h" #include "absl/base/internal/endian.h" #include "absl/base/internal/raw_logging.h" #include "absl/base/internal/spinlock.h" #include "absl/base/internal/sysinfo.h" #include "absl/base/internal/unaligned_access.h" #include "absl/base/optimization.h" #include "absl/random/internal/randen.h" #include "absl/random/internal/seed_material.h" #include "absl/random/seed_gen_exception.h" SpinLock; SpinLockHolder; namespace absl { ABSL_NAMESPACE_BEGIN namespace random_internal { namespace { // RandenPoolEntry is a thread-safe pseudorandom bit generator, implementing a // single generator within a RandenPool<T>. It is an internal implementation // detail, and does not aim to conform to [rand.req.urng]. // // NOTE: There are alignment issues when used on ARM, for instance. // See the allocation code in PoolAlignedAlloc(). class RandenPoolEntry { … }; template <> inline uint8_t RandenPoolEntry::Generate<uint8_t>() { … } template <> inline uint16_t RandenPoolEntry::Generate<uint16_t>() { … } template <> inline uint32_t RandenPoolEntry::Generate<uint32_t>() { … } template <> inline uint64_t RandenPoolEntry::Generate<uint64_t>() { … } void RandenPoolEntry::Fill(uint8_t* out, size_t bytes) { … } // Number of pooled urbg entries. static constexpr size_t kPoolSize = …; // Shared pool entries. static absl::once_flag pool_once; ABSL_CACHELINE_ALIGNED static RandenPoolEntry* shared_pools[kPoolSize]; // Returns an id in the range [0 ... kPoolSize), which indexes into the // pool of random engines. // // Each thread to access the pool is assigned a sequential ID (without reuse) // from the pool-id space; the id is cached in a thread_local variable. // This id is assigned based on the arrival-order of the thread to the // GetPoolID call; this has no binary, CL, or runtime stability because // on subsequent runs the order within the same program may be significantly // different. However, as other thread IDs are not assigned sequentially, // this is not expected to matter. size_t GetPoolID() { … } // Allocate a RandenPoolEntry with at least 32-byte alignment, which is required // by ARM platform code. RandenPoolEntry* PoolAlignedAlloc() { … } // Allocate and initialize kPoolSize objects of type RandenPoolEntry. // // The initialization strategy is to initialize one object directly from // OS entropy, then to use that object to seed all of the individual // pool instances. void InitPoolURBG() { … } // Returns the pool entry for the current thread. RandenPoolEntry* GetPoolForCurrentThread() { … } } // namespace template <typename T> typename RandenPool<T>::result_type RandenPool<T>::Generate() { … } template <typename T> void RandenPool<T>::Fill(absl::Span<result_type> data) { … } template class RandenPool<uint8_t>; template class RandenPool<uint16_t>; template class RandenPool<uint32_t>; template class RandenPool<uint64_t>; } // namespace random_internal ABSL_NAMESPACE_END } // namespace absl