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