Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/cache: fix memory corruption when CGO_ENABLED=0
If cgo is disabled, `manual.New` allocates memory using `make([]byte,...)`. The cache was using this memory as if it was manually allocated, and casting it to Go structs and then storing Go pointers inside the structs. But a Go pointer stored inside of a `[]byte` is invisible to the Go GC, so the GC was merrily reclaiming the memory which would lead to all sorts of badness in the cache, often resulting in segfaults or violations of refcount invariants. Now, when cgo is disabled, the `entry` and `Value` structs are directly allocated using the Go allocator. The cache code mostly supported this mode of operation already when the `invariants` tag. The difference between disabling cgo and enabling invariants is that the latter also has the effect of adding the finalizers uses for leak detection. Fixes #1010
- Loading branch information
1 parent
e325d9d
commit c2b05f1
Showing
5 changed files
with
40 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use | ||
// of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
// +build !cgo | ||
|
||
package cache | ||
|
||
const cgoEnabled = false |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// Copyright 2020 The LevelDB-Go and Pebble Authors. All rights reserved. Use | ||
// of this source code is governed by a BSD-style license that can be found in | ||
// the LICENSE file. | ||
|
||
// +build cgo | ||
|
||
package cache | ||
|
||
const cgoEnabled = true |
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