2019-02-06 11:10:05 +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 <test/libsolidity/SemanticTest.h>
|
2021-01-08 03:45:19 +00:00
|
|
|
|
2020-05-11 07:26:27 +00:00
|
|
|
#include <libsolutil/Whiskers.h>
|
|
|
|
#include <libyul/Exceptions.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2021-01-08 03:45:19 +00:00
|
|
|
#include <test/libsolidity/util/BytesUtils.h>
|
|
|
|
|
2019-02-06 11:10:05 +00:00
|
|
|
#include <boost/algorithm/string.hpp>
|
|
|
|
#include <boost/algorithm/string/predicate.hpp>
|
|
|
|
#include <boost/algorithm/string/trim.hpp>
|
|
|
|
#include <boost/throw_exception.hpp>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <fstream>
|
2021-01-08 03:45:19 +00:00
|
|
|
#include <functional>
|
2019-02-06 11:10:05 +00:00
|
|
|
#include <memory>
|
2021-01-08 03:45:19 +00:00
|
|
|
#include <optional>
|
2019-02-06 11:10:05 +00:00
|
|
|
#include <stdexcept>
|
2021-10-01 05:48:52 +00:00
|
|
|
#include <string>
|
2021-01-08 03:45:19 +00:00
|
|
|
#include <utility>
|
2019-02-06 11:10:05 +00:00
|
|
|
|
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity;
|
2020-05-11 07:26:27 +00:00
|
|
|
using namespace solidity::yul;
|
2020-11-09 13:26:59 +00:00
|
|
|
using namespace solidity::langutil;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::util;
|
|
|
|
using namespace solidity::util::formatting;
|
|
|
|
using namespace solidity::frontend::test;
|
2019-02-06 11:10:05 +00:00
|
|
|
using namespace boost::algorithm;
|
|
|
|
using namespace boost::unit_test;
|
2019-02-06 11:10:48 +00:00
|
|
|
namespace fs = boost::filesystem;
|
2019-02-06 11:10:05 +00:00
|
|
|
|
2023-08-22 18:00:00 +00:00
|
|
|
ostream& solidity::frontend::test::operator<<(ostream& _output, RequiresYulOptimizer _requiresYulOptimizer)
|
|
|
|
{
|
|
|
|
switch (_requiresYulOptimizer)
|
|
|
|
{
|
|
|
|
case RequiresYulOptimizer::False: _output << "false"; break;
|
|
|
|
case RequiresYulOptimizer::MinimalStack: _output << "minimalStack"; break;
|
|
|
|
case RequiresYulOptimizer::Full: _output << "full"; break;
|
|
|
|
}
|
|
|
|
return _output;
|
|
|
|
}
|
|
|
|
|
2021-01-29 09:55:34 +00:00
|
|
|
SemanticTest::SemanticTest(
|
|
|
|
string const& _filename,
|
|
|
|
langutil::EVMVersion _evmVersion,
|
2022-11-22 12:05:20 +00:00
|
|
|
optional<uint8_t> _eofVersion,
|
2021-01-29 09:55:34 +00:00
|
|
|
vector<boost::filesystem::path> const& _vmPaths,
|
2021-01-29 13:07:22 +00:00
|
|
|
bool _enforceGasCost,
|
|
|
|
u256 _enforceGasCostMinValue
|
2021-01-29 09:55:34 +00:00
|
|
|
):
|
2022-11-22 12:05:20 +00:00
|
|
|
SolidityExecutionFramework(_evmVersion, _eofVersion, _vmPaths, false),
|
2020-04-17 20:24:33 +00:00
|
|
|
EVMVersionRestrictedTestCase(_filename),
|
2020-06-18 10:31:55 +00:00
|
|
|
m_sources(m_reader.sources()),
|
|
|
|
m_lineOffset(m_reader.lineNumber()),
|
2021-05-01 03:13:08 +00:00
|
|
|
m_builtins(makeBuiltins()),
|
2021-04-28 21:35:59 +00:00
|
|
|
m_sideEffectHooks(makeSideEffectHooks()),
|
2021-01-29 13:07:22 +00:00
|
|
|
m_enforceGasCost(_enforceGasCost),
|
2022-08-23 17:28:45 +00:00
|
|
|
m_enforceGasCostMinValue(std::move(_enforceGasCostMinValue))
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2022-05-13 16:41:55 +00:00
|
|
|
static set<string> const compileViaYulAllowedValues{"also", "true", "false"};
|
2020-11-19 21:32:35 +00:00
|
|
|
static set<string> const yulRunTriggers{"also", "true"};
|
|
|
|
static set<string> const legacyRunTriggers{"also", "false", "default"};
|
|
|
|
|
2023-08-22 18:00:00 +00:00
|
|
|
m_requiresYulOptimizer = m_reader.enumSetting<RequiresYulOptimizer>(
|
|
|
|
"requiresYulOptimizer",
|
|
|
|
{
|
|
|
|
{toString(RequiresYulOptimizer::False), RequiresYulOptimizer::False},
|
|
|
|
{toString(RequiresYulOptimizer::MinimalStack), RequiresYulOptimizer::MinimalStack},
|
|
|
|
{toString(RequiresYulOptimizer::Full), RequiresYulOptimizer::Full},
|
|
|
|
},
|
|
|
|
toString(RequiresYulOptimizer::False)
|
|
|
|
);
|
|
|
|
|
2022-05-13 16:41:55 +00:00
|
|
|
m_runWithABIEncoderV1Only = m_reader.boolSetting("ABIEncoderV1Only", false);
|
|
|
|
if (m_runWithABIEncoderV1Only && !solidity::test::CommonOptions::get().useABIEncoderV1)
|
|
|
|
m_shouldRun = false;
|
|
|
|
|
2022-05-19 17:21:19 +00:00
|
|
|
string compileViaYul = m_reader.stringSetting("compileViaYul", "also");
|
2022-05-13 16:41:55 +00:00
|
|
|
if (m_runWithABIEncoderV1Only && compileViaYul != "false")
|
|
|
|
BOOST_THROW_EXCEPTION(runtime_error(
|
|
|
|
"ABIEncoderV1Only tests cannot be run via yul, "
|
|
|
|
"so they need to also specify ``compileViaYul: false``"
|
|
|
|
));
|
2022-03-07 04:25:35 +00:00
|
|
|
if (!util::contains(compileViaYulAllowedValues, compileViaYul))
|
2020-11-19 21:32:35 +00:00
|
|
|
BOOST_THROW_EXCEPTION(runtime_error("Invalid compileViaYul value: " + compileViaYul + "."));
|
2022-03-07 04:25:35 +00:00
|
|
|
m_testCaseWantsYulRun = util::contains(yulRunTriggers, compileViaYul);
|
|
|
|
m_testCaseWantsLegacyRun = util::contains(legacyRunTriggers, compileViaYul);
|
2020-11-19 21:32:35 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
auto revertStrings = revertStringsFromString(m_reader.stringSetting("revertStrings", "default"));
|
|
|
|
soltestAssert(revertStrings, "Invalid revertStrings setting.");
|
|
|
|
m_revertStrings = revertStrings.value();
|
2020-01-22 14:48:56 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
m_allowNonExistingFunctions = m_reader.boolSetting("allowNonExistingFunctions", false);
|
2020-02-12 17:28:30 +00:00
|
|
|
|
2020-03-06 00:22:51 +00:00
|
|
|
parseExpectations(m_reader.stream());
|
2019-08-01 12:02:33 +00:00
|
|
|
soltestAssert(!m_tests.empty(), "No tests specified in " + _filename);
|
2021-02-12 11:33:49 +00:00
|
|
|
|
2021-02-12 11:55:07 +00:00
|
|
|
if (m_enforceGasCost)
|
2021-02-12 11:33:49 +00:00
|
|
|
{
|
2021-02-12 11:55:07 +00:00
|
|
|
m_compiler.setMetadataFormat(CompilerStack::MetadataFormat::NoMetadata);
|
2021-02-12 11:33:49 +00:00
|
|
|
m_compiler.setMetadataHash(CompilerStack::MetadataHash::None);
|
|
|
|
}
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 23:11:29 +00:00
|
|
|
map<string, Builtin> SemanticTest::makeBuiltins()
|
2021-04-23 14:56:19 +00:00
|
|
|
{
|
2021-05-01 03:13:08 +00:00
|
|
|
return {
|
|
|
|
{
|
2021-04-28 21:35:59 +00:00
|
|
|
"isoltest_builtin_test",
|
2021-05-01 03:13:08 +00:00
|
|
|
[](FunctionCall const&) -> optional<bytes>
|
|
|
|
{
|
2021-09-16 14:33:28 +00:00
|
|
|
return toBigEndian(u256(0x1234));
|
2021-05-01 03:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
2021-04-28 21:35:59 +00:00
|
|
|
{
|
|
|
|
"isoltest_side_effects_test",
|
|
|
|
[](FunctionCall const& _call) -> optional<bytes>
|
|
|
|
{
|
|
|
|
if (_call.arguments.parameters.empty())
|
2021-09-16 14:33:28 +00:00
|
|
|
return toBigEndian(0);
|
2021-04-28 21:35:59 +00:00
|
|
|
else
|
|
|
|
return _call.arguments.rawBytes();
|
|
|
|
}
|
|
|
|
},
|
2021-05-01 03:13:08 +00:00
|
|
|
{
|
|
|
|
"balance",
|
|
|
|
[this](FunctionCall const& _call) -> optional<bytes>
|
|
|
|
{
|
|
|
|
soltestAssert(_call.arguments.parameters.size() <= 1, "Account address expected.");
|
|
|
|
h160 address;
|
|
|
|
if (_call.arguments.parameters.size() == 1)
|
|
|
|
address = h160(_call.arguments.parameters.at(0).rawString);
|
|
|
|
else
|
|
|
|
address = m_contractAddress;
|
2021-09-16 14:33:28 +00:00
|
|
|
return toBigEndian(balanceAt(address));
|
2021-05-01 03:13:08 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"storageEmpty",
|
|
|
|
[this](FunctionCall const& _call) -> optional<bytes>
|
|
|
|
{
|
|
|
|
soltestAssert(_call.arguments.parameters.empty(), "No arguments expected.");
|
2021-05-20 23:11:29 +00:00
|
|
|
return toBigEndian(u256(storageEmpty(m_contractAddress) ? 1 : 0));
|
2021-05-01 03:13:08 +00:00
|
|
|
}
|
2021-05-20 23:11:29 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"account",
|
|
|
|
[this](FunctionCall const& _call) -> optional<bytes>
|
|
|
|
{
|
|
|
|
soltestAssert(_call.arguments.parameters.size() == 1, "Account number expected.");
|
|
|
|
size_t accountNumber = static_cast<size_t>(stoi(_call.arguments.parameters.at(0).rawString));
|
|
|
|
// Need to pad it to 32-bytes to workaround limitations in BytesUtils::formatHex.
|
|
|
|
return toBigEndian(h256(ExecutionFramework::setAccount(accountNumber).asBytes(), h256::AlignRight));
|
|
|
|
}
|
|
|
|
},
|
2021-01-08 03:45:19 +00:00
|
|
|
};
|
2021-04-23 14:56:19 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 21:35:59 +00:00
|
|
|
vector<SideEffectHook> SemanticTest::makeSideEffectHooks() const
|
|
|
|
{
|
2021-04-29 23:09:36 +00:00
|
|
|
using namespace std::placeholders;
|
2021-04-28 21:35:59 +00:00
|
|
|
return {
|
|
|
|
[](FunctionCall const& _call) -> vector<string>
|
|
|
|
{
|
|
|
|
if (_call.signature == "isoltest_side_effects_test")
|
|
|
|
{
|
|
|
|
vector<string> result;
|
|
|
|
for (auto const& argument: _call.arguments.parameters)
|
2022-03-07 04:25:35 +00:00
|
|
|
result.emplace_back(util::toHex(argument.rawBytes));
|
2021-04-28 21:35:59 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return {};
|
2021-04-29 23:09:36 +00:00
|
|
|
},
|
|
|
|
bind(&SemanticTest::eventSideEffectHook, this, _1)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
string SemanticTest::formatEventParameter(optional<AnnotatedEventSignature> _signature, bool _indexed, size_t _index, bytes const& _data)
|
|
|
|
{
|
|
|
|
auto isPrintableASCII = [](bytes const& s)
|
|
|
|
{
|
|
|
|
bool zeroes = true;
|
|
|
|
for (auto c: s)
|
|
|
|
{
|
|
|
|
if (static_cast<unsigned>(c) != 0x00)
|
|
|
|
{
|
|
|
|
zeroes = false;
|
|
|
|
if (static_cast<unsigned>(c) <= 0x1f || static_cast<unsigned>(c) >= 0x7f)
|
|
|
|
return false;
|
|
|
|
} else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return !zeroes;
|
|
|
|
};
|
|
|
|
|
|
|
|
ABIType abiType(ABIType::Type::Hex);
|
|
|
|
if (isPrintableASCII(_data))
|
|
|
|
abiType = ABIType(ABIType::Type::String);
|
|
|
|
if (_signature.has_value())
|
|
|
|
{
|
|
|
|
vector<string> const& types = _indexed ? _signature->indexedTypes : _signature->nonIndexedTypes;
|
|
|
|
if (_index < types.size())
|
|
|
|
{
|
|
|
|
if (types.at(_index) == "bool")
|
|
|
|
abiType = ABIType(ABIType::Type::Boolean);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return BytesUtils::formatBytes(_data, abiType);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<string> SemanticTest::eventSideEffectHook(FunctionCall const&) const
|
|
|
|
{
|
|
|
|
vector<string> sideEffects;
|
|
|
|
vector<LogRecord> recordedLogs = ExecutionFramework::recordedLogs();
|
|
|
|
for (LogRecord const& log: recordedLogs)
|
|
|
|
{
|
|
|
|
optional<AnnotatedEventSignature> eventSignature;
|
|
|
|
if (!log.topics.empty())
|
|
|
|
eventSignature = matchEvent(log.topics[0]);
|
|
|
|
stringstream sideEffect;
|
|
|
|
sideEffect << "emit ";
|
|
|
|
if (eventSignature.has_value())
|
|
|
|
sideEffect << eventSignature.value().signature;
|
|
|
|
else
|
|
|
|
sideEffect << "<anonymous>";
|
|
|
|
|
|
|
|
if (m_contractAddress != log.creator)
|
|
|
|
sideEffect << " from 0x" << log.creator;
|
|
|
|
|
|
|
|
vector<string> eventStrings;
|
|
|
|
size_t index{0};
|
|
|
|
for (h256 const& topic: log.topics)
|
|
|
|
{
|
|
|
|
if (!eventSignature.has_value() || index != 0)
|
|
|
|
eventStrings.push_back("#" + formatEventParameter(eventSignature, true, index, topic.asBytes()));
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
|
|
|
|
soltestAssert(log.data.size() % 32 == 0, "");
|
|
|
|
for (size_t index = 0; index < log.data.size() / 32; ++index)
|
|
|
|
{
|
|
|
|
auto begin = log.data.begin() + static_cast<long>(index * 32);
|
|
|
|
bytes const& data = bytes{begin, begin + 32};
|
|
|
|
eventStrings.emplace_back(formatEventParameter(eventSignature, false, index, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!eventStrings.empty())
|
|
|
|
sideEffect << ": ";
|
|
|
|
sideEffect << joinHumanReadable(eventStrings);
|
|
|
|
sideEffects.emplace_back(sideEffect.str());
|
|
|
|
}
|
|
|
|
return sideEffects;
|
|
|
|
}
|
|
|
|
|
|
|
|
optional<AnnotatedEventSignature> SemanticTest::matchEvent(util::h256 const& hash) const
|
|
|
|
{
|
|
|
|
optional<AnnotatedEventSignature> result;
|
|
|
|
for (string& contractName: m_compiler.contractNames())
|
|
|
|
{
|
|
|
|
ContractDefinition const& contract = m_compiler.contractDefinition(contractName);
|
|
|
|
for (EventDefinition const* event: contract.events())
|
|
|
|
{
|
|
|
|
FunctionTypePointer eventFunctionType = event->functionType(true);
|
|
|
|
if (!event->isAnonymous() && keccak256(eventFunctionType->externalSignature()) == hash)
|
|
|
|
{
|
|
|
|
AnnotatedEventSignature eventInfo;
|
|
|
|
eventInfo.signature = eventFunctionType->externalSignature();
|
|
|
|
for (auto const& param: event->parameters())
|
|
|
|
if (param->isIndexed())
|
|
|
|
eventInfo.indexedTypes.emplace_back(param->type()->toString(true));
|
|
|
|
else
|
|
|
|
eventInfo.nonIndexedTypes.emplace_back(param->type()->toString(true));
|
|
|
|
result = eventInfo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2021-04-28 21:35:59 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:00:00 +00:00
|
|
|
frontend::OptimiserSettings SemanticTest::optimizerSettingsFor(RequiresYulOptimizer _requiresYulOptimizer)
|
|
|
|
{
|
2023-08-22 18:19:20 +00:00
|
|
|
switch (_requiresYulOptimizer)
|
|
|
|
{
|
2023-08-22 18:00:00 +00:00
|
|
|
case RequiresYulOptimizer::False:
|
|
|
|
return OptimiserSettings::minimal();
|
|
|
|
case RequiresYulOptimizer::MinimalStack:
|
|
|
|
{
|
|
|
|
OptimiserSettings settings = OptimiserSettings::minimal();
|
|
|
|
settings.runYulOptimiser = true;
|
|
|
|
settings.yulOptimiserSteps = "uljmul jmul";
|
|
|
|
return settings;
|
|
|
|
}
|
|
|
|
case RequiresYulOptimizer::Full:
|
|
|
|
return OptimiserSettings::full();
|
|
|
|
}
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
TestCase::TestResult SemanticTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2020-06-05 23:10:48 +00:00
|
|
|
TestResult result = TestResult::Success;
|
|
|
|
|
2022-11-22 12:05:20 +00:00
|
|
|
if (m_testCaseWantsLegacyRun && !m_eofVersion.has_value())
|
2023-08-22 18:00:00 +00:00
|
|
|
result = runTest(_stream, _linePrefix, _formatted, false /* _isYulRun */);
|
2019-05-15 10:01:14 +00:00
|
|
|
|
2022-05-13 16:41:55 +00:00
|
|
|
if (m_testCaseWantsYulRun && result == TestResult::Success)
|
2023-08-22 18:00:00 +00:00
|
|
|
{
|
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
|
|
|
result = runTest(_stream, _linePrefix, _formatted, true /* _isYulRun */);
|
|
|
|
else
|
|
|
|
result = tryRunTestWithYulOptimizer(_stream, _linePrefix, _formatted);
|
|
|
|
}
|
2022-05-27 17:55:41 +00:00
|
|
|
|
|
|
|
if (result != TestResult::Success)
|
|
|
|
solidity::test::CommonOptions::get().printSelectedOptions(
|
|
|
|
_stream,
|
|
|
|
_linePrefix,
|
|
|
|
{"evmVersion", "optimize", "useABIEncoderV1", "batch"}
|
|
|
|
);
|
|
|
|
|
2020-06-05 23:10:48 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-08 03:45:19 +00:00
|
|
|
TestCase::TestResult SemanticTest::runTest(
|
|
|
|
ostream& _stream,
|
|
|
|
string const& _linePrefix,
|
|
|
|
bool _formatted,
|
2023-08-22 18:00:00 +00:00
|
|
|
bool _isYulRun
|
|
|
|
)
|
2020-06-05 23:10:48 +00:00
|
|
|
{
|
2020-11-09 13:26:59 +00:00
|
|
|
bool success = true;
|
2021-03-09 20:26:36 +00:00
|
|
|
m_gasCostFailure = false;
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2023-02-22 15:32:29 +00:00
|
|
|
selectVM(evmc_capabilities::EVMC_CAPABILITY_EVM1);
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2020-11-09 13:26:59 +00:00
|
|
|
reset();
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2020-11-19 21:32:35 +00:00
|
|
|
m_compileViaYul = _isYulRun;
|
2019-02-06 11:10:05 +00:00
|
|
|
|
2020-11-19 21:32:35 +00:00
|
|
|
if (_isYulRun)
|
2023-02-22 15:32:29 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Running via Yul: " << endl;
|
2019-11-12 13:13:57 +00:00
|
|
|
|
2021-01-08 03:45:19 +00:00
|
|
|
for (TestFunctionCall& test: m_tests)
|
2020-11-09 13:26:59 +00:00
|
|
|
test.reset();
|
2019-11-12 13:13:57 +00:00
|
|
|
|
2020-11-09 13:26:59 +00:00
|
|
|
map<string, solidity::test::Address> libraries;
|
2019-05-15 10:01:14 +00:00
|
|
|
|
2020-11-09 13:26:59 +00:00
|
|
|
bool constructed = false;
|
2019-05-15 10:24:23 +00:00
|
|
|
|
2021-01-08 03:45:19 +00:00
|
|
|
for (TestFunctionCall& test: m_tests)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
2021-01-14 15:21:14 +00:00
|
|
|
if (constructed)
|
2020-06-05 23:10:48 +00:00
|
|
|
{
|
2021-01-08 03:45:19 +00:00
|
|
|
soltestAssert(
|
|
|
|
test.call().kind != FunctionCall::Kind::Library,
|
|
|
|
"Libraries have to be deployed before any other call."
|
|
|
|
);
|
2021-01-14 15:21:14 +00:00
|
|
|
soltestAssert(
|
|
|
|
test.call().kind != FunctionCall::Kind::Constructor,
|
2021-01-08 03:45:19 +00:00
|
|
|
"Constructor has to be the first function call expect for library deployments."
|
|
|
|
);
|
2020-11-09 13:26:59 +00:00
|
|
|
}
|
2021-01-14 15:21:14 +00:00
|
|
|
else if (test.call().kind == FunctionCall::Kind::Library)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
2021-01-14 15:21:14 +00:00
|
|
|
soltestAssert(
|
|
|
|
deploy(test.call().signature, 0, {}, libraries) && m_transactionSuccessful,
|
|
|
|
"Failed to deploy library " + test.call().signature);
|
2021-10-01 05:48:52 +00:00
|
|
|
// For convenience, in semantic tests we assume that an unqualified name like `L` is equivalent to one
|
|
|
|
// with an empty source unit name (`:L`). This is fine because the compiler never uses unqualified
|
|
|
|
// names in the Yul code it produces and does not allow `linkersymbol()` at all in inline assembly.
|
2022-06-17 14:02:05 +00:00
|
|
|
libraries[test.call().libraryFile + ":" + test.call().signature] = m_contractAddress;
|
2021-01-14 15:21:14 +00:00
|
|
|
continue;
|
2020-11-09 13:26:59 +00:00
|
|
|
}
|
2021-01-14 15:21:14 +00:00
|
|
|
else
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
2021-01-14 15:21:14 +00:00
|
|
|
if (test.call().kind == FunctionCall::Kind::Constructor)
|
|
|
|
deploy("", test.call().value.value, test.call().arguments.rawBytes(), libraries);
|
|
|
|
else
|
|
|
|
soltestAssert(deploy("", 0, bytes(), libraries), "Failed to deploy contract.");
|
|
|
|
constructed = true;
|
2020-04-17 20:24:33 +00:00
|
|
|
}
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2021-01-08 03:45:19 +00:00
|
|
|
if (test.call().kind == FunctionCall::Kind::Constructor)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
|
|
|
if (m_transactionSuccessful == test.call().expectations.failure)
|
|
|
|
success = false;
|
2020-11-19 21:32:35 +00:00
|
|
|
if (success && !checkGasCostExpectation(test, _isYulRun))
|
2021-11-15 13:29:22 +00:00
|
|
|
{
|
|
|
|
success = false;
|
2021-03-09 20:26:36 +00:00
|
|
|
m_gasCostFailure = true;
|
2021-11-15 13:29:22 +00:00
|
|
|
}
|
2020-06-05 23:10:48 +00:00
|
|
|
|
2020-11-09 13:26:59 +00:00
|
|
|
test.setFailure(!m_transactionSuccessful);
|
|
|
|
test.setRawBytes(bytes());
|
|
|
|
}
|
|
|
|
else
|
2020-05-11 07:26:27 +00:00
|
|
|
{
|
2020-11-09 13:26:59 +00:00
|
|
|
bytes output;
|
|
|
|
if (test.call().kind == FunctionCall::Kind::LowLevel)
|
|
|
|
output = callLowLevel(test.call().arguments.rawBytes(), test.call().value.value);
|
2021-01-08 03:45:19 +00:00
|
|
|
else if (test.call().kind == FunctionCall::Kind::Builtin)
|
|
|
|
{
|
2021-05-01 03:13:08 +00:00
|
|
|
optional<bytes> builtinOutput = m_builtins.at(test.call().signature)(test.call());
|
2021-01-08 03:45:19 +00:00
|
|
|
if (builtinOutput.has_value())
|
|
|
|
{
|
|
|
|
m_transactionSuccessful = true;
|
|
|
|
output = builtinOutput.value();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
m_transactionSuccessful = false;
|
|
|
|
}
|
2020-11-09 13:26:59 +00:00
|
|
|
else
|
2020-06-05 23:10:48 +00:00
|
|
|
{
|
2020-11-09 13:26:59 +00:00
|
|
|
soltestAssert(
|
|
|
|
m_allowNonExistingFunctions ||
|
2022-02-01 14:42:08 +00:00
|
|
|
m_compiler.interfaceSymbols(m_compiler.lastContractName(m_sources.mainSourceFile))["methods"].isMember(test.call().signature),
|
2020-11-09 13:26:59 +00:00
|
|
|
"The function " + test.call().signature + " is not known to the compiler"
|
|
|
|
);
|
|
|
|
|
|
|
|
output = callContractFunctionWithValueNoEncoding(
|
|
|
|
test.call().signature,
|
|
|
|
test.call().value.value,
|
|
|
|
test.call().arguments.rawBytes()
|
|
|
|
);
|
2020-06-05 23:10:48 +00:00
|
|
|
}
|
2020-11-09 13:26:59 +00:00
|
|
|
|
|
|
|
bool outputMismatch = (output != test.call().expectations.rawBytes());
|
2020-11-19 21:32:35 +00:00
|
|
|
if (!outputMismatch && !checkGasCostExpectation(test, _isYulRun))
|
2021-03-09 20:27:38 +00:00
|
|
|
{
|
|
|
|
success = false;
|
2021-03-09 20:26:36 +00:00
|
|
|
m_gasCostFailure = true;
|
2021-03-09 20:27:38 +00:00
|
|
|
}
|
2021-03-09 20:26:36 +00:00
|
|
|
|
2020-11-09 13:26:59 +00:00
|
|
|
// Pre byzantium, it was not possible to return failure data, so we disregard
|
|
|
|
// output mismatch for those EVM versions.
|
|
|
|
if (test.call().expectations.failure && !m_transactionSuccessful && !m_evmVersion.supportsReturndata())
|
|
|
|
outputMismatch = false;
|
|
|
|
if (m_transactionSuccessful != !test.call().expectations.failure || outputMismatch)
|
|
|
|
success = false;
|
|
|
|
|
|
|
|
test.setFailure(!m_transactionSuccessful);
|
2022-08-23 17:28:45 +00:00
|
|
|
test.setRawBytes(std::move(output));
|
2023-06-07 11:40:14 +00:00
|
|
|
if (test.call().kind != FunctionCall::Kind::LowLevel)
|
|
|
|
test.setContractABI(m_compiler.contractABI(m_compiler.lastContractName(m_sources.mainSourceFile)));
|
2020-05-11 07:26:27 +00:00
|
|
|
}
|
2021-04-28 21:35:59 +00:00
|
|
|
|
|
|
|
vector<string> effects;
|
|
|
|
for (SideEffectHook const& hook: m_sideEffectHooks)
|
|
|
|
effects += hook(test.call());
|
2022-08-23 17:28:45 +00:00
|
|
|
test.setSideEffects(std::move(effects));
|
2021-04-28 21:35:59 +00:00
|
|
|
|
|
|
|
success &= test.call().expectedSideEffects == test.call().actualSideEffects;
|
2020-06-05 23:10:48 +00:00
|
|
|
}
|
2020-11-09 13:26:59 +00:00
|
|
|
|
2020-11-19 21:32:35 +00:00
|
|
|
if (!success)
|
|
|
|
{
|
2020-11-09 13:26:59 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Expected result:" << endl;
|
2021-01-08 03:45:19 +00:00
|
|
|
for (TestFunctionCall const& test: m_tests)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
|
|
|
ErrorReporter errorReporter;
|
2021-02-04 18:18:09 +00:00
|
|
|
_stream << test.format(
|
|
|
|
errorReporter,
|
|
|
|
_linePrefix,
|
|
|
|
TestFunctionCall::RenderMode::ExpectedValuesExpectedGas,
|
|
|
|
_formatted,
|
|
|
|
/* _interactivePrint */ true
|
|
|
|
) << endl;
|
2020-11-09 13:26:59 +00:00
|
|
|
_stream << errorReporter.format(_linePrefix, _formatted);
|
|
|
|
}
|
|
|
|
_stream << endl;
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, CYAN}) << _linePrefix << "Obtained result:" << endl;
|
2021-01-08 03:45:19 +00:00
|
|
|
for (TestFunctionCall const& test: m_tests)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
|
|
|
ErrorReporter errorReporter;
|
2021-02-04 18:18:09 +00:00
|
|
|
_stream << test.format(
|
|
|
|
errorReporter,
|
|
|
|
_linePrefix,
|
|
|
|
m_gasCostFailure ? TestFunctionCall::RenderMode::ExpectedValuesActualGas : TestFunctionCall::RenderMode::ActualValuesExpectedGas,
|
|
|
|
_formatted,
|
|
|
|
/* _interactivePrint */ true
|
|
|
|
) << endl;
|
2020-11-09 13:26:59 +00:00
|
|
|
_stream << errorReporter.format(_linePrefix, _formatted);
|
|
|
|
}
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, RED})
|
|
|
|
<< _linePrefix << endl
|
|
|
|
<< _linePrefix << "Attention: Updates on the test will apply the detected format displayed." << endl;
|
2020-11-19 21:32:35 +00:00
|
|
|
if (_isYulRun && m_testCaseWantsLegacyRun)
|
2020-11-09 13:26:59 +00:00
|
|
|
{
|
|
|
|
_stream << _linePrefix << endl << _linePrefix;
|
|
|
|
AnsiColorized(_stream, _formatted, {RED_BACKGROUND}) << "Note that the test passed without Yul.";
|
|
|
|
_stream << endl;
|
|
|
|
}
|
2020-11-19 21:32:35 +00:00
|
|
|
else if (!_isYulRun && m_testCaseWantsYulRun)
|
2020-11-09 13:26:59 +00:00
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, YELLOW})
|
|
|
|
<< _linePrefix << endl
|
|
|
|
<< _linePrefix << "Note that the test also has to pass via Yul." << endl;
|
|
|
|
return TestResult::Failure;
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
2019-06-06 16:02:48 +00:00
|
|
|
|
2019-05-07 13:33:22 +00:00
|
|
|
return TestResult::Success;
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-22 18:00:00 +00:00
|
|
|
TestCase::TestResult SemanticTest::tryRunTestWithYulOptimizer(
|
|
|
|
std::ostream& _stream,
|
|
|
|
std::string const& _linePrefix,
|
|
|
|
bool _formatted
|
|
|
|
)
|
|
|
|
{
|
|
|
|
TestResult result{};
|
|
|
|
for (auto requiresYulOptimizer: {
|
|
|
|
RequiresYulOptimizer::False,
|
|
|
|
RequiresYulOptimizer::MinimalStack,
|
|
|
|
RequiresYulOptimizer::Full,
|
|
|
|
})
|
|
|
|
{
|
|
|
|
ScopedSaveAndRestore optimizerSettings(
|
|
|
|
m_optimiserSettings,
|
|
|
|
optimizerSettingsFor(requiresYulOptimizer)
|
|
|
|
);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = runTest(_stream, _linePrefix, _formatted, true /* _isYulRun */);
|
|
|
|
}
|
|
|
|
catch (yul::StackTooDeepError const&)
|
|
|
|
{
|
|
|
|
if (requiresYulOptimizer == RequiresYulOptimizer::Full)
|
|
|
|
throw;
|
|
|
|
else
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (m_requiresYulOptimizer != requiresYulOptimizer && result != TestResult::FatalError)
|
|
|
|
{
|
|
|
|
soltestAssert(result == TestResult::Success || result == TestResult::Failure);
|
|
|
|
|
|
|
|
AnsiColorized(_stream, _formatted, {BOLD, YELLOW})
|
|
|
|
<< _linePrefix << endl
|
|
|
|
<< _linePrefix << "requiresYulOptimizer is set to " << m_requiresYulOptimizer
|
|
|
|
<< " but should be " << requiresYulOptimizer << endl;
|
|
|
|
m_requiresYulOptimizer = requiresYulOptimizer;
|
|
|
|
return TestResult::Failure;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
unreachable();
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:26:36 +00:00
|
|
|
bool SemanticTest::checkGasCostExpectation(TestFunctionCall& io_test, bool _compileViaYul) const
|
|
|
|
{
|
|
|
|
string setting =
|
|
|
|
(_compileViaYul ? "ir"s : "legacy"s) +
|
|
|
|
(m_optimiserSettings == OptimiserSettings::full() ? "Optimized" : "");
|
2021-01-29 09:40:03 +00:00
|
|
|
|
2021-02-12 11:55:07 +00:00
|
|
|
// We don't check gas if enforce gas cost is not active
|
|
|
|
// or test is run with abi encoder v1 only
|
2021-02-04 16:53:27 +00:00
|
|
|
// or gas used less than threshold for enforcing feature
|
2021-11-15 13:36:13 +00:00
|
|
|
// or the test has used up all available gas (test will fail anyway)
|
2021-02-04 16:53:27 +00:00
|
|
|
// or setting is "ir" and it's not included in expectations
|
2021-04-27 21:22:36 +00:00
|
|
|
// or if the called function is an isoltest builtin e.g. `smokeTest` or `storageEmpty`
|
2021-01-29 13:07:22 +00:00
|
|
|
if (
|
2021-02-12 11:55:07 +00:00
|
|
|
!m_enforceGasCost ||
|
2021-11-15 13:36:13 +00:00
|
|
|
m_gasUsed < m_enforceGasCostMinValue ||
|
2021-11-15 13:42:10 +00:00
|
|
|
m_gasUsed >= InitialGas ||
|
2021-11-15 13:36:13 +00:00
|
|
|
(setting == "ir" && io_test.call().expectations.gasUsed.count(setting) == 0) ||
|
2021-04-27 21:22:36 +00:00
|
|
|
io_test.call().kind == FunctionCall::Kind::Builtin
|
2021-01-29 13:07:22 +00:00
|
|
|
)
|
2021-01-29 09:40:03 +00:00
|
|
|
return true;
|
|
|
|
|
2021-02-12 11:55:07 +00:00
|
|
|
solAssert(!m_runWithABIEncoderV1Only, "");
|
|
|
|
|
2021-03-09 20:26:36 +00:00
|
|
|
io_test.setGasCost(setting, m_gasUsed);
|
2021-01-29 13:07:22 +00:00
|
|
|
return
|
|
|
|
io_test.call().expectations.gasUsed.count(setting) > 0 &&
|
|
|
|
m_gasUsed == io_test.call().expectations.gasUsed.at(setting);
|
2021-03-09 20:26:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-18 10:31:55 +00:00
|
|
|
void SemanticTest::printSource(ostream& _stream, string const& _linePrefix, bool _formatted) const
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2020-06-18 10:31:55 +00:00
|
|
|
if (m_sources.sources.empty())
|
|
|
|
return;
|
|
|
|
|
2021-03-15 22:52:24 +00:00
|
|
|
bool outputNames = (m_sources.sources.size() - m_sources.externalSources.size() != 1 || !m_sources.sources.begin()->first.empty());
|
|
|
|
|
|
|
|
set<string> externals;
|
|
|
|
for (auto const& [name, path]: m_sources.externalSources)
|
|
|
|
{
|
|
|
|
externals.insert(name);
|
|
|
|
string externalSource;
|
|
|
|
if (name == path)
|
|
|
|
externalSource = name;
|
|
|
|
else
|
|
|
|
externalSource = name + "=" + path.generic_string();
|
2020-06-18 10:31:55 +00:00
|
|
|
|
|
|
|
if (_formatted)
|
2021-03-15 22:52:24 +00:00
|
|
|
_stream << _linePrefix << formatting::CYAN << "==== ExternalSource: " << externalSource << " ===="s << formatting::RESET << endl;
|
|
|
|
else
|
|
|
|
_stream << _linePrefix << "==== ExternalSource: " << externalSource << " ===="s << endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto const& [name, source]: m_sources.sources)
|
|
|
|
if (externals.find(name) == externals.end())
|
2020-06-18 10:31:55 +00:00
|
|
|
{
|
2021-03-15 22:52:24 +00:00
|
|
|
if (_formatted)
|
|
|
|
{
|
|
|
|
if (source.empty())
|
|
|
|
continue;
|
2020-06-18 10:31:55 +00:00
|
|
|
|
2021-03-15 22:52:24 +00:00
|
|
|
if (outputNames)
|
|
|
|
_stream << _linePrefix << formatting::CYAN << "==== Source: " << name
|
|
|
|
<< " ====" << formatting::RESET << endl;
|
2020-06-18 10:31:55 +00:00
|
|
|
|
2021-03-15 22:52:24 +00:00
|
|
|
vector<char const*> sourceFormatting(source.length(), formatting::RESET);
|
|
|
|
_stream << _linePrefix << sourceFormatting.front() << source.front();
|
|
|
|
for (size_t i = 1; i < source.length(); i++)
|
2020-06-18 10:31:55 +00:00
|
|
|
{
|
2021-03-15 22:52:24 +00:00
|
|
|
if (sourceFormatting[i] != sourceFormatting[i - 1])
|
|
|
|
_stream << sourceFormatting[i];
|
|
|
|
if (source[i] != '\n')
|
|
|
|
_stream << source[i];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_stream << formatting::RESET << endl;
|
|
|
|
if (i + 1 < source.length())
|
|
|
|
_stream << _linePrefix << sourceFormatting[i];
|
|
|
|
}
|
2020-06-18 10:31:55 +00:00
|
|
|
}
|
2021-03-15 22:52:24 +00:00
|
|
|
_stream << formatting::RESET;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (outputNames)
|
|
|
|
_stream << _linePrefix << "==== Source: " + name << " ====" << endl;
|
|
|
|
stringstream stream(source);
|
|
|
|
string line;
|
|
|
|
while (getline(stream, line))
|
|
|
|
_stream << _linePrefix << line << endl;
|
2020-06-18 10:31:55 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 11:10:48 +00:00
|
|
|
void SemanticTest::printUpdatedExpectations(ostream& _stream, string const&) const
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2021-01-08 03:45:19 +00:00
|
|
|
for (TestFunctionCall const& test: m_tests)
|
2021-03-09 20:27:38 +00:00
|
|
|
_stream << test.format(
|
|
|
|
"",
|
2021-02-04 18:18:09 +00:00
|
|
|
m_gasCostFailure ? TestFunctionCall::RenderMode::ExpectedValuesActualGas : TestFunctionCall::RenderMode::ActualValuesExpectedGas,
|
|
|
|
/* _highlight = */ false
|
2021-03-09 20:27:38 +00:00
|
|
|
) << endl;
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 20:24:33 +00:00
|
|
|
void SemanticTest::printUpdatedSettings(ostream& _stream, string const& _linePrefix)
|
|
|
|
{
|
|
|
|
auto& settings = m_reader.settings();
|
2023-08-22 18:32:12 +00:00
|
|
|
if (settings.empty() && m_requiresYulOptimizer == RequiresYulOptimizer::False)
|
2020-04-17 20:24:33 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
_stream << _linePrefix << "// ====" << endl;
|
2023-08-22 18:32:12 +00:00
|
|
|
if (m_requiresYulOptimizer != RequiresYulOptimizer::False)
|
|
|
|
_stream << _linePrefix << "// requiresYulOptimizer: " << m_requiresYulOptimizer << endl;
|
|
|
|
|
2020-11-19 21:32:35 +00:00
|
|
|
for (auto const& [settingName, settingValue]: settings)
|
2023-08-22 18:32:12 +00:00
|
|
|
if (settingName != "requiresYulOptimizer")
|
2023-08-22 18:00:00 +00:00
|
|
|
_stream << _linePrefix << "// " << settingName << ": " << settingValue<< endl;
|
2020-04-17 20:24:33 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 11:10:05 +00:00
|
|
|
void SemanticTest::parseExpectations(istream& _stream)
|
|
|
|
{
|
2021-04-28 21:35:59 +00:00
|
|
|
m_tests += TestFileParser{_stream, m_builtins}.parseFunctionCalls(m_lineOffset);
|
2019-02-06 11:10:05 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 03:45:19 +00:00
|
|
|
bool SemanticTest::deploy(
|
|
|
|
string const& _contractName,
|
|
|
|
u256 const& _value,
|
|
|
|
bytes const& _arguments,
|
|
|
|
map<string, solidity::test::Address> const& _libraries
|
|
|
|
)
|
2019-02-06 11:10:05 +00:00
|
|
|
{
|
2021-03-15 22:52:24 +00:00
|
|
|
auto output = compileAndRunWithoutCheck(m_sources.sources, _value, _contractName, _arguments, _libraries, m_sources.mainSourceFile);
|
2019-02-06 11:10:05 +00:00
|
|
|
return !output.empty() && m_transactionSuccessful;
|
|
|
|
}
|