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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
#include <memory>
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libevmasm/Assembly.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <libsolidity/parsing/Scanner.h>
|
2016-03-01 21:56:39 +00:00
|
|
|
#include <libsolidity/inlineasm/AsmStack.h>
|
2016-02-22 01:13:41 +00:00
|
|
|
#include <libsolidity/interface/Exceptions.h>
|
|
|
|
#include <libsolidity/ast/AST.h>
|
|
|
|
#include "../TestHelper.h"
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace dev
|
|
|
|
{
|
|
|
|
namespace solidity
|
|
|
|
{
|
|
|
|
namespace test
|
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2016-11-15 08:16:33 +00:00
|
|
|
bool successParse(std::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;
|
2016-02-22 01:13:41 +00:00
|
|
|
try
|
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
if (!stack.parse(std::make_shared<Scanner>(CharStream(_source))))
|
2016-02-22 01:13:41 +00:00
|
|
|
return false;
|
2016-03-01 21:56:39 +00:00
|
|
|
if (_assemble)
|
|
|
|
{
|
|
|
|
stack.assemble();
|
2016-11-15 08:16:33 +00:00
|
|
|
if (!stack.errors().empty())
|
|
|
|
if (!_allowWarnings || !Error::containsOnlyWarnings(stack.errors()))
|
|
|
|
return false;
|
2016-03-01 21:56:39 +00:00
|
|
|
}
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
catch (FatalError const&)
|
|
|
|
{
|
2016-03-01 21:56:39 +00:00
|
|
|
if (Error::containsErrorOfType(stack.errors(), Error::Type::ParserError))
|
2016-02-22 01:13:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-03-01 21:56:39 +00:00
|
|
|
if (Error::containsErrorOfType(stack.errors(), Error::Type::ParserError))
|
2016-02-22 01:13:41 +00:00
|
|
|
return false;
|
|
|
|
|
2016-03-01 21:56:39 +00:00
|
|
|
BOOST_CHECK(Error::containsOnlyWarnings(stack.errors()));
|
2016-02-22 01:13:41 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(SolidityInlineAssembly)
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ 7 8 add =: x }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ add(7, mul(6, x)) add mul(7, 8) }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(functional_assignment)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ x := 7 }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(functional_assignment_complex)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ x := add(7, mul(6, x)) add mul(7, 8) }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(vardecl_complex)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ let x := add(7, mul(6, x)) add mul(7, 8) }"));
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(blocks)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(successParse("{ let x := 7 { let y := 3 } { let z := 2 } }"));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(!successAssemble("{ let x := \"123456789012345678901234567890123\" }"));
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
BOOST_CHECK(!successAssemble("{ this }"));
|
|
|
|
BOOST_CHECK(!successAssemble("{ ecrecover }"));
|
|
|
|
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));
|
|
|
|
BOOST_CHECK(!successAssemble("{ 1 }", false));
|
|
|
|
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
|
|
|
{
|
|
|
|
// Error message: "Cannot use instruction names for identifier names."
|
|
|
|
BOOST_CHECK(!successAssemble("{ let gas := 1 }"));
|
|
|
|
}
|
|
|
|
|
2017-01-25 16:27:01 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_assignment)
|
|
|
|
{
|
2017-01-26 12:52:02 +00:00
|
|
|
// Error message: "Identifier expected, got instruction name."
|
2017-01-25 16:27:01 +00:00
|
|
|
BOOST_CHECK(!successAssemble("{ 2 =: gas }"));
|
|
|
|
}
|
|
|
|
|
2017-01-26 12:40:40 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(inline_assembly_shadowed_instruction_functional_assignment)
|
|
|
|
{
|
|
|
|
// Error message: "Cannot use instruction names for identifier names."
|
|
|
|
BOOST_CHECK(!successAssemble("{ gas := 2 }"));
|
|
|
|
}
|
|
|
|
|
2016-02-22 01:13:41 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // end namespaces
|