-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat(forge): revert diagnostic inspector #10446
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
klkvr
merged 18 commits into
foundry-rs:master
from
0xrusowsky:chore/revert-diagnostic-inspector
May 22, 2025
Merged
Changes from 7 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3439f21
chore: revert diagnostic inspector
0xrusowsky bdff692
style: clippy + fmt
0xrusowsky 381a47a
style: more readable code + docs
0xrusowsky 337c358
fix: track call stack depth + EXTCODESIZE checks
0xrusowsky 68a4577
chore(traces decoder): non-supported fn selector call
0xrusowsky 8c571d5
disable diagnostic revert when verbosity < '-vvv'
0xrusowsky bbb18a8
Merge branch 'master' into chore/revert-diagnostic-inspector
0xrusowsky 89e4fe8
fix: do not warm address
0xrusowsky 0ab8905
fix: call inspector
0xrusowsky 832c2ac
fix: merge conflicts
0xrusowsky 959c713
fix: config revert diag with `fn tracing()`
0xrusowsky e0c5f9e
improve docs + make diagnostics more restrictive
0xrusowsky af5e1cc
inject revert reason directly into interpreter
0xrusowsky 656fb2d
style: clippy + fmt
0xrusowsky 2a9d9ca
Merge branch 'master' of github.com:foundry-rs/foundry into chore/rev…
0xrusowsky dfea9f0
integrate new revm version
0xrusowsky 4a606ce
style: nits
0xrusowsky ced741e
Merge branch 'master' into chore/revert-diagnostic-inspector
0xrusowsky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
use alloy_primitives::Address; | ||
use foundry_evm_core::{ | ||
backend::DatabaseExt, | ||
constants::{CHEATCODE_ADDRESS, HARDHAT_CONSOLE_ADDRESS}, | ||
decode::DetailedRevertReason, | ||
}; | ||
use revm::{ | ||
interpreter::{ | ||
opcode::EXTCODESIZE, CallInputs, CallOutcome, CallScheme, InstructionResult, Interpreter, | ||
}, | ||
precompile::{PrecompileSpecId, Precompiles}, | ||
primitives::SpecId, | ||
Database, EvmContext, Inspector, | ||
}; | ||
|
||
const IGNORE: [Address; 2] = [HARDHAT_CONSOLE_ADDRESS, CHEATCODE_ADDRESS]; | ||
|
||
/// An inspector that tracks call context to enhances revert diagnostics. | ||
/// Useful for understanding reverts that are not linked to custom errors or revert strings. | ||
#[derive(Clone, Debug, Default)] | ||
pub struct RevertDiagnostic { | ||
/// Tracks calls with calldata that target an address without executable code | ||
pub non_contract_call: Option<(Address, CallScheme, u64)>, | ||
/// Tracks EXTCODESIZE checks that target an address without executable code | ||
pub non_contract_size_check: Option<(Address, u64)>, | ||
/// Tracks whether a failed call has been spotted or not. | ||
pub reverted: bool, | ||
} | ||
|
||
impl RevertDiagnostic { | ||
fn is_delegatecall(&self, scheme: CallScheme) -> bool { | ||
grandizzy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
matches!( | ||
scheme, | ||
CallScheme::DelegateCall | CallScheme::ExtDelegateCall | CallScheme::CallCode | ||
) | ||
} | ||
|
||
fn is_precompile(&self, spec_id: SpecId, target: Address) -> bool { | ||
let precompiles = Precompiles::new(PrecompileSpecId::from_spec_id(spec_id)); | ||
precompiles.contains(&target) | ||
} | ||
|
||
pub fn no_code_target_address(&self, inputs: &mut CallInputs) -> Address { | ||
if self.is_delegatecall(inputs.scheme) { | ||
inputs.bytecode_address | ||
} else { | ||
inputs.target_address | ||
} | ||
} | ||
|
||
pub fn reason(&self) -> Option<DetailedRevertReason> { | ||
if self.reverted { | ||
if let Some((addr, scheme, _)) = self.non_contract_call { | ||
let reason = if self.is_delegatecall(scheme) { | ||
DetailedRevertReason::DelegateCallToNonContract(addr) | ||
} else { | ||
DetailedRevertReason::CallToNonContract(addr) | ||
}; | ||
|
||
return Some(reason); | ||
} | ||
} | ||
|
||
if let Some((addr, _)) = self.non_contract_size_check { | ||
// call never took place, so schema is unknown --> output most generic msg | ||
return Some(DetailedRevertReason::CallToNonContract(addr)); | ||
} | ||
0xrusowsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
None | ||
} | ||
} | ||
|
||
impl<DB: Database + DatabaseExt> Inspector<DB> for RevertDiagnostic { | ||
/// Tracks the first call, with non-zero calldata, that targeted a non-contract address. | ||
/// Excludes precompiles and test addresses. | ||
fn call(&mut self, ctx: &mut EvmContext<DB>, inputs: &mut CallInputs) -> Option<CallOutcome> { | ||
let target = self.no_code_target_address(inputs); | ||
|
||
if IGNORE.contains(&target) || self.is_precompile(ctx.spec_id(), target) { | ||
return None; | ||
} | ||
|
||
if let Ok(state) = ctx.code(target) { | ||
if state.is_empty() && !inputs.input.is_empty() && !self.reverted { | ||
self.non_contract_call = Some((target, inputs.scheme, ctx.journaled_state.depth())); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/// Tracks `EXTCODESIZE` targeted to addresses without code. Clears the cache when the call | ||
/// stack depth changes. | ||
fn step(&mut self, interpreter: &mut Interpreter, ctx: &mut EvmContext<DB>) { | ||
if let Some((_, depth)) = self.non_contract_size_check { | ||
if depth != ctx.journaled_state.depth() { | ||
self.non_contract_size_check = None; | ||
} | ||
|
||
return; | ||
} | ||
|
||
if EXTCODESIZE == interpreter.current_opcode() { | ||
if let Ok(word) = interpreter.stack().peek(0) { | ||
let addr = Address::from_word(word.into()); | ||
if let Ok(state) = ctx.code(addr) { | ||
grandizzy marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if state.is_empty() { | ||
self.non_contract_size_check = Some((addr, ctx.journaled_state.depth())); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
/// Records whether the call reverted or not. Only if the revert call depth matches the | ||
/// inspector cache. | ||
fn call_end( | ||
&mut self, | ||
ctx: &mut EvmContext<DB>, | ||
_inputs: &CallInputs, | ||
outcome: CallOutcome, | ||
) -> CallOutcome { | ||
if let Some((_, _, depth)) = self.non_contract_call { | ||
if outcome.result.result == InstructionResult::Revert && | ||
ctx.journaled_state.depth() == depth - 1 | ||
{ | ||
self.reverted = true | ||
}; | ||
} | ||
|
||
outcome | ||
} | ||
0xrusowsky marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.