2019-01-17 10:19:54 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
solidity is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/protoToYul.h>
|
|
|
|
|
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace yul::test::yul_fuzzer;
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Literal const& _x)
|
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.literal_oneof_case())
|
|
|
|
{
|
|
|
|
case Literal::kIntval:
|
|
|
|
_os << _x.intval();
|
|
|
|
break;
|
|
|
|
case Literal::LITERAL_ONEOF_NOT_SET:
|
|
|
|
_os << "1";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _os;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, VarRef const& _x)
|
|
|
|
{
|
|
|
|
return _os << "x_" << (static_cast<uint32_t>(_x.varnum()) % 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Expression const& _x)
|
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.expr_oneof_case())
|
|
|
|
{
|
|
|
|
case Expression::kVarref:
|
|
|
|
_os << _x.varref();
|
|
|
|
break;
|
|
|
|
case Expression::kCons:
|
|
|
|
_os << _x.cons();
|
|
|
|
break;
|
|
|
|
case Expression::kBinop:
|
|
|
|
_os << _x.binop();
|
|
|
|
break;
|
|
|
|
case Expression::kUnop:
|
|
|
|
_os << _x.unop();
|
|
|
|
break;
|
|
|
|
case Expression::EXPR_ONEOF_NOT_SET:
|
|
|
|
_os << "1";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _os;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, BinaryOp const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case BinaryOp::ADD:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "add";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::SUB:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "sub";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::MUL:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "mul";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::DIV:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "div";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::MOD:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "mod";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::XOR:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "xor";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::AND:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "and";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::OR:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "or";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::EQ:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "eq";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::LT:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "lt";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case BinaryOp::GT:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "gt";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
2019-03-14 21:26:25 +00:00
|
|
|
case BinaryOp::SHR:
|
|
|
|
_os << "shr";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SHL:
|
|
|
|
_os << "shl";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SAR:
|
|
|
|
_os << "sar";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SDIV:
|
|
|
|
_os << "sdiv";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SMOD:
|
|
|
|
_os << "smod";
|
|
|
|
break;
|
|
|
|
case BinaryOp::EXP:
|
|
|
|
_os << "exp";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SLT:
|
|
|
|
_os << "slt";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SGT:
|
|
|
|
_os << "sgt";
|
|
|
|
break;
|
|
|
|
case BinaryOp::BYTE:
|
|
|
|
_os << "byte";
|
|
|
|
break;
|
|
|
|
case BinaryOp::SI:
|
|
|
|
_os << "signextend";
|
|
|
|
break;
|
|
|
|
case BinaryOp::KECCAK:
|
|
|
|
_os << "keccak256";
|
|
|
|
break;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
2019-03-14 14:40:54 +00:00
|
|
|
return _os << "(" << _x.left() << "," << _x.right() << ")";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New var numbering starts from x_10 until x_16
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, VarDecl const& _x)
|
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
return _os << "let x_" << ((_x.id() % 7) + 10) << " := " << _x.expr() << "\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, TypedVarDecl const& _x)
|
|
|
|
{
|
|
|
|
_os << "let x_" << ((_x.id() % 7) + 10);
|
|
|
|
switch (_x.type())
|
|
|
|
{
|
|
|
|
case TypedVarDecl::BOOL:
|
|
|
|
_os << ": bool := " << _x.expr() << " : bool\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::S8:
|
|
|
|
_os << ": s8 := " << _x.expr() << " : s8\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::S32:
|
|
|
|
_os << ": s32 := " << _x.expr() << " : s32\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::S64:
|
|
|
|
_os << ": s64 := " << _x.expr() << " : s64\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::S128:
|
|
|
|
_os << ": s128 := " << _x.expr() << " : s128\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::S256:
|
|
|
|
_os << ": s256 := " << _x.expr() << " : s256\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::U8:
|
|
|
|
_os << ": u8 := " << _x.expr() << " : u8\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::U32:
|
|
|
|
_os << ": u32 := " << _x.expr() << " : u32\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::U64:
|
|
|
|
_os << ": u64 := " << _x.expr() << " : u64\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::U128:
|
|
|
|
_os << ": u128 := " << _x.expr() << " : u128\n";
|
|
|
|
break;
|
|
|
|
case TypedVarDecl::U256:
|
|
|
|
_os << ": u256 := " << _x.expr() << " : u256\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _os;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, UnaryOp const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.op())
|
|
|
|
{
|
|
|
|
case UnaryOp::NOT:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "not";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::MLOAD:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "mload";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::SLOAD:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "sload";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
case UnaryOp::ISZERO:
|
2019-03-14 14:40:54 +00:00
|
|
|
_os << "iszero";
|
2019-01-17 10:19:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-03-14 14:40:54 +00:00
|
|
|
return _os << "(" << _x.operand() << ")";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, AssignmentStatement const& _x)
|
|
|
|
{
|
|
|
|
return _os << _x.ref_id() << " := " << _x.expr() << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, IfStmt const& _x)
|
|
|
|
{
|
|
|
|
return _os <<
|
2019-03-14 14:40:54 +00:00
|
|
|
"if " <<
|
|
|
|
_x.cond() <<
|
|
|
|
" " <<
|
|
|
|
_x.if_body();
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, StoreFunc const& _x)
|
|
|
|
{
|
|
|
|
switch (_x.st())
|
|
|
|
{
|
|
|
|
case StoreFunc::MSTORE:
|
|
|
|
_os << "mstore(" << _x.loc() << ", " << _x.val() << ")\n";
|
|
|
|
break;
|
|
|
|
case StoreFunc::SSTORE:
|
|
|
|
_os << "sstore(" << _x.loc() << ", " << _x.val() << ")\n";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _os;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Statement const& _x)
|
|
|
|
{
|
2019-03-14 14:40:54 +00:00
|
|
|
switch (_x.stmt_oneof_case())
|
|
|
|
{
|
|
|
|
case Statement::kDecl:
|
|
|
|
_os << _x.decl();
|
|
|
|
break;
|
|
|
|
case Statement::kAssignment:
|
|
|
|
_os << _x.assignment();
|
|
|
|
break;
|
|
|
|
case Statement::kIfstmt:
|
|
|
|
_os << _x.ifstmt();
|
|
|
|
break;
|
|
|
|
case Statement::kStorageFunc:
|
|
|
|
_os << _x.storage_func();
|
|
|
|
break;
|
|
|
|
case Statement::kBlockstmt:
|
|
|
|
_os << _x.blockstmt();
|
|
|
|
break;
|
|
|
|
case Statement::STMT_ONEOF_NOT_SET:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return _os;
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Block const& _x)
|
|
|
|
{
|
|
|
|
if (_x.statements_size() > 0)
|
|
|
|
{
|
|
|
|
_os << "{\n";
|
|
|
|
for (auto const& st: _x.statements())
|
|
|
|
_os << st;
|
|
|
|
_os << "}\n";
|
2019-03-14 14:40:54 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_os << "{}\n";
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
|
|
|
return _os;
|
|
|
|
}
|
|
|
|
|
|
|
|
ostream& yul::test::yul_fuzzer::operator<<(ostream& _os, Function const& _x)
|
|
|
|
{
|
|
|
|
_os << "{\n"
|
|
|
|
<< "let a,b := foo(calldataload(0),calldataload(32),calldataload(64),calldataload(96),calldataload(128),"
|
|
|
|
<< "calldataload(160),calldataload(192),calldataload(224))\n"
|
|
|
|
<< "sstore(0, a)\n"
|
|
|
|
<< "sstore(32, b)\n"
|
|
|
|
<< "function foo(x_0, x_1, x_2, x_3, x_4, x_5, x_6, x_7) -> x_8, x_9\n"
|
|
|
|
<< _x.statements()
|
|
|
|
<< "}\n";
|
|
|
|
return _os;
|
|
|
|
}
|
|
|
|
|
|
|
|
string yul::test::yul_fuzzer::functionToString(Function const& _input)
|
|
|
|
{
|
|
|
|
ostringstream os;
|
|
|
|
os << _input;
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
string yul::test::yul_fuzzer::protoToYul(const uint8_t* _data, size_t _size)
|
|
|
|
{
|
|
|
|
Function message;
|
|
|
|
if (!message.ParsePartialFromArray(_data, _size))
|
|
|
|
return "#error invalid proto\n";
|
|
|
|
return functionToString(message);
|
|
|
|
}
|