-
Notifications
You must be signed in to change notification settings - Fork 790
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
Make stack.dup
explicitly copy the BigNumber
#732
Comments
I'll try to convert it to a test, but here is a minimal example: Test file: Output: |
Hi @jochem-brouwer, thanks for going so deep here, really impressive find, also thanks for providing such explicit notes on the analysis. Is it possible to create a simple test case in tests/api/evm/stack.ts which fails before your fix from #733 and passes after applying the fix? If we got a confirmation here this might very well be worth a short-term bugfix |
Hey @holgerd77, I have added a test. It fails before the commit to |
Closed by #733 |
Stack.ts dup method states it copies a BigNumber. However, it does not do this; it copies a pointer to BigNumber.
This results into some annoying bugs. For instance, at the
CALL
operation atopFns.ts
thegasLimit
is popped from the stack of the VM. If we have to use the "gas stipend" of 2300 gas when we send nonzero Ether, then theiaddn
ofBN.js
is called on thisgasLimit
number. See the code I'm referring to.If this
gasLimit
stack item has beenDUP
ed before, then we are thus changing the number which it originally pointed to, which could still be on the stack!. I found this issue trying to replay mainnet transactions and comparingdebug_traceTransaction
against VmState traces and seeing that after a call, suddenly an item on the stack has changed which the VM should not even reach at that point.As a minimal example to show what I mean:
In this minimal example it seems obvious; but if you start to locally perform arithmetic on BigNumbers it might not be obvious you are operating at a pointer. By cloning the stack item explicitly it is at least ensured that if they are edited elsewhere (which should not happen - if you perform arithmetic on them just for "local" operations, I think they should be cloned!) the
DUP
d item is not changed.The text was updated successfully, but these errors were encountered: