diff --git a/test/tools/ossfuzz/Generators.h b/test/tools/ossfuzz/Generators.h index e02bddd7a..47e98cbb0 100644 --- a/test/tools/ossfuzz/Generators.h +++ b/test/tools/ossfuzz/Generators.h @@ -56,5 +56,6 @@ MACRO(SourceUnitGenerator) SEP \ MACRO(StatementGenerator) SEP \ MACRO(TestCaseGenerator) SEP \ + MACRO(TryCatchStmtGenerator) SEP \ MACRO(VarDeclStmtGenerator) SEP \ MACRO(WhileStmtGenerator) ENDSEP diff --git a/test/tools/ossfuzz/SolidityGenerator.cpp b/test/tools/ossfuzz/SolidityGenerator.cpp index ee59e34b3..2bc991ac6 100644 --- a/test/tools/ossfuzz/SolidityGenerator.cpp +++ b/test/tools/ossfuzz/SolidityGenerator.cpp @@ -1567,13 +1567,36 @@ string FunctionCallGenerator::callStmt(shared_ptr _callee) return callStmtStream.str(); } +string FunctionCallGenerator::generateTryCatchCall() +{ + set> availableFunctions; + if (state->insideContract) + availableFunctions = state->currentContractState()->functions; + if (availableFunctions.empty()) + return {}; + + shared_ptr callee; + if (availableFunctions.size() > 1) + { + for (auto const& i: availableFunctions) + if (uRandDist()->probable(availableFunctions.size())) + callee = i; + } + else + callee = *availableFunctions.begin(); + + if (callee) + { + ostringstream tryCatchCall; + // TODO: Implement try-catch stmt + return tryCatchCall.str(); + } + else + return {}; +} + string FunctionCallGenerator::visit() { -// // TODO: Generalise call to varargs function -// for (auto const& f: state->currentFunctionState()->inputs) -// if (holds_alternative>(f.first)) -// return indentation() + f.second + "();\n"; - // Consolidate available functions auto availableFunctions = state->currentSourceState()->freeFunctions; if (state->insideContract) @@ -1597,6 +1620,25 @@ string FunctionCallGenerator::visit() return {}; } +string TryCatchStmtGenerator::visit() +{ + auto availableFunctions = state->currentSourceState()->freeFunctions; + if (state->insideContract) + availableFunctions += state->currentContractState()->functions; + if (availableFunctions.empty()) + return {}; + + shared_ptr callee; + if (availableFunctions.size() > 1) + { + for (auto const& i: availableFunctions) + if (uRandDist()->probable(availableFunctions.size())) + callee = i; + } + else + callee = *availableFunctions.begin(); +} + template shared_ptr SolidityGenerator::generator() { diff --git a/test/tools/ossfuzz/SolidityGenerator.h b/test/tools/ossfuzz/SolidityGenerator.h index 5c75e26f9..1ba9342f9 100644 --- a/test/tools/ossfuzz/SolidityGenerator.h +++ b/test/tools/ossfuzz/SolidityGenerator.h @@ -1281,9 +1281,23 @@ public: { return "Function call generator"; } + std::string generateTryCatchCall(); private: std::string lhs(std::vector>& _functionReturnTypeNames); std::optional rhs(std::vector>& _functionInputTypeNames); std::string callStmt(std::shared_ptr _callee); }; + +class TryCatchStmtGenerator: public GeneratorBase +{ +public: + TryCatchStmtGenerator(SolidityGenerator* _mutator): + GeneratorBase(std::move(_mutator)) + {} + std::string visit() override; + std::string name() override + { + return "Try/Catch statement generator"; + } +}; }