aboutsummaryrefslogtreecommitdiff
path: root/src/weak/pointer_test.go
AgeCommit message (Collapse)Author
2025-12-08weak: fix weak pointer test to correctly iterate over weak pointers after GCcuishuang
This change fixes a bug in the weak pointer test where the loop was attempting to iterate over a nil slice (bt) instead of the weak pointer slice (wt). After setting bt to nil and running GC, the test should iterate over the weak pointers to verify they've been cleared, not attempt to iterate over the now-nil strong references. Change-Id: Ic0425f59da132257770ed87d1bcea5d2c0a54e07 Reviewed-on: https://go-review.googlesource.com/c/go/+/723600 Reviewed-by: David Chase <drchase@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> Reviewed-by: Sean Liao <sean@liao.dev>
2025-11-14runtime: ensure weak handles end up in their own allocationMichael Anthony Knyszek
Currently weak handles are atomic.Uintptr values that may end up in a tiny block which can cause all sorts of surprising leaks. See #76007 for one example. This change pads out the underlying allocation of the atomic.Uintptr to 16 bytes to ensure we bypass the tiny allocator, and it gets its own block. This wastes 8 bytes per weak handle. We could potentially do better by using the 8 byte noscan size class, but this can be a follow-up change. For #76007. Change-Id: I3ab74dda9bf312ea0007f167093052de28134944 Reviewed-on: https://go-review.googlesource.com/c/go/+/719960 Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-02-25weak: test the use of runtime.AddCleanupCarlos Amedee
This change adds a test case for runtime.AddCleanup. Updates #70907 Change-Id: I29cba9dc5b40cec8e610215974e61ee47e10d00f Reviewed-on: https://go-review.googlesource.com/c/go/+/649459 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Michael Knyszek <mknyszek@google.com>
2025-02-19weak: accept linker-allocated objects to MakeMichael Anthony Knyszek
Currently Make panics when passed a linker-allocated object. This is inconsistent with both runtime.AddCleanup and runtime.SetFinalizer. Not panicking in this case is important so that all pointers can be treated equally by these APIs. Libraries should not have to worry where a pointer came from to still make weak pointers. Supporting this behavior is a bit complex for weak pointers versus finalizers and cleanups. For the latter two, it means a function is never called, so we can just drop everything on the floor. For weak pointers, we still need to produce pointers that compare as per the API. To do this, copy the tiny lock-free trace map implementation and use it to store weak handles for "immortal" objects. These paths in the runtime should be rare, so it's OK if it's not incredibly fast, but we should keep the memory footprint relatively low (at least not have it be any worse than specials), so this change tweaks the map implementation a little bit to ensure that's the case. Fixes #71726. Change-Id: I0c87c9d90656d81659ac8d70f511773d0093ce27 Reviewed-on: https://go-review.googlesource.com/c/go/+/649460 Reviewed-by: Cherry Mui <cherryyz@google.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-02-06weak: prevent unsafe conversions using weak pointersthepudds
Prevent conversions between Pointer types, like we do for sync/atomic.Pointer. Fixes #71583 Change-Id: I20e83106d8a27996f221e6cd9d52637b0442cea4 Reviewed-on: https://go-review.googlesource.com/c/go/+/647195 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> Reviewed-by: Michael Knyszek <mknyszek@google.com> Reviewed-by: Cherry Mui <cherryyz@google.com>
2025-01-07weak: don't panic when calling Value on a zero PointerMichael Anthony Knyszek
Currently weak.Pointer.Value will panic if the weak.Pointer is uninitialized (zero value) which goes against it's documentation. Fix this and add a test. While we're here, also add a test to ensure weak.Make[T](nil) is equivalent to the zero value of weak.Pointer[T]. Fixes #71153. Change-Id: I4d9196026360bc42a5bfcb33ce449131ec251dba Reviewed-on: https://go-review.googlesource.com/c/go/+/641095 Reviewed-by: David Finkel <david.finkel@gmail.com> Auto-Submit: Michael Knyszek <mknyszek@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org>
2024-12-09runtime: make special offset a uintptrMichael Anthony Knyszek
Currently specials try to save on space by only encoding the offset from the base of the span in a uint16. This worked fine up until Go 1.24. - Most specials have an offset of 0 (mem profile, finalizers, etc.) - Cleanups do not care about the offset at all, so even if it's wrong, it's OK. - Weak pointers *do* care, but the unique package always makes a new allocation, so the weak pointer handle offset it makes is always zero. With Go 1.24 and general weak pointers now available, nothing is stopping someone from just creating a weak pointer that is >64 KiB offset from the start of an object, and this weak pointer must be distinct from others. Fix this problem by just increasing the size of a special and making the offset a uintptr, to capture all possible offsets. Since we're in the freeze, this is the safest thing to do. Specials aren't so common that I expect a substantial memory increase from this change. In a future release (or if there is a problem) we can almost certainly pack the special's kind and offset together. There was already a bunch of wasted space due to padding, so this would bring us back to the same memory footprint before this change. Also, add tests for equality of basic weak interior pointers. This works, but we really should've had tests for it. Fixes #70739. Change-Id: Ib49a7f8f0f1ec3db4571a7afb0f4d94c8a93aa40 Reviewed-on: https://go-review.googlesource.com/c/go/+/634598 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Michael Knyszek <mknyszek@google.com> Commit-Queue: Michael Knyszek <mknyszek@google.com>
2024-11-18weak: move internal/weak to weak, and update according to proposalMichael Anthony Knyszek
The updates are: - API documentation changes. - Removal of the old package documentation discouraging linkname. - Addition of new package documentation with some advice. - Renaming of weak.Pointer.Strong -> weak.Pointer.Value. Fixes #67552. Change-Id: Ifad7e629b6d339dacaf2ca37b459d7f903e31bf8 Reviewed-on: https://go-review.googlesource.com/c/go/+/628455 LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Carlos Amedee <carlos@golang.org> Auto-Submit: Michael Knyszek <mknyszek@google.com>