From 3ca5cafaf29b28c6a148021bf4f101790dbb7e33 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 28 Oct 2026 16:32:20 +0100 Subject: [PATCH 1/6] perf(precompiles): replace the base precompile lock with a lock-free list (#835) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `Precompiles::base` takes a read lock on the global `BASE_PRECOMPILES` map on every call. Taking a read lock writes its reader count, a cache line that bounces between last-level caches when threads on several of them build EVMs at once, as @gakonst found in #763. This replaces the `RwLock>` with an append-only list of nodes, one per host type. The first node lives inline in the static and later nodes hang off `OnceLock>` links; each node owns its type-erased cache, so nothing is leaked by hand. A node is never removed or replaced and each link is set once, so a reader only loads, and a creator that loses a race uses the winner's node. The common single-host-type case allocates only the cache itself, which stays boxed because the list holds caches of different types behind one `dyn Any`. There is no `unsafe` and no new dependency, and the `no_std` path is unchanged. This is a per-EVM-construction cost. It matters for callers that build an EVM per transaction on many threads; I make no claim about block-level speedup. Measured on an AMD EPYC 9124 (4 L3s), threads pinned, `Precompiles::base(Cancun)` in a release build, ns per call from the slowest thread, median of 3 rounds: | Threads | main | this PR | |---|---|---| | 1 | 31.6 ns | 2.4 ns ๐ŸŸข | | 4, one L3 | 120.9 ns | 2.4 ns ๐ŸŸข | | 4, 4 L3s | 180.0 ns | 2.4 ns ๐ŸŸข | | 16, 4 L3s | 655.3 ns | 4.1 ns ๐ŸŸข | --- crates/evm2/src/precompiles/mod.rs | 66 ++++++++++++++++++------------ 1 file changed, 40 insertions(+), 26 deletions(-) diff --git a/crates/evm2/src/precompiles/mod.rs b/crates/evm2/src/precompiles/mod.rs index dc2dd5443..710974bb4 100644 --- a/crates/evm2/src/precompiles/mod.rs +++ b/crates/evm2/src/precompiles/mod.rs @@ -11,8 +11,6 @@ use alloc::{borrow::Cow, vec::Vec}; use alloy_primitives::Address; #[cfg(feature = "std")] use core::any::{Any, TypeId}; -#[cfg(feature = "std")] -use std::{collections::HashMap, sync::RwLock}; pub mod blake2; pub mod bls12_381; @@ -178,13 +176,36 @@ impl PrecompileProvider for Precompiles { #[cfg(feature = "std")] type BasePrecompileCache = [OnceLock>; 7]; +/// Append-only list of the [`BasePrecompileCache`] of each host type, starting at +/// [`BASE_PRECOMPILES`]. +/// +/// There are only a handful of host types per process, so a linear walk is cheap. Nodes are never +/// removed or replaced, and each link is set once, so readers only load: unlike a read lock, a hit +/// writes no shared cache line. +#[cfg(feature = "std")] +struct BasePrecompileNode { + type_id: TypeId, + cache: Box, + next: OnceLock>, +} + +#[cfg(feature = "std")] +impl BasePrecompileNode { + fn new() -> Self { + Self { + type_id: TypeId::of::(), + cache: Box::new(BasePrecompileCache::::default()), + next: OnceLock::new(), + } + } +} + #[cfg(feature = "std")] -static BASE_PRECOMPILES: RwLock>> = - RwLock::new(None); +static BASE_PRECOMPILES: OnceLock = OnceLock::new(); #[cfg(feature = "std")] +#[inline] fn cached_base_precompiles(spec: SpecId) -> &'static PrecompileMap { - let type_id = TypeId::of::(); let index = match spec { SpecId::FRONTIER | SpecId::HOMESTEAD | SpecId::TANGERINE | SpecId::SPURIOUS_DRAGON => 0, SpecId::BYZANTIUM | SpecId::PETERSBURG => 1, @@ -195,29 +216,22 @@ fn cached_base_precompiles(spec: SpecId) -> &'static Precompile SpecId::OSAKA | SpecId::AMSTERDAM => 6, }; - { - let cache = BASE_PRECOMPILES.read().expect("base precompile cache poisoned"); - if let Some(precompiles) = cache.as_ref().and_then(|cache| cache.get(&type_id)) { - return precompiles - .downcast_ref::>() - .expect("base precompile cache type mismatch")[index] - .get_or_init(|| base_precompiles::(spec)); - } - } - - let mut cache = BASE_PRECOMPILES.write().expect("base precompile cache poisoned"); - let cache = cache.get_or_insert_with(HashMap::new); + shared_base_precompiles::() + .downcast_ref::>() + .expect("base precompile cache type mismatch")[index] + .get_or_init(|| base_precompiles::(spec)) +} - if let Some(precompiles) = cache.get(&type_id) { - return precompiles - .downcast_ref::>() - .expect("base precompile cache type mismatch")[index] - .get_or_init(|| base_precompiles::(spec)); +/// Returns the cache of `T`, appending it to [`BASE_PRECOMPILES`] on first use. +#[cfg(feature = "std")] +fn shared_base_precompiles() -> &'static (dyn Any + Send + Sync) { + let type_id = TypeId::of::(); + // Only an empty link is initialized, and racing creators agree on the winner's node. + let mut node = BASE_PRECOMPILES.get_or_init(BasePrecompileNode::new::); + while node.type_id != type_id { + node = node.next.get_or_init(|| Box::new(BasePrecompileNode::new::())); } - - let precompiles = Box::leak(Box::new([const { OnceLock::new() }; 7])); - cache.insert(type_id, precompiles); - precompiles[index].get_or_init(|| base_precompiles::(spec)) + &*node.cache } fn base_precompiles(spec: SpecId) -> PrecompileMap { From bf9900abb88a62d2ed25f74946afc65f5bac928a Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Wed, 28 Oct 2026 17:13:55 +0100 Subject: [PATCH 2/6] perf(jit): resolve the returns of recursive functions (#796) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Jump resolution keys each block state on the jump targets on its stack, which in practice is the call chain. A recursive call adds a return address per level, so a recursion never reaches a fixed set of contexts: the full-context fixpoint runs out of budget, and the coarser fallbacks join the frames of every level, which loses the return addresses of the code below the recursion. That one unresolved return then makes evm2 discard every other resolution in the contract. In Solar's output this is the last cause of unresolved jumps at `-Osize` (quicksorts, OZ reentrancy mocks, the `_update`/`_mint` mutual recursion in `ERC20WithAutoMinerReward`, Solady's `_execute`), and the 12 jumps left in the `solady-algorithms` runtime bench. This adds a folded context mode that keeps a recursion's contexts finite without joining its frames. A stack that holds the same jump target twice keeps only the entries above the deeper copy, and the rest moves into a segment: a stack fragment shared by every state with the same context, which can continue in other segments, including itself. A block that reads below its state's entries runs again on each segment below, with the segment's entries put back under the state, so every caller chain still sees its own frames and the returns resolve to their full set of return sites. Joins keep the union of the segments of equal-height stacks and fall back to the existing truncation otherwise. Shared return stubs (blocks that only shuffle the stack and jump) count from their third copy, since compilers push them for many nested non-recursive calls. Folding also fires on calls that share a jump target without a recursion, such as a `0x20` that happens to equal a `JUMPDEST` pc, and there it joins the frames of unrelated callers. So the analysis still runs the full-context mode first, and tries the folded mode only when Full does not converge or leaves a `Top` jump. It keeps the folded result only when it converges and resolves every jump (a host instruction in a state with segments counts as not converging, since the host may write any stack word), and otherwise falls back from Full as before. A contract that Full resolves gets exactly the same analysis as on `main`. Interpretation now stops before recording an instruction that underflows, so a state that continues in segments never records `Top` for an operand that lives in a segment. On a stack that is not truncated, the underflow halts at runtime, so this is also more precise outside folding. `recursive_return` used a constant depth and dropped each caller's frame, so its last return really underflows, and Full now resolves it exactly; the fixture now recurses to an unknown depth and keeps its frames. ## Benchmarks Counts and solady compile numbers compare against `main` with #791; the per-call and adversarial numbers predate that merge. The JIT bench tables at the end compare against `main` with the latest merge. Solar census of 581 runtime contracts (Solar `b58a650db`, the -Ztune corpus), dynamic jumps left in the final IR: | | main | this PR | |---|---|---| | `-Osize` | 12 contracts / 339 jumps | 0 / 0 ๐ŸŸข | | `-Ogas` | 16 / 435 | 6 / 338 ๐ŸŸข | The 6 contracts left at `-Ogas` are packed u16 jump tables, not recursion. No contract regresses. The 2306 contract codes in `data/mainnet-25347446-25347455.bin.zst`, `evm2 run --parse-only`: | | main | this PR | |---|---|---| | contracts with dynamic jumps | 33 | 23 ๐ŸŸข | | dynamic jumps | 2763 | 1799 ๐ŸŸข | | `--display` output of the 2273 contracts `main` resolves | | identical | | analysis `instructions:u`, contracts `main` resolves (before #791) | 248.57G | 248.39G (-0.1%) | | analysis `instructions:u`, the 10 newly resolved (before #791) | 3.70G | 4.55G (+22.9%) ๐Ÿ”ด | | analysis `instructions:u`, the 29 then still unresolved (before #791) | 12.30G | 15.62G (+27.0%) ๐Ÿ”ด | The unresolved contracts pay for a folded attempt that fails, on top of the same Full, Coarse and Insensitive attempts as on `main`. `solady-algorithms`, `instructions:u` from `perf stat`: | | main | this PR | |---|---|---| | `evm2 run 0` (parse and JIT compile, whole process) | 757.7M | 637.3M (-15.9%) ๐ŸŸข | | `evm2 run --parse-only` (whole process) | 47.0M | 49.9M (+6.2%) ๐Ÿ”ด | | `sort(uint256[200])` per call | 38.13K | 36.63K (-3.9%) ๐ŸŸข | | `decimal(uint256)` per call | 17.49K | 17.51K (+0.1%) | | `decode(string)` per call | 18.39K | 18.34K (-0.3%) | Adversarial inputs, `evm2 run --parse-only` instructions (whole process, about 9M of it startup): | input | main | this PR | |---|---|---| | n self-recursive functions with m sites, c callers: 16/3/4 | 57M, 32 unresolved | 42M, 0 unresolved ๐ŸŸข | | same, 32/4/8 | 137M, 64 unresolved | 95M, 0 unresolved ๐ŸŸข | | same, 8/8/8 | 56M, 16 unresolved | 82M, 16 unresolved ๐Ÿ”ด | | random mutual recursion, 32 functions ร— 8 sites | 122M | 162M ๐Ÿ”ด | | random mutual recursion, 64 functions ร— 8 sites | 219M | 288M ๐Ÿ”ด | | random mutual recursion, 128 functions ร— 4 sites | 270M | 360M ๐Ÿ”ด | | a chain of 1000 blocks that each push their own address | 166M | 164M | Every attempt keeps its own budget, so the worst case is one extra full-context attempt. On the JIT benches, `opt.s` changes in 0/31 and total compile instructions grow by 0.2% (snailtracer +2.3%, where Full and the folded mode both run out of budget before Coarse converges, and balancer_vault +1.0%, which Full resolves, from the segment bookkeeping in the shared fixpoint loop). The generated code is identical, so the per-execution instruction deltas in the runtime table (up to +2.1%) are measurement noise. Head `421ddd7572` vs base `origin/main` (`64167bf0f1`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:---------------|---------------:|------------:|------------:| | snailtracer | +2.3% ๐Ÿ”ด | +2.5% ๐Ÿ”ด | +2.9% ๐Ÿ”ด | | balancer_vault | +1.0% ๐Ÿ”ด | +1.2% ๐Ÿ”ด | +1.4% ๐Ÿ”ด | | **TOTAL** | **+0.2% ๐Ÿ”ด** | **+0.7% ๐Ÿ”ด** | **+0.7% ๐Ÿ”ด** | | **GEOMEAN** | **+0.1% ๐Ÿ”ด** | **+0.6% ๐Ÿ”ด** | **+0.3% ๐Ÿ”ด** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|----------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|------------------------------:|--------------------------------:|--------------------------------:| | fibonacci-calldata | 37.61M
37.53M
-0.2% ๐ŸŸข | 36.97M
37.28M
+0.9% ๐Ÿ”ด | 11.0ms
11.2ms
+2.0% ๐Ÿ”ด | 149.8ยตs
152.7ยตs
+2.0% ๐Ÿ”ด | 399.8ยตs
406.0ยตs
+1.5% ๐Ÿ”ด | 1.31ms
1.35ms
+3.0% ๐Ÿ”ด | 2.75ms
2.98ms
+8.4% ๐Ÿ”ด | | factorial | 41.81M
41.78M
-0.1% ๐ŸŸข | 39.22M
39.57M
+0.9% ๐Ÿ”ด | 11.3ms
11.2ms
-0.4% ๐ŸŸข | 137.7ยตs
137.7ยตs
= | 364.1ยตs
357.8ยตs
-1.7% ๐ŸŸข | 1.19ms
1.18ms
-0.7% ๐ŸŸข | 3.10ms
3.12ms
+0.8% ๐Ÿ”ด | | counter | 54.46M
54.34M
-0.2% ๐ŸŸข | 38.62M
39.26M
+1.7% ๐Ÿ”ด | 9.11ms
9.22ms
+1.2% ๐Ÿ”ด | 264.8ยตs
267.5ยตs
+1.0% ๐Ÿ”ด | 469.3ยตs
474.4ยตs
+1.1% ๐Ÿ”ด | 1.34ms
1.34ms
+0.1% ๐Ÿ”ด | 3.50ms
4.00ms
+14.3% ๐Ÿ”ด | | snailtracer | 5.16G
5.29G
+2.3% ๐Ÿ”ด | 1.94G
1.99G
+2.5% ๐Ÿ”ด | 366.2ms
376.9ms
+2.9% ๐Ÿ”ด | 23.5ms
31.2ms
+33.0% ๐Ÿ”ด | 7.42ms
7.21ms
-2.8% ๐ŸŸข | 65.8ms
66.4ms
+1.0% ๐Ÿ”ด | 300.2ms
305.0ms
+1.6% ๐Ÿ”ด | | weth | 540.22M
540.19M
= | 241.01M
242.66M
+0.7% ๐Ÿ”ด | 47.5ms
47.8ms
+0.7% ๐Ÿ”ด | 1.85ms
1.87ms
+0.7% ๐Ÿ”ด | 2.15ms
2.03ms
-5.4% ๐ŸŸข | 11.0ms
11.0ms
+0.2% ๐Ÿ”ด | 32.3ms
32.8ms
+1.5% ๐Ÿ”ด | | hash_10k | 80.58M
80.57M
= | 51.61M
52.48M
+1.7% ๐Ÿ”ด | 11.9ms
11.9ms
-0.5% ๐ŸŸข | 244.8ยตs
258.8ยตs
+5.7% ๐Ÿ”ด | 481.2ยตs
488.1ยตs
+1.4% ๐Ÿ”ด | 1.84ms
1.99ms
+8.4% ๐Ÿ”ด | 5.06ms
5.24ms
+3.6% ๐Ÿ”ด | | erc20_transfer | 578.45M
578.78M
+0.1% ๐Ÿ”ด | 251.91M
252.25M
+0.1% ๐Ÿ”ด | 49.7ms
49.5ms
-0.3% ๐ŸŸข | 1.98ms
1.95ms
-1.2% ๐ŸŸข | 2.06ms
2.04ms
-1.4% ๐ŸŸข | 11.7ms
11.7ms
+0.5% ๐Ÿ”ด | 33.0ms
33.5ms
+1.3% ๐Ÿ”ด | | push0_proxy | 34.82M
34.78M
-0.1% ๐ŸŸข | 32.70M
32.49M
-0.6% ๐ŸŸข | 10.3ms
10.6ms
+2.8% ๐Ÿ”ด | 120.3ยตs
126.7ยตs
+5.3% ๐Ÿ”ด | 345.9ยตs
361.9ยตs
+4.6% ๐Ÿ”ด | 830.7ยตs
912.9ยตs
+9.9% ๐Ÿ”ด | 2.10ms
2.22ms
+5.4% ๐Ÿ”ด | | usdc_proxy | 244.74M
244.79M
= | 122.28M
123.07M
+0.6% ๐Ÿ”ด | 25.0ms
24.8ms
-0.8% ๐ŸŸข | 1.12ms
1.15ms
+2.7% ๐Ÿ”ด | 1.20ms
1.21ms
+1.3% ๐Ÿ”ด | 5.72ms
5.81ms
+1.5% ๐Ÿ”ด | 14.3ms
14.6ms
+2.5% ๐Ÿ”ด | | fiat_token | 3.99G
3.99G
= | 1.60G
1.59G
-0.3% ๐ŸŸข | 302.5ms
302.1ms
-0.1% ๐ŸŸข | 11.7ms
11.8ms
+0.7% ๐Ÿ”ด | 10.8ms
10.7ms
-0.4% ๐ŸŸข | 67.6ms
68.3ms
+1.1% ๐Ÿ”ด | 239.0ms
240.5ms
+0.6% ๐Ÿ”ด | | uniswap_v2_pair | 2.06G
2.06G
= | 853.00M
853.95M
+0.1% ๐Ÿ”ด | 163.9ms
162.9ms
-0.6% ๐ŸŸข | 6.48ms
6.65ms
+2.7% ๐Ÿ”ด | 6.32ms
6.74ms
+6.7% ๐Ÿ”ด | 33.5ms
33.9ms
+1.2% ๐Ÿ”ด | 127.7ms
128.7ms
+0.7% ๐Ÿ”ด | | univ2_router | 4.39G
4.39G
= | 1.79G
1.80G
+0.4% ๐Ÿ”ด | 339.5ms
340.2ms
+0.2% ๐Ÿ”ด | 14.0ms
13.3ms
-4.9% ๐ŸŸข | 11.0ms
11.1ms
+0.3% ๐Ÿ”ด | 68.6ms
68.8ms
+0.3% ๐Ÿ”ด | 278.8ms
281.5ms
+1.0% ๐Ÿ”ด | | seaport | 9.75G
9.74G
= | 4.24G
4.24G
= | 798.1ms
796.9ms
-0.2% ๐ŸŸข | 23.4ms
23.4ms
+0.4% ๐Ÿ”ด | 18.9ms
18.5ms
-2.1% ๐ŸŸข | 172.0ms
173.1ms
+0.7% ๐Ÿ”ด | 678.6ms
685.0ms
+0.9% ๐Ÿ”ด | | airdrop | 1.42G
1.42G
= | 588.99M
590.57M
+0.3% ๐Ÿ”ด | 113.7ms
113.5ms
-0.2% ๐ŸŸข | 4.23ms
4.15ms
-1.9% ๐ŸŸข | 4.25ms
4.18ms
-1.7% ๐ŸŸข | 29.1ms
29.1ms
-0.1% ๐ŸŸข | 81.9ms
82.7ms
+1.0% ๐Ÿ”ด | | bswap64 | 67.31M
67.32M
= | 43.85M
44.30M
+1.0% ๐Ÿ”ด | 10.5ms
10.3ms
-1.5% ๐ŸŸข | 410.5ยตs
421.4ยตs
+2.7% ๐Ÿ”ด | 603.0ยตs
601.3ยตs
-0.3% ๐ŸŸข | 1.67ms
1.66ms
-0.9% ๐ŸŸข | 3.92ms
3.85ms
-1.7% ๐ŸŸข | | bswap64_opt | 73.98M
73.93M
-0.1% ๐ŸŸข | 45.50M
45.34M
-0.4% ๐ŸŸข | 10.5ms
10.4ms
-0.8% ๐ŸŸข | 351.8ยตs
364.3ยตs
+3.6% ๐Ÿ”ด | 527.3ยตs
520.2ยตs
-1.4% ๐ŸŸข | 1.62ms
1.61ms
-0.9% ๐ŸŸข | 4.12ms
4.13ms
+0.3% ๐Ÿ”ด | | eip4788 | 48.72M
48.73M
= | 36.51M
36.43M
-0.2% ๐ŸŸข | 8.76ms
8.68ms
-0.9% ๐ŸŸข | 173.5ยตs
168.0ยตs
-3.2% ๐ŸŸข | 404.0ยตs
384.9ยตs
-4.7% ๐ŸŸข | 1.04ms
991.7ยตs
-4.9% ๐ŸŸข | 3.48ms
3.37ms
-3.0% ๐ŸŸข | | eip2935 | 42.48M
42.51M
+0.1% ๐Ÿ”ด | 37.85M
37.55M
-0.8% ๐ŸŸข | 11.1ms
10.9ms
-2.0% ๐ŸŸข | 144.2ยตs
145.9ยตs
+1.2% ๐Ÿ”ด | 368.6ยตs
359.7ยตs
-2.4% ๐ŸŸข | 932.0ยตs
903.3ยตs
-3.1% ๐ŸŸข | 2.88ms
2.90ms
+0.9% ๐Ÿ”ด | | burntpix | 6.71G
6.71G
= | 2.65G
2.67G
+0.7% ๐Ÿ”ด | 498.0ms
501.0ms
+0.6% ๐Ÿ”ด | 11.2ms
11.1ms
-1.0% ๐ŸŸข | 1.66ms
1.70ms
+2.5% ๐Ÿ”ด | 9.87ms
10.1ms
+2.6% ๐Ÿ”ด | 27.7ms
28.2ms
+1.5% ๐Ÿ”ด | | curve_stableswap | 1.14G
1.14G
= | 481.79M
485.37M
+0.7% ๐Ÿ”ด | 92.7ms
93.1ms
+0.5% ๐Ÿ”ด | 2.81ms
2.89ms
+2.7% ๐Ÿ”ด | 2.73ms
2.69ms
-1.4% ๐ŸŸข | 21.2ms
21.0ms
-0.9% ๐ŸŸข | 46.7ms
47.2ms
+1.1% ๐Ÿ”ด | | onchain_lm_v2 | 3.67G
3.67G
= | 1.41G
1.42G
+0.5% ๐Ÿ”ด | 268.3ms
270.0ms
+0.6% ๐Ÿ”ด | 9.29ms
9.08ms
-2.3% ๐ŸŸข | 7.58ms
7.40ms
-2.4% ๐ŸŸข | 46.4ms
46.6ms
+0.4% ๐Ÿ”ด | 185.7ms
188.5ms
+1.5% ๐Ÿ”ด | | usdt | 1.34G
1.34G
= | 550.52M
556.02M
+1.0% ๐Ÿ”ด | 106.4ms
106.7ms
+0.3% ๐Ÿ”ด | 5.61ms
5.59ms
-0.4% ๐ŸŸข | 5.62ms
5.41ms
-3.6% ๐ŸŸข | 24.8ms
25.0ms
+0.4% ๐Ÿ”ด | 76.3ms
77.7ms
+1.9% ๐Ÿ”ด | | universal_router | 11.59G
11.59G
= | 4.78G
4.80G
+0.5% ๐Ÿ”ด | 892.8ms
895.8ms
+0.3% ๐Ÿ”ด | 12.0ms
12.1ms
+0.9% ๐Ÿ”ด | 11.4ms
11.4ms
-0.2% ๐ŸŸข | 80.7ms
80.9ms
+0.3% ๐Ÿ”ด | 314.3ms
314.7ms
+0.1% ๐Ÿ”ด | | aggregation_router_v6 | 35.21G
35.20G
= | 14.33G
14.45G
+0.9% ๐Ÿ”ด | 2.659s
2.683s
+0.9% ๐Ÿ”ด | 19.3ms
19.3ms
= | 18.4ms
17.9ms
-2.5% ๐ŸŸข | 136.0ms
137.4ms
+1.0% ๐Ÿ”ด | 511.0ms
520.4ms
+1.8% ๐Ÿ”ด | | entry_point_v0_7 | 17.92G
17.92G
= | 7.35G
7.40G
+0.8% ๐Ÿ”ด | 1.371s
1.381s
+0.7% ๐Ÿ”ด | 9.29ms
9.27ms
-0.2% ๐ŸŸข | 10.0ms
10.2ms
+1.6% ๐Ÿ”ด | 62.8ms
62.7ms
-0.2% ๐ŸŸข | 267.0ms
267.0ms
= | | uniswap_v3_pool | 6.21G
6.21G
= | 2.56G
2.57G
+0.6% ๐Ÿ”ด | 483.2ms
487.0ms
+0.8% ๐Ÿ”ด | 19.4ms
19.0ms
-2.5% ๐ŸŸข | 16.9ms
16.6ms
-1.2% ๐ŸŸข | 110.4ms
109.8ms
-0.5% ๐ŸŸข | 398.5ms
400.1ms
+0.4% ๐Ÿ”ด | | uniswap_v4_pool_manager | 6.72G
6.72G
= | 2.73G
2.74G
+0.2% ๐Ÿ”ด | 515.2ms
517.1ms
+0.4% ๐Ÿ”ด | 13.5ms
13.8ms
+2.1% ๐Ÿ”ด | 13.8ms
14.1ms
+2.2% ๐Ÿ”ด | 105.5ms
106.4ms
+0.9% ๐Ÿ”ด | 390.3ms
391.5ms
+0.3% ๐Ÿ”ด | | curve_3pool | 6.61G
6.60G
= | 2.55G
2.57G
+0.6% ๐Ÿ”ด | 482.2ms
483.9ms
+0.4% ๐Ÿ”ด | 18.3ms
18.7ms
+1.7% ๐Ÿ”ด | 14.4ms
14.3ms
-0.9% ๐ŸŸข | 85.3ms
85.7ms
+0.4% ๐Ÿ”ด | 431.0ms
441.7ms
+2.5% ๐Ÿ”ด | | curve_tricrypto2 | 9.42G
9.42G
= | 3.62G
3.65G
+0.8% ๐Ÿ”ด | 683.0ms
687.1ms
+0.6% ๐Ÿ”ด | 17.1ms
17.5ms
+2.6% ๐Ÿ”ด | 16.0ms
16.9ms
+5.5% ๐Ÿ”ด | 94.6ms
95.5ms
+1.0% ๐Ÿ”ด | 418.2ms
420.8ms
+0.6% ๐Ÿ”ด | | balancer_vault | 18.85G
19.04G
+1.0% ๐Ÿ”ด | 8.05G
8.15G
+1.2% ๐Ÿ”ด | 1.498s
1.519s
+1.4% ๐Ÿ”ด | 25.9ms
25.5ms
-1.5% ๐ŸŸข | 20.1ms
20.3ms
+1.2% ๐Ÿ”ด | 201.3ms
201.8ms
+0.2% ๐Ÿ”ด | 647.1ms
656.9ms
+1.5% ๐Ÿ”ด | | succinct_staking | 10.87G
10.86G
= | 4.40G
4.43G
+0.5% ๐Ÿ”ด | 822.4ms
826.9ms
+0.6% ๐Ÿ”ด | 16.9ms
17.0ms
+0.8% ๐Ÿ”ด | 11.9ms
11.9ms
-0.1% ๐ŸŸข | 86.7ms
87.4ms
+0.8% ๐Ÿ”ด | 313.8ms
315.5ms
+0.5% ๐Ÿ”ด | | **TOTAL** | **164.88G
165.17G
+0.2% ๐Ÿ”ด** | **67.46G
67.93G
+0.7% ๐Ÿ”ด** | **12.673s
12.761s
+0.7% ๐Ÿ”ด** | **270.8ms
278.3ms
+2.8% ๐Ÿ”ด** | **218.6ms
218.5ms
=** | **1.542s
1.550s
+0.5% ๐Ÿ”ด** | **5.844s
5.906s
+1.1% ๐Ÿ”ด** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:----------------|--------------------:|--------------:| | counter | +2.1% ๐Ÿ”ด | -3.9% ๐ŸŸข | | fiat_token | = | -5.8% ๐ŸŸข | | uniswap_v2_pair | +0.1% ๐Ÿ”ด | -5.7% ๐ŸŸข | | univ2_router | +1.0% ๐Ÿ”ด | -3.6% ๐ŸŸข | | eip4788 | +0.1% ๐Ÿ”ด | -5.4% ๐ŸŸข | | **TOTAL** | **=** | **-0.9% ๐ŸŸข** | | **GEOMEAN** | **+0.1% ๐Ÿ”ด** | **-2.0% ๐ŸŸข** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 44.01K
44.06K
+0.1% ๐Ÿ”ด | 11.36K
11.00K
-3.2% ๐ŸŸข | | factorial | 79.49K
80.04K
+0.7% ๐Ÿ”ด | 18.58K
18.46K
-0.7% ๐ŸŸข | | counter | 22.77K
23.26K
+2.1% ๐Ÿ”ด | 8.00K
7.69K
-3.9% ๐ŸŸข | | snailtracer | 192.98M
192.99M
= | 58.29M
58.26M
= | | weth | 28.15K
27.88K
-1.0% ๐ŸŸข | 9.89K
9.53K
-3.6% ๐ŸŸข | | hash_10k | 8.80M
8.80M
= | 2.77M
2.78M
+0.3% ๐Ÿ”ด | | erc20_transfer | 18.79M
18.80M
= | 5.26M
5.34M
+1.5% ๐Ÿ”ด | | push0_proxy | 23.02K
23.07K
+0.2% ๐Ÿ”ด | 8.58K
8.35K
-2.7% ๐ŸŸข | | usdc_proxy | 24.68K
24.72K
+0.1% ๐Ÿ”ด | 9.01K
8.77K
-2.7% ๐ŸŸข | | fiat_token | 22.51K
22.52K
= | 8.43K
7.94K
-5.8% ๐ŸŸข | | uniswap_v2_pair | 22.68K
22.71K
+0.1% ๐Ÿ”ด | 8.49K
8.01K
-5.7% ๐ŸŸข | | univ2_router | 20.57K
20.78K
+1.0% ๐Ÿ”ด | 7.75K
7.48K
-3.6% ๐ŸŸข | | seaport | 23.20K
23.05K
-0.6% ๐ŸŸข | 8.58K
8.20K
-4.4% ๐ŸŸข | | airdrop | 22.84K
22.89K
+0.2% ๐Ÿ”ด | 8.50K
8.12K
-4.5% ๐ŸŸข | | bswap64 | 19.65K
19.67K
+0.1% ๐Ÿ”ด | 7.31K
7.06K
-3.4% ๐ŸŸข | | bswap64_opt | 19.82K
19.84K
+0.1% ๐Ÿ”ด | 7.46K
7.11K
-4.7% ๐ŸŸข | | eip4788 | 24.06K
24.09K
+0.1% ๐Ÿ”ด | 8.82K
8.34K
-5.4% ๐ŸŸข | | eip2935 | 22.91K
22.96K
+0.2% ๐Ÿ”ด | 8.56K
8.15K
-4.8% ๐ŸŸข | | burntpix | 101.78M
101.82M
= | 26.94M
26.88M
-0.2% ๐ŸŸข | | curve_stableswap | 260.50K
260.63K
+0.1% ๐Ÿ”ด | 103.32K
103.41K
+0.1% ๐Ÿ”ด | | onchain_lm_v2 | 521.39M
521.39M
= | 217.26M
214.47M
-1.3% ๐ŸŸข | | usdt | 38.95K
38.76K
-0.5% ๐ŸŸข | 13.25K
12.75K
-3.8% ๐ŸŸข | | universal_router | 152.24K
151.23K
-0.7% ๐ŸŸข | 52.88K
52.70K
-0.3% ๐ŸŸข | | aggregation_router_v6 | 475.39K
475.17K
= | 170.87K
171.87K
+0.6% ๐Ÿ”ด | | entry_point_v0_7 | 1.28M
1.28M
= | 329.13K
342.12K
+3.9% ๐Ÿ”ด | | uniswap_v3_pool | 82.88K
82.85K
= | 27.74K
27.56K
-0.7% ๐ŸŸข | | uniswap_v4_pool_manager | 69.13K
69.33K
+0.3% ๐Ÿ”ด | 21.70K
21.59K
-0.5% ๐ŸŸข | | curve_3pool | 82.24K
82.22K
= | 28.07K
27.57K
-1.8% ๐ŸŸข | | curve_tricrypto2 | 222.82K
222.71K
= | 83.16K
82.55K
-0.7% ๐ŸŸข | | balancer_vault | 123.33K
123.38K
= | 41.69K
41.97K
+0.7% ๐Ÿ”ด | | succinct_staking | 64.14K
64.03K
-0.2% ๐ŸŸข | 23.90K
23.83K
-0.3% ๐ŸŸข | | **TOTAL** | **847.02M
847.07M
=** | **311.55M
308.78M
-0.9% ๐ŸŸข** |
--- .../src/bytecode/passes/block_analysis.rs | 722 +++++++++++++----- 1 file changed, 538 insertions(+), 184 deletions(-) diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index de1a1cad4..a326de5a5 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -29,6 +29,15 @@ //! constants on the stack, so that internal functions are analyzed separately for each call chain //! (see [`ContextMode`]). //! +//! A recursive call chain repeats return addresses, and would need a new context at every level. +//! Instead, a stack that holds a jump target twice keeps only the entries above the deeper copy, +//! and continues in one of a set of segments: shared stack fragments that may in turn continue in +//! segments, including themselves. The recursion then reaches a fixed set of contexts without +//! joining the frames of different levels, so that the code below the recursion still finds its +//! own return addresses. Calls that share a jump target without a recursion fold as well, which +//! can join their frames, so the analysis only folds when it cannot resolve every jump without +//! folds, and keeps the folded result only when it resolves every jump. +//! //! A `JUMPI` propagates only along the edges that its condition allows in each context, and the //! values that its condition bounds on an edge, such as an index below an array length, get that //! bound there. Memory writes at ranged offsets only forget the words in their range. @@ -274,6 +283,10 @@ const MAX_RANGE_JUMP_WIDTH: usize = 4096; /// compile time. const MAX_RANGE_JUMP_TARGETS: usize = 1024; +/// Maximum number of segments that may continue a stack, beyond which the stack is truncated +/// instead, so that the joins and the runs of a block on each segment stay cheap. +const MAX_SEGS_BELOW: usize = 64; + /// Memory words at constant offsets whose values are known, sorted by offset and disjoint. /// /// Only words written by `MSTORE` are tracked, so memory already covers a word that `MLOAD` @@ -364,6 +377,9 @@ enum BlockState { /// Whether entries below `stack` were dropped by a join or by [`MAX_ABS_STACK_DEPTH`] /// clamping, so that running out of tracked entries is not a stack underflow. truncated: bool, + /// The segments, one of which continues the stack below `stack`, sorted. Empty when the + /// stack ends at `stack` or is truncated. + below: SmallVec<[Seg; 2]>, /// Known memory words. mem: MemWords, }, @@ -375,11 +391,13 @@ impl BlockState { /// When stack heights differ, the stacks are top-aligned and the entries below the shorter /// one are dropped, marking the state truncated. This handles the common Solidity dispatch /// pattern where the "no selector match" fallthrough leaves an extra item on the stack that - /// the fallback ignores. + /// the fallback ignores. Stacks of the same height that both continue in segments keep the + /// segments of both. fn join( &mut self, incoming: &[AbsValue], incoming_truncated: bool, + incoming_below: &[Seg], incoming_mem: &MemWords, sets: &mut ConstSetInterner, ) -> bool { @@ -390,19 +408,32 @@ impl BlockState { *self = Self::Known { stack: incoming[start..].to_vec(), truncated: incoming_truncated, + below: if incoming_truncated { SmallVec::new() } else { incoming_below.into() }, mem: incoming_mem.clone(), }; true } - Self::Known { stack: existing, truncated, mem } => { + Self::Known { stack: existing, truncated, below, mem } => { let mut changed = mem.join(incoming_mem, sets); // Keep only the entries that both stacks have, so that joins never grow the // stack. let join_len = existing.len().min(incoming.len()); - if (incoming_truncated || incoming.len() != existing.len()) && !*truncated { - *truncated = true; - changed = true; + if !*truncated { + let lost = incoming_truncated + || incoming.len() != existing.len() + || incoming_below.is_empty() != below.is_empty(); + for &seg in incoming_below { + if let Err(i) = below.binary_search(&seg) { + below.insert(i, seg); + changed = true; + } + } + if lost || below.len() > MAX_SEGS_BELOW { + *truncated = true; + below.clear(); + changed = true; + } } if existing.len() > join_len { existing.drain(..existing.len() - join_len); @@ -444,6 +475,9 @@ enum ContextMode { /// function apart, and the stack shape keeps the iterations of these loops apart, so that /// their memory offsets stay constant. Full, + /// Like [`Full`](Self::Full), but the stacks of recursive calls continue in segments, as the + /// module docs describe, cut at [`Bytecode::recursion_cut`]. + Folded, /// Key on the tracked stack shape and the topmost valid jump target constant. Coarse, /// One state per block. @@ -454,22 +488,25 @@ impl ContextMode { /// Returns the budget of a fixpoint attempt in this mode. fn budget(self, num_blocks: usize, num_insts: usize) -> Budget { match self { - Self::Full => Budget { + Self::Full | Self::Folded => Budget { block_parts: 8192, parts: (num_blocks * 32).min(1 << 17), pops: num_blocks * 128, + pop_work: 64, work: num_insts as u64 * 320, }, Self::Coarse => Budget { block_parts: 128, parts: (num_blocks * 8).min(1 << 15), pops: num_blocks * 32, + pop_work: 0, work: num_insts as u64 * 64, }, Self::Insensitive => Budget { block_parts: 1, parts: num_blocks, pops: num_blocks * 8, + pop_work: 0, work: num_insts as u64 * 64, }, } @@ -484,8 +521,12 @@ struct Budget { parts: usize, /// Worklist pops. pops: usize, - /// Interpreted instructions, propagated edges, joined stack entries, and merged set - /// constants. + /// Work of a worklist pop of a state that continues in segments, besides its instructions. + /// Segments leave many pops of short stacks, which then cost about as much as the few pops + /// of long stacks without them. + pop_work: u64, + /// Worklist pops, interpreted instructions, propagated edges, joined stack entries, and + /// merged set constants. work: u64, } @@ -494,6 +535,11 @@ oxc_index::define_nonmax_u32_index_type! { struct Part; } +oxc_index::define_nonmax_u32_index_type! { + /// A stack segment that continues the stacks of block states below their tracked entries. + struct Seg; +} + /// How [`Bytecode::interpret_inst`] records snapshots. enum Record<'a> { Skip, @@ -513,6 +559,70 @@ struct Fixpoint { converged: bool, } +/// Block states of [`Bytecode::run_fixpoint`], one per context of each block. +struct Parts { + states: IndexVec, + /// The part of each context of each block. + by_key: IndexVec>, + /// Parts to visit, in code order, which mostly follows the CFG, so that a state is complete + /// before it propagates. + worklist: BinaryHeap>, + queued: BitVec, +} + +impl Parts { + /// Joins a state into the part of context `key` of `bid`, queueing the part if it changes. + /// + /// Returns `false` if a new part would exceed `budget`. + #[expect(clippy::too_many_arguments)] + fn join( + &mut self, + bid: Block, + key: u64, + stack: &[AbsValue], + truncated: bool, + below: &[Seg], + mem: &MemWords, + sets: &mut ConstSetInterner, + budget: &Budget, + ) -> bool { + let part = match self.by_key[bid].get(&key) { + Some(&part) => part, + None => { + if self.by_key[bid].len() == budget.block_parts || self.states.len() == budget.parts + { + return false; + } + let part = self.states.push((bid, BlockState::Bottom)); + self.queued.push(false); + self.by_key[bid].insert(key, part); + part + } + }; + if self.states[part].1.join(stack, truncated, below, mem, sets) { + self.queue(part); + } + true + } + + fn queue(&mut self, part: Part) { + if !self.queued[part.index()] { + self.queued.set(part.index(), true); + self.worklist.push(Reverse((self.states[part].0, part))); + } + } +} + +/// Stack segments of [`Bytecode::run_fixpoint`], one per context. +#[derive(Default)] +struct Segs { + /// The entries of each segment, as a state without memory words. + states: IndexVec, + by_key: FxHashMap, + /// The parts whose blocks read into each segment, to visit again when it changes. + users: IndexVec>, +} + oxc_index::define_nonmax_u32_index_type! { /// A block index in the CFG. pub(crate) struct Block; @@ -1000,34 +1110,32 @@ impl Bytecode<'_> { } // Try the most precise contexts first, falling back to coarser ones when an attempt - // exceeds its budget. + // exceeds its budget. Recursive calls need a new context at every level, so when `Full` + // runs out of budget or leaves a jump unresolved, try folding them. Folds also join the + // frames of calls that share a jump target without a recursion, so their result + // only counts when it resolves every jump. + let has_top = |targets: &[(Inst, JumpResolution)]| targets.iter().any(|(_, t)| t.is_top()); let mut fixpoint = self.run_fixpoint(ContextMode::Full); - for mode in [ContextMode::Coarse, ContextMode::Insensitive] { - if fixpoint.converged { - break; - } - fixpoint = self.run_fixpoint(mode); - } - let Fixpoint { reached, discovered: discovered_edges, const_sets, converged } = fixpoint; - - // After convergence, resolve each dynamic jump from its snapshot operand. - let mut jump_targets: Vec<(Inst, JumpResolution)> = Vec::new(); - let mut has_top_jump = false; - let mut range_budget = MAX_RANGE_JUMP_TARGETS; - for &jump_inst in &jump_insts { - // Blocks the fixpoint never reached keep their block-local snapshots. - let target = if converged - && self.cfg.inst_to_block[jump_inst].is_some_and(|bid| !reached[bid.index()]) - { - JumpResolution::bottom() + let mut jump_targets = self.resolve_jumps(&jump_insts, &fixpoint); + if !fixpoint.converged || has_top(&jump_targets) { + let full_snapshots = std::mem::replace(&mut self.snapshots, local_snapshots.clone()); + let folded = self.run_fixpoint(ContextMode::Folded); + let folded_targets = self.resolve_jumps(&jump_insts, &folded); + if folded.converged && !has_top(&folded_targets) { + (fixpoint, jump_targets) = (folded, folded_targets); } else { - self.resolve_jump(jump_inst, &const_sets, &mut range_budget) - }; - if target.is_top() { - has_top_jump = true; + self.snapshots = full_snapshots; + for mode in [ContextMode::Coarse, ContextMode::Insensitive] { + if fixpoint.converged { + break; + } + fixpoint = self.run_fixpoint(mode); + jump_targets = self.resolve_jumps(&jump_insts, &fixpoint); + } } - jump_targets.push((jump_inst, target)); } + let has_top_jump = has_top(&jump_targets); + let Fixpoint { reached, discovered: discovered_edges, converged, .. } = fixpoint; // Invalidate resolutions that may be unsound due to incomplete analysis. // When the fixpoint didn't converge, partially-discovered ConstSets may be @@ -1052,6 +1160,30 @@ impl Bytecode<'_> { jump_targets } + /// Resolves each jump in `jump_insts` from its snapshot operand after `fixpoint`. + fn resolve_jumps( + &self, + jump_insts: &[Inst], + fixpoint: &Fixpoint, + ) -> Vec<(Inst, JumpResolution)> { + let Fixpoint { reached, const_sets, converged, .. } = fixpoint; + let mut range_budget = MAX_RANGE_JUMP_TARGETS; + jump_insts + .iter() + .map(|&jump_inst| { + // Blocks the fixpoint never reached keep their block-local snapshots. + let target = if *converged + && self.cfg.inst_to_block[jump_inst].is_some_and(|bid| !reached[bid.index()]) + { + JumpResolution::bottom() + } else { + self.resolve_jump(jump_inst, const_sets, &mut range_budget) + }; + (jump_inst, target) + }) + .collect() + } + fn resolve_jump( &self, jump_inst: Inst, @@ -1268,6 +1400,10 @@ impl Bytecode<'_> { /// Each block keeps one state per [context](Self::context_key) of the states reaching it, as /// chosen by `mode`. Snapshots join the values of all contexts. /// + /// In [`ContextMode::Folded`], the stacks of recursive calls continue in segments. Segments + /// with the same context share a state. A block that reads below its state's entries runs + /// again on each segment below them, with the segment's entries prepended. + /// /// Snapshots are only recorded once the fixpoint converges, so on non-convergence they keep /// the safe block-local values computed by `block_analysis_local`. fn run_fixpoint(&mut self, mode: ContextMode) -> Fixpoint { @@ -1275,28 +1411,19 @@ impl Bytecode<'_> { let mut const_sets = ConstSetInterner::new(self.jumpdests.clone()); let budget = mode.budget(num_blocks, self.insts.len()); - let mut states = IndexVec::::new(); - let mut parts = IndexVec::>::from_vec(vec![ - FxHashMap::default(); - num_blocks - ]); + let mut parts = Parts { + states: IndexVec::new(), + by_key: IndexVec::from_vec(vec![FxHashMap::default(); num_blocks]), + worklist: BinaryHeap::new(), + queued: BitVec::new(), + }; + let mut segs = Segs::default(); // Entry block starts with an empty stack, or with unknown entries when the caller may // pass a stack. + let no_mem = MemWords::default(); let entry = Block::from_usize(0); - let entry_state = BlockState::Known { - stack: Vec::new(), - truncated: self.stack_observed(), - mem: MemWords::default(), - }; - let entry_part = states.push((entry, entry_state)); - parts[entry].insert(0, entry_part); - // Visit blocks in code order, which mostly follows the CFG, so that a state is complete - // before it propagates. - let mut worklist = BinaryHeap::new(); - let mut queued: BitVec = BitVec::new(); - queued.push(true); - worklist.push(Reverse((entry, entry_part))); + parts.join(entry, 0, &[], self.stack_observed(), &[], &no_mem, &mut const_sets, &budget); // Discovered dynamic-jump target edges per block. let mut discovered: IndexVec> = @@ -1304,6 +1431,8 @@ impl Bytecode<'_> { // The same edges, to deduplicate them in constant time: a shared return block can have // thousands of targets. let mut discovered_set = FxHashSet::default(); + // The parts in `segs.users`, to deduplicate them in constant time. + let mut seg_users = FxHashSet::default(); let mut pops = 0; let mut work = 0; @@ -1311,15 +1440,20 @@ impl Bytecode<'_> { // Reusable buffers to avoid per-iteration allocations. let mut stack_buf: Vec = Vec::with_capacity(MAX_ABS_STACK_DEPTH); + let mut below_buf = SmallVec::<[Seg; 2]>::new(); + let mut input_buf = Vec::new(); let mut targets = SmallVec::<[Block; 4]>::new(); let mut mem_buf = MemWords::default(); let mut refined_buf = Vec::new(); let branches: IndexVec> = self.cfg.blocks.indices().map(|bid| self.branch(bid)).collect(); + let forwarders: BitVec = + self.cfg.blocks.indices().map(|bid| self.is_forwarder(bid)).collect(); + let mut copies = vec![0; self.jumpdests.len()]; - 'fixpoint: while let Some(Reverse((_, pid))) = worklist.pop() { - queued.set(pid.index(), false); + 'fixpoint: while let Some(Reverse((_, pid))) = parts.worklist.pop() { + parts.queued.set(pid.index(), false); pops += 1; if pops > budget.pops || work + const_sets.merged > budget.work { converged = false; @@ -1327,12 +1461,13 @@ impl Bytecode<'_> { } // Copy input state into reusable buffer. - let (bid, state) = &states[pid]; + let (bid, state) = &parts.states[pid]; let bid = *bid; stack_buf.clear(); - let truncated = match state { - BlockState::Known { stack, truncated, mem } => { + let mut truncated = match state { + BlockState::Known { stack, truncated, below, mem } => { stack_buf.extend_from_slice(stack); + below_buf.clone_from(below); mem_buf.clone_from(mem); *truncated } @@ -1341,6 +1476,9 @@ impl Bytecode<'_> { let insts = self.cfg.blocks[bid].insts(); work += insts.len() as u64; + if !below_buf.is_empty() { + work += budget.pop_work; + } let term_inst = self.cfg.blocks[bid].terminator(); let mut operand = None; let mut condition = AbsValue::Top; @@ -1349,9 +1487,52 @@ impl Bytecode<'_> { operand = stack_buf.last().copied(); condition = stack_buf.iter().rev().nth(1).copied().unwrap_or_default(); } - if !self.interpret_inst(i, &mut stack_buf, truncated, &mut mem_buf, Record::Skip) { + // A host instruction may write any stack word, including the entries in segments. + if !below_buf.is_empty() && self.insts[i].flags.contains(InstFlags::HOST) { + converged = false; + break 'fixpoint; + } + if self.interpret_inst(i, &mut stack_buf, truncated, &mut mem_buf, Record::Skip) { + continue; + } + // If the block reads below the state's entries rather than halts, run it on each + // segment below. + if stack_buf.len() >= self.insts[i].stack_io().0 as usize { continue 'fixpoint; } + let BlockState::Known { stack, mem, .. } = &parts.states[pid].1 else { + unreachable!() + }; + input_buf.clone_from(stack); + mem_buf.clone_from(mem); + for &seg in &below_buf { + if seg_users.insert((seg, pid)) { + segs.users[seg].push(pid); + } + let BlockState::Known { stack, truncated, below, .. } = &segs.states[seg] + else { + unreachable!() + }; + stack_buf.clone_from(stack); + stack_buf.extend_from_slice(&input_buf); + let key = + self.context_key(mode, &stack_buf, *truncated, !below.is_empty(), &mem_buf); + work += budget.pop_work + 1 + stack_buf.len().min(MAX_ABS_STACK_DEPTH) as u64; + if !parts.join( + bid, + key, + &stack_buf, + *truncated, + below, + &mem_buf, + &mut const_sets, + &budget, + ) { + converged = false; + break 'fixpoint; + } + } + continue 'fixpoint; } let branch = branches[bid].as_ref(); let condition = match branch { @@ -1379,7 +1560,43 @@ impl Bytecode<'_> { if self.cfg.blocks[bid].succs.is_empty() && targets.is_empty() { continue; } - let key = self.context_key(mode, &stack_buf, truncated, &mem_buf); + + // Move the entries from a repeated return address down into a segment. + if let ContextMode::Folded = mode + && let Some(depth) = self.recursion_cut(&stack_buf, &forwarders, &mut copies) + { + let split = stack_buf.len() - depth; + let seg_stack = &stack_buf[..split]; + let key = + self.context_key(mode, seg_stack, truncated, !below_buf.is_empty(), &no_mem); + let seg = match segs.by_key.get(&key) { + Some(&seg) => seg, + None => { + if segs.states.len() == budget.parts { + converged = false; + break 'fixpoint; + } + segs.users.push(Vec::new()); + let seg = segs.states.push(BlockState::Bottom); + segs.by_key.insert(key, seg); + seg + } + }; + work += 1 + split.min(MAX_ABS_STACK_DEPTH) as u64; + if segs.states[seg].join(seg_stack, truncated, &below_buf, &no_mem, &mut const_sets) + { + for &user in &segs.users[seg] { + parts.queue(user); + } + } + stack_buf.drain(..split); + truncated = false; + below_buf.clear(); + below_buf.push(seg); + } + + let continued = !below_buf.is_empty(); + let key = self.context_key(mode, &stack_buf, truncated, continued, &mem_buf); let mut refined_key = None; for &succ in self.cfg.blocks[bid].succs.iter().chain(&targets) { let (mut stack, mut key) = (&stack_buf, key); @@ -1407,48 +1624,45 @@ impl Bytecode<'_> { refined_buf.clone_from(&stack_buf); let value = &mut refined_buf[stack_buf.len() - 1 - depth]; *value = value.at_most(bound, &mut self.u256_interner.borrow_mut()); - self.context_key(mode, &refined_buf, truncated, &mem_buf) + self.context_key(mode, &refined_buf, truncated, continued, &mem_buf) }); (stack, key) = (&refined_buf, refined); } } - let part = match parts[succ].get(&key) { - Some(&part) => part, - None => { - if parts[succ].len() == budget.block_parts || states.len() == budget.parts { - converged = false; - break 'fixpoint; - } - let part = states.push((succ, BlockState::Bottom)); - queued.push(false); - parts[succ].insert(key, part); - part - } - }; work += 1 + stack.len().min(MAX_ABS_STACK_DEPTH) as u64; - if states[part].1.join(stack, truncated, &mem_buf, &mut const_sets) - && !queued[part.index()] - { - queued.set(part.index(), true); - worklist.push(Reverse((succ, part))); + if !parts.join( + succ, + key, + stack, + truncated, + &below_buf, + &mem_buf, + &mut const_sets, + &budget, + ) { + converged = false; + break 'fixpoint; } } } debug!( ?mode, - parts = states.len(), + parts = parts.states.len(), + segs = segs.states.len(), work = work + const_sets.merged, "{msg} after {pops} iterations (max={max})", msg = if converged { "converged" } else { "did not converge" }, max = budget.pops, ); - // Record the snapshots of the final states, joined across contexts. + // Record the snapshots of the final states, joined across contexts. A state with + // segments below stops where its block reads below it: the runs of the block on the + // segments record the rest. if converged { let mut visited: BitVec = BitVec::repeat(false, self.insts.len()); - for (bid, state) in &states { - let BlockState::Known { stack, truncated, mem } = state else { continue }; + for (bid, state) in &parts.states { + let BlockState::Known { stack, truncated, mem, .. } = state else { continue }; stack_buf.clone_from(stack); mem_buf.clone_from(mem); for i in self.cfg.blocks[*bid].insts() { @@ -1464,7 +1678,7 @@ impl Bytecode<'_> { } } } - let reached = parts.iter().map(|p| !p.is_empty()).collect(); + let reached = parts.by_key.iter().map(|p| !p.is_empty()).collect(); Fixpoint { reached, discovered, const_sets, converged } } @@ -1546,7 +1760,8 @@ impl Bytecode<'_> { Some(Branch { fallthrough, fallthrough_is_target, bound: bound(tail) }) } - /// Returns the context of an abstract stack: a hash of the constants `mode` keys on. + /// Returns the context of an abstract stack: a hash of the constants `mode` keys on, and of + /// whether segments continue the stack. /// /// Keys that collide share a state, which only costs precision. fn context_key( @@ -1554,21 +1769,18 @@ impl Bytecode<'_> { mode: ContextMode, stack: &[AbsValue], truncated: bool, + continued: bool, mem: &MemWords, ) -> u64 { - let jump_target = |value: AbsValue| { - let pc = value.as_const()?.get_inline()?; - self.is_valid_jump(pc as usize).then_some(pc) - }; let targets = (stack.iter().rev().enumerate()) - .filter_map(|(depth, &value)| Some((depth, jump_target(value)?))); + .filter_map(|(depth, &value)| Some((depth, self.jump_target_pc(value)?))); let mut hasher = FxHasher::default(); let tracked = stack.len().min(MAX_CONTEXT_DEPTH); - (tracked as u8, truncated || stack.len() > tracked).hash(&mut hasher); + (tracked as u8, truncated || stack.len() > tracked, continued).hash(&mut hasher); match mode { - ContextMode::Full => { + ContextMode::Full | ContextMode::Folded => { for &(offset, value) in &mem.0 { - if let Some(pc) = jump_target(value) { + if let Some(pc) = self.jump_target_pc(value) { (offset, pc).hash(&mut hasher); } } @@ -1592,6 +1804,59 @@ impl Bytecode<'_> { hasher.finish() } + /// Returns the depth of the shallowest valid jump target constant in `stack` that repeats a + /// shallower one, such as the return address of a recursive call one level up. + /// + /// Targets in `forwarders` count from their third copy: compilers share such blocks as the + /// return address of many calls, which nest without a recursion. + /// + /// `copies` holds zero for every pc, and does again on return. + fn recursion_cut( + &self, + stack: &[AbsValue], + forwarders: &BitVec, + copies: &mut [u8], + ) -> Option { + let cut = stack.iter().rev().enumerate().find_map(|(depth, &value)| { + let pc = self.jump_target_pc(value)?; + let left = &mut copies[pc as usize]; + if *left == 0 { + let bid = self.cfg.inst_to_block[self.pc_to_inst(pc as usize)]?; + *left = if forwarders[bid.index()] { 2 } else { 1 }; + return None; + } + *left -= 1; + (*left == 0).then_some(depth) + }); + let scanned = cut.map_or(stack.len(), |depth| depth + 1); + for &value in stack.iter().rev().take(scanned) { + if let Some(pc) = self.jump_target_pc(value) { + copies[pc as usize] = 0; + } + } + cut + } + + /// Returns whether `bid` only moves stack entries and jumps, such as a shared block that + /// returns to the address below the top of the stack. + fn is_forwarder(&self, bid: Block) -> bool { + let block = &self.cfg.blocks[bid]; + self.insts[block.terminator()].opcode == op::JUMP + && block.insts().rev().skip(1).map(|i| &self.insts[i]).all(|data| { + data.is_dead_code() + || matches!( + data.opcode, + op::JUMPDEST | op::POP | op::DUP1..=op::DUP16 | op::SWAP1..=op::SWAP16 + ) + }) + } + + /// Returns the pc of `value` if it is a valid jump target constant. + fn jump_target_pc(&self, value: AbsValue) -> Option { + let pc = value.as_const()?.get_inline()?; + self.is_valid_jump(pc as usize).then_some(pc) + } + /// Collects the blocks of the valid jump targets in `operand`. fn jump_target_blocks( &self, @@ -1670,25 +1935,27 @@ impl Bytecode<'_> { let inp = inp as usize; let out = out as usize; - if truncated && stack.len() < inp { + // Check the stack height before recording, so that a state with segments below records + // nothing for an instruction that reads them. + if stack.len() < inp { + if !truncated { + return false; + } stack.splice(0..0, std::iter::repeat_n(AbsValue::Top, inp - stack.len())); } // Record pre-instruction input operand snapshot (in stack order, TOS last). if inp > 0 { - let start = stack.len().saturating_sub(inp); - let pad = inp.saturating_sub(stack.len()); + let inputs = &stack[stack.len() - inp..]; let snap = &mut self.snapshots.inputs[i]; match &mut record { Record::Skip => {} Record::Overwrite => { snap.clear(); - snap.resize(pad, AbsValue::Top); - snap.extend_from_slice(&stack[start..]); + snap.extend_from_slice(inputs); } Record::Join(sets) => { - for (k, slot) in snap.iter_mut().enumerate() { - let value = if k < pad { AbsValue::Top } else { stack[start + k - pad] }; + for (slot, &value) in snap.iter_mut().zip(inputs) { *slot = join(sets, *slot, value); } } @@ -1701,85 +1968,27 @@ impl Bytecode<'_> { stack.push(AbsValue::Const(inst.imm())); } op::POP => { - if stack.pop().is_none() { - return false; - } + stack.pop(); } - op::DUP1..=op::DUP16 => { - let depth = (inst.opcode - op::DUP1 + 1) as usize; - if stack.len() < depth { - return false; - } - stack.push(stack[stack.len() - depth]); - } - op::SWAP1..=op::SWAP16 => { - let depth = (inst.opcode - op::SWAP1 + 1) as usize; - let len = stack.len(); - if len < depth + 1 { - return false; - } - stack.swap(len - 1, len - 1 - depth); + // Unknown, disabled, or an invalid immediate. + op::DUP1..=op::DUP16 | op::SWAP1..=op::SWAP16 | op::DUPN | op::SWAPN | op::EXCHANGE + if inp == 0 => + { + return false; } - op::DUPN => { - let depth = crate::decode_single(inst.imm_byte()); - match depth { - Some(n) => { - let n = n as usize; - if stack.len() < n { - // Depth exceeds the tracked abstract stack (truncated by a - // join or MAX_ABS_STACK_DEPTH). The slot is reachable at runtime - // but unknown abstractly. - stack.push(AbsValue::Top); - } else { - stack.push(stack[stack.len() - n]); - } - } - None => return false, - } + op::DUP1..=op::DUP16 | op::DUPN => { + stack.push(stack[stack.len() - inp]); } - op::SWAPN => { - let depth = crate::decode_single(inst.imm_byte()); - match depth { - Some(n) => { - let n = n as usize; - let len = stack.len(); - if len < n + 1 { - // Deep slot beyond tracked abstract stack; TOS becomes Top - // and the deep slot (not tracked) is unchanged. - if let Some(tos) = stack.last_mut() { - *tos = AbsValue::Top; - } - } else { - stack.swap(len - 1, len - 1 - n); - } - } - None => return false, - } + op::SWAP1..=op::SWAP16 | op::SWAPN => { + let len = stack.len(); + stack.swap(len - 1, len - inp); } op::EXCHANGE => { - let pair = crate::decode_pair(inst.imm_byte()); - match pair { - Some((n, m)) => { - let (n, m) = (n as usize, m as usize); - let len = stack.len(); - if len < m + 1 { - // Deep slot beyond tracked abstract stack; the shallower - // slot (if tracked) becomes Top. - if len > n { - stack[len - 1 - n] = AbsValue::Top; - } - } else { - stack.swap(len - 1 - n, len - 1 - m); - } - } - None => return false, - } + let (n, _) = crate::decode_pair(inst.imm_byte()).unwrap(); + let len = stack.len(); + stack.swap(len - 1 - n as usize, len - inp); } _ => { - if stack.len() < inp { - return false; - } - // Disabled opcodes halt and have no stack inputs. let loaded = if inst.flags.contains(InstFlags::DISABLED) { None @@ -2407,6 +2616,7 @@ pub(crate) mod tests { #[cfg(test)] mod tests_edge_cases { use super::{tests::*, *}; + use evm2::{BaseEvmTypes, OpcodeConfig}; /// Three callers to the same internal function. The return JUMP should /// resolve to Multi with three targets. @@ -3096,8 +3306,9 @@ mod tests_edge_cases { #[test] fn truncated_stack_does_not_make_jumpi_unconditional() { - // Both paths reach `body` in one context. The join keeps the shorter stack, which - // does not hold the JUMPI condition. + // Both paths reach `body` in one context, as their stacks are deeper than + // `MAX_CONTEXT_DEPTH`. The join keeps the shorter stack, which does not hold the JUMPI + // condition. let src = format!( " CALLDATASIZE @@ -3129,8 +3340,8 @@ mod tests_edge_cases { SSTORE STOP ", - "PUSH1 0x11\n".repeat(63), - "POP\n".repeat(63), + "PUSH1 0x11\n".repeat(MAX_CONTEXT_DEPTH + 2), + "POP\n".repeat(MAX_CONTEXT_DEPTH + 2), ); let bytecode = analyze_asm(&src); @@ -3794,13 +4005,13 @@ mod tests_edge_cases { assert_eq!(targets, [Inst::from_usize(5), Inst::from_usize(12)]); } - /// A recursive function's return must not resolve to a partial set of return sites. + /// A recursive function's return must resolve to all of its return sites. #[test] fn recursive_return() { let bytecode = analyze_asm( " PUSH %done - PUSH1 0x03 + CALLDATASIZE PUSH %rec JUMP done: @@ -3813,10 +4024,9 @@ mod tests_edge_cases { PUSH %base JUMPI PUSH %back ; [ret, n, back] - SWAP1 ; [ret, back, n] PUSH1 0x01 - SWAP1 - SUB ; [ret, back, n - 1] + DUP3 + SUB ; [ret, n, back, n - 1] PUSH %rec JUMP back: @@ -3827,12 +4037,12 @@ mod tests_edge_cases { JUMP ; return ", ); - // Each recursion level keys a new context until the return addresses sink below - // `MAX_CONTEXT_DEPTH`, where the levels share a context whose join loses `done`, so the - // return must stay dynamic. - let (_, data) = bytecode.iter_insts().rfind(|(_, d)| d.is_jump()).unwrap(); - assert!(!data.flags.contains(InstFlags::STATIC_JUMP)); - assert!(bytecode.has_dynamic_jumps); + let (ret, _) = bytecode.iter_insts().rfind(|(_, d)| d.is_jump()).unwrap(); + let mut targets: Vec<_> = + bytecode.multi_jump_targets(ret).unwrap().iter().map(|&t| bytecode.pc(t)).collect(); + targets.sort_unstable(); + assert_eq!(targets, [6, 23]); + assert!(!bytecode.has_dynamic_jumps); } /// A `try`/`catch` shape: the failed call jumps to the block that checks the decoded @@ -4237,6 +4447,138 @@ mod tests_edge_cases { assert!(!bytecode.has_dynamic_jumps); } + /// A function that calls itself directly and through another function has a different call + /// chain at every recursion level. Its return must resolve to its three return sites, and its + /// caller must still find its own return address below the recursion. + #[test] + fn recursive_call_return() { + let bytecode = analyze_asm( + " + PUSH %main_ret + CALLDATASIZE + PUSH %outer + JUMP + main_ret: + JUMPDEST + STOP + outer: + JUMPDEST ; [main_ret, n] + PUSH %outer_ret + DUP2 + PUSH %rec + JUMP + outer_ret: + JUMPDEST ; [main_ret, n] + POP + JUMP + rec: + JUMPDEST ; [ret, n] + DUP1 + ISZERO + PUSH %done + JUMPI + PUSH %r1 + PUSH1 1 + DUP3 + SUB + PUSH %rec + JUMP + r1: + JUMPDEST ; [ret, n] + PUSH %r2 + PUSH1 2 + DUP3 + SUB + PUSH %g + JUMP + r2: + JUMPDEST + done: + JUMPDEST ; [ret, n] + POP + JUMP + g: + JUMPDEST ; [ret, n] + PUSH %g_ret + DUP2 + PUSH %rec + JUMP + g_ret: + JUMPDEST ; [ret, n] + POP + JUMP + ", + ); + + assert!(!bytecode.has_dynamic_jumps, "expected no dynamic jumps"); + let jump_at = + |pc| bytecode.iter_insts().find(|&(i, d)| bytecode.pc(i) == pc && d.is_jump()).unwrap(); + let (_, data) = jump_at(17); + assert!(!data.flags.contains(InstFlags::MULTI_JUMP)); + assert_eq!(bytecode.pc(data.static_jump_target()), 6, "outer must return to main_ret"); + let (rec_ret, _) = jump_at(46); + let mut targets: Vec<_> = + bytecode.multi_jump_targets(rec_ret).unwrap().iter().map(|&t| bytecode.pc(t)).collect(); + targets.sort_unstable(); + assert_eq!(targets, [15, 33, 54]); + let (_, data) = jump_at(56); + assert!(!data.flags.contains(InstFlags::MULTI_JUMP)); + assert_eq!(bytecode.pc(data.static_jump_target()), 43, "g must return to r2"); + } + + /// Callers that pass a jump target twice, without a recursion, would fold into segments, which + /// joins their entries above the segments. That joins the flags that pick the branch of each + /// caller, so each return would also reach `t`. Each return must resolve to its caller alone. + #[test] + fn calls_sharing_a_jump_target_keep_full_precision() { + let bytecode = analyze_asm( + " + PUSH %t ; w + PUSH %ra + PUSH %t + PUSH1 0x00 ; flag + PUSH %t + PUSH %f + JUMP + ra: + JUMPDEST ; [w] + PUSH %rb + PUSH %t ; z + PUSH %t + PUSH1 0x01 ; flag + PUSH %t + PUSH %f + JUMP + rb: + JUMPDEST + STOP + t: + JUMPDEST + STOP + f: + JUMPDEST ; [.., flag, t] + POP + PUSH %b + JUMPI + POP ; [w, ra, t] + JUMP + b: + JUMPDEST ; [w, rb, z, t] + POP + POP + JUMP + ", + ); + + assert!(!bytecode.has_dynamic_jumps, "expected no dynamic jumps"); + let mut jumps = bytecode.iter_insts().filter(|(_, d)| d.is_jump()).rev(); + for (ret, label) in [(27, "rb"), (13, "ra")] { + let (_, data) = jumps.next().unwrap(); + assert!(!data.flags.contains(InstFlags::MULTI_JUMP)); + assert_eq!(bytecode.pc(data.static_jump_target()), ret, "must return to {label}"); + } + } + /// Returns the pcs of the targets of the only multi-target jump. fn multi_jump_target_pcs(bytecode: &Bytecode<'_>) -> Vec { let mut jumps = @@ -4378,4 +4720,16 @@ mod tests_edge_cases { )); assert!(bytecode.has_dynamic_jumps); } + + /// A view that overrides every opcode leaves the stack opcodes unsupported, without stack + /// inputs. Analysis must reject them rather than read the stack. + #[test] + fn overridden_stack_opcodes_are_rejected() { + let view = OpcodeConfig::::base_for_spec(crate::tests::DEF_SPEC).view(None); + for opcode in [op::DUP1, op::DUP16, op::SWAP1, op::SWAP16] { + let code = vec![opcode, op::STOP]; + let mut bytecode = Bytecode::new(code, crate::tests::DEF_SPEC, None, Some(&view)); + assert!(bytecode.analyze().is_err(), "opcode {opcode:#04x}"); + } + } } From 3a81431192c870c63ab119bebef8fc5625e0ddf1 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 14:19:37 +0100 Subject: [PATCH 3/6] perf(jit): resolve jumps through packed jump tables (#836) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Solar's `-Ogas` dispatcher picks its target from a packed table: `PUSH1 0x0f DUP2 MOD PUSH1 0x04 SHL PUSH30 SWAP1 SHR PUSH2 0xffff AND JUMP`. The `MOD` gives a range of 15 values, but the shift of a constant by a range is not a range (the sets only hold jump targets), so the operand ends as `Range(0..=65535)`, too wide to resolve. That one unresolved jump also makes every other resolution in the contract suspect. When a dynamic jump's operand is `Top` or a range that is too wide, the fixpoint now looks at the narrowest range of 3 to 256 values that an earlier instruction of the block left on the stack. It rebuilds the stack after that instruction from the entry state of the block, puts each value of the range on top in turn, and runs the rest of the block up to the jump. Values whose path halts have no target, a valid jump target becomes a discovered edge like any other, any other constant marks the jump as possibly invalid, and a value that is not constant fails the enumeration. The work counts against the fixpoint budget. Only blocks that end in a dynamic jump track the range, so other blocks pay one flag test per pop. `resolve_jumps` turns an unresolved jump into the union of the targets that the fixpoint discovered for it when every state either resolved or enumerated its operand. Two or more targets give a switch that keeps the invalid jump default; a single target resolves only if no state may jump to an invalid one. On the Solar census of 581 runtime contracts compiled with `-Ogas`, 6 contracts with 338 dynamic jumps (seaport-1.6 Consideration, ReferenceConsideration, Seaport and SeaportValidatorHelper, solady-0.1.26 Timelock and WETH) now resolve every jump; `-Osize` stays at 0. The parsed `--display` output of every other census and benchmark code is identical. ## Benchmarks `opt.s` is unchanged in all 31 JIT benchmarks, and their compile `instructions` move by +0.02% (165.31G to 165.35G; the cycle and time columns are noise on a busy machine). Analysis cost, from `perf stat -e instructions:u` of `evm2 run --parse-only` over the 1162 census codes: the 6 newly resolved contracts cost +20% (2.54G to 3.05G), since they now analyse the code that their tables reach, and no other code costs more than +0.18%. Head `e825743d94` vs base `origin/main` (`bf9900abb8`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **-5.9% ๐ŸŸข** | **-6.6% ๐ŸŸข** | | **GEOMEAN** | **=** | **-0.8% ๐ŸŸข** | **-0.8% ๐ŸŸข** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|------------------------------:|--------------------------------:|----------------------------------:|-----------------------------------:|-----------------------------------:|---------------------------------:|----------------------------------:| | fibonacci-calldata | 37.66M
37.64M
-0.1% ๐ŸŸข | 39.10M
35.56M
-9.0% ๐ŸŸข | 12.2ms
11.9ms
-2.5% ๐ŸŸข | 502.6ยตs
318.2ยตs
-36.7% ๐ŸŸข | 821.9ยตs
419.9ยตs
-48.9% ๐ŸŸข | 2.31ms
1.57ms
-31.7% ๐ŸŸข | 13.7ms
2.89ms
-78.9% ๐ŸŸข | | factorial | 41.86M
41.81M
-0.1% ๐ŸŸข | 44.82M
41.25M
-8.0% ๐ŸŸข | 13.7ms
12.9ms
-6.0% ๐ŸŸข | 2.96ms
260.5ยตs
-91.2% ๐ŸŸข | 866.8ยตs
394.8ยตs
-54.5% ๐ŸŸข | 4.68ms
1.22ms
-74.0% ๐ŸŸข | 9.02ms
4.14ms
-54.1% ๐ŸŸข | | counter | 54.43M
54.43M
= | 45.60M
42.15M
-7.6% ๐ŸŸข | 11.3ms
10.8ms
-4.8% ๐ŸŸข | 1.03ms
455.3ยตs
-55.6% ๐ŸŸข | 928.1ยตs
492.1ยตs
-47.0% ๐ŸŸข | 9.18ms
1.47ms
-84.0% ๐ŸŸข | 19.7ms
3.76ms
-80.9% ๐ŸŸข | | snailtracer | 5.30G
5.31G
+0.1% ๐Ÿ”ด | 2.29G
2.35G
+2.6% ๐Ÿ”ด | 444.3ms
463.8ms
+4.4% ๐Ÿ”ด | 96.2ms
37.2ms
-61.4% ๐ŸŸข | 19.3ms
8.28ms
-57.0% ๐ŸŸข | 234.1ms
72.1ms
-69.2% ๐ŸŸข | 685.1ms
353.6ms
-48.4% ๐ŸŸข | | weth | 540.53M
540.84M
+0.1% ๐Ÿ”ด | 259.26M
252.44M
-2.6% ๐ŸŸข | 52.9ms
51.6ms
-2.4% ๐ŸŸข | 16.6ms
2.79ms
-83.1% ๐ŸŸข | 5.09ms
2.20ms
-56.8% ๐ŸŸข | 30.3ms
11.6ms
-61.9% ๐ŸŸข | 63.8ms
34.9ms
-45.4% ๐ŸŸข | | hash_10k | 80.65M
80.57M
-0.1% ๐ŸŸข | 51.77M
56.50M
+9.1% ๐Ÿ”ด | 12.4ms
13.4ms
+8.1% ๐Ÿ”ด | 1.06ms
448.7ยตs
-57.7% ๐ŸŸข | 896.9ยตs
560.3ยตs
-37.5% ๐ŸŸข | 3.60ms
2.17ms
-39.8% ๐ŸŸข | 10.5ms
5.54ms
-47.1% ๐ŸŸข | | erc20_transfer | 578.78M
578.82M
= | 261.96M
264.22M
+0.9% ๐Ÿ”ด | 53.3ms
54.3ms
+1.7% ๐Ÿ”ด | 7.92ms
2.90ms
-63.4% ๐ŸŸข | 4.51ms
2.39ms
-47.0% ๐ŸŸข | 76.6ms
14.2ms
-81.5% ๐ŸŸข | 75.6ms
39.8ms
-47.4% ๐ŸŸข | | push0_proxy | 34.83M
34.82M
= | 37.24M
31.17M
-16.3% ๐ŸŸข | 12.1ms
10.8ms
-11.4% ๐ŸŸข | 4.35ms
266.6ยตs
-93.9% ๐ŸŸข | 866.1ยตs
405.9ยตs
-53.1% ๐ŸŸข | 1.89ms
1.01ms
-46.7% ๐ŸŸข | 9.54ms
2.58ms
-73.0% ๐ŸŸข | | usdc_proxy | 244.91M
244.99M
= | 129.48M
139.98M
+8.1% ๐Ÿ”ด | 27.4ms
30.3ms
+10.3% ๐Ÿ”ด | 5.10ms
1.66ms
-67.5% ๐ŸŸข | 3.03ms
1.28ms
-57.8% ๐ŸŸข | 11.3ms
5.97ms
-47.2% ๐ŸŸข | 35.0ms
15.7ms
-55.2% ๐ŸŸข | | fiat_token | 3.99G
3.99G
= | 1.93G
1.85G
-4.1% ๐ŸŸข | 380.5ms
361.6ms
-5.0% ๐ŸŸข | 43.9ms
16.9ms
-61.4% ๐ŸŸข | 36.0ms
12.8ms
-64.5% ๐ŸŸข | 216.9ms
76.5ms
-64.7% ๐ŸŸข | 616.0ms
258.4ms
-58.1% ๐ŸŸข | | uniswap_v2_pair | 2.06G
2.06G
= | 883.35M
918.89M
+4.0% ๐Ÿ”ด | 178.4ms
179.9ms
+0.8% ๐Ÿ”ด | 204.9ms
9.27ms
-95.5% ๐ŸŸข | 26.8ms
6.39ms
-76.2% ๐ŸŸข | 89.3ms
42.4ms
-52.6% ๐ŸŸข | 372.2ms
140.7ms
-62.2% ๐ŸŸข | | univ2_router | 4.40G
4.40G
= | 1.88G
2.00G
+6.5% ๐Ÿ”ด | 365.9ms
388.4ms
+6.2% ๐Ÿ”ด | 87.3ms
18.4ms
-78.9% ๐ŸŸข | 71.6ms
11.7ms
-83.7% ๐ŸŸข | 341.0ms
80.0ms
-76.6% ๐ŸŸข | 956.2ms
300.5ms
-68.6% ๐ŸŸข | | seaport | 9.75G
9.76G
= | 4.62G
4.83G
+4.5% ๐Ÿ”ด | 895.6ms
933.2ms
+4.2% ๐Ÿ”ด | 137.8ms
31.9ms
-76.9% ๐ŸŸข | 152.6ms
19.6ms
-87.1% ๐ŸŸข | 1.062s
183.3ms
-82.7% ๐ŸŸข | 1.630s
763.0ms
-53.2% ๐ŸŸข | | airdrop | 1.42G
1.42G
= | 606.94M
614.94M
+1.3% ๐Ÿ”ด | 121.4ms
122.9ms
+1.3% ๐Ÿ”ด | 12.7ms
5.69ms
-55.1% ๐ŸŸข | 8.21ms
4.37ms
-46.7% ๐ŸŸข | 52.9ms
30.1ms
-43.1% ๐ŸŸข | 143.0ms
90.8ms
-36.5% ๐ŸŸข | | bswap64 | 67.39M
67.33M
-0.1% ๐ŸŸข | 44.11M
43.52M
-1.3% ๐ŸŸข | 11.4ms
10.8ms
-5.5% ๐ŸŸข | 851.2ยตs
703.2ยตs
-17.4% ๐ŸŸข | 877.8ยตs
682.3ยตs
-22.3% ๐ŸŸข | 2.62ms
1.97ms
-24.7% ๐ŸŸข | 6.69ms
4.23ms
-36.8% ๐ŸŸข | | bswap64_opt | 74.00M
74.01M
= | 43.28M
47.60M
+10.0% ๐Ÿ”ด | 10.9ms
11.4ms
+4.7% ๐Ÿ”ด | 906.2ยตs
661.3ยตs
-27.0% ๐ŸŸข | 1.70ms
708.7ยตs
-58.2% ๐ŸŸข | 3.17ms
1.88ms
-40.6% ๐ŸŸข | 6.28ms
4.65ms
-26.0% ๐ŸŸข | | eip4788 | 48.76M
48.73M
-0.1% ๐ŸŸข | 33.41M
35.79M
+7.1% ๐Ÿ”ด | 9.34ms
8.41ms
-10.0% ๐ŸŸข | 452.7ยตs
410.2ยตs
-9.4% ๐ŸŸข | 566.2ยตs
478.0ยตs
-15.6% ๐ŸŸข | 1.57ms
1.20ms
-23.8% ๐ŸŸข | 5.44ms
3.65ms
-32.8% ๐ŸŸข | | eip2935 | 42.51M
42.57M
+0.1% ๐Ÿ”ด | 36.23M
33.61M
-7.2% ๐ŸŸข | 10.5ms
11.0ms
+4.4% ๐Ÿ”ด | 500.6ยตs
297.0ยตs
-40.7% ๐ŸŸข | 696.3ยตs
438.8ยตs
-37.0% ๐ŸŸข | 1.94ms
1.15ms
-40.7% ๐ŸŸข | 6.16ms
3.30ms
-46.3% ๐ŸŸข | | burntpix | 6.72G
6.72G
= | 2.79G
2.71G
-3.1% ๐ŸŸข | 538.3ms
518.1ms
-3.8% ๐ŸŸข | 30.7ms
16.1ms
-47.8% ๐ŸŸข | 3.48ms
1.77ms
-49.2% ๐ŸŸข | 15.1ms
11.3ms
-24.9% ๐ŸŸข | 44.1ms
30.1ms
-31.7% ๐ŸŸข | | curve_stableswap | 1.14G
1.14G
= | 496.64M
487.49M
-1.8% ๐ŸŸข | 96.9ms
95.5ms
-1.5% ๐ŸŸข | 6.77ms
4.03ms
-40.5% ๐ŸŸข | 3.48ms
2.83ms
-18.7% ๐ŸŸข | 35.6ms
23.6ms
-33.6% ๐ŸŸข | 77.3ms
49.8ms
-35.6% ๐ŸŸข | | onchain_lm_v2 | 3.68G
3.68G
+0.1% ๐Ÿ”ด | 1.57G
1.45G
-7.5% ๐ŸŸข | 308.9ms
278.6ms
-9.8% ๐ŸŸข | 27.1ms
12.0ms
-55.5% ๐ŸŸข | 11.5ms
8.74ms
-24.2% ๐ŸŸข | 80.8ms
55.3ms
-31.6% ๐ŸŸข | 410.8ms
271.1ms
-34.0% ๐ŸŸข | | usdt | 1.34G
1.35G
+0.1% ๐Ÿ”ด | 602.75M
634.36M
+5.2% ๐Ÿ”ด | 120.2ms
127.7ms
+6.3% ๐Ÿ”ด | 13.5ms
8.19ms
-39.6% ๐ŸŸข | 11.3ms
6.16ms
-45.6% ๐ŸŸข | 60.5ms
26.1ms
-56.7% ๐ŸŸข | 110.8ms
82.7ms
-25.4% ๐ŸŸข | | universal_router | 11.60G
11.60G
= | 6.04G
6.99G
+15.7% ๐Ÿ”ด | 1.441s
1.726s
+19.8% ๐Ÿ”ด | 29.6ms
17.6ms
-40.6% ๐ŸŸข | 16.7ms
12.9ms
-22.5% ๐ŸŸข | 133.7ms
88.2ms
-34.1% ๐ŸŸข | 850.9ms
355.3ms
-58.2% ๐ŸŸข | | aggregation_router_v6 | 35.22G
35.22G
= | 30.13G
23.02G
-23.6% ๐ŸŸข | 6.127s
4.494s
-26.7% ๐ŸŸข | 27.9ms
26.4ms
-5.4% ๐ŸŸข | 21.2ms
19.8ms
-6.8% ๐ŸŸข | 184.4ms
154.9ms
-16.0% ๐ŸŸข | 792.4ms
558.0ms
-29.6% ๐ŸŸข | | entry_point_v0_7 | 17.93G
17.94G
= | 10.08G
9.49G
-5.9% ๐ŸŸข | 1.970s
1.845s
-6.4% ๐ŸŸข | 13.2ms
12.8ms
-2.6% ๐ŸŸข | 11.3ms
10.2ms
-9.8% ๐ŸŸข | 84.8ms
68.5ms
-19.2% ๐ŸŸข | 322.3ms
301.8ms
-6.4% ๐ŸŸข | | uniswap_v3_pool | 6.22G
6.22G
= | 3.15G
2.89G
-8.5% ๐ŸŸข | 613.4ms
557.6ms
-9.1% ๐ŸŸข | 27.3ms
26.6ms
-2.4% ๐ŸŸข | 23.1ms
19.5ms
-15.3% ๐ŸŸข | 164.2ms
132.0ms
-19.6% ๐ŸŸข | 454.2ms
462.6ms
+1.9% ๐Ÿ”ด | | uniswap_v4_pool_manager | 6.72G
6.72G
= | 3.47G
3.10G
-10.8% ๐ŸŸข | 669.8ms
605.9ms
-9.5% ๐ŸŸข | 19.0ms
18.7ms
-1.6% ๐ŸŸข | 16.0ms
15.9ms
-0.9% ๐ŸŸข | 118.5ms
150.4ms
+26.9% ๐Ÿ”ด | 428.1ms
568.1ms
+32.7% ๐Ÿ”ด | | curve_3pool | 6.61G
6.61G
= | 3.12G
3.62G
+16.1% ๐Ÿ”ด | 614.9ms
717.7ms
+16.7% ๐Ÿ”ด | 27.5ms
38.9ms
+41.4% ๐Ÿ”ด | 15.8ms
23.7ms
+50.3% ๐Ÿ”ด | 89.9ms
159.2ms
+77.2% ๐Ÿ”ด | 502.3ms
728.1ms
+44.9% ๐Ÿ”ด | | curve_tricrypto2 | 9.43G
9.43G
= | 4.66G
4.61G
-1.1% ๐ŸŸข | 927.8ms
908.1ms
-2.1% ๐ŸŸข | 26.7ms
29.2ms
+9.2% ๐Ÿ”ด | 20.1ms
22.3ms
+11.3% ๐Ÿ”ด | 120.9ms
147.7ms
+22.2% ๐Ÿ”ด | 659.1ms
615.5ms
-6.6% ๐ŸŸข | | balancer_vault | 19.07G
19.08G
+0.1% ๐Ÿ”ด | 10.54G
11.42G
+8.3% ๐Ÿ”ด | 2.051s
2.234s
+9.0% ๐Ÿ”ด | 43.2ms
56.0ms
+29.7% ๐Ÿ”ด | 32.4ms
27.5ms
-15.3% ๐ŸŸข | 621.5ms
429.0ms
-31.0% ๐ŸŸข | 941.7ms
849.0ms
-9.9% ๐ŸŸข | | succinct_staking | 10.87G
10.87G
= | 5.04G
5.30G
+5.1% ๐Ÿ”ด | 981.2ms
1.035s
+5.5% ๐Ÿ”ด | 24.2ms
29.1ms
+19.9% ๐Ÿ”ด | 13.4ms
19.7ms
+47.1% ๐Ÿ”ด | 129.8ms
144.0ms
+11.0% ๐Ÿ”ด | 398.3ms
1.044s
+162.1% ๐Ÿ”ด | | **TOTAL** | **165.31G
165.35G
=** | **94.93G
89.29G
-5.9% ๐ŸŸข** | **19.083s
17.830s
-6.6% ๐ŸŸข** | **941.7ms
426.1ms
-54.8% ๐ŸŸข** | **535.3ms
264.7ms
-50.5% ๐ŸŸข** | **3.985s
2.120s
-46.8% ๐ŸŸข** | **10.656s
7.948s
-25.4% ๐ŸŸข** |
### Jump resolution | benchmark | local | nonadj | fixpt | unres | total | |:------------------------|--------:|---------:|--------:|--------:|--------:| | fibonacci-calldata | 2 | 0 | 0 | 0 | 2 | | factorial | 2 | 0 | 0 | 0 | 2 | | counter | 11 | 0 | 0 | 0 | 11 | | snailtracer | 502 | 17 | 0 | 62 | 564 | | weth | 55 | 0 | 11 | 0 | 66 | | hash_10k | 6 | 0 | 1 | 0 | 7 | | erc20_transfer | 114 | 0 | 18 | 0 | 132 | | push0_proxy | 1 | 0 | 0 | 0 | 1 | | usdc_proxy | 54 | 0 | 14 | 0 | 68 | | fiat_token | 452 | 6 | 68 | 0 | 520 | | uniswap_v2_pair | 257 | 30 | 37 | 0 | 294 | | univ2_router | 474 | 27 | 26 | 0 | 500 | | seaport | 1026 | 39 | 153 | 0 | 1179 | | airdrop | 217 | 7 | 35 | 0 | 252 | | bswap64 | 14 | 0 | 0 | 0 | 14 | | bswap64_opt | 8 | 0 | 2 | 0 | 10 | | eip4788 | 4 | 0 | 0 | 0 | 4 | | eip2935 | 3 | 0 | 1 | 0 | 4 | | burntpix | 365 | 2 | 31 | 0 | 396 | | curve_stableswap | 42 | 5 | 9 | 0 | 51 | | onchain_lm_v2 | 0 | 0 | 2 | 0 | 2 | | usdt | 197 | 14 | 41 | 0 | 238 | | universal_router | 133 | 14 | 9 | 0 | 142 | | aggregation_router_v6 | 374 | 118 | 36 | 0 | 410 | | entry_point_v0_7 | 356 | 55 | 46 | 0 | 402 | | uniswap_v3_pool | 661 | 4 | 67 | 0 | 728 | | uniswap_v4_pool_manager | 152 | 10 | 17 | 0 | 169 | | curve_3pool | 718 | 0 | 11 | 0 | 729 | | curve_tricrypto2 | 264 | 0 | 2 | 0 | 266 | | balancer_vault | 960 | 18 | 0 | 132 | 1092 | | succinct_staking | 332 | 49 | 45 | 0 | 377 | --- .../src/bytecode/passes/block_analysis.rs | 342 +++++++++++++++++- 1 file changed, 330 insertions(+), 12 deletions(-) diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index a326de5a5..a4713ff54 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -42,6 +42,9 @@ //! values that its condition bounds on an edge, such as an index below an array length, get that //! bound there. Memory writes at ranged offsets only forget the words in their range. //! A jump to a range goes to the valid jump targets in it, such as the entries of a jump table. +//! When a jump's operand is no range but an instruction before it leaves a narrow one, such as the +//! index of a table that the code reads with a shift, the analysis runs the rest of the block on +//! each value of that range, and the jump goes to the targets it finds. //! //! ## Soundness //! @@ -283,6 +286,10 @@ const MAX_RANGE_JUMP_WIDTH: usize = 4096; /// compile time. const MAX_RANGE_JUMP_TARGETS: usize = 1024; +/// Number of values below which [`Bytecode::enumerate_jump`] runs the rest of a block on each value +/// of a range. Ranges of two values are conditions, which no table indexes. +const MAX_ENUMERATED_VALUES: u32 = 256; + /// Maximum number of segments that may continue a stack, beyond which the stack is truncated /// instead, so that the joins and the runs of a block on each segment stay cheap. const MAX_SEGS_BELOW: usize = 64; @@ -556,9 +563,32 @@ struct Fixpoint { discovered: IndexVec>, /// Constant sets referenced by the snapshots. const_sets: ConstSetInterner, + /// What enumerating ranges found for the dynamic jump that ends each block. + enumerated: IndexVec, converged: bool, } +/// What [`Bytecode::enumerate_jump`] found over all states of the dynamic jump that ends a block. +#[derive(Clone, Copy, Default)] +struct Enumerated { + /// Whether a state enumerated the range of its jump operand. + done: bool, + /// Whether the operand may be an invalid jump target in some state. + invalid: bool, + /// Whether some state has an operand that is neither resolvable nor enumerable. + failed: bool, +} + +/// Scratch stacks of [`Bytecode::enumerate_jump`]. +#[derive(Default)] +struct EnumerateBuf { + stack: Vec, + mem: MemWords, + /// The state after the instruction that leaves the range. + split_stack: Vec, + split_mem: MemWords, +} + /// Block states of [`Bytecode::run_fixpoint`], one per context of each block. struct Parts { states: IndexVec, @@ -1166,19 +1196,38 @@ impl Bytecode<'_> { jump_insts: &[Inst], fixpoint: &Fixpoint, ) -> Vec<(Inst, JumpResolution)> { - let Fixpoint { reached, const_sets, converged, .. } = fixpoint; + let Fixpoint { reached, discovered, const_sets, enumerated, converged } = fixpoint; let mut range_budget = MAX_RANGE_JUMP_TARGETS; jump_insts .iter() .map(|&jump_inst| { + let bid = self.cfg.inst_to_block[jump_inst]; // Blocks the fixpoint never reached keep their block-local snapshots. - let target = if *converged - && self.cfg.inst_to_block[jump_inst].is_some_and(|bid| !reached[bid.index()]) - { + let mut target = if *converged && bid.is_some_and(|bid| !reached[bid.index()]) { JumpResolution::bottom() } else { self.resolve_jump(jump_inst, const_sets, &mut range_budget) }; + // The snapshot of a jump operand that was enumerated is too imprecise, but the + // targets that the fixpoint discovered are all the jump can reach, as long as every + // state resolved or enumerated its operand. + if *converged + && target.is_top() + && let Some(bid) = bid + && let Enumerated { done: true, invalid, failed: false } = enumerated[bid] + { + let mut targets: SmallVec<[Inst; 4]> = + discovered[bid].iter().map(|&tb| self.cfg.blocks[tb].insts.start).collect(); + targets.sort_unstable(); + // As for a range, a single target would skip the invalid jump check. + let count = targets.len(); + if count > 1 && count <= range_budget { + range_budget -= count; + } else if count != 1 || invalid { + return (jump_inst, target); + } + target = JumpResolution::resolved(targets).with_condition(target.condition); + } (jump_inst, target) }) .collect() @@ -1451,6 +1500,19 @@ impl Bytecode<'_> { let forwarders: BitVec = self.cfg.blocks.indices().map(|bid| self.is_forwarder(bid)).collect(); let mut copies = vec![0; self.jumpdests.len()]; + // The blocks that end in a dynamic jump, whose operand ranges the fixpoint may enumerate. + let dynamic_jumps: BitVec = self + .cfg + .blocks + .iter() + .map(|block| { + let term = &self.insts[block.terminator()]; + term.is_jump() && !term.flags.contains(InstFlags::STATIC_JUMP) + }) + .collect(); + let mut enumerated = + IndexVec::::from_vec(vec![Enumerated::default(); num_blocks]); + let mut enumerate_buf = EnumerateBuf::default(); 'fixpoint: while let Some(Reverse((_, pid))) = parts.worklist.pop() { parts.queued.set(pid.index(), false); @@ -1482,6 +1544,10 @@ impl Bytecode<'_> { let term_inst = self.cfg.blocks[bid].terminator(); let mut operand = None; let mut condition = AbsValue::Top; + // The narrowest range that an instruction of a dynamic jump's block leaves on the + // stack. + let dynamic_jump = dynamic_jumps[bid.index()]; + let mut narrowest: Option<(Inst, RangeInclusive)> = None; for i in insts { if i == term_inst { operand = stack_buf.last().copied(); @@ -1493,6 +1559,16 @@ impl Bytecode<'_> { break 'fixpoint; } if self.interpret_inst(i, &mut stack_buf, truncated, &mut mem_buf, Record::Skip) { + if dynamic_jump + && i != term_inst + && let Some(&AbsValue::Range(range)) = stack_buf.last() + && (2..MAX_ENUMERATED_VALUES).contains(&(range.last - range.start)) + && narrowest + .as_ref() + .is_none_or(|(_, n)| range.last - range.start < n.last - n.start) + { + narrowest = Some((i, range)); + } continue; } // If the block reads below the state's entries rather than halts, run it on each @@ -1542,13 +1618,66 @@ impl Bytecode<'_> { // Discover dynamic-jump target edges from this context's jump operand. targets.clear(); - let term = &self.insts[term_inst]; - if term.is_jump() - && !term.flags.contains(InstFlags::STATIC_JUMP) - && condition != JumpCondition::AlwaysFalse - && let Some(operand) = operand - { - self.jump_target_blocks(operand, &const_sets, &mut targets); + if dynamic_jump && condition != JumpCondition::AlwaysFalse { + let state = &mut enumerated[bid]; + match operand { + Some(operand) => { + self.jump_target_blocks(operand, &const_sets, &mut targets); + let unresolved = match operand { + AbsValue::Top => true, + AbsValue::Range(range) => self.range_jumpdests(range).is_none(), + AbsValue::Const(_) | AbsValue::ConstSet(_) => false, + }; + if unresolved { + let len = targets.len(); + let found = narrowest.and_then(|(split, range)| { + // Each value copies the stack and runs the rest of the block. + let values = u64::from(range.last - range.start + 1); + let rest = term_inst.index() - split.index(); + let copy = stack_buf.len().min(MAX_ABS_STACK_DEPTH); + work += self.cfg.blocks[bid].insts().len() as u64 + + values * (rest + copy) as u64; + // The runs repeat folds that the analysis keeps, so they must not + // drain that budget, but their cost counts as work. + let gas = self.compiler_gas_used; + let found = self.enumerate_jump( + bid, + &parts.states[pid].1, + split, + range, + &mut enumerate_buf, + &mut targets, + ); + work += self.compiler_gas_used - gas; + self.compiler_gas_used = gas; + found + }); + match found { + Some(invalid) => { + state.done = true; + state.invalid |= invalid; + } + None => { + targets.truncate(len); + state.failed = true; + } + } + } else { + // Sets only hold valid targets, and a range may hold others. + state.invalid |= match operand { + AbsValue::Const(_) => self.jump_target_pc(operand).is_none(), + AbsValue::ConstSet(_) => false, + AbsValue::Range(_) | AbsValue::Top => true, + }; + if let AbsValue::Const(imm) = operand + && !self.fits_switch_case(imm) + { + state.failed = true; + } + } + } + None => state.failed = true, + } for &tb in &targets { if discovered_set.insert((bid, tb)) { discovered[bid].push(tb); @@ -1679,7 +1808,75 @@ impl Bytecode<'_> { } } let reached = parts.by_key.iter().map(|p| !p.is_empty()).collect(); - Fixpoint { reached, discovered, const_sets, converged } + Fixpoint { reached, discovered, const_sets, enumerated, converged } + } + + /// Finds the targets of the dynamic jump that ends `bid` by running the rest of the block on + /// each value of `range`, which the instruction `split` leaves on top of the stack, such as + /// the index of a jump table that the code reads with a shift. + /// + /// `entry` is the state at the entry of the block. Returns `None` if a value leaves a jump + /// operand that is not constant, and otherwise whether a value jumps to an invalid target, + /// after adding the blocks of the valid targets to `targets`. A value whose path halts before + /// the jump has no target. + fn enumerate_jump( + &mut self, + bid: Block, + entry: &BlockState, + split: Inst, + range: RangeInclusive, + buf: &mut EnumerateBuf, + targets: &mut SmallVec<[Block; 4]>, + ) -> Option { + let BlockState::Known { stack, truncated, mem, .. } = entry else { return None }; + let EnumerateBuf { stack: values, mem: values_mem, split_stack, split_mem } = buf; + split_stack.clone_from(stack); + split_mem.clone_from(mem); + let term = self.cfg.blocks[bid].terminator(); + for i in self.cfg.blocks[bid].insts().take_while(|&i| i <= split) { + if !self.interpret_inst(i, split_stack, *truncated, split_mem, Record::Skip) { + return None; + } + } + + let mut invalid = false; + for value in range.start..=range.last { + values.clone_from(split_stack); + values_mem.clone_from(split_mem); + let imm = U256Imm::new(U256::from(value), &mut self.u256_interner.borrow_mut()); + *values.last_mut()? = AbsValue::Const(imm); + let mut rest = + self.cfg.blocks[bid].insts().skip_while(|&i| i <= split).take_while(|&i| i < term); + let reaches_jump = + rest.all(|i| self.interpret_inst(i, values, *truncated, values_mem, Record::Skip)); + if !reaches_jump { + continue; + } + let target = *values.last()?; + let AbsValue::Const(_) = target else { return None }; + match self.jump_target_pc(target) { + Some(pc) => { + let tb = self.cfg.inst_to_block[self.pc_to_inst(pc as usize)]?; + if !targets.contains(&tb) { + targets.push(tb); + } + } + None => { + let AbsValue::Const(imm) = target else { return None }; + if !self.fits_switch_case(imm) { + return None; + } + invalid = true; + } + } + } + Some(invalid) + } + + /// Returns whether a switch on the low 64 bits of a jump operand tells `imm` from the valid + /// jump targets, which it does unless `imm` has bits above them. + fn fits_switch_case(&self, imm: U256Imm) -> bool { + u64::try_from(imm.get(&self.u256_interner.borrow())).is_ok() } /// Returns what the condition of the `JUMPI` that ends `bid` shows about its successors. @@ -4721,6 +4918,127 @@ mod tests_edge_cases { assert!(bytecode.has_dynamic_jumps); } + /// Solar's `-Ogas` dispatcher shifts a packed table of `u16` targets right by `16 * (selector % + /// n)`. The shift of a constant by a range is not a range, so only enumerating the values of + /// the index finds the targets. + #[test] + fn packed_jump_table() { + let src = |entry1: &str, table: &str| { + format!( + " + PUSH0 + CALLDATALOAD + PUSH1 0xe0 + SHR ; selector + PUSH1 0x03 + DUP2 + MOD + PUSH1 0x04 + SHL ; 16 * (selector % 3) + PUSH6 {table} + SWAP1 + SHR + PUSH2 0xffff + AND + JUMP + JUMPDEST ; 0x1a + STOP + {entry1} ; 0x1c + STOP + JUMPDEST ; 0x1e + STOP + " + ) + }; + let bytecode = analyze_asm(&src("JUMPDEST", "0x001e001c001a")); + assert!(!bytecode.has_dynamic_jumps); + assert_eq!(multi_jump_target_pcs(&bytecode), [0x1a, 0x1c, 0x1e]); + + // An entry that is not a jump target fails at runtime, in the switch's default case. + let bytecode = analyze_asm(&src("STOP", "0x001e001c001a")); + assert!(!bytecode.has_dynamic_jumps); + assert_eq!(multi_jump_target_pcs(&bytecode), [0x1a, 0x1e]); + + // A static jump to the only target would skip that failure. + let bytecode = analyze_asm(&src("STOP", "0x001c001c001a")); + assert!(bytecode.has_dynamic_jumps); + + // The switch compares only the low 64 bits of the operand, so an invalid target above + // them could take a valid one. + let wide = |entry2: &str| { + format!( + " + PUSH0 + CALLDATALOAD + PUSH1 0xe0 + SHR + PUSH1 0x03 + DUP2 + MOD + PUSH1 0x48 + MUL ; 72 * (selector % 3) + PUSH27 0x{entry2}000000000000000038000000000000000036 + SWAP1 + SHR + PUSH9 0xffffffffffffffffff + AND + JUMP + JUMPDEST ; 0x36 + STOP + JUMPDEST ; 0x38 + STOP + JUMPDEST ; 0x3a + STOP + " + ) + }; + let bytecode = analyze_asm(&wide("00000000000000003a")); + assert!(!bytecode.has_dynamic_jumps); + let bytecode = analyze_asm(&wide("01000000000000003a")); + assert!(bytecode.has_dynamic_jumps); + + // A narrow range that is not the index of the jump operand. + let bytecode = analyze_asm( + " + JUMPDEST + JUMPDEST + JUMPDEST + PUSH0 + CALLDATALOAD + PUSH1 0x03 + AND + PUSH0 + CALLDATALOAD + JUMP + ", + ); + assert!(bytecode.has_dynamic_jumps); + + // A narrow range below the jump operand, which the jump leaves on top of the stack. + let bytecode = analyze_asm( + " + JUMPDEST + JUMPDEST + JUMPDEST + PUSH0 + CALLDATALOAD + PUSH1 0x03 + AND + PUSH0 + CALLDATALOAD + PUSH %end + JUMPI + PUSH0 + CALLDATALOAD + JUMP + end: + JUMPDEST + STOP + ", + ); + assert!(bytecode.has_dynamic_jumps); + } + /// A view that overrides every opcode leaves the stack opcodes unsupported, without stack /// inputs. Analysis must reject them rather than read the stack. #[test] From da5e6ace24f4791a527add6ea97d3fadf30f1cb0 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Thu, 29 Oct 2026 14:20:17 +0100 Subject: [PATCH 4/6] fix(inspectors): report the gas delta as step cost on out-of-gas (#752) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A step that runs out of gas consumes the rest of the frame's gas, so its cost is that remainder. This is what reth's previous tracer reported, in both the parity vmTrace and the geth struct logs: the gas remaining before the step minus the gas remaining after it, for every step including out-of-gas ones. evm2 diverged in two ways. A skipped cold load halts after charging only the warm base cost, so the vmTrace showed 100 instead of the 642 gas that was left. And #564 made struct logs report the attempted charge on a failed deduction, which differs from reth (a cold SSTORE with 5000 gas left reported 22100, reth reports 5000). `CallTraceStep::gas_cost` is now always the gas delta, and `Interpreter::failed_charge` and the per-step bookkeeping behind it are removed, since only #564 used them. The interpreter still zeroes the remaining gas on `OutOfGas` before `step_end`, so the delta equals the remainder. The reentrancy sentry still costs 0, since it halts before charging. This fixes seven EXTCODESIZE vmTrace costs (100 โ†’ 642) for mainnet transaction `0x5f108da9a84d82e0357361df1d4d8aa3fa52ebcefdbc848234c914200853b9d8`, found while comparing paradigmxyz/reth#25002 against main. The new test checks both formats against values taken from reth's previous tracer: cold BALANCE, EXTCODE*, SLOAD and CALL with 642 gas left report 642, a cold SSTORE with 5000 left reports 5000, PUSH1 with 2 left reports 2, a value CALL with 5000 left reports 5000, and a 32-byte LOG0 with 385 left reports 385. --------- Co-authored-by: DaniPopes <57450786+DaniPopes@users.noreply.github.com> --- crates/evm2/src/interpreter/runtime.rs | 19 ------ crates/inspectors/src/tracing/mod.rs | 11 ++-- crates/inspectors/tests/it/geth.rs | 5 +- crates/inspectors/tests/it/parity.rs | 3 + crates/inspectors/tests/it/parity/oog_cost.rs | 58 +++++++++++++++++++ 5 files changed, 71 insertions(+), 25 deletions(-) create mode 100644 crates/inspectors/tests/it/parity/oog_cost.rs diff --git a/crates/evm2/src/interpreter/runtime.rs b/crates/evm2/src/interpreter/runtime.rs index 3cbe3c999..258138f66 100644 --- a/crates/evm2/src/interpreter/runtime.rs +++ b/crates/evm2/src/interpreter/runtime.rs @@ -44,8 +44,6 @@ pub struct Interpreter<'frame, 'host, T: EvmTypesHost> { pub(in crate::interpreter) gas: Gas, pub(in crate::interpreter) result: Result, error: Option, - step_gas_before: u64, - failed_charge: Option, spec: SpecId, features: EvmFeatures, is_static: bool, @@ -88,8 +86,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { memory: Memory::new(), result: Ok(()), error: None, - step_gas_before: 0, - failed_charge: None, output: 0..0, tx_env: None, message: None, @@ -114,8 +110,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.memory.clear(); self.result = Ok(()); self.error = None; - self.step_gas_before = 0; - self.failed_charge = None; self.output = 0..0; self.tx_env = Some(tx_env); self.message = Some(message); @@ -267,15 +261,6 @@ impl<'frame, 'host, T: EvmTypesHost> Interpreter<'frame, 'host, T> { self.result = result; } - /// Returns the execution gas the current step attempted to charge when it ran out of gas. - /// - /// Only set during an inspector's `step_end`, where the remaining gas has already been - /// zeroed after the failed charge. - #[inline] - pub const fn failed_charge(&self) -> Option { - self.failed_charge - } - /// Sets the current instruction result to `stop`. #[inline] pub const fn set_stop(&mut self, stop: InstrStop) { @@ -719,8 +704,6 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { let mut inspector = self.0.inspector.unwrap_unchecked(); inspector.as_mut().step(&mut self.0); } - self.0.step_gas_before = self.0.gas.remaining(); - self.0.failed_charge = None; } #[inline] @@ -729,8 +712,6 @@ impl<'frame, 'host, T: EvmTypesHost> InterpreterState<'frame, 'host, T> { self.0.stack_len = stack_len; if self.0.result.is_err_and(InstrStop::is_out_of_gas) { cold_path(); - self.0.failed_charge = - Some(self.0.step_gas_before.wrapping_sub(self.0.gas.remaining())); // Failed charges may leave a wrapped counter until frame settlement. self.0.gas.set_remaining(0); } diff --git a/crates/inspectors/src/tracing/mod.rs b/crates/inspectors/src/tracing/mod.rs index f0274b885..f5f545159 100644 --- a/crates/inspectors/src/tracing/mod.rs +++ b/crates/inspectors/src/tracing/mod.rs @@ -652,10 +652,13 @@ impl TracingInspector { } // The gas cost is the difference between the recorded gas remaining at the start of the - // step the remaining gas here, at the end of the step. - step.gas_cost = interp - .failed_charge() - .unwrap_or_else(|| step.gas_remaining.saturating_sub(interp.gas().remaining())); + // step and the remaining gas here, at the end of the step. An out-of-gas step consumes + // the whole remainder, except the reentrancy sentry, which halts before any charge. + step.gas_cost = if interp.result() == Err(InstrStop::ReentrancySentryOOG) { + 0 + } else { + step.gas_remaining.saturating_sub(interp.gas().remaining()) + }; let state_gas_delta = interp.gas().state_gas_spent().saturating_sub(step.state_gas_spent); if step.state_gas_reservoir.is_some() && state_gas_delta != 0 { step.state_gas_cost = Some(state_gas_delta); diff --git a/crates/inspectors/tests/it/geth.rs b/crates/inspectors/tests/it/geth.rs index da57616be..c0147e4e6 100644 --- a/crates/inspectors/tests/it/geth.rs +++ b/crates/inspectors/tests/it/geth.rs @@ -1854,7 +1854,8 @@ fn test_geth_struct_log_out_of_gas_cost() { let mut insp = TracingInspector::new(TracingInspectorConfig::default_geth()); let mut evm = context.build_mainnet().with_inspector(&mut insp); - // Leaves 5000 gas for the cold zero-to-nonzero SSTORE, which costs 22100. + // Leaves 5000 gas for the cold zero-to-nonzero SSTORE, which would cost 22100. The step + // consumes the 5000 left. let res = evm .inspect_tx(TxEnv { caller, @@ -1876,7 +1877,7 @@ fn test_geth_struct_log_out_of_gas_cost() { let sstore = frame.struct_logs.last().unwrap(); assert_eq!(sstore.opcode(), "SSTORE"); assert_eq!(sstore.gas, 5_000); - assert_eq!(sstore.gas_cost, 22_100); + assert_eq!(sstore.gas_cost, 5_000); } /// EIP-7708: a transfer log of a reverted call is rolled back and does not consume an index. diff --git a/crates/inspectors/tests/it/parity.rs b/crates/inspectors/tests/it/parity.rs index 0e1e4a6c8..ff440f8cf 100644 --- a/crates/inspectors/tests/it/parity.rs +++ b/crates/inspectors/tests/it/parity.rs @@ -1145,3 +1145,6 @@ mod state_diff_birth; #[path = "parity/state_diff_selfdestruct.rs"] mod state_diff_selfdestruct; + +#[path = "parity/oog_cost.rs"] +mod oog_cost; diff --git a/crates/inspectors/tests/it/parity/oog_cost.rs b/crates/inspectors/tests/it/parity/oog_cost.rs new file mode 100644 index 000000000..3d344c0a6 --- /dev/null +++ b/crates/inspectors/tests/it/parity/oog_cost.rs @@ -0,0 +1,58 @@ +//! The cost of a step that runs out of gas is the gas it consumed, in both trace formats. + +use super::*; +use alloy_primitives::Bytes; +use alloy_rpc_types_trace::geth::GethDefaultTracingOptions; +use evm2::interpreter::InstrStop; + +fn inspect_child(child: &[u8], gas: u16) -> TracingInspector { + let mut parent = hex!("5f5f5f5f5f604361").to_vec(); + parent.extend(gas.to_be_bytes()); + parent.extend([op::CALL, op::STOP]); + inspect_code(&parent, child, SpecId::PRAGUE, TracingInspectorConfig::all()) +} + +#[test] +fn out_of_gas_step_costs_the_consumed_gas() { + // `gas` is the child's gas limit. The last opcode of each child runs out of gas with + // `remaining` left, which it consumes entirely, except for the reentrancy sentry that halts + // before charging anything. + for (opcode, code, gas, remaining, cost, status) in [ + ("BALANCE", &hex!("604431")[..], 645, 642, 642, InstrStop::OutOfGas), + ("EXTCODESIZE", &hex!("60443b")[..], 645, 642, 642, InstrStop::OutOfGas), + ("EXTCODEHASH", &hex!("60443f")[..], 645, 642, 642, InstrStop::OutOfGas), + ("EXTCODECOPY", &hex!("60015f5f60443c")[..], 652, 642, 642, InstrStop::OutOfGas), + ("SLOAD", &hex!("5f54")[..], 644, 642, 642, InstrStop::OutOfGas), + ("CALL", &hex!("5f5f5f5f5f60445af1")[..], 657, 642, 642, InstrStop::OutOfGas), + ("CALLCODE", &hex!("5f5f5f5f5f60445af2")[..], 657, 642, 642, InstrStop::OutOfGas), + ("DELEGATECALL", &hex!("5f5f5f5f60445af4")[..], 655, 642, 642, InstrStop::OutOfGas), + ("STATICCALL", &hex!("5f5f5f5f60445afa")[..], 655, 642, 642, InstrStop::OutOfGas), + ("PUSH1", &hex!("6001")[..], 2, 2, 2, InstrStop::OutOfGas), + ("SSTORE", &hex!("602a5f55")[..], 5005, 5000, 5000, InstrStop::OutOfGas), + ("CALL", &hex!("5f5f5f5f600160445ff1")[..], 5016, 5000, 5000, InstrStop::OutOfGas), + ("LOG0", &hex!("60205fa0")[..], 390, 385, 385, InstrStop::OutOfGas), + ("SSTORE", &hex!("5f5f55")[..], 100, 96, 0, InstrStop::ReentrancySentryOOG), + ] { + let inspector = inspect_child(code, gas); + let step = inspector.traces().nodes()[1].trace.steps.last().unwrap(); + assert_eq!(step.status, Some(status), "{opcode}"); + assert_eq!(step.gas_remaining, remaining, "{opcode}"); + assert_eq!(step.gas_cost, cost, "{opcode}"); + + let geth = inspector.geth_builder().geth_traces( + 0, + Bytes::new(), + GethDefaultTracingOptions::default(), + ); + let failed = geth.struct_logs.iter().rfind(|log| log.depth == 2).unwrap(); + assert_eq!(failed.opcode(), opcode); + assert_eq!(failed.gas, remaining, "{opcode}"); + assert_eq!(failed.gas_cost, cost, "{opcode}"); + + let trace = inspector.into_parity_builder().vm_trace(); + let failed = trace.ops[7].sub.as_ref().unwrap().ops.last().unwrap(); + assert_eq!(failed.op.as_deref(), Some(opcode)); + assert_eq!(failed.cost, cost, "{opcode}"); + assert!(failed.ex.is_none() && failed.sub.is_none(), "{opcode}"); + } +} From 2d80db947a78d827f590100e60078688de3a926f Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:41:01 +0100 Subject: [PATCH 5/6] perf(jit): track the memory words that a constant CODECOPY writes (#839) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vyper's dense selector table computes `selector % n << 1 + base`, copies two bytes of the table out of the code into the end of the first memory word with `CODECOPY(0x1e, src, 2)`, and jumps to that word with `PUSH0 MLOAD JUMP`. The word is the table entry over zeros, but the analysis forgot every word that a `CODECOPY` wrote, so the jump stayed dynamic even with the jump-table enumeration from #836. `MemWords` now tracks the word that a constant `CODECOPY` of 1 to 32 bytes leaves at the end of its destination (when that ends at or after byte 32), if the rest of the word is known: either a tracked word at that offset, or zeros when memory is pristine. A new `pristine` flag records that no instruction has written memory yet. It is set only in the entry block and only when the caller passes no stack, every memory-writing opcode and the host path clear it, and joins keep it only when it holds on every path. On the mainnet corpus (2306 codes, `evm2 run --parse-only`), dynamic jumps go from 1798 to 1792 and the contracts that have any from 23 to 21: `0xD37A6aa3d8460Bd2b6536d608103D880695A23CD` goes from 5 to 0 and `0x45312ea0eFf7E09C83CBE249fa1d7598c4C8cd4e` from 1 to 0. No code gets more dynamic jumps. JIT compile of `0xD37Aโ€ฆ` with `evm2 run --jit-only` takes 1.702G instructions on `main` and 1.460G here (-14%). ## Benchmarks The assembly of all 31 benches is unchanged (`opt.s changed in 0/31`), so the compile-time and runtime deltas below come from machine noise (the runner's load average was above 100 during the run) and not from this change. The instruction columns are within 0.6% everywhere; snailtracer, the largest, is +0.6%. Head `da5e6ace24` with uncommitted changes vs base `origin/main` (`da5e6ace24`). ### Codegen statistics `opt.s` changed in 0/31 benchmarks. | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------|-----------:|---------:|--------:|-----------:|-------------:|--------------:|----------:|---------:|----------:| | **TOTAL** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | | **GEOMEAN** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** | **=** |
Full details | benchmark | unopt.ll | opt.ll | opt.s | jit size | i256 loads | i256 stores | mresize | spills | reloads | |:------------------------|----------------------------:|--------------------------:|--------------------------:|----------------------------:|------------------------:|------------------------:|----------------------:|----------------------:|------------------------:| | fibonacci-calldata | 335
335
= | 114
114
= | 355
355
= | 348 B
348 B
= | 2
2
= | 2
2
= | 2
2
= | 6
6
= | 5
5
= | | factorial | 331
331
= | 116
116
= | 391
391
= | 478 B
478 B
= | 2
2
= | 2
2
= | 2
2
= | 7
7
= | 7
7
= | | counter | 992
992
= | 341
341
= | 603
603
= | 785 B
785 B
= | 9
9
= | 9
9
= | 3
3
= | 0
0
- | 0
0
- | | snailtracer | 60156
60156
= | 26888
26888
= | 33501
33501
= | 95.2 KiB
95.2 KiB
= | 1515
1515
= | 1973
1973
= | 449
449
= | 16
16
= | 231
231
= | | weth | 11578
11578
= | 2825
2825
= | 4518
4518
= | 14.0 KiB
14.0 KiB
= | 178
178
= | 237
237
= | 25
25
= | 32
32
= | 92
92
= | | hash_10k | 991
991
= | 354
354
= | 670
670
= | 1.4 KiB
1.4 KiB
= | 17
17
= | 20
20
= | 5
5
= | 1
1
= | 1
1
= | | erc20_transfer | 12073
12073
= | 3811
3811
= | 4978
4978
= | 13.9 KiB
13.9 KiB
= | 160
160
= | 322
322
= | 53
53
= | 6
6
= | 6
6
= | | push0_proxy | 313
313
= | 125
125
= | 286
286
= | 350 B
350 B
= | 1
1
= | 15
15
= | 0
0
- | 0
0
- | 0
0
- | | usdc_proxy | 6054
6054
= | 1583
1583
= | 2274
2274
= | 5.5 KiB
5.5 KiB
= | 54
54
= | 131
131
= | 21
21
= | 5
5
= | 5
5
= | | fiat_token | 75544
75544
= | 21440
21440
= | 29239
29239
= | 92.8 KiB
92.8 KiB
= | 1320
1320
= | 1902
1902
= | 277
277
= | 66
66
= | 65
65
= | | uniswap_v2_pair | 41668
41668
= | 12039
12039
= | 16662
16662
= | 52.5 KiB
52.5 KiB
= | 680
680
= | 1079
1079
= | 147
147
= | 55
55
= | 434
434
= | | univ2_router | 80476
80476
= | 24414
24414
= | 32740
32740
= | 112.0 KiB
112.0 KiB
= | 1675
1675
= | 2262
2262
= | 320
320
= | 107
107
= | 436
436
= | | seaport | 133044
133044
= | 51832
51832
= | 68657
68657
= | 233.6 KiB
233.6 KiB
= | 3785
3785
= | 4386
4386
= | 782
782
= | 245
245
= | 3057
3057
= | | airdrop | 26607
26607
= | 8462
8462
= | 11032
11032
= | 31.6 KiB
31.6 KiB
= | 432
432
= | 666
666
= | 91
91
= | 30
30
= | 211
211
= | | bswap64 | 1757
1757
= | 361
361
= | 622
622
= | 951 B
951 B
= | 12
12
= | 19
19
= | 6
6
= | 0
0
- | 0
0
- | | bswap64_opt | 1496
1496
= | 346
346
= | 632
632
= | 1008 B
1008 B
= | 5
5
= | 25
25
= | 6
6
= | 0
0
- | 0
0
- | | eip4788 | 631
631
= | 249
249
= | 459
459
= | 670 B
670 B
= | 12
12
= | 13
13
= | 2
2
= | 2
2
= | 2
2
= | | eip2935 | 488
488
= | 190
190
= | 420
420
= | 554 B
554 B
= | 7
7
= | 5
5
= | 2
2
= | 0
0
- | 0
0
- | | burntpix | 9179
9179
= | 3048
3048
= | 3880
3880
= | 11.9 KiB
11.9 KiB
= | 163
163
= | 284
284
= | 26
26
= | 9
9
= | 20
20
= | | curve_stableswap | 16363
16363
= | 5666
5666
= | 7170
7170
= | 20.5 KiB
20.5 KiB
= | 324
324
= | 506
506
= | 44
44
= | 18
18
= | 20
20
= | | onchain_lm_v2 | 53236
53236
= | 17441
17441
= | 25264
25264
= | 74.7 KiB
74.7 KiB
= | 1087
1087
= | 1294
1294
= | 155
155
= | 57
57
= | 1138
1138
= | | usdt | 34574
34574
= | 8007
8007
= | 10687
10687
= | 31.4 KiB
31.4 KiB
= | 473
473
= | 657
657
= | 52
52
= | 39
39
= | 47
47
= | | universal_router | 77644
77644
= | 29450
29450
= | 36915
36915
= | 121.0 KiB
121.0 KiB
= | 2195
2195
= | 2850
2850
= | 232
232
= | 197
197
= | 1221
1221
= | | aggregation_router_v6 | 125084
125084
= | 45234
45234
= | 52441
52441
= | 171.4 KiB
171.4 KiB
= | 3087
3087
= | 4672
4672
= | 313
313
= | 74
74
= | 846
846
= | | entry_point_v0_7 | 66940
66940
= | 25010
25010
= | 31306
31306
= | 103.9 KiB
103.9 KiB
= | 1771
1771
= | 2081
2081
= | 501
501
= | 47
47
= | 854
854
= | | uniswap_v3_pool | 117314
117314
= | 33907
33907
= | 44303
44303
= | 146.1 KiB
146.1 KiB
= | 2275
2275
= | 2598
2598
= | 432
432
= | 131
131
= | 1345
1345
= | | uniswap_v4_pool_manager | 93527
93527
= | 32124
32124
= | 42527
42527
= | 141.6 KiB
141.6 KiB
= | 2241
2241
= | 2625
2625
= | 358
358
= | 185
185
= | 1146
1146
= | | curve_3pool | 105047
105047
= | 30590
30590
= | 47304
47304
= | 170.2 KiB
170.2 KiB
= | 1784
1784
= | 2299
2299
= | 460
460
= | 374
374
= | 3164
3164
= | | curve_tricrypto2 | 114225
114225
= | 31974
31974
= | 49484
49484
= | 175.7 KiB
175.7 KiB
= | 2030
2030
= | 2635
2635
= | 342
342
= | 645
645
= | 1683
1683
= | | balancer_vault | 145873
145873
= | 51478
51478
= | 64838
64838
= | 219.1 KiB
219.1 KiB
= | 3384
3384
= | 4377
4377
= | 508
508
= | 83
83
= | 1493
1493
= | | succinct_staking | 85875
85875
= | 26294
26294
= | 38378
38378
= | 117.3 KiB
117.3 KiB
= | 1508
1508
= | 2340
2340
= | 214
214
= | 98
98
= | 2534
2534
= | | **TOTAL** | **1499415
1499415
=** | **495713
495713
=** | **662536
662536
=** | **2.1 MiB
2.1 MiB
=** | **32188
32188
=** | **42286
42286
=** | **5830
5830
=** | **2535
2535
=** | **20063
20063
=** |
### Compile times | benchmark | instructions | cycles | time | |:------------|---------------:|------------:|------------:| | **TOTAL** | **=** | **+1.3% ๐Ÿ”ด** | **+1.7% ๐Ÿ”ด** | | **GEOMEAN** | **=** | **-0.9% ๐ŸŸข** | **-0.3% ๐ŸŸข** |
Full compile times | benchmark | instructions | cycles | time | parse | translate | optimize | codegen | |:------------------------|------------------------------:|--------------------------------:|----------------------------------:|----------------------------------:|----------------------------------:|---------------------------------:|---------------------------------:| | fibonacci-calldata | 37.64M
37.61M
-0.1% ๐ŸŸข | 35.97M
35.62M
-1.0% ๐ŸŸข | 10.8ms
10.6ms
-2.0% ๐ŸŸข | 189.5ยตs
209.1ยตs
+10.3% ๐Ÿ”ด | 404.5ยตs
556.7ยตs
+37.6% ๐Ÿ”ด | 1.29ms
1.55ms
+20.0% ๐Ÿ”ด | 2.83ms
3.31ms
+16.7% ๐Ÿ”ด | | factorial | 41.84M
41.80M
-0.1% ๐ŸŸข | 34.50M
35.22M
+2.1% ๐Ÿ”ด | 10.4ms
9.87ms
-5.3% ๐ŸŸข | 185.7ยตs
228.0ยตs
+22.8% ๐Ÿ”ด | 380.7ยตs
425.5ยตs
+11.8% ๐Ÿ”ด | 1.24ms
1.40ms
+13.1% ๐Ÿ”ด | 3.02ms
3.53ms
+16.8% ๐Ÿ”ด | | counter | 54.45M
54.43M
= | 34.04M
35.33M
+3.8% ๐Ÿ”ด | 8.26ms
8.78ms
+6.3% ๐Ÿ”ด | 333.4ยตs
455.0ยตs
+36.5% ๐Ÿ”ด | 462.9ยตs
614.5ยตs
+32.7% ๐Ÿ”ด | 1.27ms
1.65ms
+30.5% ๐Ÿ”ด | 3.58ms
4.29ms
+19.6% ๐Ÿ”ด | | snailtracer | 5.30G
5.33G
+0.6% ๐Ÿ”ด | 1.99G
2.04G
+2.6% ๐Ÿ”ด | 381.3ms
390.7ms
+2.4% ๐Ÿ”ด | 31.9ms
39.2ms
+22.8% ๐Ÿ”ด | 7.49ms
8.29ms
+10.6% ๐Ÿ”ด | 71.8ms
83.3ms
+16.1% ๐Ÿ”ด | 308.3ms
349.5ms
+13.4% ๐Ÿ”ด | | weth | 540.93M
540.86M
= | 244.52M
247.78M
+1.3% ๐Ÿ”ด | 49.5ms
49.0ms
-0.8% ๐ŸŸข | 2.47ms
2.28ms
-7.9% ๐ŸŸข | 2.03ms
2.17ms
+6.8% ๐Ÿ”ด | 10.9ms
13.7ms
+25.3% ๐Ÿ”ด | 32.4ms
36.1ms
+11.5% ๐Ÿ”ด | | hash_10k | 80.72M
80.61M
-0.1% ๐ŸŸข | 45.75M
48.66M
+6.4% ๐Ÿ”ด | 11.1ms
11.2ms
+0.9% ๐Ÿ”ด | 274.9ยตs
355.9ยตs
+29.5% ๐Ÿ”ด | 751.2ยตs
553.3ยตs
-26.3% ๐ŸŸข | 1.93ms
2.35ms
+22.1% ๐Ÿ”ด | 5.14ms
5.80ms
+12.9% ๐Ÿ”ด | | erc20_transfer | 578.86M
579.16M
+0.1% ๐Ÿ”ด | 251.92M
251.58M
-0.1% ๐ŸŸข | 49.9ms
49.9ms
= | 2.35ms
2.93ms
+24.8% ๐Ÿ”ด | 3.07ms
2.23ms
-27.5% ๐ŸŸข | 11.8ms
13.0ms
+9.9% ๐Ÿ”ด | 33.3ms
38.4ms
+15.5% ๐Ÿ”ด | | push0_proxy | 34.87M
34.81M
-0.2% ๐ŸŸข | 28.51M
26.05M
-8.6% ๐ŸŸข | 9.74ms
9.45ms
-3.0% ๐ŸŸข | 133.4ยตs
180.2ยตs
+35.1% ๐Ÿ”ด | 328.5ยตs
510.9ยตs
+55.5% ๐Ÿ”ด | 812.7ยตs
1.09ms
+34.7% ๐Ÿ”ด | 2.13ms
2.59ms
+21.2% ๐Ÿ”ด | | usdc_proxy | 245.10M
245.11M
= | 120.02M
115.32M
-3.9% ๐ŸŸข | 24.8ms
24.5ms
-1.3% ๐ŸŸข | 1.39ms
1.42ms
+2.3% ๐Ÿ”ด | 1.25ms
1.47ms
+17.7% ๐Ÿ”ด | 5.93ms
7.16ms
+20.6% ๐Ÿ”ด | 14.6ms
16.2ms
+10.9% ๐Ÿ”ด | | fiat_token | 3.99G
3.99G
= | 1.63G
1.60G
-2.4% ๐ŸŸข | 312.6ms
304.9ms
-2.5% ๐ŸŸข | 14.4ms
14.5ms
+0.6% ๐Ÿ”ด | 10.9ms
12.1ms
+11.3% ๐Ÿ”ด | 71.0ms
81.8ms
+15.2% ๐Ÿ”ด | 240.1ms
278.7ms
+16.1% ๐Ÿ”ด | | uniswap_v2_pair | 2.06G
2.06G
= | 861.79M
858.61M
-0.4% ๐ŸŸข | 167.7ms
166.1ms
-1.0% ๐ŸŸข | 7.78ms
7.91ms
+1.7% ๐Ÿ”ด | 6.33ms
6.78ms
+7.2% ๐Ÿ”ด | 35.6ms
38.2ms
+7.3% ๐Ÿ”ด | 128.2ms
144.6ms
+12.8% ๐Ÿ”ด | | univ2_router | 4.40G
4.40G
= | 1.81G
1.82G
+0.4% ๐Ÿ”ด | 347.8ms
349.4ms
+0.5% ๐Ÿ”ด | 15.8ms
17.0ms
+7.2% ๐Ÿ”ด | 11.1ms
12.0ms
+8.4% ๐Ÿ”ด | 68.8ms
81.1ms
+17.8% ๐Ÿ”ด | 281.7ms
331.3ms
+17.6% ๐Ÿ”ด | | seaport | 9.75G
9.76G
= | 4.25G
4.30G
+1.3% ๐Ÿ”ด | 803.7ms
814.2ms
+1.3% ๐Ÿ”ด | 26.4ms
29.3ms
+11.0% ๐Ÿ”ด | 18.8ms
22.2ms
+18.0% ๐Ÿ”ด | 176.0ms
232.2ms
+31.9% ๐Ÿ”ด | 683.7ms
887.7ms
+29.8% ๐Ÿ”ด | | airdrop | 1.42G
1.42G
= | 586.86M
589.11M
+0.4% ๐Ÿ”ด | 113.6ms
114.3ms
+0.7% ๐Ÿ”ด | 4.79ms
5.17ms
+8.0% ๐Ÿ”ด | 4.21ms
4.37ms
+3.8% ๐Ÿ”ด | 31.0ms
32.5ms
+5.0% ๐Ÿ”ด | 83.2ms
93.0ms
+11.7% ๐Ÿ”ด | | bswap64 | 67.38M
67.41M
= | 38.64M
38.06M
-1.5% ๐ŸŸข | 9.56ms
9.50ms
-0.6% ๐ŸŸข | 512.1ยตs
516.1ยตs
+0.8% ๐Ÿ”ด | 580.8ยตs
657.1ยตs
+13.1% ๐Ÿ”ด | 1.58ms
1.87ms
+18.3% ๐Ÿ”ด | 3.78ms
4.71ms
+24.5% ๐Ÿ”ด | | bswap64_opt | 74.02M
74.01M
= | 42.63M
38.90M
-8.8% ๐ŸŸข | 10.2ms
9.44ms
-7.0% ๐ŸŸข | 415.5ยตs
642.7ยตs
+54.7% ๐Ÿ”ด | 496.1ยตs
613.3ยตs
+23.6% ๐Ÿ”ด | 1.58ms
1.93ms
+21.9% ๐Ÿ”ด | 5.85ms
5.96ms
+1.9% ๐Ÿ”ด | | eip4788 | 48.76M
48.73M
= | 33.08M
29.33M
-11.3% ๐ŸŸข | 8.16ms
7.33ms
-10.2% ๐ŸŸข | 199.8ยตs
247.5ยตs
+23.8% ๐Ÿ”ด | 440.0ยตs
449.7ยตs
+2.2% ๐Ÿ”ด | 1.01ms
1.22ms
+20.1% ๐Ÿ”ด | 4.54ms
6.31ms
+39.0% ๐Ÿ”ด | | eip2935 | 42.53M
42.54M
= | 38.52M
31.20M
-19.0% ๐ŸŸข | 11.3ms
11.2ms
-1.0% ๐ŸŸข | 200.2ยตs
196.0ยตs
-2.1% ๐ŸŸข | 408.4ยตs
565.0ยตs
+38.4% ๐Ÿ”ด | 1.00ms
1.21ms
+20.0% ๐Ÿ”ด | 2.89ms
3.52ms
+21.5% ๐Ÿ”ด | | burntpix | 6.72G
6.72G
= | 2.70G
2.66G
-1.6% ๐ŸŸข | 514.4ms
503.9ms
-2.0% ๐ŸŸข | 14.9ms
14.5ms
-2.6% ๐ŸŸข | 1.67ms
1.88ms
+12.2% ๐Ÿ”ด | 10.7ms
10.8ms
+0.6% ๐Ÿ”ด | 28.3ms
31.2ms
+10.5% ๐Ÿ”ด | | curve_stableswap | 1.14G
1.14G
= | 488.27M
486.24M
-0.4% ๐ŸŸข | 96.2ms
94.5ms
-1.8% ๐ŸŸข | 3.21ms
3.45ms
+7.5% ๐Ÿ”ด | 2.60ms
2.89ms
+11.1% ๐Ÿ”ด | 23.7ms
26.5ms
+12.0% ๐Ÿ”ด | 47.4ms
55.1ms
+16.2% ๐Ÿ”ด | | onchain_lm_v2 | 3.68G
3.68G
= | 1.44G
1.43G
-0.7% ๐ŸŸข | 277.9ms
273.7ms
-1.5% ๐ŸŸข | 10.4ms
11.3ms
+8.2% ๐Ÿ”ด | 7.39ms
8.04ms
+8.7% ๐Ÿ”ด | 46.7ms
55.0ms
+17.8% ๐Ÿ”ด | 186.7ms
220.9ms
+18.3% ๐Ÿ”ด | | usdt | 1.35G
1.35G
= | 551.22M
554.58M
+0.6% ๐Ÿ”ด | 106.5ms
108.1ms
+1.4% ๐Ÿ”ด | 6.65ms
6.88ms
+3.4% ๐Ÿ”ด | 5.24ms
5.78ms
+10.5% ๐Ÿ”ด | 25.1ms
27.6ms
+10.2% ๐Ÿ”ด | 77.4ms
86.3ms
+11.5% ๐Ÿ”ด | | universal_router | 11.60G
11.60G
= | 4.89G
4.89G
= | 922.6ms
922.3ms
= | 13.9ms
15.1ms
+8.6% ๐Ÿ”ด | 11.3ms
12.6ms
+11.4% ๐Ÿ”ด | 81.0ms
105.5ms
+30.2% ๐Ÿ”ด | 316.1ms
376.3ms
+19.1% ๐Ÿ”ด | | aggregation_router_v6 | 35.22G
35.22G
= | 14.83G
14.56G
-1.8% ๐ŸŸข | 2.781s
2.731s
-1.8% ๐ŸŸข | 22.6ms
23.2ms
+2.3% ๐Ÿ”ด | 18.4ms
21.0ms
+14.3% ๐Ÿ”ด | 140.6ms
192.6ms
+37.0% ๐Ÿ”ด | 515.0ms
632.6ms
+22.8% ๐Ÿ”ด | | entry_point_v0_7 | 17.94G
17.94G
= | 8.04G
8.83G
+9.8% ๐Ÿ”ด | 1.503s
1.719s
+14.4% ๐Ÿ”ด | 10.8ms
11.0ms
+1.2% ๐Ÿ”ด | 10.3ms
10.6ms
+3.0% ๐Ÿ”ด | 66.5ms
79.1ms
+19.0% ๐Ÿ”ด | 267.0ms
321.4ms
+20.4% ๐Ÿ”ด | | uniswap_v3_pool | 6.22G
6.22G
= | 3.22G
2.95G
-8.3% ๐ŸŸข | 640.1ms
580.2ms
-9.4% ๐ŸŸข | 22.2ms
22.7ms
+2.2% ๐Ÿ”ด | 16.9ms
18.7ms
+10.8% ๐Ÿ”ด | 110.3ms
132.4ms
+20.0% ๐Ÿ”ด | 401.0ms
502.9ms
+25.4% ๐Ÿ”ด | | uniswap_v4_pool_manager | 6.72G
6.72G
= | 3.10G
3.40G
+9.9% ๐Ÿ”ด | 604.0ms
662.6ms
+9.7% ๐Ÿ”ด | 16.1ms
17.6ms
+9.3% ๐Ÿ”ด | 14.5ms
15.7ms
+8.1% ๐Ÿ”ด | 105.9ms
131.8ms
+24.5% ๐Ÿ”ด | 392.4ms
476.1ms
+21.3% ๐Ÿ”ด | | curve_3pool | 6.61G
6.61G
= | 2.99G
3.00G
+0.1% ๐Ÿ”ด | 585.1ms
577.8ms
-1.3% ๐ŸŸข | 22.1ms
23.9ms
+8.0% ๐Ÿ”ด | 15.8ms
16.2ms
+2.4% ๐Ÿ”ด | 85.1ms
101.5ms
+19.3% ๐Ÿ”ด | 431.4ms
456.6ms
+5.8% ๐Ÿ”ด | | curve_tricrypto2 | 9.42G
9.43G
= | 3.62G
3.90G
+7.7% ๐Ÿ”ด | 696.1ms
754.5ms
+8.4% ๐Ÿ”ด | 19.7ms
21.3ms
+8.2% ๐Ÿ”ด | 15.8ms
17.0ms
+7.9% ๐Ÿ”ด | 94.5ms
102.5ms
+8.4% ๐Ÿ”ด | 423.6ms
452.2ms
+6.8% ๐Ÿ”ด | | balancer_vault | 19.07G
19.08G
= | 8.22G
8.26G
+0.5% ๐Ÿ”ด | 1.550s
1.565s
+1.0% ๐Ÿ”ด | 28.7ms
29.0ms
+1.0% ๐Ÿ”ด | 19.7ms
20.6ms
+4.2% ๐Ÿ”ด | 202.1ms
242.3ms
+19.9% ๐Ÿ”ด | 642.3ms
832.6ms
+29.6% ๐Ÿ”ด | | succinct_staking | 10.87G
10.87G
= | 4.44G
4.46G
+0.3% ๐Ÿ”ด | 841.4ms
843.5ms
+0.3% ๐Ÿ”ด | 21.3ms
21.2ms
-0.5% ๐ŸŸข | 12.5ms
12.3ms
-1.2% ๐ŸŸข | 100.5ms
101.0ms
+0.6% ๐Ÿ”ด | 425.0ms
321.3ms
-24.4% ๐ŸŸข | | **TOTAL** | **165.33G
165.37G
=** | **70.61G
71.52G
+1.3% ๐Ÿ”ด** | **13.459s
13.686s
+1.7% ๐Ÿ”ด** | **322.4ms
343.7ms
+6.6% ๐Ÿ”ด** | **221.5ms
239.9ms
+8.3% ๐Ÿ”ด** | **1.587s
1.906s
+20.1% ๐Ÿ”ด** | **5.993s
6.981s
+16.5% ๐Ÿ”ด** |
### Runtime | benchmark | instructions/iter | cycles/iter | |:------------|--------------------:|--------------:| | counter | +1.1% ๐Ÿ”ด | +1.7% ๐Ÿ”ด | | push0_proxy | -1.0% ๐ŸŸข | -1.7% ๐ŸŸข | | usdc_proxy | -2.1% ๐ŸŸข | -0.5% ๐ŸŸข | | **TOTAL** | **=** | **+0.1% ๐Ÿ”ด** | | **GEOMEAN** | **-0.1% ๐ŸŸข** | **+0.1% ๐Ÿ”ด** |
Full runtime | benchmark | instructions/iter | cycles/iter | |:------------------------|------------------------------:|----------------------------------:| | fibonacci-calldata | 44.09K
44.25K
+0.4% ๐Ÿ”ด | 10.55K
10.58K
+0.3% ๐Ÿ”ด | | factorial | 79.89K
80.08K
+0.2% ๐Ÿ”ด | 18.01K
17.98K
-0.1% ๐ŸŸข | | counter | 23.13K
23.39K
+1.1% ๐Ÿ”ด | 7.22K
7.34K
+1.7% ๐Ÿ”ด | | snailtracer | 196.51M
196.51M
= | 58.37M
58.61M
+0.4% ๐Ÿ”ด | | weth | 28.57K
28.34K
-0.8% ๐ŸŸข | 9.29K
9.37K
+0.9% ๐Ÿ”ด | | hash_10k | 8.99M
8.99M
= | 2.82M
2.85M
+1.1% ๐Ÿ”ด | | erc20_transfer | 19.18M
19.19M
= | 5.36M
5.39M
+0.6% ๐Ÿ”ด | | push0_proxy | 23.49K
23.24K
-1.0% ๐ŸŸข | 8.01K
7.87K
-1.7% ๐ŸŸข | | usdc_proxy | 25.19K
24.65K
-2.1% ๐ŸŸข | 8.40K
8.36K
-0.5% ๐ŸŸข | | fiat_token | 22.84K
22.85K
= | 7.71K
7.64K
-0.9% ๐ŸŸข | | uniswap_v2_pair | 23.03K
23.20K
+0.7% ๐Ÿ”ด | 7.83K
7.81K
-0.3% ๐ŸŸข | | univ2_router | 20.85K
21.01K
+0.8% ๐Ÿ”ด | 7.11K
7.11K
-0.1% ๐ŸŸข | | seaport | 23.54K
23.40K
-0.6% ๐ŸŸข | 7.89K
7.88K
-0.1% ๐ŸŸข | | airdrop | 23.19K
23.05K
-0.6% ๐ŸŸข | 7.81K
7.79K
-0.3% ๐ŸŸข | | bswap64 | 20.10K
19.93K
-0.9% ๐ŸŸข | 6.80K
6.75K
-0.7% ๐ŸŸข | | bswap64_opt | 20.10K
20.09K
= | 6.75K
6.73K
-0.3% ๐ŸŸข | | eip4788 | 24.24K
24.47K
+0.9% ๐Ÿ”ด | 7.94K
8.07K
+1.7% ๐Ÿ”ด | | eip2935 | 23.07K
22.96K
-0.5% ๐ŸŸข | 7.58K
7.75K
+2.2% ๐Ÿ”ด | | burntpix | 103.29M
103.35M
+0.1% ๐Ÿ”ด | 27.62M
27.02M
-2.2% ๐ŸŸข | | curve_stableswap | 263.14K
262.99K
-0.1% ๐ŸŸข | 109.08K
109.85K
+0.7% ๐Ÿ”ด | | onchain_lm_v2 | 521.51M
521.51M
= | 216.36M
217.11M
+0.3% ๐Ÿ”ด | | usdt | 39.39K
39.48K
+0.2% ๐Ÿ”ด | 12.40K
12.33K
-0.5% ๐ŸŸข | | universal_router | 154.06K
154.23K
+0.1% ๐Ÿ”ด | 52.01K
52.85K
+1.6% ๐Ÿ”ด | | aggregation_router_v6 | 486.03K
486.52K
+0.1% ๐Ÿ”ด | 169.55K
171.47K
+1.1% ๐Ÿ”ด | | entry_point_v0_7 | 1.30M
1.30M
= | 325.19K
326.39K
+0.4% ๐Ÿ”ด | | uniswap_v3_pool | 84.96K
84.70K
-0.3% ๐ŸŸข | 26.51K
26.54K
+0.1% ๐Ÿ”ด | | uniswap_v4_pool_manager | 70.40K
70.39K
= | 21.10K
21.07K
-0.1% ๐ŸŸข | | curve_3pool | 83.17K
83.30K
+0.1% ๐Ÿ”ด | 27.75K
27.27K
-1.7% ๐ŸŸข | | curve_tricrypto2 | 226.89K
226.73K
-0.1% ๐ŸŸข | 83.43K
83.42K
= | | balancer_vault | 125.68K
125.28K
-0.3% ๐ŸŸข | 41.24K
41.59K
+0.8% ๐Ÿ”ด | | succinct_staking | 67.29K
66.64K
-1.0% ๐ŸŸข | 23.68K
23.73K
+0.2% ๐Ÿ”ด | | **TOTAL** | **852.81M
852.87M
=** | **311.56M
312.02M
+0.1% ๐Ÿ”ด** |
--- .../src/bytecode/passes/block_analysis.rs | 198 +++++++++++++++--- 1 file changed, 170 insertions(+), 28 deletions(-) diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index a4713ff54..58c6288b9 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -296,20 +296,38 @@ const MAX_SEGS_BELOW: usize = 64; /// Memory words at constant offsets whose values are known, sorted by offset and disjoint. /// -/// Only words written by `MSTORE` are tracked, so memory already covers a word that `MLOAD` -/// folds and the load needs no memory expansion. Every instruction that writes memory clears -/// the words it may overlap, and joins keep only the words known on every path. The number of -/// words is not capped: dropping a word to make room for another would make the transfer -/// non-monotone (a more precise state could forget a word that a less precise one keeps), which -/// lets the fixpoint reach a block that the committed jump resolutions no longer justify. +/// Only words written by `MSTORE` or by a constant `CODECOPY` are tracked, so memory already +/// covers a word that `MLOAD` folds and the load needs no memory expansion. Every instruction +/// that writes memory clears the words it may overlap, and joins keep only the words known on +/// every path. The number of words is not capped: dropping a word to make room for another would +/// make the transfer non-monotone (a more precise state could forget a word that a less precise +/// one keeps), which lets the fixpoint reach a block that the committed jump resolutions no +/// longer justify. #[derive(Clone, Debug, Default, PartialEq, Eq)] -struct MemWords(SmallVec<[(u32, AbsValue); 4]>); +struct MemWords { + words: SmallVec<[(u32, AbsValue); 4]>, + /// Whether no instruction has written memory yet, so every byte not in `words` is zero. + pristine: bool, +} impl MemWords { - /// Applies the memory effect of `opcode` with the given `inputs` (TOS last). + /// Returns the state at the start of the code, where memory is empty if the caller passes no + /// stack. + fn entry(stack_observed: bool) -> Self { + Self { words: SmallVec::new(), pristine: !stack_observed } + } + + /// Applies the memory effect of `opcode` with the given `inputs` (TOS last), where `code` is + /// the code being analyzed. /// /// Returns the known value loaded by an `MLOAD`. - fn transfer(&mut self, opcode: u8, inputs: &[AbsValue]) -> Option { + fn transfer( + &mut self, + opcode: u8, + inputs: &[AbsValue], + code: &[u8], + interner: &mut U256Interner, + ) -> Option { // Offsets and lengths that fit in an inline immediate, and their bounds; larger ones are // unknown. let imm = |depth: usize| inputs[inputs.len() - 1 - depth].as_const()?.get_inline(); @@ -318,8 +336,8 @@ impl MemWords { match opcode { op::MLOAD => { let offset = imm(0)?; - let i = self.0.binary_search_by_key(&offset, |&(o, _)| o).ok()?; - return Some(self.0[i].1); + let i = self.words.binary_search_by_key(&offset, |&(o, _)| o).ok()?; + return Some(self.words[i].1); } op::MSTORE => { let value = inputs[inputs.len() - 2]; @@ -327,14 +345,18 @@ impl MemWords { if let Some(offset) = imm(0) && value != AbsValue::Top { - let i = self.0.partition_point(|&(o, _)| o < offset); - self.0.insert(i, (offset, value)); + self.set(offset, value); } } op::MSTORE8 => self.clobber(bounds(0), Some(1)), - op::CALLDATACOPY | op::CODECOPY | op::RETURNDATACOPY | op::MCOPY => { - self.clobber(bounds(0), len(2)) + op::CODECOPY => { + let word = self.codecopy_word(imm(0), imm(1), imm(2), code, interner); + self.clobber(bounds(0), len(2)); + if let Some((offset, value)) = word { + self.set(offset, AbsValue::Const(U256Imm::new(value, interner))); + } } + op::CALLDATACOPY | op::RETURNDATACOPY | op::MCOPY => self.clobber(bounds(0), len(2)), op::EXTCODECOPY => self.clobber(bounds(1), len(3)), op::CALL | op::CALLCODE => self.clobber(bounds(5), len(6)), op::DELEGATECALL | op::STATICCALL => self.clobber(bounds(4), len(5)), @@ -343,33 +365,71 @@ impl MemWords { None } + /// Returns the word that a `CODECOPY` of `len` bytes at `src` to `dest` leaves at the end of + /// its destination, where memory expansion covers it, if the rest of that word is known: a + /// tracked word at the same offset, or zeros when memory is `pristine`. + fn codecopy_word( + &self, + dest: Option, + src: Option, + len: Option, + code: &[u8], + interner: &U256Interner, + ) -> Option<(u32, U256)> { + let (dest, src, len) = (dest?, src? as usize, len?); + let offset = dest.checked_add(len)?.checked_sub(32).filter(|_| (1..=32).contains(&len))?; + let len = len as usize; + let base = match self.words.binary_search_by_key(&offset, |&(o, _)| o) { + Ok(i) => self.words[i].1.as_const()?.get(interner), + Err(_) if self.pristine => U256::ZERO, + Err(_) => return None, + }; + let mut word = base.to_be_bytes::<32>(); + let copied = &mut word[32 - len..]; + copied.fill(0); + // Bytes past the end of the code are zero. + if let Some(code) = code.get(src..) { + let n = len.min(code.len()); + copied[..n].copy_from_slice(&code[..n]); + } + Some((offset, U256::from_be_bytes(word))) + } + + /// Records the known `value` of the word at `offset`. + fn set(&mut self, offset: u32, value: AbsValue) { + let i = self.words.partition_point(|&(o, _)| o < offset); + self.words.insert(i, (offset, value)); + } + /// Forgets every word that a write of at most `len` bytes at an offset in `offset` may - /// overlap. + /// overlap, and clears `pristine`. fn clobber(&mut self, offset: Option>, len: Option) { + self.pristine = false; match (offset, len) { (_, Some(0)) => {} (Some(offset), len) => { // Inline immediates are below 2^31, so these sums cannot overflow. let end = len.map_or(u32::MAX, |len| offset.last + len); - self.0.retain(|&mut (o, _)| o + 32 <= offset.start || end <= o); + self.words.retain(|&mut (o, _)| o + 32 <= offset.start || end <= o); } - (None, _) => self.0.clear(), + (None, _) => self.words.clear(), } } /// Keeps the words known in both states, joining their values. Returns `true` if `self` /// changed. fn join(&mut self, other: &Self, sets: &mut ConstSetInterner) -> bool { - let len = self.0.len(); - let mut changed = false; - self.0.retain(|(offset, value)| { - let Ok(i) = other.0.binary_search_by_key(offset, |&(o, _)| o) else { return false }; - let joined = sets.join(*value, other.0[i].1); + let len = self.words.len(); + let mut changed = self.pristine && !other.pristine; + self.pristine &= other.pristine; + self.words.retain(|(offset, value)| { + let Ok(i) = other.words.binary_search_by_key(offset, |&(o, _)| o) else { return false }; + let joined = sets.join(*value, other.words[i].1); changed |= joined != *value; *value = joined; joined != AbsValue::Top }); - changed || self.0.len() != len + changed || self.words.len() != len } } @@ -1471,8 +1531,9 @@ impl Bytecode<'_> { // Entry block starts with an empty stack, or with unknown entries when the caller may // pass a stack. let no_mem = MemWords::default(); + let entry_mem = MemWords::entry(self.stack_observed()); let entry = Block::from_usize(0); - parts.join(entry, 0, &[], self.stack_observed(), &[], &no_mem, &mut const_sets, &budget); + parts.join(entry, 0, &[], self.stack_observed(), &[], &entry_mem, &mut const_sets, &budget); // Discovered dynamic-jump target edges per block. let mut discovered: IndexVec> = @@ -1976,7 +2037,7 @@ impl Bytecode<'_> { (tracked as u8, truncated || stack.len() > tracked, continued).hash(&mut hasher); match mode { ContextMode::Full | ContextMode::Folded => { - for &(offset, value) in &mem.0 { + for &(offset, value) in &mem.words { if let Some(pc) = self.jump_target_pc(value) { (offset, pc).hash(&mut hasher); } @@ -2191,11 +2252,16 @@ impl Bytecode<'_> { None } else if inst.flags.contains(InstFlags::HOST) { // The host's instruction may write any memory and any stack word. - mem.0.clear(); + *mem = MemWords::default(); stack.fill(AbsValue::Top); None } else { - mem.transfer(inst.opcode, &stack[stack.len() - inp..]) + mem.transfer( + inst.opcode, + &stack[stack.len() - inp..], + &self.code, + &mut self.u256_interner.borrow_mut(), + ) }; // Try constant folding for common arithmetic, respecting the gas budget. @@ -4385,6 +4451,11 @@ mod tests_edge_cases { ]; for op in ["CALLDATACOPY", "CODECOPY", "RETURNDATACOPY", "MCOPY", "PUSH0 EXTCODECOPY"] { for (operands, expected) in cases { + // A constant `CODECOPY` ending the word rewrites it, see + // `mload_word_after_codecopy`. + if op == "CODECOPY" && operands == "PUSH 1 PUSH0 PUSH 0x5f" { + continue; + } assert_eq!( mload_0x40_after(&format!("{operands} {op}")), expected, @@ -4394,6 +4465,28 @@ mod tests_edge_cases { } } + /// A constant `CODECOPY` that ends the word at `0x40` replaces its last bytes with the copied + /// code, and zeros past the end of the code. + #[test] + fn mload_word_after_codecopy() { + for len in [1u8, 2, 16, 31, 32] { + for src in [0usize, 3, 20, 1000] { + let copy = format!("PUSH {len} PUSH {src} PUSH {:#x} CODECOPY", 0x60 - len); + let code = crate::parse_asm(&format!( + "PUSH 0x80 PUSH 0x40 MSTORE\n{copy}\nPUSH 0x40 MLOAD PUSH0 MSTORE STOP" + )) + .unwrap(); + let mut memory = [0u8; 0x60]; + memory[0x5f] = 0x80; + for (i, byte) in memory[0x60 - len as usize..].iter_mut().enumerate() { + *byte = code.get(src + i).copied().unwrap_or(0); + } + let expected = U256::from_be_slice(&memory[0x40..]); + assert_eq!(mload_0x40_after(©), Some(expected), "{copy}"); + } + } + } + #[test] fn mload_known_word_after_call() { let known = Some(U256::from(0x80)); @@ -4902,6 +4995,55 @@ mod tests_edge_cases { assert_eq!(multi_jump_target_pcs(&bytecode), [0x20, 0x22, 0x24, 0x26]); } + /// Vyper's dense selector table copies the entry for `selector % n` from the code into the + /// last two bytes of the first word of memory, which is all zeros otherwise, and jumps to that + /// word. + #[test] + fn codecopy_jump_table() { + let src = |prefix: &str| { + format!( + " + {prefix} + PUSH0 + CALLDATALOAD + PUSH1 0xe0 + SHR ; selector + PUSH1 0x03 + SWAP1 + MOD + PUSH1 0x01 + SHL ; 2 * (selector % 3) + PUSH %table + ADD + PUSH1 0x01 + ADD ; skip the PUSH6 opcode + PUSH1 0x02 + SWAP1 + PUSH1 0x1e + CODECOPY ; memory[0x1e..0x20] = entry + PUSH0 + MLOAD + JUMP + JUMPDEST ; 0x1b + STOP + JUMPDEST ; 0x1d + STOP + JUMPDEST ; 0x1f + STOP + table: + PUSH6 0x001f001d001b + " + ) + }; + let bytecode = analyze_asm(&src("")); + assert!(!bytecode.has_dynamic_jumps); + assert_eq!(multi_jump_target_pcs(&bytecode), [0x1b, 0x1d, 0x1f]); + + // The word is only known over zeros: after a write to memory the rest of it is unknown. + let bytecode = analyze_asm(&src("PUSH0 CALLDATALOAD PUSH1 0x40 MSTORE")); + assert!(bytecode.has_dynamic_jumps); + } + /// Range jumps resolve to at most [`MAX_RANGE_JUMP_TARGETS`] targets in total, since each /// becomes its own switch. #[test] From 592bf18c4e22fff54ac9b4db606c4ee276e345cb Mon Sep 17 00:00:00 2001 From: adityapk00 <31996805+adityapk00@users.noreply.github.com> Date: Thu, 29 Oct 2026 11:41:39 -0400 Subject: [PATCH 6/6] feat: expose transaction state changes in place with TxStateChanges (#840) Adds `TxStateChanges`, a borrowed view of one transaction's state changes, returned by `ExecutedTx::changes()` (in place, without detaching) and `PendingState::changes()`. It implements `StateChangeSource`, and `ExecutedTx` now does too. Lets callers such as reth's commit-condition path read what a transaction wrote (balances, storage slots) before choosing to commit or discard it, without paying for `detach` and reattach. --- crates/evm2/src/evm/mod.rs | 52 +++++++++++++++++- crates/evm2/src/evm/state/mod.rs | 27 ++-------- crates/evm2/src/evm/state/pending.rs | 81 +++++++++++++++++++++++++--- crates/evm2/src/evm/tx.rs | 36 ++++++++++--- crates/evm2/src/lib.rs | 2 +- 5 files changed, 160 insertions(+), 38 deletions(-) diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 6c4a60c34..fb1b6b400 100644 --- a/crates/evm2/src/evm/mod.rs +++ b/crates/evm2/src/evm/mod.rs @@ -180,7 +180,7 @@ pub use state::{ AccountChangeRef, AccountChanges, AccountFlags, AccountHandle, AccountInfo, BlockStateAccumulator, JournalEntry, NoopChangeSink, PendingState, State, StateChangeSink, StateChangeSource, StateCheckpoint, StateInner, StateSnapshot, StorageChange, StorageHandle, - StorageOverlay, StorageSlot, StorageSlotHandle, Tee, Tracked, + StorageOverlay, StorageSlot, StorageSlotHandle, Tee, Tracked, TxStateChanges, }; pub(crate) mod prewarm_set; @@ -4620,6 +4620,56 @@ mod tests { assert!(!next.account_changes().find(|c| c.address == target).unwrap().touched); } + #[test] + fn changes_inspect_transaction_state_before_commit_or_discard() { + let caller = Address::repeat_byte(0xaa); + let target = Address::repeat_byte(0x11); + // SSTORE(0, 1) + let code = vec![op::PUSH1, 1, op::PUSH0, op::SSTORE, op::STOP]; + let mut database = InMemoryDB::default(); + database.insert_account_info( + &target, + AccountInfo::default().with_nonce(1).with_code(legacy_bytecode(code)), + ); + let mut evm = Evm::::new( + SpecId::CANCUN, + BlockEnvExt::default(), + ethereum_tx_registry(SpecId::CANCUN), + database, + Precompiles::base(SpecId::CANCUN), + ); + let tx = Recovered::new_unchecked( + TxEnvelope::Legacy(TxLegacy { + to: TxKind::Call(target), + gas_limit: 100_000, + ..Default::default() + }), + caller, + ); + + let executed = evm.transact(&tx).unwrap(); + assert!(executed.result().status); + let changes = executed.changes(); + assert!(!changes.is_empty()); + assert_eq!(changes.account_info(&caller).unwrap().nonce, 1); + let slot = changes.account_changes_for(&target).unwrap().storage.slots[&Word::ZERO].value; + assert_eq!(slot.current, Word::from(1)); + let mut in_place = changes.account_changes().map(|c| c.address).collect::>(); + in_place.sort(); + let _ = executed.discard(); + + // Discarding after inspection leaves the accepted state unchanged, so the same transaction + // executes again, and its detached state reports the same accounts. + let executed = evm.transact(&tx).unwrap(); + assert!(executed.result().status); + let pending = executed.detach().pending_state; + let mut detached = + pending.changes().account_changes().map(|c| c.address).collect::>(); + detached.sort(); + assert_eq!(in_place, detached); + assert_eq!(pending.changes().account_info(&caller).unwrap().nonce, 1); + } + #[derive(Default)] struct CallCounter { calls: usize, diff --git a/crates/evm2/src/evm/state/mod.rs b/crates/evm2/src/evm/state/mod.rs index 57177c61f..b1eaff64d 100644 --- a/crates/evm2/src/evm/state/mod.rs +++ b/crates/evm2/src/evm/state/mod.rs @@ -17,7 +17,7 @@ pub use block::BlockStateAccumulator; #[cfg(feature = "account-ext")] pub use extension::AccountExtension; pub use journal::{JournalEntry, StateCheckpoint}; -pub use pending::PendingState; +pub use pending::{PendingState, TxStateChanges}; pub use storage::{StorageHandle, StorageOverlay, StorageSlot, StorageSlotHandle}; pub use stream::{ AccountChangeRef, AccountChanges, NoopChangeSink, StateChangeSink, StateChangeSource, @@ -981,29 +981,10 @@ impl<'a> State<'a> { Ok(()) } - /// Visits transaction state changes in database application order: each account's bytecode - /// and storage precede its metadata. - /// - /// This borrows changes directly from the transaction layer without detaching it and does not - /// mutate the accepted overlay. - pub(crate) fn visit_transaction_changes( - &self, - sink: &mut S, - ) -> Result<(), S::Error> { - for changes in self.transaction_account_changes() { - changes.visit(sink)?; - } - Ok(()) - } - - /// Returns each account loaded by the transaction with its changes, in an unspecified order. - /// - /// This borrows the transaction layer without detaching it. + /// Returns a borrowed view of the transaction layer's changes, without detaching it. #[inline] - pub(crate) fn transaction_account_changes(&self) -> impl Iterator> { - self.accounts.iter().map(|(&address, account)| { - account.changes(address, self.selfdestructs.contains(&address)) - }) + pub(crate) fn transaction_changes(&self) -> TxStateChanges<'_> { + TxStateChanges::new(&self.accounts, &self.selfdestructs) } /// Detaches the transaction overlay into an owned [`PendingState`]. diff --git a/crates/evm2/src/evm/state/pending.rs b/crates/evm2/src/evm/state/pending.rs index 651e28dfa..42bc64fc7 100644 --- a/crates/evm2/src/evm/state/pending.rs +++ b/crates/evm2/src/evm/state/pending.rs @@ -97,21 +97,23 @@ impl PendingState { self.accounts.values().any(|account| account.is_changed() || account.storage.is_changed()) } + /// Returns a borrowed view of the transaction's changes. + #[inline] + pub const fn changes(&self) -> TxStateChanges<'_> { + TxStateChanges::new(&self.accounts, &self.selfdestructs) + } + /// Returns each loaded account's changes, in an unspecified account order. #[inline] pub fn account_changes(&self) -> impl Iterator> { - self.accounts.iter().map(|(&address, account)| { - account.changes(address, self.selfdestructs.contains(&address)) - }) + self.changes().account_changes() } /// Returns the changes of the account at `address`, or `None` if the transaction did not load /// it. #[inline] pub fn account_changes_for(&self, address: &Address) -> Option> { - self.accounts - .get(address) - .map(|account| account.changes(*address, self.selfdestructs.contains(address))) + self.changes().account_changes_for(address) } } @@ -125,6 +127,73 @@ impl StateChangeSource for PendingState { /// Changed accounts โ€” including created or selfdestructed accounts whose info ended up /// unchanged โ€” go through [`StateChangeSink::account`]; loaded-but-unchanged entries go /// through the read callbacks. + fn visit(&self, sink: &mut S) -> Result<(), S::Error> { + self.changes().visit(sink) + } +} + +/// A borrowed view of one transaction's state changes, whether still attached to the EVM or +/// detached into a [`PendingState`]. +/// +/// Returned by [`ExecutedTx::changes`](crate::ExecutedTx::changes), which borrows the +/// transaction layer in place, and by [`PendingState::changes`]. It lets a caller inspect what a +/// transaction wrote before deciding to commit or discard it, without detaching the state. +#[derive(Clone, Copy, Debug, Default)] +pub struct TxStateChanges<'a> { + inner: Option<(&'a AddressMap, &'a AddressSet)>, +} + +impl<'a> TxStateChanges<'a> { + #[inline] + pub(crate) const fn new( + accounts: &'a AddressMap, + selfdestructs: &'a AddressSet, + ) -> Self { + Self { inner: Some((accounts, selfdestructs)) } + } + + /// Returns a view with no changes. + #[inline] + pub const fn empty() -> Self { + Self { inner: None } + } + + /// Returns whether the transaction loaded no accounts and no storage. + #[inline] + pub fn is_empty(&self) -> bool { + self.inner.is_none_or(|(accounts, _)| accounts.is_empty()) + } + + /// Returns the current account information when the transaction loaded the account and it + /// exists after the transaction. + #[inline] + pub fn account_info(&self, address: &Address) -> Option<&'a AccountInfo> { + self.inner?.0.get(address).and_then(|account| account.present.as_ref()) + } + + /// Returns each loaded account's changes, in an unspecified account order. + #[inline] + pub fn account_changes(self) -> impl Iterator> { + self.inner.into_iter().flat_map(|(accounts, selfdestructs)| { + accounts.iter().map(|(&address, account)| { + account.changes(address, selfdestructs.contains(&address)) + }) + }) + } + + /// Returns the changes of the account at `address`, or `None` if the transaction did not load + /// it. + #[inline] + pub fn account_changes_for(&self, address: &Address) -> Option> { + let (accounts, selfdestructs) = self.inner?; + accounts + .get(address) + .map(|account| account.changes(*address, selfdestructs.contains(address))) + } +} + +impl StateChangeSource for TxStateChanges<'_> { + /// Visits the transaction's loaded entries as [`PendingState`]'s implementation does. fn visit(&self, sink: &mut S) -> Result<(), S::Error> { for changes in self.account_changes() { changes.visit(sink)?; diff --git a/crates/evm2/src/evm/tx.rs b/crates/evm2/src/evm/tx.rs index 3d55d23df..c304d5664 100644 --- a/crates/evm2/src/evm/tx.rs +++ b/crates/evm2/src/evm/tx.rs @@ -1,6 +1,9 @@ //! Transaction execution lifecycle and result types. -use super::{AccountChanges, BlockStateAccumulator, Evm, PendingState, StateChangeSink}; +use super::{ + AccountChanges, BlockStateAccumulator, Evm, PendingState, StateChangeSink, StateChangeSource, + TxStateChanges, +}; use crate::{EvmTypesHost, interpreter::InstrStop}; use alloc::vec::Vec; use alloy_primitives::{Address, Bytes, Log}; @@ -264,10 +267,21 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { /// and then [`commit`](Self::commit) or [`discard`](Self::discard) the transaction. #[inline] pub fn account_changes(&self) -> impl Iterator> { - self.has_pending_state() - .then(|| self.evm.state.transaction_account_changes()) - .into_iter() - .flatten() + self.changes().account_changes() + } + + /// Returns a borrowed view of the transaction's state changes. + /// + /// Like [`account_changes`](Self::account_changes), this borrows the transaction state in + /// place, so a caller can inspect what the transaction wrote, including single accounts and + /// storage slots, before deciding to [`commit`](Self::commit) or [`discard`](Self::discard). + #[inline] + pub fn changes(&self) -> TxStateChanges<'_> { + if self.has_pending_state() { + self.evm.state.transaction_changes() + } else { + TxStateChanges::empty() + } } /// Accepts the transaction state into the internal accepted overlay. @@ -297,7 +311,7 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { sink: &mut S, ) -> Result, S::Error> { if self.has_pending_state() { - self.evm.state.visit_transaction_changes(sink)?; + self.evm.state.transaction_changes().visit(sink)?; self.commit_pending_state(); } Ok(self.take_result()) @@ -322,7 +336,7 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { sink: &mut S, ) -> Result, S::Error> { if self.has_pending_state() { - self.evm.state.visit_transaction_changes(sink)?; + self.evm.state.transaction_changes().visit(sink)?; self.clear_pending_state(); } Ok(self.take_result()) @@ -344,6 +358,14 @@ impl<'evm, 'host, T: EvmTypesHost> ExecutedTx<'evm, 'host, T> { } } +impl StateChangeSource for ExecutedTx<'_, '_, T> { + /// Visits the transaction's changes in place, as [`PendingState`]'s implementation does for + /// detached state. + fn visit(&self, sink: &mut S) -> Result<(), S::Error> { + self.changes().visit(sink) + } +} + impl Drop for ExecutedTx<'_, '_, T> { #[inline] fn drop(&mut self) { diff --git a/crates/evm2/src/lib.rs b/crates/evm2/src/lib.rs index 16abc9e30..f32e35036 100644 --- a/crates/evm2/src/lib.rs +++ b/crates/evm2/src/lib.rs @@ -24,7 +24,7 @@ pub mod evm; pub use evm::config::EvmTypesHost; pub use evm::{ AccountInfo, BlockStateAccumulator, Evm, ExecutedTx, InterpreterRunner, JournalEntry, - PendingState, TxResult, TxResultExt, TxResultWithState, config, + PendingState, TxResult, TxResultExt, TxResultWithState, TxStateChanges, config, config::{ BaseEvmConfig, BaseEvmConfigSelector, BaseEvmTypes, EvmConfig, EvmConfigSelector, EvmTypes, ExecutionConfig,