Use separate random number generator for each randomization pass
This removes random number generator from GlobalContext class and decouples different randomization passes
1. Add a new constructor for random number generator which merge three arguments to into one seed for the underlying implementation of random number generator.
RandomNumberGenerator(uint64_t Seed, RandomizationPassesEnum RandomizationPassID, uint64_t Salt=0)
param Seed: Should be the global random number seed passed through command line.
param RandomizationPassID: Should be the ID for different randomization passes.
param Salt: Should be an additional integer salt, default to be 0.
2. Move the creation of random number generators to the call sites of randomization passes. Each randomization pass create its own random number generator with specific salt value.
Function reordering: Salt = 0 (default)
Basic Block reordering: Salt = Function Sequence Number
Global Variable reordering: Salt = 0 (default)
Pooled Constants reordering: Salt = Constants' Kind value (return of getKind())
*Jump Tables: Salt = 0
Nop Insertion: Salt = Function Sequence Number
Register Alloc Randomization: Salt = (Function Sequence Number << 1) ^ (Kind == RAK_Phi ? 0u : 1u)
Constants Blinding: Salt = Function Sequence Number
*Jump tables are treated as pooled constants, but without Kind value as salt.
BUG=
R=stichnot@chromium.org
Review URL: https://codereview.chromium.org/1300993002.
diff --git a/src/IceCfg.cpp b/src/IceCfg.cpp
index 218ecd9..cc6d07c 100644
--- a/src/IceCfg.cpp
+++ b/src/IceCfg.cpp
@@ -43,6 +43,16 @@
VMetadata(new VariablesMetadata(this)),
TargetAssembler(TargetLowering::createAssembler(
Ctx->getFlags().getTargetArch(), this)) {
+ assert(!Ctx->isIRGenerationDisabled() &&
+ "Attempt to build cfg when IR generation disabled");
+ if (Ctx->getFlags().getRandomizeAndPoolImmediatesOption() == RPI_Randomize) {
+ // If -randomize-pool-immediates=randomize, create a random number generator
+ // to generate a cookie for constant blinding.
+ RandomNumberGenerator RNG(Ctx->getFlags().getRandomSeed(),
+ RPE_ConstantBlinding, SequenceNumber);
+ ConstantBlindingCookie =
+ (uint32_t)RNG.next((uint64_t)std::numeric_limits<uint32_t>::max + 1);
+ }
}
Cfg::~Cfg() { assert(ICE_TLS_GET_FIELD(CurrentCfg) == nullptr); }
@@ -396,9 +406,11 @@
NodeList ReversedReachable;
NodeList Unreachable;
llvm::BitVector ToVisit(Nodes.size(), true);
+ // Create Random number generator for function reordering
+ RandomNumberGenerator RNG(Ctx->getFlags().getRandomSeed(),
+ RPE_BasicBlockReordering, SequenceNumber);
// Traverse from entry node.
- getRandomPostOrder(getEntryNode(), ToVisit, ReversedReachable,
- &Ctx->getRNG());
+ getRandomPostOrder(getEntryNode(), ToVisit, ReversedReachable, &RNG);
// Collect the unreachable nodes.
for (CfgNode *Node : Nodes)
if (ToVisit[Node->getIndex()])
@@ -431,8 +443,10 @@
if (!Ctx->getFlags().shouldDoNopInsertion())
return;
TimerMarker T(TimerStack::TT_doNopInsertion, this);
+ RandomNumberGenerator RNG(Ctx->getFlags().getRandomSeed(), RPE_NopInsertion,
+ SequenceNumber);
for (CfgNode *Node : Nodes)
- Node->doNopInsertion();
+ Node->doNopInsertion(RNG);
}
void Cfg::genCode() {