slightly simplify how we deal with linknamed runtime deps

Obfuscating the runtime only needs to list the linknamed packages,
and doesn't need to know about their dependencies directly.

Refactor the script to return a "flat" list that includes all packages
we need, except those that we know the runtime already pulled in.

This allows us to simplify the script and avoid passing -deps to cmd/go.
Performance is unaffected, but I reckon it's worthwhile given how much
we simplified the script.

Longer term, it's also best to avoid using -deps when we don't need it,
as cmd/go could avoid computing information we don't need.

	name              old time/op       new time/op       delta
	Build/NoCache-16        1.68s ± 1%        1.68s ± 0%    ~     (p=1.000 n=5+5)

	name              old bin-B         new bin-B         delta
	Build/NoCache-16        6.72M ± 0%        6.72M ± 0%  +0.01%  (p=0.008 n=5+5)

	name              old sys-time/op   new sys-time/op   delta
	Build/NoCache-16        1.88s ± 1%        1.89s ± 2%    ~     (p=0.548 n=5+5)

	name              old user-time/op  new user-time/op  delta
	Build/NoCache-16        19.9s ± 1%        19.8s ± 0%    ~     (p=0.421 n=5+5)
pull/472/head
Daniel Martí 2 years ago committed by lu4p
parent f7ed99aa25
commit 8652271db2

@ -455,7 +455,7 @@ This command wraps "go %s". Below is its help:
}
cache.BinaryContentID = decodeHash(splitContentID(binaryBuildID))
if err := appendListedPackages(args...); err != nil {
if err := appendListedPackages(args, true); err != nil {
return nil, err
}

@ -1,21 +1,11 @@
#!/bin/bash
# The list of all packages the runtime source linknames to.
linked="$(sed -rn 's@//go:linkname .* ([^.]*)\.[^.]*@\1@p' $(go env GOROOT)/src/runtime/*.go | grep -vE '^main|^runtime\.' | sort -u)"
# The list of all implied dependencies of the packages above,
# across all main GOOS targets.
implied="$(for GOOS in linux darwin windows js; do
for pkg in $linked; do
GOOS=$GOOS GOARCH=$GOARCH go list -e -deps $pkg | grep -v '^'$pkg'$'
done
done | sort -u)"
# All packages in linked, except those implied by others already.
# All packages that the runtime linknames to, except runtime and its dependencies.
# This resulting list is what we need to "go list" when obfuscating the runtime,
# as they are the packages that we may be missing.
comm -23 <(
echo "$linked"
sed -rn 's@//go:linkname .* ([^.]*)\.[^.]*@\1@p' $(go env GOROOT)/src/runtime/*.go | grep -vE '^main|^runtime\.' | sort -u
) <(
echo "$implied"
# Note that we assume this is constant across platforms.
go list -deps runtime | sort -u
)

@ -160,14 +160,17 @@ func (p *listedPackage) obfuscatedImportPath() string {
// appendListedPackages gets information about the current package
// and all of its dependencies
func appendListedPackages(patterns ...string) error {
func appendListedPackages(packages []string, withDeps bool) error {
startTime := time.Now()
// TODO: perhaps include all top-level build flags set by garble,
// including -buildvcs=false.
// They shouldn't affect "go list" here, but might as well be consistent.
args := []string{"list", "-json", "-deps", "-export", "-trimpath"}
args := []string{"list", "-json", "-export", "-trimpath"}
if withDeps {
args = append(args, "-deps")
}
args = append(args, cache.ForwardBuildFlags...)
args = append(args, patterns...)
args = append(args, packages...)
cmd := exec.Command("go", args...)
defer func() {
@ -260,16 +263,24 @@ func listPackage(path string) (*listedPackage, error) {
// Obtained via scripts/runtime-linknamed-nodeps.sh as of Go 1.18beta1.
runtimeLinknamed := []string{
"crypto/x509/internal/macos",
"internal/poll",
"internal/reflectlite",
"net",
"os",
"os/signal",
"plugin",
"reflect",
"runtime/debug",
"runtime/metrics",
"runtime/pprof",
"runtime/trace",
"sync",
"sync/atomic",
"syscall",
"syscall/js",
"time",
}
var missing []string
missing := make([]string, 0, len(runtimeLinknamed))
for _, linknamed := range runtimeLinknamed {
switch {
case cache.ListedPackages[linknamed] != nil:
@ -282,7 +293,8 @@ func listPackage(path string) (*listedPackage, error) {
missing = append(missing, linknamed)
}
}
if err := appendListedPackages(missing...); err != nil {
// We don't need any information about their dependencies, in this case.
if err := appendListedPackages(missing, false); err != nil {
panic(err) // should never happen
}
pkg, ok := cache.ListedPackages[path]

Loading…
Cancel
Save