2014-10-30 00:20:32 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2014-10-30 00:20:32 +00:00
|
|
|
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.
|
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is distributed in the hope that it will be useful,
|
2014-10-30 00:20:32 +00:00
|
|
|
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
|
2016-11-18 23:13:20 +00:00
|
|
|
along with solidity. If not, see <http://www.gnu.org/licenses/>.
|
2014-10-30 00:20:32 +00:00
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2014-10-30 00:20:32 +00:00
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2014
|
|
|
|
* Unit tests for the solidity expression compiler.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/Scanner.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/parsing/Parser.h>
|
|
|
|
#include <libsolidity/analysis/NameAndTypeResolver.h>
|
2020-08-06 12:46:04 +00:00
|
|
|
#include <libsolidity/analysis/Scoper.h>
|
2020-04-07 17:31:48 +00:00
|
|
|
#include <libsolidity/analysis/DeclarationTypeChecker.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/codegen/CompilerContext.h>
|
|
|
|
#include <libsolidity/codegen/ExpressionCompiler.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
2019-04-15 13:33:39 +00:00
|
|
|
#include <libsolidity/ast/TypeProvider.h>
|
2015-10-20 22:21:52 +00:00
|
|
|
#include <libsolidity/analysis/TypeChecker.h>
|
2018-11-14 13:59:30 +00:00
|
|
|
#include <liblangutil/ErrorReporter.h>
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
using namespace std;
|
2019-12-23 15:50:30 +00:00
|
|
|
using namespace solidity::evmasm;
|
|
|
|
using namespace solidity::langutil;
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::frontend::test
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
/// Helper class that extracts the first expression in an AST.
|
|
|
|
class FirstExpressionExtractor: private ASTVisitor
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
FirstExpressionExtractor(ASTNode& _node): m_expression(nullptr) { _node.accept(*this); }
|
2015-09-01 09:19:02 +00:00
|
|
|
Expression* expression() const { return m_expression; }
|
2014-10-30 00:20:32 +00:00
|
|
|
private:
|
2020-03-31 23:58:22 +00:00
|
|
|
bool visit(Assignment& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(UnaryOperation& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(BinaryOperation& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(FunctionCall& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(MemberAccess& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(IndexAccess& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(Identifier& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(ElementaryTypeNameExpression& _expression) override { return checkExpression(_expression); }
|
|
|
|
bool visit(Literal& _expression) override { return checkExpression(_expression); }
|
2014-10-30 00:20:32 +00:00
|
|
|
bool checkExpression(Expression& _expression)
|
|
|
|
{
|
|
|
|
if (m_expression == nullptr)
|
|
|
|
m_expression = &_expression;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
Expression* m_expression;
|
|
|
|
};
|
|
|
|
|
2015-03-05 23:20:20 +00:00
|
|
|
Declaration const& resolveDeclaration(
|
2015-12-05 02:09:47 +00:00
|
|
|
SourceUnit const& _sourceUnit,
|
|
|
|
vector<string> const& _namespacedName,
|
|
|
|
NameAndTypeResolver const& _resolver
|
|
|
|
)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2015-12-05 02:09:47 +00:00
|
|
|
ASTNode const* scope = &_sourceUnit;
|
2018-07-10 07:18:19 +00:00
|
|
|
// bracers are required, cause msvc couldn't handle this macro in for statement
|
2014-10-30 00:20:32 +00:00
|
|
|
for (string const& namePart: _namespacedName)
|
2014-12-08 23:58:02 +00:00
|
|
|
{
|
2015-12-05 02:09:47 +00:00
|
|
|
auto declarations = _resolver.resolveName(namePart, scope);
|
2015-02-28 08:20:11 +00:00
|
|
|
BOOST_REQUIRE(!declarations.empty());
|
2015-12-05 02:09:47 +00:00
|
|
|
BOOST_REQUIRE(scope = *declarations.begin());
|
2014-12-08 23:58:02 +00:00
|
|
|
}
|
2015-12-05 02:09:47 +00:00
|
|
|
BOOST_REQUIRE(scope);
|
|
|
|
return dynamic_cast<Declaration const&>(*scope);
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
2015-10-02 12:41:40 +00:00
|
|
|
bytes compileFirstExpression(
|
|
|
|
const string& _sourceCode,
|
|
|
|
vector<vector<string>> _functions = {},
|
2019-04-29 16:29:40 +00:00
|
|
|
vector<vector<string>> _localVariables = {}
|
2015-10-02 12:41:40 +00:00
|
|
|
)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2014-12-03 06:46:55 +00:00
|
|
|
ASTPointer<SourceUnit> sourceUnit;
|
2015-02-04 21:02:35 +00:00
|
|
|
try
|
|
|
|
{
|
2015-10-14 18:37:41 +00:00
|
|
|
ErrorList errors;
|
2017-05-11 13:26:35 +00:00
|
|
|
ErrorReporter errorReporter(errors);
|
2020-01-14 16:48:17 +00:00
|
|
|
sourceUnit = Parser(errorReporter, solidity::test::CommonOptions::get().evmVersion()).parse(
|
2019-05-22 11:57:48 +00:00
|
|
|
make_shared<Scanner>(CharStream(_sourceCode, ""))
|
|
|
|
);
|
2015-10-14 18:37:41 +00:00
|
|
|
if (!sourceUnit)
|
|
|
|
return bytes();
|
2015-02-04 21:02:35 +00:00
|
|
|
}
|
|
|
|
catch(boost::exception const& _e)
|
|
|
|
{
|
|
|
|
auto msg = std::string("Parsing source code failed with: \n") + boost::diagnostic_information(_e);
|
|
|
|
BOOST_FAIL(msg);
|
|
|
|
}
|
2015-01-19 16:00:23 +00:00
|
|
|
|
2015-10-14 18:37:41 +00:00
|
|
|
ErrorList errors;
|
2017-05-11 13:26:35 +00:00
|
|
|
ErrorReporter errorReporter(errors);
|
2019-04-29 16:29:40 +00:00
|
|
|
GlobalContext globalContext;
|
2020-08-06 12:46:04 +00:00
|
|
|
Scoper::assignScopes(*sourceUnit);
|
2020-05-14 09:56:10 +00:00
|
|
|
NameAndTypeResolver resolver(globalContext, solidity::test::CommonOptions::get().evmVersion(), errorReporter);
|
2014-12-03 16:45:12 +00:00
|
|
|
resolver.registerDeclarations(*sourceUnit);
|
2020-06-11 15:17:07 +00:00
|
|
|
BOOST_REQUIRE_MESSAGE(resolver.resolveNamesAndTypes(*sourceUnit), "Resolving names failed");
|
2020-04-07 17:31:48 +00:00
|
|
|
DeclarationTypeChecker declarationTypeChecker(errorReporter, solidity::test::CommonOptions::get().evmVersion());
|
|
|
|
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
|
|
|
|
BOOST_REQUIRE(declarationTypeChecker.check(*node));
|
2020-06-11 15:17:07 +00:00
|
|
|
TypeChecker typeChecker(solidity::test::CommonOptions::get().evmVersion(), errorReporter);
|
|
|
|
BOOST_REQUIRE(typeChecker.checkTypeRequirements(*sourceUnit));
|
2015-08-31 16:44:29 +00:00
|
|
|
for (ASTPointer<ASTNode> const& node: sourceUnit->nodes())
|
2014-12-16 22:45:24 +00:00
|
|
|
if (ContractDefinition* contract = dynamic_cast<ContractDefinition*>(node.get()))
|
|
|
|
{
|
2014-12-03 06:46:55 +00:00
|
|
|
FirstExpressionExtractor extractor(*contract);
|
2015-09-01 09:19:02 +00:00
|
|
|
BOOST_REQUIRE(extractor.expression() != nullptr);
|
2014-12-03 06:46:55 +00:00
|
|
|
|
2020-01-22 14:48:56 +00:00
|
|
|
CompilerContext context(
|
|
|
|
solidity::test::CommonOptions::get().evmVersion(),
|
|
|
|
RevertStrings::Default
|
|
|
|
);
|
2015-02-24 11:08:51 +00:00
|
|
|
context.resetVisitedNodes(contract);
|
2020-03-24 17:25:59 +00:00
|
|
|
context.setMostDerivedContract(*contract);
|
2020-07-22 08:28:04 +00:00
|
|
|
context.setArithmetic(Arithmetic::Wrapping);
|
2020-06-02 13:42:46 +00:00
|
|
|
size_t parametersSize = _localVariables.size(); // assume they are all one slot on the stack
|
|
|
|
context.adjustStackOffset(static_cast<int>(parametersSize));
|
2014-12-03 06:46:55 +00:00
|
|
|
for (vector<string> const& variable: _localVariables)
|
2015-12-05 02:09:47 +00:00
|
|
|
context.addVariable(
|
|
|
|
dynamic_cast<VariableDeclaration const&>(resolveDeclaration(*sourceUnit, variable, resolver)),
|
|
|
|
parametersSize--
|
|
|
|
);
|
2014-12-03 06:46:55 +00:00
|
|
|
|
2019-09-18 14:44:36 +00:00
|
|
|
ExpressionCompiler(
|
|
|
|
context,
|
2020-01-14 16:48:17 +00:00
|
|
|
solidity::test::CommonOptions::get().optimize
|
2019-09-18 14:44:36 +00:00
|
|
|
).compile(*extractor.expression());
|
2014-12-03 06:46:55 +00:00
|
|
|
|
|
|
|
for (vector<string> const& function: _functions)
|
2015-12-05 02:09:47 +00:00
|
|
|
context << context.functionEntryLabel(dynamic_cast<FunctionDefinition const&>(
|
|
|
|
resolveDeclaration(*sourceUnit, function, resolver)
|
|
|
|
));
|
2015-09-10 10:01:05 +00:00
|
|
|
bytes instructions = context.assembledObject().bytecode;
|
2014-12-03 06:46:55 +00:00
|
|
|
// debug
|
2019-12-11 16:31:36 +00:00
|
|
|
// cout << evmasm::disassemble(instructions) << endl;
|
2014-12-03 06:46:55 +00:00
|
|
|
return instructions;
|
|
|
|
}
|
|
|
|
BOOST_FAIL("No contract found in source.");
|
2014-12-03 17:52:28 +00:00
|
|
|
return bytes();
|
2014-10-30 00:20:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(SolidityExpressionCompiler)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(literal_true)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-29 14:52:41 +00:00
|
|
|
function f() public { bool x = true; }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH1), 0x1});
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(literal_false)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-28 13:19:53 +00:00
|
|
|
function f() { bool x = false; }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH1), 0x0});
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(int_literal)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-28 13:19:53 +00:00
|
|
|
function f() { uint x = 0x12345678901234567890; }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH10), 0x12, 0x34, 0x56, 0x78, 0x90,
|
2014-10-30 00:20:32 +00:00
|
|
|
0x12, 0x34, 0x56, 0x78, 0x90});
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2015-02-05 12:48:02 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(int_with_wei_ether_subdenomination)
|
2015-02-04 21:02:35 +00:00
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
2015-02-05 12:48:02 +00:00
|
|
|
contract test {
|
2018-06-27 11:22:33 +00:00
|
|
|
constructor() {
|
2018-06-28 13:19:53 +00:00
|
|
|
uint x = 1 wei;
|
2015-02-04 21:02:35 +00:00
|
|
|
}
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2015-02-04 21:02:35 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH1), 0x1});
|
2015-02-05 12:48:02 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2020-06-18 15:09:24 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(int_with_gwei_ether_subdenomination)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
function test () {
|
|
|
|
uint x = 1 gwei;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
|
|
|
bytes expectation({uint8_t(Instruction::PUSH4), 0x3b, 0x9a, 0xca, 0x00});
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2015-02-05 12:48:02 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(int_with_ether_ether_subdenomination)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-27 11:22:33 +00:00
|
|
|
constructor() {
|
2018-06-28 13:19:53 +00:00
|
|
|
uint x = 1 ether;
|
2015-02-05 12:48:02 +00:00
|
|
|
}
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2015-02-05 12:48:02 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH8), 0xd, 0xe0, 0xb6, 0xb3, 0xa7, 0x64, 0x00, 0x00});
|
2015-02-04 21:02:35 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(comparison)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-28 13:19:53 +00:00
|
|
|
function f() { bool x = (0x10aa < 0x11aa) != true; }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation;
|
2020-01-14 16:48:17 +00:00
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
2019-02-28 22:25:24 +00:00
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::PUSH2), 0x11, 0xaa,
|
|
|
|
uint8_t(Instruction::PUSH2), 0x10, 0xaa,
|
|
|
|
uint8_t(Instruction::LT), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO)
|
|
|
|
};
|
|
|
|
else
|
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::PUSH2), 0x11, 0xaa,
|
|
|
|
uint8_t(Instruction::PUSH2), 0x10, 0xaa,
|
|
|
|
uint8_t(Instruction::LT), uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO)
|
|
|
|
};
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(short_circuiting)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-28 13:19:53 +00:00
|
|
|
function f() { bool x = true != (4 <= 8 + 10 || 9 != 2); }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation{
|
|
|
|
uint8_t(Instruction::PUSH1), 0x12, // 8 + 10
|
|
|
|
uint8_t(Instruction::PUSH1), 0x4,
|
|
|
|
uint8_t(Instruction::GT),
|
|
|
|
uint8_t(Instruction::ISZERO), // after this we have 4 <= 8 + 10
|
|
|
|
uint8_t(Instruction::DUP1),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x11,
|
|
|
|
uint8_t(Instruction::JUMPI), // short-circuit if it is true
|
|
|
|
uint8_t(Instruction::POP),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x9,
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO), // after this we have 9 != 2
|
|
|
|
uint8_t(Instruction::JUMPDEST),
|
|
|
|
uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1, uint8_t(Instruction::ISZERO), uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO)
|
|
|
|
};
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2018-07-10 07:18:19 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(arithmetic)
|
2014-10-30 00:20:32 +00:00
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2020-09-15 14:16:30 +00:00
|
|
|
function f(uint y) { unchecked { ((((((((y ^ 8) & 7) | 6) - 5) + 4) % 3) / 2) * 1); } }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2018-02-09 15:54:08 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
|
2019-02-28 22:25:24 +00:00
|
|
|
|
2020-10-13 11:28:39 +00:00
|
|
|
bytes panic =
|
|
|
|
bytes{uint8_t(Instruction::PUSH32)} +
|
|
|
|
fromHex("4E487B7100000000000000000000000000000000000000000000000000000000") +
|
|
|
|
bytes{
|
|
|
|
uint8_t(Instruction::PUSH1), 0x0,
|
|
|
|
uint8_t(Instruction::MSTORE),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x12,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x4,
|
|
|
|
uint8_t(Instruction::MSTORE),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x24,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x0,
|
|
|
|
uint8_t(Instruction::REVERT)
|
|
|
|
};
|
|
|
|
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation;
|
2020-01-14 16:48:17 +00:00
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
2020-10-13 11:28:39 +00:00
|
|
|
expectation = bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x3,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x5,
|
|
|
|
uint8_t(Instruction::DUP4),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x8,
|
|
|
|
uint8_t(Instruction::XOR),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x7,
|
|
|
|
uint8_t(Instruction::AND),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x6,
|
|
|
|
uint8_t(Instruction::OR),
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x4,
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
2020-10-13 11:28:39 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x48,
|
|
|
|
uint8_t(Instruction::JUMPI)
|
|
|
|
} + panic + bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::JUMPDEST),
|
|
|
|
uint8_t(Instruction::MOD),
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
2020-10-13 11:28:39 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x7e,
|
|
|
|
uint8_t(Instruction::JUMPI)
|
|
|
|
} + panic + bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::JUMPDEST),
|
|
|
|
uint8_t(Instruction::DIV),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::MUL)
|
|
|
|
};
|
|
|
|
else
|
2020-10-13 11:28:39 +00:00
|
|
|
expectation = bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x3,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x4,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x5,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x6,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x7,
|
|
|
|
uint8_t(Instruction::PUSH1), 0x8,
|
|
|
|
uint8_t(Instruction::DUP9),
|
|
|
|
uint8_t(Instruction::XOR),
|
|
|
|
uint8_t(Instruction::AND),
|
|
|
|
uint8_t(Instruction::OR),
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
2020-10-13 11:28:39 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x4a,
|
|
|
|
uint8_t(Instruction::JUMPI)
|
|
|
|
} + panic + bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::JUMPDEST),
|
|
|
|
uint8_t(Instruction::MOD),
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
|
|
|
uint8_t(Instruction::ISZERO),
|
2020-10-13 11:28:39 +00:00
|
|
|
uint8_t(Instruction::PUSH1), 0x80,
|
|
|
|
uint8_t(Instruction::JUMPI)
|
|
|
|
} + panic + bytes{
|
2019-02-28 22:25:24 +00:00
|
|
|
uint8_t(Instruction::JUMPDEST),
|
|
|
|
uint8_t(Instruction::DIV),
|
|
|
|
uint8_t(Instruction::MUL)
|
|
|
|
};
|
2020-10-13 11:28:39 +00:00
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(unary_operators)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2020-09-15 14:16:30 +00:00
|
|
|
function f(int y) { unchecked { !(~- y == 2); } }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2018-02-09 15:54:08 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "y"}});
|
2014-10-30 00:20:32 +00:00
|
|
|
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation;
|
2020-01-14 16:48:17 +00:00
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
2019-02-28 22:25:24 +00:00
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::DUP1),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x0,
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
uint8_t(Instruction::NOT),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO)
|
|
|
|
};
|
|
|
|
else
|
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x0,
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
uint8_t(Instruction::NOT),
|
|
|
|
uint8_t(Instruction::EQ),
|
|
|
|
uint8_t(Instruction::ISZERO)
|
|
|
|
};
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(unary_inc_dec)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2020-09-15 14:16:30 +00:00
|
|
|
function f(uint a) public returns (uint x) { unchecked { x = --a ^ (a-- ^ (++a ^ a++)); } }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "x"}});
|
|
|
|
|
|
|
|
// Stack: a, x
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation{
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::DUP1),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
// Stack here: a x a (a+1)
|
|
|
|
uint8_t(Instruction::SWAP3),
|
|
|
|
uint8_t(Instruction::POP), // first ++
|
|
|
|
// Stack here: (a+1) x a
|
|
|
|
uint8_t(Instruction::DUP3),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
// Stack here: (a+1) x a (a+2)
|
|
|
|
uint8_t(Instruction::SWAP3),
|
|
|
|
uint8_t(Instruction::POP),
|
|
|
|
// Stack here: (a+2) x a
|
|
|
|
uint8_t(Instruction::DUP3), // second ++
|
|
|
|
uint8_t(Instruction::XOR),
|
|
|
|
// Stack here: (a+2) x a^(a+2)
|
|
|
|
uint8_t(Instruction::DUP3),
|
|
|
|
uint8_t(Instruction::DUP1),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::SWAP1),
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
// Stack here: (a+2) x a^(a+2) (a+2) (a+1)
|
|
|
|
uint8_t(Instruction::SWAP4),
|
|
|
|
uint8_t(Instruction::POP), // first --
|
|
|
|
uint8_t(Instruction::XOR),
|
|
|
|
// Stack here: (a+1) x a^(a+2)^(a+2)
|
|
|
|
uint8_t(Instruction::DUP3),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x1,
|
|
|
|
uint8_t(Instruction::SWAP1),
|
|
|
|
uint8_t(Instruction::SUB),
|
|
|
|
// Stack here: (a+1) x a^(a+2)^(a+2) a
|
|
|
|
uint8_t(Instruction::SWAP3),
|
|
|
|
uint8_t(Instruction::POP), // second ++
|
|
|
|
// Stack here: a x a^(a+2)^(a+2)
|
|
|
|
uint8_t(Instruction::DUP3), // will change
|
|
|
|
uint8_t(Instruction::XOR),
|
|
|
|
uint8_t(Instruction::SWAP1),
|
|
|
|
uint8_t(Instruction::POP),
|
|
|
|
uint8_t(Instruction::DUP1)
|
|
|
|
};
|
|
|
|
// Stack here: a x a^(a+2)^(a+2)^a
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(assignment)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2020-09-15 14:16:30 +00:00
|
|
|
function f(uint a, uint b) { unchecked { (a += b) * 2; } }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-10-30 00:20:32 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {{"test", "f", "a"}, {"test", "f", "b"}});
|
|
|
|
|
|
|
|
// Stack: a, b
|
2019-02-28 22:25:24 +00:00
|
|
|
bytes expectation;
|
2020-01-14 16:48:17 +00:00
|
|
|
if (solidity::test::CommonOptions::get().optimize)
|
2019-02-28 22:25:24 +00:00
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::DUP1),
|
|
|
|
uint8_t(Instruction::DUP3),
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
uint8_t(Instruction::SWAP2),
|
|
|
|
uint8_t(Instruction::POP),
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::MUL)
|
|
|
|
};
|
|
|
|
else
|
|
|
|
expectation = {
|
|
|
|
uint8_t(Instruction::PUSH1), 0x2,
|
|
|
|
uint8_t(Instruction::DUP2),
|
|
|
|
uint8_t(Instruction::DUP4),
|
|
|
|
uint8_t(Instruction::ADD),
|
|
|
|
// Stack here: a b 2 a+b
|
|
|
|
uint8_t(Instruction::SWAP3),
|
|
|
|
uint8_t(Instruction::POP),
|
|
|
|
uint8_t(Instruction::DUP3),
|
|
|
|
// Stack here: a+b b 2 a+b
|
|
|
|
uint8_t(Instruction::MUL)
|
|
|
|
};
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2014-11-05 07:40:21 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(negative_literals_8bits)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
function f() { int8 x = -0x80; }
|
|
|
|
}
|
|
|
|
)";
|
2014-11-05 07:40:21 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation(bytes({uint8_t(Instruction::PUSH32)}) + bytes(31, 0xff) + bytes(1, 0x80));
|
2014-11-05 07:40:21 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(negative_literals_16bits)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
function f() { int64 x = ~0xabc; }
|
|
|
|
}
|
|
|
|
)";
|
2014-12-19 10:31:17 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation(bytes({uint8_t(Instruction::PUSH32)}) + bytes(30, 0xff) + bytes{0xf5, 0x43});
|
2014-12-19 10:31:17 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(intermediately_overflowing_literals)
|
|
|
|
{
|
|
|
|
// first literal itself is too large for 256 bits but it fits after all constant operations
|
|
|
|
// have been applied
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-28 13:19:53 +00:00
|
|
|
function f() { uint8 x = (0x00ffffffffffffffffffffffffffffffffffffffff * 0xffffffffffffffffffffffffff01) & 0xbf; }
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
)";
|
2014-11-05 07:40:21 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode);
|
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation(bytes({uint8_t(Instruction::PUSH1), 0xbf}));
|
2014-11-05 07:40:21 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2015-01-19 16:00:23 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(blockhash)
|
|
|
|
{
|
2016-12-03 20:52:51 +00:00
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
function f() {
|
2018-03-05 18:24:33 +00:00
|
|
|
blockhash(3);
|
2016-12-03 20:52:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
2018-03-05 18:24:33 +00:00
|
|
|
|
2019-04-29 16:29:40 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {});
|
2015-01-19 16:00:23 +00:00
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation({uint8_t(Instruction::PUSH1), 0x03,
|
|
|
|
uint8_t(Instruction::BLOCKHASH)});
|
2015-01-19 16:00:23 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2018-03-02 16:58:27 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(gas_left)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
2018-06-29 14:52:41 +00:00
|
|
|
function f() public returns (uint256 val) {
|
2018-03-02 16:58:27 +00:00
|
|
|
return gasleft();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
2019-04-29 16:29:40 +00:00
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {});
|
2018-03-02 16:58:27 +00:00
|
|
|
|
2018-11-07 11:04:46 +00:00
|
|
|
bytes expectation = bytes({uint8_t(Instruction::GAS)});
|
2018-03-02 16:58:27 +00:00
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
|
2019-10-14 04:25:42 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(selfbalance)
|
|
|
|
{
|
|
|
|
char const* sourceCode = R"(
|
|
|
|
contract test {
|
|
|
|
function f() returns (uint) {
|
|
|
|
return address(this).balance;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)";
|
|
|
|
|
|
|
|
bytes code = compileFirstExpression(sourceCode, {}, {});
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
if (solidity::test::CommonOptions::get().evmVersion() == EVMVersion::istanbul())
|
2019-10-14 04:25:42 +00:00
|
|
|
{
|
|
|
|
bytes expectation({uint8_t(Instruction::SELFBALANCE)});
|
|
|
|
BOOST_CHECK_EQUAL_COLLECTIONS(code.begin(), code.end(), expectation.begin(), expectation.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 00:20:32 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
} // end namespaces
|