2019-01-29 09:51:25 +00:00
|
|
|
/*
|
|
|
|
This file is part of solidity.
|
|
|
|
|
|
|
|
solidity is free software: you can redistribute it and/or modify
|
|
|
|
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,
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* Unit tests for the compilability checker.
|
|
|
|
*/
|
|
|
|
|
2020-01-14 16:48:17 +00:00
|
|
|
#include <test/Common.h>
|
2019-01-29 09:51:25 +00:00
|
|
|
|
|
|
|
#include <test/libyul/Common.h>
|
|
|
|
#include <libyul/backends/evm/EVMDialect.h>
|
|
|
|
|
|
|
|
#include <libyul/CompilabilityChecker.h>
|
|
|
|
|
2020-01-14 14:55:31 +00:00
|
|
|
#include <boost/test/unit_test.hpp>
|
2019-01-29 09:51:25 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-12-23 15:50:30 +00:00
|
|
|
namespace solidity::yul::test
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
string check(string const& _input)
|
|
|
|
{
|
2019-07-09 15:23:14 +00:00
|
|
|
Object obj;
|
|
|
|
std::tie(obj.code, obj.analysisInfo) = yul::test::parse(_input, false);
|
|
|
|
BOOST_REQUIRE(obj.code);
|
2020-07-02 10:48:20 +00:00
|
|
|
auto functions = CompilabilityChecker(EVMDialect::strictAssemblyForEVM(solidity::test::CommonOptions::get().evmVersion()), obj, true).stackDeficit;
|
2019-01-29 09:51:25 +00:00
|
|
|
string out;
|
|
|
|
for (auto const& function: functions)
|
|
|
|
out += function.first.str() + ": " + to_string(function.second) + " ";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_SUITE(CompilabilityChecker)
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(smoke_test)
|
|
|
|
{
|
|
|
|
string out = check("{}");
|
|
|
|
BOOST_CHECK_EQUAL(out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(simple_function)
|
|
|
|
{
|
|
|
|
string out = check("{ function f(a, b) -> x, y { x := a y := b } }");
|
|
|
|
BOOST_CHECK_EQUAL(out, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(many_variables_few_uses)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> x, y {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(x, r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
|
|
|
BOOST_CHECK_EQUAL(out, "f: 4 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(many_variables_many_uses)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> x, y {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
|
|
|
BOOST_CHECK_EQUAL(out, "f: 10 ");
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(many_return_variables_unused_arguments)
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
|
|
|
}
|
|
|
|
})");
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_CHECK_EQUAL(out, "f: 3 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(many_return_variables_used_arguments)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
|
|
|
r1 := 0
|
|
|
|
sstore(a, b)
|
|
|
|
}
|
|
|
|
})");
|
2019-01-29 09:51:25 +00:00
|
|
|
BOOST_CHECK_EQUAL(out, "f: 5 ");
|
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(multiple_functions_used_arguments)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
|
|
|
r1 := 0
|
|
|
|
sstore(a, b)
|
|
|
|
}
|
|
|
|
function g(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19) -> x, y {
|
|
|
|
x := 0
|
|
|
|
sstore(r1, r2)
|
|
|
|
}
|
|
|
|
function h(x) {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
|
|
|
BOOST_CHECK_EQUAL(out, "h: 9 g: 3 f: 5 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(multiple_functions_unused_arguments)
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function f(a, b) -> r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19 {
|
|
|
|
}
|
|
|
|
function g(r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19) -> x, y {
|
|
|
|
}
|
|
|
|
function h(x) {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_CHECK_EQUAL(out, "h: 9 f: 3 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(nested_used_arguments)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function h(x) {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
function f(a, b) -> t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19 {
|
|
|
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
|
|
|
w := v
|
|
|
|
sstore(s1, s2)
|
|
|
|
}
|
|
|
|
t1 := t2
|
|
|
|
sstore(a, b)
|
|
|
|
}
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
|
|
|
BOOST_CHECK_EQUAL(out, "h: 9 g: 3 f: 5 ");
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(nested_unused_arguments)
|
2019-01-29 09:51:25 +00:00
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
function h(x) {
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
function f(a, b) -> t1, t2, t3, t4, t5, t6, t7, t8, t9, t10, t11, t12, t13, t14, t15, t16, t17, t18, t19 {
|
|
|
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
}
|
|
|
|
})");
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_CHECK_EQUAL(out, "h: 9 f: 3 ");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(also_in_outer_block_used_arguments)
|
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
let x := 0
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
|
|
|
w := v
|
|
|
|
sstore(s1, s2)
|
|
|
|
}
|
|
|
|
})");
|
|
|
|
BOOST_CHECK_EQUAL(out, "g: 3 : 9 ");
|
2019-01-29 09:51:25 +00:00
|
|
|
}
|
|
|
|
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_AUTO_TEST_CASE(also_in_outer_block_unused_arguments)
|
2019-01-31 14:06:20 +00:00
|
|
|
{
|
|
|
|
string out = check(R"({
|
|
|
|
let x := 0
|
|
|
|
let r1 := 0
|
|
|
|
let r2 := 0
|
|
|
|
let r3 := 0
|
|
|
|
let r4 := 0
|
|
|
|
let r5 := 0
|
|
|
|
let r6 := 0
|
|
|
|
let r7 := 0
|
|
|
|
let r8 := 0
|
|
|
|
let r9 := 0
|
|
|
|
let r10 := 0
|
|
|
|
let r11 := 0
|
|
|
|
let r12 := 0
|
|
|
|
let r13 := 0
|
|
|
|
let r14 := 0
|
|
|
|
let r15 := 0
|
|
|
|
let r16 := 0
|
|
|
|
let r17 := 0
|
|
|
|
let r18 := 0
|
|
|
|
x := add(add(add(add(add(add(add(add(add(add(add(add(x, r12), r11), r10), r9), r8), r7), r6), r5), r4), r3), r2), r1)
|
|
|
|
function g(s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19) -> w, v {
|
|
|
|
}
|
|
|
|
})");
|
2021-02-04 20:52:07 +00:00
|
|
|
BOOST_CHECK_EQUAL(out, ": 9 ");
|
2019-01-31 14:06:20 +00:00
|
|
|
}
|
|
|
|
|
2019-01-29 09:51:25 +00:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|
|
|
|
|
|
|
|
}
|