]> Cypherpunks repositories - gostls13.git/commitdiff
cmd: use cmd/internal/hash.New32 and Sum32 only
authorRuss Cox <rsc@golang.org>
Tue, 7 Jan 2025 16:28:44 +0000 (11:28 -0500)
committerGopher Robot <gobot@golang.org>
Thu, 13 Feb 2025 20:34:30 +0000 (12:34 -0800)
Do not use New16, New20, Sum16, Sum20 anymore.
As of CL 641096, these are just wrappers around New32 and Sum32.
Change call sites to use them directly.

Change-Id: Icea91a77449f6839b903894997057ba404bd04e0
Reviewed-on: https://go-review.googlesource.com/c/go/+/641076
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Auto-Submit: Russ Cox <rsc@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
src/cmd/cgo/main.go
src/cmd/compile/internal/liveness/plive.go
src/cmd/compile/internal/types/fmt.go
src/cmd/internal/hash/hash.go
src/cmd/internal/obj/objfile.go
src/cmd/internal/obj/sym.go
src/cmd/link/internal/ld/elf.go
src/cmd/link/internal/ld/lib.go
src/cmd/objdump/objdump_test.go

index 939e282ff069ed02ba8f3b11652573bd848ed8b4..77beb0992cf74f1fa592ba7661f7c512649f5da2 100644 (file)
@@ -390,7 +390,7 @@ func main() {
        // We already put _cgo_ at the beginning, so the main
        // concern is other cgo wrappers for the same functions.
        // Use the beginning of the 16 bytes hash of the input to disambiguate.
-       h := hash.New16()
+       h := hash.New32()
        io.WriteString(h, *importPath)
        var once sync.Once
        var wg sync.WaitGroup
index ac0c2dff0a96c24c0650a898b773c666e44ac638..6c97858cf6efbb2ccfc616cd05b23856740bdf7c 100644 (file)
@@ -981,7 +981,7 @@ func (lv *Liveness) enableClobber() {
                // Clobber only functions where the hash of the function name matches a pattern.
                // Useful for binary searching for a miscompiled function.
                hstr := ""
-               for _, b := range hash.Sum20([]byte(lv.f.Name)) {
+               for _, b := range hash.Sum32([]byte(lv.f.Name)) {
                        hstr += fmt.Sprintf("%08b", b)
                }
                if !strings.HasSuffix(hstr, h) {
index 0dba510ac44e208d454797e984b317e5287fdee7..139defafe20b4c12ef457acfd6a81f1105fddeaa 100644 (file)
@@ -646,7 +646,7 @@ func SplitVargenSuffix(name string) (base, suffix string) {
 func TypeHash(t *Type) uint32 {
        p := t.LinkString()
 
-       // Using 16 bytes hash is overkill, but reduces accidental collisions.
-       h := hash.Sum16([]byte(p))
+       // Using a cryptographic hash is overkill but minimizes accidental collisions.
+       h := hash.Sum32([]byte(p))
        return binary.LittleEndian.Uint32(h[:4])
 }
index a37368f50e74b9f7883ea5798e4bdf3ddd8f0c3f..cdb24a2645ccd54294a7f9b6fab9b927166e5d21 100644 (file)
@@ -5,69 +5,26 @@
 // Package hash implements hash functions used in the compiler toolchain.
 package hash
 
-// TODO(rsc): Delete the 16 and 20 forms and use 32 at all call sites.
-
 import (
        "crypto/sha256"
        "hash"
 )
 
-const (
-       // Size32 is the size of the 32-byte hash checksum.
-       Size32 = 32
-       // Size20 is the size of the 20-byte hash checksum.
-       Size20 = 20
-       // Size16 is the size of the 16-byte hash checksum.
-       Size16 = 16
-)
-
-type shortHash struct {
-       hash.Hash
-       n int
-}
+// Size32 is the size of the 32-byte hash functions [New32] and [Sum32].
+const Size32 = 32
 
-func (h *shortHash) Sum(b []byte) []byte {
-       old := b
-       sum := h.Hash.Sum(b)
-       return sum[:len(old)+h.n]
-}
-
-// New32 returns a new [hash.Hash] computing the 32 bytes hash checksum.
+// New32 returns a new [hash.Hash] computing the 32-byte hash checksum.
+// Note that New32 and [Sum32] compute different hashes.
 func New32() hash.Hash {
        h := sha256.New()
        _, _ = h.Write([]byte{1}) // make this hash different from sha256
        return h
 }
 
-// New20 returns a new [hash.Hash] computing the 20 bytes hash checksum.
-func New20() hash.Hash {
-       return &shortHash{New32(), 20}
-}
-
-// New16 returns a new [hash.Hash] computing the 16 bytes hash checksum.
-func New16() hash.Hash {
-       return &shortHash{New32(), 16}
-}
-
-// Sum32 returns the 32 bytes checksum of the data.
-func Sum32(data []byte) [Size32]byte {
+// Sum32 returns a 32-byte checksum of the data.
+// Note that Sum32 and [New32] compute different hashes.
+func Sum32(data []byte) [32]byte {
        sum := sha256.Sum256(data)
-       sum[0] ^= 1 // make this hash different from sha256
+       sum[0] ^= 0xff // make this hash different from sha256
        return sum
 }
-
-// Sum20 returns the 20 bytes checksum of the data.
-func Sum20(data []byte) [Size20]byte {
-       sum := Sum32(data)
-       var short [Size20]byte
-       copy(short[:], sum[4:])
-       return short
-}
-
-// Sum16 returns the 16 bytes checksum of the data.
-func Sum16(data []byte) [Size16]byte {
-       sum := Sum32(data)
-       var short [Size16]byte
-       copy(short[:], sum[8:])
-       return short
-}
index bc22765abcd91aa7e3c5b9c61395c9b879c78e87..3299fbf4e6e1b0484b79dfed763f9d77ff588788 100644 (file)
@@ -494,7 +494,7 @@ func contentHash64(s *LSym) goobj.Hash64Type {
 // For now, we assume there is no circular dependencies among
 // hashed symbols.
 func (w *writer) contentHash(s *LSym) goobj.HashType {
-       h := hash.New20()
+       h := hash.New32()
        var tmp [14]byte
 
        // Include the size of the symbol in the hash.
index 887257905035fb7ae814c3c7ef28687ff231327c..08c50ec72b6a5c49222b72cfb8e8f966d2c3a2a0 100644 (file)
@@ -216,7 +216,7 @@ func (ctxt *Link) Int128Sym(hi, lo int64) *LSym {
 
 // GCLocalsSym generates a content-addressable sym containing data.
 func (ctxt *Link) GCLocalsSym(data []byte) *LSym {
-       sum := hash.Sum16(data)
+       sum := hash.Sum32(data)
        str := base64.StdEncoding.EncodeToString(sum[:16])
        return ctxt.LookupInit(fmt.Sprintf("gclocals·%s", str), func(lsym *LSym) {
                lsym.P = data
index e6a525198fae25f31285abbc6153ea9b041bfaa6..6ff1d9438337836c166d2ee8faa8e234ffa4b225 100644 (file)
@@ -1690,11 +1690,11 @@ func (ctxt *Link) doelf() {
                sb.SetType(sym.SRODATA)
                ldr.SetAttrSpecial(s, true)
                sb.SetReachable(true)
-               sb.SetSize(hash.Size20)
+               sb.SetSize(hash.Size32)
                slices.SortFunc(ctxt.Library, func(a, b *sym.Library) int {
                        return strings.Compare(a.Pkg, b.Pkg)
                })
-               h := hash.New20()
+               h := hash.New32()
                for _, l := range ctxt.Library {
                        h.Write(l.Fingerprint[:])
                }
index 2d8f964f3594c6d8ae66fd6a91024dd41315cb70..b114ca2a3d4115a0767e166a8839ccac60856584 100644 (file)
@@ -1022,7 +1022,7 @@ func typeSymbolMangle(name string) string {
                return name
        }
        if isType {
-               hb := hash.Sum20([]byte(name[5:]))
+               hb := hash.Sum32([]byte(name[5:]))
                prefix := "type:"
                if name[5] == '.' {
                        prefix = "type:."
@@ -1035,7 +1035,7 @@ func typeSymbolMangle(name string) string {
        if j == -1 || j <= i {
                j = len(name)
        }
-       hb := hash.Sum20([]byte(name[i+1 : j]))
+       hb := hash.Sum32([]byte(name[i+1 : j]))
        return name[:i+1] + base64.StdEncoding.EncodeToString(hb[:6]) + name[j:]
 }
 
index 0f3a183c612d1c2e8d76cdd90e781611ec521f1e..0d6f608a3f831344493724047d9268846b3a1de0 100644 (file)
@@ -134,9 +134,9 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
                goarch = f[1]
        }
 
-       hash := hash.Sum16([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
+       hash := hash.Sum32([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
        tmp := t.TempDir()
-       hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
+       hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash[:16]))
        args := []string{"build", "-o", hello}
        args = append(args, flags...)
        args = append(args, srcfname)