Skip to content

Commit 17eb0a2

Browse files
committed
runtime: fix liveness issue in test-only getgcmask
getgcmask stops referencing the object passed to it sometime between when the object is looked up and when the function returns. Notably, this can happen while the GC mask is actively being produced, and thus the GC might free the object. This is easily reproducible by adding a runtime.GC call at just the right place. Adding a KeepAlive on the heap-object path fixes it. Fixes #64188. Change-Id: I5ed4cae862fc780338b60d969fd7fbe896352ce4 Reviewed-on: https://go-review.googlesource.com/c/go/+/542716 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Cherry Mui <[email protected]>
1 parent 0011342 commit 17eb0a2

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/runtime/mbitmap_allocheaders.go

+5
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,11 @@ func getgcmask(ep any) (mask []byte) {
10781078
for len(mask) > 0 && mask[len(mask)-1] == 0 {
10791079
mask = mask[:len(mask)-1]
10801080
}
1081+
1082+
// Make sure we keep ep alive. We may have stopped referencing
1083+
// ep's data pointer sometime before this point and it's possible
1084+
// for that memory to get freed.
1085+
KeepAlive(ep)
10811086
return
10821087
}
10831088

src/runtime/mbitmap_noallocheaders.go

+5
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,11 @@ func getgcmask(ep any) (mask []byte) {
744744
for len(mask) > 0 && mask[len(mask)-1] == 0 {
745745
mask = mask[:len(mask)-1]
746746
}
747+
748+
// Make sure we keep ep alive. We may have stopped referencing
749+
// ep's data pointer sometime before this point and it's possible
750+
// for that memory to get freed.
751+
KeepAlive(ep)
747752
return
748753
}
749754

0 commit comments

Comments
 (0)