diff --git a/slither/tools/read_storage/read_storage.py b/slither/tools/read_storage/read_storage.py index 3ccada8444..b18d22a325 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 +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 +587,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 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 0000000000..a0ed4958cf --- /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 ec077547b9..f3067fb32c 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: @@ -90,3 +93,13 @@ 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(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) + srs.get_all_storage_variables() + srs.get_storage_layout()