Skip to content

Commit 7dc5e78

Browse files
yihuangholiman
andauthored
core/vm: deepcopy jumptable when enabling extra eips (#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: #26136 Co-authored-by: Martin Holst Swende <martin@swende.se>
1 parent 6685f88 commit 7dc5e78

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
@@ -82,15 +82,17 @@ func NewEVMInterpreter(evm *EVM, cfg Config) *EVMInterpreter {
8282
cfg.JumpTable = &frontierInstructionSet
8383
}
8484
var extraEips []int
85+
if len(cfg.ExtraEips) > 0 {
86+
// Deep-copy jumptable to prevent modification of opcodes in other tables
87+
cfg.JumpTable = copyJumpTable(cfg.JumpTable)
88+
}
8589
for _, eip := range cfg.ExtraEips {
86-
copy := *cfg.JumpTable
87-
if err := EnableEIP(eip, &copy); err != nil {
90+
if err := EnableEIP(eip, cfg.JumpTable); err != nil {
8891
// Disable it, so caller can check if it's activated or not
8992
log.Error("EIP activation failed", "eip", eip, "error", err)
9093
} else {
9194
extraEips = append(extraEips, eip)
9295
}
93-
cfg.JumpTable = &copy
9496
}
9597
cfg.ExtraEips = extraEips
9698
}

core/vm/jump_table.go

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

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

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)