1 // Copyright 2016 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.
32 // C compiler with args (from $(go env CC) $(go env GOGCCFLAGS)).
38 var GOOS, GOARCH, GOPATH string
41 var testWork bool // If true, preserve temporary directories.
43 func TestMain(m *testing.M) {
44 flag.BoolVar(&testWork, "testwork", false, "if true, log and preserve the test's temporary working directory")
46 if testing.Short() && os.Getenv("GO_BUILDER_NAME") == "" {
47 fmt.Printf("SKIP - short mode and $GO_BUILDER_NAME not set\n")
50 if runtime.GOOS == "linux" {
51 if _, err := os.Stat("/etc/alpine-release"); err == nil {
52 fmt.Printf("SKIP - skipping failing test on alpine - go.dev/issue/19938\n")
57 log.SetFlags(log.Lshortfile)
61 func testMain(m *testing.M) int {
62 // We need a writable GOPATH in which to run the tests.
63 // Construct one in a temporary directory.
65 GOPATH, err = os.MkdirTemp("", "carchive_test")
72 defer os.RemoveAll(GOPATH)
74 os.Setenv("GOPATH", GOPATH)
76 // Copy testdata into GOPATH/src/testarchive, along with a go.mod file
77 // declaring the same path.
78 modRoot := filepath.Join(GOPATH, "src", "testcarchive")
79 if err := overlayDir(modRoot, "testdata"); err != nil {
82 if err := os.Chdir(modRoot); err != nil {
85 os.Setenv("PWD", modRoot)
86 if err := os.WriteFile("go.mod", []byte("module testcarchive\n"), 0666); err != nil {
91 GOARCH = goEnv("GOARCH")
92 bin = cmdToRun("./testp")
95 cc = []string{string(ccOut)}
97 out := goEnv("GOGCCFLAGS")
103 for i, c := range s {
104 if quote == '\000' && unicode.IsSpace(c) {
106 cc = append(cc, s[start:i])
114 if quote == '\000' && !backslash && (c == '"' || c == '\'') {
117 } else if !backslash && quote == c {
119 } else if (quote == '\000' || quote == '"') && !backslash && c == '\\' {
127 cc = append(cc, s[start:])
131 // -Wl,-bnoobjreorder is mandatory to keep the same layout
133 cc = append(cc, "-Wl,-bnoobjreorder")
135 libbase := GOOS + "_" + GOARCH
136 if runtime.Compiler == "gccgo" {
137 libbase = "gccgo_" + libgodir + "_fPIC"
140 case "darwin", "ios":
141 if GOARCH == "arm64" {
144 case "dragonfly", "freebsd", "linux", "netbsd", "openbsd", "solaris", "illumos":
148 libgodir = filepath.Join(GOPATH, "pkg", libbase, "testcarchive")
149 cc = append(cc, "-I", libgodir)
151 // Force reallocation (and avoid aliasing bugs) for parallel tests that append to cc.
152 cc = cc[:len(cc):len(cc)]
154 if GOOS == "windows" {
161 func goEnv(key string) string {
162 out, err := exec.Command("go", "env", key).Output()
164 if ee, ok := err.(*exec.ExitError); ok {
165 fmt.Fprintf(os.Stderr, "%s", ee.Stderr)
167 log.Panicf("go env %s failed:\n%s\n", key, err)
169 return strings.TrimSpace(string(out))
172 func cmdToRun(name string) []string {
173 execScript := "go_" + goEnv("GOOS") + "_" + goEnv("GOARCH") + "_exec"
174 executor, err := exec.LookPath(execScript)
176 return []string{name}
178 return []string{executor, name}
181 // genHeader writes a C header file for the C-exported declarations found in .go
182 // source files in dir.
184 // TODO(golang.org/issue/35715): This should be simpler.
185 func genHeader(t *testing.T, header, dir string) {
188 // The 'cgo' command generates a number of additional artifacts,
189 // but we're only interested in the header.
190 // Shunt the rest of the outputs to a temporary directory.
191 objDir, err := os.MkdirTemp(GOPATH, "_obj")
195 defer os.RemoveAll(objDir)
197 files, err := filepath.Glob(filepath.Join(dir, "*.go"))
202 cmd := exec.Command("go", "tool", "cgo",
204 "-exportheader", header)
205 cmd.Args = append(cmd.Args, files...)
207 if out, err := cmd.CombinedOutput(); err != nil {
213 func testInstall(t *testing.T, exe, libgoa, libgoh string, buildcmd ...string) {
215 cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
216 cmd.Env = append(cmd.Environ(), "GO111MODULE=off") // 'go install' only works in GOPATH mode
218 if out, err := cmd.CombinedOutput(); err != nil {
229 ccArgs := append(cc, "-o", exe, "main.c")
230 if GOOS == "windows" {
231 ccArgs = append(ccArgs, "main_windows.c", libgoa, "-lntdll", "-lws2_32", "-lwinmm")
233 ccArgs = append(ccArgs, "main_unix.c", libgoa)
235 if runtime.Compiler == "gccgo" {
236 ccArgs = append(ccArgs, "-lgo")
239 if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
247 binArgs := append(cmdToRun(exe), "arg1", "arg2")
248 cmd = exec.Command(binArgs[0], binArgs[1:]...)
249 if runtime.Compiler == "gccgo" {
250 cmd.Env = append(cmd.Environ(), "GCCGO=1")
252 if out, err := cmd.CombinedOutput(); err != nil {
257 checkLineComments(t, libgoh)
260 var badLineRegexp = regexp.MustCompile(`(?m)^#line [0-9]+ "/.*$`)
262 // checkLineComments checks that the export header generated by
263 // -buildmode=c-archive doesn't have any absolute paths in the #line
264 // comments. We don't want those paths because they are unhelpful for
265 // the user and make the files change based on details of the location
267 func checkLineComments(t *testing.T, hdrname string) {
268 hdr, err := os.ReadFile(hdrname)
270 if !os.IsNotExist(err) {
275 if line := badLineRegexp.Find(hdr); line != nil {
276 t.Errorf("bad #line directive with absolute path in %s: %q", hdrname, line)
280 // checkArchive verifies that the created library looks OK.
281 // We just check a couple of things now, we can add more checks as needed.
282 func checkArchive(t *testing.T, arname string) {
286 case "aix", "darwin", "ios", "windows":
287 // We don't have any checks for non-ELF libraries yet.
288 if _, err := os.Stat(arname); err != nil {
289 t.Errorf("archive %s does not exist: %v", arname, err)
292 checkELFArchive(t, arname)
296 // checkELFArchive checks an ELF archive.
297 func checkELFArchive(t *testing.T, arname string) {
300 f, err := os.Open(arname)
302 t.Errorf("archive %s does not exist: %v", arname, err)
307 // TODO(iant): put these in a shared package? But where?
319 hdrlen = namelen + datelen + uidlen + gidlen + modelen + sizelen + fmaglen
332 var magbuf [len(magic)]byte
333 if _, err := io.ReadFull(f, magbuf[:]); err != nil {
334 t.Errorf("%s: archive too short", arname)
337 if string(magbuf[:]) != magic {
338 t.Errorf("%s: incorrect archive magic string %q", arname, magbuf)
341 off := int64(len(magic))
345 if _, err := f.Read(b[:]); err != nil {
349 t.Errorf("%s: error skipping alignment byte at %d: %v", arname, off, err)
354 var hdrbuf [hdrlen]byte
355 if _, err := io.ReadFull(f, hdrbuf[:]); err != nil {
359 t.Errorf("%s: error reading archive header at %d: %v", arname, off, err)
364 hdrslice := hdrbuf[:]
365 set := func(len int, ps *string) {
366 *ps = string(bytes.TrimSpace(hdrslice[:len]))
367 hdrslice = hdrslice[len:]
369 set(namelen, &hdr.name)
370 set(datelen, &hdr.date)
371 set(uidlen, &hdr.uid)
372 set(gidlen, &hdr.gid)
373 set(modelen, &hdr.mode)
374 set(sizelen, &hdr.size)
375 hdr.fmag = string(hdrslice[:fmaglen])
376 hdrslice = hdrslice[fmaglen:]
377 if len(hdrslice) != 0 {
378 t.Fatalf("internal error: len(hdrslice) == %d", len(hdrslice))
381 if hdr.fmag != fmag {
382 t.Errorf("%s: invalid fmagic value %q at %d", arname, hdr.fmag, off)
386 size, err := strconv.ParseInt(hdr.size, 10, 64)
388 t.Errorf("%s: error parsing size %q at %d: %v", arname, hdr.size, off, err)
395 case "__.SYMDEF", "/", "/SYM64/":
396 // The archive symbol map.
397 case "//", "ARFILENAMES/":
398 // The extended name table.
400 // This should be an ELF object.
401 checkELFArchiveObject(t, arname, off, io.NewSectionReader(f, off, size))
405 if _, err := f.Seek(off, os.SEEK_SET); err != nil {
406 t.Errorf("%s: failed to seek to %d: %v", arname, off, err)
411 // checkELFArchiveObject checks an object in an ELF archive.
412 func checkELFArchiveObject(t *testing.T, arname string, off int64, obj io.ReaderAt) {
415 ef, err := elf.NewFile(obj)
417 t.Errorf("%s: failed to open ELF file at %d: %v", arname, off, err)
422 // Verify section types.
423 for _, sec := range ef.Sections {
426 case ".text", ".data":
427 want = elf.SHT_PROGBITS
429 want = elf.SHT_NOBITS
431 want = elf.SHT_SYMTAB
433 want = elf.SHT_STRTAB
435 want = elf.SHT_INIT_ARRAY
437 want = elf.SHT_FINI_ARRAY
438 case ".preinit_array":
439 want = elf.SHT_PREINIT_ARRAY
441 if want != elf.SHT_NULL && sec.Type != want {
442 t.Errorf("%s: incorrect section type in elf file at %d for section %q: got %v want %v", arname, off, sec.Name, sec.Type, want)
447 func TestInstall(t *testing.T) {
449 defer os.RemoveAll(filepath.Join(GOPATH, "pkg"))
453 if runtime.Compiler == "gccgo" {
454 libgoa = "liblibgo.a"
457 // Generate the p.h header file.
459 // 'go install -i -buildmode=c-archive ./libgo' would do that too, but that
460 // would also attempt to install transitive standard-library dependencies to
461 // GOROOT, and we cannot assume that GOROOT is writable. (A non-root user may
462 // be running this test in a GOROOT owned by root.)
463 genHeader(t, "p.h", "./p")
465 testInstall(t, "./testp1"+exeSuffix,
466 filepath.Join(libgodir, libgoa),
467 filepath.Join(libgodir, "libgo.h"),
468 "go", "install", "-buildmode=c-archive", "./libgo")
470 // Test building libgo other than installing it.
471 // Header files are now present.
472 testInstall(t, "./testp2"+exeSuffix, "libgo.a", "libgo.h",
473 "go", "build", "-buildmode=c-archive", filepath.Join(".", "libgo", "libgo.go"))
475 testInstall(t, "./testp3"+exeSuffix, "libgo.a", "libgo.h",
476 "go", "build", "-buildmode=c-archive", "-o", "libgo.a", "./libgo")
479 func TestEarlySignalHandler(t *testing.T) {
481 case "darwin", "ios":
484 t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", GOOS, GOARCH)
487 t.Skip("skipping signal test on Windows")
492 os.Remove("libgo2.a")
493 os.Remove("libgo2.h")
494 os.Remove("testp" + exeSuffix)
495 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
499 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo2.a", "./libgo2")
500 if out, err := cmd.CombinedOutput(); err != nil {
504 checkLineComments(t, "libgo2.h")
505 checkArchive(t, "libgo2.a")
507 ccArgs := append(cc, "-o", "testp"+exeSuffix, "main2.c", "libgo2.a")
508 if runtime.Compiler == "gccgo" {
509 ccArgs = append(ccArgs, "-lgo")
511 if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
517 if runtime.GOOS == "darwin" {
520 cmd = exec.Command(bin[0], append(bin[1:], darwin)...)
522 if out, err := cmd.CombinedOutput(); err != nil {
528 func TestSignalForwarding(t *testing.T) {
529 checkSignalForwardingTest(t)
530 buildSignalForwardingTest(t)
532 cmd := exec.Command(bin[0], append(bin[1:], "1")...)
534 out, err := cmd.CombinedOutput()
535 t.Logf("%v\n%s", cmd.Args, out)
536 expectSignal(t, err, syscall.SIGSEGV, 0)
538 // SIGPIPE is never forwarded on darwin. See golang.org/issue/33384.
539 if runtime.GOOS != "darwin" && runtime.GOOS != "ios" {
540 // Test SIGPIPE forwarding
541 cmd = exec.Command(bin[0], append(bin[1:], "3")...)
543 out, err = cmd.CombinedOutput()
547 expectSignal(t, err, syscall.SIGPIPE, 0)
551 func TestSignalForwardingExternal(t *testing.T) {
552 if GOOS == "freebsd" || GOOS == "aix" {
553 t.Skipf("skipping on %s/%s; signal always goes to the Go runtime", GOOS, GOARCH)
554 } else if GOOS == "darwin" && GOARCH == "amd64" {
555 t.Skipf("skipping on %s/%s: runtime does not permit SI_USER SIGSEGV", GOOS, GOARCH)
557 checkSignalForwardingTest(t)
558 buildSignalForwardingTest(t)
560 // We want to send the process a signal and see if it dies.
561 // Normally the signal goes to the C thread, the Go signal
562 // handler picks it up, sees that it is running in a C thread,
563 // and the program dies. Unfortunately, occasionally the
564 // signal is delivered to a Go thread, which winds up
565 // discarding it because it was sent by another program and
566 // there is no Go handler for it. To avoid this, run the
567 // program several times in the hopes that it will eventually
570 for i := 0; i < tries; i++ {
571 err := runSignalForwardingTest(t, "2")
576 // If the signal is delivered to a C thread, as expected,
577 // the Go signal handler will disable itself and re-raise
578 // the signal, causing the program to die with SIGSEGV.
580 // It is also possible that the signal will be
581 // delivered to a Go thread, such as a GC thread.
582 // Currently when the Go runtime sees that a SIGSEGV was
583 // sent from a different program, it first tries to send
584 // the signal to the os/signal API. If nothing is looking
585 // for (or explicitly ignoring) SIGSEGV, then it crashes.
586 // Because the Go runtime is invoked via a c-archive,
587 // it treats this as GOTRACEBACK=crash, meaning that it
588 // dumps a stack trace for all goroutines, which it does
589 // by raising SIGQUIT. The effect is that we will see the
590 // program die with SIGQUIT in that case, not SIGSEGV.
591 if expectSignal(t, err, syscall.SIGSEGV, syscall.SIGQUIT) {
596 t.Errorf("program succeeded unexpectedly %d times", tries)
599 func TestSignalForwardingGo(t *testing.T) {
600 // This test fails on darwin-amd64 because of the special
601 // handling of user-generated SIGSEGV signals in fixsigcode in
602 // runtime/signal_darwin_amd64.go.
603 if runtime.GOOS == "darwin" && runtime.GOARCH == "amd64" {
604 t.Skip("not supported on darwin-amd64")
607 checkSignalForwardingTest(t)
608 buildSignalForwardingTest(t)
609 err := runSignalForwardingTest(t, "4")
611 // Occasionally the signal will be delivered to a C thread,
612 // and the program will crash with SIGSEGV.
613 expectSignal(t, err, syscall.SIGQUIT, syscall.SIGSEGV)
616 // checkSignalForwardingTest calls t.Skip if the SignalForwarding test
617 // doesn't work on this platform.
618 func checkSignalForwardingTest(t *testing.T) {
620 case "darwin", "ios":
623 t.Skipf("skipping on %s/%s; see https://golang.org/issue/13701", GOOS, GOARCH)
626 t.Skip("skipping signal test on Windows")
630 // buildSignalForwardingTest builds the executable used by the various
631 // signal forwarding tests.
632 func buildSignalForwardingTest(t *testing.T) {
635 os.Remove("libgo2.a")
636 os.Remove("libgo2.h")
637 os.Remove("testp" + exeSuffix)
638 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
642 t.Log("go build -buildmode=c-archive -o libgo2.a ./libgo2")
643 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo2.a", "./libgo2")
644 out, err := cmd.CombinedOutput()
652 checkLineComments(t, "libgo2.h")
653 checkArchive(t, "libgo2.a")
655 ccArgs := append(cc, "-o", "testp"+exeSuffix, "main5.c", "libgo2.a")
656 if runtime.Compiler == "gccgo" {
657 ccArgs = append(ccArgs, "-lgo")
660 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
669 func runSignalForwardingTest(t *testing.T, arg string) error {
670 t.Logf("%v %s", bin, arg)
671 cmd := exec.Command(bin[0], append(bin[1:], arg)...)
673 var out strings.Builder
676 stderr, err := cmd.StderrPipe()
682 r := bufio.NewReader(stderr)
689 // Wait for trigger to ensure that process is started.
690 ok, err := r.ReadString('\n')
693 if err != nil || ok != "OK\n" {
694 t.Fatal("Did not receive OK signal")
697 var wg sync.WaitGroup
699 var errsb strings.Builder
705 // Give the program a chance to enter the function.
706 // If the program doesn't get there the test will still
707 // pass, although it doesn't quite test what we intended.
708 // This is fine as long as the program normally makes it.
709 time.Sleep(time.Millisecond)
711 cmd.Process.Signal(syscall.SIGSEGV)
728 // expectSignal checks that err, the exit status of a test program,
729 // shows a failure due to a specific signal or two. Returns whether we
730 // found an expected signal.
731 func expectSignal(t *testing.T, err error, sig1, sig2 syscall.Signal) bool {
734 t.Error("test program succeeded unexpectedly")
735 } else if ee, ok := err.(*exec.ExitError); !ok {
736 t.Errorf("error (%v) has type %T; expected exec.ExitError", err, err)
737 } else if ws, ok := ee.Sys().(syscall.WaitStatus); !ok {
738 t.Errorf("error.Sys (%v) has type %T; expected syscall.WaitStatus", ee.Sys(), ee.Sys())
739 } else if !ws.Signaled() || (ws.Signal() != sig1 && ws.Signal() != sig2) {
741 t.Errorf("got %q; expected signal %q", ee, sig1)
743 t.Errorf("got %q; expected signal %q or %q", ee, sig1, sig2)
751 func TestOsSignal(t *testing.T) {
754 t.Skip("skipping signal test on Windows")
759 os.Remove("libgo3.a")
760 os.Remove("libgo3.h")
761 os.Remove("testp" + exeSuffix)
762 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
766 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo3.a", "./libgo3")
767 if out, err := cmd.CombinedOutput(); err != nil {
771 checkLineComments(t, "libgo3.h")
772 checkArchive(t, "libgo3.a")
774 ccArgs := append(cc, "-o", "testp"+exeSuffix, "main3.c", "libgo3.a")
775 if runtime.Compiler == "gccgo" {
776 ccArgs = append(ccArgs, "-lgo")
778 if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
783 if out, err := exec.Command(bin[0], bin[1:]...).CombinedOutput(); err != nil {
789 func TestSigaltstack(t *testing.T) {
792 t.Skip("skipping signal test on Windows")
797 os.Remove("libgo4.a")
798 os.Remove("libgo4.h")
799 os.Remove("testp" + exeSuffix)
800 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
804 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo4.a", "./libgo4")
805 if out, err := cmd.CombinedOutput(); err != nil {
809 checkLineComments(t, "libgo4.h")
810 checkArchive(t, "libgo4.a")
812 ccArgs := append(cc, "-o", "testp"+exeSuffix, "main4.c", "libgo4.a")
813 if runtime.Compiler == "gccgo" {
814 ccArgs = append(ccArgs, "-lgo")
816 if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
821 if out, err := exec.Command(bin[0], bin[1:]...).CombinedOutput(); err != nil {
827 const testar = `#!/usr/bin/env bash
828 while [[ $1 == -* ]] >/dev/null; do
832 echo "testar" > PWD/testar.ran
835 func TestExtar(t *testing.T) {
838 t.Skip("skipping signal test on Windows")
840 if runtime.Compiler == "gccgo" {
841 t.Skip("skipping -extar test when using gccgo")
843 if runtime.GOOS == "ios" {
844 t.Skip("shell scripts are not executable on iOS hosts")
849 os.Remove("libgo4.a")
850 os.Remove("libgo4.h")
852 os.Remove("testar.ran")
853 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
858 dir, err := os.Getwd()
862 s := strings.Replace(testar, "PWD", dir, 1)
863 if err := os.WriteFile("testar", []byte(s), 0777); err != nil {
867 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-ldflags=-extar="+filepath.Join(dir, "testar"), "-o", "libgo4.a", "./libgo4")
868 if out, err := cmd.CombinedOutput(); err != nil {
872 checkLineComments(t, "libgo4.h")
874 if _, err := os.Stat("testar.ran"); err != nil {
875 if os.IsNotExist(err) {
876 t.Error("testar does not exist after go build")
878 t.Errorf("error checking testar: %v", err)
883 func TestPIE(t *testing.T) {
885 case "windows", "darwin", "ios", "plan9":
886 t.Skipf("skipping PIE test on %s", GOOS)
890 if runtime.Compiler == "gccgo" {
891 libgoa = "liblibgo.a"
896 os.Remove("testp" + exeSuffix)
898 os.RemoveAll(filepath.Join(GOPATH, "pkg"))
902 // Generate the p.h header file.
904 // 'go install -i -buildmode=c-archive ./libgo' would do that too, but that
905 // would also attempt to install transitive standard-library dependencies to
906 // GOROOT, and we cannot assume that GOROOT is writable. (A non-root user may
907 // be running this test in a GOROOT owned by root.)
908 genHeader(t, "p.h", "./p")
910 cmd := exec.Command("go", "build", "-buildmode=c-archive", "./libgo")
911 if out, err := cmd.CombinedOutput(); err != nil {
916 ccArgs := append(cc, "-fPIE", "-pie", "-o", "testp"+exeSuffix, "main.c", "main_unix.c", libgoa)
917 if runtime.Compiler == "gccgo" {
918 ccArgs = append(ccArgs, "-lgo")
920 if out, err := exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput(); err != nil {
925 binArgs := append(bin, "arg1", "arg2")
926 cmd = exec.Command(binArgs[0], binArgs[1:]...)
927 if runtime.Compiler == "gccgo" {
928 cmd.Env = append(os.Environ(), "GCCGO=1")
930 if out, err := cmd.CombinedOutput(); err != nil {
936 f, err := elf.Open("testp" + exeSuffix)
938 t.Fatal("elf.Open failed: ", err)
941 if hasDynTag(t, f, elf.DT_TEXTREL) {
942 t.Errorf("%s has DT_TEXTREL flag", "testp"+exeSuffix)
947 func hasDynTag(t *testing.T, f *elf.File, tag elf.DynTag) bool {
948 ds := f.SectionByType(elf.SHT_DYNAMIC)
950 t.Error("no SHT_DYNAMIC section")
955 t.Errorf("can't read SHT_DYNAMIC contents: %v", err)
962 t = elf.DynTag(f.ByteOrder.Uint32(d[:4]))
965 t = elf.DynTag(f.ByteOrder.Uint64(d[:8]))
975 func TestSIGPROF(t *testing.T) {
977 case "windows", "plan9":
978 t.Skipf("skipping SIGPROF test on %s", GOOS)
979 case "darwin", "ios":
980 t.Skipf("skipping SIGPROF test on %s; see https://golang.org/issue/19320", GOOS)
987 os.Remove("testp6" + exeSuffix)
988 os.Remove("libgo6.a")
989 os.Remove("libgo6.h")
993 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo6.a", "./libgo6")
994 out, err := cmd.CombinedOutput()
995 t.Logf("%v\n%s", cmd.Args, out)
999 checkLineComments(t, "libgo6.h")
1000 checkArchive(t, "libgo6.a")
1002 ccArgs := append(cc, "-o", "testp6"+exeSuffix, "main6.c", "libgo6.a")
1003 if runtime.Compiler == "gccgo" {
1004 ccArgs = append(ccArgs, "-lgo")
1006 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1007 t.Logf("%v\n%s", ccArgs, out)
1012 argv := cmdToRun("./testp6")
1013 cmd = exec.Command(argv[0], argv[1:]...)
1014 out, err = cmd.CombinedOutput()
1015 t.Logf("%v\n%s", argv, out)
1021 // TestCompileWithoutShared tests that if we compile code without the
1022 // -shared option, we can put it into an archive. When we use the go
1023 // tool with -buildmode=c-archive, it passes -shared to the compiler,
1024 // so we override that. The go tool doesn't work this way, but Bazel
1025 // will likely do it in the future. And it ought to work. This test
1026 // was added because at one time it did not work on PPC Linux.
1027 func TestCompileWithoutShared(t *testing.T) {
1028 // For simplicity, reuse the signal forwarding test.
1029 checkSignalForwardingTest(t)
1033 os.Remove("libgo2.a")
1034 os.Remove("libgo2.h")
1038 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-gcflags=-shared=false", "-o", "libgo2.a", "./libgo2")
1039 out, err := cmd.CombinedOutput()
1040 t.Logf("%v\n%s", cmd.Args, out)
1044 checkLineComments(t, "libgo2.h")
1045 checkArchive(t, "libgo2.a")
1047 exe := "./testnoshared" + exeSuffix
1049 // In some cases, -no-pie is needed here, but not accepted everywhere. First try
1050 // if -no-pie is accepted. See #22126.
1051 ccArgs := append(cc, "-o", exe, "-no-pie", "main5.c", "libgo2.a")
1052 if runtime.Compiler == "gccgo" {
1053 ccArgs = append(ccArgs, "-lgo")
1055 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1056 t.Logf("%v\n%s", ccArgs, out)
1058 // If -no-pie unrecognized, try -nopie if this is possibly clang
1059 if err != nil && bytes.Contains(out, []byte("unknown")) && !strings.Contains(cc[0], "gcc") {
1060 ccArgs = append(cc, "-o", exe, "-nopie", "main5.c", "libgo2.a")
1061 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1062 t.Logf("%v\n%s", ccArgs, out)
1065 // Don't use either -no-pie or -nopie
1066 if err != nil && bytes.Contains(out, []byte("unrecognized")) {
1067 ccArgs = append(cc, "-o", exe, "main5.c", "libgo2.a")
1068 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1069 t.Logf("%v\n%s", ccArgs, out)
1075 defer os.Remove(exe)
1078 binArgs := append(cmdToRun(exe), "1")
1079 out, err = exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput()
1080 t.Logf("%v\n%s", binArgs, out)
1081 expectSignal(t, err, syscall.SIGSEGV, 0)
1083 // SIGPIPE is never forwarded on darwin. See golang.org/issue/33384.
1084 if runtime.GOOS != "darwin" && runtime.GOOS != "ios" {
1085 binArgs := append(cmdToRun(exe), "3")
1086 out, err = exec.Command(binArgs[0], binArgs[1:]...).CombinedOutput()
1087 t.Logf("%v\n%s", binArgs, out)
1088 expectSignal(t, err, syscall.SIGPIPE, 0)
1092 // Test that installing a second time recreates the header file.
1093 func TestCachedInstall(t *testing.T) {
1095 defer os.RemoveAll(filepath.Join(GOPATH, "pkg"))
1098 h := filepath.Join(libgodir, "libgo.h")
1100 buildcmd := []string{"go", "install", "-buildmode=c-archive", "./libgo"}
1102 cmd := exec.Command(buildcmd[0], buildcmd[1:]...)
1103 cmd.Env = append(cmd.Environ(), "GO111MODULE=off") // 'go install' only works in GOPATH mode
1105 if out, err := cmd.CombinedOutput(); err != nil {
1110 if _, err := os.Stat(h); err != nil {
1111 t.Errorf("libgo.h not installed: %v", err)
1114 if err := os.Remove(h); err != nil {
1118 cmd = exec.Command(buildcmd[0], buildcmd[1:]...)
1119 cmd.Env = append(cmd.Environ(), "GO111MODULE=off")
1121 if out, err := cmd.CombinedOutput(); err != nil {
1126 if _, err := os.Stat(h); err != nil {
1127 t.Errorf("libgo.h not installed in second run: %v", err)
1132 func TestManyCalls(t *testing.T) {
1137 os.Remove("testp7" + exeSuffix)
1138 os.Remove("libgo7.a")
1139 os.Remove("libgo7.h")
1143 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo7.a", "./libgo7")
1144 out, err := cmd.CombinedOutput()
1145 t.Logf("%v\n%s", cmd.Args, out)
1149 checkLineComments(t, "libgo7.h")
1150 checkArchive(t, "libgo7.a")
1152 ccArgs := append(cc, "-o", "testp7"+exeSuffix, "main7.c", "libgo7.a")
1153 if runtime.Compiler == "gccgo" {
1154 ccArgs = append(ccArgs, "-lgo")
1156 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1157 t.Logf("%v\n%s", ccArgs, out)
1162 argv := cmdToRun("./testp7")
1163 cmd = exec.Command(argv[0], argv[1:]...)
1164 sb := new(strings.Builder)
1167 if err := cmd.Start(); err != nil {
1171 timer := time.AfterFunc(time.Minute,
1173 t.Error("test program timed out")
1180 t.Logf("%v\n%s", cmd.Args, sb)
1187 func TestPreemption(t *testing.T) {
1188 if runtime.Compiler == "gccgo" {
1189 t.Skip("skipping asynchronous preemption test with gccgo")
1196 os.Remove("testp8" + exeSuffix)
1197 os.Remove("libgo8.a")
1198 os.Remove("libgo8.h")
1202 cmd := exec.Command("go", "build", "-buildmode=c-archive", "-o", "libgo8.a", "./libgo8")
1203 out, err := cmd.CombinedOutput()
1204 t.Logf("%v\n%s", cmd.Args, out)
1208 checkLineComments(t, "libgo8.h")
1209 checkArchive(t, "libgo8.a")
1211 ccArgs := append(cc, "-o", "testp8"+exeSuffix, "main8.c", "libgo8.a")
1212 out, err = exec.Command(ccArgs[0], ccArgs[1:]...).CombinedOutput()
1213 t.Logf("%v\n%s", ccArgs, out)
1218 argv := cmdToRun("./testp8")
1219 cmd = exec.Command(argv[0], argv[1:]...)
1220 sb := new(strings.Builder)
1223 if err := cmd.Start(); err != nil {
1227 timer := time.AfterFunc(time.Minute,
1229 t.Error("test program timed out")
1236 t.Logf("%v\n%s", cmd.Args, sb)