Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(27)

Unified Diff: src/IceRNG.cpp

Issue 455593004: Subzero: Add a random number generator. (Closed) Base URL: https://gerrit.chromium.org/gerrit/p/native_client/pnacl-subzero.git@master
Patch Set: Rebase Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/IceRNG.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/IceRNG.cpp
diff --git a/src/IceRNG.cpp b/src/IceRNG.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..6a9f51552e475d7d6b974b8de3c33d69f18588f6
--- /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
« no previous file with comments | « src/IceRNG.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698