2016-02-22 01:13:41 +00:00
|
|
|
/*
|
2016-11-18 23:13:20 +00:00
|
|
|
This file is part of solidity.
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2016-11-18 23:13:20 +00:00
|
|
|
solidity is free software: you can redistribute it and/or modify
|
2016-02-22 01:13:41 +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,
|
2016-02-22 01:13:41 +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/>.
|
2016-02-22 01:13:41 +00:00
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @author Christian <c@ethdev.com>
|
|
|
|
* @date 2016
|
|
|
|
* Unit tests for inline assembly.
|
|
|
|
*/
|
|
|
|
|
2017-02-17 15:04:42 +00:00
|
|
|
#include "../TestHelper.h"
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmStack.h>
|
2017-02-17 15:04:42 +00:00
|
|
|
#include <libsolidity/parsing/Scanner.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
2017-02-17 15:04:42 +00:00
|
|
|
#include <test/libsolidity/ErrorCheck.h>
|
|
|
|
#include <libevmasm/Assembly.h>
|
|
|
|
|
|
|
|
#include <boost/optional.hpp>
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2016-02-22 01:13:41 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2017-02-17 15:04:42 +00:00
|
|
|
boost::optional<Error> parseAndReturnFirstError(string const& _source, bool _assemble = false, bool _allowWarnings = true)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
assembly::InlineAssemblyStack stack;
|
2017-02-17 15:04:42 +00:00
|
|
|
bool success = false;
|
2016-02-22 01:13:41 +00:00
|
|
|
try
|
|
|
|
{
|
2017-02-17 15:04:42 +00:00
|
|
|
success = stack.parse(std::make_shared<Scanner>(CharStream(_source)));
|
|
|
|
if (success && _assemble)
|
2016-03-01 21:56:39 +00:00
|
|
|
stack.assemble();
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-02-20 10:57:50 +00:00
|
|
|
catch (FatalError const&)
|
2016-02-22 01:13:41 +00:00
|
|
|
{
|
2017-02-17 15:04:42 +00:00
|
|
|
BOOST_FAIL("Fatal error leaked.");
|
|
|
|
success = false;
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
2017-02-17 15:04:42 +00:00
|
|
|
if (!success)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_REQUIRE_EQUAL(stack.errors().size(), 1);
|
2017-02-17 15:04:42 +00:00
|
|
|
return *stack.errors().front();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If success is true, there might still be an error in the assembly stage.
|
|
|
|
if (_allowWarnings && Error::containsOnlyWarnings(stack.errors()))
|
|
|
|
return {};
|
|
|
|
else if (!stack.errors().empty())
|
|
|
|
{
|
|
|
|
if (!_allowWarnings)
|
|
|
|
BOOST_CHECK_EQUAL(stack.errors().size(), 1);
|
|
|
|
return *stack.errors().front();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
|
2017-02-17 15:04:42 +00:00
|
|
|
bool successParse(std::string const& _source, bool _assemble = false, bool _allowWarnings = true)
|
|
|
|
{
|
|
|
|
return !parseAndReturnFirstError(_source, _assemble, _allowWarnings);
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2016-11-15 08:16:33 +00:00
|
|
|
bool successAssemble(string const& _source, bool _allowWarnings = true)
|
2016-03-01 21:56:39 +00:00
|
|
|
{
|
2016-11-15 08:16:33 +00:00
|
|
|
return successParse(_source, true, _allowWarnings);
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 15:04:42 +00:00
|
|
|
Error expectError(std::string const& _source, bool _assemble, bool _allowWarnings = false)
|
|
|
|
{
|
|
|
|
|
|
|
|
auto error = parseAndReturnFirstError(_source, _assemble, _allowWarnings);
|
|
|
|
BOOST_REQUIRE(error);
|
|
|
|
return *error;
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:40:58 +00:00
|
|
|
void parsePrintCompare(string const& _source)
|
|
|
|
{
|
|
|
|
assembly::InlineAssemblyStack stack;
|
|
|
|
BOOST_REQUIRE(stack.parse(std::make_shared<Scanner>(CharStream(_source))));
|
|
|
|
BOOST_REQUIRE(stack.errors().empty());
|
|
|
|
BOOST_CHECK_EQUAL(stack.toString(), _source);
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
2017-02-17 15:04:42 +00:00
|
|
|
#define CHECK_ERROR(text, assemble, typ, substring) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
Error err = expectError((text), (assemble), false); \
|
|
|
|
BOOST_CHECK(err.type() == (Error::Type::typ)); \
|
|
|
|
BOOST_CHECK(searchErrorMessage(err, (substring))); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define CHECK_PARSE_ERROR(text, type, substring) \
|
|
|
|
CHECK_ERROR(text, false, type, substring)
|
|
|
|
|
|
|
|
#define CHECK_ASSEMBLE_ERROR(text, type, substring) \
|
|
|
|
CHECK_ERROR(text, true, type, substring)
|
|
|
|
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(SolidityInlineAssembly)
|
|
|
|
|
2017-02-14 14:40:58 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(Parsing)
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(smoke_test)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(simple_instructions)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ dup1 dup1 mul dup1 sub }"));
|
|
|
|
}
|
|
|
|
|
2016-10-05 11:00:36 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(suicide_selfdestruct)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ suicide selfdestruct }"));
|
|
|
|
}
|
|
|
|
|
2016-04-05 12:57:40 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(keywords)
|
|
|
|
{
|
2016-10-05 10:48:30 +00:00
|
|
|
BOOST_CHECK(successParse("{ byte return address }"));
|
2016-04-05 12:57:40 +00:00
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(constants)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ 7 8 mul }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(vardecl)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ let x := 7 }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(assignment)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ let x := 2 7 8 add =: x }"));
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(label)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ 7 abc: 8 eq abc jump }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(label_complex)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ 7 abc: 8 eq jump(abc) jumpi(eq(7, 8), abc) }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(functional)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ let x := 2 add(7, mul(6, x)) mul(7, 8) add }"));
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(functional_assignment)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ let x := 2 x := 7 }"));
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(functional_assignment_complex)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ let x := 2 x := add(7, mul(6, x)) mul(7, 8) add }"));
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(vardecl_complex)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ let y := 2 let x := add(7, mul(6, y)) add mul(7, 8) }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(variable_use_before_decl)
|
|
|
|
{
|
|
|
|
CHECK_PARSE_ERROR("{ x := 2 let x := 3 }", DeclarationError, "Variable x used before it was declared.");
|
|
|
|
CHECK_PARSE_ERROR("{ let x := mul(2, x) }", DeclarationError, "Variable x used before it was declared.");
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(blocks)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ let x := 7 { let y := 3 } { let z := 2 } }"));
|
|
|
|
}
|
|
|
|
|
2017-01-31 22:59:41 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(function_definitions)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ function f() { } function g(a) -> (x) { } }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ function f(a, d) { } function g(a, d) -> (x, y) { } }"));
|
|
|
|
}
|
|
|
|
|
2017-02-01 20:20:21 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(function_calls)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
BOOST_CHECK(successParse("{ function f(a) {} function g(a, b, c) {} function x() { g(1, 2, f(mul(2, 3))) x() } }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(opcode_for_functions)
|
|
|
|
{
|
|
|
|
CHECK_PARSE_ERROR("{ function gas() { } }", ParserError, "Cannot use instruction names for identifier names.");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(opcode_for_function_args)
|
|
|
|
{
|
|
|
|
CHECK_PARSE_ERROR("{ function f(gas) { } }", ParserError, "Cannot use instruction names for identifier names.");
|
|
|
|
CHECK_PARSE_ERROR("{ function f() -> (gas) { } }", ParserError, "Cannot use instruction names for identifier names.");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(name_clashes)
|
|
|
|
{
|
|
|
|
CHECK_PARSE_ERROR("{ let g := 2 function g() { } }", DeclarationError, "Function name g already taken in this scope");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(variable_access_cross_functions)
|
|
|
|
{
|
|
|
|
CHECK_PARSE_ERROR("{ let x := 2 function g() { x } }", DeclarationError, "Identifier not found.");
|
2017-02-01 20:20:21 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 14:40:58 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(Printing)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_smoke)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_instructions)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n 7\n 8\n mul\n dup10\n add\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_subblock)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n {\n dup4\n add\n }\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_functional)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n mul(sload(0x12), 7)\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_label)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n loop:\n jump(loop)\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_assignments)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n let x := mul(2, 3)\n 7\n =: x\n x := add(1, 2)\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(print_string_literals)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n \"\\n'\\xab\\x95\\\"\"\n}");
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:21:11 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(print_string_literal_unicode)
|
|
|
|
{
|
|
|
|
string source = "{ \"\\u1bac\" }";
|
|
|
|
string parsed = "{\n \"\\xe1\\xae\\xac\"\n}";
|
|
|
|
assembly::InlineAssemblyStack stack;
|
|
|
|
BOOST_REQUIRE(stack.parse(std::make_shared<Scanner>(CharStream(source))));
|
|
|
|
BOOST_REQUIRE(stack.errors().empty());
|
|
|
|
BOOST_CHECK_EQUAL(stack.toString(), parsed);
|
|
|
|
parsePrintCompare(parsed);
|
|
|
|
}
|
|
|
|
|
2017-02-14 15:08:33 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
|
|
|
|
{
|
|
|
|
parsePrintCompare("{\n function f(a, d)\n {\n mstore(a, d)\n }\n function g(a, d) -> (x, y)\n {\n }\n}");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(function_calls)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
parsePrintCompare(
|
|
|
|
"{\n function y()\n {\n }\n function f(a)\n {\n }\n function g(a, b, c)\n {\n }\n g(1, mul(2, address), f(mul(2, caller)))\n y()\n}"
|
|
|
|
);
|
2017-02-14 15:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-02-14 14:40:58 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(Analysis)
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(string_literals)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successAssemble("{ let x := \"12345678901234567890123456789012\" }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(oversize_string_literals)
|
|
|
|
{
|
2017-02-20 10:42:23 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ let x := \"123456789012345678901234567890123\" }", TypeError, "String literal too long");
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|
|
|
|
|
2016-10-06 13:32:11 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(assignment_after_tag)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ let x := 1 { tag: =: x } }"));
|
|
|
|
}
|
|
|
|
|
2016-10-07 23:03:56 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(magic_variables)
|
|
|
|
{
|
2017-02-17 15:05:22 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ this pop }", DeclarationError, "Identifier not found");
|
|
|
|
CHECK_ASSEMBLE_ERROR("{ ecrecover pop }", DeclarationError, "Identifier not found");
|
2016-10-07 23:03:56 +00:00
|
|
|
BOOST_CHECK(successAssemble("{ let ecrecover := 1 ecrecover }"));
|
|
|
|
}
|
|
|
|
|
2016-11-15 08:16:33 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(imbalanced_stack)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successAssemble("{ 1 2 mul pop }", false));
|
2017-02-20 10:42:23 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ 1 }", Warning, "Inline assembly block is not balanced. It leaves");
|
|
|
|
CHECK_ASSEMBLE_ERROR("{ pop }", Warning, "Inline assembly block is not balanced. It takes");
|
2016-11-15 08:16:33 +00:00
|
|
|
BOOST_CHECK(successAssemble("{ let x := 4 7 add }", false));
|
|
|
|
}
|
|
|
|
|
2016-10-20 11:30:10 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(error_tag)
|
|
|
|
{
|
2016-11-15 10:12:03 +00:00
|
|
|
BOOST_CHECK(successAssemble("{ invalidJumpLabel }"));
|
2016-10-20 11:30:10 +00:00
|
|
|
}
|
|
|
|
|
2017-01-27 21:24:58 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(designated_invalid_instruction)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successAssemble("{ invalid }"));
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:27:01 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_declaration)
|
2017-01-25 16:24:43 +00:00
|
|
|
{
|
2017-02-17 15:04:42 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ let gas := 1 }", ParserError, "Cannot use instruction names for identifier names.");
|
2017-01-25 16:24:43 +00:00
|
|
|
}
|
|
|
|
|
2017-01-25 16:27:01 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_assignment)
|
|
|
|
{
|
2017-02-17 15:04:42 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ 2 =: gas }", ParserError, "Identifier expected, got instruction name.");
|
2017-01-25 16:27:01 +00:00
|
|
|
}
|
|
|
|
|
2017-01-26 12:40:40 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_functional_assignment)
|
|
|
|
{
|
2017-02-17 15:04:42 +00:00
|
|
|
CHECK_ASSEMBLE_ERROR("{ gas := 2 }", ParserError, "Label name / variable name must precede \":\"");
|
2017-01-26 12:40:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-09 16:34:48 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(revert)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successAssemble("{ revert(0, 0) }"));
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
2017-02-14 14:40:58 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end namespaces
|