From 0a1140dd9ee5101f6f6ed39ff438e25a008a6bc8 Mon Sep 17 00:00:00 2001 From: Martin Holst Swende Date: Thu, 28 Nov 2024 15:25:30 +0100 Subject: [PATCH] core: prevent block level tracers from crashing the node --- core/blockchain.go | 17 ++--- core/blockchain_tracing.go | 97 ++++++++++++++++++++++++++++ eth/tracers/live/crash.go | 129 +++++++++++++++++++++++++++++++++++++ 3 files changed, 233 insertions(+), 10 deletions(-) create mode 100644 core/blockchain_tracing.go create mode 100644 eth/tracers/live/crash.go diff --git a/core/blockchain.go b/core/blockchain.go index c3da61b281..3e4aca85e9 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -412,7 +412,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis bc.engine.VerifyHeader(bc, bc.CurrentHeader()) if bc.logger != nil && bc.logger.OnBlockchainInit != nil { - bc.logger.OnBlockchainInit(chainConfig) + bc.guardedOnBlockchainInit(chainConfig) } if bc.logger != nil && bc.logger.OnGenesisBlock != nil { if block := bc.CurrentBlock(); block.Number.Uint64() == 0 { @@ -423,7 +423,7 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, genesis *Genesis if alloc == nil { return nil, errors.New("live blockchain tracer requires genesis alloc to be set") } - bc.logger.OnGenesisBlock(bc.genesisBlock, alloc) + bc.guardedOnGenesisBlock(bc.genesisBlock, alloc) } } @@ -1155,7 +1155,7 @@ func (bc *BlockChain) Stop() { } // Allow tracers to clean-up and release resources. if bc.logger != nil && bc.logger.OnClose != nil { - bc.logger.OnClose() + bc.guardedOnClose() } // Close the trie database, release all the held resources as the last step. if err := bc.triedb.Close(); err != nil { @@ -1752,7 +1752,7 @@ func (bc *BlockChain) insertChain(chain types.Blocks, setHead bool, makeWitness } stats.processed++ if bc.logger != nil && bc.logger.OnSkippedBlock != nil { - bc.logger.OnSkippedBlock(tracing.BlockEvent{ + bc.guardedOnSkippedBlock(tracing.BlockEvent{ Block: block, TD: bc.GetTd(block.ParentHash(), block.NumberU64()-1), Finalized: bc.CurrentFinalBlock(), @@ -1879,18 +1879,15 @@ type blockProcessingResult struct { // it writes the block and associated state to database. func (bc *BlockChain) processBlock(block *types.Block, statedb *state.StateDB, start time.Time, setHead bool) (_ *blockProcessingResult, blockEndErr error) { if bc.logger != nil && bc.logger.OnBlockStart != nil { - td := bc.GetTd(block.ParentHash(), block.NumberU64()-1) - bc.logger.OnBlockStart(tracing.BlockEvent{ + bc.guardedOnBlockStart(tracing.BlockEvent{ Block: block, - TD: td, + TD: bc.GetTd(block.ParentHash(), block.NumberU64()-1), Finalized: bc.CurrentFinalBlock(), Safe: bc.CurrentSafeBlock(), }) } if bc.logger != nil && bc.logger.OnBlockEnd != nil { - defer func() { - bc.logger.OnBlockEnd(blockEndErr) - }() + defer bc.guardedOnBlockEnd(blockEndErr) } // Process block using the parent state as reference point diff --git a/core/blockchain_tracing.go b/core/blockchain_tracing.go new file mode 100644 index 0000000000..20b6746ca9 --- /dev/null +++ b/core/blockchain_tracing.go @@ -0,0 +1,97 @@ +// 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 . + +// Package core implements the Ethereum consensus protocol. +package core + +import ( + "github.com/ethereum/go-ethereum/core/tracing" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/params" +) + +// guardedOnGenesisBlock wraps the tracer OnGenesisBlock method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnGenesisBlock(genesis *types.Block, alloc types.GenesisAlloc) { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnGenesisBlock exited with panic, disabling tracer", "err", err) + bc.logger.OnGenesisBlock = nil + } + }() + bc.logger.OnGenesisBlock(genesis, alloc) +} + +// guardedOnBlockchainInit wraps the tracer OnBlockchainInit method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnBlockchainInit(chainConfig *params.ChainConfig) { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnBlockchainInit exited with panic, disabling tracer", "err", err) + bc.logger.OnBlockchainInit = nil + } + }() + bc.logger.OnBlockchainInit(chainConfig) +} + +// guardedOnBlockStart wraps the tracer OnBlockStart method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnBlockStart(blockEvent tracing.BlockEvent) { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnBlockStart exited with panic, disabling tracer", "err", err) + bc.logger.OnBlockStart = nil + } + }() + bc.logger.OnBlockStart(blockEvent) +} + +// guardedOnBlockEnd wraps the tracer OnBlockEnd method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnBlockEnd(err error) { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnBlockEnd exited with panic, disabling tracer", "err", err) + bc.logger.OnBlockEnd = nil + } + }() + bc.logger.OnBlockEnd(err) +} + +// guardedOnClose wraps the tracer OnClose method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnClose() { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnClose exited with panic, disabling tracer", "err", err) + bc.logger.OnClose = nil + } + }() + bc.logger.OnClose() +} + +// guardedOnSkippedBlock wraps the tracer OnSkippedBlock method with 'recover'. If +// a panic occurs, the hook will be disabled. +func (bc *BlockChain) guardedOnSkippedBlock(event tracing.BlockEvent) { + defer func() { + if err := recover(); err != nil { + log.Warn("Tracer OnSkippedBlock exited with panic, disabling tracer", "err", err) + bc.logger.OnSkippedBlock = nil + } + }() + bc.logger.OnSkippedBlock(event) +} diff --git a/eth/tracers/live/crash.go b/eth/tracers/live/crash.go new file mode 100644 index 0000000000..3d6556a4c2 --- /dev/null +++ b/eth/tracers/live/crash.go @@ -0,0 +1,129 @@ +// 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 . + +package live + +import ( + "encoding/json" + "math/big" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/tracing" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth/tracers" + "github.com/ethereum/go-ethereum/params" +) + +func init() { + tracers.LiveDirectory.Register("crash", newCrashTracer) +} + +// crash is a no-op live tracer. It's there to +// catch changes in the tracing interface, as well as +// for testing live tracing performance. Can be removed +// as soon as we have a real live tracer. +type crash struct{} + +func newCrashTracer(_ json.RawMessage) (*tracing.Hooks, error) { + t := &crash{} + return &tracing.Hooks{ + OnTxStart: t.OnTxStart, + OnTxEnd: t.OnTxEnd, + OnEnter: t.OnEnter, + OnExit: t.OnExit, + OnOpcode: t.OnOpcode, + OnFault: t.OnFault, + OnGasChange: t.OnGasChange, + OnBlockchainInit: t.OnBlockchainInit, + OnBlockStart: t.OnBlockStart, + OnBlockEnd: t.OnBlockEnd, + OnSkippedBlock: t.OnSkippedBlock, + OnGenesisBlock: t.OnGenesisBlock, + OnBalanceChange: t.OnBalanceChange, + OnNonceChange: t.OnNonceChange, + OnCodeChange: t.OnCodeChange, + OnStorageChange: t.OnStorageChange, + OnLog: t.OnLog, + }, nil +} + +func (t *crash) OnOpcode(pc uint64, op byte, gas, cost uint64, scope tracing.OpContext, rData []byte, depth int, err error) { + panic("OnOpcode") +} + +func (t *crash) OnFault(pc uint64, op byte, gas, cost uint64, _ tracing.OpContext, depth int, err error) { + panic("OnFault") +} + +func (t *crash) OnEnter(depth int, typ byte, from common.Address, to common.Address, input []byte, gas uint64, value *big.Int) { + panic("OnEnter") +} + +func (t *crash) OnExit(depth int, output []byte, gasUsed uint64, err error, reverted bool) { + panic("OnExit") +} + +func (t *crash) OnTxStart(vm *tracing.VMContext, tx *types.Transaction, from common.Address) { + panic("OnTxStart") +} + +func (t *crash) OnTxEnd(receipt *types.Receipt, err error) { + panic("OnTxEnd") +} + +func (t *crash) OnBlockStart(ev tracing.BlockEvent) { + panic("OnBlockStart") +} + +func (t *crash) OnBlockEnd(err error) { + panic("OnBlockEnd") +} + +func (t *crash) OnSkippedBlock(ev tracing.BlockEvent) { + panic("OnSkippedBlock") +} + +func (t *crash) OnBlockchainInit(chainConfig *params.ChainConfig) { + panic("OnBlockchainInit") +} + +func (t *crash) OnGenesisBlock(b *types.Block, alloc types.GenesisAlloc) { + panic("OnGenesisBlock") +} + +func (t *crash) OnBalanceChange(a common.Address, prev, new *big.Int, reason tracing.BalanceChangeReason) { + panic("OnBalanceChange") +} + +func (t *crash) OnNonceChange(a common.Address, prev, new uint64) { + panic("OnNonceChange") +} + +func (t *crash) OnCodeChange(a common.Address, prevCodeHash common.Hash, prev []byte, codeHash common.Hash, code []byte) { + panic("OnCodeChange") +} + +func (t *crash) OnStorageChange(a common.Address, k, prev, new common.Hash) { + panic("OnStorageChange") +} + +func (t *crash) OnLog(l *types.Log) { + panic("OnLog") +} + +func (t *crash) OnGasChange(old, new uint64, reason tracing.GasChangeReason) { + panic("OnGasChange") +}