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 <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <string>
|
2019-03-20 15:21:38 +00:00
|
|
|
#include <ostream>
|
|
|
|
#include <sstream>
|
2019-03-20 15:21:38 +00:00
|
|
|
#include <stack>
|
2019-04-17 09:41:28 +00:00
|
|
|
#include <set>
|
2019-04-09 06:45:36 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <tuple>
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
#include <test/tools/ossfuzz/yulProto.pb.h>
|
2019-04-17 09:41:28 +00:00
|
|
|
#include <libdevcore/Common.h>
|
|
|
|
#include <libdevcore/FixedHash.h>
|
2019-01-17 10:19:54 +00:00
|
|
|
|
|
|
|
namespace yul
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
namespace yul_fuzzer
|
|
|
|
{
|
2019-03-20 15:21:38 +00:00
|
|
|
class ProtoConverter
|
|
|
|
{
|
|
|
|
public:
|
2019-03-20 15:21:38 +00:00
|
|
|
ProtoConverter()
|
|
|
|
{
|
2019-03-26 09:52:30 +00:00
|
|
|
m_inForBodyScope = false;
|
|
|
|
m_inForInitScope = false;
|
|
|
|
m_numNestedForLoops = 0;
|
2019-06-15 15:11:26 +00:00
|
|
|
m_counter = 0;
|
|
|
|
m_inputSize = 0;
|
2019-08-27 12:57:45 +00:00
|
|
|
m_inFunctionDef = false;
|
2019-03-20 15:21:38 +00:00
|
|
|
}
|
|
|
|
ProtoConverter(ProtoConverter const&) = delete;
|
|
|
|
ProtoConverter(ProtoConverter&&) = delete;
|
2019-04-09 06:45:36 +00:00
|
|
|
std::string programToString(Program const& _input);
|
2019-03-20 15:21:38 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void visit(BinaryOp const&);
|
2019-08-27 12:57:45 +00:00
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Visits a basic block optionally adding @a _funcParams to scope.
|
|
|
|
/// @param _block Reference to a basic block of yul statements.
|
|
|
|
/// @param _funcParams List of function parameter names, defaults to
|
|
|
|
/// an empty vector.
|
|
|
|
void visit(Block const& _block, std::vector<std::string> _funcParams = {});
|
|
|
|
|
2019-08-22 19:53:16 +00:00
|
|
|
std::string visit(Literal const&);
|
2019-03-20 15:21:38 +00:00
|
|
|
void visit(VarRef const&);
|
|
|
|
void visit(Expression const&);
|
|
|
|
void visit(VarDecl const&);
|
|
|
|
void visit(TypedVarDecl const&);
|
|
|
|
void visit(UnaryOp const&);
|
|
|
|
void visit(AssignmentStatement const&);
|
|
|
|
void visit(IfStmt const&);
|
|
|
|
void visit(StoreFunc const&);
|
|
|
|
void visit(Statement const&);
|
|
|
|
void visit(ForStmt const&);
|
2019-03-26 09:52:30 +00:00
|
|
|
void visit(BoundedForStmt const&);
|
2019-03-20 15:21:38 +00:00
|
|
|
void visit(CaseStmt const&);
|
|
|
|
void visit(SwitchStmt const&);
|
2019-03-26 09:52:30 +00:00
|
|
|
void visit(TernaryOp const&);
|
2019-03-26 09:52:30 +00:00
|
|
|
void visit(NullaryOp const&);
|
|
|
|
void visit(LogFunc const&);
|
2019-03-26 09:52:30 +00:00
|
|
|
void visit(CopyFunc const&);
|
|
|
|
void visit(ExtCodeCopy const&);
|
2019-03-26 09:52:30 +00:00
|
|
|
void visit(StopInvalidStmt const&);
|
|
|
|
void visit(RetRevStmt const&);
|
|
|
|
void visit(SelfDestructStmt const&);
|
|
|
|
void visit(TerminatingStmt const&);
|
2019-04-09 06:45:36 +00:00
|
|
|
void visit(FunctionCall const&);
|
2019-08-27 12:57:45 +00:00
|
|
|
void visit(FunctionDef const&);
|
2019-04-09 06:45:36 +00:00
|
|
|
void visit(Program const&);
|
2019-03-20 15:21:38 +00:00
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Creates a new scope, and adds @a _funcParams to it if it
|
|
|
|
/// is non-empty.
|
2019-08-27 12:57:45 +00:00
|
|
|
void openScope(std::vector<std::string> const& _funcParams);
|
|
|
|
/// Closes current scope
|
|
|
|
void closeScope();
|
|
|
|
/// Adds @a _vars to current scope
|
2019-08-27 12:57:45 +00:00
|
|
|
void addVarsToScope(std::vector<std::string> const& _vars);
|
2019-08-27 12:57:45 +00:00
|
|
|
|
2019-06-15 15:11:26 +00:00
|
|
|
std::string createHex(std::string const& _hexBytes);
|
2019-08-22 19:53:16 +00:00
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Returns a new variable name.
|
|
|
|
std::string newVarName()
|
|
|
|
{
|
|
|
|
return "x_" + std::to_string(counter());
|
|
|
|
}
|
|
|
|
|
2019-08-22 19:53:16 +00:00
|
|
|
/// Accepts an arbitrary string, removes all characters that are neither
|
|
|
|
/// alphabets nor digits from it and returns the said string.
|
2019-06-15 15:11:26 +00:00
|
|
|
std::string createAlphaNum(std::string const& _strBytes);
|
2019-04-09 06:45:36 +00:00
|
|
|
enum class NumFunctionReturns
|
|
|
|
{
|
|
|
|
None,
|
|
|
|
Single,
|
|
|
|
Multiple
|
|
|
|
};
|
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
void visitFunctionInputParams(FunctionCall const&, unsigned);
|
|
|
|
void createFunctionDefAndCall(FunctionDef const&, unsigned, unsigned);
|
2019-04-09 06:45:36 +00:00
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Convert function type to a string to be used while naming a
|
|
|
|
/// function that is created by a function declaration statement.
|
|
|
|
/// @param _type Type classified according to the number of
|
|
|
|
/// values returned by function.
|
|
|
|
/// @return A string as follows. If _type is
|
|
|
|
/// None -> "n"
|
|
|
|
/// Single -> "s"
|
|
|
|
/// Multiple -> "m"
|
2019-04-09 06:45:36 +00:00
|
|
|
std::string functionTypeToString(NumFunctionReturns _type);
|
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Return true if at least one variable declaration is in scope,
|
|
|
|
/// false otherwise.
|
|
|
|
/// @return True in the following cases:
|
|
|
|
/// - If we are inside a function that has already declared a variable
|
|
|
|
/// - If there is at least one variable declaration that is
|
|
|
|
/// in scope
|
|
|
|
bool varDeclAvailable();
|
|
|
|
|
|
|
|
/// Return true if a function call cannot be made, false otherwise.
|
|
|
|
/// @param _type is an enum denoting the type of function call. It
|
|
|
|
/// can be one of NONE, SINGLE, MULTIDECL, MULTIASSIGN.
|
|
|
|
/// NONE -> Function call does not return a value
|
|
|
|
/// SINGLE -> Function call returns a single value
|
|
|
|
/// MULTIDECL -> Function call returns more than one value
|
|
|
|
/// and it is used to create a multi declaration
|
|
|
|
/// statement
|
|
|
|
/// MULTIASSIGN -> Function call returns more than one value
|
|
|
|
/// and it is used to create a multi assignment
|
|
|
|
/// statement
|
|
|
|
/// @return True if the function call cannot be created for one of the
|
|
|
|
/// following reasons
|
|
|
|
// - It is a SINGLE function call (we reserve SINGLE functions for
|
|
|
|
// expressions)
|
|
|
|
// - It is a MULTIASSIGN function call and we do not have any
|
|
|
|
// variables available for assignment.
|
|
|
|
bool functionCallNotPossible(FunctionCall_Returns _type);
|
|
|
|
|
|
|
|
/// Checks if function call of type @a _type returns the correct number
|
|
|
|
/// of values.
|
|
|
|
/// @param _type Function call type of the function being checked
|
|
|
|
/// @param _numOutParams Number of values returned by the function
|
|
|
|
/// being checked
|
|
|
|
/// @return true if the function returns the correct number of values,
|
|
|
|
/// false otherwise
|
|
|
|
bool functionValid(FunctionCall_Returns _type, unsigned _numOutParams);
|
|
|
|
|
|
|
|
/// Converts protobuf function call to a yul function call and appends
|
|
|
|
/// it to output stream.
|
|
|
|
/// @param _x Protobuf function call
|
|
|
|
/// @param _name Function name
|
|
|
|
/// @param _numInParams Number of input arguments accepted by function
|
|
|
|
/// @param _newLine Flag that prints a new line to the output stream if
|
|
|
|
/// true. Default value for the flag is true.
|
|
|
|
void convertFunctionCall(
|
|
|
|
FunctionCall const& _x,
|
|
|
|
std::string _name,
|
|
|
|
unsigned _numInParams,
|
|
|
|
bool _newLine = true
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Prints a yul formatted variable declaration statement to the output
|
|
|
|
/// stream.
|
|
|
|
/// Example 1: createVarDecls(0, 1, true) returns {"x_0"} and prints
|
|
|
|
/// let x_0 :=
|
|
|
|
/// Example 2: createVarDecls(0, 2, false) returns {"x_0", "x_1"} and prints
|
|
|
|
/// let x_0, x_1
|
|
|
|
/// @param _start Start index of variable (inclusive)
|
|
|
|
/// @param _end End index of variable (exclusive)
|
|
|
|
/// @param _isAssignment Flag indicating if variable declaration is also
|
|
|
|
/// an assignment. If true, the string " := " follows the variable
|
|
|
|
/// declaration. Otherwise, a new line is follows the variable
|
|
|
|
/// declaration.
|
|
|
|
/// @return A vector of strings containing the variable names used in
|
|
|
|
/// the declaration statement.
|
|
|
|
std::vector<std::string> createVarDecls(unsigned _start, unsigned _end, bool _isAssignment);
|
|
|
|
|
|
|
|
/// Prints comma separated variable names to output stream and
|
|
|
|
/// returns a vector containing the printed variable names.
|
|
|
|
/// Example: createVars(0, 2) returns {"x_0", "x_1"} and prints
|
|
|
|
/// x_0, x_1
|
|
|
|
/// @param _startIdx Start index of variable (inclusive)
|
|
|
|
/// @param _endIdx End index of variable (exclusive)
|
|
|
|
/// @return A vector of strings containing the printed variable names.
|
2019-08-27 12:57:45 +00:00
|
|
|
std::vector<std::string> createVars(unsigned _startIdx, unsigned _endIdx);
|
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Register a function declaration
|
|
|
|
/// @param _f Pointer to a FunctionDef object
|
|
|
|
void registerFunction(FunctionDef const* _f);
|
|
|
|
|
|
|
|
/// Removes entry from m_functionMap and m_functionName
|
|
|
|
void updateFunctionMaps(std::string const& _x);
|
|
|
|
|
2019-06-15 15:11:26 +00:00
|
|
|
/// Returns a pseudo-random dictionary token.
|
|
|
|
/// @param _p Enum that decides if the returned token is hex prefixed ("0x") or not
|
|
|
|
/// @return Dictionary token at the index computed using a
|
|
|
|
/// monotonically increasing counter as follows:
|
|
|
|
/// index = (m_inputSize * m_inputSize + counter) % dictionarySize
|
|
|
|
/// where m_inputSize is the size of the protobuf input and
|
|
|
|
/// dictionarySize is the total number of entries in the dictionary.
|
|
|
|
std::string dictionaryToken(dev::HexPrefix _p = dev::HexPrefix::Add);
|
|
|
|
|
|
|
|
/// Returns a monotonically increasing counter that starts from zero.
|
|
|
|
unsigned counter()
|
|
|
|
{
|
|
|
|
return m_counter++;
|
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Generate function name of the form "foo_<typeSuffix>_<counter>".
|
|
|
|
/// @param _type Type classified according to the number of
|
|
|
|
/// values returned by function.
|
|
|
|
std::string functionName(NumFunctionReturns _type)
|
|
|
|
{
|
|
|
|
return "foo_" + functionTypeToString(_type) + "_" + std::to_string(counter());
|
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
std::ostringstream m_output;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Variables in current scope
|
|
|
|
std::stack<std::vector<std::string>> m_scopeVars;
|
|
|
|
/// Functions in current scope
|
|
|
|
std::stack<std::vector<std::string>> m_scopeFuncs;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Variables
|
|
|
|
std::vector<std::string> m_variables;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Functions
|
|
|
|
std::vector<std::string> m_functions;
|
|
|
|
/// Maps FunctionDef object to its name
|
|
|
|
std::map<FunctionDef const*, std::string> m_functionDefMap;
|
2019-04-09 06:45:36 +00:00
|
|
|
// Set that is used for deduplicating switch case literals
|
2019-04-17 09:41:28 +00:00
|
|
|
std::stack<std::set<dev::u256>> m_switchLiteralSetPerScope;
|
2019-04-09 06:45:36 +00:00
|
|
|
// Look-up table per function type that holds the number of input (output) function parameters
|
2019-08-27 12:57:45 +00:00
|
|
|
std::map<std::string, std::pair<unsigned, unsigned>> m_functionSigMap;
|
2019-04-09 06:45:36 +00:00
|
|
|
// mod input/output parameters impose an upper bound on the number of input/output parameters a function may have.
|
2019-08-27 12:57:45 +00:00
|
|
|
static unsigned constexpr s_modInputParams = 5;
|
|
|
|
static unsigned constexpr s_modOutputParams = 5;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Predicate to keep track of for body scope. If true, break/continue
|
|
|
|
/// statements can not be created.
|
2019-03-26 09:52:30 +00:00
|
|
|
bool m_inForBodyScope;
|
|
|
|
// Index used for naming loop variable of bounded for loops
|
|
|
|
unsigned m_numNestedForLoops;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Predicate to keep track of for loop init scope. If true, variable
|
|
|
|
/// or function declarations can not be created.
|
2019-03-26 09:52:30 +00:00
|
|
|
bool m_inForInitScope;
|
2019-06-15 15:11:26 +00:00
|
|
|
/// Monotonically increasing counter
|
|
|
|
unsigned m_counter;
|
|
|
|
/// Size of protobuf input
|
|
|
|
unsigned m_inputSize;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Predicate that is true if inside function definition, false otherwise
|
|
|
|
bool m_inFunctionDef;
|
2019-03-20 15:21:38 +00:00
|
|
|
};
|
2019-01-17 10:19:54 +00:00
|
|
|
}
|
2019-03-14 14:40:54 +00:00
|
|
|
}
|
2019-03-20 15:21:38 +00:00
|
|
|
}
|