-
Notifications
You must be signed in to change notification settings - Fork 989
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
feat: add detector for unused custom errors #2565
Open
gvladika
wants to merge
19
commits into
crytic:dev
Choose a base branch
from
gvladika:unused-errors-detector
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
08b4795
Initial skeleton for unused custom errors detector
gvladika 617fe8b
Add function that checks if custom error is unused
gvladika 0edd70c
Add first sol contract for testing unused custom errors
gvladika 82dfd33
Refactor and include errors defined outside of contracts
gvladika a6b84cf
Change format
gvladika 0efbf5c
Add filename to the output
gvladika 53e896c
Simplify fetching the file name
gvladika acc542e
Add wiki docs
gvladika cacb154
Make test contract(s) more complete
gvladika c61e536
Lint
gvladika 3d68770
Move detector to another folder
gvladika 3c18062
Update comments
gvladika 415e65e
Remove leftovers
gvladika 4f7d0bc
Merge branch 'dev' into unused-errors-detector
gvladika b63e62a
Use sets to avoid duplicates
gvladika 44de39b
Use sorted list to get deterministic order in output
gvladika 1bdbecb
Merge branch 'dev' into unused-errors-detector
gvladika 9af60e6
internal_call.function is instance of SolidityCustomRevert
gvladika 65d6943
Merge branch 'dev' into unused-errors-detector
gvladika 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
There are no files selected for viewing
This file contains 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 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,94 @@ | ||
""" | ||
Module detecting unused custom errors | ||
""" | ||
from typing import List, Set | ||
from slither.core.declarations.custom_error import CustomError | ||
from slither.core.declarations.custom_error_top_level import CustomErrorTopLevel | ||
from slither.core.declarations.solidity_variables import SolidityCustomRevert | ||
from slither.detectors.abstract_detector import ( | ||
AbstractDetector, | ||
DetectorClassification, | ||
DETECTOR_INFO, | ||
) | ||
from slither.utils.output import Output | ||
|
||
|
||
class UnusedCustomErrors(AbstractDetector): | ||
""" | ||
Unused custom errors detector | ||
""" | ||
|
||
ARGUMENT = "unused-custom-errors" | ||
HELP = "Detects unused custom errors" | ||
IMPACT = DetectorClassification.INFORMATIONAL | ||
CONFIDENCE = DetectorClassification.HIGH | ||
|
||
WIKI = "https://github.com/crytic/slither/wiki/Detector-Documentation#unused-custom-errors" | ||
|
||
WIKI_TITLE = "Unused Custom Errors" | ||
WIKI_DESCRIPTION = "Declaring a custom error, but never using it might indicate a mistake." | ||
|
||
# region wiki_exploit_scenario | ||
WIKI_EXPLOIT_SCENARIO = """ | ||
```solidity | ||
contract A { | ||
error ZeroAddressNotAllowed(); | ||
|
||
address public owner; | ||
|
||
constructor(address _owner) { | ||
owner = _owner; | ||
} | ||
} | ||
``` | ||
Custom error `ZeroAddressNotAllowed` is declared but never used. It shall be either invoked where needed, or removed if there's no need for it. | ||
""" | ||
# endregion wiki_exploit_scenario = "" | ||
WIKI_RECOMMENDATION = "Remove the unused custom errors." | ||
|
||
def _detect(self) -> List[Output]: | ||
"""Detect unused custom errors""" | ||
declared_custom_errors: Set[CustomError] = set() | ||
custom_reverts: Set[SolidityCustomRevert] = set() | ||
unused_custom_errors: List[CustomError] = [] | ||
|
||
# Collect all custom errors defined in the contracts | ||
for contract in self.compilation_unit.contracts: | ||
for custom_error in contract.custom_errors: | ||
declared_custom_errors.add(custom_error) | ||
|
||
# Add custom errors defined outside of contracts | ||
for custom_error in self.compilation_unit.custom_errors: | ||
declared_custom_errors.add(custom_error) | ||
|
||
# Collect all custom errors invoked in revert statements | ||
for contract in self.compilation_unit.contracts: | ||
for function in contract.functions_and_modifiers: | ||
for internal_call in function.internal_calls: | ||
if isinstance(internal_call.function, SolidityCustomRevert): | ||
custom_reverts.add(internal_call.function) | ||
|
||
# Find unused custom errors | ||
for declared_error in declared_custom_errors: | ||
if not any( | ||
declared_error.name in custom_revert.name for custom_revert in custom_reverts | ||
): | ||
unused_custom_errors.append(declared_error) | ||
|
||
# Sort by error name | ||
unused_custom_errors = sorted(unused_custom_errors, key=lambda err: err.name) | ||
|
||
# Format results | ||
results = [] | ||
if len(unused_custom_errors) > 0: | ||
info: DETECTOR_INFO = ["The following unused error(s) should be removed:"] | ||
for custom_error in unused_custom_errors: | ||
file_scope = ( | ||
custom_error.file_scope | ||
if isinstance(custom_error, CustomErrorTopLevel) | ||
else custom_error.contract.file_scope | ||
) | ||
info += ["\n\t-", custom_error.full_name, " (", file_scope.filename.short, ")\n"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just use |
||
results.append(self.generate_result(info)) | ||
|
||
return results |
11 changes: 11 additions & 0 deletions
11
...ts/detectors__detector_UnusedCustomErrors_0_8_15_ContractToTestForCustomErrors_sol__0.txt
This file contains 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,11 @@ | ||
The following unused error(s) should be removed: | ||
-UnusedErrorLibConsumerErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ErrorLibConsumer.sol) | ||
|
||
-UnusedLibError() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/CustomErrorsLib.sol) | ||
|
||
-UnusedParentErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol) | ||
|
||
-UnusedParentErrWithArg(uint256) (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol) | ||
|
||
-UnusedTopLevelErr() (tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol) | ||
|
41 changes: 41 additions & 0 deletions
41
tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol
This file contains 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,41 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.15; | ||
|
||
error UnusedTopLevelErr(); | ||
error UsedTopLevelErr(); | ||
|
||
import "./ErrorLibConsumer.sol"; | ||
|
||
contract ParentContract { | ||
error UnusedParentErr(); | ||
error UnusedParentErrWithArg(uint256 x); | ||
error UsedParentErr(address x); | ||
error UsedParentErrInChild(uint256 x); | ||
|
||
constructor() { | ||
new ErrorLibConsumer(); | ||
} | ||
|
||
function x() public view { | ||
uint256 d = 7; | ||
if (msg.sender == address(0)) { | ||
d = 100; | ||
revert UsedParentErr(msg.sender); | ||
} | ||
} | ||
} | ||
|
||
contract ContractToTestForCustomErrors is ParentContract { | ||
function y() public { | ||
address f = msg.sender; | ||
(bool s,) = f.call(""); | ||
if (!s) { | ||
revert UsedParentErrInChild(1); | ||
} | ||
revert UsedTopLevelErr(); | ||
} | ||
|
||
function z() public { | ||
revert Lib.UsedLibErrorB(8); | ||
} | ||
} |
Binary file added
BIN
+5.83 KB
...ectors/test_data/unused-custom-errors/0.8.15/ContractToTestForCustomErrors.sol-0.8.15.zip
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/CustomErrorsLib.sol
This file contains 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 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.15; | ||
|
||
library Lib { | ||
error UnusedLibError(); | ||
error UsedLibErrorA(); | ||
error UsedLibErrorB(uint256 x); | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/e2e/detectors/test_data/unused-custom-errors/0.8.15/ErrorLibConsumer.sol
This file contains 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 @@ | ||
// SPDX-License-Identifier: Unlicense | ||
pragma solidity 0.8.15; | ||
|
||
import "./CustomErrorsLib.sol"; | ||
|
||
contract ErrorLibConsumer { | ||
error UnusedErrorLibConsumerErr(); | ||
error UsedErrorLibConsumerErr(); | ||
|
||
constructor() { | ||
revert Lib.UsedLibErrorA(); | ||
} | ||
|
||
function a() public pure { | ||
revert UsedErrorLibConsumerErr(); | ||
} | ||
} |
This file contains 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
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.
Checking the name could have false negative. For example try adding this in the contract test. It won't get detected.
A better approach could be to have
custom_errors
with the actual CustomError instance instead of the name and then check here withdeclared_error == custom_revert
. When you are adding the custom error you can get the instance withinternal_call.function.custom_error