core/vm: fix off-by-one, add fuzzer

This commit is contained in:
Marius van der Wijden 2025-02-26 12:34:24 +01:00
parent 5841899bd5
commit 057305ace7
3 changed files with 29 additions and 7 deletions

View file

@ -979,9 +979,9 @@ func opPush2(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]by
integer = new(uint256.Int)
)
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 {
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc]) << 8))
scope.Stack.push(integer.SetUint64(uint64(scope.Contract.Code[*pc+1]) << 8))
} else {
scope.Stack.push(integer.Clear())
}

View file

@ -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) {
var (
code = common.FromHex("0011223344556677889900aabbccddeeff0102030405060708090a0b0c0d0e0ff1e1d1c1b1a19181716151413121")
push32 = makePush(2, 2)
scope = &ScopeContext{
code = common.FromHex("0011223344556677889900aabbccddeeff0102030405060708090a0b0c0d0e0ff1e1d1c1b1a19181716151413121")
push2 = makePush(2, 2)
scope = &ScopeContext{
Memory: nil,
Stack: newstack(),
Contract: &Contract{
@ -989,7 +1011,7 @@ func BenchmarkPush(b *testing.B) {
b.Run("makePush", func(b *testing.B) {
for i := 0; i < b.N; i++ {
push32(pc, nil, scope)
push2(pc, nil, scope)
scope.Stack.pop()
}
})

View file

@ -631,7 +631,7 @@ func newFrontierInstructionSet() JumpTable {
maxStack: maxStack(0, 1),
},
PUSH2: {
execute: makePush(2, 2),
execute: opPush2,
constantGas: GasFastestStep,
minStack: minStack(0, 1),
maxStack: maxStack(0, 1),