[dev.boringcrypto] cmd: use notsha256 instead of md5, sha1, sha256

When we add GOEXPERIMENT=boringcrypto, the bootstrap process
will not converge if the compiler itself depends on the boringcrypto
cgo-based implementations of sha1 and sha256.

Using notsha256 avoids boringcrypto and makes bootstrap converge.
Removing md5 is not strictly necessary but it seemed worthwhile to
be consistent.

For #51940.

Change-Id: Iba649507e0964d1a49a1d16e463dd23c4e348f14
Reviewed-on: https://go-review.googlesource.com/c/go/+/402595
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
This commit is contained in:
Russ Cox 2022-04-27 09:03:35 -04:00
parent fe006d6410
commit e5407501cb
15 changed files with 47 additions and 37 deletions

View File

@ -11,7 +11,6 @@
package main package main
import ( import (
"crypto/md5"
"flag" "flag"
"fmt" "fmt"
"go/ast" "go/ast"
@ -28,6 +27,7 @@ import (
"strings" "strings"
"cmd/internal/edit" "cmd/internal/edit"
"cmd/internal/notsha256"
"cmd/internal/objabi" "cmd/internal/objabi"
) )
@ -329,8 +329,8 @@ func main() {
// we use to coordinate between gcc and ourselves. // we use to coordinate between gcc and ourselves.
// We already put _cgo_ at the beginning, so the main // We already put _cgo_ at the beginning, so the main
// concern is other cgo wrappers for the same functions. // concern is other cgo wrappers for the same functions.
// Use the beginning of the md5 of the input to disambiguate. // Use the beginning of the notsha256 of the input to disambiguate.
h := md5.New() h := notsha256.New()
io.WriteString(h, *importPath) io.WriteString(h, *importPath)
fs := make([]*File, len(goFiles)) fs := make([]*File, len(goFiles))
for i, input := range goFiles { for i, input := range goFiles {

View File

@ -15,7 +15,6 @@
package liveness package liveness
import ( import (
"crypto/sha1"
"fmt" "fmt"
"os" "os"
"sort" "sort"
@ -30,6 +29,7 @@ import (
"cmd/compile/internal/ssa" "cmd/compile/internal/ssa"
"cmd/compile/internal/typebits" "cmd/compile/internal/typebits"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/notsha256"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/src" "cmd/internal/src"
@ -959,7 +959,7 @@ func (lv *liveness) enableClobber() {
// Clobber only functions where the hash of the function name matches a pattern. // Clobber only functions where the hash of the function name matches a pattern.
// Useful for binary searching for a miscompiled function. // Useful for binary searching for a miscompiled function.
hstr := "" hstr := ""
for _, b := range sha1.Sum([]byte(lv.f.Name)) { for _, b := range notsha256.Sum256([]byte(lv.f.Name)) {
hstr += fmt.Sprintf("%08b", b) hstr += fmt.Sprintf("%08b", b)
} }
if !strings.HasSuffix(hstr, h) { if !strings.HasSuffix(hstr, h) {

View File

@ -8,8 +8,8 @@ import (
"cmd/compile/internal/abi" "cmd/compile/internal/abi"
"cmd/compile/internal/base" "cmd/compile/internal/base"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/notsha256"
"cmd/internal/src" "cmd/internal/src"
"crypto/sha1"
"fmt" "fmt"
"io" "io"
"math" "math"
@ -854,7 +854,7 @@ func (f *Func) DebugHashMatch(evname string) bool {
// We use this feature to do a binary search to // We use this feature to do a binary search to
// find a function that is incorrectly compiled. // find a function that is incorrectly compiled.
hstr := "" hstr := ""
for _, b := range sha1.Sum([]byte(name)) { for _, b := range notsha256.Sum256([]byte(name)) {
hstr += fmt.Sprintf("%08b", b) hstr += fmt.Sprintf("%08b", b)
} }

View File

@ -6,10 +6,11 @@ package ssa
import ( import (
"bytes" "bytes"
"cmd/internal/src"
"crypto/sha256"
"fmt" "fmt"
"io" "io"
"cmd/internal/notsha256"
"cmd/internal/src"
) )
func printFunc(f *Func) { func printFunc(f *Func) {
@ -17,7 +18,7 @@ func printFunc(f *Func) {
} }
func hashFunc(f *Func) []byte { func hashFunc(f *Func) []byte {
h := sha256.New() h := notsha256.New()
p := stringFuncPrinter{w: h, printDead: true} p := stringFuncPrinter{w: h, printDead: true}
fprintFunc(p, f) fprintFunc(p, f)
return h.Sum(nil) return h.Sum(nil)
@ -32,7 +33,7 @@ func (f *Func) String() string {
// rewriteHash returns a hash of f suitable for detecting rewrite cycles. // rewriteHash returns a hash of f suitable for detecting rewrite cycles.
func (f *Func) rewriteHash() string { func (f *Func) rewriteHash() string {
h := sha256.New() h := notsha256.New()
p := stringFuncPrinter{w: h, printDead: false} p := stringFuncPrinter{w: h, printDead: false}
fprintFunc(p, f) fprintFunc(p, f)
return fmt.Sprintf("%x", h.Sum(nil)) return fmt.Sprintf("%x", h.Sum(nil))

View File

@ -5,7 +5,6 @@
package staticdata package staticdata
import ( import (
"crypto/sha256"
"fmt" "fmt"
"go/constant" "go/constant"
"io" "io"
@ -20,6 +19,7 @@ import (
"cmd/compile/internal/objw" "cmd/compile/internal/objw"
"cmd/compile/internal/typecheck" "cmd/compile/internal/typecheck"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/notsha256"
"cmd/internal/obj" "cmd/internal/obj"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/src" "cmd/internal/src"
@ -73,7 +73,7 @@ func StringSym(pos src.XPos, s string) (data *obj.LSym) {
// Indulge in some paranoia by writing the length of s, too, // Indulge in some paranoia by writing the length of s, too,
// as protection against length extension attacks. // as protection against length extension attacks.
// Same pattern is known to fileStringSym below. // Same pattern is known to fileStringSym below.
h := sha256.New() h := notsha256.New()
io.WriteString(h, s) io.WriteString(h, s)
symname = fmt.Sprintf(stringSymPattern, len(s), h.Sum(nil)) symname = fmt.Sprintf(stringSymPattern, len(s), h.Sum(nil))
} else { } else {
@ -131,7 +131,7 @@ func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.
sym = slicedata(pos, string(data)).Linksym() sym = slicedata(pos, string(data)).Linksym()
} }
if len(hash) > 0 { if len(hash) > 0 {
sum := sha256.Sum256(data) sum := notsha256.Sum256(data)
copy(hash, sum[:]) copy(hash, sum[:])
} }
return sym, size, nil return sym, size, nil
@ -148,7 +148,7 @@ func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.
// Compute hash if needed for read-only content hashing or if the caller wants it. // Compute hash if needed for read-only content hashing or if the caller wants it.
var sum []byte var sum []byte
if readonly || len(hash) > 0 { if readonly || len(hash) > 0 {
h := sha256.New() h := notsha256.New()
n, err := io.Copy(h, f) n, err := io.Copy(h, f)
if err != nil { if err != nil {
return nil, 0, err return nil, 0, err

View File

@ -236,7 +236,6 @@ package typecheck
import ( import (
"bytes" "bytes"
"crypto/md5"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"go/constant" "go/constant"
@ -250,6 +249,7 @@ import (
"cmd/compile/internal/ir" "cmd/compile/internal/ir"
"cmd/compile/internal/types" "cmd/compile/internal/types"
"cmd/internal/goobj" "cmd/internal/goobj"
"cmd/internal/notsha256"
"cmd/internal/src" "cmd/internal/src"
) )
@ -353,7 +353,7 @@ func WriteExports(out io.Writer, extensions bool) {
hdr.uint64(dataLen) hdr.uint64(dataLen)
// Flush output. // Flush output.
h := md5.New() h := notsha256.New()
wr := io.MultiWriter(out, h) wr := io.MultiWriter(out, h)
io.Copy(wr, &hdr) io.Copy(wr, &hdr)
io.Copy(wr, &p.strings) io.Copy(wr, &p.strings)

View File

@ -6,7 +6,6 @@ package types
import ( import (
"bytes" "bytes"
"crypto/md5"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"go/constant" "go/constant"
@ -15,6 +14,7 @@ import (
"sync" "sync"
"cmd/compile/internal/base" "cmd/compile/internal/base"
"cmd/internal/notsha256"
) )
// BuiltinPkg is a fake package that declares the universe block. // BuiltinPkg is a fake package that declares the universe block.
@ -771,7 +771,7 @@ func FmtConst(v constant.Value, sharp bool) string {
func TypeHash(t *Type) uint32 { func TypeHash(t *Type) uint32 {
p := tconv(t, 0, fmtTypeIDHash) p := tconv(t, 0, fmtTypeIDHash)
// Using MD5 is overkill, but reduces accidental collisions. // Using SHA256 is overkill, but reduces accidental collisions.
h := md5.Sum([]byte(p)) h := notsha256.Sum256([]byte(p))
return binary.LittleEndian.Uint32(h[:4]) return binary.LittleEndian.Uint32(h[:4])
} }

View File

@ -44,6 +44,7 @@ var bootstrapDirs = []string{
"cmd/internal/edit", "cmd/internal/edit",
"cmd/internal/gcprog", "cmd/internal/gcprog",
"cmd/internal/goobj", "cmd/internal/goobj",
"cmd/internal/notsha256",
"cmd/internal/obj/...", "cmd/internal/obj/...",
"cmd/internal/objabi", "cmd/internal/objabi",
"cmd/internal/pkgpath", "cmd/internal/pkgpath",

View File

@ -11,10 +11,11 @@
package codesign package codesign
import ( import (
"crypto/sha256"
"debug/macho" "debug/macho"
"encoding/binary" "encoding/binary"
"io" "io"
"cmd/internal/notsha256"
) )
// Code signature layout. // Code signature layout.
@ -190,7 +191,7 @@ func Size(codeSize int64, id string) int64 {
nhashes := (codeSize + pageSize - 1) / pageSize nhashes := (codeSize + pageSize - 1) / pageSize
idOff := int64(codeDirectorySize) idOff := int64(codeDirectorySize)
hashOff := idOff + int64(len(id)+1) hashOff := idOff + int64(len(id)+1)
cdirSz := hashOff + nhashes*sha256.Size cdirSz := hashOff + nhashes*notsha256.Size
return int64(superBlobSize+blobSize) + cdirSz return int64(superBlobSize+blobSize) + cdirSz
} }
@ -226,7 +227,7 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
identOffset: uint32(idOff), identOffset: uint32(idOff),
nCodeSlots: uint32(nhashes), nCodeSlots: uint32(nhashes),
codeLimit: uint32(codeSize), codeLimit: uint32(codeSize),
hashSize: sha256.Size, hashSize: notsha256.Size,
hashType: CS_HASHTYPE_SHA256, hashType: CS_HASHTYPE_SHA256,
pageSize: uint8(pageSizeBits), pageSize: uint8(pageSizeBits),
execSegBase: uint64(textOff), execSegBase: uint64(textOff),
@ -245,8 +246,12 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
outp = puts(outp, []byte(id+"\000")) outp = puts(outp, []byte(id+"\000"))
// emit hashes // emit hashes
// NOTE(rsc): These must be SHA256, but for cgo bootstrap reasons
// we cannot import crypto/sha256 when GOEXPERIMENT=boringcrypto
// and the host is linux/amd64. So we use NOT-SHA256
// and then apply a NOT ourselves to get SHA256. Sigh.
var buf [pageSize]byte var buf [pageSize]byte
h := sha256.New() h := notsha256.New()
p := 0 p := 0
for p < int(codeSize) { for p < int(codeSize) {
n, err := io.ReadFull(data, buf[:]) n, err := io.ReadFull(data, buf[:])
@ -263,6 +268,9 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
h.Reset() h.Reset()
h.Write(buf[:n]) h.Write(buf[:n])
b := h.Sum(nil) b := h.Sum(nil)
for i := range b {
b[i] ^= 0xFF // convert notsha256 to sha256
}
outp = puts(outp, b[:]) outp = puts(outp, b[:])
} }
} }

View File

@ -20,7 +20,7 @@ package goobj
import ( import (
"cmd/internal/bio" "cmd/internal/bio"
"crypto/sha1" "cmd/internal/notsha256"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
@ -367,7 +367,7 @@ const Hash64Size = 8
// Hash // Hash
type HashType [HashSize]byte type HashType [HashSize]byte
const HashSize = sha1.Size const HashSize = notsha256.Size
// Relocation. // Relocation.
// //

View File

@ -10,9 +10,9 @@ import (
"bytes" "bytes"
"cmd/internal/bio" "cmd/internal/bio"
"cmd/internal/goobj" "cmd/internal/goobj"
"cmd/internal/notsha256"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/sys" "cmd/internal/sys"
"crypto/sha1"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"io" "io"
@ -460,7 +460,7 @@ func contentHash64(s *LSym) goobj.Hash64Type {
// For now, we assume there is no circular dependencies among // For now, we assume there is no circular dependencies among
// hashed symbols. // hashed symbols.
func (w *writer) contentHash(s *LSym) goobj.HashType { func (w *writer) contentHash(s *LSym) goobj.HashType {
h := sha1.New() h := notsha256.New()
var tmp [14]byte var tmp [14]byte
// Include the size of the symbol in the hash. // Include the size of the symbol in the hash.

View File

@ -33,8 +33,8 @@ package obj
import ( import (
"cmd/internal/goobj" "cmd/internal/goobj"
"cmd/internal/notsha256"
"cmd/internal/objabi" "cmd/internal/objabi"
"crypto/md5"
"fmt" "fmt"
"internal/buildcfg" "internal/buildcfg"
"log" "log"
@ -175,7 +175,7 @@ func (ctxt *Link) Int64Sym(i int64) *LSym {
// GCLocalsSym generates a content-addressable sym containing data. // GCLocalsSym generates a content-addressable sym containing data.
func (ctxt *Link) GCLocalsSym(data []byte) *LSym { func (ctxt *Link) GCLocalsSym(data []byte) *LSym {
return ctxt.LookupInit(fmt.Sprintf("gclocals·%x", md5.Sum(data)), func(lsym *LSym) { return ctxt.LookupInit(fmt.Sprintf("gclocals·%x", notsha256.Sum256(data)), func(lsym *LSym) {
lsym.P = data lsym.P = data
lsym.Set(AttrContentAddressable, true) lsym.Set(AttrContentAddressable, true)
}) })

View File

@ -5,11 +5,11 @@
package ld package ld
import ( import (
"cmd/internal/notsha256"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/sys" "cmd/internal/sys"
"cmd/link/internal/loader" "cmd/link/internal/loader"
"cmd/link/internal/sym" "cmd/link/internal/sym"
"crypto/sha1"
"debug/elf" "debug/elf"
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
@ -1533,10 +1533,10 @@ func (ctxt *Link) doelf() {
sb.SetType(sym.SRODATA) sb.SetType(sym.SRODATA)
ldr.SetAttrSpecial(s, true) ldr.SetAttrSpecial(s, true)
sb.SetReachable(true) sb.SetReachable(true)
sb.SetSize(sha1.Size) sb.SetSize(notsha256.Size)
sort.Sort(byPkg(ctxt.Library)) sort.Sort(byPkg(ctxt.Library))
h := sha1.New() h := notsha256.New()
for _, l := range ctxt.Library { for _, l := range ctxt.Library {
h.Write(l.Fingerprint[:]) h.Write(l.Fingerprint[:])
} }

View File

@ -34,6 +34,7 @@ import (
"bytes" "bytes"
"cmd/internal/bio" "cmd/internal/bio"
"cmd/internal/goobj" "cmd/internal/goobj"
"cmd/internal/notsha256"
"cmd/internal/objabi" "cmd/internal/objabi"
"cmd/internal/sys" "cmd/internal/sys"
"cmd/link/internal/loadelf" "cmd/link/internal/loadelf"
@ -42,7 +43,6 @@ import (
"cmd/link/internal/loadpe" "cmd/link/internal/loadpe"
"cmd/link/internal/loadxcoff" "cmd/link/internal/loadxcoff"
"cmd/link/internal/sym" "cmd/link/internal/sym"
"crypto/sha1"
"debug/elf" "debug/elf"
"debug/macho" "debug/macho"
"encoding/base64" "encoding/base64"
@ -929,7 +929,7 @@ func typeSymbolMangle(name string) string {
if len(name) <= 14 && !strings.Contains(name, "@") { // Issue 19529 if len(name) <= 14 && !strings.Contains(name, "@") { // Issue 19529
return name return name
} }
hash := sha1.Sum([]byte(name)) hash := notsha256.Sum256([]byte(name))
prefix := "type." prefix := "type."
if name[5] == '.' { if name[5] == '.' {
prefix = "type.." prefix = "type.."

View File

@ -5,7 +5,7 @@
package main package main
import ( import (
"crypto/md5" "cmd/internal/notsha256"
"flag" "flag"
"fmt" "fmt"
"go/build" "go/build"
@ -142,7 +142,7 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
goarch = f[1] goarch = f[1]
} }
hash := md5.Sum([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm))) hash := notsha256.Sum256([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash)) hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
args := []string{"build", "-o", hello} args := []string{"build", "-o", hello}
args = append(args, flags...) args = append(args, flags...)