Skip to content

Commit

Permalink
Fix grave bug under concurrent use of PieceAggregateCommP
Browse files Browse the repository at this point in the history
Original code naively used a global hasher without any concurrency control
  • Loading branch information
ribasushi committed Aug 4, 2024
1 parent f1a31a6 commit 82182a6
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion commd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package commp
import (
"errors"
"fmt"
"hash"
"math/bits"
"sync"

"github.com/filecoin-project/go-commp-utils/zerocomm"
commcid "github.com/filecoin-project/go-fil-commcid"
Expand Down Expand Up @@ -110,11 +112,14 @@ func PieceAggregateCommP(proofType abi.RegisteredSealProof, pieceInfos []abi.Pie
return commcid.PieceCommitmentV1ToCID(stack[0].commP)
}

var s256 = sha256simd.New()
var s256pool = sync.Pool{New: func() any { return sha256simd.New() }}

func zeroCommForSize(s uint64) []byte { return zerocomm.PieceComms[bits.TrailingZeros64(s)-7][:] }

func reduceStack(s []stackFrame) []stackFrame {

s256 := s256pool.Get().(hash.Hash)

for len(s) > 1 && s[len(s)-2].size == s[len(s)-1].size {

s256.Reset()
Expand All @@ -131,5 +136,7 @@ func reduceStack(s []stackFrame) []stackFrame {
s = s[:len(s)-1]
}

s256pool.Put(s256)

return s
}

0 comments on commit 82182a6

Please sign in to comment.