Skip to content

Commit

Permalink
eth/tracers/js: fix isPush for push0 (ethereum#28520)
Browse files Browse the repository at this point in the history
Fixes so that `push0` opcode is correctly reported as `true` by the `IsPush` function

---------

Co-authored-by: Martin Holst Swende <martin@swende.se>
  • Loading branch information
wang-gerui and holiman committed Mar 7, 2024
1 parent 968440c commit 863d4f1
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 53 deletions.
80 changes: 32 additions & 48 deletions core/asm/asm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,37 @@ import (
"encoding/hex"
)

// Tests disassembling the instructions for valid evm code
func TestInstructionIteratorValid(t *testing.T) {
cnt := 0
script, _ := hex.DecodeString("61000000")

it := NewInstructionIterator(script)
for it.Next() {
cnt++
}

if err := it.Error(); err != nil {
t.Errorf("Expected 2, but encountered error %v instead.", err)
}
if cnt != 2 {
t.Errorf("Expected 2, but got %v instead.", cnt)
}
}

// Tests disassembling the instructions for invalid evm code
func TestInstructionIteratorInvalid(t *testing.T) {
cnt := 0
script, _ := hex.DecodeString("6100")

it := NewInstructionIterator(script)
for it.Next() {
cnt++
}

if it.Error() == nil {
t.Errorf("Expected an error, but got %v instead.", cnt)
}
}

// Tests disassembling the instructions for empty evm code
func TestInstructionIteratorEmpty(t *testing.T) {
cnt := 0
script, _ := hex.DecodeString("")

it := NewInstructionIterator(script)
for it.Next() {
cnt++
}

if err := it.Error(); err != nil {
t.Errorf("Expected 0, but encountered error %v instead.", err)
}
if cnt != 0 {
t.Errorf("Expected 0, but got %v instead.", cnt)
// Tests disassembling instructions
func TestInstructionIterator(t *testing.T) {
for i, tc := range []struct {
want int
code string
wantErr string
}{
{2, "61000000", ""}, // valid code
{0, "6100", "incomplete push instruction at 0"}, // invalid code
{2, "5900", ""}, // push0
{0, "", ""}, // empty

} {
var (
have int
code, _ = hex.DecodeString(tc.code)
it = NewInstructionIterator(code)
)
for it.Next() {
have++
}
var haveErr = ""
if it.Error() != nil {
haveErr = it.Error().Error()
}
if haveErr != tc.wantErr {
t.Errorf("test %d: encountered error: %q want %q", i, haveErr, tc.wantErr)
continue
}
if have != tc.want {
t.Errorf("wrong instruction count, have %d want %d", have, tc.want)
}
}
}
6 changes: 1 addition & 5 deletions core/vm/opcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ type OpCode byte

// IsPush specifies if an opcode is a PUSH opcode.
func (op OpCode) IsPush() bool {
switch op {
case PUSH1, PUSH2, PUSH3, PUSH4, PUSH5, PUSH6, PUSH7, PUSH8, PUSH9, PUSH10, PUSH11, PUSH12, PUSH13, PUSH14, PUSH15, PUSH16, PUSH17, PUSH18, PUSH19, PUSH20, PUSH21, PUSH22, PUSH23, PUSH24, PUSH25, PUSH26, PUSH27, PUSH28, PUSH29, PUSH30, PUSH31, PUSH32:
return true
}
return false
return PUSH0 <= op && op <= PUSH32
}

// 0x0 range - arithmetic ops.
Expand Down

0 comments on commit 863d4f1

Please sign in to comment.