This commit is contained in:
Daniel Kirchner 2022-02-10 17:57:43 +01:00
parent ad13062978
commit 6b6e163be5
25 changed files with 354 additions and 14 deletions

View File

@ -83,6 +83,8 @@ set(libsolidity_sources
libsolidity/InlineAssembly.cpp
libsolidity/LibSolc.cpp
libsolidity/Metadata.cpp
libsolidity/MemoryGuardTest.cpp
libsolidity/MemoryGuardTest.h
libsolidity/SemanticTest.cpp
libsolidity/SemanticTest.h
libsolidity/SemVerMatcher.cpp

View File

@ -22,6 +22,7 @@
#include <test/libsolidity/ABIJsonTest.h>
#include <test/libsolidity/ASTJSONTest.h>
#include <test/libsolidity/GasTest.h>
#include <test/libsolidity/MemoryGuardTest.h>
#include <test/libsolidity/SyntaxTest.h>
#include <test/libsolidity/SemanticTest.h>
#include <test/libsolidity/SMTCheckerTest.h>
@ -74,6 +75,7 @@ Testsuite const g_interactiveTestsuites[] = {
{"JSON ABI", "libsolidity", "ABIJson", false, false, &ABIJsonTest::create},
{"SMT Checker", "libsolidity", "smtCheckerTests", true, false, &SMTCheckerTest::create},
{"Gas Estimates", "libsolidity", "gasTests", false, false, &GasTest::create},
{"Memory Guard Tests", "libsolidity", "memoryGuardTests", false, false, &MemoryGuardTest::create},
{"Ewasm Translation", "libyul", "ewasmTranslationTests", false, false, &yul::test::EwasmTranslationTest::create}
};

View File

@ -11,14 +11,15 @@ object "C_12" {
code {
{
/// @src 0:61:418 "contract C {..."
mstore(64, 128)
let _1 := memoryguard(0x80)
mstore(64, _1)
if callvalue() { revert(0, 0) }
/// @src 0:103:238 "assembly {..."
sstore(0, shl(180, 1))
/// @src 0:61:418 "contract C {..."
let _1 := datasize("C_12_deployed")
codecopy(128, dataoffset("C_12_deployed"), _1)
return(128, _1)
let _2 := datasize("C_12_deployed")
codecopy(_1, dataoffset("C_12_deployed"), _2)
return(_1, _2)
}
}
/// @use-src 0:"constant_optimizer_yul/input.sol"
@ -26,7 +27,7 @@ object "C_12" {
code {
{
/// @src 0:61:418 "contract C {..."
mstore(64, 128)
mstore(64, memoryguard(0x80))
if callvalue() { revert(0, 0) }
/// @src 0:279:410 "assembly {..."
sstore(0, 0x1000000000000000000000000000000000000000000000)

View File

@ -3,6 +3,6 @@ pragma solidity >=0.0.0;
pragma abicoder v2;
contract D {
constructor() { assembly {}}
constructor() { assembly { mstore(0,0) } }
function f() public pure {}
}

View File

@ -10,9 +10,12 @@ Optimized IR:
object "D_12" {
code {
{
/// @src 0:82:161 "contract D {..."
/// @src 0:82:175 "contract D {..."
mstore(64, 128)
if callvalue() { revert(0, 0) }
/// @src 0:115:139 "assembly { mstore(0,0) }"
mstore(0, 0)
/// @src 0:82:175 "contract D {..."
let _1 := datasize("D_12_deployed")
codecopy(128, dataoffset("D_12_deployed"), _1)
return(128, _1)
@ -22,7 +25,7 @@ object "D_12" {
object "D_12_deployed" {
code {
{
/// @src 0:82:161 "contract D {..."
/// @src 0:82:175 "contract D {..."
let _1 := memoryguard(0x80)
mstore(64, _1)
if iszero(lt(calldatasize(), 4))

View File

@ -4,6 +4,6 @@ pragma abicoder v2;
contract D {
function f() public pure {
assembly {}
assembly { mstore(0,0) }
}
}

View File

@ -10,7 +10,7 @@ Optimized IR:
object "D_8" {
code {
{
/// @src 0:82:153 "contract D {..."
/// @src 0:82:166 "contract D {..."
let _1 := memoryguard(0x80)
mstore(64, _1)
if callvalue() { revert(0, 0) }
@ -23,7 +23,7 @@ object "D_8" {
object "D_8_deployed" {
code {
{
/// @src 0:82:153 "contract D {..."
/// @src 0:82:166 "contract D {..."
mstore(64, 128)
if iszero(lt(calldatasize(), 4))
{
@ -32,6 +32,8 @@ object "D_8" {
{
if callvalue() { revert(_1, _1) }
if slt(add(calldatasize(), not(3)), _1) { revert(_1, _1) }
/// @src 0:134:158 "assembly { mstore(0,0) }"
mstore(/** @src 0:82:166 "contract D {..." */ _1, _1)
return(128, _1)
}
}

View File

@ -0,0 +1,78 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <test/libsolidity/MemoryGuardTest.h>
#include <test/libyul/Common.h>
#include <libsolidity/codegen/ir/Common.h>
#include <libsolutil/Algorithms.h>
#include <libyul/Object.h>
#include <libyul/backends/evm/EVMDialect.h>
#include <libyul/optimiser/FunctionCallFinder.h>
#include <fstream>
#include <memory>
#include <stdexcept>
using namespace std;
using namespace solidity;
using namespace solidity::util;
using namespace solidity::util::formatting;
using namespace solidity::langutil;
using namespace solidity::frontend;
using namespace solidity::frontend::test;
using namespace yul;
TestCase::TestResult MemoryGuardTest::run(ostream& _stream, string const& _linePrefix, bool _formatted)
{
compiler().reset();
compiler().setSources(StringMap{{"", m_source}});
compiler().setViaIR(true);
compiler().setOptimiserSettings(OptimiserSettings::none());
if (!compiler().compile())
return TestResult::FatalError;
m_obtainedResult.clear();
for (string contractName: compiler().contractNames())
{
ErrorList errors;
auto [object, analysisInfo] = yul::test::parse(
compiler().yulIR(contractName),
EVMDialect::strictAssemblyForEVMObjects({}),
errors
);
if (!object || !analysisInfo || Error::containsErrors(errors))
{
AnsiColorized(_stream, _formatted, {formatting::BOLD, formatting::RED}) << _linePrefix << "Error parsing IR." << endl;
return TestResult::FatalError;
}
auto handleObject = [&](std::string const& _kind, Object const& _object) {
m_obtainedResult += contractName + "(" + _kind + ") " + (FunctionCallFinder::run(
*_object.code,
"memoryguard"_yulstring
).empty() ? "false" : "true") + "\n";
};
handleObject("creation", *object);
size_t deployedIndex = object->subIndexByName.at(
YulString(IRNames::deployedObject(compiler().contractDefinition(contractName)))
);
handleObject("runtime", dynamic_cast<Object const&>(*object->subObjects[deployedIndex]));
}
return checkResult(_stream, _linePrefix, _formatted);
}

View File

@ -0,0 +1,53 @@
/*
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/>.
*/
// SPDX-License-Identifier: GPL-3.0
#pragma once
#include <test/libsolidity/AnalysisFramework.h>
#include <test/TestCase.h>
#include <test/CommonSyntaxTest.h>
#include <liblangutil/Exceptions.h>
#include <libsolutil/AnsiColorized.h>
#include <iosfwd>
#include <string>
#include <vector>
#include <utility>
namespace solidity::frontend::test
{
using solidity::test::SyntaxTestError;
class MemoryGuardTest: public AnalysisFramework, public TestCase
{
public:
static std::unique_ptr<TestCase> create(Config const& _config)
{
return std::make_unique<MemoryGuardTest>(_config.filename);
}
MemoryGuardTest(std::string const& _filename): TestCase(_filename)
{
m_source = m_reader.source();
m_expectation = m_reader.simpleExpectations();
}
TestResult run(std::ostream& _stream, std::string const& _linePrefix = "", bool _formatted = false) override;
};
}

View File

@ -0,0 +1,17 @@
contract C {
constructor() {
uint256 x;
assembly { x := 0 }
f();
}
function f() internal pure {
/// @solidity memory-safe-assembly
assembly { mstore(0, 0) }
}
function g() public pure {
assembly { mstore(0, 0) }
}
}
// ----
// :C(creation) true
// :C(runtime) false

View File

@ -0,0 +1,17 @@
contract C {
constructor() {
uint256 x;
assembly { x := 0 }
f();
}
function f() internal pure {
assembly { mstore(0, 0) }
}
function g() public pure {
/// @solidity memory-safe-assembly
assembly { mstore(0, 0) }
}
}
// ----
// :C(creation) false
// :C(runtime) true

View File

@ -0,0 +1,29 @@
function safe() pure returns (uint256 x) {
assembly { x := 42 }
/// @solidity memory-safe-assembly
assembly { mstore(0, 0) }
}
function unsafe() pure returns (uint256 x) {
assembly { pop(mload(0)) }
}
contract C {
constructor() {
unsafe();
}
function f() public pure {
safe();
}
}
contract D {
constructor() {
safe();
}
function f() public pure {
unsafe();
}
}
// ----
// :C(creation) false
// :C(runtime) true
// :D(creation) true
// :D(runtime) false

View File

@ -0,0 +1,17 @@
contract C {
constructor() {
/// @solidity memory-safe-assembly a memory-safe-assembly
assembly { mstore(0, 0) }
}
function f() internal pure {
/// @solidity a memory-safe-assembly
assembly { mstore(0, 0) }
/// @solidity a
/// memory-safe-assembly
/// b
assembly { mstore(0, 0) }
}
}
// ----
// :C(creation) true
// :C(runtime) true

View File

@ -0,0 +1,25 @@
contract C {
constructor(uint256 x) {
assembly { x := 4 }
/// @solidity memory-safe-assembly
assembly { mstore(0, 0) }
}
function f() public pure {
assembly { mstore(0,0) }
}
}
contract D {
constructor() {
assembly { mstore(0,0) }
}
function f(uint256 x) public pure {
assembly { x := 4 }
/// @solidity memory-safe-assembly
assembly { mstore(0, 0) }
}
}
// ----
// :C(creation) true
// :C(runtime) false
// :D(creation) false
// :D(runtime) true

View File

@ -0,0 +1,10 @@
contract C {
function f() external pure {
/// @solidity memory-safe-assembly
assembly {}
assembly {}
}
}
// ----
// :C(creation) true
// :C(runtime) true

View File

@ -0,0 +1,10 @@
contract C {
function f() external pure {
/// @solidity memory-safe-assembly
assembly {}
assembly { mstore(0,0) }
}
}
// ----
// :C(creation) true
// :C(runtime) false

View File

@ -0,0 +1,4 @@
contract C {}
// ----
// :C(creation) true
// :C(runtime) true

View File

@ -0,0 +1,8 @@
contract C {
function f(uint256 x, uint256 y) public pure returns (uint256 z){
assembly { z := add(x, y) }
}
}
// ----
// :C(creation) true
// :C(runtime) true

View File

@ -0,0 +1,8 @@
contract C {
function f() public pure {
assembly { mstore(0,0) }
}
}
// ----
// :C(creation) true
// :C(runtime) false

View File

@ -0,0 +1,11 @@
contract C {
function f() public pure {
bytes memory x;
assembly {
x := 0
}
}
}
// ----
// :C(creation) true
// :C(runtime) false

View File

@ -50,7 +50,7 @@ contract test {
// compileViaYul: also
// ----
// constructor()
// gas irOptimized: 1790188
// gas irOptimized: 1792108
// gas legacy: 2250130
// gas legacyOptimized: 1746528
// div(uint256,uint256): 3141592653589793238, 88714123 -> 35412542528203691288251815328

View File

@ -9,6 +9,6 @@ contract C {
}
}
// ----
// Warning 6269: (60-71): Unexpected natspec tag in inline assembly: test
// Warning 6269: (60-71): Unexpected NatSpec tag "test" with value "test" in inline assembly.
// Warning 8787: (95-106): Unexpected value for @solidity tag in inline assembly: test
// Warning 7828: (122-133): Inline assembly has invalid natspec documentation.
// Warning 7828: (122-133): Inline assembly has invalid NatSpec documentation.

View File

@ -0,0 +1,23 @@
function f() pure {
/// @unrelated bogus-value
/// @before bogus-value
///
/// @solidity a memory-safe-assembly b c
/// d
/// @after bogus-value
assembly {}
/// @solidity memory-safe-assembly a a a
/// memory-safe-assembly
assembly {}
}
// ----
// Warning 6269: (189-200): Unexpected NatSpec tag "after" with value "bogus-value" in inline assembly.
// Warning 6269: (189-200): Unexpected NatSpec tag "before" with value "bogus-value" in inline assembly.
// Warning 8787: (189-200): Unexpected value for @solidity tag in inline assembly: a
// Warning 8787: (189-200): Unexpected value for @solidity tag in inline assembly: b
// Warning 8787: (189-200): Unexpected value for @solidity tag in inline assembly: c
// Warning 8787: (189-200): Unexpected value for @solidity tag in inline assembly: d
// Warning 8787: (289-300): Unexpected value for @solidity tag in inline assembly: a
// Warning 4377: (289-300): Value for @solidity tag in inline assembly specified multiple times: a
// Warning 4377: (289-300): Value for @solidity tag in inline assembly specified multiple times: memory-safe-assembly

View File

@ -0,0 +1,19 @@
function f() pure {
/// @unrelated bogus-value
/// @before
///
/// @solidity a memory-safe-assembly b c
/// d
/// @after bogus-value
assembly {}
/// @solidity memory-safe-assembly a a a
/// memory-safe-assembly
assembly {}
}
// ----
// Warning 6269: (177-188): Unexpected NatSpec tag "after" with value "bogus-value" in inline assembly.
// Warning 6269: (177-188): Unexpected NatSpec tag "before" with value "@solidity a memory-safe-assembly b c d" in inline assembly.
// Warning 8787: (277-288): Unexpected value for @solidity tag in inline assembly: a
// Warning 4377: (277-288): Value for @solidity tag in inline assembly specified multiple times: a
// Warning 4377: (277-288): Value for @solidity tag in inline assembly specified multiple times: memory-safe-assembly

View File

@ -23,6 +23,7 @@ add_executable(isoltest
../libsolidity/util/TestFileParser.cpp
../libsolidity/util/TestFunctionCall.cpp
../libsolidity/GasTest.cpp
../libsolidity/MemoryGuardTest.cpp
../libsolidity/SyntaxTest.cpp
../libsolidity/SemanticTest.cpp
../libsolidity/AnalysisFramework.cpp