Skip to content

Commit

Permalink
Merge pull request ethereum#22 from OffchainLabs/stylus-reentrancy
Browse files Browse the repository at this point in the history
Expose whether in delegatecall or callcode
  • Loading branch information
rachel-bousfield authored Aug 8, 2023
2 parents c51d75b + 10b4729 commit 7a35694
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
3 changes: 3 additions & 0 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type Contract struct {
caller ContractRef
self ContractRef

// Arbitrum
delegateOrCallcode bool

jumpdests map[common.Hash]bitvec // Aggregated result of JUMPDEST analysis.
analysis bitvec // Locally cached result of JUMPDEST analysis

Expand Down
4 changes: 4 additions & 0 deletions core/vm/contract_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ func (c *Contract) BurnGas(amount uint64) error {
c.Gas -= amount
return nil
}

func (c *Contract) IsDelegateOrCallcode() bool {
return c.delegateOrCallcode
}
8 changes: 8 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ func (evm *EVM) CallCode(caller ContractRef, addr common.Address, input []byte,
// The contract is a scoped environment for this execution context only.
contract := NewContract(caller, AccountRef(caller.Address()), value, gas)
contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))

// Arbitrum: note the callcode
contract.delegateOrCallcode = true

ret, err = evm.interpreter.Run(contract, input, false)
gas = contract.Gas
}
Expand Down Expand Up @@ -368,6 +372,10 @@ func (evm *EVM) DelegateCall(caller ContractRef, addr common.Address, input []by
// Initialise a new contract and make initialise the delegate values
contract := NewContract(caller, AccountRef(caller.Address()), nil, gas).AsDelegate()
contract.SetCallCode(&addrCopy, evm.StateDB.GetCodeHash(addrCopy), evm.StateDB.GetCode(addrCopy))

// Arbitrum: note the delegate call
contract.delegateOrCallcode = true

ret, err = evm.interpreter.Run(contract, input, false)
gas = contract.Gas
}
Expand Down
8 changes: 4 additions & 4 deletions core/vm/evm_arbitrum.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ func (evm *EVM) DecrementDepth() {
type TxProcessingHook interface {
StartTxHook() (bool, uint64, error, []byte) // return 4-tuple rather than *struct to avoid an import cycle
GasChargingHook(gasRemaining *uint64) (common.Address, error)
PushCaller(addr common.Address)
PopCaller()
PushContract(contract *Contract)
PopContract()
ForceRefundGas() uint64
NonrefundableGas() uint64
DropTip() bool
Expand All @@ -67,9 +67,9 @@ func (p DefaultTxProcessor) GasChargingHook(gasRemaining *uint64) (common.Addres
return p.evm.Context.Coinbase, nil
}

func (p DefaultTxProcessor) PushCaller(addr common.Address) {}
func (p DefaultTxProcessor) PushContract(contract *Contract) {}

func (p DefaultTxProcessor) PopCaller() {}
func (p DefaultTxProcessor) PopContract() {}

func (p DefaultTxProcessor) ForceRefundGas() uint64 { return 0 }

Expand Down
4 changes: 2 additions & 2 deletions core/vm/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ func NewEVMInterpreter(evm *EVM) *EVMInterpreter {
func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) (ret []byte, err error) {
// Increment the call depth which is restricted to 1024
in.evm.depth++
in.evm.ProcessingHook.PushCaller(contract.Caller())
in.evm.ProcessingHook.PushContract(contract)
defer func() { in.evm.depth-- }()
defer func() { in.evm.ProcessingHook.PopCaller() }()
defer func() { in.evm.ProcessingHook.PopContract() }()

// Make sure the readOnly is only set if we aren't in readOnly yet.
// This also makes sure that the readOnly flag isn't removed for child calls.
Expand Down

0 comments on commit 7a35694

Please sign in to comment.