File tree Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Expand file tree Collapse file tree 2 files changed +27
-3
lines changed Original file line number Diff line number Diff line change @@ -107,14 +107,20 @@ func pinnerGetPtr(i *any) unsafe.Pointer {
107
107
if etyp == nil {
108
108
panic (errorString ("runtime.Pinner: argument is nil" ))
109
109
}
110
- if kind := etyp .Kind_ & kindMask ; kind != kindPtr && kind != kindUnsafePointer {
111
- panic (errorString ("runtime.Pinner: argument is not a pointer: " + toRType (etyp ).string ()))
110
+ var data unsafe.Pointer
111
+ kind := etyp .Kind_ & kindMask
112
+ switch kind {
113
+ case kindPtr , kindUnsafePointer :
114
+ case kindString :
115
+ data = unsafe .Pointer (unsafe .StringData (* (* string )(e .data )))
116
+ default :
117
+ panic (errorString ("runtime.Pinner: argument is not a pointer or string: " + toRType (etyp ).string ()))
112
118
}
113
119
if inUserArenaChunk (uintptr (e .data )) {
114
120
// Arena-allocated objects are not eligible for pinning.
115
121
panic (errorString ("runtime.Pinner: object was allocated into an arena" ))
116
122
}
117
- return e . data
123
+ return data
118
124
}
119
125
120
126
// isPinned checks if a Go pointer is pinned.
Original file line number Diff line number Diff line change @@ -538,3 +538,21 @@ func TestPinnerConstStringData(t *testing.T) {
538
538
t .Fatal ("not marked as pinned" )
539
539
}
540
540
}
541
+
542
+ func TestPinnerPinString (t * testing.T ) {
543
+ var pinner runtime.Pinner
544
+ heapStr := getHeapStr ()
545
+ pinner .Pin (heapStr )
546
+ addr := unsafe .Pointer (unsafe .StringData (heapStr ))
547
+ if ! runtime .IsPinned (addr ) {
548
+ t .Fatal ("not marked as pinned" )
549
+ }
550
+ pinner .Unpin ()
551
+ if runtime .IsPinned (addr ) {
552
+ t .Fatal ("still marked as pinned" )
553
+ }
554
+ }
555
+
556
+ func getHeapStr () string {
557
+ return string (byte (fastrand ()))
558
+ }
You can’t perform that action at this time.
0 commit comments