-
Notifications
You must be signed in to change notification settings - Fork 2k
chore(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
Open
0xrusowsky
wants to merge
7
commits into
foundry-rs:master
Choose a base branch
from
0xrusowsky:chore/revert-diagnostic-inspector
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+480
−35
Open
Changes from all commits
Commits
Show all changes
7 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 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 { | ||
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)); | ||
} | ||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this warms the account ( |
||
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 | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these can be all helper functions, not RevertDiagnostic specific
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would u keep them in the inspector file? or should i place them somewhere else (like utils or similar)?