// Copyright 2026 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. //go:build goexperiment.cgobindings && (unix || windows) package cgo import ( "runtime" "strings" "sync" "testing" ) func systemLibPath() string { switch runtime.GOOS { case "darwin", "ios": return "/usr/lib/libSystem.B.dylib" case "windows": return "msvcrt.dll" case "freebsd": return "libc.so.7" case "dragonfly": return "libc.so.8" case "netbsd": return "libc.so.12" case "openbsd", "solaris", "illumos", "android": return "libc.so" case "aix": return "libc.a(shr_64.o)" default: return "libc.so.6" } } type testLib struct { Library } type badPtrLib struct { *Library } // symLib and missingLib stand in for //cgo:dynload types, whose symbols // code generated by cmd/cgo registers. type symLib struct { Library } type missingLib struct { Library } func init() { registerLibrary((*symLib)(nil), []string{"strlen", "strcmp"}) registerLibrary((*missingLib)(nil), []string{"strlen", "__cgo_nonexistent_sym_999"}) } // expectPanic calls f and checks that it panics with a string containing want. func expectPanic(t *testing.T, want string, f func()) { t.Helper() defer func() { t.Helper() if s, ok := recover().(string); !ok || !strings.Contains(s, want) { t.Errorf("panic = %q, want panic containing %q", s, want) } }() f() } func TestOpenErrors(t *testing.T) { if lib, err := Open[testLib](""); err == nil || lib != nil { t.Fatalf("Open(\"\") = %v, %v, want nil, error", lib, err) } if lib, err := Open[badPtrLib](systemLibPath()); err == nil || lib != nil || !strings.Contains(err.Error(), "embed cgo.Library by value") { t.Fatalf("Open[badPtrLib] = %v, %v, want nil, embed-by-value error", lib, err) } if lib, err := Open[testLib]("/nonexistent/does_not_exist_12345.so"); err == nil || lib != nil || !strings.HasPrefix(err.Error(), "cgo: dlopen: ") { t.Fatalf("Open(nonexistent) = %v, %v, want nil, cgo: dlopen error", lib, err) } // Open resolves all registered symbols up front, and fails if any of // them is missing. lib, err := Open[missingLib](systemLibPath()) if err == nil || lib != nil || !strings.Contains(err.Error(), "cannot resolve symbol __cgo_nonexistent_sym_999") { t.Fatalf("Open[missingLib] = %v, %v, want nil, cannot resolve symbol error", lib, err) } } func TestOpenSymbols(t *testing.T) { lib, err := Open[symLib](systemLibPath()) if err != nil { t.Fatalf("Open(%s): %v", systemLibPath(), err) } strlen := librarySymbol(&lib.Library, 0) strcmp := librarySymbol(&lib.Library, 1) if strlen == nil || strcmp == nil || strlen == strcmp { t.Fatalf("symbols = %p, %p, want distinct non-nil addresses", strlen, strcmp) } var wg sync.WaitGroup for range 16 { wg.Go(func() { if p := librarySymbol(&lib.Library, 0); p != strlen { t.Errorf("concurrent librarySymbol(0) = %p, want %p", p, strlen) } }) } wg.Wait() // A second instance of the same library has its own symbol table. lib2, err := Open[symLib](systemLibPath()) if err != nil { t.Fatalf("second Open(%s): %v", systemLibPath(), err) } if lib2 == lib || &lib2.syms[0] == &lib.syms[0] { t.Fatalf("second Open returned a shared instance") } if p := librarySymbol(&lib2.Library, 0); p != strlen { t.Fatalf("librarySymbol(lib2, 0) = %p, want %p", p, strlen) } expectPanic(t, "bad symbol index", func() { librarySymbol(&lib.Library, -1) }) expectPanic(t, "bad symbol index", func() { librarySymbol(&lib.Library, 2) }) // A type with no registered symbols can be opened, but has no symbols. unreg, err := Open[testLib](systemLibPath()) if err != nil { t.Fatalf("Open[testLib](%s): %v", systemLibPath(), err) } expectPanic(t, "bad symbol index", func() { librarySymbol(&unreg.Library, 0) }) // A Library that was not returned by Open is not usable. var zero symLib expectPanic(t, "not returned by cgo.Open", func() { librarySymbol(&zero.Library, 0) }) }