Minor code cleanup and added two new mutations: add/remove Function call and vardecl

This commit is contained in:
Bhargava Shastry 2019-12-30 14:12:05 +01:00
parent f0ade1b4de
commit 26b3ee91e3
2 changed files with 304 additions and 87 deletions

View File

@ -1,6 +1,7 @@
#include <test/tools/ossfuzz/protomutators/YulProtoMutator.h>
#include <src/text_format.h>
#include <libyul/Exceptions.h>
#define DEBUG
@ -11,24 +12,26 @@ static YulProtoMutator invertIfCondition(
IfStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
IfStmt* ifStmt = static_cast<IfStmt*>(_message);
auto ifStmt = static_cast<IfStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (ifStmt->has_cond())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: If condition inverted" << std::endl;
#endif
UnaryOp* notOp = new UnaryOp();
auto notOp = new UnaryOp();
notOp->set_op(UnaryOp::NOT);
Expression *oldCond = ifStmt->release_cond();
auto oldCond = ifStmt->release_cond();
notOp->set_allocated_operand(oldCond);
Expression *ifCond = new Expression();
auto ifCond = new Expression();
ifCond->set_allocated_unop(notOp);
ifStmt->set_allocated_cond(ifCond);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -40,7 +43,7 @@ static YulProtoMutator removeInvertedIfCondition(
IfStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
IfStmt* ifStmt = static_cast<IfStmt*>(_message);
auto ifStmt = static_cast<IfStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 1)
{
if (ifStmt->has_cond() &&
@ -50,17 +53,19 @@ static YulProtoMutator removeInvertedIfCondition(
)
{
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove If condition inverted" << std::endl;
#endif
Expression *oldCondition = ifStmt->release_cond();
UnaryOp *unop = oldCondition->release_unop();
Expression *conditionWithoutNot = unop->release_operand();
auto oldCondition = ifStmt->release_cond();
auto unop = oldCondition->release_unop();
auto conditionWithoutNot = unop->release_operand();
ifStmt->set_allocated_cond(conditionWithoutNot);
delete(oldCondition);
delete(unop);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -72,20 +77,22 @@ static YulProtoMutator addBreak(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (forStmt->has_for_body())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Break added" << std::endl;
#endif
BreakStmt* breakStmt = new BreakStmt();
Statement* statement = forStmt->mutable_for_body()->add_statements();
auto breakStmt = new BreakStmt();
auto statement = forStmt->mutable_for_body()->add_statements();
statement->set_allocated_breakstmt(breakStmt);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -97,12 +104,13 @@ static YulProtoMutator removeBreak(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 1)
{
if (forStmt->has_for_body())
{
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove Break" << std::endl;
#endif
@ -114,6 +122,7 @@ static YulProtoMutator removeBreak(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -125,20 +134,22 @@ static YulProtoMutator addContinue(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (forStmt->has_for_body())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Continue added" << std::endl;
#endif
ContinueStmt* contStmt = new ContinueStmt();
Statement* statement = forStmt->mutable_for_body()->add_statements();
auto contStmt = new ContinueStmt();
auto statement = forStmt->mutable_for_body()->add_statements();
statement->set_allocated_contstmt(contStmt);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -150,12 +161,13 @@ static YulProtoMutator removeContinue(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 1)
{
if (forStmt->has_for_body())
{
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove Continue" << std::endl;
#endif
@ -167,6 +179,7 @@ static YulProtoMutator removeContinue(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -178,28 +191,29 @@ static YulProtoMutator addMloadZero(
VarDecl::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
VarDecl* varDeclStmt = static_cast<VarDecl*>(_message);
auto varDeclStmt = static_cast<VarDecl*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (varDeclStmt->has_expr())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: mload added" << std::endl;
#endif
varDeclStmt->clear_expr();
Literal *zeroLit = new Literal();
zeroLit->set_intval(0);
Expression *consExpr = new Expression();
auto zeroLit = YulProtoMutator::intLiteral(0);
auto consExpr = new Expression();
consExpr->set_allocated_cons(zeroLit);
UnaryOp *mloadOp = new UnaryOp();
auto mloadOp = new UnaryOp();
mloadOp->set_op(UnaryOp::MLOAD);
mloadOp->set_allocated_operand(consExpr);
Expression *mloadExpr = new Expression();
auto mloadExpr = new Expression();
mloadExpr->set_allocated_unop(mloadOp);
varDeclStmt->set_allocated_expr(mloadExpr);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -211,24 +225,26 @@ static YulProtoMutator invertForCondition(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (forStmt->has_for_cond())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: For condition inverted" << std::endl;
#endif
UnaryOp* notOp = new UnaryOp();
auto notOp = new UnaryOp();
notOp->set_op(UnaryOp::NOT);
Expression *oldCond = forStmt->release_for_cond();
auto oldCond = forStmt->release_for_cond();
notOp->set_allocated_operand(oldCond);
Expression *forCond = new Expression();
auto forCond = new Expression();
forCond->set_allocated_unop(notOp);
forStmt->set_allocated_for_cond(forCond);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -240,7 +256,7 @@ static YulProtoMutator removeInvertedForCondition(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 1)
{
if (forStmt->has_for_cond() &&
@ -250,17 +266,19 @@ static YulProtoMutator removeInvertedForCondition(
)
{
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove For condition inverted" << std::endl;
#endif
Expression *oldCondition = forStmt->release_for_cond();
UnaryOp *unop = oldCondition->release_unop();
Expression *newCondition = unop->release_operand();
auto oldCondition = forStmt->release_for_cond();
auto unop = oldCondition->release_unop();
auto newCondition = unop->release_operand();
forStmt->set_allocated_for_cond(newCondition);
delete oldCondition;
delete unop;
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -272,31 +290,33 @@ static YulProtoMutator funcCallForCondition(
ForStmt::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
ForStmt* forStmt = static_cast<ForStmt*>(_message);
auto forStmt = static_cast<ForStmt*>(_message);
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
if (forStmt->has_for_cond())
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Function call in for condition" << std::endl;
#endif
forStmt->release_for_cond();
FunctionCall *functionCall = new FunctionCall();
auto functionCall = new FunctionCall();
functionCall->set_ret(FunctionCall::SINGLE);
functionCall->set_func_index(0);
Expression *forCondExpr = new Expression();
functionCall->set_func_index(_seed);
auto forCondExpr = new Expression();
forCondExpr->set_allocated_func_expr(functionCall);
forStmt->set_allocated_for_cond(forCondExpr);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
}
);
/// Define an identity function f(x) = x
/// Define an identity function y = x
static YulProtoMutator identityFunction(
Block::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
@ -304,30 +324,30 @@ static YulProtoMutator identityFunction(
if (_seed % YulProtoMutator::s_mediumIP == 0)
{
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Identity function" << std::endl;
#endif
Block* blockStmt = static_cast<Block*>(_message);
FunctionDef* functionDef = new FunctionDef();
auto blockStmt = static_cast<Block*>(_message);
auto functionDef = new FunctionDef();
functionDef->set_num_input_params(1);
functionDef->set_num_output_params(1);
Block* functionBlock = new Block();
AssignmentStatement* assignmentStatement = new AssignmentStatement();
VarRef* varRef = new VarRef();
varRef->set_varnum(1);
auto functionBlock = new Block();
auto assignmentStatement = new AssignmentStatement();
auto varRef = YulProtoMutator::varRef(_seed);
assignmentStatement->set_allocated_ref_id(varRef);
Expression* rhs = new Expression();
VarRef* rhsRef = new VarRef();
rhsRef->set_varnum(0);
auto rhs = new Expression();
auto rhsRef = YulProtoMutator::varRef(_seed + blockStmt->ByteSizeLong());
rhs->set_allocated_varref(rhsRef);
assignmentStatement->set_allocated_expr(rhs);
Statement *stmt = functionBlock->add_statements();
auto stmt = functionBlock->add_statements();
stmt->set_allocated_assignment(assignmentStatement);
functionDef->set_allocated_block(functionBlock);
Statement *funcdefStmt = blockStmt->add_statements();
auto funcdefStmt = blockStmt->add_statements();
funcdefStmt->set_allocated_funcdef(functionDef);
#ifdef DEBUG
// std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -340,15 +360,17 @@ static YulProtoMutator addLeave(
{
if (_seed % YulProtoMutator::s_lowIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Leave in function" << std::endl;
#endif
FunctionDef *funcDef = static_cast<FunctionDef*>(_message);
Statement *newStmt = funcDef->mutable_block()->add_statements();
LeaveStmt *leaveStmt = new LeaveStmt();
auto funcDef = static_cast<FunctionDef*>(_message);
auto newStmt = funcDef->mutable_block()->add_statements();
auto leaveStmt = new LeaveStmt();
newStmt->set_allocated_leave(leaveStmt);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -361,10 +383,11 @@ static YulProtoMutator removeLeave(
{
if (_seed % YulProtoMutator::s_lowIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove Leave in function" << std::endl;
#endif
FunctionDef *funcDef = static_cast<FunctionDef*>(_message);
auto funcDef = static_cast<FunctionDef*>(_message);
for (auto &stmt: *funcDef->mutable_block()->mutable_statements())
if (stmt.has_leave())
{
@ -373,6 +396,7 @@ static YulProtoMutator removeLeave(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -385,23 +409,23 @@ static YulProtoMutator addAssignment(
{
if (_seed % YulProtoMutator::s_mediumIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Add assignment" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
AssignmentStatement *assignmentStatement = new AssignmentStatement();
VarRef *varRef = new VarRef();
varRef->set_varnum(0);
auto block = static_cast<Block*>(_message);
auto assignmentStatement = new AssignmentStatement();
auto varRef = YulProtoMutator::varRef(_seed);
assignmentStatement->set_allocated_ref_id(varRef);
VarRef *rhs = new VarRef();
rhs->set_varnum(1);
Expression *rhsExpr = new Expression();
auto rhs = YulProtoMutator::varRef(_seed + block->ByteSizeLong());
auto rhsExpr = new Expression();
rhsExpr->set_allocated_varref(rhs);
assignmentStatement->set_allocated_expr(rhsExpr);
Statement *newStmt = block->add_statements();
auto newStmt = block->add_statements();
newStmt->set_allocated_assignment(assignmentStatement);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -414,10 +438,11 @@ static YulProtoMutator removeAssignment(
{
if (_seed % YulProtoMutator::s_mediumIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove assignment" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
auto block = static_cast<Block*>(_message);
for (auto &stmt: *block->mutable_statements())
if (stmt.has_assignment())
{
@ -426,6 +451,7 @@ static YulProtoMutator removeAssignment(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -438,15 +464,17 @@ static YulProtoMutator addIf(
{
if (_seed % YulProtoMutator::s_mediumIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Add if" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
Statement *stmt = block->add_statements();
IfStmt *ifStmt = new IfStmt();
auto block = static_cast<Block*>(_message);
auto stmt = block->add_statements();
auto ifStmt = new IfStmt();
stmt->set_allocated_ifstmt(ifStmt);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -459,10 +487,11 @@ static YulProtoMutator removeIf(
{
if (_seed % YulProtoMutator::s_mediumIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove if" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
auto block = static_cast<Block*>(_message);
for (auto &stmt: *block->mutable_statements())
if (stmt.has_ifstmt())
{
@ -471,6 +500,7 @@ static YulProtoMutator removeIf(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -483,15 +513,17 @@ static YulProtoMutator addSwitch(
{
if (_seed % YulProtoMutator::s_mediumIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Add switch" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
Statement *stmt = block->add_statements();
SwitchStmt *switchStmt = new SwitchStmt();
auto block = static_cast<Block*>(_message);
auto stmt = block->add_statements();
auto switchStmt = new SwitchStmt();
stmt->set_allocated_switchstmt(switchStmt);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -504,10 +536,11 @@ static YulProtoMutator removeSwitch(
{
if (_seed % YulProtoMutator::s_mediumIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove switch" << std::endl;
#endif
Block *block = static_cast<Block*>(_message);
auto block = static_cast<Block*>(_message);
for (auto &stmt: *block->mutable_statements())
if (stmt.has_switchstmt())
{
@ -516,6 +549,106 @@ static YulProtoMutator removeSwitch(
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
);
/// Add function call
static YulProtoMutator addCall(
Block::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
if (_seed % YulProtoMutator::s_mediumIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Add function call" << std::endl;
#endif
auto block = static_cast<Block*>(_message);
auto stmt = block->add_statements();
auto call = new FunctionCall();
YulProtoMutator::configureCall(call, _seed);
stmt->set_allocated_functioncall(call);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
);
/// Remove function call
static YulProtoMutator removeCall(
Block::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
if (_seed % YulProtoMutator::s_mediumIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove function call" << std::endl;
#endif
auto block = static_cast<Block*>(_message);
for (auto &stmt: *block->mutable_statements())
if (stmt.has_functioncall())
{
stmt.clear_functioncall();
break;
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
);
/// Add variable declaration
static YulProtoMutator addVarDecl(
Block::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
if (_seed % YulProtoMutator::s_mediumIP == 0) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Add variable decl" << std::endl;
#endif
auto block = static_cast<Block*>(_message);
auto stmt = block->add_statements();
auto varDecl = new VarDecl();
stmt->set_allocated_decl(varDecl);
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
);
/// Remove variable declaration
static YulProtoMutator removeVarDecl(
Block::descriptor(),
[](google::protobuf::Message* _message, unsigned int _seed)
{
if (_seed % YulProtoMutator::s_mediumIP == 1) {
#ifdef DEBUG
std::cout << "----------------------------------" << std::endl;
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "YULMUTATOR: Remove var decl" << std::endl;
#endif
auto block = static_cast<Block*>(_message);
for (auto &stmt: *block->mutable_statements())
if (stmt.has_decl())
{
stmt.clear_decl();
break;
}
#ifdef DEBUG
std::cout << protobuf_mutator::SaveMessageAsText(*_message) << std::endl;
std::cout << "----------------------------------" << std::endl;
#endif
}
}
@ -528,9 +661,84 @@ Literal* YulProtoMutator::intLiteral(unsigned _value)
return lit;
}
VarRef* YulProtoMutator::varRef(unsigned _index)
VarRef* YulProtoMutator::varRef(unsigned _seed)
{
VarRef *varref = new VarRef();
varref->set_varnum(_index);
varref->set_varnum(_seed);
return varref;
}
FunctionCall_Returns YulProtoMutator::callType(unsigned _seed)
{
switch (_seed % 4)
{
case 0:
return FunctionCall_Returns_ZERO;
case 1:
return FunctionCall_Returns_SINGLE;
case 2:
return FunctionCall_Returns_MULTIASSIGN;
case 3:
return FunctionCall_Returns_MULTIDECL;
default:
yulAssert(false, "");
}
}
void YulProtoMutator::configureCall(FunctionCall *_call, unsigned int _seed)
{
auto type = callType(_seed);
_call->set_ret(type);
_call->set_func_index(_seed);
// Configuration rules:
// - In/Out parameters do not need to be configured for zero I/O call
// - Single in and zero out parameter needs to be configured for single I/O call
// - Four in and no out parameters need to be configured for multidecl call
// - Four in and out parameters need to be configured for multiassign call
switch (type)
{
case FunctionCall_Returns_MULTIASSIGN:
{
auto outRef4 = YulProtoMutator::varRef(_seed * 8);
_call->set_allocated_out_param4(outRef4);
auto outRef3 = YulProtoMutator::varRef(_seed * 7);
_call->set_allocated_out_param3(outRef3);
auto outRef2 = YulProtoMutator::varRef(_seed * 6);
_call->set_allocated_out_param2(outRef2);
auto outRef1 = YulProtoMutator::varRef(_seed * 5);
_call->set_allocated_out_param1(outRef1);
}
BOOST_FALLTHROUGH;
case FunctionCall_Returns_MULTIDECL:
{
auto inArg4 = new Expression();
auto inRef4 = YulProtoMutator::varRef(_seed * 4);
inArg4->set_allocated_varref(inRef4);
_call->set_allocated_in_param4(inArg4);
auto inArg3 = new Expression();
auto inRef3 = YulProtoMutator::varRef(_seed * 3);
inArg3->set_allocated_varref(inRef3);
_call->set_allocated_in_param3(inArg3);
auto inArg2 = new Expression();
auto inRef2 = YulProtoMutator::varRef(_seed * 2);
inArg2->set_allocated_varref(inRef2);
_call->set_allocated_in_param2(inArg2);
}
BOOST_FALLTHROUGH;
case FunctionCall_Returns_SINGLE:
{
auto inArg1 = new Expression();
auto inRef1 = YulProtoMutator::varRef(_seed);
inArg1->set_allocated_varref(inRef1);
_call->set_allocated_in_param1(inArg1);
}
BOOST_FALLTHROUGH;
case FunctionCall_Returns_ZERO:
break;
}
}

View File

@ -25,9 +25,18 @@ struct YulProtoMutator
static Literal* intLiteral(unsigned _value);
/// Return a variable reference
/// @param _index: Index of a variable in scope, 0 being the first
/// variable in scope.
static VarRef* varRef(unsigned _index);
/// @param _seed: Pseudo-random unsigned integer used to reference
/// an existing variable.
static VarRef* varRef(unsigned _seed);
/// Return a valid function call type from a pseudo-random seed.
/// @param _seed: Pseudo-random unsigned integer
static FunctionCall_Returns callType(unsigned _seed);
/// Configure function call from a pseudo-random seed.
/// @param _call: Pre-allocated FunctionCall protobuf message
/// @param _seed: Pseudo-random unsigned integer
static void configureCall(FunctionCall *_call, unsigned _seed);
static constexpr unsigned s_lowIP = 827;
static constexpr unsigned s_mediumIP = 569;