mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-27 23:26:44 +00:00
core/vm: fix off-by-one, add fuzzer
This commit is contained in:
parent
5841899bd5
commit
057305ace7
3 changed files with 29 additions and 7 deletions
|
|
@ -979,9 +979,9 @@ func opPush2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by
|
||||||
integer = new(uint256.Int)
|
integer = new(uint256.Int)
|
||||||
)
|
)
|
||||||
if *pc+2 < codeLen {
|
if *pc+2 < codeLen {
|
||||||
scope.Stack.push(integer.SetUint64(uint64(binary.BigEndian.Uint16(scope.Contract.Code[*pc : *pc+2]))))
|
scope.Stack.push(integer.SetUint64(uint64(binary.BigEndian.Uint16(scope.Contract.Code[*pc+1 : *pc+3]))))
|
||||||
} else if *pc+1 < codeLen {
|
} else if *pc+1 < codeLen {
|
||||||
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]) << 8))
|
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8))
|
||||||
} else {
|
} else {
|
||||||
scope.Stack.push(integer.Clear())
|
scope.Stack.push(integer.Clear())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -973,11 +973,33 @@ func TestPush(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FuzzPush(f *testing.F) {
|
||||||
|
push2 := makePush(2, 2)
|
||||||
|
f.Fuzz(func(t *testing.T, code []byte, origPC uint64) {
|
||||||
|
scope := &ScopeContext{
|
||||||
|
Memory: nil,
|
||||||
|
Stack: newstack(),
|
||||||
|
Contract: &Contract{
|
||||||
|
Code: code,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
pc := origPC
|
||||||
|
push2(&pc, nil, scope)
|
||||||
|
a := scope.Stack.pop()
|
||||||
|
pc = origPC
|
||||||
|
opPush2(&pc, nil, scope)
|
||||||
|
b := scope.Stack.pop()
|
||||||
|
if a.Cmp(&b) != 0 {
|
||||||
|
panic(fmt.Sprintf("code: %v origPC: %v, %v %v", code, origPC, a, b))
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func BenchmarkPush(b *testing.B) {
|
func BenchmarkPush(b *testing.B) {
|
||||||
var (
|
var (
|
||||||
code = common.FromHex("0011223344556677889900aabbccddeeff0102030405060708090a0b0c0d0e0ff1e1d1c1b1a19181716151413121")
|
code = common.FromHex("0011223344556677889900aabbccddeeff0102030405060708090a0b0c0d0e0ff1e1d1c1b1a19181716151413121")
|
||||||
push32 = makePush(2, 2)
|
push2 = makePush(2, 2)
|
||||||
scope = &ScopeContext{
|
scope = &ScopeContext{
|
||||||
Memory: nil,
|
Memory: nil,
|
||||||
Stack: newstack(),
|
Stack: newstack(),
|
||||||
Contract: &Contract{
|
Contract: &Contract{
|
||||||
|
|
@ -989,7 +1011,7 @@ func BenchmarkPush(b *testing.B) {
|
||||||
|
|
||||||
b.Run("makePush", func(b *testing.B) {
|
b.Run("makePush", func(b *testing.B) {
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
push32(pc, nil, scope)
|
push2(pc, nil, scope)
|
||||||
scope.Stack.pop()
|
scope.Stack.pop()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -631,7 +631,7 @@ func newFrontierInstructionSet() JumpTable {
|
||||||
maxStack: maxStack(0, 1),
|
maxStack: maxStack(0, 1),
|
||||||
},
|
},
|
||||||
PUSH2: {
|
PUSH2: {
|
||||||
execute: makePush(2, 2),
|
execute: opPush2,
|
||||||
constantGas: GasFastestStep,
|
constantGas: GasFastestStep,
|
||||||
minStack: minStack(0, 1),
|
minStack: minStack(0, 1),
|
||||||
maxStack: maxStack(0, 1),
|
maxStack: maxStack(0, 1),
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue