Skip to content

Commit

Permalink
Add fork condition for datacopy bug in evm (#3858)
Browse files Browse the repository at this point in the history
  • Loading branch information
rlan35 authored Aug 30, 2021
1 parent dece072 commit c9ec647
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
5 changes: 3 additions & 2 deletions core/vm/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ type Contract struct {
CodeAddr *common.Address
Input []byte

Gas uint64
value *big.Int
Gas uint64
value *big.Int
WithDataCopyFix bool // with fix for https://github.com/ethereum/go-ethereum/pull/23446
}

// NewContract returns a new contract environment for the execution of EVM.
Expand Down
4 changes: 4 additions & 0 deletions core/vm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func run(evm *EVM, contract *Contract, input []byte, readOnly bool) ([]byte, err
}(evm.interpreter)
evm.interpreter = interpreter
}

if evm.ChainConfig().IsDataCopyFixEpoch(evm.EpochNumber) {
contract.WithDataCopyFix = true
}
return interpreter.Run(contract, input, readOnly)

}
Expand Down
16 changes: 12 additions & 4 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -767,7 +767,9 @@ func opCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, memory
stack.push(interpreter.intPool.get().SetUint64(1))
}
if err == nil || err == ErrExecutionReverted {
ret = common.CopyBytes(ret)
if contract.WithDataCopyFix {
ret = common.CopyBytes(ret)
}
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
}
contract.Gas += returnGas
Expand Down Expand Up @@ -797,7 +799,9 @@ func opCallCode(pc *uint64, interpreter *EVMInterpreter, contract *Contract, mem
stack.push(interpreter.intPool.get().SetUint64(1))
}
if err == nil || err == ErrExecutionReverted {
ret = common.CopyBytes(ret)
if contract.WithDataCopyFix {
ret = common.CopyBytes(ret)
}
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
}
contract.Gas += returnGas
Expand All @@ -823,7 +827,9 @@ func opDelegateCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract,
stack.push(interpreter.intPool.get().SetUint64(1))
}
if err == nil || err == ErrExecutionReverted {
ret = common.CopyBytes(ret)
if contract.WithDataCopyFix {
ret = common.CopyBytes(ret)
}
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
}
contract.Gas += returnGas
Expand All @@ -849,7 +855,9 @@ func opStaticCall(pc *uint64, interpreter *EVMInterpreter, contract *Contract, m
stack.push(interpreter.intPool.get().SetUint64(1))
}
if err == nil || err == ErrExecutionReverted {
ret = common.CopyBytes(ret)
if contract.WithDataCopyFix {
ret = common.CopyBytes(ret)
}
memory.Set(retOffset.Uint64(), retSize.Uint64(), ret)
}
contract.Gas += returnGas
Expand Down
16 changes: 16 additions & 0 deletions internal/params/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
EPoSBound35Epoch: big.NewInt(631), // Around Wed July 7th 2021
EIP155Epoch: big.NewInt(28),
S3Epoch: big.NewInt(28),
DataCopyFixEpoch: big.NewInt(689), // Around Wed Sept 15th 2021 with 3.5s block time
IstanbulEpoch: big.NewInt(314),
ReceiptLogEpoch: big.NewInt(101),
}
Expand Down Expand Up @@ -88,6 +89,7 @@ var (
EPoSBound35Epoch: big.NewInt(73880),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
DataCopyFixEpoch: big.NewInt(74412),
IstanbulEpoch: big.NewInt(43800),
ReceiptLogEpoch: big.NewInt(0),
}
Expand Down Expand Up @@ -118,6 +120,7 @@ var (
EPoSBound35Epoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
DataCopyFixEpoch: big.NewInt(0),
IstanbulEpoch: big.NewInt(0),
ReceiptLogEpoch: big.NewInt(0),
}
Expand Down Expand Up @@ -148,6 +151,7 @@ var (
EPoSBound35Epoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
DataCopyFixEpoch: big.NewInt(0),
IstanbulEpoch: big.NewInt(0),
ReceiptLogEpoch: big.NewInt(0),
}
Expand Down Expand Up @@ -178,6 +182,7 @@ var (
EPoSBound35Epoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
DataCopyFixEpoch: big.NewInt(0),
IstanbulEpoch: big.NewInt(0),
ReceiptLogEpoch: big.NewInt(0),
}
Expand Down Expand Up @@ -207,6 +212,7 @@ var (
EPoSBound35Epoch: big.NewInt(0),
EIP155Epoch: big.NewInt(0),
S3Epoch: big.NewInt(0),
DataCopyFixEpoch: big.NewInt(0),
IstanbulEpoch: big.NewInt(0),
ReceiptLogEpoch: big.NewInt(0),
}
Expand Down Expand Up @@ -238,6 +244,7 @@ var (
big.NewInt(0), // EPoSBound35Epoch
big.NewInt(0), // EIP155Epoch
big.NewInt(0), // S3Epoch
big.NewInt(0), // DataCopyFixEpoch
big.NewInt(0), // IstanbulEpoch
big.NewInt(0), // ReceiptLogEpoch
}
Expand Down Expand Up @@ -269,6 +276,7 @@ var (
big.NewInt(0), // EPoSBound35Epoch
big.NewInt(0), // EIP155Epoch
big.NewInt(0), // S3Epoch
big.NewInt(0), // DataCopyFixEpoch
big.NewInt(0), // IstanbulEpoch
big.NewInt(0), // ReceiptLogEpoch
}
Expand Down Expand Up @@ -371,6 +379,9 @@ type ChainConfig struct {
// S3 epoch is the first epoch containing S3 mainnet and all ethereum update up to Constantinople
S3Epoch *big.Int `json:"s3-epoch,omitempty"`

// DataCopyFix epoch is the first epoch containing fix for evm datacopy bug.
DataCopyFixEpoch *big.Int `json:"data-copy-fix-epoch,omitempty"`

// Istanbul epoch
IstanbulEpoch *big.Int `json:"istanbul-epoch,omitempty"`

Expand Down Expand Up @@ -501,6 +512,11 @@ func (c *ChainConfig) IsS3(epoch *big.Int) bool {
return isForked(c.S3Epoch, epoch)
}

// IsDataCopyFixEpoch returns whether epoch has the fix for DataCopy evm bug.
func (c *ChainConfig) IsDataCopyFixEpoch(epoch *big.Int) bool {
return isForked(c.DataCopyFixEpoch, epoch)
}

// IsIstanbul returns whether epoch is either equal to the Istanbul fork epoch or greater.
func (c *ChainConfig) IsIstanbul(epoch *big.Int) bool {
return isForked(c.IstanbulEpoch, epoch)
Expand Down

0 comments on commit c9ec647

Please sign in to comment.