solidity-lean/SolidCore/Witness/NarrowAddNestedMappingKey.lean
Daniel Robinson e9a45b1185
fix(mapping): m[a+b][0] narrow inner-mapping key over checked arithmetic Panics 0x11 (was 256-bit success reading m[300][0]=0) (#32)
f(uint8 a, uint8 b) view returns (uint256) { return m[a + b][0]; } over
mapping(uint8 => mapping(uint8 => uint256)) m, with f(200,100): solc computes
a+b at uint8 (the common type of the two uint8 operands), so the checked add
overflows (300 > 255) and reverts Panic(0x11) while computing the OUTER mapping
key, before the nested lookup. solidity-lean returned 0 successfully — a
revert-vs-success soundness gap.

Root cause: the env-aware `Expr.index` arm of `Expr.toCoreAsWithEnvFuel?`
reroutes narrow/flagged KEYS through the operand-width cleanup, but always
built the BASE via the env-LESS `Expr.indexReadCoreBuilder?` (`Expr.toCore?`).
For a nested read `m[a + b][0]` the outer key (`0`) is harmless, but the base
`m[a + b]` carries the narrow checked add; lowered env-less, the inner key ran
at 256 bits (= 300, no overflow) so `m[300][0] = 0` was read and returned. The
single-level `n[a + b]` case already Panicked (its own key is the flagged one).

Fix: the index arm now ALSO reroutes when `Expr.abiArgNeedsEnvCleanup? base`
(the base subtree carries narrow checked arithmetic), and in that case lowers
the base through the FULL env-aware recursion (`Expr.toCoreAsWithEnvFuel?`) so
the inner narrow key hits its operand-width `implicitCleanupCore` and Panics
0x11 before the outer lookup. The outer read is the general
`index`/`fixedBytesIndex` over the env-lowered base value. Unflagged
bases/keys keep the exact env-less `indexReadCoreBuilder?` shapes
byte-identically.

Pinned by SolidCore.Witness.NarrowAddNestedMappingKey (overflow -> Panic 0x11;
boundary 256 -> Panic; safe 255/200 -> read 0; single-level control -> Panic).
Full lake build (1193 jobs) + smoke_replay (29 cases) green.

Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-24 15:14:35 -04:00

156 lines
5.4 KiB