github.com/ronhuafeng/gofrontend@v0.0.0-20220715151246-ff23266b8bc5/libgo/misc/cgo/testshared/shared_test.go (about) 1 // Copyright 2015 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 package shared_test 6 7 import ( 8 "bufio" 9 "bytes" 10 "debug/elf" 11 "encoding/binary" 12 "flag" 13 "fmt" 14 "go/build" 15 "io" 16 "log" 17 "os" 18 "os/exec" 19 "path/filepath" 20 "regexp" 21 "runtime" 22 "sort" 23 "strconv" 24 "strings" 25 "testing" 26 "time" 27 ) 28 29 var gopathInstallDir, gorootInstallDir string 30 31 // This is the smallest set of packages we can link into a shared 32 // library (runtime/cgo is built implicitly). 33 var minpkgs = []string{"runtime", "sync/atomic"} 34 var soname = "libruntime,sync-atomic.so" 35 36 var testX = flag.Bool("testx", false, "if true, pass -x to 'go' subcommands invoked by the test") 37 var testWork = flag.Bool("testwork", false, "if true, log and do not delete the temporary working directory") 38 39 // run runs a command and calls t.Errorf if it fails. 40 func run(t *testing.T, msg string, args ...string) { 41 runWithEnv(t, msg, nil, args...) 42 } 43 44 // runWithEnv runs a command under the given environment and calls t.Errorf if it fails. 45 func runWithEnv(t *testing.T, msg string, env []string, args ...string) { 46 c := exec.Command(args[0], args[1:]...) 47 if len(env) != 0 { 48 c.Env = append(os.Environ(), env...) 49 } 50 if output, err := c.CombinedOutput(); err != nil { 51 t.Errorf("executing %s (%s) failed %s:\n%s", strings.Join(args, " "), msg, err, output) 52 } 53 } 54 55 // goCmd invokes the go tool with the installsuffix set up by TestMain. It calls 56 // t.Fatalf if the command fails. 57 func goCmd(t *testing.T, args ...string) string { 58 newargs := []string{args[0]} 59 if *testX && args[0] != "env" { 60 newargs = append(newargs, "-x") 61 } 62 newargs = append(newargs, args[1:]...) 63 c := exec.Command("go", newargs...) 64 stderr := new(strings.Builder) 65 c.Stderr = stderr 66 67 if testing.Verbose() && t == nil { 68 fmt.Fprintf(os.Stderr, "+ go %s\n", strings.Join(args, " ")) 69 c.Stderr = os.Stderr 70 } 71 output, err := c.Output() 72 73 if err != nil { 74 if t != nil { 75 t.Helper() 76 t.Fatalf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr) 77 } else { 78 // Panic instead of using log.Fatalf so that deferred cleanup may run in testMain. 79 log.Panicf("executing %s failed %v:\n%s", strings.Join(c.Args, " "), err, stderr) 80 } 81 } 82 if testing.Verbose() && t != nil { 83 t.Logf("go %s", strings.Join(args, " ")) 84 if stderr.Len() > 0 { 85 t.Logf("%s", stderr) 86 } 87 } 88 return string(bytes.TrimSpace(output)) 89 } 90 91 // TestMain calls testMain so that the latter can use defer (TestMain exits with os.Exit). 92 func testMain(m *testing.M) (int, error) { 93 workDir, err := os.MkdirTemp("", "shared_test") 94 if err != nil { 95 return 0, err 96 } 97 if *testWork || testing.Verbose() { 98 fmt.Printf("+ mkdir -p %s\n", workDir) 99 } 100 if !*testWork { 101 defer os.RemoveAll(workDir) 102 } 103 104 // Some tests need to edit the source in GOPATH, so copy this directory to a 105 // temporary directory and chdir to that. 106 gopath := filepath.Join(workDir, "gopath") 107 modRoot, err := cloneTestdataModule(gopath) 108 if err != nil { 109 return 0, err 110 } 111 if testing.Verbose() { 112 fmt.Printf("+ export GOPATH=%s\n", gopath) 113 fmt.Printf("+ cd %s\n", modRoot) 114 } 115 os.Setenv("GOPATH", gopath) 116 // Explicitly override GOBIN as well, in case it was set through a GOENV file. 117 os.Setenv("GOBIN", filepath.Join(gopath, "bin")) 118 os.Chdir(modRoot) 119 os.Setenv("PWD", modRoot) 120 121 // The test also needs to install libraries into GOROOT/pkg, so copy the 122 // subset of GOROOT that we need. 123 // 124 // TODO(golang.org/issue/28553): Rework -buildmode=shared so that it does not 125 // need to write to GOROOT. 126 goroot := filepath.Join(workDir, "goroot") 127 if err := cloneGOROOTDeps(goroot); err != nil { 128 return 0, err 129 } 130 if testing.Verbose() { 131 fmt.Fprintf(os.Stderr, "+ export GOROOT=%s\n", goroot) 132 } 133 os.Setenv("GOROOT", goroot) 134 135 myContext := build.Default 136 myContext.GOROOT = goroot 137 myContext.GOPATH = gopath 138 runtimeP, err := myContext.Import("runtime", ".", build.ImportComment) 139 if err != nil { 140 return 0, fmt.Errorf("import failed: %v", err) 141 } 142 gorootInstallDir = runtimeP.PkgTargetRoot + "_dynlink" 143 144 // All tests depend on runtime being built into a shared library. Because 145 // that takes a few seconds, do it here and have all tests use the version 146 // built here. 147 goCmd(nil, append([]string{"install", "-buildmode=shared"}, minpkgs...)...) 148 149 myContext.InstallSuffix = "_dynlink" 150 depP, err := myContext.Import("./depBase", ".", build.ImportComment) 151 if err != nil { 152 return 0, fmt.Errorf("import failed: %v", err) 153 } 154 if depP.PkgTargetRoot == "" { 155 gopathInstallDir = filepath.Dir(goCmd(nil, "list", "-buildmode=shared", "-f", "{{.Target}}", "./depBase")) 156 } else { 157 gopathInstallDir = filepath.Join(depP.PkgTargetRoot, "testshared") 158 } 159 return m.Run(), nil 160 } 161 162 func TestMain(m *testing.M) { 163 log.SetFlags(log.Lshortfile) 164 flag.Parse() 165 166 exitCode, err := testMain(m) 167 if err != nil { 168 log.Fatal(err) 169 } 170 os.Exit(exitCode) 171 } 172 173 // cloneTestdataModule clones the packages from src/testshared into gopath. 174 // It returns the directory within gopath at which the module root is located. 175 func cloneTestdataModule(gopath string) (string, error) { 176 modRoot := filepath.Join(gopath, "src", "testshared") 177 if err := overlayDir(modRoot, "testdata"); err != nil { 178 return "", err 179 } 180 if err := os.WriteFile(filepath.Join(modRoot, "go.mod"), []byte("module testshared\n"), 0644); err != nil { 181 return "", err 182 } 183 return modRoot, nil 184 } 185 186 // cloneGOROOTDeps copies (or symlinks) the portions of GOROOT/src and 187 // GOROOT/pkg relevant to this test into the given directory. 188 // It must be run from within the testdata module. 189 func cloneGOROOTDeps(goroot string) error { 190 oldGOROOT := strings.TrimSpace(goCmd(nil, "env", "GOROOT")) 191 if oldGOROOT == "" { 192 return fmt.Errorf("go env GOROOT returned an empty string") 193 } 194 195 // Before we clone GOROOT, figure out which packages we need to copy over. 196 listArgs := []string{ 197 "list", 198 "-deps", 199 "-f", "{{if and .Standard (not .ForTest)}}{{.ImportPath}}{{end}}", 200 } 201 stdDeps := goCmd(nil, append(listArgs, minpkgs...)...) 202 testdataDeps := goCmd(nil, append(listArgs, "-test", "./...")...) 203 204 pkgs := append(strings.Split(strings.TrimSpace(stdDeps), "\n"), 205 strings.Split(strings.TrimSpace(testdataDeps), "\n")...) 206 sort.Strings(pkgs) 207 var pkgRoots []string 208 for _, pkg := range pkgs { 209 parentFound := false 210 for _, prev := range pkgRoots { 211 if strings.HasPrefix(pkg, prev) { 212 // We will copy in the source for pkg when we copy in prev. 213 parentFound = true 214 break 215 } 216 } 217 if !parentFound { 218 pkgRoots = append(pkgRoots, pkg) 219 } 220 } 221 222 gorootDirs := []string{ 223 "pkg/tool", 224 "pkg/include", 225 } 226 for _, pkg := range pkgRoots { 227 gorootDirs = append(gorootDirs, filepath.Join("src", pkg)) 228 } 229 230 for _, dir := range gorootDirs { 231 if testing.Verbose() { 232 fmt.Fprintf(os.Stderr, "+ cp -r %s %s\n", filepath.Join(oldGOROOT, dir), filepath.Join(goroot, dir)) 233 } 234 if err := overlayDir(filepath.Join(goroot, dir), filepath.Join(oldGOROOT, dir)); err != nil { 235 return err 236 } 237 } 238 239 return nil 240 } 241 242 // The shared library was built at the expected location. 243 func TestSOBuilt(t *testing.T) { 244 _, err := os.Stat(filepath.Join(gorootInstallDir, soname)) 245 if err != nil { 246 t.Error(err) 247 } 248 } 249 250 func hasDynTag(f *elf.File, tag elf.DynTag) bool { 251 ds := f.SectionByType(elf.SHT_DYNAMIC) 252 if ds == nil { 253 return false 254 } 255 d, err := ds.Data() 256 if err != nil { 257 return false 258 } 259 for len(d) > 0 { 260 var t elf.DynTag 261 switch f.Class { 262 case elf.ELFCLASS32: 263 t = elf.DynTag(f.ByteOrder.Uint32(d[0:4])) 264 d = d[8:] 265 case elf.ELFCLASS64: 266 t = elf.DynTag(f.ByteOrder.Uint64(d[0:8])) 267 d = d[16:] 268 } 269 if t == tag { 270 return true 271 } 272 } 273 return false 274 } 275 276 // The shared library does not have relocations against the text segment. 277 func TestNoTextrel(t *testing.T) { 278 sopath := filepath.Join(gorootInstallDir, soname) 279 f, err := elf.Open(sopath) 280 if err != nil { 281 t.Fatal("elf.Open failed: ", err) 282 } 283 defer f.Close() 284 if hasDynTag(f, elf.DT_TEXTREL) { 285 t.Errorf("%s has DT_TEXTREL set", soname) 286 } 287 } 288 289 // The shared library does not contain symbols called ".dup" 290 // (See golang.org/issue/14841.) 291 func TestNoDupSymbols(t *testing.T) { 292 sopath := filepath.Join(gorootInstallDir, soname) 293 f, err := elf.Open(sopath) 294 if err != nil { 295 t.Fatal("elf.Open failed: ", err) 296 } 297 defer f.Close() 298 syms, err := f.Symbols() 299 if err != nil { 300 t.Errorf("error reading symbols %v", err) 301 return 302 } 303 for _, s := range syms { 304 if s.Name == ".dup" { 305 t.Fatalf("%s contains symbol called .dup", sopath) 306 } 307 } 308 } 309 310 // The install command should have created a "shlibname" file for the 311 // listed packages (and runtime/cgo, and math on arm) indicating the 312 // name of the shared library containing it. 313 func TestShlibnameFiles(t *testing.T) { 314 pkgs := append([]string{}, minpkgs...) 315 pkgs = append(pkgs, "runtime/cgo") 316 if runtime.GOARCH == "arm" { 317 pkgs = append(pkgs, "math") 318 } 319 for _, pkg := range pkgs { 320 shlibnamefile := filepath.Join(gorootInstallDir, pkg+".shlibname") 321 contentsb, err := os.ReadFile(shlibnamefile) 322 if err != nil { 323 t.Errorf("error reading shlibnamefile for %s: %v", pkg, err) 324 continue 325 } 326 contents := strings.TrimSpace(string(contentsb)) 327 if contents != soname { 328 t.Errorf("shlibnamefile for %s has wrong contents: %q", pkg, contents) 329 } 330 } 331 } 332 333 // Is a given offset into the file contained in a loaded segment? 334 func isOffsetLoaded(f *elf.File, offset uint64) bool { 335 for _, prog := range f.Progs { 336 if prog.Type == elf.PT_LOAD { 337 if prog.Off <= offset && offset < prog.Off+prog.Filesz { 338 return true 339 } 340 } 341 } 342 return false 343 } 344 345 func rnd(v int32, r int32) int32 { 346 if r <= 0 { 347 return v 348 } 349 v += r - 1 350 c := v % r 351 if c < 0 { 352 c += r 353 } 354 v -= c 355 return v 356 } 357 358 func readwithpad(r io.Reader, sz int32) ([]byte, error) { 359 data := make([]byte, rnd(sz, 4)) 360 _, err := io.ReadFull(r, data) 361 if err != nil { 362 return nil, err 363 } 364 data = data[:sz] 365 return data, nil 366 } 367 368 type note struct { 369 name string 370 tag int32 371 desc string 372 section *elf.Section 373 } 374 375 // Read all notes from f. As ELF section names are not supposed to be special, one 376 // looks for a particular note by scanning all SHT_NOTE sections looking for a note 377 // with a particular "name" and "tag". 378 func readNotes(f *elf.File) ([]*note, error) { 379 var notes []*note 380 for _, sect := range f.Sections { 381 if sect.Type != elf.SHT_NOTE { 382 continue 383 } 384 r := sect.Open() 385 for { 386 var namesize, descsize, tag int32 387 err := binary.Read(r, f.ByteOrder, &namesize) 388 if err != nil { 389 if err == io.EOF { 390 break 391 } 392 return nil, fmt.Errorf("read namesize failed: %v", err) 393 } 394 err = binary.Read(r, f.ByteOrder, &descsize) 395 if err != nil { 396 return nil, fmt.Errorf("read descsize failed: %v", err) 397 } 398 err = binary.Read(r, f.ByteOrder, &tag) 399 if err != nil { 400 return nil, fmt.Errorf("read type failed: %v", err) 401 } 402 name, err := readwithpad(r, namesize) 403 if err != nil { 404 return nil, fmt.Errorf("read name failed: %v", err) 405 } 406 desc, err := readwithpad(r, descsize) 407 if err != nil { 408 return nil, fmt.Errorf("read desc failed: %v", err) 409 } 410 notes = append(notes, ¬e{name: string(name), tag: tag, desc: string(desc), section: sect}) 411 } 412 } 413 return notes, nil 414 } 415 416 func dynStrings(t *testing.T, path string, flag elf.DynTag) []string { 417 t.Helper() 418 f, err := elf.Open(path) 419 if err != nil { 420 t.Fatalf("elf.Open(%q) failed: %v", path, err) 421 } 422 defer f.Close() 423 dynstrings, err := f.DynString(flag) 424 if err != nil { 425 t.Fatalf("DynString(%s) failed on %s: %v", flag, path, err) 426 } 427 return dynstrings 428 } 429 430 func AssertIsLinkedToRegexp(t *testing.T, path string, re *regexp.Regexp) { 431 t.Helper() 432 for _, dynstring := range dynStrings(t, path, elf.DT_NEEDED) { 433 if re.MatchString(dynstring) { 434 return 435 } 436 } 437 t.Errorf("%s is not linked to anything matching %v", path, re) 438 } 439 440 func AssertIsLinkedTo(t *testing.T, path, lib string) { 441 t.Helper() 442 AssertIsLinkedToRegexp(t, path, regexp.MustCompile(regexp.QuoteMeta(lib))) 443 } 444 445 func AssertHasRPath(t *testing.T, path, dir string) { 446 t.Helper() 447 for _, tag := range []elf.DynTag{elf.DT_RPATH, elf.DT_RUNPATH} { 448 for _, dynstring := range dynStrings(t, path, tag) { 449 for _, rpath := range strings.Split(dynstring, ":") { 450 if filepath.Clean(rpath) == filepath.Clean(dir) { 451 return 452 } 453 } 454 } 455 } 456 t.Errorf("%s does not have rpath %s", path, dir) 457 } 458 459 // Build a trivial program that links against the shared runtime and check it runs. 460 func TestTrivialExecutable(t *testing.T) { 461 goCmd(t, "install", "-linkshared", "./trivial") 462 run(t, "trivial executable", "../../bin/trivial") 463 AssertIsLinkedTo(t, "../../bin/trivial", soname) 464 AssertHasRPath(t, "../../bin/trivial", gorootInstallDir) 465 // It is 19K on linux/amd64, with separate-code in binutils ld and 64k being most common alignment 466 // 4*64k should be enough, but this might need revision eventually. 467 checkSize(t, "../../bin/trivial", 256000) 468 } 469 470 // Build a trivial program in PIE mode that links against the shared runtime and check it runs. 471 func TestTrivialExecutablePIE(t *testing.T) { 472 goCmd(t, "build", "-buildmode=pie", "-o", "trivial.pie", "-linkshared", "./trivial") 473 run(t, "trivial executable", "./trivial.pie") 474 AssertIsLinkedTo(t, "./trivial.pie", soname) 475 AssertHasRPath(t, "./trivial.pie", gorootInstallDir) 476 // It is 19K on linux/amd64, with separate-code in binutils ld and 64k being most common alignment 477 // 4*64k should be enough, but this might need revision eventually. 478 checkSize(t, "./trivial.pie", 256000) 479 } 480 481 // Check that the file size does not exceed a limit. 482 func checkSize(t *testing.T, f string, limit int64) { 483 fi, err := os.Stat(f) 484 if err != nil { 485 t.Fatalf("stat failed: %v", err) 486 } 487 if sz := fi.Size(); sz > limit { 488 t.Errorf("file too large: got %d, want <= %d", sz, limit) 489 } 490 } 491 492 // Build a division test program and check it runs. 493 func TestDivisionExecutable(t *testing.T) { 494 goCmd(t, "install", "-linkshared", "./division") 495 run(t, "division executable", "../../bin/division") 496 } 497 498 // Build an executable that uses cgo linked against the shared runtime and check it 499 // runs. 500 func TestCgoExecutable(t *testing.T) { 501 goCmd(t, "install", "-linkshared", "./execgo") 502 run(t, "cgo executable", "../../bin/execgo") 503 } 504 505 func checkPIE(t *testing.T, name string) { 506 f, err := elf.Open(name) 507 if err != nil { 508 t.Fatal("elf.Open failed: ", err) 509 } 510 defer f.Close() 511 if f.Type != elf.ET_DYN { 512 t.Errorf("%s has type %v, want ET_DYN", name, f.Type) 513 } 514 if hasDynTag(f, elf.DT_TEXTREL) { 515 t.Errorf("%s has DT_TEXTREL set", name) 516 } 517 } 518 519 func TestTrivialPIE(t *testing.T) { 520 name := "trivial_pie" 521 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./trivial") 522 defer os.Remove(name) 523 run(t, name, "./"+name) 524 checkPIE(t, name) 525 } 526 527 func TestCgoPIE(t *testing.T) { 528 name := "cgo_pie" 529 goCmd(t, "build", "-buildmode=pie", "-o="+name, "./execgo") 530 defer os.Remove(name) 531 run(t, name, "./"+name) 532 checkPIE(t, name) 533 } 534 535 // Build a GOPATH package into a shared library that links against the goroot runtime 536 // and an executable that links against both. 537 func TestGopathShlib(t *testing.T) { 538 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 539 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase") 540 AssertIsLinkedTo(t, shlib, soname) 541 goCmd(t, "install", "-linkshared", "./exe") 542 AssertIsLinkedTo(t, "../../bin/exe", soname) 543 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib)) 544 AssertHasRPath(t, "../../bin/exe", gorootInstallDir) 545 AssertHasRPath(t, "../../bin/exe", filepath.Dir(gopathInstallDir)) 546 // And check it runs. 547 run(t, "executable linked to GOPATH library", "../../bin/exe") 548 } 549 550 // The shared library contains a note listing the packages it contains in a section 551 // that is not mapped into memory. 552 func testPkgListNote(t *testing.T, f *elf.File, note *note) { 553 if note.section.Flags != 0 { 554 t.Errorf("package list section has flags %v, want 0", note.section.Flags) 555 } 556 if isOffsetLoaded(f, note.section.Offset) { 557 t.Errorf("package list section contained in PT_LOAD segment") 558 } 559 if note.desc != "testshared/depBase\n" { 560 t.Errorf("incorrect package list %q, want %q", note.desc, "testshared/depBase\n") 561 } 562 } 563 564 // The shared library contains a note containing the ABI hash that is mapped into 565 // memory and there is a local symbol called go.link.abihashbytes that points 16 566 // bytes into it. 567 func testABIHashNote(t *testing.T, f *elf.File, note *note) { 568 if note.section.Flags != elf.SHF_ALLOC { 569 t.Errorf("abi hash section has flags %v, want SHF_ALLOC", note.section.Flags) 570 } 571 if !isOffsetLoaded(f, note.section.Offset) { 572 t.Errorf("abihash section not contained in PT_LOAD segment") 573 } 574 var hashbytes elf.Symbol 575 symbols, err := f.Symbols() 576 if err != nil { 577 t.Errorf("error reading symbols %v", err) 578 return 579 } 580 for _, sym := range symbols { 581 if sym.Name == "go.link.abihashbytes" { 582 hashbytes = sym 583 } 584 } 585 if hashbytes.Name == "" { 586 t.Errorf("no symbol called go.link.abihashbytes") 587 return 588 } 589 if elf.ST_BIND(hashbytes.Info) != elf.STB_LOCAL { 590 t.Errorf("%s has incorrect binding %v, want STB_LOCAL", hashbytes.Name, elf.ST_BIND(hashbytes.Info)) 591 } 592 if f.Sections[hashbytes.Section] != note.section { 593 t.Errorf("%s has incorrect section %v, want %s", hashbytes.Name, f.Sections[hashbytes.Section].Name, note.section.Name) 594 } 595 if hashbytes.Value-note.section.Addr != 16 { 596 t.Errorf("%s has incorrect offset into section %d, want 16", hashbytes.Name, hashbytes.Value-note.section.Addr) 597 } 598 } 599 600 // A Go shared library contains a note indicating which other Go shared libraries it 601 // was linked against in an unmapped section. 602 func testDepsNote(t *testing.T, f *elf.File, note *note) { 603 if note.section.Flags != 0 { 604 t.Errorf("package list section has flags %v, want 0", note.section.Flags) 605 } 606 if isOffsetLoaded(f, note.section.Offset) { 607 t.Errorf("package list section contained in PT_LOAD segment") 608 } 609 // libdepBase.so just links against the lib containing the runtime. 610 if note.desc != soname { 611 t.Errorf("incorrect dependency list %q, want %q", note.desc, soname) 612 } 613 } 614 615 // The shared library contains notes with defined contents; see above. 616 func TestNotes(t *testing.T) { 617 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 618 shlib := goCmd(t, "list", "-f", "{{.Shlib}}", "-buildmode=shared", "-linkshared", "./depBase") 619 f, err := elf.Open(shlib) 620 if err != nil { 621 t.Fatal(err) 622 } 623 defer f.Close() 624 notes, err := readNotes(f) 625 if err != nil { 626 t.Fatal(err) 627 } 628 pkgListNoteFound := false 629 abiHashNoteFound := false 630 depsNoteFound := false 631 for _, note := range notes { 632 if note.name != "Go\x00\x00" { 633 continue 634 } 635 switch note.tag { 636 case 1: // ELF_NOTE_GOPKGLIST_TAG 637 if pkgListNoteFound { 638 t.Error("multiple package list notes") 639 } 640 testPkgListNote(t, f, note) 641 pkgListNoteFound = true 642 case 2: // ELF_NOTE_GOABIHASH_TAG 643 if abiHashNoteFound { 644 t.Error("multiple abi hash notes") 645 } 646 testABIHashNote(t, f, note) 647 abiHashNoteFound = true 648 case 3: // ELF_NOTE_GODEPS_TAG 649 if depsNoteFound { 650 t.Error("multiple dependency list notes") 651 } 652 testDepsNote(t, f, note) 653 depsNoteFound = true 654 } 655 } 656 if !pkgListNoteFound { 657 t.Error("package list note not found") 658 } 659 if !abiHashNoteFound { 660 t.Error("abi hash note not found") 661 } 662 if !depsNoteFound { 663 t.Error("deps note not found") 664 } 665 } 666 667 // Build a GOPATH package (depBase) into a shared library that links against the goroot 668 // runtime, another package (dep2) that links against the first, and an 669 // executable that links against dep2. 670 func TestTwoGopathShlibs(t *testing.T) { 671 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 672 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2") 673 goCmd(t, "install", "-linkshared", "./exe2") 674 run(t, "executable linked to GOPATH library", "../../bin/exe2") 675 } 676 677 func TestThreeGopathShlibs(t *testing.T) { 678 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 679 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep2") 680 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./dep3") 681 goCmd(t, "install", "-linkshared", "./exe3") 682 run(t, "executable linked to GOPATH library", "../../bin/exe3") 683 } 684 685 // If gccgo is not available or not new enough, call t.Skip. 686 func requireGccgo(t *testing.T) { 687 t.Helper() 688 689 gccgoName := os.Getenv("GCCGO") 690 if gccgoName == "" { 691 gccgoName = "gccgo" 692 } 693 gccgoPath, err := exec.LookPath(gccgoName) 694 if err != nil { 695 t.Skip("gccgo not found") 696 } 697 cmd := exec.Command(gccgoPath, "-dumpversion") 698 output, err := cmd.CombinedOutput() 699 if err != nil { 700 t.Fatalf("%s -dumpversion failed: %v\n%s", gccgoPath, err, output) 701 } 702 dot := bytes.Index(output, []byte{'.'}) 703 if dot > 0 { 704 output = output[:dot] 705 } 706 major, err := strconv.Atoi(string(output)) 707 if err != nil { 708 t.Skipf("can't parse gccgo version number %s", output) 709 } 710 if major < 5 { 711 t.Skipf("gccgo too old (%s)", strings.TrimSpace(string(output))) 712 } 713 714 gomod, err := exec.Command("go", "env", "GOMOD").Output() 715 if err != nil { 716 t.Fatalf("go env GOMOD: %v", err) 717 } 718 if len(bytes.TrimSpace(gomod)) > 0 { 719 t.Skipf("gccgo not supported in module mode; see golang.org/issue/30344") 720 } 721 } 722 723 // Build a GOPATH package into a shared library with gccgo and an executable that 724 // links against it. 725 func TestGoPathShlibGccgo(t *testing.T) { 726 requireGccgo(t) 727 728 libgoRE := regexp.MustCompile("libgo.so.[0-9]+") 729 730 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase") 731 732 // Run 'go list' after 'go install': with gccgo, we apparently don't know the 733 // shlib location until after we've installed it. 734 shlib := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase") 735 736 AssertIsLinkedToRegexp(t, shlib, libgoRE) 737 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe") 738 AssertIsLinkedToRegexp(t, "../../bin/exe", libgoRE) 739 AssertIsLinkedTo(t, "../../bin/exe", filepath.Base(shlib)) 740 AssertHasRPath(t, "../../bin/exe", filepath.Dir(shlib)) 741 // And check it runs. 742 run(t, "gccgo-built", "../../bin/exe") 743 } 744 745 // The gccgo version of TestTwoGopathShlibs: build a GOPATH package into a shared 746 // library with gccgo, another GOPATH package that depends on the first and an 747 // executable that links the second library. 748 func TestTwoGopathShlibsGccgo(t *testing.T) { 749 requireGccgo(t) 750 751 libgoRE := regexp.MustCompile("libgo.so.[0-9]+") 752 753 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./depBase") 754 goCmd(t, "install", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "./dep2") 755 goCmd(t, "install", "-compiler=gccgo", "-linkshared", "./exe2") 756 757 // Run 'go list' after 'go install': with gccgo, we apparently don't know the 758 // shlib location until after we've installed it. 759 dep2 := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./dep2") 760 depBase := goCmd(t, "list", "-compiler=gccgo", "-buildmode=shared", "-linkshared", "-f", "{{.Shlib}}", "./depBase") 761 762 AssertIsLinkedToRegexp(t, depBase, libgoRE) 763 AssertIsLinkedToRegexp(t, dep2, libgoRE) 764 AssertIsLinkedTo(t, dep2, filepath.Base(depBase)) 765 AssertIsLinkedToRegexp(t, "../../bin/exe2", libgoRE) 766 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(dep2)) 767 AssertIsLinkedTo(t, "../../bin/exe2", filepath.Base(depBase)) 768 769 // And check it runs. 770 run(t, "gccgo-built", "../../bin/exe2") 771 } 772 773 // Testing rebuilding of shared libraries when they are stale is a bit more 774 // complicated that it seems like it should be. First, we make everything "old": but 775 // only a few seconds old, or it might be older than gc (or the runtime source) and 776 // everything will get rebuilt. Then define a timestamp slightly newer than this 777 // time, which is what we set the mtime to of a file to cause it to be seen as new, 778 // and finally another slightly even newer one that we can compare files against to 779 // see if they have been rebuilt. 780 var oldTime = time.Now().Add(-9 * time.Second) 781 var nearlyNew = time.Now().Add(-6 * time.Second) 782 var stampTime = time.Now().Add(-3 * time.Second) 783 784 // resetFileStamps makes "everything" (bin, src, pkg from GOPATH and the 785 // test-specific parts of GOROOT) appear old. 786 func resetFileStamps() { 787 chtime := func(path string, info os.FileInfo, err error) error { 788 return os.Chtimes(path, oldTime, oldTime) 789 } 790 reset := func(path string) { 791 if err := filepath.Walk(path, chtime); err != nil { 792 log.Panicf("resetFileStamps failed: %v", err) 793 } 794 795 } 796 reset("../../bin") 797 reset("../../pkg") 798 reset("../../src") 799 reset(gorootInstallDir) 800 } 801 802 // touch changes path and returns a function that changes it back. 803 // It also sets the time of the file, so that we can see if it is rewritten. 804 func touch(t *testing.T, path string) (cleanup func()) { 805 t.Helper() 806 data, err := os.ReadFile(path) 807 if err != nil { 808 t.Fatal(err) 809 } 810 old := make([]byte, len(data)) 811 copy(old, data) 812 if bytes.HasPrefix(data, []byte("!<arch>\n")) { 813 // Change last digit of build ID. 814 // (Content ID in the new content-based build IDs.) 815 const marker = `build id "` 816 i := bytes.Index(data, []byte(marker)) 817 if i < 0 { 818 t.Fatal("cannot find build id in archive") 819 } 820 j := bytes.IndexByte(data[i+len(marker):], '"') 821 if j < 0 { 822 t.Fatal("cannot find build id in archive") 823 } 824 i += len(marker) + j - 1 825 if data[i] == 'a' { 826 data[i] = 'b' 827 } else { 828 data[i] = 'a' 829 } 830 } else { 831 // assume it's a text file 832 data = append(data, '\n') 833 } 834 835 // If the file is still a symlink from an overlay, delete it so that we will 836 // replace it with a regular file instead of overwriting the symlinked one. 837 fi, err := os.Lstat(path) 838 if err == nil && !fi.Mode().IsRegular() { 839 fi, err = os.Stat(path) 840 if err := os.Remove(path); err != nil { 841 t.Fatal(err) 842 } 843 } 844 if err != nil { 845 t.Fatal(err) 846 } 847 848 // If we're replacing a symlink to a read-only file, make the new file 849 // user-writable. 850 perm := fi.Mode().Perm() | 0200 851 852 if err := os.WriteFile(path, data, perm); err != nil { 853 t.Fatal(err) 854 } 855 if err := os.Chtimes(path, nearlyNew, nearlyNew); err != nil { 856 t.Fatal(err) 857 } 858 return func() { 859 if err := os.WriteFile(path, old, perm); err != nil { 860 t.Fatal(err) 861 } 862 } 863 } 864 865 // isNew returns if the path is newer than the time stamp used by touch. 866 func isNew(t *testing.T, path string) bool { 867 t.Helper() 868 fi, err := os.Stat(path) 869 if err != nil { 870 t.Fatal(err) 871 } 872 return fi.ModTime().After(stampTime) 873 } 874 875 // Fail unless path has been rebuilt (i.e. is newer than the time stamp used by 876 // isNew) 877 func AssertRebuilt(t *testing.T, msg, path string) { 878 t.Helper() 879 if !isNew(t, path) { 880 t.Errorf("%s was not rebuilt (%s)", msg, path) 881 } 882 } 883 884 // Fail if path has been rebuilt (i.e. is newer than the time stamp used by isNew) 885 func AssertNotRebuilt(t *testing.T, msg, path string) { 886 t.Helper() 887 if isNew(t, path) { 888 t.Errorf("%s was rebuilt (%s)", msg, path) 889 } 890 } 891 892 func TestRebuilding(t *testing.T) { 893 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 894 goCmd(t, "install", "-linkshared", "./exe") 895 info := strings.Fields(goCmd(t, "list", "-buildmode=shared", "-linkshared", "-f", "{{.Target}} {{.Shlib}}", "./depBase")) 896 if len(info) != 2 { 897 t.Fatalf("go list failed to report Target and/or Shlib") 898 } 899 target := info[0] 900 shlib := info[1] 901 902 // If the source is newer than both the .a file and the .so, both are rebuilt. 903 t.Run("newsource", func(t *testing.T) { 904 resetFileStamps() 905 cleanup := touch(t, "./depBase/dep.go") 906 defer func() { 907 cleanup() 908 goCmd(t, "install", "-linkshared", "./exe") 909 }() 910 goCmd(t, "install", "-linkshared", "./exe") 911 AssertRebuilt(t, "new source", target) 912 AssertRebuilt(t, "new source", shlib) 913 }) 914 915 // If the .a file is newer than the .so, the .so is rebuilt (but not the .a) 916 t.Run("newarchive", func(t *testing.T) { 917 resetFileStamps() 918 AssertNotRebuilt(t, "new .a file before build", target) 919 goCmd(t, "list", "-linkshared", "-f={{.ImportPath}} {{.Stale}} {{.StaleReason}} {{.Target}}", "./depBase") 920 AssertNotRebuilt(t, "new .a file before build", target) 921 cleanup := touch(t, target) 922 defer func() { 923 cleanup() 924 goCmd(t, "install", "-v", "-linkshared", "./exe") 925 }() 926 goCmd(t, "install", "-v", "-linkshared", "./exe") 927 AssertNotRebuilt(t, "new .a file", target) 928 AssertRebuilt(t, "new .a file", shlib) 929 }) 930 } 931 932 func appendFile(t *testing.T, path, content string) { 933 t.Helper() 934 f, err := os.OpenFile(path, os.O_WRONLY|os.O_APPEND, 0660) 935 if err != nil { 936 t.Fatalf("os.OpenFile failed: %v", err) 937 } 938 defer func() { 939 err := f.Close() 940 if err != nil { 941 t.Fatalf("f.Close failed: %v", err) 942 } 943 }() 944 _, err = f.WriteString(content) 945 if err != nil { 946 t.Fatalf("f.WriteString failed: %v", err) 947 } 948 } 949 950 func createFile(t *testing.T, path, content string) { 951 t.Helper() 952 f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644) 953 if err != nil { 954 t.Fatalf("os.OpenFile failed: %v", err) 955 } 956 _, err = f.WriteString(content) 957 if closeErr := f.Close(); err == nil { 958 err = closeErr 959 } 960 if err != nil { 961 t.Fatalf("WriteString failed: %v", err) 962 } 963 } 964 965 func TestABIChecking(t *testing.T) { 966 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 967 goCmd(t, "install", "-linkshared", "./exe") 968 969 // If we make an ABI-breaking change to depBase and rebuild libp.so but not exe, 970 // exe will abort with a complaint on startup. 971 // This assumes adding an exported function breaks ABI, which is not true in 972 // some senses but suffices for the narrow definition of ABI compatibility the 973 // toolchain uses today. 974 resetFileStamps() 975 976 createFile(t, "./depBase/break.go", "package depBase\nfunc ABIBreak() {}\n") 977 defer os.Remove("./depBase/break.go") 978 979 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 980 c := exec.Command("../../bin/exe") 981 output, err := c.CombinedOutput() 982 if err == nil { 983 t.Fatal("executing exe did not fail after ABI break") 984 } 985 scanner := bufio.NewScanner(bytes.NewReader(output)) 986 foundMsg := false 987 const wantPrefix = "abi mismatch detected between the executable and lib" 988 for scanner.Scan() { 989 if strings.HasPrefix(scanner.Text(), wantPrefix) { 990 foundMsg = true 991 break 992 } 993 } 994 if err = scanner.Err(); err != nil { 995 t.Errorf("scanner encountered error: %v", err) 996 } 997 if !foundMsg { 998 t.Fatalf("exe failed, but without line %q; got output:\n%s", wantPrefix, output) 999 } 1000 1001 // Rebuilding exe makes it work again. 1002 goCmd(t, "install", "-linkshared", "./exe") 1003 run(t, "rebuilt exe", "../../bin/exe") 1004 1005 // If we make a change which does not break ABI (such as adding an unexported 1006 // function) and rebuild libdepBase.so, exe still works, even if new function 1007 // is in a file by itself. 1008 resetFileStamps() 1009 createFile(t, "./depBase/dep2.go", "package depBase\nfunc noABIBreak() {}\n") 1010 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./depBase") 1011 run(t, "after non-ABI breaking change", "../../bin/exe") 1012 } 1013 1014 // If a package 'explicit' imports a package 'implicit', building 1015 // 'explicit' into a shared library implicitly includes implicit in 1016 // the shared library. Building an executable that imports both 1017 // explicit and implicit builds the code from implicit into the 1018 // executable rather than fetching it from the shared library. The 1019 // link still succeeds and the executable still runs though. 1020 func TestImplicitInclusion(t *testing.T) { 1021 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./explicit") 1022 goCmd(t, "install", "-linkshared", "./implicitcmd") 1023 run(t, "running executable linked against library that contains same package as it", "../../bin/implicitcmd") 1024 } 1025 1026 // Tests to make sure that the type fields of empty interfaces and itab 1027 // fields of nonempty interfaces are unique even across modules, 1028 // so that interface equality works correctly. 1029 func TestInterface(t *testing.T) { 1030 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_a") 1031 // Note: iface_i gets installed implicitly as a dependency of iface_a. 1032 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./iface_b") 1033 goCmd(t, "install", "-linkshared", "./iface") 1034 run(t, "running type/itab uniqueness tester", "../../bin/iface") 1035 } 1036 1037 // Access a global variable from a library. 1038 func TestGlobal(t *testing.T) { 1039 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./globallib") 1040 goCmd(t, "install", "-linkshared", "./global") 1041 run(t, "global executable", "../../bin/global") 1042 AssertIsLinkedTo(t, "../../bin/global", soname) 1043 AssertHasRPath(t, "../../bin/global", gorootInstallDir) 1044 } 1045 1046 // Run a test using -linkshared of an installed shared package. 1047 // Issue 26400. 1048 func TestTestInstalledShared(t *testing.T) { 1049 goCmd(t, "test", "-linkshared", "-test.short", "sync/atomic") 1050 } 1051 1052 // Test generated pointer method with -linkshared. 1053 // Issue 25065. 1054 func TestGeneratedMethod(t *testing.T) { 1055 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue25065") 1056 } 1057 1058 // Test use of shared library struct with generated hash function. 1059 // Issue 30768. 1060 func TestGeneratedHash(t *testing.T) { 1061 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue30768/issue30768lib") 1062 goCmd(t, "test", "-linkshared", "./issue30768") 1063 } 1064 1065 // Test that packages can be added not in dependency order (here a depends on b, and a adds 1066 // before b). This could happen with e.g. go build -buildmode=shared std. See issue 39777. 1067 func TestPackageOrder(t *testing.T) { 1068 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue39777/a", "./issue39777/b") 1069 } 1070 1071 // Test that GC data are generated correctly by the linker when it needs a type defined in 1072 // a shared library. See issue 39927. 1073 func TestGCData(t *testing.T) { 1074 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./gcdata/p") 1075 goCmd(t, "build", "-linkshared", "./gcdata/main") 1076 runWithEnv(t, "running gcdata/main", []string{"GODEBUG=clobberfree=1"}, "./main") 1077 } 1078 1079 // Test that we don't decode type symbols from shared libraries (which has no data, 1080 // causing panic). See issue 44031. 1081 func TestIssue44031(t *testing.T) { 1082 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/a") 1083 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue44031/b") 1084 goCmd(t, "run", "-linkshared", "./issue44031/main") 1085 } 1086 1087 // Test that we use a variable from shared libraries (which implement an 1088 // interface in shared libraries.). A weak reference is used in the itab 1089 // in main process. It can cause unreacheble panic. See issue 47873. 1090 func TestIssue47873(t *testing.T) { 1091 goCmd(t, "install", "-buildmode=shared", "-linkshared", "./issue47837/a") 1092 goCmd(t, "run", "-linkshared", "./issue47837/main") 1093 }