Text file src/cmd/go/testdata/script/cgo_noescape.txt

     1  # Test #cgo noescape and #cgo nocallback directives with Go-syntax bindings.
     2  env GOEXPERIMENT=$GOEXPERIMENT,runtimecgopurego,cgobindings
     3  [!GOEXPERIMENT:cgobindings] skip
     4  [!GOOS:darwin] [!GOOS:linux] skip
     5  [!GOARCH:amd64] [!GOARCH:arm64] skip
     6  [!exec:cc] skip
     7  
     8  cd p
     9  cc -fPIC -shared -o libtest.so ../c/lib.c
    10  
    11  env CGO_ENABLED=bindings
    12  env CC=/bin/false
    13  go build -gcflags=-m -o app.exe .
    14  stderr 'noesc does not escape'
    15  stderr 'leaking param: esc'
    16  
    17  [GOOS:linux] env LD_LIBRARY_PATH=.
    18  exec ./app.exe
    19  stdout 'Add=15'
    20  stdout 'Esc=25'
    21  
    22  ! exec ./app.exe cb
    23  stderr 'runtime: function marked with #cgo nocallback called back into Go'
    24  cd ..
    25  
    26  cd errpkg
    27  ! go build -o app_err.exe .
    28  stderr '#cgo noescape NoSuchFunc: no matched C function'
    29  stderr '#cgo nocallback AnotherNoSuchFunc: no matched C function'
    30  
    31  -- c/lib.c --
    32  int Add(int *p, int b) { return *p + b; }
    33  int Esc(int *p, int b) { return *p + b; }
    34  typedef int (*cb_fn)(int, int);
    35  int CallMeBack(cb_fn cb, int a, int b) { return cb(a, b); }
    36  -- p/go.mod --
    37  module p
    38  go 1.28
    39  -- p/binding.go --
    40  package main
    41  
    42  // #cgo LDFLAGS: ./libtest.so
    43  // #cgo noescape Add
    44  // #cgo nocallback Add
    45  // #cgo nocallback CallMeBack
    46  import "C"
    47  
    48  //cgo:type cb_fn C.cb_fn
    49  //cgo:func Add C.Add "libtest.so"
    50  //cgo:func Esc C.Esc "libtest.so"
    51  //cgo:func CallMeBack C.CallMeBack "libtest.so"
    52  
    53  type cb_fn *[0]byte
    54  
    55  func Add(p *int32, b int32) int32
    56  func Esc(p *int32, b int32) int32
    57  func CallMeBack(cb cb_fn, a, b int32) int32
    58  -- p/main.go --
    59  package main
    60  
    61  import (
    62  	"fmt"
    63  	"os"
    64  )
    65  
    66  import "C"
    67  
    68  //export GoCallback
    69  func GoCallback(a, b int32) int32 { return a + b }
    70  
    71  func testNoEscape(noesc *int32) int32 { return C.Add(noesc, 10) }
    72  func testEscape(esc *int32) int32     { return C.Esc(esc, 10) }
    73  
    74  func main() {
    75  	if len(os.Args) > 1 && os.Args[1] == "cb" {
    76  		C.CallMeBack(C.cb_fn(C.GoCallback), 10, 20)
    77  		return
    78  	}
    79  	var v1, v2 int32 = 5, 15
    80  	fmt.Printf("Add=%d\n", testNoEscape(&v1))
    81  	fmt.Printf("Esc=%d\n", testEscape(&v2))
    82  }
    83  -- errpkg/go.mod --
    84  module errpkg
    85  go 1.28
    86  -- errpkg/binding.go --
    87  package main
    88  
    89  // #cgo LDFLAGS: ./libtest.so
    90  import "C"
    91  
    92  //cgo:func Add C.Add "libtest.so"
    93  func Add(p *int32, b int32) int32
    94  -- errpkg/main.go --
    95  package main
    96  
    97  // #cgo noescape NoSuchFunc
    98  // #cgo nocallback AnotherNoSuchFunc
    99  import "C"
   100  
   101  func main() {}
   102  

View as plain text