Subzero: Add a random number generator. This is inital work necessary for diversification support in Subzero. The random number generator implementation is temporary. It will eventually use a cryptographically secure pseudorandom number generator (perhaps from LLVM, if LLVM gets one). Add the -rng-seed= option to seed the random number generator from the command line. BUG=none R=stichnot@chromium.org Review URL: https://codereview.chromium.org/455593004
diff --git a/src/IceRNG.cpp b/src/IceRNG.cpp new file mode 100644 index 0000000..6a9f515 --- /dev/null +++ b/src/IceRNG.cpp
@@ -0,0 +1,46 @@ +//===- subzero/src/IceRNG.cpp - PRNG implementation -----------------------===// +// +// The Subzero Code Generator +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file implements the random number generator. +// +//===----------------------------------------------------------------------===// + +#include <time.h> + +#include "llvm/Support/CommandLine.h" + +#include "IceRNG.h" + +namespace Ice { + +namespace { +namespace cl = llvm::cl; + +cl::opt<unsigned long long> +RandomSeed("rng-seed", cl::desc("Seed the random number generator"), + cl::init(time(0))); + +} // end of anonymous namespace + +// TODO(wala,stichnot): Switch to RNG implementation from LLVM or C++11. +// +// TODO(wala,stichnot): Make it possible to replay the RNG sequence in a +// subsequent run, for reproducing a bug. Print the seed in a comment +// in the asm output. Embed the seed in the binary via metadata that an +// attacker can't introspect. +RandomNumberGenerator::RandomNumberGenerator(llvm::StringRef) + : State(RandomSeed) {} + +uint64_t RandomNumberGenerator::next(uint64_t Max) { + // Lewis, Goodman, and Miller (1969) + State = (16807 * State) % 2147483647; + return State % Max; +} + +} // end of namespace Ice