don't use the current directory to patch and build cmd/link

We were noticing sporadic `go test` failures where running
an obfuscated binary could panic with:

    fatal error: invalid function symbol table

It turns out that this could happen when modinfo.txtar runs first;
when it patched the linker with `git -C apply`, its current directory
had a valid git repository initialized, and apparently that somehow
prevents the patches from being applied.

It's unclear whether this is git's fault or our own, but in any case,
using a temporary working directory is easier and fixes the bug.
Do the same for `go build`, just in case.
pull/674/head
pagran 1 year ago committed by GitHub
parent 99d30c033e
commit f7bde1d40e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -119,7 +119,8 @@ func applyPatches(srcDir, workingDir string, modFiles map[string]bool, patches [
}
}
cmd := exec.Command("git", "-C", workingDir, "apply")
cmd := exec.Command("git", "apply")
cmd.Dir = workingDir
cmd.Stdin = bytes.NewReader(bytes.Join(patches, []byte("\n")))
if err := cmd.Run(); err != nil {
return nil, err
@ -187,6 +188,8 @@ func buildLinker(workingDir string, overlay map[string]string, outputLinkPath st
cmd := exec.Command("go", "build", "-overlay", overlayPath, "-o", outputLinkPath, "cmd/link")
// Explicitly setting GOOS and GOARCH variables prevents conflicts during cross-build
cmd.Env = append(os.Environ(), "GOOS="+runtime.GOOS, "GOARCH="+runtime.GOARCH)
// Building cmd/link is possible from anywhere, but to avoid any possible side effects build in a temp directory
cmd.Dir = workingDir
out, err := cmd.CombinedOutput()
if err != nil {

Loading…
Cancel
Save