Debian Codegraph @

src/runtime/iface.go

raw
1 // Copyright 2014 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package runtime
6
7 import (
8 "internal/abi"
9 "internal/goarch"
10 "internal/runtime/atomic"
11 "internal/runtime/sys"
12 "unsafe"
13 )
14
15 const itabInitSize = 512
16
17 var (
18 itabLock mutex // lock for accessing itab table
19 itabTable = &itabTableInit // pointer to current table
20 itabTableInit = itabTableType{size: itabInitSize} // starter table
21 )
22
23 // Note: change the formula in the mallocgc call in itabAdd if you change these fields.
24 type itabTableType struct {
25 size uintptr // length of entries array. Always a power of 2.
26 count uintptr // current number of filled entries.
27 entries [itabInitSize]*itab // really [size] large
28 }
29
30 func itabHashFunc(inter *interfacetype, typ *_type) uintptr {
31 // compiler has provided some good hash codes for us.
32 return uintptr(inter.Type.Hash ^ typ.Hash)
33 }
34
35 // getitab should be an internal detail,
36 // but widely used packages access it using linkname.
37 // Notable members of the hall of shame include:
38 // - github.com/bytedance/sonic
39 //
40 // Do not remove or change the type signature.
41 // See go.dev/issue/67401.
42 //
43 //go:linkname getitab
44 func getitab(inter *interfacetype, typ *_type, canfail bool) *itab {
45 if len(inter.Methods) == 0 {
46 throw("internal error - misuse of itab")
47 }
48
49 // easy case
50 if typ.TFlag&abi.TFlagUncommon == 0 {
51 if canfail {
52 return nil
53 }
54 name := toRType(&inter.Type).nameOff(inter.Methods[0].Name)
55 panic(&TypeAssertionError{nil, typ, &inter.Type, name.Name()})
56 }
57
58 var m *itab
59
60 // First, look in the existing table to see if we can find the itab we need.
61 // This is by far the most common case, so do it without locks.
62 // Use atomic to ensure we see any previous writes done by the thread
63 // that updates the itabTable field (with atomic.Storep in itabAdd).
64 t := (*itabTableType)(atomic.Loadp(unsafe.Pointer(&itabTable)))
65 if m = t.find(inter, typ); m != nil {
66 goto finish
67 }
68
69 // Not found. Grab the lock and try again.
70 lock(&itabLock)
71 if m = itabTable.find(inter, typ); m != nil {
72 unlock(&itabLock)
73 goto finish
74 }
75
76 // Entry doesn't exist yet. Make a new entry & add it.
77 m = (*itab)(persistentalloc(unsafe.Sizeof(itab{})+uintptr(len(inter.Methods)-1)*goarch.PtrSize, 0, &memstats.other_sys))
78 m.Inter = inter
79 m.Type = typ
80 // The hash is used in type switches. However, compiler statically generates itab's
81 // for all interface/type pairs used in switches (which are added to itabTable
82 // in itabsinit). The dynamically-generated itab's never participate in type switches,
83 // and thus the hash is irrelevant.
84 // Note: m.Hash is _not_ the hash used for the runtime itabTable hash table.
85 m.Hash = 0
86 itabInit(m, true)
87 itabAdd(m)
88 unlock(&itabLock)
89 finish:
90 if m.Fun[0] != 0 {
91 return m
92 }
93 if canfail {
94 return nil
95 }
96 // this can only happen if the conversion
97 // was already done once using the , ok form
98 // and we have a cached negative result.
99 // The cached result doesn't record which
100 // interface function was missing, so initialize
101 // the itab again to get the missing function name.
102 panic(&TypeAssertionError{concrete: typ, asserted: &inter.Type, missingMethod: itabInit(m, false)})
103 }
104
105 // find finds the given interface/type pair in t.
106 // Returns nil if the given interface/type pair isn't present.
107 func (t *itabTableType) find(inter *interfacetype, typ *_type) *itab {
108 // Implemented using quadratic probing.
109 // Probe sequence is h(i) = h0 + i*(i+1)/2 mod 2^k.
110 // We're guaranteed to hit all table entries using this probe sequence.
111 mask := t.size - 1
112 h := itabHashFunc(inter, typ) & mask
113 for i := uintptr(1); ; i++ {
114 p := (**itab)(add(unsafe.Pointer(&t.entries), h*goarch.PtrSize))
115 // Use atomic read here so if we see m != nil, we also see
116 // the initializations of the fields of m.
117 // m := *p
118 m := (*itab)(atomic.Loadp(unsafe.Pointer(p)))
119 if m == nil {
120 return nil
121 }
122 if m.Inter == inter && m.Type == typ {
123 return m
124 }
125 h += i
126 h &= mask
127 }
128 }
129
130 // itabAdd adds the given itab to the itab hash table.
131 // itabLock must be held.
132 func itabAdd(m *itab) {
133 // Bugs can lead to calling this while mallocing is set,
134 // typically because this is called while panicking.
135 // Crash reliably, rather than only when we need to grow
136 // the hash table.
137 if getg().m.mallocing != 0 {
138 throw("malloc deadlock")
139 }
140
141 t := itabTable
142 if t.count >= 3*(t.size/4) { // 75% load factor
143 // Grow hash table.
144 // t2 = new(itabTableType) + some additional entries
145 // We lie and tell malloc we want pointer-free memory because
146 // all the pointed-to values are not in the heap.
147 t2 := (*itabTableType)(mallocgc((2+2*t.size)*goarch.PtrSize, nil, true))
148 t2.size = t.size * 2
149
150 // Copy over entries.
151 // Note: while copying, other threads may look for an itab and
152 // fail to find it. That's ok, they will then try to get the itab lock
153 // and as a consequence wait until this copying is complete.
154 iterate_itabs(t2.add)
155 if t2.count != t.count {
156 throw("mismatched count during itab table copy")
157 }
158 // Publish new hash table. Use an atomic write: see comment in getitab.
159 atomicstorep(unsafe.Pointer(&itabTable), unsafe.Pointer(t2))
160 // Adopt the new table as our own.
161 t = itabTable
162 // Note: the old table can be GC'ed here.
163 }
164 t.add(m)
165 }
166
167 // add adds the given itab to itab table t.
168 // itabLock must be held.
169 func (t *itabTableType) add(m *itab) {
170 // See comment in find about the probe sequence.
171 // Insert new itab in the first empty spot in the probe sequence.
172 mask := t.size - 1
173 h := itabHashFunc(m.Inter, m.Type) & mask
174 for i := uintptr(1); ; i++ {
175 p := (**itab)(add(unsafe.Pointer(&t.entries), h*goarch.PtrSize))
176 m2 := *p
177 if m2 == m {
178 // A given itab may be used in more than one module
179 // and thanks to the way global symbol resolution works, the
180 // pointed-to itab may already have been inserted into the
181 // global 'hash'.
182 return
183 }
184 if m2 == nil {
185 // Use atomic write here so if a reader sees m, it also
186 // sees the correctly initialized fields of m.
187 // NoWB is ok because m is not in heap memory.
188 // *p = m
189 atomic.StorepNoWB(unsafe.Pointer(p), unsafe.Pointer(m))
190 t.count++
191 return
192 }
193 h += i
194 h &= mask
195 }
196 }
197
198 // itabInit fills in the m.Fun array with all the code pointers for
199 // the m.Inter/m.Type pair. If the type does not implement the interface,
200 // it sets m.Fun[0] to 0 and returns the name of an interface function that is missing.
201 // If !firstTime, itabInit will not write anything to m.Fun (see issue 65962).
202 // It is ok to call this multiple times on the same m, even concurrently
203 // (although it will only be called once with firstTime==true).
204 //
205 // itabInit stores only code pointers, so it must not emit a write barrier;
206 // //go:nowritebarrier enforces that (see the methods store below).
207 //
208 //go:nowritebarrier
209 func itabInit(m *itab, firstTime bool) string {
210 inter := m.Inter
211 typ := m.Type
212 x := typ.Uncommon()
213
214 // both inter and typ have method sorted by name,
215 // and interface names are unique,
216 // so can iterate over both in lock step;
217 // the loop is O(ni+nt) not O(ni*nt).
218 ni := len(inter.Methods)
219 nt := int(x.Mcount)
220 xmhdr := (*[1 << 16]abi.Method)(add(unsafe.Pointer(x), uintptr(x.Moff)))[:nt:nt]
221 j := 0
222 // These are code pointers, not heap pointers. Use a uintptr slice so the
223 // store emits no write barrier: on wasm a code pointer can look like a
224 // heap pointer and make the GC crash (see issue 80472).
225 methods := (*[1 << 16]uintptr)(unsafe.Pointer(&m.Fun[0]))[:ni:ni]
226 var fun0 unsafe.Pointer
227 imethods:
228 for k := 0; k < ni; k++ {
229 i := &inter.Methods[k]
230 itype := toRType(&inter.Type).typeOff(i.Typ)
231 name := toRType(&inter.Type).nameOff(i.Name)
232 iname := name.Name()
233 ipkg := pkgPath(name)
234 if ipkg == "" {
235 ipkg = inter.PkgPath.Name()
236 }
237 for ; j < nt; j++ {
238 t := &xmhdr[j]
239 rtyp := toRType(typ)
240 tname := rtyp.nameOff(t.Name)
241 if rtyp.typeOff(t.Mtyp) == itype && tname.Name() == iname {
242 pkgPath := pkgPath(tname)
243 if pkgPath == "" {
244 pkgPath = rtyp.nameOff(x.PkgPath).Name()
245 }
246 if tname.IsExported() || pkgPath == ipkg {
247 ifn := rtyp.textOff(t.Ifn)
248 if k == 0 {
249 fun0 = ifn // we'll set m.Fun[0] at the end
250 } else if firstTime {
251 methods[k] = uintptr(ifn)
252 }
253 continue imethods
254 }
255 }
256 }
257 // didn't find method
258 // Leaves m.Fun[0] set to 0.
259 return iname
260 }
261 if firstTime {
262 m.Fun[0] = uintptr(fun0)
263 }
264 return ""
265 }
266
267 func itabsinit() {
268 lockInit(&itabLock, lockRankItab)
269 lock(&itabLock)
270 for _, md := range activeModules() {
271 for _, i := range md.itablinks {
272 itabAdd(i)
273 }
274 }
275 unlock(&itabLock)
276 }
277
278 // panicdottypeE is called when doing an e.(T) conversion and the conversion fails.
279 // have = the dynamic type we have.
280 // want = the static type we're trying to convert to.
281 // iface = the static type we're converting from.
282 func panicdottypeE(have, want, iface *_type) {
283 panic(&TypeAssertionError{iface, have, want, ""})
284 }
285
286 // panicdottypeI is called when doing an i.(T) conversion and the conversion fails.
287 // Same args as panicdottypeE, but "have" is the dynamic itab we have.
288 func panicdottypeI(have *itab, want, iface *_type) {
289 var t *_type
290 if have != nil {
291 t = have.Type
292 }
293 panicdottypeE(t, want, iface)
294 }
295
296 // panicnildottype is called when doing an i.(T) conversion and the interface i is nil.
297 // want = the static type we're trying to convert to.
298 func panicnildottype(want *_type) {
299 panic(&TypeAssertionError{nil, nil, want, ""})
300 // TODO: Add the static type we're converting from as well.
301 // It might generate a better error message.
302 // Just to match other nil conversion errors, we don't for now.
303 }
304
305 // The specialized convTx routines need a type descriptor to use when calling mallocgc.
306 // We don't need the type to be exact, just to have the correct size, alignment, and pointer-ness.
307 // However, when debugging, it'd be nice to have some indication in mallocgc where the types came from,
308 // so we use named types here.
309 // We then construct interface values of these types,
310 // and then extract the type word to use as needed.
311 type (
312 uint16InterfacePtr uint16
313 uint32InterfacePtr uint32
314 uint64InterfacePtr uint64
315 stringInterfacePtr string
316 sliceInterfacePtr []byte
317 )
318
319 var (
320 uint16Eface any = uint16InterfacePtr(0)
321 uint32Eface any = uint32InterfacePtr(0)
322 uint64Eface any = uint64InterfacePtr(0)
323 stringEface any = stringInterfacePtr("")
324 sliceEface any = sliceInterfacePtr(nil)
325
326 uint16Type *_type = efaceOf(&uint16Eface)._type
327 uint32Type *_type = efaceOf(&uint32Eface)._type
328 uint64Type *_type = efaceOf(&uint64Eface)._type
329 stringType *_type = efaceOf(&stringEface)._type
330 sliceType *_type = efaceOf(&sliceEface)._type
331 )
332
333 // The conv and assert functions below do very similar things.
334 // The convXXX functions are guaranteed by the compiler to succeed.
335 // The assertXXX functions may fail (either panicking or returning false,
336 // depending on whether they are 1-result or 2-result).
337 // The convXXX functions succeed on a nil input, whereas the assertXXX
338 // functions fail on a nil input.
339
340 // convT converts a value of type t, which is pointed to by v, to a pointer that can
341 // be used as the second word of an interface value.
342 func convT(t *_type, v unsafe.Pointer) unsafe.Pointer {
343 if raceenabled {
344 raceReadObjectPC(t, v, sys.GetCallerPC(), abi.FuncPCABIInternal(convT))
345 }
346 if msanenabled {
347 msanread(v, t.Size_)
348 }
349 if asanenabled {
350 asanread(v, t.Size_)
351 }
352 x := mallocgc(t.Size_, t, true)
353 typedmemmove(t, x, v)
354 return x
355 }
356 func convTnoptr(t *_type, v unsafe.Pointer) unsafe.Pointer {
357 // TODO: maybe take size instead of type?
358 if raceenabled {
359 raceReadObjectPC(t, v, sys.GetCallerPC(), abi.FuncPCABIInternal(convTnoptr))
360 }
361 if msanenabled {
362 msanread(v, t.Size_)
363 }
364 if asanenabled {
365 asanread(v, t.Size_)
366 }
367
368 x := mallocgc(t.Size_, t, false)
369 memmove(x, v, t.Size_)
370 return x
371 }
372
373 func convT16(val uint16) (x unsafe.Pointer) {
374 if val < uint16(len(staticuint64s)) {
375 x = unsafe.Pointer(&staticuint64s[val])
376 if goarch.BigEndian {
377 x = add(x, 6)
378 }
379 } else {
380 x = mallocgc(2, uint16Type, false)
381 *(*uint16)(x) = val
382 }
383 return
384 }
385
386 func convT32(val uint32) (x unsafe.Pointer) {
387 if val < uint32(len(staticuint64s)) {
388 x = unsafe.Pointer(&staticuint64s[val])
389 if goarch.BigEndian {
390 x = add(x, 4)
391 }
392 } else {
393 x = mallocgc(4, uint32Type, false)
394 *(*uint32)(x) = val
395 }
396 return
397 }
398
399 // convT64 should be an internal detail,
400 // but widely used packages access it using linkname.
401 // Notable members of the hall of shame include:
402 // - github.com/bytedance/sonic
403 //
404 // Do not remove or change the type signature.
405 // See go.dev/issue/67401.
406 //
407 //go:linkname convT64
408 func convT64(val uint64) (x unsafe.Pointer) {
409 if val < uint64(len(staticuint64s)) {
410 x = unsafe.Pointer(&staticuint64s[val])
411 } else {
412 x = mallocgc(8, uint64Type, false)
413 *(*uint64)(x) = val
414 }
415 return
416 }
417
418 // convTstring should be an internal detail,
419 // but widely used packages access it using linkname.
420 // Notable members of the hall of shame include:
421 // - github.com/bytedance/sonic
422 //
423 // Do not remove or change the type signature.
424 // See go.dev/issue/67401.
425 //
426 //go:linkname convTstring
427 func convTstring(val string) (x unsafe.Pointer) {
428 if val == "" {
429 x = unsafe.Pointer(&zeroVal[0])
430 } else {
431 x = mallocgc(unsafe.Sizeof(val), stringType, true)
432 *(*string)(x) = val
433 }
434 return
435 }
436
437 // convTslice should be an internal detail,
438 // but widely used packages access it using linkname.
439 // Notable members of the hall of shame include:
440 // - github.com/bytedance/sonic
441 //
442 // Do not remove or change the type signature.
443 // See go.dev/issue/67401.
444 //
445 //go:linkname convTslice
446 func convTslice(val []byte) (x unsafe.Pointer) {
447 // Note: this must work for any element type, not just byte.
448 if (*slice)(unsafe.Pointer(&val)).array == nil {
449 x = unsafe.Pointer(&zeroVal[0])
450 } else {
451 x = mallocgc(unsafe.Sizeof(val), sliceType, true)
452 *(*[]byte)(x) = val
453 }
454 return
455 }
456
457 func assertE2I(inter *interfacetype, t *_type) *itab {
458 if t == nil {
459 // explicit conversions require non-nil interface value.
460 panic(&TypeAssertionError{nil, nil, &inter.Type, ""})
461 }
462 return getitab(inter, t, false)
463 }
464
465 func assertE2I2(inter *interfacetype, t *_type) *itab {
466 if t == nil {
467 return nil
468 }
469 return getitab(inter, t, true)
470 }
471
472 // typeAssert builds an itab for the concrete type t and the
473 // interface type s.Inter. If the conversion is not possible it
474 // panics if s.CanFail is false and returns nil if s.CanFail is true.
475 func typeAssert(s *abi.TypeAssert, t *_type) *itab {
476 var tab *itab
477 if t == nil {
478 if !s.CanFail {
479 panic(&TypeAssertionError{nil, nil, &s.Inter.Type, ""})
480 }
481 } else {
482 tab = getitab(s.Inter, t, s.CanFail)
483 }
484
485 if !abi.UseInterfaceSwitchCache(goarch.ArchFamily) {
486 return tab
487 }
488
489 // Maybe update the cache, so the next time the generated code
490 // doesn't need to call into the runtime.
491 if cheaprand()&1023 != 0 {
492 // Only bother updating the cache ~1 in 1000 times.
493 return tab
494 }
495 // Load the current cache.
496 oldC := (*abi.TypeAssertCache)(atomic.Loadp(unsafe.Pointer(&s.Cache)))
497
498 if cheaprand()&uint32(oldC.Mask) != 0 {
499 // As cache gets larger, choose to update it less often
500 // so we can amortize the cost of building a new cache.
501 return tab
502 }
503
504 // Make a new cache.
505 newC := buildTypeAssertCache(oldC, t, tab)
506
507 // Update cache. Use compare-and-swap so if multiple threads
508 // are fighting to update the cache, at least one of their
509 // updates will stick.
510 atomic_casPointer((*unsafe.Pointer)(unsafe.Pointer(&s.Cache)), unsafe.Pointer(oldC), unsafe.Pointer(newC))
511
512 return tab
513 }
514
515 func buildTypeAssertCache(oldC *abi.TypeAssertCache, typ *_type, tab *itab) *abi.TypeAssertCache {
516 oldEntries := unsafe.Slice(&oldC.Entries[0], oldC.Mask+1)
517
518 // Count the number of entries we need.
519 n := 1
520 for _, e := range oldEntries {
521 if e.Typ != 0 {
522 n++
523 }
524 }
525
526 // Figure out how big a table we need.
527 // We need at least one more slot than the number of entries
528 // so that we are guaranteed an empty slot (for termination).
529 newN := n * 2 // make it at most 50% full
530 newN = 1 << sys.Len64(uint64(newN-1)) // round up to a power of 2
531
532 // Allocate the new table.
533 newSize := unsafe.Sizeof(abi.TypeAssertCache{}) + uintptr(newN-1)*unsafe.Sizeof(abi.TypeAssertCacheEntry{})
534 newC := (*abi.TypeAssertCache)(mallocgc(newSize, nil, true))
535 newC.Mask = uintptr(newN - 1)
536 newEntries := unsafe.Slice(&newC.Entries[0], newN)
537
538 // Fill the new table.
539 addEntry := func(typ *_type, tab *itab) {
540 h := int(typ.Hash) & (newN - 1)
541 for {
542 if newEntries[h].Typ == 0 {
543 newEntries[h].Typ = uintptr(unsafe.Pointer(typ))
544 newEntries[h].Itab = uintptr(unsafe.Pointer(tab))
545 return
546 }
547 h = (h + 1) & (newN - 1)
548 }
549 }
550 for _, e := range oldEntries {
551 if e.Typ != 0 {
552 addEntry((*_type)(unsafe.Pointer(e.Typ)), (*itab)(unsafe.Pointer(e.Itab)))
553 }
554 }
555 addEntry(typ, tab)
556
557 return newC
558 }
559
560 // Empty type assert cache. Contains one entry with a nil Typ (which
561 // causes a cache lookup to fail immediately.)
562 var emptyTypeAssertCache = abi.TypeAssertCache{Mask: 0}
563
564 // interfaceSwitch compares t against the list of cases in s.
565 // If t matches case i, interfaceSwitch returns the case index i and
566 // an itab for the pair <t, s.Cases[i]>.
567 // If there is no match, return N,nil, where N is the number
568 // of cases.
569 func interfaceSwitch(s *abi.InterfaceSwitch, t *_type) (int, *itab) {
570 cases := unsafe.Slice(&s.Cases[0], s.NCases)
571
572 // Results if we don't find a match.
573 case_ := len(cases)
574 var tab *itab
575
576 // Look through each case in order.
577 for i, c := range cases {
578 tab = getitab(c, t, true)
579 if tab != nil {
580 case_ = i
581 break
582 }
583 }
584
585 if !abi.UseInterfaceSwitchCache(goarch.ArchFamily) {
586 return case_, tab
587 }
588
589 // Maybe update the cache, so the next time the generated code
590 // doesn't need to call into the runtime.
591 if cheaprand()&1023 != 0 {
592 // Only bother updating the cache ~1 in 1000 times.
593 // This ensures we don't waste memory on switches, or
594 // switch arguments, that only happen a few times.
595 return case_, tab
596 }
597 // Load the current cache.
598 oldC := (*abi.InterfaceSwitchCache)(atomic.Loadp(unsafe.Pointer(&s.Cache)))
599
600 if cheaprand()&uint32(oldC.Mask) != 0 {
601 // As cache gets larger, choose to update it less often
602 // so we can amortize the cost of building a new cache
603 // (that cost is linear in oldc.Mask).
604 return case_, tab
605 }
606
607 // Make a new cache.
608 newC := buildInterfaceSwitchCache(oldC, t, case_, tab)
609
610 // Update cache. Use compare-and-swap so if multiple threads
611 // are fighting to update the cache, at least one of their
612 // updates will stick.
613 atomic_casPointer((*unsafe.Pointer)(unsafe.Pointer(&s.Cache)), unsafe.Pointer(oldC), unsafe.Pointer(newC))
614
615 return case_, tab
616 }
617
618 // buildInterfaceSwitchCache constructs an interface switch cache
619 // containing all the entries from oldC plus the new entry
620 // (typ,case_,tab).
621 func buildInterfaceSwitchCache(oldC *abi.InterfaceSwitchCache, typ *_type, case_ int, tab *itab) *abi.InterfaceSwitchCache {
622 oldEntries := unsafe.Slice(&oldC.Entries[0], oldC.Mask+1)
623
624 // Count the number of entries we need.
625 n := 1
626 for _, e := range oldEntries {
627 if e.Typ != 0 {
628 n++
629 }
630 }
631
632 // Figure out how big a table we need.
633 // We need at least one more slot than the number of entries
634 // so that we are guaranteed an empty slot (for termination).
635 newN := n * 2 // make it at most 50% full
636 newN = 1 << sys.Len64(uint64(newN-1)) // round up to a power of 2
637
638 // Allocate the new table.
639 newSize := unsafe.Sizeof(abi.InterfaceSwitchCache{}) + uintptr(newN-1)*unsafe.Sizeof(abi.InterfaceSwitchCacheEntry{})
640 newC := (*abi.InterfaceSwitchCache)(mallocgc(newSize, nil, true))
641 newC.Mask = uintptr(newN - 1)
642 newEntries := unsafe.Slice(&newC.Entries[0], newN)
643
644 // Fill the new table.
645 addEntry := func(typ *_type, case_ int, tab *itab) {
646 h := int(typ.Hash) & (newN - 1)
647 for {
648 if newEntries[h].Typ == 0 {
649 newEntries[h].Typ = uintptr(unsafe.Pointer(typ))
650 newEntries[h].Case = case_
651 newEntries[h].Itab = uintptr(unsafe.Pointer(tab))
652 return
653 }
654 h = (h + 1) & (newN - 1)
655 }
656 }
657 for _, e := range oldEntries {
658 if e.Typ != 0 {
659 addEntry((*_type)(unsafe.Pointer(e.Typ)), e.Case, (*itab)(unsafe.Pointer(e.Itab)))
660 }
661 }
662 addEntry(typ, case_, tab)
663
664 return newC
665 }
666
667 // Empty interface switch cache. Contains one entry with a nil Typ (which
668 // causes a cache lookup to fail immediately.)
669 var emptyInterfaceSwitchCache = abi.InterfaceSwitchCache{Mask: 0}
670
671 // reflect_ifaceE2I is for package reflect,
672 // but widely used packages access it using linkname.
673 // Notable members of the hall of shame include:
674 // - gitee.com/quant1x/gox
675 // - github.com/modern-go/reflect2
676 // - github.com/v2pro/plz
677 //
678 // Do not remove or change the type signature.
679 //
680 //go:linkname reflect_ifaceE2I reflect.ifaceE2I
681 func reflect_ifaceE2I(inter *interfacetype, e eface, dst *iface) {
682 *dst = iface{assertE2I(inter, e._type), e.data}
683 }
684
685 //go:linkname reflectlite_ifaceE2I internal/reflectlite.ifaceE2I
686 func reflectlite_ifaceE2I(inter *interfacetype, e eface, dst *iface) {
687 *dst = iface{assertE2I(inter, e._type), e.data}
688 }
689
690 func iterate_itabs(fn func(*itab)) {
691 // Note: only runs during stop the world or with itabLock held,
692 // so no other locks/atomics needed.
693 t := itabTable
694 for i := uintptr(0); i < t.size; i++ {
695 m := *(**itab)(add(unsafe.Pointer(&t.entries), i*goarch.PtrSize))
696 if m != nil {
697 fn(m)
698 }
699 }
700 }
701
702 // staticuint64s is used to avoid allocating in convTx for small integer values.
703 // staticuint64s[0] == 0, staticuint64s[1] == 1, and so forth.
704 // It is defined in assembler code so that it is read-only.
705 var staticuint64s [256]uint64
706
707 // getStaticuint64s is called by the reflect package to get a pointer
708 // to the read-only array.
709 //
710 //go:linkname getStaticuint64s
711 func getStaticuint64s() *[256]uint64 {
712 return &staticuint64s
713 }
714
715 // The linker redirects a reference of a method that it determined
716 // unreachable to a reference to this function, so it will throw if
717 // ever called.
718 func unreachableMethod() {
719 throw("unreachable method called. linker bug?")
720 }