Repository navigation
Invariant-first protocol-math lens + second-order reentrancy - #2
Open
arena-ai-coding-agent[bot] wants to merge 1 commit into
Open
arena-ai-coding-agent[bot] wants to merge 1 commit into
arena-ai-coding-agent[bot] wants to merge 1 commit into
Conversation
Sink-hunting finds the line that looks wrong; invariants find the property
that is missing. This adds the other half of an expert review: the accounting
properties a protocol is supposed to preserve, and whether the source actually
asserts them anywhere.
self_tool/autonomous/invariants.py (new)
Five facts, each an unenforced property rather than a suspicious line:
missing_accrue_on_value_path value path prices against a rate it never
accrues (one level of helper indirection is
followed)
missing_share_invariant share math with no require/assert tying
totalSupply back to totalAssets
missing_k_invariant constant-product reserves mutated with no
non-decreasing k check and no _update hook
missing_health_check borrow/liquidate/withdraw moves collateral or
debt without a solvency check on that path
tstore_delete_poison transient slot outliving a delete/.pop of the
entry it describes
Two rules keep this honest. A conversion formula such as
assets * totalSupply() / totalAssets() is NOT an invariant: it prices a
share without constraining one, so only require/assert expressions count as
enforced properties. And a bare tstore is NOT poison unless the same function
also deletes or pops the storage that slot describes.
propose_hypotheses() emits Foundry handler sketches gated on the detected
protocol shape (INV-SHARE-BACKING, INV-EMPTY-VAULT, INV-K, INV-ACCRUE-FIRST,
INV-HEALTH, INV-VIEW-LOCK, INV-TSTORE-CLEAR, INV-SIG-BIND). These are
hypotheses to falsify, never claims of proof.
deep_scan.py
second_order_reentrancy: a public function that writes value state AFTER
delegating to a helper performing a low-level .call. Skipped when either the
public function or the helper carries a reentrancy guard, or when the public
function makes the call itself — that is first-order and already covered by
external_call_before_write. Plus a locator so findings point at the right fn.
Wiring
understand.py facts.update(invariant_facts(...)), hypotheses, arch notes
models.py ProtocolUnderstanding.hypotheses + to_dict + briefing
reason.py _DEEP_PLAYBOOK_FACTS for the five new AVs
report.py "Must-hold invariants (hypotheses, not proofs)" section
exploit_paths.py PATH-SECOND-ORDER-REENTRANCY
brain five AV playbooks + TAC-INVARIANT-FIRST
Constraints held: no new catalog detector IDs (still 161), RULE_VERSION stays
2.3.0, catalog <-> review-profile parity intact, no external scanner is
wrapped. Test suite 151 -> 175 (24 new in tests/test_invariants.py).
Co-authored-by: arena-agent <297053741+arena-agent@users.noreply.github.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
Sink-hunting finds the line that looks wrong. Invariants find the property that is missing. This PR adds the other half of an expert review: the accounting properties a protocol is supposed to preserve, and whether the source actually asserts them anywhere.
self_tool/autonomous/invariants.py(new)Five facts, each an unenforced property rather than a suspicious line:
missing_accrue_on_value_pathmissing_share_invariantrequire/asserttiestotalSupplyback tototalAssetsmissing_k_invariantkcheck and no Uniswap-style_updatehookmissing_health_checktstore_delete_poisondelete/.pop()of the entry it describesTwo rules keep this honest, both pinned by tests:
assets * totalSupply() / totalAssets()prices a share; it constrains nothing. Onlyrequire/assertexpressions count as enforced properties — so a vault whose only share-related construct is that formula still correctly reportsmissing_share_invariant.tstoreis not poison. It only fires when the same function alsodeletes or.pop()s the storage that slot describes. Normal EIP-1153 usage stays quiet.propose_hypotheses()emits Foundry handler sketches gated on the detected protocol shape —INV-SHARE-BACKING,INV-EMPTY-VAULT,INV-K,INV-ACCRUE-FIRST,INV-HEALTH,INV-VIEW-LOCK,INV-TSTORE-CLEAR,INV-SIG-BIND. These are hypotheses to falsify, never claims of proof.deep_scan.py— second-order reentrancyA public function that writes value state after delegating to a helper performing a low-level
.call. Skipped when either the public function or the helper carries a reentrancy guard, or when the public function makes the call itself — that case is first-order and already covered byexternal_call_before_write. All four skip conditions plus write ordering are tested.Wiring
understand.py·models.py(hypothesesonProtocolUnderstanding,to_dict,briefing) ·reason.py(_DEEP_PLAYBOOK_FACTSfor the five AVs) ·report.py(new "Must-hold invariants (hypotheses, not proofs)" section) ·exploit_paths.py(PATH-SECOND-ORDER-REENTRANCY) · brain (5 AV playbooks +TAC-INVARIANT-FIRST).Constraints held
RULE_VERSIONstays2.3.0.real_incidentsentry points at a documented public class (The DAO CEI violation, CompoundexchangeRateStoredvsexchangeRateCurrent, ERC-4626 inflation, Uniswap V2_update, EIP-1153 semantics).self TARGETandself autonomous TARGETboth verified; offline guard test still passes.Verification
All seven CI gates run locally against a clean venv:
Pyflakes is clean on every file this PR touches. The four warnings it reports (
models.pyunusedOptional,reason.pyunusedDict/Iterable,report.pyunusedcounts) reproduce identically on the pre-change files at HEAD, so they are pre-existing and left alone.Note on the test count
The brief expected 175 = 164 on main + 11 new. The actual baseline on
mainis 151, not 164 — verified by exporting main's tree withgit archiveand running its suite independently:PR #1 was squash-merged into the single parentless commit
9b332df, and no test files exist outsidetests/. Rather than pad with 13 filler tests to hit the stated number, this PR lands 24 real tests so the suite still ends at 175 — the 11 specified cases plus 13 more covering the guard/ordering skip conditions, thelocate_deeplocator,.pop()poisoning, accrual through helper indirection, thehypothesesmodel surface, the playbook↔fact mapping, and end-to-end finding/path/report integration.The referenced commits
47a1c40and9d803c2do not exist as objects in this repository (git cat-file -tfails for both). The content described for47a1c40— read-only reentrancy, TSTORE residue, Permit2, EIP-1271, CREATE2 — is present in the tree, so it landed via the squash; only the SHAs are gone.