-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(script): revert if address(this) used #10295
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
Conversation
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.
very supportive, some questions.
wdyt @klkvr @grandizzy
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.
can you pls add a test too?
hey @gap-editor I pushed some cleanups in https://github.com/gap-editor/foundry/commit/8fd0fbf5d06a4e548dcefaa6d480ca087af6e752 pls check. thank you! |
@klkvr did i do good? |
@gap-editor we should keep the changes in script inspector only and not add custom logic in stack. For this case, since when calling libraries the bytecode address is the library address and not script's contract, we could add one more check to verify that |
should be rfr, @mattsse @klkvr @DaniPopes pls check (any perf improvement that could be done?) |
lgtm! Small notes in regards to output mode for the error |
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.
lgtm!
ci error is unrelated
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.
lgtm
Any chance this behavior can be put behind a flag? We use |
Motivation
Closes #10289
Users can accidentally use
address(this)
withinforge script
contracts, often intending to refer to the script contract itself for operations like transferring ownership or sending tokens during deployment. However, theaddress(this)
in a script context refers to a temporary, locally deployed contract instance used for the simulation. This address does not exist on the target network and relying on it leads to deployment errors or incorrect state on-chain.This PR introduces a warning mechanism to alert users when
address(this)
is used during aforge script
execution, helping prevent these common mistakes.Solution
Introduced
ScriptAddressWarnInspector
:revm::Inspector
implementation namedScriptAddressWarnInspector
withincrates/evm/evm/src/inspectors/stack.rs
.step
method, which checks if the currently executing EVM opcode isADDRESS
(0x30).ADDRESS
opcode is detected, it prints a warning message tostderr
:"forge script warning: Usage of \
address(this)` detected. Script contracts are ephemeral and their addresses should not be relied upon."`Integrated into
InspectorStack
:address_warn_inspector
field to theInspectorStackInner
struct.address_warn
boolean flag and a builder method.address_warn_inspector(bool)
toInspectorStackBuilder
.InspectorStackBuilder::build
method to instantiateScriptAddressWarnInspector
if theaddress_warn
flag is true.Inspector
implementation forInspectorStackRefMut
to invoke thestep
method of theaddress_warn_inspector
if it's present.Enabled for
forge script
:crates/script/src/lib.rs
, within the_get_runner
function (which configures theExecutor
for scripts), modified theExecutorBuilder
chain to call.address_warn_inspector(true)
. This ensures the warning inspector is always active duringforge script
runs.This approach leverages the existing
revm
inspector system to passively detect the opcode usage without significantly impacting performance and provides a clear warning directly to the user during script execution.PR Checklist