Source file src/runtime/cgo/trampoline_test.go

     1  // Copyright 2026 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  //go:build goexperiment.runtimecgopurego && !msan && !tsan && ((unix && !arm && !mips && !mipsle && !ppc64 && !ppc64le) || (windows && !386))
     6  
     7  package cgo
     8  
     9  import (
    10  	"internal/abi"
    11  	"runtime"
    12  	"testing"
    13  	"unsafe"
    14  )
    15  
    16  func trampolineCallers()
    17  
    18  func TestTrampolineCallers(t *testing.T) {
    19  	var pcs [16]uintptr
    20  	var pin runtime.Pinner
    21  	pin.Pin(&pcs[0])
    22  	defer pin.Unpin()
    23  	n := asmcgocall(abi.FuncPCABI0(trampolineCallers), uintptr(unsafe.Pointer(&pcs[0])), uintptr(len(pcs)), abi.FuncPCABIInternal(runtime.Callers), 0, 0, 0)
    24  	caller := "runtime.asmcgocall"
    25  	if runtime.GOOS == "windows" && runtime.GOARCH == "amd64" {
    26  		// The Windows landing pad calls the trampoline instead of tail-calling it.
    27  		caller = "runtime.asmcgocall_landingpad"
    28  	}
    29  	var names []string
    30  	for _, pc := range pcs[:n] {
    31  		name := runtime.FuncForPC(pc - 1).Name()
    32  		if len(names) != 0 && names[len(names)-1] == "runtime/cgo.cgoCallTrampoline" && name == caller {
    33  			return
    34  		}
    35  		names = append(names, name)
    36  	}
    37  	t.Fatalf("missing trampoline to %s transition in %v", caller, names)
    38  }
    39  

View as plain text