Skip to content

fix: stop _explore_functions from mutating cached node/IR lists - #3170

Open
monperrus wants to merge 1 commit into
crytic:masterfrom
monperrus:fix/explore-functions-aliasing
Open

monperrus wants to merge 1 commit into
crytic:masterfrom
monperrus:fix/explore-functions-aliasing

Conversation

@monperrus

Copy link
Copy Markdown

Summary

Function.all_nodes(), all_expressions() and all_slithir_operations() permanently append every callee's nodes, expressions and operations to the function's own nodes, expressions and slithir_operations.

Root cause

_explore_functions starts with values = f_new_values(self) and then extends it with values += f_new_values(f) for each callee. Most getters return a copy (list(self._vars_read) and so on), but nodes, expressions and slithir_operations return the cached list itself. For those three, values += extends the function's own cache in place.

After any all_* call on those three, code that iterates function.slithir_operations or function.nodes sees the callees' operations as if they belonged to the function. A callee's parameters then appear as unknown values in the caller, which breaks data-dependency reasoning done per function.

Fix

Copy the first list: values = list(f_new_values(self)).

Test

test_explore_does_not_mutate_own_lists (in tests/unit/core/test_explore_functions.py, reusing test_call_graph.sol) calls the three all_* methods on chainA() and checks that its own lists are unchanged. It fails on master and passes with the fix.

Other test runs:

  • tests/e2e/detectors: 384 passed.
  • tests/unit: 6 Vyper tests fail in my environment (no Vyper installed), the same 6 with and without this change.

🤖 Generated with Claude Code

https://claude.ai/code/session_016xNEyu4v9TDCcVaJyaHkJy

Function.nodes, expressions and slithir_operations return their cached
list without a copy. _explore_functions started from that list and
extended it with `values +=`, so calling all_nodes(),
all_expressions() or all_slithir_operations() appended every callee's
nodes/expressions/operations to the function's own list. Copy it first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016xNEyu4v9TDCcVaJyaHkJy
@monperrus
monperrus requested a review from smonicas as a code owner December 1, 2026 06:55
@DefiAudit0x

Copy link
Copy Markdown

Good catch — this is a real bug, and the copy is in the right place: the in-place extension happens in the values += f_new_values(f) loop, so copying the initial list is enough, and the callees' lists are never touched since += extends values, not the callee's list.

One correction to the comment, though: nodes already returns a copy (return list(self._nodes) in function.py), so all_nodes() could never pollute function.nodes — the aliasing is only in expressions and slithir_operations, which return the cached list itself. That means the chain_a.nodes == nodes assertion in the test passes with or without the fix; the expressions / slithir_operations assertions are the ones that actually pin the bug. This matters beyond list hygiene because detectors read these same getters directly, so a polluted cache would attribute callee IR to the caller.

Worth considering as a follow-up: make expressions and slithir_operations return copies like every other getter in this file, so _explore_functions doesn't have to defend against the aliasing at the call site. Not required for this PR — the fix as-is is correct and minimal.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants