Migrate yulStackShuffling tests to isoltest

This commit is contained in:
Rodrigo Q. Saramago 2023-04-25 12:07:44 +02:00
parent 4a8d6618f5
commit 28c7fdae09
No known key found for this signature in database
GPG Key ID: 9B36B2525704A359
8 changed files with 228 additions and 30 deletions

View File

@ -149,6 +149,7 @@ set(libyul_sources
libyul/StackLayoutGeneratorTest.cpp
libyul/StackLayoutGeneratorTest.h
libyul/StackShufflingTest.cpp
libyul/StackShufflingTest.h
libyul/SyntaxTest.h
libyul/SyntaxTest.cpp
libyul/YulInterpreterTest.cpp

View File

@ -35,6 +35,7 @@
#include <test/libyul/ControlFlowSideEffectsTest.h>
#include <test/libyul/FunctionSideEffects.h>
#include <test/libyul/StackLayoutGeneratorTest.h>
#include <test/libyul/StackShufflingTest.h>
#include <test/libyul/SyntaxTest.h>
#include <boost/filesystem.hpp>
@ -64,6 +65,7 @@ Testsuite const g_interactiveTestsuites[] = {
{"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},
{"Yul Stack Shuffling", "libyul", "yulStackShuffling", false, false, &yul::test::StackShufflingTest::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},

View File

@ -51,7 +51,7 @@ void TestCase::printUpdatedSettings(std::ostream& _stream, std::string const& _l
bool TestCase::isTestFilename(boost::filesystem::path const& _filename)
{
string extension = _filename.extension().string();
return (extension == ".sol" || extension == ".yul") &&
return (extension == ".sol" || extension == ".yul" || extension == ".stack") &&
!boost::starts_with(_filename.string(), "~") &&
!boost::starts_with(_filename.string(), ".");
}

View File

@ -14,41 +14,166 @@
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* Unit tests for stack shuffling.
*/
#include <test/libyul/StackShufflingTest.h>
#include <liblangutil/Scanner.h>
#include <libsolutil/AnsiColorized.h>
#include <libyul/backends/evm/StackHelpers.h>
#include <boost/test/unit_test.hpp>
using namespace std;
using namespace solidity::util;
using namespace solidity::langutil;
using namespace solidity::yul;
using namespace solidity::yul::test;
using namespace std;
namespace solidity::yul::test
bool StackShufflingTest::parse(string const& _source)
{
CharStream stream(_source, "");
Scanner scanner(stream);
BOOST_AUTO_TEST_SUITE(YulStackShuffling)
BOOST_AUTO_TEST_CASE(swap_cycle)
auto expectToken = [&](Token _token)
{
std::vector<Scope::Variable> scopeVariables;
Scope::Function function;
std::vector<VariableSlot> v;
for (size_t i = 0; i < 17; ++i)
scopeVariables.emplace_back(Scope::Variable{""_yulstring, YulString{"v" + to_string(i)}});
for (size_t i = 0; i < 17; ++i)
v.emplace_back(VariableSlot{scopeVariables[i]});
Stack sourceStack{
v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[9], v[10], v[11], v[12], v[13], v[14], v[15], v[16],
FunctionReturnLabelSlot{function}, FunctionReturnLabelSlot{function}, v[5]};
Stack targetStack{
v[1], v[0], v[2], v[3], v[4], v[5], v[6], v[7], v[9], v[10], v[11], v[12], v[13], v[14], v[15], v[16],
FunctionReturnLabelSlot{function}, JunkSlot{}, JunkSlot{}
soltestAssert(
scanner.next() == _token,
"Invalid token. Expected: \"" + TokenTraits::friendlyName(_token) + "\"."
);
};
// Used to hit a swapping cycle.
createStackLayout(sourceStack, targetStack, [](auto){}, [](auto){}, [](){});
auto parseStack = [&](Stack& stack) -> bool
{
if (scanner.currentToken() != Token::LBrack)
return false;
scanner.next();
while (scanner.currentToken() != Token::RBrack &&
scanner.currentToken() != Token::EOS)
{
string literal = scanner.currentLiteral();
if (literal == "RET")
{
scanner.next();
if (scanner.currentToken() == Token::LBrack)
{
scanner.next();
string functionName = scanner.currentLiteral();
auto call = yul::FunctionCall{
{}, yul::Identifier{{}, YulString(functionName)}, {}
};
stack.emplace_back(FunctionCallReturnLabelSlot{
m_functions.insert(
make_pair(functionName, call)
).first->second
});
expectToken(Token::RBrack);
}
else
{
static Scope::Function function;
stack.emplace_back(FunctionReturnLabelSlot{function});
continue;
}
}
else if (literal == "TMP")
{
expectToken(Token::LBrack);
scanner.next();
string functionName = scanner.currentLiteral();
auto call = yul::FunctionCall{
{}, yul::Identifier{{}, YulString(functionName)}, {}
};
expectToken(Token::Comma);
scanner.next();
size_t index = size_t(atoi(scanner.currentLiteral().c_str()));
stack.emplace_back(TemporarySlot{
m_functions.insert(make_pair(functionName, call)).first->second,
index
});
expectToken(Token::RBrack);
}
else if (literal.find("0x") != string::npos || scanner.currentToken() == Token::Number)
{
stack.emplace_back(LiteralSlot{u256(literal)});
}
else if (literal == "JUNK")
{
stack.emplace_back(JunkSlot());
}
else if (literal == "GHOST")
{
expectToken(Token::LBrack);
scanner.next(); // read number of ghost variables as ghostVariableId
string ghostVariableId = scanner.currentLiteral();
Scope::Variable ghostVar = Scope::Variable{""_yulstring, YulString(literal + "[" + ghostVariableId + "]")};
stack.emplace_back(VariableSlot{
m_variables.insert(make_pair(ghostVar.name, ghostVar)).first->second
});
expectToken(Token::RBrack);
}
else
{
Scope::Variable var = Scope::Variable{""_yulstring, YulString(literal)};
stack.emplace_back(VariableSlot{
m_variables.insert(
make_pair(literal, var)
).first->second
});
}
scanner.next();
}
return scanner.currentToken() == Token::RBrack;
};
if (!parseStack(m_sourceStack))
return false;
scanner.next();
return parseStack(m_targetStack);
}
BOOST_AUTO_TEST_SUITE_END()
StackShufflingTest::StackShufflingTest(string const& _filename):
TestCase(_filename)
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
}
TestCase::TestResult StackShufflingTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
if (!parse(m_source))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing source." << endl;
return TestResult::FatalError;
}
ostringstream output;
createStackLayout(
m_sourceStack,
m_targetStack,
[&](unsigned _swapDepth) // swap
{
output << stackToString(m_sourceStack) << endl;
output << "SWAP" << _swapDepth << endl;
},
[&](StackSlot const& _slot) // dupOrPush
{
output << stackToString(m_sourceStack) << endl;
if (canBeFreelyGenerated(_slot))
output << "PUSH " << stackSlotToString(_slot) << endl;
else
{
if (auto depth = util::findOffset(m_sourceStack | ranges::views::reverse, _slot))
output << "DUP" << *depth + 1 << endl;
else
BOOST_THROW_EXCEPTION(runtime_error("Invalid DUP operation."));
}
},
[&](){ // pop
output << stackToString(m_sourceStack) << endl;
output << "POP" << endl;
}
);
output << stackToString(m_sourceStack) << endl;
m_obtainedResult = output.str();
return checkResult(_stream, _linePrefix, _formatted);
}

View File

@ -0,0 +1,47 @@
/*
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 <libyul/backends/evm/ControlFlowGraph.h>
using namespace solidity::frontend::test;
namespace solidity::yul::test
{
class StackShufflingTest: public TestCase
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::make_unique<StackShufflingTest>(_config.filename);
}
explicit StackShufflingTest(std::string const& _filename);
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool const _formatted = false) override;
private:
bool parse(std::string const& _source);
Stack m_sourceStack;
Stack m_targetStack;
std::map<YulString, yul::FunctionCall> m_functions;
std::map<YulString, Scope::Variable> m_variables;
};
}

View File

@ -0,0 +1,22 @@
[ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET RET v5 ]
[ v1 v0 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET JUNK JUNK ]
// ----
// [ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET RET v5 ]
// POP
// [ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET RET ]
// SWAP16
// [ v0 RET v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET v1 ]
// SWAP16
// [ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET RET ]
// POP
// [ v0 v1 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET ]
// SWAP15
// [ v0 RET v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 v1 ]
// SWAP16
// [ v1 RET v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 v0 ]
// SWAP15
// [ v1 v0 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET ]
// PUSH JUNK
// [ v1 v0 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET JUNK ]
// PUSH JUNK
// [ v1 v0 v2 v3 v4 v5 v6 v7 v9 v10 v11 v12 v13 v14 v15 v16 RET JUNK JUNK ]

View File

@ -40,6 +40,7 @@ add_executable(isoltest
../libyul/FunctionSideEffects.cpp
../libyul/ObjectCompilerTest.cpp
../libyul/SyntaxTest.cpp
../libyul/StackShufflingTest.cpp
../libyul/StackLayoutGeneratorTest.cpp
../libyul/YulOptimizerTest.cpp
../libyul/YulOptimizerTestCommon.cpp

View File

@ -76,7 +76,7 @@ public:
boost::replace_all(filter, "/", "\\/");
boost::replace_all(filter, "*", ".*");
m_filterExpression = regex{"(" + filter + "(\\.sol|\\.yul))"};
m_filterExpression = regex{"(" + filter + "(\\.sol|\\.yul|\\.stack))"};
}
bool matches(fs::path const& _path, string const& _name) const