Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions core/vm/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ func (e ErrStackOverflow) Unwrap() error {

// ErrInvalidOpCode wraps an evm error when an invalid opcode is encountered.
type ErrInvalidOpCode struct {
opcode OpCode
opcode OpCode
operand *byte
}

func (e *ErrInvalidOpCode) Error() string { return fmt.Sprintf("invalid opcode: %s", e.opcode) }
func (e *ErrInvalidOpCode) Error() string {
if e.operand != nil {
return fmt.Sprintf("invalid opcode: %s (operand: 0x%02x)", e.opcode, *e.operand)
}
return fmt.Sprintf("invalid opcode: %s", e.opcode)
}

// rpcError is the same interface as the one defined in rpc/errors.go
// but we do not want to depend on rpc package here so we redefine it.
Expand Down
9 changes: 6 additions & 3 deletions core/vm/instructions.go
Original file line number Diff line number Diff line change
Expand Up @@ -996,7 +996,8 @@ func opDupN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {

// This range is excluded to preserve compatibility with existing opcodes.
if x > 90 && x < 128 {
return nil, &ErrInvalidOpCode{opcode: OpCode(x)}
operand := x
return nil, &ErrInvalidOpCode{opcode: DUPN, operand: &operand}
}
n := decodeSingle(x)

Expand All @@ -1023,7 +1024,8 @@ func opSwapN(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {

// This range is excluded to preserve compatibility with existing opcodes.
if x > 90 && x < 128 {
return nil, &ErrInvalidOpCode{opcode: OpCode(x)}
operand := x
return nil, &ErrInvalidOpCode{opcode: SWAPN, operand: &operand}
}
n := decodeSingle(x)

Expand Down Expand Up @@ -1053,7 +1055,8 @@ func opExchange(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
// This range is excluded both to preserve compatibility with existing opcodes
// and to keep decode_pair’s 16-aligned arithmetic mapping valid (0–81, 128–255).
if x > 81 && x < 128 {
return nil, &ErrInvalidOpCode{opcode: OpCode(x)}
operand := x
return nil, &ErrInvalidOpCode{opcode: EXCHANGE, operand: &operand}
}
n, m := decodePair(x)
need := max(n, m) + 1
Expand Down
56 changes: 41 additions & 15 deletions core/vm/instructions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,11 +1014,12 @@ func TestEIP8024_Execution(t *testing.T) {
evm := NewEVM(BlockContext{}, nil, params.TestChainConfig, Config{})

tests := []struct {
name string
codeHex string
wantErr error
wantOpcode OpCode
wantVals []uint64
name string
codeHex string
wantErr error
wantOpcode OpCode
wantOperand *byte
wantVals []uint64
}{
{
name: "DUPN",
Expand Down Expand Up @@ -1063,10 +1064,18 @@ func TestEIP8024_Execution(t *testing.T) {
},
},
{
name: "INVALID_SWAPN_LOW",
codeHex: "e75b",
wantErr: &ErrInvalidOpCode{},
wantOpcode: SWAPN,
name: "INVALID_DUPN_LOW",
codeHex: "e65b",
wantErr: &ErrInvalidOpCode{},
wantOpcode: DUPN,
wantOperand: ptrToByte(0x5b),
},
{
name: "INVALID_SWAPN_LOW",
codeHex: "e75b",
wantErr: &ErrInvalidOpCode{},
wantOpcode: SWAPN,
wantOperand: ptrToByte(0x5b),
},
{
name: "JUMP_OVER_INVALID_DUPN",
Expand All @@ -1079,10 +1088,11 @@ func TestEIP8024_Execution(t *testing.T) {
wantVals: []uint64{1, 0, 0},
},
{
name: "INVALID_EXCHANGE",
codeHex: "e852",
wantErr: &ErrInvalidOpCode{},
wantOpcode: EXCHANGE,
name: "INVALID_EXCHANGE",
codeHex: "e852",
wantErr: &ErrInvalidOpCode{},
wantOpcode: EXCHANGE,
wantOperand: ptrToByte(0x52),
},
{
name: "UNDERFLOW_DUPN",
Expand Down Expand Up @@ -1150,10 +1160,21 @@ func TestEIP8024_Execution(t *testing.T) {
// Fail if we don't get the error we expect.
switch tc.wantErr.(type) {
case *ErrInvalidOpCode:
var want *ErrInvalidOpCode
if !errors.As(err, &want) {
var got *ErrInvalidOpCode
if !errors.As(err, &got) {
t.Fatalf("expected ErrInvalidOpCode, got %v", err)
}
if got.opcode != tc.wantOpcode {
t.Fatalf("ErrInvalidOpCode.opcode=%s; want %s", got.opcode, tc.wantOpcode)
}
if tc.wantOperand != nil {
if got.operand == nil {
t.Fatalf("ErrInvalidOpCode.operand=nil; want 0x%02x", *tc.wantOperand)
}
if *got.operand != *tc.wantOperand {
t.Fatalf("ErrInvalidOpCode.operand=0x%02x; want 0x%02x", *got.operand, *tc.wantOperand)
}
}
case *ErrStackUnderflow:
var want *ErrStackUnderflow
if !errors.As(err, &want) {
Expand Down Expand Up @@ -1183,3 +1204,8 @@ func TestEIP8024_Execution(t *testing.T) {
})
}
}

func ptrToByte(v byte) *byte {
b := v
return &b
}
Loading