From 97cc968a133247e729ed1d189d35ef2b7b53b3d4 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 24 May 2017 18:34:19 +0200 Subject: Initial EVM1.5 assembly implementation. --- test/libsolidity/InlineAssembly.cpp | 36 ++++++++ test/libsolidity/SolidityEndToEndTest.cpp | 96 ++++++++++++++++++++++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 15 ++++ 3 files changed, 147 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index f0543101..88b2d584 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -481,6 +481,42 @@ BOOST_AUTO_TEST_CASE(revert) BOOST_CHECK(successAssemble("{ revert(0, 0) }")); } +BOOST_AUTO_TEST_CASE(function_calls) +{ + BOOST_CHECK(successAssemble("{ function f() {} }")); + BOOST_CHECK(successAssemble("{ function f() { let y := 2 } }")); + BOOST_CHECK(successAssemble("{ function f() -> z { let y := 2 } }")); + BOOST_CHECK(successAssemble("{ function f(a) { let y := 2 } }")); + BOOST_CHECK(successAssemble("{ function f(a) { let y := a } }")); + BOOST_CHECK(successAssemble("{ function f() -> x, y, z {} }")); + BOOST_CHECK(successAssemble("{ function f(x, y, z) {} }")); + BOOST_CHECK(successAssemble("{ function f(a, b) -> x, y, z { y := a } }")); + BOOST_CHECK(successAssemble("{ function f() {} f() }")); + BOOST_CHECK(successAssemble("{ function f() -> x, y { x := 1 y := 2} let a, b := f() }")); + BOOST_CHECK(successAssemble("{ function f(a, b) -> x, y { x := b y := a } let a, b := f(2, 3) }")); + BOOST_CHECK(successAssemble("{ function rec(a) { rec(sub(a, 1)) } rec(2) }")); + BOOST_CHECK(successAssemble("{ let r := 2 function f() -> x, y { x := 1 y := 2} let a, b := f() b := r }")); +} + +BOOST_AUTO_TEST_CASE(switch_statement) +{ + BOOST_CHECK(successAssemble("{ switch 1 default {} }")); + BOOST_CHECK(successAssemble("{ switch 1 case 1 {} default {} }")); + BOOST_CHECK(successAssemble("{ switch 1 case 1 {} }")); + BOOST_CHECK(successAssemble("{ let a := 3 switch a case 1 { a := 1 } case 2 { a := 5 } a := 9}")); + BOOST_CHECK(successAssemble("{ let a := 2 switch calldataload(0) case 1 { a := 1 } case 2 { a := 5 } }")); +} + +BOOST_AUTO_TEST_CASE(large_constant) +{ + auto source = R"({ + switch mul(1, 2) + case 0x0000000000000000000000000000000000000000000000000000000026121ff0 { + } + })"; + BOOST_CHECK(successAssemble(source)); +} + BOOST_AUTO_TEST_CASE(keccak256) { BOOST_CHECK(successAssemble("{ 0 0 keccak256 pop }")); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index aae8b146..bb8ec112 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -7535,6 +7535,102 @@ BOOST_AUTO_TEST_CASE(inline_assembly_function_access) BOOST_CHECK(callContractFunction("x()") == encodeArgs(u256(10))); } +BOOST_AUTO_TEST_CASE(inline_assembly_function_call) +{ + char const* sourceCode = R"( + contract C { + function f() { + assembly { + function asmfun(a, b, c) -> x, y, z { + x := a + y := b + z := 7 + } + let a1, b1, c1 := asmfun(1, 2, 3) + mstore(0x00, a1) + mstore(0x20, b1) + mstore(0x40, c1) + return(0, 0x60) + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(1), u256(2), u256(7))); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_function_call2) +{ + char const* sourceCode = R"( + contract C { + function f() { + assembly { + let d := 0x10 + function asmfun(a, b, c) -> x, y, z { + x := a + y := b + z := 7 + } + let a1, b1, c1 := asmfun(1, 2, 3) + mstore(0x00, a1) + mstore(0x20, b1) + mstore(0x40, c1) + mstore(0x60, d) + return(0, 0x80) + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(1), u256(2), u256(7), u256(0x10))); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_switch) +{ + char const* sourceCode = R"( + contract C { + function f(uint a) returns (uint b) { + assembly { + switch a + case 1 { b := 8 } + case 2 { b := 9 } + default { b := 2 } + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f(uint256)", u256(0)) == encodeArgs(u256(2))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(1)) == encodeArgs(u256(8))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(2)) == encodeArgs(u256(9))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(3)) == encodeArgs(u256(2))); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_recursion) +{ + char const* sourceCode = R"( + contract C { + function f(uint a) returns (uint b) { + assembly { + function fac(n) -> nf { + switch n + case 0 { nf := 1 } + case 1 { nf := 1 } + default { nf := mul(n, fac(sub(n, 1))) } + } + b := fac(a) + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f(uint256)", u256(0)) == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(1)) == encodeArgs(u256(1))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(2)) == encodeArgs(u256(2))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(3)) == encodeArgs(u256(6))); + BOOST_CHECK(callContractFunction("f(uint256)", u256(4)) == encodeArgs(u256(24))); +} + BOOST_AUTO_TEST_CASE(index_access_with_type_conversion) { // Test for a bug where higher order bits cleanup was not done for array index access. diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 71726b93..16e2dea4 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -5159,6 +5159,21 @@ BOOST_AUTO_TEST_CASE(inline_assembly_constant_access) CHECK_ERROR(text, TypeError, "Constant variables not supported by inline assembly"); } +BOOST_AUTO_TEST_CASE(inline_assembly_variable_access_out_of_functions) +{ + char const* text = R"( + contract test { + function f() { + uint a; + assembly { + function g() -> x { x := a } + } + } + } + )"; + CHECK_ERROR(text, DeclarationError, "Inline assembly functions cannot access their outer scope."); +} + BOOST_AUTO_TEST_CASE(invalid_mobile_type) { char const* text = R"( -- cgit From 64ddb176bb71498f3a129e0cc549797f4138ec1f Mon Sep 17 00:00:00 2001 From: chriseth Date: Mon, 29 May 2017 20:32:47 +0200 Subject: Test for accessing outer inline assembly scope. --- test/libsolidity/SolidityEndToEndTest.cpp | 27 +++++++++++++++++ test/libsolidity/SolidityNameAndTypeResolution.cpp | 35 ++++++++++++++++++++-- 2 files changed, 60 insertions(+), 2 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index bb8ec112..212c5111 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -7462,6 +7462,33 @@ BOOST_AUTO_TEST_CASE(inline_assembly_storage_access) BOOST_CHECK(callContractFunction("z()") == encodeArgs(u256(7))); } +BOOST_AUTO_TEST_CASE(inline_assembly_storage_access_inside_function) +{ + char const* sourceCode = R"( + contract C { + uint16 x; + uint16 public y; + uint public z; + function f() returns (bool) { + uint off1; + uint off2; + assembly { + function f() -> o1 { + sstore(z_slot, 7) + o1 := y_offset + } + off2 := f() + } + assert(off2 == 2); + return true; + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(true)); + BOOST_CHECK(callContractFunction("z()") == encodeArgs(u256(7))); +} + BOOST_AUTO_TEST_CASE(inline_assembly_storage_access_via_pointer) { char const* sourceCode = R"( diff --git a/test/libsolidity/SolidityNameAndTypeResolution.cpp b/test/libsolidity/SolidityNameAndTypeResolution.cpp index 16e2dea4..db5b9bf8 100644 --- a/test/libsolidity/SolidityNameAndTypeResolution.cpp +++ b/test/libsolidity/SolidityNameAndTypeResolution.cpp @@ -5159,7 +5159,7 @@ BOOST_AUTO_TEST_CASE(inline_assembly_constant_access) CHECK_ERROR(text, TypeError, "Constant variables not supported by inline assembly"); } -BOOST_AUTO_TEST_CASE(inline_assembly_variable_access_out_of_functions) +BOOST_AUTO_TEST_CASE(inline_assembly_local_variable_access_out_of_functions) { char const* text = R"( contract test { @@ -5171,7 +5171,38 @@ BOOST_AUTO_TEST_CASE(inline_assembly_variable_access_out_of_functions) } } )"; - CHECK_ERROR(text, DeclarationError, "Inline assembly functions cannot access their outer scope."); + CHECK_ERROR(text, DeclarationError, "Cannot access local Solidity variables from inside an inline assembly function."); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_local_variable_access_out_of_functions_storage_ptr) +{ + char const* text = R"( + contract test { + uint[] r; + function f() { + uint[] storage a = r; + assembly { + function g() -> x { x := a_offset } + } + } + } + )"; + CHECK_ERROR(text, DeclarationError, "Cannot access local Solidity variables from inside an inline assembly function."); +} + +BOOST_AUTO_TEST_CASE(inline_assembly_storage_variable_access_out_of_functions) +{ + char const* text = R"( + contract test { + uint a; + function f() { + assembly { + function g() -> x { x := a_slot } + } + } + } + )"; + CHECK_SUCCESS_NO_WARNINGS(text); } BOOST_AUTO_TEST_CASE(invalid_mobile_type) -- cgit From b75c7b577583721a853a01a070f5497d702d17f0 Mon Sep 17 00:00:00 2001 From: chriseth Date: Wed, 31 May 2017 12:39:50 +0200 Subject: Move inline assembly to new abstraction and test both backends. --- test/libsolidity/InlineAssembly.cpp | 39 ++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'test/libsolidity') diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index 88b2d584..4de2ea81 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -22,7 +22,7 @@ #include "../TestHelper.h" -#include +#include #include #include #include @@ -47,15 +47,20 @@ namespace test namespace { -boost::optional parseAndReturnFirstError(string const& _source, bool _assemble = false, bool _allowWarnings = true) +boost::optional parseAndReturnFirstError( + string const& _source, + bool _assemble = false, + bool _allowWarnings = true, + AssemblyStack::Machine _machine = AssemblyStack::Machine::EVM +) { - assembly::InlineAssemblyStack stack; + AssemblyStack stack; bool success = false; try { - success = stack.parse(std::make_shared(CharStream(_source))); + success = stack.parseAndAnalyze("", _source); if (success && _assemble) - stack.assemble(); + stack.assemble(_machine); } catch (FatalError const&) { @@ -82,14 +87,20 @@ boost::optional parseAndReturnFirstError(string const& _source, bool _ass return {}; } -bool successParse(std::string const& _source, bool _assemble = false, bool _allowWarnings = true) +bool successParse( + string const& _source, + bool _assemble = false, + bool _allowWarnings = true, + AssemblyStack::Machine _machine = AssemblyStack::Machine::EVM +) { - return !parseAndReturnFirstError(_source, _assemble, _allowWarnings); + return !parseAndReturnFirstError(_source, _assemble, _allowWarnings, _machine); } bool successAssemble(string const& _source, bool _allowWarnings = true) { - return successParse(_source, true, _allowWarnings); + return successParse(_source, true, _allowWarnings, AssemblyStack::Machine::EVM) && + successParse(_source, true, _allowWarnings, AssemblyStack::Machine::EVM15); } Error expectError(std::string const& _source, bool _assemble, bool _allowWarnings = false) @@ -102,10 +113,10 @@ Error expectError(std::string const& _source, bool _assemble, bool _allowWarning void parsePrintCompare(string const& _source) { - assembly::InlineAssemblyStack stack; - BOOST_REQUIRE(stack.parse(std::make_shared(CharStream(_source)))); + AssemblyStack stack; + BOOST_REQUIRE(stack.parseAndAnalyze("", _source)); BOOST_REQUIRE(stack.errors().empty()); - BOOST_CHECK_EQUAL(stack.toString(), _source); + BOOST_CHECK_EQUAL(stack.print(), _source); } } @@ -376,10 +387,10 @@ BOOST_AUTO_TEST_CASE(print_string_literal_unicode) { string source = "{ let x := \"\\u1bac\" }"; string parsed = "{\n let x := \"\\xe1\\xae\\xac\"\n}"; - assembly::InlineAssemblyStack stack; - BOOST_REQUIRE(stack.parse(std::make_shared(CharStream(source)))); + AssemblyStack stack; + BOOST_REQUIRE(stack.parseAndAnalyze("", source)); BOOST_REQUIRE(stack.errors().empty()); - BOOST_CHECK_EQUAL(stack.toString(), parsed); + BOOST_CHECK_EQUAL(stack.print(), parsed); parsePrintCompare(parsed); } -- cgit From b098b363b55a687542c97c8f35f65d0eb08eb44a Mon Sep 17 00:00:00 2001 From: chriseth Date: Thu, 1 Jun 2017 17:59:29 +0200 Subject: Test for embedded functions. --- test/libsolidity/InlineAssembly.cpp | 5 +++++ test/libsolidity/SolidityEndToEndTest.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index 4de2ea81..e9e310d0 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -509,6 +509,11 @@ BOOST_AUTO_TEST_CASE(function_calls) BOOST_CHECK(successAssemble("{ let r := 2 function f() -> x, y { x := 1 y := 2} let a, b := f() b := r }")); } +BOOST_AUTO_TEST_CASE(embedded_functions) +{ + BOOST_CHECK(successAssemble("{ function f(r, s) -> x { function g(a) -> b { } x := g(2) } let x := f(2, 3) }")); +} + BOOST_AUTO_TEST_CASE(switch_statement) { BOOST_CHECK(successAssemble("{ switch 1 default {} }")); diff --git a/test/libsolidity/SolidityEndToEndTest.cpp b/test/libsolidity/SolidityEndToEndTest.cpp index 212c5111..52ce65f1 100644 --- a/test/libsolidity/SolidityEndToEndTest.cpp +++ b/test/libsolidity/SolidityEndToEndTest.cpp @@ -7612,6 +7612,33 @@ BOOST_AUTO_TEST_CASE(inline_assembly_function_call2) BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(1), u256(2), u256(7), u256(0x10))); } +BOOST_AUTO_TEST_CASE(inline_assembly_embedded_function_call) +{ + char const* sourceCode = R"( + contract C { + function f() { + assembly { + let d := 0x10 + function asmfun(a, b, c) -> x, y, z { + x := g(a) + function g(r) -> s { s := mul(r, r) } + y := g(b) + z := 7 + } + let a1, b1, c1 := asmfun(1, 2, 3) + mstore(0x00, a1) + mstore(0x20, b1) + mstore(0x40, c1) + mstore(0x60, d) + return(0, 0x80) + } + } + } + )"; + compileAndRun(sourceCode, 0, "C"); + BOOST_CHECK(callContractFunction("f()") == encodeArgs(u256(1), u256(4), u256(7), u256(0x10))); +} + BOOST_AUTO_TEST_CASE(inline_assembly_switch) { char const* sourceCode = R"( -- cgit From 80227af08a72e37e35a0a8bfacadc1c201f1d114 Mon Sep 17 00:00:00 2001 From: Alex Beregszaszi Date: Fri, 9 Jun 2017 10:43:33 +0100 Subject: Add test for two functions calling eachother --- test/libsolidity/InlineAssembly.cpp | 1 + 1 file changed, 1 insertion(+) (limited to 'test/libsolidity') diff --git a/test/libsolidity/InlineAssembly.cpp b/test/libsolidity/InlineAssembly.cpp index e9e310d0..3db07184 100644 --- a/test/libsolidity/InlineAssembly.cpp +++ b/test/libsolidity/InlineAssembly.cpp @@ -507,6 +507,7 @@ BOOST_AUTO_TEST_CASE(function_calls) BOOST_CHECK(successAssemble("{ function f(a, b) -> x, y { x := b y := a } let a, b := f(2, 3) }")); BOOST_CHECK(successAssemble("{ function rec(a) { rec(sub(a, 1)) } rec(2) }")); BOOST_CHECK(successAssemble("{ let r := 2 function f() -> x, y { x := 1 y := 2} let a, b := f() b := r }")); + BOOST_CHECK(successAssemble("{ function f() { g() } function g() { f() } }")); } BOOST_AUTO_TEST_CASE(embedded_functions) -- cgit