]> Cypherpunks.ru repositories - gostls13.git/blob - src/runtime/cgocall.go
[dev.boringcrypto] crypto/hmac: merge up to 2a206c7 and skip test
[gostls13.git] / src / runtime / cgocall.go
1 // Copyright 2009 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 // Cgo call and callback support.
6 //
7 // To call into the C function f from Go, the cgo-generated code calls
8 // runtime.cgocall(_cgo_Cfunc_f, frame), where _cgo_Cfunc_f is a
9 // gcc-compiled function written by cgo.
10 //
11 // runtime.cgocall (below) calls entersyscall so as not to block
12 // other goroutines or the garbage collector, and then calls
13 // runtime.asmcgocall(_cgo_Cfunc_f, frame).
14 //
15 // runtime.asmcgocall (in asm_$GOARCH.s) switches to the m->g0 stack
16 // (assumed to be an operating system-allocated stack, so safe to run
17 // gcc-compiled code on) and calls _cgo_Cfunc_f(frame).
18 //
19 // _cgo_Cfunc_f invokes the actual C function f with arguments
20 // taken from the frame structure, records the results in the frame,
21 // and returns to runtime.asmcgocall.
22 //
23 // After it regains control, runtime.asmcgocall switches back to the
24 // original g (m->curg)'s stack and returns to runtime.cgocall.
25 //
26 // After it regains control, runtime.cgocall calls exitsyscall, which blocks
27 // until this m can run Go code without violating the $GOMAXPROCS limit,
28 // and then unlocks g from m.
29 //
30 // The above description skipped over the possibility of the gcc-compiled
31 // function f calling back into Go. If that happens, we continue down
32 // the rabbit hole during the execution of f.
33 //
34 // To make it possible for gcc-compiled C code to call a Go function p.GoF,
35 // cgo writes a gcc-compiled function named GoF (not p.GoF, since gcc doesn't
36 // know about packages).  The gcc-compiled C function f calls GoF.
37 //
38 // GoF calls crosscall2(_cgoexp_GoF, frame, framesize, ctxt).
39 // Crosscall2 (in cgo/asm_$GOARCH.s) is a four-argument adapter from
40 // the gcc function call ABI to the gc function call ABI.
41 // It is called from gcc to call gc functions. In this case it calls
42 // _cgoexp_GoF(frame, framesize), still running on m.g0's stack
43 // and outside the $GOMAXPROCS limit. Thus, this code cannot yet
44 // call arbitrary Go code directly and must be careful not to allocate
45 // memory or use up m.g0's stack.
46 //
47 // _cgoexp_GoF (generated by cmd/cgo) calls
48 // runtime.cgocallback(funcPC(p.GoF), frame, framesize, ctxt).
49 // (The reason for having _cgoexp_GoF instead of writing a crosscall3
50 // to make this call directly is that _cgoexp_GoF, because it is compiled
51 // with gc instead of gcc, can refer to dotted names like
52 // runtime.cgocallback and p.GoF.)
53 //
54 // runtime.cgocallback (in asm_$GOARCH.s) turns the raw PC of p.GoF
55 // into a Go function value and calls runtime.cgocallback_gofunc.
56 //
57 // runtime.cgocallback_gofunc (in asm_$GOARCH.s) switches from m.g0's
58 // stack to the original g (m.curg)'s stack, on which it calls
59 // runtime.cgocallbackg(p.GoF, frame, framesize).
60 // As part of the stack switch, runtime.cgocallback saves the current
61 // SP as m.g0.sched.sp, so that any use of m.g0's stack during the
62 // execution of the callback will be done below the existing stack frames.
63 // Before overwriting m.g0.sched.sp, it pushes the old value on the
64 // m.g0 stack, so that it can be restored later.
65 //
66 // runtime.cgocallbackg (below) is now running on a real goroutine
67 // stack (not an m.g0 stack).  First it calls runtime.exitsyscall, which will
68 // block until the $GOMAXPROCS limit allows running this goroutine.
69 // Once exitsyscall has returned, it is safe to do things like call the memory
70 // allocator or invoke the Go callback function p.GoF.  runtime.cgocallbackg
71 // first defers a function to unwind m.g0.sched.sp, so that if p.GoF
72 // panics, m.g0.sched.sp will be restored to its old value: the m.g0 stack
73 // and the m.curg stack will be unwound in lock step.
74 // Then it calls p.GoF.  Finally it pops but does not execute the deferred
75 // function, calls runtime.entersyscall, and returns to runtime.cgocallback.
76 //
77 // After it regains control, runtime.cgocallback switches back to
78 // m.g0's stack (the pointer is still in m.g0.sched.sp), restores the old
79 // m.g0.sched.sp value from the stack, and returns to _cgoexp_GoF.
80 //
81 // _cgoexp_GoF immediately returns to crosscall2, which restores the
82 // callee-save registers for gcc and returns to GoF, which returns to f.
83
84 package runtime
85
86 import (
87         "runtime/internal/atomic"
88         "runtime/internal/sys"
89         "unsafe"
90 )
91
92 // Addresses collected in a cgo backtrace when crashing.
93 // Length must match arg.Max in x_cgo_callers in runtime/cgo/gcc_traceback.c.
94 type cgoCallers [32]uintptr
95
96 // Call from Go to C.
97 //
98 // This must be nosplit because it's used for syscalls on some
99 // platforms. Syscalls may have untyped arguments on the stack, so
100 // it's not safe to grow or scan the stack.
101 //
102 //go:nosplit
103 func cgocall(fn, arg unsafe.Pointer) int32 {
104         if !iscgo && GOOS != "solaris" && GOOS != "illumos" && GOOS != "windows" {
105                 throw("cgocall unavailable")
106         }
107
108         if fn == nil {
109                 throw("cgocall nil")
110         }
111
112         if raceenabled {
113                 racereleasemerge(unsafe.Pointer(&racecgosync))
114         }
115
116         mp := getg().m
117         mp.ncgocall++
118         mp.ncgo++
119
120         // Reset traceback.
121         mp.cgoCallers[0] = 0
122
123         // Announce we are entering a system call
124         // so that the scheduler knows to create another
125         // M to run goroutines while we are in the
126         // foreign code.
127         //
128         // The call to asmcgocall is guaranteed not to
129         // grow the stack and does not allocate memory,
130         // so it is safe to call while "in a system call", outside
131         // the $GOMAXPROCS accounting.
132         //
133         // fn may call back into Go code, in which case we'll exit the
134         // "system call", run the Go code (which may grow the stack),
135         // and then re-enter the "system call" reusing the PC and SP
136         // saved by entersyscall here.
137         entersyscall()
138
139         // Tell asynchronous preemption that we're entering external
140         // code. We do this after entersyscall because this may block
141         // and cause an async preemption to fail, but at this point a
142         // sync preemption will succeed (though this is not a matter
143         // of correctness).
144         osPreemptExtEnter(mp)
145
146         mp.incgo = true
147         errno := asmcgocall(fn, arg)
148
149         // Update accounting before exitsyscall because exitsyscall may
150         // reschedule us on to a different M.
151         mp.incgo = false
152         mp.ncgo--
153
154         osPreemptExtExit(mp)
155
156         exitsyscall()
157
158         // Note that raceacquire must be called only after exitsyscall has
159         // wired this M to a P.
160         if raceenabled {
161                 raceacquire(unsafe.Pointer(&racecgosync))
162         }
163
164         // From the garbage collector's perspective, time can move
165         // backwards in the sequence above. If there's a callback into
166         // Go code, GC will see this function at the call to
167         // asmcgocall. When the Go call later returns to C, the
168         // syscall PC/SP is rolled back and the GC sees this function
169         // back at the call to entersyscall. Normally, fn and arg
170         // would be live at entersyscall and dead at asmcgocall, so if
171         // time moved backwards, GC would see these arguments as dead
172         // and then live. Prevent these undead arguments from crashing
173         // GC by forcing them to stay live across this time warp.
174         KeepAlive(fn)
175         KeepAlive(arg)
176         KeepAlive(mp)
177
178         return errno
179 }
180
181 // Call from C back to Go.
182 //go:nosplit
183 func cgocallbackg(ctxt uintptr) {
184         gp := getg()
185         if gp != gp.m.curg {
186                 println("runtime: bad g in cgocallback")
187                 exit(2)
188         }
189
190         // The call from C is on gp.m's g0 stack, so we must ensure
191         // that we stay on that M. We have to do this before calling
192         // exitsyscall, since it would otherwise be free to move us to
193         // a different M. The call to unlockOSThread is in unwindm.
194         lockOSThread()
195
196         // Save current syscall parameters, so m.syscall can be
197         // used again if callback decide to make syscall.
198         syscall := gp.m.syscall
199
200         // entersyscall saves the caller's SP to allow the GC to trace the Go
201         // stack. However, since we're returning to an earlier stack frame and
202         // need to pair with the entersyscall() call made by cgocall, we must
203         // save syscall* and let reentersyscall restore them.
204         savedsp := unsafe.Pointer(gp.syscallsp)
205         savedpc := gp.syscallpc
206         exitsyscall() // coming out of cgo call
207         gp.m.incgo = false
208
209         osPreemptExtExit(gp.m)
210
211         cgocallbackg1(ctxt)
212
213         // At this point unlockOSThread has been called.
214         // The following code must not change to a different m.
215         // This is enforced by checking incgo in the schedule function.
216
217         osPreemptExtEnter(gp.m)
218
219         gp.m.incgo = true
220         // going back to cgo call
221         reentersyscall(savedpc, uintptr(savedsp))
222
223         gp.m.syscall = syscall
224 }
225
226 func cgocallbackg1(ctxt uintptr) {
227         gp := getg()
228         if gp.m.needextram || atomic.Load(&extraMWaiters) > 0 {
229                 gp.m.needextram = false
230                 systemstack(newextram)
231         }
232
233         if ctxt != 0 {
234                 s := append(gp.cgoCtxt, ctxt)
235
236                 // Now we need to set gp.cgoCtxt = s, but we could get
237                 // a SIGPROF signal while manipulating the slice, and
238                 // the SIGPROF handler could pick up gp.cgoCtxt while
239                 // tracing up the stack.  We need to ensure that the
240                 // handler always sees a valid slice, so set the
241                 // values in an order such that it always does.
242                 p := (*slice)(unsafe.Pointer(&gp.cgoCtxt))
243                 atomicstorep(unsafe.Pointer(&p.array), unsafe.Pointer(&s[0]))
244                 p.cap = cap(s)
245                 p.len = len(s)
246
247                 defer func(gp *g) {
248                         // Decrease the length of the slice by one, safely.
249                         p := (*slice)(unsafe.Pointer(&gp.cgoCtxt))
250                         p.len--
251                 }(gp)
252         }
253
254         if gp.m.ncgo == 0 {
255                 // The C call to Go came from a thread not currently running
256                 // any Go. In the case of -buildmode=c-archive or c-shared,
257                 // this call may be coming in before package initialization
258                 // is complete. Wait until it is.
259                 <-main_init_done
260         }
261
262         // Add entry to defer stack in case of panic.
263         restore := true
264         defer unwindm(&restore)
265
266         if raceenabled {
267                 raceacquire(unsafe.Pointer(&racecgosync))
268         }
269
270         type args struct {
271                 fn      *funcval
272                 arg     unsafe.Pointer
273                 argsize uintptr
274         }
275         var cb *args
276
277         // Location of callback arguments depends on stack frame layout
278         // and size of stack frame of cgocallback_gofunc.
279         sp := gp.m.g0.sched.sp
280         switch GOARCH {
281         default:
282                 throw("cgocallbackg is unimplemented on arch")
283         case "arm":
284                 // On arm, stack frame is two words and there's a saved LR between
285                 // SP and the stack frame and between the stack frame and the arguments.
286                 cb = (*args)(unsafe.Pointer(sp + 4*sys.PtrSize))
287         case "arm64":
288                 // On arm64, stack frame is four words and there's a saved LR between
289                 // SP and the stack frame and between the stack frame and the arguments.
290                 // Additional two words (16-byte alignment) are for saving FP.
291                 cb = (*args)(unsafe.Pointer(sp + 7*sys.PtrSize))
292         case "amd64":
293                 // On amd64, stack frame is two words, plus caller PC and BP.
294                 cb = (*args)(unsafe.Pointer(sp + 4*sys.PtrSize))
295         case "386":
296                 // On 386, stack frame is three words, plus caller PC.
297                 cb = (*args)(unsafe.Pointer(sp + 4*sys.PtrSize))
298         case "ppc64", "ppc64le", "s390x":
299                 // On ppc64 and s390x, the callback arguments are in the arguments area of
300                 // cgocallback's stack frame. The stack looks like this:
301                 // +--------------------+------------------------------+
302                 // |                    | ...                          |
303                 // | cgoexp_$fn         +------------------------------+
304                 // |                    | fixed frame area             |
305                 // +--------------------+------------------------------+
306                 // |                    | arguments area               |
307                 // | cgocallback        +------------------------------+ <- sp + 2*minFrameSize + 2*ptrSize
308                 // |                    | fixed frame area             |
309                 // +--------------------+------------------------------+ <- sp + minFrameSize + 2*ptrSize
310                 // |                    | local variables (2 pointers) |
311                 // | cgocallback_gofunc +------------------------------+ <- sp + minFrameSize
312                 // |                    | fixed frame area             |
313                 // +--------------------+------------------------------+ <- sp
314                 cb = (*args)(unsafe.Pointer(sp + 2*sys.MinFrameSize + 2*sys.PtrSize))
315         case "mips64", "mips64le":
316                 // On mips64x, stack frame is two words and there's a saved LR between
317                 // SP and the stack frame and between the stack frame and the arguments.
318                 cb = (*args)(unsafe.Pointer(sp + 4*sys.PtrSize))
319         case "mips", "mipsle":
320                 // On mipsx, stack frame is two words and there's a saved LR between
321                 // SP and the stack frame and between the stack frame and the arguments.
322                 cb = (*args)(unsafe.Pointer(sp + 4*sys.PtrSize))
323         }
324
325         // Invoke callback.
326         // NOTE(rsc): passing nil for argtype means that the copying of the
327         // results back into cb.arg happens without any corresponding write barriers.
328         // For cgo, cb.arg points into a C stack frame and therefore doesn't
329         // hold any pointers that the GC can find anyway - the write barrier
330         // would be a no-op.
331         reflectcall(nil, unsafe.Pointer(cb.fn), cb.arg, uint32(cb.argsize), 0)
332
333         if raceenabled {
334                 racereleasemerge(unsafe.Pointer(&racecgosync))
335         }
336         if msanenabled {
337                 // Tell msan that we wrote to the entire argument block.
338                 // This tells msan that we set the results.
339                 // Since we have already called the function it doesn't
340                 // matter that we are writing to the non-result parameters.
341                 msanwrite(cb.arg, cb.argsize)
342         }
343
344         // Do not unwind m->g0->sched.sp.
345         // Our caller, cgocallback, will do that.
346         restore = false
347 }
348
349 func unwindm(restore *bool) {
350         if *restore {
351                 // Restore sp saved by cgocallback during
352                 // unwind of g's stack (see comment at top of file).
353                 mp := acquirem()
354                 sched := &mp.g0.sched
355                 switch GOARCH {
356                 default:
357                         throw("unwindm not implemented")
358                 case "386", "amd64", "arm", "ppc64", "ppc64le", "mips64", "mips64le", "s390x", "mips", "mipsle":
359                         sched.sp = *(*uintptr)(unsafe.Pointer(sched.sp + sys.MinFrameSize))
360                 case "arm64":
361                         sched.sp = *(*uintptr)(unsafe.Pointer(sched.sp + 16))
362                 }
363
364                 // Do the accounting that cgocall will not have a chance to do
365                 // during an unwind.
366                 //
367                 // In the case where a Go call originates from C, ncgo is 0
368                 // and there is no matching cgocall to end.
369                 if mp.ncgo > 0 {
370                         mp.incgo = false
371                         mp.ncgo--
372                         osPreemptExtExit(mp)
373                 }
374
375                 releasem(mp)
376         }
377
378         // Undo the call to lockOSThread in cgocallbackg.
379         // We must still stay on the same m.
380         unlockOSThread()
381 }
382
383 // called from assembly
384 func badcgocallback() {
385         throw("misaligned stack in cgocallback")
386 }
387
388 // called from (incomplete) assembly
389 func cgounimpl() {
390         throw("cgo not implemented")
391 }
392
393 var racecgosync uint64 // represents possible synchronization in C code
394
395 // Pointer checking for cgo code.
396
397 // We want to detect all cases where a program that does not use
398 // unsafe makes a cgo call passing a Go pointer to memory that
399 // contains a Go pointer. Here a Go pointer is defined as a pointer
400 // to memory allocated by the Go runtime. Programs that use unsafe
401 // can evade this restriction easily, so we don't try to catch them.
402 // The cgo program will rewrite all possibly bad pointer arguments to
403 // call cgoCheckPointer, where we can catch cases of a Go pointer
404 // pointing to a Go pointer.
405
406 // Complicating matters, taking the address of a slice or array
407 // element permits the C program to access all elements of the slice
408 // or array. In that case we will see a pointer to a single element,
409 // but we need to check the entire data structure.
410
411 // The cgoCheckPointer call takes additional arguments indicating that
412 // it was called on an address expression. An additional argument of
413 // true means that it only needs to check a single element. An
414 // additional argument of a slice or array means that it needs to
415 // check the entire slice/array, but nothing else. Otherwise, the
416 // pointer could be anything, and we check the entire heap object,
417 // which is conservative but safe.
418
419 // When and if we implement a moving garbage collector,
420 // cgoCheckPointer will pin the pointer for the duration of the cgo
421 // call.  (This is necessary but not sufficient; the cgo program will
422 // also have to change to pin Go pointers that cannot point to Go
423 // pointers.)
424
425 // cgoCheckPointer checks if the argument contains a Go pointer that
426 // points to a Go pointer, and panics if it does.
427 func cgoCheckPointer(ptr interface{}, arg interface{}) {
428         if debug.cgocheck == 0 {
429                 return
430         }
431
432         ep := efaceOf(&ptr)
433         t := ep._type
434
435         top := true
436         if arg != nil && (t.kind&kindMask == kindPtr || t.kind&kindMask == kindUnsafePointer) {
437                 p := ep.data
438                 if t.kind&kindDirectIface == 0 {
439                         p = *(*unsafe.Pointer)(p)
440                 }
441                 if p == nil || !cgoIsGoPointer(p) {
442                         return
443                 }
444                 aep := efaceOf(&arg)
445                 switch aep._type.kind & kindMask {
446                 case kindBool:
447                         if t.kind&kindMask == kindUnsafePointer {
448                                 // We don't know the type of the element.
449                                 break
450                         }
451                         pt := (*ptrtype)(unsafe.Pointer(t))
452                         cgoCheckArg(pt.elem, p, true, false, cgoCheckPointerFail)
453                         return
454                 case kindSlice:
455                         // Check the slice rather than the pointer.
456                         ep = aep
457                         t = ep._type
458                 case kindArray:
459                         // Check the array rather than the pointer.
460                         // Pass top as false since we have a pointer
461                         // to the array.
462                         ep = aep
463                         t = ep._type
464                         top = false
465                 default:
466                         throw("can't happen")
467                 }
468         }
469
470         cgoCheckArg(t, ep.data, t.kind&kindDirectIface == 0, top, cgoCheckPointerFail)
471 }
472
473 const cgoCheckPointerFail = "cgo argument has Go pointer to Go pointer"
474 const cgoResultFail = "cgo result has Go pointer"
475
476 // cgoCheckArg is the real work of cgoCheckPointer. The argument p
477 // is either a pointer to the value (of type t), or the value itself,
478 // depending on indir. The top parameter is whether we are at the top
479 // level, where Go pointers are allowed.
480 func cgoCheckArg(t *_type, p unsafe.Pointer, indir, top bool, msg string) {
481         if t.ptrdata == 0 || p == nil {
482                 // If the type has no pointers there is nothing to do.
483                 return
484         }
485
486         switch t.kind & kindMask {
487         default:
488                 throw("can't happen")
489         case kindArray:
490                 at := (*arraytype)(unsafe.Pointer(t))
491                 if !indir {
492                         if at.len != 1 {
493                                 throw("can't happen")
494                         }
495                         cgoCheckArg(at.elem, p, at.elem.kind&kindDirectIface == 0, top, msg)
496                         return
497                 }
498                 for i := uintptr(0); i < at.len; i++ {
499                         cgoCheckArg(at.elem, p, true, top, msg)
500                         p = add(p, at.elem.size)
501                 }
502         case kindChan, kindMap:
503                 // These types contain internal pointers that will
504                 // always be allocated in the Go heap. It's never OK
505                 // to pass them to C.
506                 panic(errorString(msg))
507         case kindFunc:
508                 if indir {
509                         p = *(*unsafe.Pointer)(p)
510                 }
511                 if !cgoIsGoPointer(p) {
512                         return
513                 }
514                 panic(errorString(msg))
515         case kindInterface:
516                 it := *(**_type)(p)
517                 if it == nil {
518                         return
519                 }
520                 // A type known at compile time is OK since it's
521                 // constant. A type not known at compile time will be
522                 // in the heap and will not be OK.
523                 if inheap(uintptr(unsafe.Pointer(it))) {
524                         panic(errorString(msg))
525                 }
526                 p = *(*unsafe.Pointer)(add(p, sys.PtrSize))
527                 if !cgoIsGoPointer(p) {
528                         return
529                 }
530                 if !top {
531                         panic(errorString(msg))
532                 }
533                 cgoCheckArg(it, p, it.kind&kindDirectIface == 0, false, msg)
534         case kindSlice:
535                 st := (*slicetype)(unsafe.Pointer(t))
536                 s := (*slice)(p)
537                 p = s.array
538                 if p == nil || !cgoIsGoPointer(p) {
539                         return
540                 }
541                 if !top {
542                         panic(errorString(msg))
543                 }
544                 if st.elem.ptrdata == 0 {
545                         return
546                 }
547                 for i := 0; i < s.cap; i++ {
548                         cgoCheckArg(st.elem, p, true, false, msg)
549                         p = add(p, st.elem.size)
550                 }
551         case kindString:
552                 ss := (*stringStruct)(p)
553                 if !cgoIsGoPointer(ss.str) {
554                         return
555                 }
556                 if !top {
557                         panic(errorString(msg))
558                 }
559         case kindStruct:
560                 st := (*structtype)(unsafe.Pointer(t))
561                 if !indir {
562                         if len(st.fields) != 1 {
563                                 throw("can't happen")
564                         }
565                         cgoCheckArg(st.fields[0].typ, p, st.fields[0].typ.kind&kindDirectIface == 0, top, msg)
566                         return
567                 }
568                 for _, f := range st.fields {
569                         if f.typ.ptrdata == 0 {
570                                 continue
571                         }
572                         cgoCheckArg(f.typ, add(p, f.offset()), true, top, msg)
573                 }
574         case kindPtr, kindUnsafePointer:
575                 if indir {
576                         p = *(*unsafe.Pointer)(p)
577                         if p == nil {
578                                 return
579                         }
580                 }
581
582                 if !cgoIsGoPointer(p) {
583                         return
584                 }
585                 if !top {
586                         panic(errorString(msg))
587                 }
588
589                 cgoCheckUnknownPointer(p, msg)
590         }
591 }
592
593 // cgoCheckUnknownPointer is called for an arbitrary pointer into Go
594 // memory. It checks whether that Go memory contains any other
595 // pointer into Go memory. If it does, we panic.
596 // The return values are unused but useful to see in panic tracebacks.
597 func cgoCheckUnknownPointer(p unsafe.Pointer, msg string) (base, i uintptr) {
598         if inheap(uintptr(p)) {
599                 b, span, _ := findObject(uintptr(p), 0, 0)
600                 base = b
601                 if base == 0 {
602                         return
603                 }
604                 hbits := heapBitsForAddr(base)
605                 n := span.elemsize
606                 for i = uintptr(0); i < n; i += sys.PtrSize {
607                         if !hbits.morePointers() {
608                                 // No more possible pointers.
609                                 break
610                         }
611                         if hbits.isPointer() && cgoIsGoPointer(*(*unsafe.Pointer)(unsafe.Pointer(base + i))) {
612                                 panic(errorString(msg))
613                         }
614                         hbits = hbits.next()
615                 }
616
617                 return
618         }
619
620         for _, datap := range activeModules() {
621                 if cgoInRange(p, datap.data, datap.edata) || cgoInRange(p, datap.bss, datap.ebss) {
622                         // We have no way to know the size of the object.
623                         // We have to assume that it might contain a pointer.
624                         panic(errorString(msg))
625                 }
626                 // In the text or noptr sections, we know that the
627                 // pointer does not point to a Go pointer.
628         }
629
630         return
631 }
632
633 // cgoIsGoPointer reports whether the pointer is a Go pointer--a
634 // pointer to Go memory. We only care about Go memory that might
635 // contain pointers.
636 //go:nosplit
637 //go:nowritebarrierrec
638 func cgoIsGoPointer(p unsafe.Pointer) bool {
639         if p == nil {
640                 return false
641         }
642
643         if inHeapOrStack(uintptr(p)) {
644                 return true
645         }
646
647         for _, datap := range activeModules() {
648                 if cgoInRange(p, datap.data, datap.edata) || cgoInRange(p, datap.bss, datap.ebss) {
649                         return true
650                 }
651         }
652
653         return false
654 }
655
656 // cgoInRange reports whether p is between start and end.
657 //go:nosplit
658 //go:nowritebarrierrec
659 func cgoInRange(p unsafe.Pointer, start, end uintptr) bool {
660         return start <= uintptr(p) && uintptr(p) < end
661 }
662
663 // cgoCheckResult is called to check the result parameter of an
664 // exported Go function. It panics if the result is or contains a Go
665 // pointer.
666 func cgoCheckResult(val interface{}) {
667         if debug.cgocheck == 0 {
668                 return
669         }
670
671         ep := efaceOf(&val)
672         t := ep._type
673         cgoCheckArg(t, ep.data, t.kind&kindDirectIface == 0, false, cgoResultFail)
674 }