-
Notifications
You must be signed in to change notification settings - Fork 995
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Add support for storage pointer info through phi (storage parameter) - Update read/write analysis to be more robust with storage parameter
- Loading branch information
Showing
7 changed files
with
283 additions
and
44 deletions.
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
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
20 changes: 20 additions & 0 deletions
20
tests/unit/slithir/test_data/variable_read_write_storage_pointer/local_alias.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,20 @@ | ||
contract Test{ | ||
|
||
struct S{ | ||
uint a; | ||
} | ||
|
||
S s0; | ||
S s1; | ||
|
||
function test() public{ | ||
S storage s_local = s0; | ||
|
||
if(true){ | ||
s_local = s1; | ||
} | ||
|
||
s_local.a = 10; | ||
|
||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/unit/slithir/test_data/variable_read_write_storage_pointer/with_library.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,47 @@ | ||
// Simplified from https://github.com/crytic/slither/issues/2598 | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
library Roles { | ||
struct Role { | ||
mapping (address => bool) bearer; | ||
} | ||
|
||
/** | ||
* @dev Give an account access to this role. | ||
*/ | ||
function add(Role storage role, address account) internal { | ||
require(!has(role, account), "Roles: account already has role"); | ||
role.bearer[account] = true; | ||
} | ||
|
||
function has(Role storage role, address account) internal view returns (bool) { | ||
require(account != address(0), "Roles: account is the zero address"); | ||
return role.bearer[account]; | ||
} | ||
|
||
} | ||
|
||
contract Context { | ||
|
||
function _msgSender() internal view returns (address payable) { | ||
return payable(msg.sender); | ||
} | ||
|
||
} | ||
|
||
|
||
contract MinterRole is Context { | ||
using Roles for Roles.Role; | ||
|
||
Roles.Role private _minters; | ||
|
||
function addMinter(address account) public { | ||
_addMinter(account); | ||
} | ||
|
||
function _addMinter(address account) internal { | ||
_minters.add(account); | ||
} | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
tests/unit/slithir/test_data/variable_read_write_storage_pointer/without_library.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,35 @@ | ||
// Simplified from https://github.com/crytic/slither/issues/2598 | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
contract MinterRole { | ||
|
||
struct Role { | ||
mapping (address => bool) bearer; | ||
} | ||
|
||
|
||
/** | ||
* @dev Give an account access to this role. | ||
*/ | ||
function add(Role storage role, address account) internal { | ||
require(!has(role, account), "Roles: account already has role"); | ||
role.bearer[account] = true; | ||
} | ||
|
||
function has(Role storage role, address account) internal view returns (bool) { | ||
require(account != address(0), "Roles: account is the zero address"); | ||
return role.bearer[account]; | ||
} | ||
|
||
Role private _minters; | ||
|
||
function addMinter(address account) public { | ||
_addMinter(account); | ||
} | ||
|
||
function _addMinter(address account) internal { | ||
add(_minters, account); | ||
} | ||
|
||
} |
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,45 @@ | ||
from pathlib import Path | ||
from slither import Slither | ||
|
||
|
||
TEST_DATA_DIR = Path(__file__).resolve().parent / "test_data" | ||
|
||
|
||
def test_local_alias(solc_binary_path) -> None: | ||
|
||
solc_path = solc_binary_path("0.8.18") | ||
slither = Slither( | ||
Path(TEST_DATA_DIR, "variable_read_write_storage_pointer/local_alias.sol").as_posix(), | ||
solc=solc_path, | ||
) | ||
|
||
contract = slither.get_contract_from_name("Test")[0] | ||
|
||
test = contract.get_function_from_full_name("test()") | ||
|
||
s0 = contract.get_state_variable_from_name("s0") | ||
s1 = contract.get_state_variable_from_name("s1") | ||
|
||
assert set(test.state_variables_written) == {s0, s1} | ||
|
||
|
||
def test_parameter_no_library(solc_binary_path) -> None: | ||
|
||
solc_path = solc_binary_path("0.8.18") | ||
slither = Slither( | ||
Path(TEST_DATA_DIR, "variable_read_write_storage_pointer/without_library.sol").as_posix(), | ||
solc=solc_path, | ||
) | ||
|
||
contract = slither.get_contract_from_name("MinterRole")[0] | ||
|
||
print(contract.available_functions_as_dict()) | ||
add = contract.get_function_from_full_name("add(MinterRole.Role,address)") | ||
has = contract.get_function_from_full_name("has(MinterRole.Role,address)") | ||
|
||
minter = contract.get_state_variable_from_name("_minters") | ||
|
||
assert set(add.state_variables_written) == {minter} | ||
assert set(add.state_variables_read) == {minter} | ||
assert set(has.state_variables_written) == set() | ||
assert set(has.state_variables_read) == {minter} |