2018-11-07 11:02:11 +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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @date 2018
|
|
|
|
* Unit tests for the Yul object parser.
|
|
|
|
*/
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2018-11-07 11:02:11 +00:00
|
|
|
|
|
|
|
#include <test/libsolidity/ErrorCheck.h>
|
|
|
|
|
2021-09-17 18:15:19 +00:00
|
|
|
#include <liblangutil/DebugInfoSelection.h>
|
2021-07-14 10:53:39 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
|
|
|
|
2022-03-28 04:27:11 +00:00
|
|
|
#include <libyul/YulStack.h>
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
2018-11-07 11:02:11 +00:00
|
|
|
|
2019-03-27 11:49:50 +00:00
|
|
|
#include <libsolidity/interface/OptimiserSettings.h>
|
|
|
|
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
2018-11-07 11:02:11 +00:00
|
|
|
#include <boost/algorithm/string/replace.hpp>
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2018-11-07 11:02:11 +00:00
|
|
|
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <range/v3/view/iota.hpp>
|
|
|
|
|
2018-11-07 11:02:11 +00:00
|
|
|
#include <memory>
|
2019-10-28 10:39:30 +00:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2021-07-12 10:05:26 +00:00
|
|
|
#include <sstream>
|
2018-11-07 11:02:11 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::frontend;
|
|
|
|
using namespace solidity::langutil;
|
2018-11-07 11:02:11 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul::test
|
2018-11-07 11:02:11 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2021-07-12 10:05:26 +00:00
|
|
|
pair<bool, ErrorList> parse(string const& _source)
|
2018-11-07 11:02:11 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2022-03-28 04:27:11 +00:00
|
|
|
YulStack asmStack(
|
2020-01-14 16:48:17 +00:00
|
|
|
solidity::test::CommonOptions::get().evmVersion(),
|
2022-11-22 12:05:20 +00:00
|
|
|
solidity::test::CommonOptions::get().eofVersion(),
|
2022-03-28 04:27:11 +00:00
|
|
|
YulStack::Language::StrictAssembly,
|
2021-09-17 18:15:19 +00:00
|
|
|
solidity::frontend::OptimiserSettings::none(),
|
|
|
|
DebugInfoSelection::All()
|
2018-11-07 11:02:11 +00:00
|
|
|
);
|
|
|
|
bool success = asmStack.parseAndAnalyze("source", _source);
|
|
|
|
return {success, asmStack.errors()};
|
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
|
|
|
BOOST_FAIL("Fatal error leaked.");
|
|
|
|
}
|
|
|
|
return {false, {}};
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:48:45 +00:00
|
|
|
optional<Error> parseAndReturnFirstError(string const& _source, bool _allowWarningsAndInfos = true)
|
2018-11-07 11:02:11 +00:00
|
|
|
{
|
|
|
|
bool success;
|
|
|
|
ErrorList errors;
|
|
|
|
tie(success, errors) = parse(_source);
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
BOOST_REQUIRE_EQUAL(errors.size(), 1);
|
|
|
|
return *errors.front();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If success is true, there might still be an error in the assembly stage.
|
2021-06-30 12:48:45 +00:00
|
|
|
if (_allowWarningsAndInfos && !Error::containsErrors(errors))
|
2018-11-07 11:02:11 +00:00
|
|
|
return {};
|
|
|
|
else if (!errors.empty())
|
|
|
|
{
|
2021-06-30 12:48:45 +00:00
|
|
|
if (!_allowWarningsAndInfos)
|
2018-11-07 11:02:11 +00:00
|
|
|
BOOST_CHECK_EQUAL(errors.size(), 1);
|
|
|
|
return *errors.front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-06-30 12:48:45 +00:00
|
|
|
bool successParse(string const& _source, bool _allowWarningsAndInfos = true)
|
2018-11-07 11:02:11 +00:00
|
|
|
{
|
2021-06-30 12:48:45 +00:00
|
|
|
return !parseAndReturnFirstError(_source, _allowWarningsAndInfos);
|
2018-11-07 11:02:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 12:48:45 +00:00
|
|
|
Error expectError(string const& _source, bool _allowWarningsAndInfos = false)
|
2018-11-07 11:02:11 +00:00
|
|
|
{
|
|
|
|
|
2021-06-30 12:48:45 +00:00
|
|
|
auto error = parseAndReturnFirstError(_source, _allowWarningsAndInfos);
|
2018-11-07 11:02:11 +00:00
|
|
|
BOOST_REQUIRE(error);
|
|
|
|
return *error;
|
|
|
|
}
|
|
|
|
|
2021-07-13 14:06:07 +00:00
|
|
|
tuple<optional<SourceNameMap>, ErrorList> tryGetSourceLocationMapping(string _source)
|
2021-07-12 10:05:26 +00:00
|
|
|
{
|
|
|
|
vector<string> lines;
|
|
|
|
boost::split(lines, _source, boost::is_any_of("\n"));
|
|
|
|
string source = util::joinHumanReadablePrefixed(lines, "\n///") + "\n{}\n";
|
|
|
|
|
|
|
|
ErrorList errors;
|
|
|
|
ErrorReporter reporter(errors);
|
|
|
|
Dialect const& dialect = yul::EVMDialect::strictAssemblyForEVM(EVMVersion::berlin());
|
|
|
|
ObjectParser objectParser{reporter, dialect};
|
2022-08-23 17:28:45 +00:00
|
|
|
CharStream stream(std::move(source), "");
|
2021-08-31 15:22:13 +00:00
|
|
|
auto object = objectParser.parse(make_shared<Scanner>(stream), false);
|
|
|
|
BOOST_REQUIRE(object && object->debugData);
|
|
|
|
return {object->debugData->sourceNames, std::move(errors)};
|
2021-07-12 10:05:26 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 11:02:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#define CHECK_ERROR(text, typ, substring) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
Error err = expectError((text), false); \
|
|
|
|
BOOST_CHECK(err.type() == (Error::Type::typ)); \
|
2019-12-23 15:50:30 +00:00
|
|
|
BOOST_CHECK(::solidity::frontend::test::searchErrorMessage(err, (substring))); \
|
2018-11-07 11:02:11 +00:00
|
|
|
} while(0)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(YulObjectParser)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(empty_code)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(recursion_depth)
|
|
|
|
{
|
|
|
|
string input;
|
|
|
|
for (size_t i = 0; i < 20000; i++)
|
|
|
|
input += "object \"a" + to_string(i) + "\" { code {} ";
|
|
|
|
for (size_t i = 0; i < 20000; i++)
|
|
|
|
input += "}";
|
|
|
|
|
|
|
|
CHECK_ERROR(input, ParserError, "recursion");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(to_string)
|
|
|
|
{
|
|
|
|
string code = R"(
|
|
|
|
object "O" {
|
|
|
|
code { let x := mload(0) if x { sstore(0, 1) } }
|
|
|
|
object "i" { code {} data "j" "def" }
|
|
|
|
data "j" "abc"
|
|
|
|
data "k" hex"010203"
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
string expectation = R"(object "O" {
|
|
|
|
code {
|
|
|
|
let x := mload(0)
|
2019-05-08 10:41:19 +00:00
|
|
|
if x { sstore(0, 1) }
|
2018-11-07 11:02:11 +00:00
|
|
|
}
|
|
|
|
object "i" {
|
2019-05-08 10:41:19 +00:00
|
|
|
code { }
|
2018-11-07 11:02:11 +00:00
|
|
|
data "j" hex"646566"
|
|
|
|
}
|
|
|
|
data "j" hex"616263"
|
|
|
|
data "k" hex"010203"
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
expectation = boost::replace_all_copy(expectation, "\t", " ");
|
2022-03-28 04:27:11 +00:00
|
|
|
YulStack asmStack(
|
2020-01-14 16:48:17 +00:00
|
|
|
solidity::test::CommonOptions::get().evmVersion(),
|
2022-11-22 12:05:20 +00:00
|
|
|
solidity::test::CommonOptions::get().eofVersion(),
|
2022-03-28 04:27:11 +00:00
|
|
|
YulStack::Language::StrictAssembly,
|
2021-09-17 18:15:19 +00:00
|
|
|
solidity::frontend::OptimiserSettings::none(),
|
|
|
|
DebugInfoSelection::All()
|
2018-11-07 11:02:11 +00:00
|
|
|
);
|
|
|
|
BOOST_REQUIRE(asmStack.parseAndAnalyze("source", code));
|
|
|
|
BOOST_CHECK_EQUAL(asmStack.print(), expectation);
|
|
|
|
}
|
|
|
|
|
2021-07-12 10:05:26 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(use_src_empty)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping("");
|
|
|
|
BOOST_REQUIRE(!mapping);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_simple)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping(R"(@use-src 0:"contract.sol")");
|
|
|
|
BOOST_REQUIRE(mapping.has_value());
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 1);
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_multiple)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping(R"(@use-src 0:"contract.sol", 1:"misc.yul")");
|
|
|
|
BOOST_REQUIRE(mapping);
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.yul");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_escaped_filenames)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping(
|
|
|
|
R"(@use-src 42:"con\"tract@\".sol")"
|
|
|
|
);
|
|
|
|
BOOST_REQUIRE(mapping);
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 1);
|
|
|
|
BOOST_REQUIRE(mapping->count(42));
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(42), "con\"tract@\".sol");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_invalid_syntax_malformed_param_1)
|
|
|
|
{
|
|
|
|
// open quote arg, missing closing quote
|
|
|
|
auto const [mapping, errors] = tryGetSourceLocationMapping(R"(@use-src 42_"con")");
|
|
|
|
|
|
|
|
BOOST_REQUIRE_EQUAL(errors.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_invalid_syntax_malformed_param_2)
|
|
|
|
{
|
|
|
|
// open quote arg, missing closing quote
|
|
|
|
auto const [mapping, errors] = tryGetSourceLocationMapping(R"(@use-src 42:"con)");
|
|
|
|
|
|
|
|
BOOST_REQUIRE_EQUAL(errors.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_error_unexpected_trailing_tokens)
|
|
|
|
{
|
|
|
|
auto const [mapping, errors] = tryGetSourceLocationMapping(
|
|
|
|
R"(@use-src 1:"file.sol" @use-src 2:"foo.sol")"
|
|
|
|
);
|
|
|
|
|
|
|
|
BOOST_REQUIRE_EQUAL(errors.size(), 1);
|
|
|
|
BOOST_CHECK_EQUAL(errors.front()->errorId().error, 9804);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_multiline)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping(
|
|
|
|
" @use-src \n 0:\"contract.sol\" \n , \n 1:\"misc.yul\""
|
|
|
|
);
|
|
|
|
BOOST_REQUIRE(mapping);
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.yul");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_empty_body)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping("@use-src");
|
|
|
|
BOOST_REQUIRE(mapping);
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(use_src_leading_text)
|
|
|
|
{
|
|
|
|
auto const [mapping, _] = tryGetSourceLocationMapping(
|
|
|
|
"@something else @use-src 0:\"contract.sol\", 1:\"misc.sol\""s
|
|
|
|
);
|
|
|
|
BOOST_REQUIRE(mapping);
|
|
|
|
BOOST_REQUIRE_EQUAL(mapping->size(), 2);
|
|
|
|
BOOST_REQUIRE(mapping->find(0) != mapping->end());
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(0), "contract.sol");
|
|
|
|
BOOST_REQUIRE_EQUAL(*mapping->at(1), "misc.sol");
|
|
|
|
}
|
|
|
|
|
2018-11-07 11:02:11 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|