Repository navigation
Conversation
The partial constant propagation caches a literal initializer against a variable name in the per-function Yul constant map. Assignment stored a new value but never refreshed that entry, so a later read of the variable re-attached the stale constant to the new value. That annotation has a real consumer: with `--enable-eravm-extensions`, the external-call lowering selects an EraVM extension intrinsic from the constant annotated on the destination argument. A variable that first holds a simulation selector and is then reassigned to an ordinary address therefore still dispatches the extension -- an intended external call is compiled as an unrelated instruction, with no diagnostic. Both binding paths are covered: the scalar path now replaces the entry when the new value is itself a known constant and drops it otherwise; the tuple path always drops it, since a tuple element is loaded from memory and is never a known constant. Requires `YulData::remove_constant` from the companion era-compiler-llvm-context change. Known gap, not addressed here: the map is keyed by bare variable name for the whole function, with no block scoping, so a shadowed name in an inner block still aliases the outer entry. That is a second, independent invalidation failure. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
@hedgar2017 thank you for the reviews, they've been really helpful. If you have time, could I also ask you to take a look at this one(it's tied with matter-labs/era-compiler-llvm-context#95)? You have the most context on this code. |
A Yul variable first holds the `meta` simulation selector (0xFFFC) and is
then reassigned to an ordinary address. With the cached constant left
stale, the following staticcall is compiled as the `meta` extension and no
call is emitted at all:
meta r1
instead of a call to 0x8002:
add 32770, r0, r2
call r0, @__staticcall, @DEFAULT_UNWIND
The test asserts the call is present and that no `meta` instruction is
emitted. It fails against an unpatched compiler.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This crate does not compile against era-compiler-llvm-context main until matter-labs/era-compiler-llvm-context#95 adds `YulData::remove_constant`. Pinning the branch lets CI run here in the meantime. Revert this commit once that PR has merged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #346 +/- ##
==========================================
- Coverage 75.54% 75.53% -0.02%
==========================================
Files 124 124
Lines 11308 11323 +15
==========================================
+ Hits 8543 8553 +10
- Misses 2765 2770 +5 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
@AntonD3 could you give me more context on how these simulations are supposed to behave?
My understanding is that missing a simulation is acceptable. The detection only handles specific patterns, so a statically known 0xFFFC can still be compiled as a regular call, e.g.:
let a := add(0xFFFB, 1)
mstore(0, staticcall(0, a, 0, 0, 0, 0)) // regular call, not meta
The feature is poorly designed in that respect, but I don't think that needs fixing. The real problem is the opposite case: a != 0xFFFC at runtime, but the call is still compiled to meta.
The PR fixes the straight-line case, but three such cases remain:
- A variable left over from a sibling block (not fixed by the PR):
{ let a := 0xFFFC }
{
let a := calldataload(0)
mstore(0, staticcall(0, a, 0, 0, 0, 0)) // meta
}
- A loop, where the call comes before the reassignment (not fixed by the PR):
let a := 0xFFFC
for { let i := 0 } lt(i, 2) { i := add(i, 1) } {
mstore(0, staticcall(0, a, 0, 0, 0, 0)) // meta on both iterations
a := 0x8002
}
- A conditional assignment of a literal (introduced by the PR; main emits a regular call here):
let a := calldataload(0)
if calldataload(32) { a := 0xFFFC }
mstore(0, staticcall(0, a, 0, 0, 0, 0)) // meta even if the condition is false
while cases 1-2 are present in main even before the patch, 3 is introduced by the patch and need to be fixed.
|
#349 is going to replace it |
The defect
The partial constant propagation keeps a per-function map of constants saved to variables
(
YulData, a flatHashMap<String, BigUint>):variable_declaration.rs:36-45— caches a literal initializerexpression/mod.rs— re-attaches the constant to a freshly loaded valuefunction_call/mod.rs→eravm/evm/call.rs:33, which selects an EraVMextension intrinsic from the constant annotated on a call destination
assignment.rsstored the new value but never refreshed the entry. The broken invariant: theconstant annotation attached to a Yul value must equal that value on every control-flow path.
Because the annotation has a real consumer, a stale entry is wrong code, not a stale label:
The unoptimised IR shows
store i256 32770, ptr %addr(0x8002) followed bycall i256 @llvm.eravm.meta()— no@__staticcall/@__farcallto 0x8002 is emitted anywhere.The call whose runtime destination is 0x8002 was compiled as the EraVM meta extension. The
reverse also holds: a genuine selector later reassigned to an ordinary address still dispatches
the extension.
Reachability
Requires
--enable-eravm-extensionsand a variable that first holds a recognised simulationselector (0xFFEB–0xFFFF) and is then reassigned. Deterministic for that pattern.
0xffffoccurrences across the 25.yulfiles are bit masks (and(shr(224, versionedCodeHash), 0xffff)) or wider constants. Novariable holds a simulation selector as a call destination, and none is reassigned. The system
contracts are built with the flag, so this is where it would bite, and today it does not.
A genuine wrong-code bug with no known reachable caller. Worth fixing because the consumer makes
a stale annotation semantic, and because an invalidation-free cache is a hazard for any future
reader of the map.
The change
Scalar path: replace the entry when the new value is itself a known constant, drop it otherwise.
Tuple path: always drop, since a tuple element is loaded from memory and is never a known
constant.
Two things for the reviewer to decide
1. Insert-or-drop, or drop-only? The scalar path inserts when the new value is a known
constant, mirroring
variable_declaration. That upholds the invariant exactly, but it is abehaviour change beyond pure invalidation:
let a := f(); a := 0xFFFC;now dispatches theextension where it previously did not. Drop-only is strictly more conservative but silently loses
an intended dispatch in
let a := 0xFFFC; a := 0xFFFC;. Neither is purely safe; insert-or-drop isthe semantically faithful one, which is why it is written this way.
2. The block-scoping gap is NOT closed. There are two independent invalidation failures. This
fixes assignment. The map is still keyed by bare variable name for the whole function with no
block scoping, so a shadowed name in an inner block still aliases the outer entry. Closing that
means scoping the map — a larger change, deliberately out of scope here.
Testing
cargo checkandcargo fmt --all -- --checkclean, against the context PR applied via a local[patch]override.One gap, stated plainly: the regression gate I wanted — build the system contracts with and
without the fix and confirm byte-identical output — was not run. A release build of
zksolcfails on my machine with a libffi linker error in LLVM's Interpreter (
_ffi_type_*symbols),unrelated to this change and absent from the type check. The reachability argument above is
static only. Running that comparison before merging is the one outstanding item.
🤖 Generated with Claude Code