solidity/test/libsolidity/InlineAssembly.cpp

402 lines
10 KiB
C++
Raw Normal View History

2016-02-22 01:13:41 +00:00
/*
This file is part of solidity.
2016-02-22 01:13:41 +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.
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
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.
*/
#include "../TestHelper.h"
#include <libsolidity/inlineasm/AsmStack.h>
#include <libsolidity/parsing/Scanner.h>
2016-02-22 01:13:41 +00:00
#include <libsolidity/interface/Exceptions.h>
#include <libsolidity/ast/AST.h>
#include <test/libsolidity/ErrorCheck.h>
#include <libevmasm/Assembly.h>
#include <boost/optional.hpp>
2017-04-21 22:05:12 +00:00
#include <boost/algorithm/string/replace.hpp>
#include <string>
#include <memory>
2016-02-22 01:13:41 +00:00
using namespace std;
namespace dev
{
namespace solidity
{
namespace test
{
namespace
{
boost::optional<Error> parseAndReturnFirstError(string const& _source, bool _assemble = false, bool _allowWarnings = true)
2016-02-22 01:13:41 +00:00
{
assembly::InlineAssemblyStack stack;
bool success = false;
2016-02-22 01:13:41 +00:00
try
{
success = stack.parse(std::make_shared<Scanner>(CharStream(_source)));
if (success && _assemble)
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
{
BOOST_FAIL("Fatal error leaked.");
success = false;
2016-02-22 01:13:41 +00:00
}
if (!success)
{
BOOST_REQUIRE_EQUAL(stack.errors().size(), 1);
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
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-11-15 08:16:33 +00:00
return successParse(_source, true, _allowWarnings);
}
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
}
#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 pop }"));
2016-02-22 01:13:41 +00:00
}
2016-10-05 11:00:36 +00:00
BOOST_AUTO_TEST_CASE(suicide_selfdestruct)
{
BOOST_CHECK(successParse("{ 0x01 suicide 0x02 selfdestruct }"));
2016-10-05 11:00:36 +00:00
}
2016-04-05 12:57:40 +00:00
BOOST_AUTO_TEST_CASE(keywords)
{
BOOST_CHECK(successParse("{ 1 2 byte 2 return address pop }"));
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 pop }"));
2016-02-22 01:13:41 +00:00
}
BOOST_AUTO_TEST_CASE(vardecl)
{
BOOST_CHECK(successParse("{ let x := 7 }"));
}
2017-05-17 12:20:24 +00:00
BOOST_AUTO_TEST_CASE(vardecl_bool)
{
CHECK_PARSE_ERROR("{ let x := true }", ParserError, "True and false are not valid literals.");
CHECK_PARSE_ERROR("{ let x := false }", ParserError, "True and false are not valid literals.");
}
2016-02-22 01:13:41 +00:00
BOOST_AUTO_TEST_CASE(assignment)
{
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 pop }"));
2016-02-22 01:13:41 +00:00
}
BOOST_AUTO_TEST_CASE(label_complex)
{
BOOST_CHECK(successParse("{ 7 abc: 8 eq jump(abc) jumpi(eq(7, 8), abc) pop }"));
2016-02-22 01:13:41 +00:00
}
BOOST_AUTO_TEST_CASE(functional)
{
BOOST_CHECK(successParse("{ let x := 2 add(7, mul(6, x)) mul(7, 8) add =: x }"));
2016-02-22 01:13:41 +00:00
}
BOOST_AUTO_TEST_CASE(functional_assignment)
{
BOOST_CHECK(successParse("{ let x := 2 x := 7 }"));
2016-02-22 01:13:41 +00:00
}
BOOST_AUTO_TEST_CASE(functional_assignment_complex)
{
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)
{
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 { } }"));
2017-01-31 22:59:41 +00:00
}
BOOST_AUTO_TEST_CASE(function_definitions_multiple_args)
{
BOOST_CHECK(successParse("{ function f(a, d) { } function g(a, d) -> x, y { } }"));
2017-01-31 22:59:41 +00:00
}
2017-02-01 20:20:21 +00:00
BOOST_AUTO_TEST_CASE(function_calls)
{
BOOST_CHECK(successParse("{ function f(a) -> b {} 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 pop } }", 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 pop\n}");
2017-02-14 14:40:58 +00:00
}
BOOST_AUTO_TEST_CASE(print_subblock)
{
parsePrintCompare("{\n {\n dup4\n add\n }\n}");
}
BOOST_AUTO_TEST_CASE(print_functional)
{
parsePrintCompare("{\n let x := mul(sload(0x12), 7)\n}");
2017-02-14 14:40:58 +00:00
}
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 pop\n}");
2017-02-14 14:40:58 +00:00
}
2017-02-15 14:21:11 +00:00
BOOST_AUTO_TEST_CASE(print_string_literal_unicode)
{
string source = "{ let x := \"\\u1bac\" }";
string parsed = "{\n let x := \"\\xe1\\xae\\xac\"\n}";
2017-02-15 14:21:11 +00:00
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}");
2017-02-14 15:08:33 +00:00
}
BOOST_AUTO_TEST_CASE(function_calls)
{
2017-03-22 15:53:15 +00:00
string source = R"({
function y()
{
}
function f(a) -> b
2017-03-22 15:53:15 +00:00
{
}
function g(a, b, c)
{
}
g(1, mul(2, address), f(mul(2, caller)))
y()
})";
boost::replace_all(source, "\t", " ");
parsePrintCompare(source);
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)
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");
}
BOOST_AUTO_TEST_CASE(assignment_after_tag)
{
BOOST_CHECK(successParse("{ let x := 1 { 7 tag: =: x } }"));
}
BOOST_AUTO_TEST_CASE(magic_variables)
{
CHECK_ASSEMBLE_ERROR("{ this pop }", DeclarationError, "Identifier not found");
CHECK_ASSEMBLE_ERROR("{ ecrecover pop }", DeclarationError, "Identifier not found");
BOOST_CHECK(successAssemble("{ let ecrecover := 1 ecrecover pop }"));
}
2017-04-11 17:24:38 +00:00
BOOST_AUTO_TEST_CASE(stack_variables)
{
BOOST_CHECK(successAssemble("{ let y := 3 { 2 { let x := y } pop} }"));
}
2016-11-15 08:16:33 +00:00
BOOST_AUTO_TEST_CASE(imbalanced_stack)
{
BOOST_CHECK(successAssemble("{ 1 2 mul pop }", false));
CHECK_ASSEMBLE_ERROR("{ 1 }", DeclarationError, "Unbalanced stack at the end of a block: 1 surplus item(s).");
CHECK_ASSEMBLE_ERROR("{ pop }", DeclarationError, "Unbalanced stack at the end of a block: 1 missing item(s).");
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)
{
CHECK_ASSEMBLE_ERROR("{ jump(invalidJumpLabel) }", DeclarationError, "Identifier not found");
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
{
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)
{
CHECK_ASSEMBLE_ERROR("{ 2 =: gas }", ParserError, "Identifier expected, got instruction name.");
2017-01-25 16:27:01 +00:00
}
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_functional_assignment)
{
CHECK_ASSEMBLE_ERROR("{ gas := 2 }", ParserError, "Label name / variable name must precede \":\"");
}
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