1 // Copyright 2017 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.
26 // C compiler with args (from $(go env CC) $(go env GOGCCFLAGS)).
32 var GOOS, GOARCH, GOROOT string
33 var installdir, androiddir string
36 func TestMain(m *testing.M) {
40 func testMain(m *testing.M) int {
41 log.SetFlags(log.Lshortfile)
43 if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
44 fmt.Printf("SKIP - short mode and $GO_BUILDER_NAME not set\n")
47 if runtime.GOOS == "linux" {
48 if _, err := os.Stat("/etc/alpine-release"); err == nil {
49 fmt.Printf("SKIP - skipping failing test on alpine - go.dev/issue/19938\n")
55 GOARCH = goEnv("GOARCH")
56 GOROOT = goEnv("GOROOT")
58 if _, err := os.Stat(GOROOT); os.IsNotExist(err) {
59 log.Fatalf("Unable able to find GOROOT at '%s'", GOROOT)
62 androiddir = fmt.Sprintf("/data/local/tmp/testcshared-%d", os.Getpid())
63 if runtime.GOOS != GOOS && GOOS == "android" {
64 args := append(adbCmd(), "exec-out", "mkdir", "-p", androiddir)
65 cmd := exec.Command(args[0], args[1:]...)
66 out, err := cmd.CombinedOutput()
68 log.Fatalf("setupAndroid failed: %v\n%s\n", err, out)
70 defer cleanupAndroid()
73 cc = []string{goEnv("CC")}
75 out := goEnv("GOGCCFLAGS")
82 if quote == '\000' && unicode.IsSpace(c) {
84 cc = append(cc, s[start:i])
92 if quote == '\000' && !backslash && (c == '"' || c == '\'') {
95 } else if !backslash && quote == c {
97 } else if (quote == '\000' || quote == '"') && !backslash && c == '\\' {
105 cc = append(cc, s[start:])
109 case "darwin", "ios":
111 // TODO(crawshaw): can we do better?
112 cc = append(cc, []string{"-framework", "CoreFoundation", "-framework", "Foundation"}...)
114 cc = append(cc, "-pie")
116 libgodir := GOOS + "_" + GOARCH
118 case "darwin", "ios":
119 if GOARCH == "arm64" {
120 libgodir += "_shared"
122 case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris", "illumos":
123 libgodir += "_shared"
125 cc = append(cc, "-I", filepath.Join("pkg", libgodir))
127 // Force reallocation (and avoid aliasing bugs) for parallel tests that append to cc.
128 cc = cc[:len(cc):len(cc)]
130 if GOOS == "windows" {
134 // Copy testdata into GOPATH/src/testcshared, along with a go.mod file
135 // declaring the same path.
137 GOPATH, err := os.MkdirTemp("", "cshared_test")
141 defer os.RemoveAll(GOPATH)
142 os.Setenv("GOPATH", GOPATH)
144 modRoot := filepath.Join(GOPATH, "src", "testcshared")
145 if err := overlayDir(modRoot, "testdata"); err != nil {
148 if err := os.Chdir(modRoot); err != nil {
151 os.Setenv("PWD", modRoot)
152 if err := os.WriteFile("go.mod", []byte("module testcshared\n"), 0666); err != nil {
157 if installdir != "" {
158 err := os.RemoveAll(installdir)
168 func goEnv(key string) string {
169 out, err := exec.Command("go", "env", key).Output()
171 log.Printf("go env %s failed:\n%s", key, err)
172 log.Panicf("%s", err.(*exec.ExitError).Stderr)
174 return strings.TrimSpace(string(out))
177 func cmdToRun(name string) string {
178 return "./" + name + exeSuffix
181 func adbCmd() []string {
182 cmd := []string{"adb"}
183 if flags := os.Getenv("GOANDROID_ADB_FLAGS"); flags != "" {
184 cmd = append(cmd, strings.Split(flags, " ")...)
189 func adbPush(t *testing.T, filename string) {
190 if runtime.GOOS == GOOS || GOOS != "android" {
193 args := append(adbCmd(), "push", filename, fmt.Sprintf("%s/%s", androiddir, filename))
194 cmd := exec.Command(args[0], args[1:]...)
195 if out, err := cmd.CombinedOutput(); err != nil {
196 t.Fatalf("adb command failed: %v\n%s\n", err, out)
200 func adbRun(t *testing.T, env []string, adbargs ...string) string {
201 if GOOS != "android" {
202 t.Fatalf("trying to run adb command when operating system is not android.")
204 args := append(adbCmd(), "exec-out")
205 // Propagate LD_LIBRARY_PATH to the adb shell invocation.
206 for _, e := range env {
207 if strings.Contains(e, "LD_LIBRARY_PATH=") {
208 adbargs = append([]string{e}, adbargs...)
212 shellcmd := fmt.Sprintf("cd %s; %s", androiddir, strings.Join(adbargs, " "))
213 args = append(args, shellcmd)
214 cmd := exec.Command(args[0], args[1:]...)
215 out, err := cmd.CombinedOutput()
217 t.Fatalf("adb command failed: %v\n%s\n", err, out)
219 return strings.Replace(string(out), "\r", "", -1)
222 func run(t *testing.T, extraEnv []string, args ...string) string {
224 cmd := exec.Command(args[0], args[1:]...)
225 if len(extraEnv) > 0 {
226 cmd.Env = append(os.Environ(), extraEnv...)
229 if GOOS != "windows" {
230 // TestUnexportedSymbols relies on file descriptor 30
231 // being closed when the program starts, so enforce
232 // that in all cases. (The first three descriptors are
233 // stdin/stdout/stderr, so we just need to make sure
234 // that cmd.ExtraFiles[27] exists and is nil.)
235 cmd.ExtraFiles = make([]*os.File, 28)
238 out, err := cmd.CombinedOutput()
240 t.Fatalf("command failed: %v\n%v\n%s\n", args, err, out)
242 t.Logf("run: %v", args)
247 func runExe(t *testing.T, extraEnv []string, args ...string) string {
249 if runtime.GOOS != GOOS && GOOS == "android" {
250 return adbRun(t, append(os.Environ(), extraEnv...), args...)
252 return run(t, extraEnv, args...)
255 func runCC(t *testing.T, args ...string) string {
257 // This function is run in parallel, so append to a copy of cc
258 // rather than cc itself.
259 return run(t, nil, append(append([]string(nil), cc...), args...)...)
262 func createHeaders() error {
263 // The 'cgo' command generates a number of additional artifacts,
264 // but we're only interested in the header.
265 // Shunt the rest of the outputs to a temporary directory.
266 objDir, err := os.MkdirTemp("", "testcshared_obj")
270 defer os.RemoveAll(objDir)
272 // Generate a C header file for p, which is a non-main dependency
273 // of main package libgo.
275 // TODO(golang.org/issue/35715): This should be simpler.
276 args := []string{"go", "tool", "cgo",
278 "-exportheader", "p.h",
279 filepath.Join(".", "p", "p.go")}
280 cmd := exec.Command(args[0], args[1:]...)
281 out, err := cmd.CombinedOutput()
283 return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
286 // Generate a C header file for libgo itself.
287 installdir, err = os.MkdirTemp("", "testcshared")
291 libgoname = "libgo.a"
293 args = []string{"go", "build", "-buildmode=c-shared", "-o", filepath.Join(installdir, libgoname), "./libgo"}
294 cmd = exec.Command(args[0], args[1:]...)
295 out, err = cmd.CombinedOutput()
297 return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
300 args = []string{"go", "build", "-buildmode=c-shared",
301 "-installsuffix", "testcshared",
303 filepath.Join(".", "libgo", "libgo.go")}
304 if GOOS == "windows" && strings.HasSuffix(args[6], ".a") {
305 args[6] = strings.TrimSuffix(args[6], ".a") + ".dll"
307 cmd = exec.Command(args[0], args[1:]...)
308 out, err = cmd.CombinedOutput()
310 return fmt.Errorf("command failed: %v\n%v\n%s\n", args, err, out)
312 if GOOS == "windows" {
313 // We can't simply pass -Wl,--out-implib, because this relies on having imports from multiple packages,
314 // which results in the linkers output implib getting overwritten at each step. So instead build the
315 // import library the traditional way, using a def file.
316 err = os.WriteFile("libgo.def",
317 []byte("LIBRARY libgo.dll\nEXPORTS\n\tDidInitRun\n\tDidMainRun\n\tDivu\n\tFromPkg\n\t_cgo_dummy_export\n"),
320 return fmt.Errorf("unable to write def file: %v", err)
322 out, err = exec.Command(cc[0], append(cc[1:], "-print-prog-name=dlltool")...).CombinedOutput()
324 return fmt.Errorf("unable to find dlltool path: %v\n%s\n", err, out)
326 dlltoolpath := strings.TrimSpace(string(out))
327 if filepath.Ext(dlltoolpath) == "" {
328 // Some compilers report slash-separated paths without extensions
329 // instead of ordinary Windows paths.
330 // Try to find the canonical name for the path.
331 if lp, err := exec.LookPath(dlltoolpath); err == nil {
336 args := []string{dlltoolpath, "-D", args[6], "-l", libgoname, "-d", "libgo.def"}
338 if filepath.Ext(dlltoolpath) == "" {
339 // This is an unfortunate workaround for
340 // https://github.com/mstorsjo/llvm-mingw/issues/205 in which
341 // we basically reimplement the contents of the dlltool.sh
342 // wrapper: https://git.io/JZFlU.
343 // TODO(thanm): remove this workaround once we can upgrade
344 // the compilers on the windows-arm64 builder.
345 dlltoolContents, err := os.ReadFile(args[0])
347 return fmt.Errorf("unable to read dlltool: %v\n", err)
349 if bytes.HasPrefix(dlltoolContents, []byte("#!/bin/sh")) && bytes.Contains(dlltoolContents, []byte("llvm-dlltool")) {
350 base, name := filepath.Split(args[0])
351 args[0] = filepath.Join(base, "llvm-dlltool")
353 switch prefix, _, _ := strings.Cut(name, "-"); prefix {
357 machine = "i386:x86-64"
363 if len(machine) > 0 {
364 args = append(args, "-m", machine)
369 out, err = exec.Command(args[0], args[1:]...).CombinedOutput()
371 return fmt.Errorf("unable to run dlltool to create import library: %v\n%s\n", err, out)
375 if runtime.GOOS != GOOS && GOOS == "android" {
376 args = append(adbCmd(), "push", libgoname, fmt.Sprintf("%s/%s", androiddir, libgoname))
377 cmd = exec.Command(args[0], args[1:]...)
378 out, err = cmd.CombinedOutput()
380 return fmt.Errorf("adb command failed: %v\n%s\n", err, out)
388 headersOnce sync.Once
392 func createHeadersOnce(t *testing.T) {
393 headersOnce.Do(func() {
394 headersErr = createHeaders()
396 if headersErr != nil {
402 func cleanupAndroid() {
403 if GOOS != "android" {
406 args := append(adbCmd(), "exec-out", "rm", "-rf", androiddir)
407 cmd := exec.Command(args[0], args[1:]...)
408 out, err := cmd.CombinedOutput()
410 log.Panicf("cleanupAndroid failed: %v\n%s\n", err, out)
414 // test0: exported symbols in shared lib are accessible.
415 func TestExportedSymbols(t *testing.T) {
423 runCC(t, "-I", installdir, "-o", cmd, "main0.c", libgoname)
428 out := runExe(t, []string{"LD_LIBRARY_PATH=."}, bin)
429 if strings.TrimSpace(out) != "PASS" {
434 func checkNumberOfExportedFunctionsWindows(t *testing.T, exportAllSymbols bool) {
454 tmpdir := t.TempDir()
456 srcfile := filepath.Join(tmpdir, "test.go")
457 objfile := filepath.Join(tmpdir, "test.dll")
458 if err := os.WriteFile(srcfile, []byte(prog), 0666); err != nil {
461 argv := []string{"build", "-buildmode=c-shared"}
462 if exportAllSymbols {
463 argv = append(argv, "-ldflags", "-extldflags=-Wl,--export-all-symbols")
465 argv = append(argv, "-o", objfile, srcfile)
466 out, err := exec.Command("go", argv...).CombinedOutput()
468 t.Fatalf("build failure: %s\n%s\n", err, string(out))
471 f, err := pe.Open(objfile)
473 t.Fatalf("pe.Open failed: %v", err)
476 section := f.Section(".edata")
478 t.Skip(".edata section is not present")
481 // TODO: deduplicate this struct from cmd/link/internal/ld/pe.go
482 type IMAGE_EXPORT_DIRECTORY struct {
486 NumberOfFunctions uint32
490 var e IMAGE_EXPORT_DIRECTORY
491 if err := binary.Read(section.Open(), binary.LittleEndian, &e); err != nil {
492 t.Fatalf("binary.Read failed: %v", err)
495 // Only the two exported functions and _cgo_dummy_export should be exported
496 expectedNumber := uint32(3)
498 if exportAllSymbols {
499 if e.NumberOfFunctions <= expectedNumber {
500 t.Fatalf("missing exported functions: %v", e.NumberOfFunctions)
502 if e.NumberOfNames <= expectedNumber {
503 t.Fatalf("missing exported names: %v", e.NumberOfNames)
506 if e.NumberOfFunctions != expectedNumber {
507 t.Fatalf("got %d exported functions; want %d", e.NumberOfFunctions, expectedNumber)
509 if e.NumberOfNames != expectedNumber {
510 t.Fatalf("got %d exported names; want %d", e.NumberOfNames, expectedNumber)
515 func TestNumberOfExportedFunctions(t *testing.T) {
516 if GOOS != "windows" {
517 t.Skip("skipping windows only test")
521 t.Run("OnlyExported", func(t *testing.T) {
522 checkNumberOfExportedFunctionsWindows(t, false)
524 t.Run("All", func(t *testing.T) {
525 checkNumberOfExportedFunctionsWindows(t, true)
529 // test1: shared library can be dynamically loaded and exported symbols are accessible.
530 func TestExportedSymbolsWithDynamicLoad(t *testing.T) {
533 if GOOS == "windows" {
534 t.Logf("Skipping on %s", GOOS)
543 if GOOS != "freebsd" {
544 runCC(t, "-o", cmd, "main1.c", "-ldl")
546 runCC(t, "-o", cmd, "main1.c")
552 out := runExe(t, nil, bin, "./"+libgoname)
553 if strings.TrimSpace(out) != "PASS" {
558 // test2: tests libgo2 which does not export any functions.
559 func TestUnexportedSymbols(t *testing.T) {
562 if GOOS == "windows" {
563 t.Logf("Skipping on %s", GOOS)
569 libname := "libgo2.a"
574 "-buildmode=c-shared",
575 "-installsuffix", "testcshared",
576 "-o", libname, "./libgo2",
580 linkFlags := "-Wl,--no-as-needed"
581 if GOOS == "darwin" || GOOS == "ios" {
585 runCC(t, "-o", cmd, "main2.c", linkFlags, libname)
588 defer os.Remove(libname)
591 out := runExe(t, []string{"LD_LIBRARY_PATH=."}, bin)
593 if strings.TrimSpace(out) != "PASS" {
598 // test3: tests main.main is exported on android.
599 func TestMainExportedOnAndroid(t *testing.T) {
606 t.Logf("Skipping on %s", GOOS)
615 runCC(t, "-o", cmd, "main3.c", "-ldl")
620 out := runExe(t, nil, bin, "./"+libgoname)
621 if strings.TrimSpace(out) != "PASS" {
626 func testSignalHandlers(t *testing.T, pkgname, cfile, cmd string) {
627 libname := pkgname + ".a"
631 "-buildmode=c-shared",
632 "-installsuffix", "testcshared",
633 "-o", libname, pkgname,
636 if GOOS != "freebsd" {
637 runCC(t, "-pthread", "-o", cmd, cfile, "-ldl")
639 runCC(t, "-pthread", "-o", cmd, cfile)
645 defer os.Remove(libname)
647 defer os.Remove(pkgname + ".h")
649 out := runExe(t, nil, bin, "./"+libname)
650 if strings.TrimSpace(out) != "PASS" {
651 t.Error(run(t, nil, bin, libname, "verbose"))
655 // test4: test signal handlers
656 func TestSignalHandlers(t *testing.T) {
658 if GOOS == "windows" {
659 t.Logf("Skipping on %s", GOOS)
662 testSignalHandlers(t, "./libgo4", "main4.c", "testp4")
665 // test5: test signal handlers with os/signal.Notify
666 func TestSignalHandlersWithNotify(t *testing.T) {
668 if GOOS == "windows" {
669 t.Logf("Skipping on %s", GOOS)
672 testSignalHandlers(t, "./libgo5", "main5.c", "testp5")
675 func TestPIE(t *testing.T) {
679 case "linux", "android":
682 t.Logf("Skipping on %s", GOOS)
688 f, err := elf.Open(libgoname)
690 t.Fatalf("elf.Open failed: %v", err)
694 ds := f.SectionByType(elf.SHT_DYNAMIC)
696 t.Fatalf("no SHT_DYNAMIC section")
700 t.Fatalf("can't read SHT_DYNAMIC contents: %v", err)
706 tag = elf.DynTag(f.ByteOrder.Uint32(d[:4]))
709 tag = elf.DynTag(f.ByteOrder.Uint64(d[:8]))
712 if tag == elf.DT_TEXTREL {
713 t.Fatalf("%s has DT_TEXTREL flag", libgoname)
718 // Test that installing a second time recreates the header file.
719 func TestCachedInstall(t *testing.T) {
720 tmpdir, err := os.MkdirTemp("", "cshared")
724 defer os.RemoveAll(tmpdir)
726 copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "go.mod"), "go.mod")
727 copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "libgo", "libgo.go"), filepath.Join("libgo", "libgo.go"))
728 copyFile(t, filepath.Join(tmpdir, "src", "testcshared", "p", "p.go"), filepath.Join("p", "p.go"))
730 buildcmd := []string{"go", "install", "-x", "-buildmode=c-shared", "-installsuffix", "testcshared", "./libgo"}
732 cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
733 cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
734 env := append(cmd.Environ(),
736 "GOBIN="+filepath.Join(tmpdir, "bin"),
737 "GO111MODULE=off", // 'go install' only works in GOPATH mode
741 out, err := cmd.CombinedOutput()
747 var libgoh, ph string
749 walker := func(path string, info os.FileInfo, err error) error {
754 switch filepath.Base(path) {
762 t.Fatalf("%s found again", *ps)
769 if err := filepath.Walk(tmpdir, walker); err != nil {
774 t.Fatal("libgo.h not installed")
777 if err := os.Remove(libgoh); err != nil {
781 cmd = exec.Command(buildcmd[0], buildcmd[1:]...)
782 cmd.Dir = filepath.Join(tmpdir, "src", "testcshared")
785 out, err = cmd.CombinedOutput()
791 if _, err := os.Stat(libgoh); err != nil {
792 t.Errorf("libgo.h not installed in second run: %v", err)
796 // copyFile copies src to dst.
797 func copyFile(t *testing.T, dst, src string) {
799 data, err := os.ReadFile(src)
803 if err := os.MkdirAll(filepath.Dir(dst), 0777); err != nil {
806 if err := os.WriteFile(dst, data, 0666); err != nil {
811 func TestGo2C2Go(t *testing.T) {
813 case "darwin", "ios", "windows":
814 // Non-ELF shared libraries don't support the multiple
815 // copies of the runtime package implied by this test.
816 t.Skipf("linking c-shared into Go programs not supported on %s; issue 29061, 49457", GOOS)
818 t.Skip("test fails on android; issue 29087")
823 tmpdir, err := os.MkdirTemp("", "cshared-TestGo2C2Go")
827 defer os.RemoveAll(tmpdir)
829 lib := filepath.Join(tmpdir, "libtestgo2c2go.a")
831 if GOOS == "windows" && strings.HasSuffix(lib, ".a") {
832 env = append(env, "CGO_LDFLAGS=-Wl,--out-implib,"+lib, "CGO_LDFLAGS_ALLOW=.*")
833 lib = strings.TrimSuffix(lib, ".a") + ".dll"
835 run(t, env, "go", "build", "-buildmode=c-shared", "-o", lib, "./go2c2go/go")
837 cgoCflags := os.Getenv("CGO_CFLAGS")
841 cgoCflags += "-I" + tmpdir
843 cgoLdflags := os.Getenv("CGO_LDFLAGS")
844 if cgoLdflags != "" {
847 cgoLdflags += "-L" + tmpdir + " -ltestgo2c2go"
849 goenv := []string{"CGO_CFLAGS=" + cgoCflags, "CGO_LDFLAGS=" + cgoLdflags}
851 ldLibPath := os.Getenv("LD_LIBRARY_PATH")
857 runenv := []string{"LD_LIBRARY_PATH=" + ldLibPath}
859 bin := filepath.Join(tmpdir, "m1") + exeSuffix
860 run(t, goenv, "go", "build", "-o", bin, "./go2c2go/m1")
861 runExe(t, runenv, bin)
863 bin = filepath.Join(tmpdir, "m2") + exeSuffix
864 run(t, goenv, "go", "build", "-o", bin, "./go2c2go/m2")
865 runExe(t, runenv, bin)
868 func TestIssue36233(t *testing.T) {
871 // Test that the export header uses GoComplex64 and GoComplex128
872 // for complex types.
874 tmpdir, err := os.MkdirTemp("", "cshared-TestIssue36233")
878 defer os.RemoveAll(tmpdir)
880 const exportHeader = "issue36233.h"
882 run(t, nil, "go", "tool", "cgo", "-exportheader", exportHeader, "-objdir", tmpdir, "./issue36233/issue36233.go")
883 data, err := os.ReadFile(exportHeader)
888 funcs := []struct{ name, signature string }{
889 {"exportComplex64", "GoComplex64 exportComplex64(GoComplex64 v)"},
890 {"exportComplex128", "GoComplex128 exportComplex128(GoComplex128 v)"},
891 {"exportComplexfloat", "GoComplex64 exportComplexfloat(GoComplex64 v)"},
892 {"exportComplexdouble", "GoComplex128 exportComplexdouble(GoComplex128 v)"},
895 scanner := bufio.NewScanner(bytes.NewReader(data))
899 for _, fn := range funcs {
900 if bytes.Contains(b, []byte(fn.name)) {
902 if !bytes.Contains(b, []byte(fn.signature)) {
903 t.Errorf("function signature mismatch; got %q, want %q", b, fn.signature)
908 if err = scanner.Err(); err != nil {
909 t.Errorf("scanner encountered error: %v", err)
911 if found != len(funcs) {
912 t.Error("missing functions")