Skip to content

Commit be062b7

Browse files
mknyszekgopherbot
authored andcommitted
[release-branch.go1.23] runtime: explicitly keep handle alive during getOrAddWeakHandle
getOrAddWeakHandle is very careful about keeping its input alive across the operation, but not very careful about keeping the heap-allocated handle it creates alive. In fact, there's a window in this function where it is *only* visible via the special. Specifically, the window of time between when the handle is stored in the special and when the special actually becomes visible to the GC. (If we fail to add the special because it already exists, that case is fine. We don't even use the same handle value, but the one we obtain from the attached GC-visible special, *and* we return that value, so it remains live.) For #70455. Fixes #70469. Change-Id: Iadaff0cfb93bcaf61ba2b05be7fa0519c481de82 Reviewed-on: https://go-review.googlesource.com/c/go/+/630316 Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent d8adc6c commit be062b7

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

‎src/runtime/mheap.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2172,8 +2172,14 @@ func getOrAddWeakHandle(p unsafe.Pointer) *atomic.Uintptr {
21722172

21732173
// Keep p alive for the duration of the function to ensure
21742174
// that it cannot die while we're trying to do this.
2175+
//
2176+
// Same for handle, which is only stored in the special.
2177+
// There's a window where it might die if we don't keep it
2178+
// alive explicitly. Returning it here is probably good enough,
2179+
// but let's be defensive and explicit. See #70455.
21752180
KeepAlive(p)
2176-
return s.handle
2181+
KeepAlive(handle)
2182+
return handle
21772183
}
21782184

21792185
// There was an existing handle. Free the special
@@ -2193,7 +2199,10 @@ func getOrAddWeakHandle(p unsafe.Pointer) *atomic.Uintptr {
21932199

21942200
// Keep p alive for the duration of the function to ensure
21952201
// that it cannot die while we're trying to do this.
2202+
//
2203+
// Same for handle, just to be defensive.
21962204
KeepAlive(p)
2205+
KeepAlive(handle)
21972206
return handle
21982207
}
21992208

0 commit comments

Comments
 (0)