Source file src/runtime/cgo/library_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.cgobindings && (unix || windows)
     6  
     7  package cgo
     8  
     9  import (
    10  	"runtime"
    11  	"strings"
    12  	"sync"
    13  	"testing"
    14  )
    15  
    16  func systemLibPath() string {
    17  	switch runtime.GOOS {
    18  	case "darwin", "ios":
    19  		return "/usr/lib/libSystem.B.dylib"
    20  	case "windows":
    21  		return "msvcrt.dll"
    22  	case "freebsd":
    23  		return "libc.so.7"
    24  	case "dragonfly":
    25  		return "libc.so.8"
    26  	case "netbsd":
    27  		return "libc.so.12"
    28  	case "openbsd", "solaris", "illumos", "android":
    29  		return "libc.so"
    30  	case "aix":
    31  		return "libc.a(shr_64.o)"
    32  	default:
    33  		return "libc.so.6"
    34  	}
    35  }
    36  
    37  type testLib struct {
    38  	Library
    39  }
    40  
    41  type badPtrLib struct {
    42  	*Library
    43  }
    44  
    45  // symLib and missingLib stand in for //cgo:dynload types, whose symbols
    46  // code generated by cmd/cgo registers.
    47  type symLib struct {
    48  	Library
    49  }
    50  
    51  type missingLib struct {
    52  	Library
    53  }
    54  
    55  func init() {
    56  	registerLibrary((*symLib)(nil), []string{"strlen", "strcmp"})
    57  	registerLibrary((*missingLib)(nil), []string{"strlen", "__cgo_nonexistent_sym_999"})
    58  }
    59  
    60  // expectPanic calls f and checks that it panics with a string containing want.
    61  func expectPanic(t *testing.T, want string, f func()) {
    62  	t.Helper()
    63  	defer func() {
    64  		t.Helper()
    65  		if s, ok := recover().(string); !ok || !strings.Contains(s, want) {
    66  			t.Errorf("panic = %q, want panic containing %q", s, want)
    67  		}
    68  	}()
    69  	f()
    70  }
    71  
    72  func TestOpenErrors(t *testing.T) {
    73  	if lib, err := Open[testLib](""); err == nil || lib != nil {
    74  		t.Fatalf("Open(\"\") = %v, %v, want nil, error", lib, err)
    75  	}
    76  	if lib, err := Open[badPtrLib](systemLibPath()); err == nil || lib != nil || !strings.Contains(err.Error(), "embed cgo.Library by value") {
    77  		t.Fatalf("Open[badPtrLib] = %v, %v, want nil, embed-by-value error", lib, err)
    78  	}
    79  	if lib, err := Open[testLib]("/nonexistent/does_not_exist_12345.so"); err == nil || lib != nil || !strings.HasPrefix(err.Error(), "cgo: dlopen: ") {
    80  		t.Fatalf("Open(nonexistent) = %v, %v, want nil, cgo: dlopen error", lib, err)
    81  	}
    82  
    83  	// Open resolves all registered symbols up front, and fails if any of
    84  	// them is missing.
    85  	lib, err := Open[missingLib](systemLibPath())
    86  	if err == nil || lib != nil || !strings.Contains(err.Error(), "cannot resolve symbol __cgo_nonexistent_sym_999") {
    87  		t.Fatalf("Open[missingLib] = %v, %v, want nil, cannot resolve symbol error", lib, err)
    88  	}
    89  }
    90  
    91  func TestOpenSymbols(t *testing.T) {
    92  	lib, err := Open[symLib](systemLibPath())
    93  	if err != nil {
    94  		t.Fatalf("Open(%s): %v", systemLibPath(), err)
    95  	}
    96  	strlen := librarySymbol(&lib.Library, 0)
    97  	strcmp := librarySymbol(&lib.Library, 1)
    98  	if strlen == nil || strcmp == nil || strlen == strcmp {
    99  		t.Fatalf("symbols = %p, %p, want distinct non-nil addresses", strlen, strcmp)
   100  	}
   101  
   102  	var wg sync.WaitGroup
   103  	for range 16 {
   104  		wg.Go(func() {
   105  			if p := librarySymbol(&lib.Library, 0); p != strlen {
   106  				t.Errorf("concurrent librarySymbol(0) = %p, want %p", p, strlen)
   107  			}
   108  		})
   109  	}
   110  	wg.Wait()
   111  
   112  	// A second instance of the same library has its own symbol table.
   113  	lib2, err := Open[symLib](systemLibPath())
   114  	if err != nil {
   115  		t.Fatalf("second Open(%s): %v", systemLibPath(), err)
   116  	}
   117  	if lib2 == lib || &lib2.syms[0] == &lib.syms[0] {
   118  		t.Fatalf("second Open returned a shared instance")
   119  	}
   120  	if p := librarySymbol(&lib2.Library, 0); p != strlen {
   121  		t.Fatalf("librarySymbol(lib2, 0) = %p, want %p", p, strlen)
   122  	}
   123  
   124  	expectPanic(t, "bad symbol index", func() { librarySymbol(&lib.Library, -1) })
   125  	expectPanic(t, "bad symbol index", func() { librarySymbol(&lib.Library, 2) })
   126  
   127  	// A type with no registered symbols can be opened, but has no symbols.
   128  	unreg, err := Open[testLib](systemLibPath())
   129  	if err != nil {
   130  		t.Fatalf("Open[testLib](%s): %v", systemLibPath(), err)
   131  	}
   132  	expectPanic(t, "bad symbol index", func() { librarySymbol(&unreg.Library, 0) })
   133  
   134  	// A Library that was not returned by Open is not usable.
   135  	var zero symLib
   136  	expectPanic(t, "not returned by cgo.Open", func() { librarySymbol(&zero.Library, 0) })
   137  }
   138  

View as plain text