Skip to content
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

interop/contract: fix state rollbacks for nested contexts #3046

Merged
merged 1 commit into from
Jun 29, 2023

Conversation

roman-khimov
Copy link
Member

Our wrapping optimization relied on the caller context having a TRY block, but each context (including internal calls!) has an exception handling stack of its own, which means that for an invocation stack of

entry
A.someMethodFromEntry()   # this one has a TRY
A.internalMethodViaCALL() # this one doesn't
B.someMethod()

we get HasTryBlock() == false for A.internalMethodViaCALL() context, which leads to missing wrapper and missing rollbacks if B is to THROW. What this patch does instead is it checks for any context within contract boundaries.

Fixes #3045.

Our wrapping optimization relied on the caller context having a TRY block,
but each context (including internal calls!) has an exception handling stack
of its own, which means that for an invocation stack of

    entry
    A.someMethodFromEntry()   # this one has a TRY
    A.internalMethodViaCALL() # this one doesn't
    B.someMethod()

we get `HasTryBlock() == false` for `A.internalMethodViaCALL()` context, which
leads to missing wrapper and missing rollbacks if B is to THROW. What this
patch does instead is it checks for any context within contract boundaries.

Fixes #3045.

Signed-off-by: Roman Khimov <[email protected]>
@roman-khimov roman-khimov added this to the v0.102.0 milestone Jun 29, 2023
if topctx == nil {
topctx = ictx
}
if ictx.sc != topctx.sc {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

micro: this always false if condition above is true

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, but it's a feature. We always check the top context.

func (v *VM) ContractHasTryBlock() bool {
var topctx *Context // Currently executing context.
for i := 0; i < len(v.istack); i++ {
ictx := v.istack[len(v.istack)-1-i] // It's a stack, going backwards like handleException().
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it'd be easier to invert i then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-pasting from handleException is more reliable!

@roman-khimov roman-khimov merged commit 91b5774 into master Jun 29, 2023
@roman-khimov roman-khimov deleted the mainnet-state-fix branch June 29, 2023 09:24
@roman-khimov roman-khimov modified the milestones: v0.102.0, v0.101.2 Jun 29, 2023
Copy link
Member

@AnnaShaleva AnnaShaleva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Late comment, but it's a really good catch, ACK.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mainnet state difference @3672783
4 participants