llvm.org GIT mirror llvm / 1d04a23
Random Number Generator (llvm) Summary: Provides an abstraction for a random number generator (RNG) that produces a stream of pseudo-random numbers. The current implementation uses C++11 facilities and is therefore not cryptographically secure. The RNG is salted with the text of the current command line invocation. In addition, a user may specify a seed (reproducible builds). In clang, the seed can be set via -frandom-seed=X In the back end, the seed can be set via -rng-seed=X This is the llvm part of the patch. clang part: D3391 Reviewers: ahomescu, rinon, nicholas, jfb Reviewed By: jfb Subscribers: jfb, perl Differential Revision: http://reviews.llvm.org/D3390 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211145 91177308-0d34-0410-b5e6-96231b3b80d8 JF Bastien 6 years ago
5 changed file(s) with 128 addition(s) and 1 deletion(s). Raw diff Collapse all Expand all
2828 class FunctionType;
2929 class GVMaterializer;
3030 class LLVMContext;
31 class RandomNumberGenerator;
3132 class StructType;
3233 template struct DenseMapInfo;
3334 template class DenseMap;
200201 std::string ModuleID; ///< Human readable identifier for the module
201202 std::string TargetTriple; ///< Platform target triple Module compiled on
202203 void *NamedMDSymTab; ///< NamedMDNode names.
204 // Allow lazy initialization in const method.
205 mutable RandomNumberGenerator *RNG; ///< The random number generator for this module.
203206
204207 // We need to keep the string because the C API expects us to own the string
205208 // representation.
247250 /// Get any module-scope inline assembly blocks.
248251 /// @returns a string containing the module-scope inline assembly blocks.
249252 const std::string &getModuleInlineAsm() const { return GlobalScopeAsm; }
253
254 /// Get the RandomNumberGenerator for this module. The RNG can be
255 /// seeded via -rng-seed= and is salted with the ModuleID.
256 /// The returned RNG should not be shared across threads.
257 RandomNumberGenerator &getRNG() const;
250258
251259 /// @}
252260 /// @name Module Level Mutators
0 //==- llvm/Support/RandomNumberGenerator.h - RNG for diversity ---*- C++ -*-==//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines an abstraction for random number generation (RNG).
10 // Note that the current implementation is not cryptographically secure
11 // as it uses the C++11 facilities.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
16 #define LLVM_SUPPORT_RANDOMNUMBERGENERATOR_H_
17
18 #include "llvm/ADT/StringRef.h"
19 #include
20
21 namespace llvm {
22
23 /// A random number generator.
24 /// Instances of this class should not be shared across threads.
25 class RandomNumberGenerator {
26 public:
27 /// Seeds and salts the underlying RNG engine. The salt of type StringRef
28 /// is passed into the constructor. The seed can be set on the command
29 /// line via -rng-seed=.
30 /// The reason for the salt is to ensure different random streams even if
31 /// the same seed is used for multiple invocations of the compiler.
32 /// A good salt value should add additional entropy and be constant across
33 /// different machines (i.e., no paths) to allow for reproducible builds.
34 /// An instance of this class can be retrieved from the current Module.
35 /// \see Module::getRNG
36 RandomNumberGenerator(StringRef Salt);
37
38 /// Returns a random number in the range [0, Max).
39 uint64_t next(uint64_t Max);
40
41 private:
42 // 64-bit Mersenne Twister by Matsumoto and Nishimura, 2000
43 // http://en.cppreference.com/w/cpp/numeric/random/mersenne_twister_engine
44 std::mt19937_64 Generator;
45
46 // Noncopyable.
47 RandomNumberGenerator(const RandomNumberGenerator &other) = delete;
48 RandomNumberGenerator &operator=(const RandomNumberGenerator &other) = delete;
49 };
50 }
51
52 #endif
2323 #include "llvm/IR/LLVMContext.h"
2424 #include "llvm/IR/LeakDetector.h"
2525 #include "llvm/Support/Dwarf.h"
26 #include "llvm/Support/Path.h"
27 #include "llvm/Support/RandomNumberGenerator.h"
2628 #include
2729 #include
2830 #include
4345 //
4446
4547 Module::Module(StringRef MID, LLVMContext &C)
46 : Context(C), Materializer(), ModuleID(MID), DL("") {
48 : Context(C), Materializer(), ModuleID(MID), RNG(nullptr), DL("") {
4749 ValSymTab = new ValueSymbolTable();
4850 NamedMDSymTab = new StringMap();
4951 Context.addModule(this);
5860 NamedMDList.clear();
5961 delete ValSymTab;
6062 delete static_cast *>(NamedMDSymTab);
63 delete RNG;
6164 }
6265
6366 /// getNamedValue - Return the first global value in the module with
354357 return &DL;
355358 }
356359
360 // We want reproducible builds, but ModuleID may be a full path so we just use
361 // the filename to salt the RNG (although it is not guaranteed to be unique).
362 RandomNumberGenerator &Module::getRNG() const {
363 if (RNG == nullptr) {
364 StringRef Salt = sys::path::filename(ModuleID);
365 RNG = new RandomNumberGenerator(Salt);
366 }
367 return *RNG;
368 }
369
357370 //===----------------------------------------------------------------------===//
358371 // Methods to control the materialization of GlobalValues in the Module.
359372 //
4040 MD5.cpp
4141 PluginLoader.cpp
4242 PrettyStackTrace.cpp
43 RandomNumberGenerator.cpp
4344 Regex.cpp
4445 SmallPtrSet.cpp
4546 SmallVector.cpp
0 //===-- RandomNumberGenerator.cpp - Implement RNG class -------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements random number generation (RNG).
10 // The current implementation is NOT cryptographically secure as it uses
11 // the C++11 facilities.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #define DEBUG_TYPE "rng"
16 #include "llvm/Support/RandomNumberGenerator.h"
17 #include "llvm/Support/CommandLine.h"
18 #include "llvm/Support/Debug.h"
19
20 using namespace llvm;
21
22 // Tracking BUG: 19665
23 // http://llvm.org/bugs/show_bug.cgi?id=19665
24 //
25 // Do not change to cl::opt since this silently breaks argument parsing.
26 static cl::opt
27 Seed("rng-seed", cl::value_desc("seed"),
28 cl::desc("Seed for the random number generator"), cl::init(0));
29
30 RandomNumberGenerator::RandomNumberGenerator(StringRef Salt) {
31 DEBUG(
32 if (Seed == 0)
33 errs() << "Warning! Using unseeded random number generator.\n"
34 );
35
36 // Combine seed and salt using std::seed_seq.
37 // Entropy: Seed-low, Seed-high, Salt...
38 size_t Size = Salt.size() + 2;
39 uint32_t Data[Size];
40 Data[0] = Seed;
41 Data[1] = Seed >> 32;
42 std::copy_n(Salt.begin(), Salt.size(), Data + 2);
43
44 std::seed_seq SeedSeq(Data, Data + Size);
45 Generator.seed(SeedSeq);
46 }
47
48 uint64_t RandomNumberGenerator::next(uint64_t Max) {
49 std::uniform_int_distribution distribution(0, Max - 1);
50 return distribution(Generator);
51 }