From c58f58b9f8df0bde53bb5bc20b5ea97d34b1531d Mon Sep 17 00:00:00 2001 From: Michael Anthony Knyszek Date: Fri, 9 May 2025 18:53:06 +0000 Subject: runtime: mark and identify tiny blocks in checkfinalizers mode This change adds support for identifying cleanups and finalizers attached to tiny blocks to checkfinalizers mode. It also notes a subtle pitfall, which is that the cleanup arg, if tiny-allocated, could end up co-located with the object with the cleanup attached! Oops... For #72949. Change-Id: Icbe0112f7dcfc63f35c66cf713216796a70121ce Reviewed-on: https://go-review.googlesource.com/c/go/+/662037 LUCI-TryBot-Result: Go LUCI Auto-Submit: Michael Knyszek Reviewed-by: Michael Pratt Reviewed-by: Carlos Amedee --- src/runtime/testdata/testprog/checkfinalizers.go | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/runtime/testdata') diff --git a/src/runtime/testdata/testprog/checkfinalizers.go b/src/runtime/testdata/testprog/checkfinalizers.go index 410a0f6a23..b542f575fe 100644 --- a/src/runtime/testdata/testprog/checkfinalizers.go +++ b/src/runtime/testdata/testprog/checkfinalizers.go @@ -13,6 +13,10 @@ func init() { register("DetectFinalizerAndCleanupLeaks", DetectFinalizerAndCleanupLeaks) } +type tiny uint8 + +var tinySink *tiny + // Intended to be run only with `GODEBUG=checkfinalizers=1`. func DetectFinalizerAndCleanupLeaks() { type T *int @@ -34,6 +38,15 @@ func DetectFinalizerAndCleanupLeaks() { **cNoLeak = x }, int(0)).Stop() + // Ensure we create an allocation into a tiny block that shares space among several values. + var ctLeak *tiny + for i := 0; i < 18; i++ { + tinySink = ctLeak + ctLeak = new(tiny) + *ctLeak = tiny(i) + } + runtime.AddCleanup(ctLeak, func(_ struct{}) {}, struct{}{}) + // Leak a finalizer. fLeak := new(T) runtime.SetFinalizer(fLeak, func(_ *T) { @@ -49,10 +62,4 @@ func DetectFinalizerAndCleanupLeaks() { // runtime.GC here should crash. runtime.GC() println("OK") - - // Keep everything alive. - runtime.KeepAlive(cLeak) - runtime.KeepAlive(cNoLeak) - runtime.KeepAlive(fLeak) - runtime.KeepAlive(fNoLeak) } -- cgit v1.3