From 97f94fdcc616d0ffc99664cca87e111e3680a45c Mon Sep 17 00:00:00 2001 From: Bhargava Shastry Date: Mon, 4 May 2020 12:06:45 +0200 Subject: [PATCH] Permit signed and unsigned types --- test/tools/ossfuzz/solarithprotoToSol.cpp | 54 +++++++++++------------ test/tools/ossfuzz/solarithprotoToSol.h | 6 ++- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/test/tools/ossfuzz/solarithprotoToSol.cpp b/test/tools/ossfuzz/solarithprotoToSol.cpp index e1d41ccb0..b411e2f26 100644 --- a/test/tools/ossfuzz/solarithprotoToSol.cpp +++ b/test/tools/ossfuzz/solarithprotoToSol.cpp @@ -81,7 +81,7 @@ string ProtoConverter::visit(VarDecl const& _vardecl) Whiskers v(R"( = ();)"); string type = visit(_vardecl.t()); string varName = newVarName(); - m_varTypeMap.emplace(varName, type); + m_varTypeMap.emplace(varName, pair(typeSign(_vardecl.t()), type)); v("type", type); v("varName", varName); v("value", varExists ? visit(_vardecl.value()) : to_string((*m_rand)())); @@ -129,21 +129,6 @@ string ProtoConverter::visit(BinaryOp const& _bop) case BinaryOp_Op_MOD: op = " % "; break; -// case BinaryOp_Op_ADDSELF: -// op = " += "; -// break; -// case BinaryOp_Op_SUBSELF: -// op = " -= "; -// break; -// case BinaryOp_Op_MULSELF: -// op = " *= "; -// break; -// case BinaryOp_Op_DIVSELF: -// op = " /= "; -// break; -// case BinaryOp_Op_MODSELF: -// op = " %= "; -// break; case BinaryOp_Op_EXP: op = " ** "; break; @@ -153,14 +138,23 @@ string ProtoConverter::visit(BinaryOp const& _bop) case BinaryOp_Op_SHR: op = " >> "; break; -// case BinaryOp_Op_SHLSELF: -// op = " <<= "; -// break; -// case BinaryOp_Op_SHRSELF: -// op = " >>= "; -// break; } - return visit(_bop.left()) + op + visit(_bop.right()); + auto left = visit(_bop.left()); + auto right = visit(_bop.right()); + + Sign leftSign = m_exprSignMap[&_bop.left()]; + Sign rightSign = m_exprSignMap[&_bop.right()]; + + bool expSignChange = _bop.op() == BinaryOp_Op_EXP && rightSign == Sign::Signed; + if (expSignChange) + { + right = Whiskers(R"(uint())")("expr", right).render(); + rightSign = Sign::Unsigned; + } + if (leftSign != rightSign && _bop.op() != BinaryOp_Op_EXP) + right = signString(leftSign) + "(" + right + ")"; + + return left + op + right; } string ProtoConverter::visit(Expression const& _expr) @@ -173,21 +167,27 @@ string ProtoConverter::visit(Expression const& _expr) string v = visit(_expr.v()); if (!m_exprSignMap.count(&_expr)) m_exprSignMap.emplace(&_expr, m_varTypeMap[v].first); + return v; } case Expression::kBop: { string b = visit(_expr.bop()); + solAssert(m_exprSignMap.count(&_expr.bop().left()), "Sol arith fuzzer: Invalid binop visit"); if (!m_exprSignMap.count(&_expr)) - m_exprSignMap.emplace(&_expr, m_varTypeMap[v].first); + m_exprSignMap.emplace(&_expr, m_exprSignMap[&_expr.bop().left()]); + return b; } case Expression::EXPR_ONEOF_NOT_SET: + { + m_exprSignMap.emplace(&_expr, m_varTypeMap["v0"].first); return "v0"; } + } } -string ProtoConverter::visit(VarRef const&) +string ProtoConverter::visit(VarRef const& _v) { - return randomVarName(); + return "v" + std::to_string(_v.id() % m_varCounter); } string ProtoConverter::visit(Assignment const& _assignment) @@ -198,7 +198,7 @@ string ProtoConverter::visit(Assignment const& _assignment) solAssert(m_varTypeMap.count(varName), "Sol arith fuzzer: Invalid varname"); Whiskers a(R"( = ();)"); a("varName", varName); - a("type", m_varTypeMap[varName]); + a("type", m_varTypeMap[varName].second); a("expr", visit(_assignment.value())); return "\t\t" + a.render() + '\n'; } diff --git a/test/tools/ossfuzz/solarithprotoToSol.h b/test/tools/ossfuzz/solarithprotoToSol.h index a8efa2398..987d91291 100644 --- a/test/tools/ossfuzz/solarithprotoToSol.h +++ b/test/tools/ossfuzz/solarithprotoToSol.h @@ -91,7 +91,11 @@ private: } static std::string signString(Type::Sign _sign) { - return _sign == Type::Sign::Type_Sign_SIGNED ? "uint" : "uint"; + return _sign == Type::Sign::Type_Sign_SIGNED ? "int" : "uint"; + } + static std::string signString(Sign _sign) + { + return _sign == Sign::Signed ? "int" : "uint"; } static std::string widthString(unsigned _width) {