fix(par2): SIMD-align slice size to prevent random AVX-512 segfaults#232
Merged
Conversation
The previous code only rounded the slice size to a 4-byte boundary (per the PAR2 spec) before handing it to par2go. On Linux x86_64 with AVX-512 the ParPar C kernel reads in 64-byte vector lanes, so non-aligned slice sizes caused overreads past the slice buffer. The crash only fired when the over-read happened to cross an unmapped page — flaky on CI, invisible locally on macOS/arm64. Extend the existing 128-byte SIMD alignment guard (previously only applied when blockSize > fileSize) to every slice size handed to par2go, and extract the per-file policy into a testable computeFileBlockSize helper.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes the random
signal: segmentation fault (core dumped)in theCoverageworkflowon
internal/par2(e.g. run 26437870974 / job 77824805140).Root cause
createPar2ForFileonly rounded the slice size to a 4-byte boundary (per the PAR2 spec)before handing it to
par2go. The ParPar C kernel on Linux x86_64 with AVX-512 reads in64-byte vector lanes, so a non-aligned slice size (e.g.
articleSize = 10_000→SliceSize = 10_000, which is156 * 64 + 16) causes overreads past the slice buffer.The crash only fires when the over-read happens to cross an unmapped page. That depends
on the allocator's run-to-run layout — flaky on Linux CI, never reproducible locally on
macOS / arm64 (different SIMD stride, and the existing 128-byte clamp happened to mask it).
The source already documented the requirement at
par2.go:430-444, but the SIMD-safealignment guard was only applied when
parBlockSize > file.Size. For the failing path(
SliceSize = 10MiB > file.Size = 100KB → fallback → articleSize = 10_000) the guardwas skipped entirely.
Fix
computeFileBlockSizehelper.par2go(both
createPar2ForFileandcomputeSetBlockSize).4to128and emit a clear "SIMD-safe"warning when skipping.
TestComputeFileBlockSize_SIMDAlignedcovering the CI repro case plus severaledge cases; asserts
result % 128 == 0so we can't regress silently.Test plan
go build ./internal/par2/...go test -race -count=3 ./internal/par2/...— passesTestComputeFileBlockSize_SIMDAlignedcovers the previously-crashing parameters