Skip to content

Commit 106ae7e

Browse files
authored
Merge pull request #14605 from ethereum/make-ypm-deterministic
Make Yul proto mutator mutations deterministic.
2 parents 38a5852 + eab92f9 commit 106ae7e

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

test/tools/ossfuzz/protomutators/YulProtoMutator.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ void MutationInfo::exitInfo()
2626
writeLine(SaveMessageAsText(*m_protobufMsg));
2727
}
2828

29-
/// Initialize deterministic PRNG.
30-
static YulRandomNumGenerator s_rand(1337);
31-
3229
/// Add m/sstore(0, variable)
3330
static LPMPostProcessor<Block> addStoreToZero(
3431
[](Block* _message, unsigned _seed)
3532
{
3633
if (_seed % YPM::s_highIP == 0)
3734
{
35+
YulRandomNumGenerator yrand(_seed);
3836
MutationInfo m{_message, "Added store to zero"};
3937
auto storeStmt = new StoreFunc();
40-
storeStmt->set_st(YPM::EnumTypeConverter<StoreFunc_Storage>{}.enumFromSeed(s_rand()));
38+
storeStmt->set_st(YPM::EnumTypeConverter<StoreFunc_Storage>{}.enumFromSeed(yrand()));
4139
storeStmt->set_allocated_loc(YPM::litExpression(0));
42-
storeStmt->set_allocated_val(YPM::refExpression(s_rand));
40+
storeStmt->set_allocated_val(YPM::refExpression(yrand));
4341
auto stmt = _message->add_statements();
4442
stmt->set_allocated_storage_func(storeStmt);
4543
}
@@ -54,10 +52,10 @@ struct addControlFlow
5452
{
5553
addControlFlow()
5654
{
57-
function = [](T* _message, unsigned)
55+
function = [](T* _message, unsigned _seed)
5856
{
5957
MutationInfo m{_message, "Added control flow."};
60-
YPM::addControlFlow(_message);
58+
YPM::addControlFlow(_message, _seed);
6159
};
6260
/// Unused variable registers callback.
6361
LPMPostProcessor<T> callback(function);
@@ -170,7 +168,7 @@ unsigned YPM::EnumTypeConverter<T>::enumMin()
170168
}
171169

172170
template <typename T>
173-
void YPM::addControlFlow(T* _msg)
171+
void YPM::addControlFlow(T* _msg, unsigned _seed)
174172
{
175173
enum class ControlFlowStmt: unsigned
176174
{
@@ -188,8 +186,9 @@ void YPM::addControlFlow(T* _msg)
188186
static_cast<unsigned>(ControlFlowStmt::For),
189187
static_cast<unsigned>(ControlFlowStmt::Termination)
190188
);
191-
auto random = static_cast<ControlFlowStmt>(d(s_rand.m_random));
192-
Statement* s = basicBlock(_msg)->add_statements();
189+
YulRandomNumGenerator yrand(_seed);
190+
auto random = static_cast<ControlFlowStmt>(d(yrand.m_random));
191+
Statement* s = basicBlock(_msg, _seed)->add_statements();
193192
switch (random)
194193
{
195194
case ControlFlowStmt::For:
@@ -222,7 +221,7 @@ void YPM::addControlFlow(T* _msg)
222221
}
223222
}
224223

225-
Block* YPM::randomBlock(ForStmt* _stmt)
224+
Block* YPM::randomBlock(ForStmt* _stmt, unsigned _seed)
226225
{
227226
enum class ForBlocks: unsigned
228227
{
@@ -234,7 +233,8 @@ Block* YPM::randomBlock(ForStmt* _stmt)
234233
static_cast<unsigned>(ForBlocks::Init),
235234
static_cast<unsigned>(ForBlocks::Body)
236235
);
237-
switch (static_cast<ForBlocks>(d(s_rand.m_random)))
236+
YulRandomNumGenerator yrand(_seed);
237+
switch (static_cast<ForBlocks>(d(yrand.m_random)))
238238
{
239239
case ForBlocks::Init:
240240
return _stmt->mutable_for_init();
@@ -246,10 +246,10 @@ Block* YPM::randomBlock(ForStmt* _stmt)
246246
}
247247

248248
template <typename T>
249-
Block* YPM::basicBlock(T* _msg)
249+
Block* YPM::basicBlock(T* _msg, unsigned _seed)
250250
{
251251
if constexpr (std::is_same_v<T, ForStmt>)
252-
return randomBlock(_msg);
252+
return randomBlock(_msg, _seed);
253253
else if constexpr (std::is_same_v<T, BoundedForStmt>)
254254
return _msg->mutable_for_body();
255255
else if constexpr (std::is_same_v<T, SwitchStmt>)

test/tools/ossfuzz/protomutators/YulProtoMutator.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ struct YulProtoMutator
9696
static constexpr unsigned s_highIP = 23;
9797
/// Add control-flow statement to basic block.
9898
template <typename T>
99-
static void addControlFlow(T* _msg);
99+
static void addControlFlow(T* _msg, unsigned _seed);
100100
/// Obtain basic block for statement type.
101101
template <typename T>
102-
static Block* basicBlock(T* _msg);
102+
static Block* basicBlock(T* _msg, unsigned _seed);
103103
/// Obtain a basic block in a for stmt uniformly
104104
/// at random
105-
static Block* randomBlock(ForStmt* _msg);
105+
static Block* randomBlock(ForStmt* _msg, unsigned _seed);
106106
/// Obtain a basic block in global scope.
107107
static Block* globalBlock(Program* _program);
108108
};

0 commit comments

Comments
 (0)