aboutsummaryrefslogtreecommitdiff
path: root/src/runtime/testdata/testprog
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2025-08-22 13:47:42 -0700
committerGopher Robot <gobot@golang.org>2025-11-25 22:10:16 -0800
commiteb63ef9d6676dc0edb112cd297820306a327017a (patch)
tree589b7cc96275ff412c7bb6c3686017df05906a03 /src/runtime/testdata/testprog
parent06412288cfd31ab365fe8ee6368742dffa759803 (diff)
downloadgo-eb63ef9d6676dc0edb112cd297820306a327017a.tar.xz
runtime: panic if cleanup function closes over cleanup pointer
This would catch problems like https://go.dev/cl/696295. Benchmark effect with this CL plus CL 697535: goos: linux goarch: amd64 pkg: runtime cpu: 12th Gen Intel(R) Core(TM) i7-1260P │ /tmp/foo.1 │ /tmp/foo.2 │ │ sec/op │ sec/op vs base │ AddCleanupAndStop-16 81.93n ± 1% 82.87n ± 1% +1.14% (p=0.041 n=10) For #75066 Change-Id: Ia41d0d6b88baebf6055cb7e5d42bc8210b31630f Reviewed-on: https://go-review.googlesource.com/c/go/+/714000 Reviewed-by: Michael Pratt <mpratt@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Auto-Submit: Ian Lance Taylor <iant@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src/runtime/testdata/testprog')
-rw-r--r--src/runtime/testdata/testprog/checkfinalizers.go27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/runtime/testdata/testprog/checkfinalizers.go b/src/runtime/testdata/testprog/checkfinalizers.go
index ea352a4e3e..9f082a25ff 100644
--- a/src/runtime/testdata/testprog/checkfinalizers.go
+++ b/src/runtime/testdata/testprog/checkfinalizers.go
@@ -28,17 +28,40 @@ func DetectFinalizerAndCleanupLeaks() {
// Leak a cleanup.
cLeak := new(T)
+
+ // Use an extra closure to avoid the simple
+ // checking done by AddCleanup.
+ var closeOverCLeak func(int)
+ closeOverCLeak = func(x int) {
+ // Use recursion to avoid inlining.
+ if x <= 0 {
+ **cLeak = x
+ } else {
+ closeOverCLeak(x - 1)
+ }
+ }
+
runtime.AddCleanup(cLeak, func(x int) {
- **cLeak = x
+ closeOverCLeak(x)
}, int(0))
// Have a regular cleanup to make sure it doesn't trip the detector.
cNoLeak := new(T)
runtime.AddCleanup(cNoLeak, func(_ int) {}, int(0))
+ // Like closeOverCLeak.
+ var closeOverCNoLeak func(int)
+ closeOverCNoLeak = func(x int) {
+ if x <= 0 {
+ **cNoLeak = x
+ } else {
+ closeOverCNoLeak(x - 1)
+ }
+ }
+
// Add a cleanup that only temporarily leaks cNoLeak.
runtime.AddCleanup(cNoLeak, func(x int) {
- **cNoLeak = x
+ closeOverCNoLeak(x)
}, int(0)).Stop()
if !asan.Enabled && !race.Enabled {