From 9b7188f259400077245297f80d9e639c5a394286 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Thu, 28 Mar 2024 21:52:13 +0800 Subject: [PATCH] Problem: no proper error for unsupported opcode --- core/vm/errors.go | 1 + core/vm/instructions.go | 3 +++ core/vm/instructions_test.go | 44 +++++++++++++++++++++--------------- 3 files changed, 30 insertions(+), 18 deletions(-) diff --git a/core/vm/errors.go b/core/vm/errors.go index ba3261c797..fd6dc89504 100644 --- a/core/vm/errors.go +++ b/core/vm/errors.go @@ -37,6 +37,7 @@ var ( ErrGasUintOverflow = errors.New("gas uint64 overflow") ErrInvalidCode = errors.New("invalid code: must not begin with 0xef") ErrNonceUintOverflow = errors.New("nonce uint64 overflow") + ErrUnsupportedRandom = errors.New("unsupported random") // errStopToken is an internal token indicating interpreter loop termination, // never returned to outside callers. diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 990bdbf925..3e731618cf 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -476,6 +476,9 @@ func opDifficulty(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) } func opRandom(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { + if interpreter.evm.Context.Random == nil { + return nil, ErrUnsupportedRandom + } v := new(uint256.Int).SetBytes(interpreter.evm.Context.Random.Bytes()) scope.Stack.push(v) return nil, nil diff --git a/core/vm/instructions_test.go b/core/vm/instructions_test.go index 8653864d11..4a604fa0d7 100644 --- a/core/vm/instructions_test.go +++ b/core/vm/instructions_test.go @@ -719,32 +719,40 @@ func TestCreate2Addreses(t *testing.T) { func TestRandom(t *testing.T) { type testcase struct { name string - random common.Hash + random *common.Hash + err error } - + keccak256Hash := crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03}) for _, tt := range []testcase{ - {name: "empty hash", random: common.Hash{}}, - {name: "1", random: common.Hash{0}}, - {name: "emptyCodeHash", random: types.EmptyCodeHash}, - {name: "hash(0x010203)", random: crypto.Keccak256Hash([]byte{0x01, 0x02, 0x03})}, + {name: "nil random", random: nil, err: ErrUnsupportedRandom}, + {name: "empty hash", random: &common.Hash{}}, + {name: "1", random: &common.Hash{0}}, + {name: "emptyCodeHash", random: &types.EmptyCodeHash}, + {name: "hash(0x010203)", random: &keccak256Hash}, } { var ( - env = NewEVM(BlockContext{Random: &tt.random}, TxContext{}, nil, params.TestChainConfig, Config{}) + env = NewEVM(BlockContext{Random: tt.random}, TxContext{}, nil, params.TestChainConfig, Config{}) stack = newstack() pc = uint64(0) evmInterpreter = env.interpreter ) - opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) - if len(stack.data) != 1 { - t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) - } - actual := stack.pop() - expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) - if overflow { - t.Errorf("Testcase %v: invalid overflow", tt.name) - } - if actual.Cmp(expected) != 0 { - t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual) + _, err := opRandom(&pc, evmInterpreter, &ScopeContext{nil, stack, nil}) + if tt.err != nil { + if err != tt.err { + t.Errorf("Testcase %v: expected %v, got %v", tt.name, tt.err, err) + } + } else { + if len(stack.data) != 1 { + t.Errorf("Expected one item on stack after %v, got %d: ", tt.name, len(stack.data)) + } + actual := stack.pop() + expected, overflow := uint256.FromBig(new(big.Int).SetBytes(tt.random.Bytes())) + if overflow { + t.Errorf("Testcase %v: invalid overflow", tt.name) + } + if actual.Cmp(expected) != 0 { + t.Errorf("Testcase %v: expected %x, got %x", tt.name, expected, actual) + } } } }