From 31d30df59f53c24f7be3f31f1d91a2842c50c615 Mon Sep 17 00:00:00 2001 From: Andrew Ashikhmin <34320705+yperbasis@users.noreply.github.com> Date: Tue, 20 Dec 2022 08:18:24 +0100 Subject: [PATCH] core/vm: fill gaps in jump table with opUndefined (#6372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cherry-pick https://github.com/ethereum/go-ethereum/pull/24031 Co-authored-by: Paweł Bylica --- core/vm/instructions.go | 4 ++++ core/vm/interpreter.go | 4 ---- core/vm/jump_table.go | 11 ++++++++++- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/core/vm/instructions.go b/core/vm/instructions.go index 5addb48e4..30a5d1adf 100644 --- a/core/vm/instructions.go +++ b/core/vm/instructions.go @@ -837,6 +837,10 @@ func opRevert(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]b return ret, nil } +func opUndefined(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { + return nil, &ErrInvalidOpCode{opcode: OpCode(scope.Contract.Code[*pc])} +} + func opStop(pc *uint64, interpreter *EVMInterpreter, scope *ScopeContext) ([]byte, error) { return nil, nil } diff --git a/core/vm/interpreter.go b/core/vm/interpreter.go index cb146818c..498188eb7 100644 --- a/core/vm/interpreter.go +++ b/core/vm/interpreter.go @@ -281,10 +281,6 @@ func (in *EVMInterpreter) Run(contract *Contract, input []byte, readOnly bool) ( // enough stack items available to perform the operation. op = contract.GetOp(_pc) operation := in.jt[op] - - if operation == nil { - return nil, &ErrInvalidOpCode{opcode: op} - } // Validate stack if sLen := locStack.Len(); sLen < operation.minStack { return nil, &ErrStackUnderflow{stackLen: sLen, required: operation.minStack} diff --git a/core/vm/jump_table.go b/core/vm/jump_table.go index b4d185ac9..e3fb667b8 100644 --- a/core/vm/jump_table.go +++ b/core/vm/jump_table.go @@ -258,7 +258,7 @@ func newHomesteadInstructionSet() JumpTable { // newFrontierInstructionSet returns the frontier instructions // that can be executed during the frontier phase. func newFrontierInstructionSet() JumpTable { - return JumpTable{ + tbl := JumpTable{ STOP: { execute: opStop, constantGas: 0, @@ -1463,4 +1463,13 @@ func newFrontierInstructionSet() JumpTable { writes: true, }, } + + // Fill all unassigned slots with opUndefined. + for i, entry := range tbl { + if entry == nil { + tbl[i] = &operation{execute: opUndefined, maxStack: maxStack(0, 0)} + } + } + + return tbl }