Skip to content

Commit 4fd73e5

Browse files
cuonglmgopherbot
authored andcommitted
cmd: do not use notsha256
CL 402595 used notsha256 to prevent the compiler from depending on cgo-based implementations of sha1 and sha256. However, since CL 454836, cmd is built with CGO_ENABLED=0, which will disable boringcrypto. Thus all usages of notsha256 is not necessary anymore. Updates #51940 Updates #64751 Change-Id: I503090f7a2efb5723e8a79523b143dc7cdb4edd0 Reviewed-on: https://go-review.googlesource.com/c/go/+/610596 Auto-Submit: Cuong Manh Le <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Keith Randall <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Keith Randall <[email protected]>
1 parent ad8b5f7 commit 4fd73e5

File tree

15 files changed

+49
-57
lines changed

15 files changed

+49
-57
lines changed

src/cmd/cgo/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727
"sync"
2828

2929
"cmd/internal/edit"
30-
"cmd/internal/notsha256"
30+
"cmd/internal/hash"
3131
"cmd/internal/objabi"
3232
"cmd/internal/telemetry/counter"
3333
)
@@ -388,8 +388,8 @@ func main() {
388388
// we use to coordinate between gcc and ourselves.
389389
// We already put _cgo_ at the beginning, so the main
390390
// concern is other cgo wrappers for the same functions.
391-
// Use the beginning of the notsha256 of the input to disambiguate.
392-
h := notsha256.New()
391+
// Use the beginning of the 32 bytes hash of the input to disambiguate.
392+
h := hash.New32()
393393
io.WriteString(h, *importPath)
394394
var once sync.Once
395395
var wg sync.WaitGroup

src/cmd/compile/internal/liveness/plive.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"cmd/compile/internal/ssa"
3030
"cmd/compile/internal/typebits"
3131
"cmd/compile/internal/types"
32-
"cmd/internal/notsha256"
32+
"cmd/internal/hash"
3333
"cmd/internal/obj"
3434
"cmd/internal/src"
3535

@@ -979,7 +979,7 @@ func (lv *liveness) enableClobber() {
979979
// Clobber only functions where the hash of the function name matches a pattern.
980980
// Useful for binary searching for a miscompiled function.
981981
hstr := ""
982-
for _, b := range notsha256.Sum256([]byte(lv.f.Name)) {
982+
for _, b := range hash.Sum32([]byte(lv.f.Name)) {
983983
hstr += fmt.Sprintf("%08b", b)
984984
}
985985
if !strings.HasSuffix(hstr, h) {

src/cmd/compile/internal/noder/reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
"cmd/compile/internal/staticinit"
2424
"cmd/compile/internal/typecheck"
2525
"cmd/compile/internal/types"
26-
"cmd/internal/notsha256"
26+
"cmd/internal/hash"
2727
"cmd/internal/obj"
2828
"cmd/internal/objabi"
2929
"cmd/internal/src"
@@ -940,7 +940,7 @@ func shapify(targ *types.Type, basic bool) *types.Type {
940940
uls := under.LinkString()
941941
if base.Debug.MaxShapeLen != 0 &&
942942
len(uls) > base.Debug.MaxShapeLen {
943-
h := notsha256.Sum256([]byte(uls))
943+
h := hash.Sum32([]byte(uls))
944944
uls = hex.EncodeToString(h[:])
945945
}
946946

src/cmd/compile/internal/ssa/print.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"io"
1010
"strings"
1111

12-
"cmd/internal/notsha256"
12+
"cmd/internal/hash"
1313
"cmd/internal/src"
1414
)
1515

@@ -18,7 +18,7 @@ func printFunc(f *Func) {
1818
}
1919

2020
func hashFunc(f *Func) []byte {
21-
h := notsha256.New()
21+
h := hash.New32()
2222
p := stringFuncPrinter{w: h, printDead: true}
2323
fprintFunc(p, f)
2424
return h.Sum(nil)
@@ -33,7 +33,7 @@ func (f *Func) String() string {
3333

3434
// rewriteHash returns a hash of f suitable for detecting rewrite cycles.
3535
func (f *Func) rewriteHash() string {
36-
h := notsha256.New()
36+
h := hash.New32()
3737
p := stringFuncPrinter{w: h, printDead: false}
3838
fprintFunc(p, f)
3939
return fmt.Sprintf("%x", h.Sum(nil))

src/cmd/compile/internal/staticdata/data.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"cmd/compile/internal/ir"
1919
"cmd/compile/internal/objw"
2020
"cmd/compile/internal/types"
21-
"cmd/internal/notsha256"
21+
"cmd/internal/hash"
2222
"cmd/internal/obj"
2323
"cmd/internal/objabi"
2424
"cmd/internal/src"
@@ -78,7 +78,7 @@ func StringSym(pos src.XPos, s string) (data *obj.LSym) {
7878
// Indulge in some paranoia by writing the length of s, too,
7979
// as protection against length extension attacks.
8080
// Same pattern is known to fileStringSym below.
81-
h := notsha256.New()
81+
h := hash.New32()
8282
io.WriteString(h, s)
8383
symname = fmt.Sprintf(stringSymPattern, len(s), shortHashString(h.Sum(nil)))
8484
} else {
@@ -115,9 +115,9 @@ const maxFileSize = int64(2e9)
115115
// or other file with the same content and is placed in a read-only section.
116116
// If readonly is false, the symbol is a read-write copy separate from any other,
117117
// for use as the backing store of a []byte.
118-
// The content hash of file is copied into hash. (If hash is nil, nothing is copied.)
118+
// The content hash of file is copied into hashBytes. (If hash is nil, nothing is copied.)
119119
// The returned symbol contains the data itself, not a string header.
120-
func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.LSym, int64, error) {
120+
func fileStringSym(pos src.XPos, file string, readonly bool, hashBytes []byte) (*obj.LSym, int64, error) {
121121
f, err := os.Open(file)
122122
if err != nil {
123123
return nil, 0, err
@@ -145,9 +145,9 @@ func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.
145145
} else {
146146
sym = slicedata(pos, string(data))
147147
}
148-
if len(hash) > 0 {
149-
sum := notsha256.Sum256(data)
150-
copy(hash, sum[:])
148+
if len(hashBytes) > 0 {
149+
sum := hash.Sum32(data)
150+
copy(hashBytes, sum[:])
151151
}
152152
return sym, size, nil
153153
}
@@ -160,10 +160,10 @@ func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.
160160
}
161161

162162
// File is too big to read and keep in memory.
163-
// Compute hash if needed for read-only content hashing or if the caller wants it.
163+
// Compute hashBytes if needed for read-only content hashing or if the caller wants it.
164164
var sum []byte
165-
if readonly || len(hash) > 0 {
166-
h := notsha256.New()
165+
if readonly || len(hashBytes) > 0 {
166+
h := hash.New32()
167167
n, err := io.Copy(h, f)
168168
if err != nil {
169169
return nil, 0, err
@@ -172,7 +172,7 @@ func fileStringSym(pos src.XPos, file string, readonly bool, hash []byte) (*obj.
172172
return nil, 0, fmt.Errorf("file changed between reads")
173173
}
174174
sum = h.Sum(nil)
175-
copy(hash, sum)
175+
copy(hashBytes, sum)
176176
}
177177

178178
var symdata *obj.LSym

src/cmd/compile/internal/types/fmt.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"sync"
1313

1414
"cmd/compile/internal/base"
15-
"cmd/internal/notsha256"
15+
"cmd/internal/hash"
1616
)
1717

1818
// BuiltinPkg is a fake package that declares the universe block.
@@ -644,7 +644,7 @@ func SplitVargenSuffix(name string) (base, suffix string) {
644644
func TypeHash(t *Type) uint32 {
645645
p := t.LinkString()
646646

647-
// Using SHA256 is overkill, but reduces accidental collisions.
648-
h := notsha256.Sum256([]byte(p))
647+
// Using 32 bytes hash is overkill, but reduces accidental collisions.
648+
h := hash.Sum32([]byte(p))
649649
return binary.LittleEndian.Uint32(h[:4])
650650
}

src/cmd/dist/buildtool.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ var bootstrapDirs = []string{
4444
"cmd/internal/edit",
4545
"cmd/internal/gcprog",
4646
"cmd/internal/goobj",
47+
"cmd/internal/hash",
4748
"cmd/internal/notsha256",
4849
"cmd/internal/obj/...",
4950
"cmd/internal/objabi",

src/cmd/internal/codesign/codesign.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
package codesign
1212

1313
import (
14+
"crypto/sha256"
1415
"debug/macho"
1516
"encoding/binary"
1617
"io"
1718

18-
"cmd/internal/notsha256"
19+
"cmd/internal/hash"
1920
)
2021

2122
// Code signature layout.
@@ -191,7 +192,7 @@ func Size(codeSize int64, id string) int64 {
191192
nhashes := (codeSize + pageSize - 1) / pageSize
192193
idOff := int64(codeDirectorySize)
193194
hashOff := idOff + int64(len(id)+1)
194-
cdirSz := hashOff + nhashes*notsha256.Size
195+
cdirSz := hashOff + nhashes*hash.Size32
195196
return int64(superBlobSize+blobSize) + cdirSz
196197
}
197198

@@ -227,7 +228,7 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
227228
identOffset: uint32(idOff),
228229
nCodeSlots: uint32(nhashes),
229230
codeLimit: uint32(codeSize),
230-
hashSize: notsha256.Size,
231+
hashSize: hash.Size32,
231232
hashType: CS_HASHTYPE_SHA256,
232233
pageSize: uint8(pageSizeBits),
233234
execSegBase: uint64(textOff),
@@ -246,12 +247,7 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
246247
outp = puts(outp, []byte(id+"\000"))
247248

248249
// emit hashes
249-
// NOTE(rsc): These must be SHA256, but for cgo bootstrap reasons
250-
// we cannot import crypto/sha256 when GOEXPERIMENT=boringcrypto
251-
// and the host is linux/amd64. So we use NOT-SHA256
252-
// and then apply a NOT ourselves to get SHA256. Sigh.
253250
var buf [pageSize]byte
254-
h := notsha256.New()
255251
p := 0
256252
for p < int(codeSize) {
257253
n, err := io.ReadFull(data, buf[:])
@@ -265,12 +261,7 @@ func Sign(out []byte, data io.Reader, id string, codeSize, textOff, textSize int
265261
n = int(codeSize) - p
266262
}
267263
p += n
268-
h.Reset()
269-
h.Write(buf[:n])
270-
b := h.Sum(nil)
271-
for i := range b {
272-
b[i] ^= 0xFF // convert notsha256 to sha256
273-
}
264+
b := sha256.Sum256(buf[:n])
274265
outp = puts(outp, b[:])
275266
}
276267
}

src/cmd/internal/obj/objfile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"bytes"
1111
"cmd/internal/bio"
1212
"cmd/internal/goobj"
13-
"cmd/internal/notsha256"
13+
"cmd/internal/hash"
1414
"cmd/internal/objabi"
1515
"cmd/internal/sys"
1616
"cmp"
@@ -494,7 +494,7 @@ func contentHash64(s *LSym) goobj.Hash64Type {
494494
// For now, we assume there is no circular dependencies among
495495
// hashed symbols.
496496
func (w *writer) contentHash(s *LSym) goobj.HashType {
497-
h := notsha256.New()
497+
h := hash.New32()
498498
var tmp [14]byte
499499

500500
// Include the size of the symbol in the hash.

src/cmd/internal/obj/sym.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ package obj
3333

3434
import (
3535
"cmd/internal/goobj"
36-
"cmd/internal/notsha256"
36+
"cmd/internal/hash"
3737
"cmd/internal/objabi"
3838
"encoding/base64"
3939
"encoding/binary"
@@ -207,7 +207,7 @@ func (ctxt *Link) Int128Sym(hi, lo int64) *LSym {
207207

208208
// GCLocalsSym generates a content-addressable sym containing data.
209209
func (ctxt *Link) GCLocalsSym(data []byte) *LSym {
210-
sum := notsha256.Sum256(data)
210+
sum := hash.Sum32(data)
211211
str := base64.StdEncoding.EncodeToString(sum[:16])
212212
return ctxt.LookupInit(fmt.Sprintf("gclocals·%s", str), func(lsym *LSym) {
213213
lsym.P = data

src/cmd/link/elf_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ package main
99
import (
1010
"bytes"
1111
"cmd/internal/buildid"
12-
"cmd/internal/notsha256"
12+
"cmd/internal/hash"
1313
"cmd/link/internal/ld"
1414
"debug/elf"
1515
"fmt"
@@ -224,7 +224,7 @@ func TestGNUBuildIDDerivedFromGoBuildID(t *testing.T) {
224224
t.Fatal(err)
225225
}
226226

227-
expectedGoBuildID := notsha256.Sum256([]byte("0x1234"))
227+
expectedGoBuildID := hash.Sum32([]byte("0x1234"))
228228

229229
gnuBuildID, err := buildid.ReadELFNote(outFile, string(ld.ELF_NOTE_BUILDINFO_NAME), ld.ELF_NOTE_BUILDINFO_TAG)
230230
if err != nil || gnuBuildID == nil {

src/cmd/link/internal/ld/elf.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package ld
66

77
import (
8-
"cmd/internal/notsha256"
8+
"cmd/internal/hash"
99
"cmd/internal/objabi"
1010
"cmd/internal/sys"
1111
"cmd/link/internal/loader"
@@ -812,7 +812,7 @@ func addbuildinfo(val string) {
812812
Exitf("-B gobuildid requires a Go build ID supplied via -buildid")
813813
}
814814

815-
hashedBuildID := notsha256.Sum256([]byte(buildID))
815+
hashedBuildID := hash.Sum32([]byte(buildID))
816816
buildinfo = hashedBuildID[:20]
817817

818818
return
@@ -1677,11 +1677,11 @@ func (ctxt *Link) doelf() {
16771677
sb.SetType(sym.SRODATA)
16781678
ldr.SetAttrSpecial(s, true)
16791679
sb.SetReachable(true)
1680-
sb.SetSize(notsha256.Size)
1680+
sb.SetSize(hash.Size32)
16811681
slices.SortFunc(ctxt.Library, func(a, b *sym.Library) int {
16821682
return strings.Compare(a.Pkg, b.Pkg)
16831683
})
1684-
h := notsha256.New()
1684+
h := hash.New32()
16851685
for _, l := range ctxt.Library {
16861686
h.Write(l.Fingerprint[:])
16871687
}

src/cmd/link/internal/ld/lib.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ import (
5151

5252
"cmd/internal/bio"
5353
"cmd/internal/goobj"
54-
"cmd/internal/notsha256"
54+
"cmd/internal/hash"
5555
"cmd/internal/objabi"
5656
"cmd/internal/sys"
5757
"cmd/link/internal/loadelf"
@@ -1012,21 +1012,21 @@ func typeSymbolMangle(name string) string {
10121012
return name
10131013
}
10141014
if isType {
1015-
hash := notsha256.Sum256([]byte(name[5:]))
1015+
hb := hash.Sum32([]byte(name[5:]))
10161016
prefix := "type:"
10171017
if name[5] == '.' {
10181018
prefix = "type:."
10191019
}
1020-
return prefix + base64.StdEncoding.EncodeToString(hash[:6])
1020+
return prefix + base64.StdEncoding.EncodeToString(hb[:6])
10211021
}
10221022
// instantiated symbol, replace type name in []
10231023
i := strings.IndexByte(name, '[')
10241024
j := strings.LastIndexByte(name, ']')
10251025
if j == -1 || j <= i {
10261026
j = len(name)
10271027
}
1028-
hash := notsha256.Sum256([]byte(name[i+1 : j]))
1029-
return name[:i+1] + base64.StdEncoding.EncodeToString(hash[:6]) + name[j:]
1028+
hb := hash.Sum32([]byte(name[i+1 : j]))
1029+
return name[:i+1] + base64.StdEncoding.EncodeToString(hb[:6]) + name[j:]
10301030
}
10311031

10321032
/*

src/cmd/link/internal/ld/macho_update_uuid.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package ld
1818
// final executable generated by the external linker.
1919

2020
import (
21-
"cmd/internal/notsha256"
21+
"cmd/internal/hash"
2222
"debug/macho"
2323
"io"
2424
"os"
@@ -32,7 +32,7 @@ func uuidFromGoBuildId(buildID string) []byte {
3232
if buildID == "" {
3333
return make([]byte, 16)
3434
}
35-
hashedBuildID := notsha256.Sum256([]byte(buildID))
35+
hashedBuildID := hash.Sum32([]byte(buildID))
3636
rv := hashedBuildID[:16]
3737

3838
// RFC 4122 conformance (see RFC 4122 Sections 4.2.2, 4.1.3). We

src/cmd/objdump/objdump_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
package main
66

77
import (
8-
"cmd/internal/notsha256"
8+
"cmd/internal/hash"
99
"flag"
1010
"fmt"
1111
"internal/platform"
@@ -126,7 +126,7 @@ func testDisasm(t *testing.T, srcfname string, printCode bool, printGnuAsm bool,
126126
goarch = f[1]
127127
}
128128

129-
hash := notsha256.Sum256([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
129+
hash := hash.Sum32([]byte(fmt.Sprintf("%v-%v-%v-%v", srcfname, flags, printCode, printGnuAsm)))
130130
tmp := t.TempDir()
131131
hello := filepath.Join(tmp, fmt.Sprintf("hello-%x.exe", hash))
132132
args := []string{"build", "-o", hello}

0 commit comments

Comments
 (0)