From 533c898c5b95203855eb408fdeda4c896030f685 Mon Sep 17 00:00:00 2001 From: Anthony Cabrera Date: Thu, 3 Jul 2025 13:43:47 -0500 Subject: [PATCH 1/4] add feature to support TypeAliasTopLevel support in `read_storage` Signed-off-by: Anthony Cabrera --- slither/tools/read_storage/read_storage.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/slither/tools/read_storage/read_storage.py b/slither/tools/read_storage/read_storage.py index 3ccada844..51fc034ff 100644 --- a/slither/tools/read_storage/read_storage.py +++ b/slither/tools/read_storage/read_storage.py @@ -13,7 +13,7 @@ from web3.middleware import ExtraDataToPOAMiddleware from slither.core.declarations import Contract, Structure -from slither.core.solidity_types import ArrayType, ElementaryType, MappingType, UserDefinedType +from slither.core.solidity_types import ArrayType, ElementaryType, MappingType, UserDefinedType, TypeAliasTopLevel from slither.core.solidity_types.type import Type from slither.core.cfg.node import NodeType from slither.core.variables.state_variable import StateVariable @@ -581,8 +581,12 @@ def _find_struct_var_slot( size = 0 for var in elems: var_type = var.type - if isinstance(var_type, ElementaryType): - size = var_type.size + is_type_alias = isinstance(var_type, TypeAliasTopLevel) + if isinstance(var_type, ElementaryType) or is_type_alias: + if is_type_alias: + size, _ = var_type.storage_size + else: + size = var_type.size if size > (256 - offset): slot += 1 offset = 0 From fafa0f6d0ab56322f39fdcd0e8b24830cc2f0a1b Mon Sep 17 00:00:00 2001 From: Anthony Cabrera Date: Thu, 3 Jul 2025 13:56:44 -0500 Subject: [PATCH 2/4] add test Signed-off-by: Anthony Cabrera --- tests/tools/read-storage/test_data/typealiastest.sol | 8 ++++++++ tests/tools/read-storage/test_read_storage.py | 9 +++++++++ 2 files changed, 17 insertions(+) create mode 100644 tests/tools/read-storage/test_data/typealiastest.sol diff --git a/tests/tools/read-storage/test_data/typealiastest.sol b/tests/tools/read-storage/test_data/typealiastest.sol new file mode 100644 index 000000000..a0ed4958c --- /dev/null +++ b/tests/tools/read-storage/test_data/typealiastest.sol @@ -0,0 +1,8 @@ +type MyUint64 is uint64; +contract C { + struct S { + MyUint64 y; + MyUint64 z; + } + S s; +} diff --git a/tests/tools/read-storage/test_read_storage.py b/tests/tools/read-storage/test_read_storage.py index ec077547b..56b95f2c2 100644 --- a/tests/tools/read-storage/test_read_storage.py +++ b/tests/tools/read-storage/test_read_storage.py @@ -90,3 +90,12 @@ def test_read_storage(test_contract, storage_file, web3, ganache, solc_binary_pa f.write(str(change.t2)) assert not diff + +def test_type_alias() -> None: + """verify support for TypeAliasTop + """ + slither = Slither(Path(TEST_DATA_DIR, "typealiastest.sol").as_posix()) + c = slither.get_contract_from_name("C") + srs = SlitherReadStorage(c, 20) + srs.get_all_storage_variables() + srs.get_storage_layout() From 4e571dd09395e7c208cd7b265f73a2b64b61f5c3 Mon Sep 17 00:00:00 2001 From: Anthony Cabrera Date: Thu, 3 Jul 2025 14:32:53 -0500 Subject: [PATCH 3/4] run black formatter Signed-off-by: Anthony Cabrera --- slither/tools/read_storage/read_storage.py | 8 +++++++- tests/tools/read-storage/test_read_storage.py | 9 ++++++--- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/slither/tools/read_storage/read_storage.py b/slither/tools/read_storage/read_storage.py index 51fc034ff..b18d22a32 100644 --- a/slither/tools/read_storage/read_storage.py +++ b/slither/tools/read_storage/read_storage.py @@ -13,7 +13,13 @@ from web3.middleware import ExtraDataToPOAMiddleware from slither.core.declarations import Contract, Structure -from slither.core.solidity_types import ArrayType, ElementaryType, MappingType, UserDefinedType, TypeAliasTopLevel +from slither.core.solidity_types import ( + ArrayType, + ElementaryType, + MappingType, + UserDefinedType, + TypeAliasTopLevel, +) from slither.core.solidity_types.type import Type from slither.core.cfg.node import NodeType from slither.core.variables.state_variable import StateVariable diff --git a/tests/tools/read-storage/test_read_storage.py b/tests/tools/read-storage/test_read_storage.py index 56b95f2c2..58f7948a3 100644 --- a/tests/tools/read-storage/test_read_storage.py +++ b/tests/tools/read-storage/test_read_storage.py @@ -42,7 +42,10 @@ def deploy_contract(w3, ganache, contract_bin, contract_abi) -> Contract: # pylint: disable=too-many-locals @pytest.mark.parametrize( "test_contract, storage_file", - [("StorageLayout", "storage_layout"), ("UnstructuredStorageLayout", "unstructured_storage")], + [ + ("StorageLayout", "storage_layout"), + ("UnstructuredStorageLayout", "unstructured_storage"), + ], ) @pytest.mark.usefixtures("web3", "ganache") def test_read_storage(test_contract, storage_file, web3, ganache, solc_binary_path) -> None: @@ -91,9 +94,9 @@ def test_read_storage(test_contract, storage_file, web3, ganache, solc_binary_pa assert not diff + def test_type_alias() -> None: - """verify support for TypeAliasTop - """ + """verify support for TypeAliasTop""" slither = Slither(Path(TEST_DATA_DIR, "typealiastest.sol").as_posix()) c = slither.get_contract_from_name("C") srs = SlitherReadStorage(c, 20) From abd0c12c347b9d17db42366e485007544275c323 Mon Sep 17 00:00:00 2001 From: Anthony Cabrera Date: Thu, 3 Jul 2025 15:16:35 -0500 Subject: [PATCH 4/4] add solc binary version from pytest fixture Signed-off-by: Anthony Cabrera --- tests/tools/read-storage/test_read_storage.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/tools/read-storage/test_read_storage.py b/tests/tools/read-storage/test_read_storage.py index 58f7948a3..f3067fb32 100644 --- a/tests/tools/read-storage/test_read_storage.py +++ b/tests/tools/read-storage/test_read_storage.py @@ -95,8 +95,9 @@ def test_read_storage(test_contract, storage_file, web3, ganache, solc_binary_pa assert not diff -def test_type_alias() -> None: +def test_type_alias(solc_binary_path) -> None: """verify support for TypeAliasTop""" + solc_path = solc_binary_path(version="0.8.10") slither = Slither(Path(TEST_DATA_DIR, "typealiastest.sol").as_posix()) c = slither.get_contract_from_name("C") srs = SlitherReadStorage(c, 20)