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