Control flow side-effects for user-defined functions.

This commit is contained in:
chriseth
2021-10-14 17:46:07 +02:00
parent d443fe415d
commit 9417d6775f
20 changed files with 637 additions and 32 deletions
+2
View File
@@ -130,6 +130,8 @@ set(libyul_sources
libyul/CompilabilityChecker.cpp
libyul/ControlFlowGraphTest.cpp
libyul/ControlFlowGraphTest.h
libyul/ControlFlowSideEffectsTest.cpp
libyul/ControlFlowSideEffectsTest.h
libyul/EVMCodeTransformTest.cpp
libyul/EVMCodeTransformTest.h
libyul/EwasmTranslationTest.cpp
+4 -2
View File
@@ -31,6 +31,7 @@
#include <test/libyul/YulOptimizerTest.h>
#include <test/libyul/YulInterpreterTest.h>
#include <test/libyul/ObjectCompilerTest.h>
#include <test/libyul/ControlFlowSideEffectsTest.h>
#include <test/libyul/FunctionSideEffects.h>
#include <test/libyul/StackLayoutGeneratorTest.h>
#include <test/libyul/SyntaxTest.h>
@@ -57,12 +58,12 @@ struct Testsuite
Testsuite const g_interactiveTestsuites[] = {
/*
Title Path Subpath SMT NeedsVM Creator function */
{"Ewasm Translation", "libyul", "ewasmTranslationTests", false, false, &yul::test::EwasmTranslationTest::create},
{"Yul Optimizer", "libyul", "yulOptimizerTests", false, false, &yul::test::YulOptimizerTest::create},
{"Yul Interpreter", "libyul", "yulInterpreterTests", false, false, &yul::test::YulInterpreterTest::create},
{"Yul Object Compiler", "libyul", "objectCompiler", false, false, &yul::test::ObjectCompilerTest::create},
{"Yul Control Flow Graph", "libyul", "yulControlFlowGraph", false, false, &yul::test::ControlFlowGraphTest::create},
{"Yul Stack Layout", "libyul", "yulStackLayout", false, false, &yul::test::StackLayoutGeneratorTest::create},
{"Control Flow Side Effects","libyul", "controlFlowSideEffects",false, false, &yul::test::ControlFlowSideEffectsTest::create},
{"Function Side Effects", "libyul", "functionSideEffects", false, false, &yul::test::FunctionSideEffects::create},
{"Yul Syntax", "libyul", "yulSyntaxTests", false, false, &yul::test::SyntaxTest::create},
{"EVM Code Transform", "libyul", "evmCodeTransform", false, false, &yul::test::EVMCodeTransformTest::create, {"nooptions"}},
@@ -72,7 +73,8 @@ Testsuite const g_interactiveTestsuites[] = {
{"JSON AST", "libsolidity", "ASTJSON", false, false, &ASTJSONTest::create},
{"JSON ABI", "libsolidity", "ABIJson", false, false, &ABIJsonTest::create},
{"SMT Checker", "libsolidity", "smtCheckerTests", true, false, &SMTCheckerTest::create},
{"Gas Estimates", "libsolidity", "gasTests", false, false, &GasTest::create}
{"Gas Estimates", "libsolidity", "gasTests", false, false, &GasTest::create},
{"Ewasm Translation", "libyul", "ewasmTranslationTests", false, false, &yul::test::EwasmTranslationTest::create}
};
}
@@ -0,0 +1,79 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <test/libyul/ControlFlowSideEffectsTest.h>
#include <test/Common.h>
#include <test/libyul/Common.h>
#include <libyul/Object.h>
#include <libyul/ControlFlowSideEffects.h>
#include <libyul/ControlFlowSideEffectsCollector.h>
#include <libyul/backends/evm/EVMDialect.h>
using namespace std;
using namespace solidity;
using namespace solidity::yul;
using namespace solidity::yul::test;
using namespace solidity::frontend::test;
namespace
{
string toString(ControlFlowSideEffects const& _sideEffects)
{
vector<string> r;
if (_sideEffects.canTerminate)
r.emplace_back("can terminate");
if (_sideEffects.canRevert)
r.emplace_back("can revert");
if (_sideEffects.canContinue)
r.emplace_back("can continue");
return util::joinHumanReadable(r);
}
}
ControlFlowSideEffectsTest::ControlFlowSideEffectsTest(string const& _filename):
TestCase(_filename)
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
}
TestCase::TestResult ControlFlowSideEffectsTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
Object obj;
std::tie(obj.code, obj.analysisInfo) = yul::test::parse(m_source, false);
if (!obj.code)
BOOST_THROW_EXCEPTION(runtime_error("Parsing input failed."));
std::map<YulString, ControlFlowSideEffects> sideEffects =
ControlFlowSideEffectsCollector(
EVMDialect::strictAssemblyForEVMObjects(langutil::EVMVersion()),
*obj.code
).functionSideEffects();
std::map<std::string, std::string> controlFlowSideEffectsStr;
for (auto&& [fun, effects]: sideEffects)
controlFlowSideEffectsStr[fun.str()] = toString(effects);
m_obtainedResult.clear();
for (auto&& [functionName, effect]: controlFlowSideEffectsStr)
m_obtainedResult += functionName + (effect.empty() ? ":" : ": " + effect) + "\n";
return checkResult(_stream, _linePrefix, _formatted);
}
+40
View File
@@ -0,0 +1,40 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <test/TestCase.h>
#include <iosfwd>
#include <string>
#include <vector>
#include <memory>
namespace solidity::yul::test
{
class ControlFlowSideEffectsTest: public solidity::frontend::test::TestCase
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{ return std::make_unique<ControlFlowSideEffectsTest>(_config.filename); }
explicit ControlFlowSideEffectsTest(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
};
}
@@ -0,0 +1,34 @@
{
function a() {
if calldataload(0) { g() }
}
function b() {
g()
if calldataload(0) { }
}
function c() {
if calldataload(0) { }
g()
}
function d() {
stop()
if calldataload(0) { g() }
}
function e() {
if calldataload(0) { g() }
stop()
}
function f() {
g()
if calldataload(0) { g() }
}
function g() { revert(0, 0) }
}
// ----
// a: can revert, can continue
// b: can revert
// c: can revert
// d: can terminate
// e: can terminate, can revert
// f: can revert
// g: can revert
@@ -0,0 +1,17 @@
{
function a() {}
function f() { g() }
function g() { revert(0, 0) }
function h() { stop() }
function i() { h() }
function j() { h() g() }
function k() { g() h() }
}
// ----
// a: can continue
// f: can revert
// g: can revert
// h: can terminate
// i: can terminate
// j: can terminate
// k: can revert
+1
View File
@@ -33,6 +33,7 @@ add_executable(isoltest
../libsolidity/SMTCheckerTest.cpp
../libyul/Common.cpp
../libyul/ControlFlowGraphTest.cpp
../libyul/ControlFlowSideEffectsTest.cpp
../libyul/EVMCodeTransformTest.cpp
../libyul/EwasmTranslationTest.cpp
../libyul/FunctionSideEffects.cpp