go-ethereum/core/vm/eof_immediates.go
Martin HS 56c4f2bfd4
core/vm, cmd/evm: implement eof validation (#30418)
The bulk of this PR is authored by @lightclient , in the original
EOF-work. More recently, the code has been picked up and reworked for the new EOF
specification, by @MariusVanDerWijden , in https://github.com/ethereum/go-ethereum/pull/29518, and also @shemnon has contributed with fixes.

This PR is an attempt to start eating the elephant one small bite at a
time, by selecting only the eof-validation as a standalone piece which
can be merged without interfering too much in the core stuff.

In this PR: 

- [x] Validation of eof containers, lifted from #29518, along with
test-vectors from consensus-tests and fuzzing, to ensure that the move
did not lose any functionality.
- [x] Definition of eof opcodes, which is a prerequisite for validation
- [x] Addition of `undefined` to a jumptable entry item. I'm not
super-happy with this, but for the moment it seems the least invasive
way to do it. A better way might be to go back and allowing nil-items or
nil execute-functions to denote "undefined".
- [x] benchmarks of eof validation speed 


---------

Co-authored-by: lightclient <lightclient@protonmail.com>
Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
Co-authored-by: Danno Ferrin <danno.ferrin@shemnon.com>
2024-10-02 15:05:50 +02:00

71 lines
2.5 KiB
Go

// Copyright 2024 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package vm
// immediate denotes how many immediate bytes an operation uses. This information
// is not required during runtime, only during EOF-validation, so is not
// places into the op-struct in the instruction table.
// Note: the immediates is fork-agnostic, and assumes that validity of opcodes at
// the given time is performed elsewhere.
var immediates [256]uint8
// terminals denotes whether instructions can be the final opcode in a code section.
// Note: the terminals is fork-agnostic, and assumes that validity of opcodes at
// the given time is performed elsewhere.
var terminals [256]bool
func init() {
// The legacy pushes
for i := uint8(1); i < 33; i++ {
immediates[int(PUSH0)+int(i)] = i
}
// And new eof opcodes.
immediates[DATALOADN] = 2
immediates[RJUMP] = 2
immediates[RJUMPI] = 2
immediates[RJUMPV] = 3
immediates[CALLF] = 2
immediates[JUMPF] = 2
immediates[DUPN] = 1
immediates[SWAPN] = 1
immediates[EXCHANGE] = 1
immediates[EOFCREATE] = 1
immediates[RETURNCONTRACT] = 1
// Define the terminals.
terminals[STOP] = true
terminals[RETF] = true
terminals[JUMPF] = true
terminals[RETURNCONTRACT] = true
terminals[RETURN] = true
terminals[REVERT] = true
terminals[INVALID] = true
}
// Immediates returns the number bytes of immediates (argument not from
// stack but from code) a given opcode has.
// OBS:
// - This function assumes EOF instruction-set. It cannot be upon in
// a. pre-EOF code
// b. post-EOF but legacy code
// - RJUMPV is unique as it has a variable sized operand. The total size is
// determined by the count byte which immediately follows RJUMPV. This method
// will return '3' for RJUMPV, which is the minimum.
func Immediates(op OpCode) int {
return int(immediates[op])
}