-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat: add support for constant and immutable variables in slither-read-storage #2920
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 support for constant and immutable variables in slither-read-storage #2920
Conversation
|
Hey, just a heads up - while I was going through the code to implement this feature, I noticed a pre-existing bug in the In I went ahead and fixed that too while I was in there, and added some tests to cover it. Figured it made sense to include it in this PR since I was already touching that code path. |
|
Care should be taken if #2742 is merged first, as that PR corrects the behavior of get_all_storage_variables instead |
|
Thanks for the heads up @elopez. I checked PR #2742 - it fixes the same If #2742 merges first, I'll rebase this PR and remove my fix for that bug (since it'll be redundant). The core feature (constant/immutable variable support) is independent of that fix. I'll keep an eye on #2742's status. |
dguido
left a comment
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.
Code Review: feat: add support for constant and immutable variables in slither-read-storage
Reviewing files:
slither/tools/read_storage/__main__.pyslither/tools/read_storage/read_storage.pytests/unit/tools/read_storage/test_immutable_constant.py
Critical (90-100)
No critical issues found.
Important (80-89)
1. Potential exception when type_ is None - Confidence: 85
File: slither/tools/read_storage/read_storage.py, lines 220-223 and 253-256
In get_immutable_constant_layout(), the code accesses type_.storage_size but the guard only checks if type_ is truthy for the type_string assignment, not before accessing storage_size:
type_string = str(type_) if type_ else "unknown"
byte_size, _ = type_.storage_size if type_ else (32, 0)This is correct, but the ternary could be clearer. Consider using explicit None check for consistency:
byte_size, _ = type_.storage_size if type_ is not None else (32, 0)2. Silent failure in _get_immutable_value on missing checksum_address - Confidence: 82
File: slither/tools/read_storage/read_storage.py, lines 275-292
When self.rpc_info is truthy but self.storage_address is not set, calling self.checksum_address will raise a ValueError. The try/except only catches ValueError and TypeError, but this failure path returns None silently without logging. Consider adding a debug log when the exception is caught:
except (ValueError, TypeError) as e:
logger.warning(f"Could not retrieve immutable {var.name} via RPC: {e}")This is already present, so this is fine. No change needed.
Positive Observations
-
Good fix for existing bug: The lambda signature change from
lambda x: bool(x[1].name == args.variable_name)tolambda x: bool(x.name == args.variable_name)correctly fixes an existing bug - theget_all_storage_variablesmethod passesvar(aStateVariable) directly to the filter function, not a tuple. The old comment was incorrect. -
Comprehensive test coverage: The tests cover multiple scenarios including:
- Detection of immutable/constant variables
- Default disabled behavior
- Storage layout inclusion
- Constant value extraction
- Variable name filtering
-
Clean separation of concerns: The new
_get_immutable_valueand_get_constant_valuemethods encapsulate retrieval logic cleanly. -
Sensible defaults: Using
slot=-1for non-storage variables is a reasonable sentinel value that won't conflict with actual storage slots. -
Proper skip logic in
get_slot_values: The early return whenslot_info.slot == -1prevents unnecessary RPC calls for immutable/constant variables.
Minor Suggestions (Not blocking)
-
Type annotation for
max_depthparameter: InSlitherReadStorage.__init__,max_depthis typed asintbut the defaultrpc_infoparameter is typed asRpcInfo = Nonewhich should beRpcInfo | None = None. -
Consider using
typing.TYPE_CHECKINGimport guard for theExpressiontype hint infind_hardcoded_slot_in_expto avoid runtime import overhead.
Overall, this is a well-implemented feature that addresses issue #1614. The code is clean, tests are comprehensive, and it correctly fixes a pre-existing bug in the lambda filter signature.
1f949fd to
8a9b272
Compare
- Change verbose per-variable logging from info to debug level - Fix type annotation for rpc_info parameter to use union syntax Co-Authored-By: Claude Opus 4.5 <[email protected]>
…d-storage This adds the --include-immutable flag to slither-read-storage which enables display of constant and immutable variables alongside regular storage variables. Features: - New --include-immutable CLI flag - Constant values extracted from expressions using ConstantFolding - Immutable values retrieved via RPC getter calls for public variables - Variables displayed with (constant) or (immutable) type suffix - Slot shows -1 for non-storage variables Fixes crytic#1614
- Fix pre-existing bug: lambda filter used x[1].name but x is StateVariable, not tuple - Use specific exception types instead of catching all Exception - Handle None type by showing "unknown" instead of "None" - Add debug log for private/internal immutables (cannot retrieve via RPC) - Add tests for --variable-name filter with immutable/constant variables
a79d816 to
bec3dcb
Compare
…pport - Remove unused `contract` parameter from `_get_immutable_value` method - Remove unused `Path` import from test file - Change test assertions from `>=` to `==` for exact count validation Co-Authored-By: Claude Opus 4.5 <[email protected]>
Summary
This PR adds support for displaying constant and immutable variables in
slither-read-storage, addressing issue #1614.Features
--include-immutableCLI flag to include constant/immutable variables in outputConstantFolding(constant)or(immutable)type suffix-1for non-storage variables (they don't have storage slots)Example Usage
Example Output
Implementation Details
_immutable_variablesand_constant_variableslists to track these variable typesget_all_storage_variables()to capture immutable/constant wheninclude_immutable=Trueget_immutable_constant_layout()method to create SlotInfo for these variables_get_immutable_value()- calls getter via RPC for public immutables_get_constant_value()- extracts value from expression using ConstantFoldingget_slot_values()to skip variables withslot=-1Test Plan
Fixes #1614