mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
Merge pull request #12620 from ethereum/assemblyAnnotation
Memory-safety annotation for inline assembly.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
@@ -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;
|
||||
};
|
||||
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -0,0 +1,10 @@
|
||||
contract C {
|
||||
function f() external pure {
|
||||
/// @solidity memory-safe-assembly
|
||||
assembly {}
|
||||
assembly {}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// :C(creation) true
|
||||
// :C(runtime) true
|
||||
@@ -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
|
||||
@@ -0,0 +1,4 @@
|
||||
contract C {}
|
||||
// ----
|
||||
// :C(creation) true
|
||||
// :C(runtime) true
|
||||
@@ -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
|
||||
@@ -0,0 +1,8 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
assembly { mstore(0,0) }
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// :C(creation) true
|
||||
// :C(runtime) false
|
||||
@@ -0,0 +1,11 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
bytes memory x;
|
||||
assembly {
|
||||
x := 0
|
||||
}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// :C(creation) true
|
||||
// :C(runtime) false
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
/// @test test
|
||||
assembly {}
|
||||
/// @solidity test
|
||||
assembly {}
|
||||
/// @param
|
||||
assembly {}
|
||||
}
|
||||
}
|
||||
// ----
|
||||
// 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.
|
||||
@@ -0,0 +1,6 @@
|
||||
contract C {
|
||||
function f() public pure {
|
||||
// @solidity memory-safe-assembly
|
||||
assembly {}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user