]> Cypherpunks.ru repositories - gostls13.git/commitdiff
internal/pkgbits: add fingerprints to unified IR export format
authorMatthew Dempsky <mdempsky@google.com>
Wed, 23 Mar 2022 19:10:02 +0000 (12:10 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 28 Mar 2022 19:58:45 +0000 (19:58 +0000)
So far unified IR has been relying on the backwards-compat iexport
data to supply package fingerprints for imports. To be able to drop
the iexport data and natively use unified IR everywhere.

This CL applies basically the same idea that iexport used: simply
hash all of the export data as it's being written out, and then tack
on an 8-byte hash at the end.

Change-Id: Iaca5fbfd7443088bc7f422a1c58be3e762c29014
Reviewed-on: https://go-review.googlesource.com/c/go/+/396196
Trust: Matthew Dempsky <mdempsky@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cherry Mui <cherryyz@google.com>
src/go/build/deps_test.go
src/internal/pkgbits/decoder.go
src/internal/pkgbits/encoder.go

index d541f0926dedf2850b5162442307a46427a1339c..5794e3d25e9db50b3230996f61145348b2a1fc94 100644 (file)
@@ -310,11 +310,6 @@ var depsRules = `
        go/build/constraint, go/doc, go/parser, internal/buildcfg, internal/goroot, internal/goversion
        < go/build;
 
-       DEBUG, go/build, go/types, text/scanner
-  < internal/pkgbits
-       < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
-       < go/importer;
-
        # databases
        FMT
        < database/sql/internal
@@ -449,6 +444,11 @@ var depsRules = `
 
        # crypto-aware packages
 
+       CRYPTO, DEBUG, go/build, go/types, text/scanner
+       < internal/pkgbits
+       < go/internal/gcimporter, go/internal/gccgoimporter, go/internal/srcimporter
+       < go/importer;
+
        NET, crypto/rand, mime/quotedprintable
        < mime/multipart;
 
index 9c8ad446ca4d36590ea18e7daddde474780cab9d..5b4e8f69aff4058346e4e9e44e633e25bfd9f197 100644 (file)
@@ -22,7 +22,7 @@ type PkgDecoder struct {
 
        elemEndsEnds [numRelocs]uint32
        elemEnds     []uint32
-       elemData     string
+       elemData     string // last 8 bytes are fingerprint
 }
 
 func (pr *PkgDecoder) PkgPath() string { return pr.pkgPath }
@@ -50,7 +50,7 @@ func NewPkgDecoder(pkgPath, input string) PkgDecoder {
        assert(err == nil)
 
        pr.elemData = input[pos:]
-       assert(len(pr.elemData) == int(pr.elemEnds[len(pr.elemEnds)-1]))
+       assert(len(pr.elemData)-8 == int(pr.elemEnds[len(pr.elemEnds)-1]))
 
        return pr
 }
@@ -67,6 +67,12 @@ func (pr *PkgDecoder) TotalElems() int {
        return len(pr.elemEnds)
 }
 
+func (pr *PkgDecoder) Fingerprint() [8]byte {
+       var fp [8]byte
+       copy(fp[:], pr.elemData[len(pr.elemData)-8:])
+       return fp
+}
+
 func (pr *PkgDecoder) AbsIdx(k RelocKind, idx int) int {
        absIdx := idx
        if k > 0 {
index 820c707940abc359283b97e9a45f3211a31d2e43..4780f01c392d46f300b534a53f0d17a06e40e572 100644 (file)
@@ -8,6 +8,7 @@ package pkgbits
 
 import (
        "bytes"
+       "crypto/md5"
        "encoding/binary"
        "go/constant"
        "io"
@@ -30,7 +31,10 @@ func NewPkgEncoder(syncFrames int) PkgEncoder {
        }
 }
 
-func (pw *PkgEncoder) DumpTo(out io.Writer) {
+func (pw *PkgEncoder) DumpTo(out0 io.Writer) (fingerprint [8]byte) {
+       h := md5.New()
+       out := io.MultiWriter(out0, h)
+
        writeUint32 := func(x uint32) {
                assert(binary.Write(out, binary.LittleEndian, x) == nil)
        }
@@ -57,6 +61,12 @@ func (pw *PkgEncoder) DumpTo(out io.Writer) {
                        assert(err == nil)
                }
        }
+
+       copy(fingerprint[:], h.Sum(nil))
+       _, err := out0.Write(fingerprint[:])
+       assert(err == nil)
+
+       return
 }
 
 func (pw *PkgEncoder) StringIdx(s string) int {