Skip to content

Commit

Permalink
fix(prover): copy tracer memory read
Browse files Browse the repository at this point in the history
  • Loading branch information
x-mass committed Feb 11, 2025
1 parent 1d52ae9 commit 1a1befa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
16 changes: 8 additions & 8 deletions nil/services/synccommittee/prover/tracer/copy_tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
if err != nil {
return copyEvent{}, err
}
data := getDataOverflowSafe(code, src, size)
data := getFixedSizeDataSafe(code, src, size)

return newFinalizedCopyEvent(CopyEvent{
From: CopyParticipant{
Expand Down Expand Up @@ -234,7 +234,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
if err != nil {
return copyEvent{}, err
}
data := getDataOverflowSafe(code, src, size)
data := getFixedSizeDataSafe(code, src, size)

return newFinalizedCopyEvent(CopyEvent{
From: CopyParticipant{
Expand All @@ -256,7 +256,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
dst = tCtx.stack.PopUint64()
src = tCtx.stack.PopUint64()
size = tCtx.stack.PopUint64()
data = getDataOverflowSafe(tCtx.vmCtx.CallInput(), src, size)
data = getFixedSizeDataSafe(tCtx.vmCtx.CallInput(), src, size)
)

return newFinalizedCopyEvent(CopyEvent{
Expand All @@ -278,7 +278,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
var (
src = tCtx.stack.PopUint64()
size = tCtx.stack.PopUint64()
data = tCtx.vmCtx.MemoryData()[src : src+size]
data = getDataIfStartValid(tCtx.vmCtx.MemoryData(), src, size)
)

return newFinalizedCopyEvent(CopyEvent{
Expand Down Expand Up @@ -327,7 +327,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
_ = tCtx.stack.Pop() // value
src = tCtx.stack.PopUint64()
size = tCtx.stack.PopUint64()
data = tCtx.vmCtx.MemoryData()[src : src+size]
data = getDataIfStartValid(tCtx.vmCtx.MemoryData(), src, size)
)
return newCopyEventWithFinalizer(CopyEvent{
From: CopyParticipant{
Expand All @@ -352,7 +352,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
_ = tCtx.stack.Pop() // value
src = tCtx.stack.PopUint64()
size = tCtx.stack.PopUint64()
data = tCtx.vmCtx.MemoryData()[src : src+size]
data = getDataIfStartValid(tCtx.vmCtx.MemoryData(), src, size)
)

return newCopyEventWithFinalizer(CopyEvent{
Expand Down Expand Up @@ -382,7 +382,7 @@ var copyEventExtractors = map[vm.OpCode]copyEventExtractor{
var (
src = tCtx.stack.PopUint64()
size = tCtx.stack.PopUint64()
data = tCtx.vmCtx.MemoryData()[src : src+size]
data = getDataIfStartValid(tCtx.vmCtx.MemoryData(), src, size)
)

return newCopyEventWithFinalizer(CopyEvent{
Expand Down Expand Up @@ -427,7 +427,7 @@ func newLogCopyEvent(tCtx copyEventTraceContext) (copyEvent, error) {
return newEmptyCopyEvent(), nil
}

data := tCtx.vmCtx.MemoryData()[src : src+size]
data := getDataIfStartValid(tCtx.vmCtx.MemoryData(), src, size)

return newFinalizedCopyEvent(CopyEvent{
From: CopyParticipant{
Expand Down
17 changes: 15 additions & 2 deletions nil/services/synccommittee/prover/tracer/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package tracer

import "github.com/NilFoundation/nil/nil/common"

// getDataOverflowSafe returns a slice from the data based on the start and size and pads
// getFixedSizeDataSafe returns a slice from the data based on the start and size and pads
// up to size with zero's
// This function is borrowed from EVM impl to process some opcodes
// (CODECOPY, EXTCODECOPY, CALLDATACOPY) the same way as it does
func getDataOverflowSafe(data []byte, start uint64, size uint64) []byte {
func getFixedSizeDataSafe(data []byte, start uint64, size uint64) []byte {
length := uint64(len(data))
if start > length {
start = length
Expand All @@ -17,3 +17,16 @@ func getDataOverflowSafe(data []byte, start uint64, size uint64) []byte {
}
return common.RightPadBytes(data[start:end], int(size))
}

// getDataIfStartValid returns a slice from `data` starting at `start` with a length of `size` bytes.
// It returns nil if `size` is zero or if `start` is out of bounds.
// Note: It does not check if `start+size` exceeds `data` length.
func getDataIfStartValid(data []byte, start uint64, size uint64) []byte {
if size == 0 {
return nil
}
if len(data) < int(start+size) {
return nil
}
return data[start : start+size]
}

0 comments on commit 1a1befa

Please sign in to comment.