Skip to content

Commit 3fccc27

Browse files
yihuangholiman
andcommitted
core/vm: deepcopy jumptable when enabling extra eips (ethereum#26137)
When the interpreter is configured to use extra-eips, this change makes it so that all the opcodes are deep-copied, to prevent accidental modification of the 'base' jumptable. Closes: ethereum#26136 Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 23bee16 commit 3fccc27

File tree

3 files changed

+51
-3
lines changed

3 files changed

+51
-3
lines changed

core/vm/interpreter.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,16 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
9090
default:
9191
cfg.JumpTable = &frontierInstructionSet
9292
}
93+
if len(cfg.ExtraEips) > 0 {
94+
// Deep-copy jumptable to prevent modification of opcodes in other tables
95+
cfg.JumpTable = copyJumpTable(cfg.JumpTable)
96+
}
9397
for i, eip := range cfg.ExtraEips {
94-
copy := *cfg.JumpTable
95-
if err := EnableEIP(eip, &copy); err != nil {
98+
if err := EnableEIP(eip, cfg.JumpTable); err != nil {
9699
// Disable it, so caller can check if it's activated or not
97100
cfg.ExtraEips = append(cfg.ExtraEips[:i], cfg.ExtraEips[i+1:]...)
98101
log.Error("EIP activation failed", "eip", eip, "error", err)
99102
}
100-
cfg.JumpTable = &copy
101103
}
102104
}
103105

core/vm/jump_table.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,14 @@ func newFrontierInstructionSet() JumpTable {
10441044

10451045
return validate(tbl)
10461046
}
1047+
1048+
func copyJumpTable(source *JumpTable) *JumpTable {
1049+
dest := *source
1050+
for i, op := range source {
1051+
if op != nil {
1052+
opCopy := *op
1053+
dest[i] = &opCopy
1054+
}
1055+
}
1056+
return &dest
1057+
}

core/vm/jump_table_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2022 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package vm
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
// TestJumpTableCopy tests that deep copy is necessery to prevent modify shared jump table
26+
func TestJumpTableCopy(t *testing.T) {
27+
tbl := newMergeInstructionSet()
28+
require.Equal(t, uint64(0), tbl[SLOAD].constantGas)
29+
30+
// a deep copy won't modify the shared jump table
31+
deepCopy := copyJumpTable(&tbl)
32+
deepCopy[SLOAD].constantGas = 100
33+
require.Equal(t, uint64(100), deepCopy[SLOAD].constantGas)
34+
require.Equal(t, uint64(0), tbl[SLOAD].constantGas)
35+
}

0 commit comments

Comments
 (0)