diff --git a/crates/evm2/src/evm/mod.rs b/crates/evm2/src/evm/mod.rs index 91792e36b..416ef289b 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; @@ -4636,6 +4636,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/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/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, 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 { 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}"); + } +} diff --git a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs index de1a1cad4..58c6288b9 100644 --- a/crates/jit/codegen/src/bytecode/passes/block_analysis.rs +++ b/crates/jit/codegen/src/bytecode/passes/block_analysis.rs @@ -29,10 +29,22 @@ //! 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. //! 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 //! @@ -274,22 +286,48 @@ 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; + /// 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(); @@ -298,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]; @@ -307,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)), @@ -323,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 } } @@ -364,6 +444,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 +458,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 +475,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 +542,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 +555,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 +588,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 +602,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, @@ -510,9 +623,96 @@ 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, + /// 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 +1200,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 +1250,49 @@ 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, 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 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() + } + fn resolve_jump( &self, jump_inst: Inst, @@ -1268,6 +1509,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 +1520,20 @@ 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_mem = MemWords::entry(self.stack_observed()); 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(), &[], &entry_mem, &mut const_sets, &budget); // Discovered dynamic-jump target edges per block. let mut discovered: IndexVec> = @@ -1304,6 +1541,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 +1550,33 @@ 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()]; + // 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))) = 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 +1584,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,17 +1599,77 @@ 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; + // 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(); 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) { + 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 + // 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 { @@ -1361,13 +1679,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); @@ -1379,7 +1750,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 +1814,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,8 +1868,76 @@ impl Bytecode<'_> { } } } - let reached = parts.iter().map(|p| !p.is_empty()).collect(); - Fixpoint { reached, discovered, const_sets, converged } + let reached = parts.by_key.iter().map(|p| !p.is_empty()).collect(); + 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. @@ -1546,7 +2018,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 +2027,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 => { - for &(offset, value) in &mem.0 { - if let Some(pc) = jump_target(value) { + ContextMode::Full | ContextMode::Folded => { + for &(offset, value) in &mem.words { + if let Some(pc) = self.jump_target_pc(value) { (offset, pc).hash(&mut hasher); } } @@ -1592,6 +2062,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 +2193,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,95 +2226,42 @@ 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]); + // 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::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); + op::DUP1..=op::DUP16 | op::DUPN => { + stack.push(stack[stack.len() - inp]); } - 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::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 } 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. @@ -2407,6 +2879,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 +3569,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 +3603,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 +4268,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 +4287,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 +4300,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 @@ -3978,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, @@ -3987,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)); @@ -4237,6 +4737,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 = @@ -4363,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] @@ -4378,4 +5059,137 @@ 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] + 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}"); + } + } }