-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix "Go pointer to Go pointer" panics
The Cgo runtime verifies that Go never passes pointers to other Go pointers, which is required for correct garbage collection. Unfortunately, its checks are not perfect, and there are occasional false positives. Our code triggers these false positives if the slice passed to compression functions is in the same memory allocation as Go pointers. This happened when trying to use zstd with another package's Writer type, which has an internal buffer. The tests added in this PR all fail with the following panic. The fix is to ensure the expression unsafe.Pointer(&src[0]) is inside the Cgo call, and not before. This is documented in the following issue: golang/go#14210 (comment) The remaining uses of the "var srcPtr *byte" pattern are safe: they all pass the address of a byte slice that is allocated internally by this library, so I believe they cannot cause this error. Fixes the following panic: panic: runtime error: cgo argument has Go pointer to Go pointer goroutine 30 [running]: panic(...) /usr/local/go/src/runtime/panic.go:969 +0x1b9 github.com/DataDog/zstd.(*ctx).CompressLevel.func1(...) /home/circleci/project/zstd_ctx.go:75 +0xd9 github.com/DataDog/zstd.(*ctx).CompressLevel(...) /home/circleci/project/zstd_ctx.go:75 +0xce github.com/DataDog/zstd.TestCtxCompressLevelNoGoPointers.func1(...) /home/circleci/project/zstd_ctx_test.go:71 +0x77 github.com/DataDog/zstd.testCompressNoGoPointers(...) /home/circleci/project/zstd_test.go:130 +0xad github.com/DataDog/zstd.TestCtxCompressLevelNoGoPointers(...) /home/circleci/project/zstd_ctx_test.go:69 +0x37 testing.tRunner(...) /usr/local/go/src/testing/testing.go:1123 +0xef
- Loading branch information
Showing
6 changed files
with
100 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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