# Test shared libraries loaded at run time with //cgo:dynload types and # hand-written bindings. env GOEXPERIMENT=$GOEXPERIMENT,runtimecgopurego,cgobindings [!GOEXPERIMENT:cgobindings] skip [!GOOS:darwin] [!GOOS:linux] skip [!GOARCH:amd64] [!GOARCH:arm64] skip [!exec:cc] skip mkdir lib cc -fPIC -shared -o lib/libmul.so c/mul.c [GOOS:linux] cc -fPIC -shared -o lib/libadd.so c/add.c -ldl [!GOOS:linux] cc -fPIC -shared -o lib/libadd.so c/add.c cd dyn env CGO_ENABLED=bindings env CC=/bin/false env LIBMUL=$PWD/../lib/libmul.so env LIBADD=$PWD/../lib/libadd.so go run . stdout '^mul: compute=42 counter=7 name=mul$' stdout '^add: compute=13 counter=7 name=add$' stdout '^mul: counter=8$' stdout '^add: counter=7$' stdout '^local: ok$' stdout '^missing: ok$' stdout '^init: ok$' stdout '^directives: ok$' stdout '^concurrent: ok$' [race] go run -race . [race] stdout '^concurrent: ok$' -- c/mul.c -- int Counter = 7; int Compute(int a, int b) { return a * b; } int MulOnlySym(void) { return 123; } void Bump(void) { Counter++; } -- c/add.c -- #define _GNU_SOURCE #include int Counter = 7; int Compute(int a, int b) { return a + b; } int GlobalSeesMulOnly(void) { return dlsym(RTLD_DEFAULT, "MulOnlySym") != 0; } void Bump(void) { Counter++; } -- dyn/go.mod -- module dyn go 1.28 -- dyn/a_init.go -- package main import ( "os" "runtime/cgo" ) // pkgCalc is opened while initializing package variables, and initCalc by // an init function in a file that is compiled before the code generated by // cmd/cgo. Open must resolve their symbols in both cases. var pkgCalc, pkgErr = cgo.Open[Calc](os.Getenv("LIBMUL")) var initCalc *Calc func init() { var err error initCalc, err = cgo.Open[Calc](os.Getenv("LIBMUL")) if err != nil { panic(err) } } -- dyn/b_directives.go -- package main import _ "embed" // The first declarations of a file keep their directives. //go:embed data.txt var data string //go:noinline func dataLen() int { return len(data) } -- dyn/c_noimports.go -- package main //go:noinline func two() int { return 2 } -- dyn/data.txt -- hello -- dyn/lib.go -- package main import "C" import "runtime/cgo" //cgo:dynload Calc //cgo:func Calc.Compute Compute //cgo:func Calc.Bump Bump //cgo:var Calc.Counter Counter // Calc is the API shared by libmul.so and libadd.so. type Calc struct { cgo.Library name string } func (*Calc) Compute(a, b int32) int32 func (*Calc) Bump() func (*Calc) Counter() *int32 //cgo:dynload MulOnly //cgo:func MulOnly.MulOnlySym MulOnlySym // MulOnly is the API only provided by libmul.so. type MulOnly struct { cgo.Library } func (*MulOnly) MulOnlySym() int32 //cgo:dynload AddOnly //cgo:func AddOnly.GlobalSeesMulOnly GlobalSeesMulOnly // AddOnly is the API only provided by libadd.so. type AddOnly struct { cgo.Library } func (*AddOnly) GlobalSeesMulOnly() int32 -- dyn/main.go -- package main import ( "fmt" "os" "runtime/cgo" "strings" "sync" ) func open(path, name string) *Calc { c, err := cgo.Open[Calc](path) if err != nil { panic(err) } c.name = name return c } func main() { mul := open(os.Getenv("LIBMUL"), "mul") add := open(os.Getenv("LIBADD"), "add") for _, c := range []*Calc{mul, add} { fmt.Printf("%s: compute=%d counter=%d name=%s\n", c.name, c.Compute(6, 7), *c.Counter(), c.name) } // Each instance refers to its own library's variables. mul.Bump() fmt.Printf("mul: counter=%d\n", *mul.Counter()) fmt.Printf("add: counter=%d\n", *add.Counter()) // Libraries are loaded locally (RTLD_LOCAL), so a symbol exported by // libmul.so must not leak into the global symbol namespace seen by // libadd.so via dlsym(RTLD_DEFAULT, ...). mulOnly, err := cgo.Open[MulOnly](os.Getenv("LIBMUL")) if err != nil { panic(err) } if got := mulOnly.MulOnlySym(); got != 123 { fmt.Printf("MulOnlySym() = %d, want 123\n", got) os.Exit(1) } addOnly, err := cgo.Open[AddOnly](os.Getenv("LIBADD")) if err != nil { panic(err) } if got := addOnly.GlobalSeesMulOnly(); got != 0 { fmt.Printf("GlobalSeesMulOnly() = %d, want 0 (RTLD_LOCAL violated)\n", got) os.Exit(1) } fmt.Println("local: ok") // Open resolves all symbols when it loads the library, so it fails if // one of them is missing. if l, err := cgo.Open[MulOnly](os.Getenv("LIBADD")); err == nil || !strings.Contains(err.Error(), "cannot resolve symbol MulOnlySym") { fmt.Printf("Open[MulOnly](libadd.so) = %v, %v, want cannot resolve symbol MulOnlySym error\n", l, err) os.Exit(1) } fmt.Println("missing: ok") if pkgErr != nil || pkgCalc.Compute(2, 3) != 6 || initCalc.Compute(2, 3) != 6 { fmt.Printf("Calc opened during initialization: %v\n", pkgErr) os.Exit(1) } fmt.Println("init: ok") if dataLen() != len("hello\n") || two() != 2 { fmt.Printf("dataLen() = %d, two() = %d\n", dataLen(), two()) os.Exit(1) } fmt.Println("directives: ok") var wg sync.WaitGroup errs := make(chan string, 64) for i := range 32 { wg.Add(1) go func() { defer wg.Done() c, want := mul, int32(i*3) if i%2 == 1 { c, want = add, int32(i+3) } if got := c.Compute(int32(i), 3); got != want { errs <- fmt.Sprintf("%s.Compute(%d, 3) = %d, want %d", c.name, i, got, want) } }() } wg.Wait() close(errs) for e := range errs { fmt.Println(e) os.Exit(1) } fmt.Println("concurrent: ok") }