Harden failure signal handler in the face of memory corruptions
[folly.git] / folly / Random.cpp
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "folly/Random.h"
18
19 #include <atomic>
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <random>
23 #include <array>
24
25 #if __GNUC_PREREQ(4, 8)
26 #include <ext/random>
27 #define USE_SIMD_PRNG
28 #endif
29
30 namespace folly {
31
32 namespace {
33 std::atomic<uint32_t> seedInput(0);
34 }
35
36 uint32_t randomNumberSeed() {
37   struct timeval tv;
38   gettimeofday(&tv, NULL);
39   const uint32_t kPrime0 = 51551;
40   const uint32_t kPrime1 = 61631;
41   const uint32_t kPrime2 = 64997;
42   const uint32_t kPrime3 = 111857;
43   return kPrime0 * (seedInput++)
44        + kPrime1 * static_cast<uint32_t>(getpid())
45        + kPrime2 * static_cast<uint32_t>(tv.tv_sec)
46        + kPrime3 * static_cast<uint32_t>(tv.tv_usec);
47 }
48
49
50 folly::ThreadLocalPtr<ThreadLocalPRNG::LocalInstancePRNG>
51 ThreadLocalPRNG::localInstance;
52
53 class ThreadLocalPRNG::LocalInstancePRNG {
54 #ifdef USE_SIMD_PRNG
55   typedef  __gnu_cxx::sfmt19937 RNG;
56 #else
57   typedef std::mt19937 RNG;
58 #endif
59
60   static RNG makeRng() {
61     std::array<int, RNG::state_size> seed_data;
62     std::random_device r;
63     std::generate_n(seed_data.data(), seed_data.size(), std::ref(r));
64     std::seed_seq seq(std::begin(seed_data), std::end(seed_data));
65     return RNG(seq);
66   }
67
68  public:
69   LocalInstancePRNG() : rng(std::move(makeRng())) {}
70
71   RNG rng;
72 };
73
74 ThreadLocalPRNG::LocalInstancePRNG* ThreadLocalPRNG::initLocal() {
75   auto ret = new LocalInstancePRNG;
76   localInstance.reset(ret);
77   return ret;
78 }
79
80 uint32_t ThreadLocalPRNG::getImpl(LocalInstancePRNG* local) {
81   return local->rng();
82 }
83
84 }