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-09-11 08:57:07 +00:00
|
|
|
|
2019-04-17 09:41:28 +00:00
|
|
|
#include <libdevcore/Common.h>
|
|
|
|
#include <libdevcore/FixedHash.h>
|
2019-09-11 08:57:07 +00:00
|
|
|
#include <libdevcore/Whiskers.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-11-05 22:37:04 +00:00
|
|
|
m_funcVars = std::vector<std::vector<std::vector<std::string>>>{};
|
2019-11-18 11:12:30 +00:00
|
|
|
m_globalVars = std::vector<std::vector<std::string>>{};
|
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-05-27 09:18:05 +00:00
|
|
|
m_objectId = 0;
|
|
|
|
m_isObject = 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.
|
2019-11-05 22:37:04 +00:00
|
|
|
void visit(Block const& _block);
|
2019-08-27 12:57:45 +00:00
|
|
|
|
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-09-04 11:24:47 +00:00
|
|
|
void visit(PopStmt const&);
|
2019-11-01 10:18:05 +00:00
|
|
|
void visit(LeaveStmt const&);
|
2019-09-04 14:05:26 +00:00
|
|
|
void visit(LowLevelCall const&);
|
|
|
|
void visit(Create const&);
|
2019-05-27 09:18:05 +00:00
|
|
|
void visit(UnaryOpData const&);
|
|
|
|
void visit(Object const&);
|
|
|
|
void visit(Data const&);
|
|
|
|
void visit(Code const&);
|
2019-04-09 06:45:36 +00:00
|
|
|
void visit(Program const&);
|
2019-03-20 15:21:38 +00:00
|
|
|
|
2019-11-05 22:37:04 +00:00
|
|
|
/// Creates a new block scope.
|
|
|
|
void openBlockScope();
|
|
|
|
/// Creates a new function scope, and adds @a _funcParams to it if it
|
2019-08-27 12:57:45 +00:00
|
|
|
/// is non-empty.
|
2019-11-05 22:37:04 +00:00
|
|
|
void openFunctionScope(std::vector<std::string> const& _funcParams);
|
|
|
|
/// Closes current block scope
|
|
|
|
void closeBlockScope();
|
|
|
|
/// Closes current function scope
|
|
|
|
void closeFunctionScope();
|
2019-08-27 12:57:45 +00:00
|
|
|
/// 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-05-27 09:18:05 +00:00
|
|
|
|
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-11-05 22:37:04 +00:00
|
|
|
/// Builds a single vector containing variables declared in
|
|
|
|
/// function scope.
|
|
|
|
void consolidateVarDeclsInFunctionDef();
|
|
|
|
|
|
|
|
/// Builds a single vector containing variables declared in
|
|
|
|
/// global scope.
|
|
|
|
void consolidateGlobalVarDecls();
|
|
|
|
|
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-09-03 12:28:01 +00:00
|
|
|
/// Print the yul syntax to make a call to a function named @a _funcName to
|
|
|
|
/// the output stream.
|
|
|
|
/// @param _funcName Name of the function to be called
|
|
|
|
/// @param _numInParams Number of input parameters in function signature
|
|
|
|
/// @param _numOutParams Number of output parameters in function signature
|
|
|
|
void createFunctionCall(std::string _funcName, unsigned _numInParams, unsigned _numOutParams);
|
|
|
|
|
2019-09-03 13:14:17 +00:00
|
|
|
/// Print the yul syntax to pass input arguments to a function that has
|
|
|
|
/// @a _numInParams number of input parameters to the output stream.
|
|
|
|
/// The input arguments are pseudo-randomly chosen from calldata, memory,
|
|
|
|
/// storage, or the yul optimizer hex dictionary.
|
|
|
|
/// @param _numInParams Number of input arguments to fill
|
|
|
|
void fillFunctionCallInput(unsigned _numInParams);
|
|
|
|
|
|
|
|
/// Print the yul syntax to save values returned by a function call
|
|
|
|
/// to the output stream. The values are either stored to memory or
|
|
|
|
/// storage based on a simulated coin flip. The saved location is
|
|
|
|
/// decided pseudo-randomly.
|
|
|
|
/// @param _varsVec A vector of strings that reference variables
|
|
|
|
/// holding the return values of a function call.
|
|
|
|
void saveFunctionCallOutput(std::vector<std::string> const& _varsVec);
|
|
|
|
|
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-09-11 08:57:07 +00:00
|
|
|
/// Build a tree of objects that contains the object/data
|
|
|
|
/// identifiers that are in scope in a given object.
|
|
|
|
/// @param _x root object of the yul protobuf specification.
|
|
|
|
void buildObjectScopeTree(Object 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-09-11 08:57:07 +00:00
|
|
|
/// Returns a pseudo-randomly chosen object identifier that is in the
|
|
|
|
/// scope of the Yul object being visited.
|
|
|
|
std::string getObjectIdentifier(ObjectId const& _x);
|
2019-05-27 09:18:05 +00:00
|
|
|
|
|
|
|
/// Return new object identifier as string. Identifier string
|
|
|
|
/// is a template of the form "\"object<n>\"" where <n> is
|
|
|
|
/// a monotonically increasing object ID counter.
|
2019-09-11 08:57:07 +00:00
|
|
|
/// @param _decorate If true (default value), object ID is
|
|
|
|
/// enclosed within double quotes.
|
|
|
|
std::string newObjectId(bool _decorate = true)
|
|
|
|
{
|
|
|
|
return dev::Whiskers(R"(<?decorate>"</decorate>object<id><?decorate>"</decorate>)")
|
|
|
|
("decorate", _decorate)
|
|
|
|
("id", std::to_string(m_objectId++))
|
|
|
|
.render();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the object counter value corresponding to the object
|
|
|
|
/// being visited.
|
|
|
|
unsigned currentObjectId()
|
2019-05-27 09:18:05 +00:00
|
|
|
{
|
2019-09-11 08:57:07 +00:00
|
|
|
return m_objectId - 1;
|
2019-05-27 09:18:05 +00:00
|
|
|
}
|
|
|
|
|
2019-03-20 15:21:38 +00:00
|
|
|
std::ostringstream m_output;
|
2019-11-05 22:37:04 +00:00
|
|
|
/// Variables in all function definitions
|
|
|
|
std::vector<std::vector<std::vector<std::string>>> m_funcVars;
|
|
|
|
/// Variables in current function definition
|
2019-11-18 11:12:30 +00:00
|
|
|
std::vector<std::string const*> m_currentFuncVars;
|
2019-11-05 22:37:04 +00:00
|
|
|
/// Variables in global scope
|
2019-11-18 11:12:30 +00:00
|
|
|
std::vector<std::string const*> m_currentGlobalVars;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Functions in current scope
|
2019-11-05 22:37:04 +00:00
|
|
|
std::vector<std::vector<std::string>> m_scopeFuncs;
|
2019-08-27 12:57:45 +00:00
|
|
|
/// Variables
|
2019-11-18 11:12:30 +00:00
|
|
|
std::vector<std::vector<std::string>> m_globalVars;
|
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-09-11 08:57:07 +00:00
|
|
|
/// Tree of objects and their scopes
|
|
|
|
std::vector<std::vector<std::string>> m_objectScopeTree;
|
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-09-11 08:57:07 +00:00
|
|
|
/// Hard-coded identifier for a Yul object's data block
|
|
|
|
static auto constexpr s_dataIdentifier = "datablock";
|
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-05-27 09:18:05 +00:00
|
|
|
/// Index used for naming objects
|
|
|
|
unsigned m_objectId;
|
|
|
|
/// Flag to track whether program is an object (true) or a statement block
|
|
|
|
/// (false: default value)
|
|
|
|
bool m_isObject;
|
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
|
|
|
}
|