-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Memory-safety annotation for inline assembly. #12620
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e6848ca
Allow annotating inline assembly as memory-safe.
ekpyron 9bcfcc6
Inline assembly without memory effects is implicitly memory safe.
ekpyron dfb7bf2
Report memoryguard in stack too deep error.
ekpyron 62a997a
Documentation.
ekpyron ad13062
Patch external tests with a safe inline assembly annotation.
ekpyron 6b6e163
Tests.
ekpyron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
Tests.
- Loading branch information
commit 6b6e163be5adec74d39f3f8e39739b658988381c
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,6 @@ pragma abicoder v2; | |
|
|
||
| contract D { | ||
| function f() public pure { | ||
| assembly {} | ||
| assembly { mstore(0,0) } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| }; | ||
|
|
||
| } |
17 changes: 17 additions & 0 deletions
17
test/libsolidity/memoryGuardTests/constructor_safe_deploy_unsafe.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
17 changes: 17 additions & 0 deletions
17
test/libsolidity/memoryGuardTests/constructor_unsafe_deploy_safe.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
10 changes: 10 additions & 0 deletions
10
test/libsolidity/memoryGuardTests/safe_and_unmarked_empty.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| contract C { | ||
| function f() external pure { | ||
| /// @solidity memory-safe-assembly | ||
| assembly {} | ||
| assembly {} | ||
| } | ||
| } | ||
| // ---- | ||
| // :C(creation) true | ||
| // :C(runtime) true |
10 changes: 10 additions & 0 deletions
10
test/libsolidity/memoryGuardTests/safe_and_unmarked_unsafe.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| contract C {} | ||
| // ---- | ||
| // :C(creation) true | ||
| // :C(runtime) true |
8 changes: 8 additions & 0 deletions
8
test/libsolidity/memoryGuardTests/unmarked_but_no_memory_access.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure reading actually needs to be unsafe...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can relax it later, but I don't see why you would read if you don't write...