Skip to content

Commit 6f2ee0f

Browse files
committed
cmd/compile: make builds reproducible in presence of **byte and **int8
CL 39915 introduced sorting of signats by ShortString for reproducible builds. But ShortString treats types byte and uint8 identically; same for rune and uint32. CL 39915 attempted to compensate for this by only adding the underlying type (uint8) to signats in addsignat. This only works for byte and uint8. For e.g. *byte and *uint, both get added, and their sort order is random, leading to non-reproducible builds. One fix would be to add yet another type printing mode that doesn't eliminate byte and rune, and use it for sorting signats. But the formatting routines are complicated enough as it is. Instead, just sort first by ShortString and then by String. We can't just use String, because ShortString makes distinctions that String doesn't. ShortString is really preferred here; String is serving only as a backstop for handling of bytes and runes. The long series of types in the test helps increase the odds of failure, allowing a smaller number of iterations in the test. On my machine, a full test takes 700ms. Passes toolstash-check. Updates #19961 Fixes #20272 name old alloc/op new alloc/op delta Template 37.9MB ± 0% 37.9MB ± 0% +0.12% (p=0.032 n=5+5) Unicode 28.9MB ± 0% 28.9MB ± 0% ~ (p=0.841 n=5+5) GoTypes 110MB ± 0% 110MB ± 0% ~ (p=0.841 n=5+5) Compiler 463MB ± 0% 463MB ± 0% ~ (p=0.056 n=5+5) SSA 1.11GB ± 0% 1.11GB ± 0% +0.02% (p=0.016 n=5+5) Flate 24.7MB ± 0% 24.8MB ± 0% +0.14% (p=0.032 n=5+5) GoParser 31.1MB ± 0% 31.1MB ± 0% ~ (p=0.421 n=5+5) Reflect 73.9MB ± 0% 73.9MB ± 0% ~ (p=1.000 n=5+5) Tar 25.8MB ± 0% 25.8MB ± 0% +0.15% (p=0.016 n=5+5) XML 41.2MB ± 0% 41.2MB ± 0% ~ (p=0.310 n=5+5) [Geo mean] 72.0MB 72.0MB +0.07% name old allocs/op new allocs/op delta Template 384k ± 0% 385k ± 1% ~ (p=0.056 n=5+5) Unicode 343k ± 0% 344k ± 0% ~ (p=0.548 n=5+5) GoTypes 1.16M ± 0% 1.16M ± 0% ~ (p=0.421 n=5+5) Compiler 4.43M ± 0% 4.44M ± 0% +0.26% (p=0.032 n=5+5) SSA 9.86M ± 0% 9.87M ± 0% +0.10% (p=0.032 n=5+5) Flate 237k ± 1% 238k ± 0% +0.49% (p=0.032 n=5+5) GoParser 319k ± 1% 320k ± 1% ~ (p=0.151 n=5+5) Reflect 957k ± 0% 957k ± 0% ~ (p=1.000 n=5+5) Tar 251k ± 0% 252k ± 1% +0.49% (p=0.016 n=5+5) XML 399k ± 0% 401k ± 1% ~ (p=0.310 n=5+5) [Geo mean] 739k 741k +0.26% Change-Id: Ic27995a8d374d012b8aca14546b1df9d28d30df7 Reviewed-on: https://go-review.googlesource.com/42955 Run-TryBot: Josh Bleecher Snyder <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Robert Griesemer <[email protected]>
1 parent 9fda4df commit 6f2ee0f

File tree

3 files changed

+99
-7
lines changed

3 files changed

+99
-7
lines changed

src/cmd/compile/internal/gc/reflect.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1412,7 +1412,7 @@ func itabsym(it *obj.LSym, offset int64) *obj.LSym {
14121412
}
14131413

14141414
func addsignat(t *types.Type) {
1415-
signatlist[formalType(t)] = true
1415+
signatlist[t] = true
14161416
}
14171417

14181418
func addsignats(dcls []*Node) {
@@ -1432,7 +1432,7 @@ func dumpsignats() {
14321432
signats = signats[:0]
14331433
// Transfer entries to a slice and sort, for reproducible builds.
14341434
for t := range signatlist {
1435-
signats = append(signats, typeAndStr{t: t, s: typesymname(t)})
1435+
signats = append(signats, typeAndStr{t: t, short: typesymname(t), regular: t.String()})
14361436
delete(signatlist, t)
14371437
}
14381438
sort.Sort(typesByString(signats))
@@ -1542,15 +1542,25 @@ func dumpbasictypes() {
15421542
}
15431543

15441544
type typeAndStr struct {
1545-
t *types.Type
1546-
s string
1545+
t *types.Type
1546+
short string
1547+
regular string
15471548
}
15481549

15491550
type typesByString []typeAndStr
15501551

1551-
func (a typesByString) Len() int { return len(a) }
1552-
func (a typesByString) Less(i, j int) bool { return a[i].s < a[j].s }
1553-
func (a typesByString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
1552+
func (a typesByString) Len() int { return len(a) }
1553+
func (a typesByString) Less(i, j int) bool {
1554+
if a[i].short != a[j].short {
1555+
return a[i].short < a[j].short
1556+
}
1557+
// When the only difference between the types is whether
1558+
// they refer to byte or uint8, such as **byte vs **uint8,
1559+
// the types' ShortStrings can be identical.
1560+
// To preserve deterministic sort ordering, sort these by String().
1561+
return a[i].regular < a[j].regular
1562+
}
1563+
func (a typesByString) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
15541564

15551565
func dalgsym(t *types.Type) *obj.LSym {
15561566
var lsym *obj.LSym
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package gc_test
6+
7+
import (
8+
"bytes"
9+
"internal/testenv"
10+
"io/ioutil"
11+
"os"
12+
"os/exec"
13+
"path/filepath"
14+
"testing"
15+
)
16+
17+
func TestReproducibleBuilds(t *testing.T) {
18+
testenv.MustHaveGoBuild(t)
19+
iters := 10
20+
if testing.Short() {
21+
iters = 4
22+
}
23+
t.Parallel()
24+
var want []byte
25+
tmp, err := ioutil.TempFile("", "")
26+
if err != nil {
27+
t.Fatalf("temp file creation failed: %v", err)
28+
}
29+
defer os.Remove(tmp.Name())
30+
defer tmp.Close()
31+
for i := 0; i < iters; i++ {
32+
out, err := exec.Command(testenv.GoToolPath(t), "tool", "compile", "-o", tmp.Name(), filepath.Join("testdata", "reproducible", "issue20272.go")).CombinedOutput()
33+
if err != nil {
34+
t.Fatalf("failed to compile: %v\n%s", err, out)
35+
}
36+
obj, err := ioutil.ReadFile(tmp.Name())
37+
if err != nil {
38+
t.Fatalf("failed to read object file: %v", err)
39+
}
40+
if i == 0 {
41+
want = obj
42+
} else {
43+
if !bytes.Equal(want, obj) {
44+
t.Fatalf("builds produced different output after %d iters (%d bytes vs %d bytes)", i, len(want), len(obj))
45+
}
46+
}
47+
}
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2017 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package p
6+
7+
var (
8+
i0 uint8
9+
b0 byte
10+
11+
i1 *uint8
12+
b1 *byte
13+
14+
i2 **uint8
15+
b2 **byte
16+
17+
i3 ***uint8
18+
b3 ***byte
19+
20+
i4 ****uint8
21+
b4 ****byte
22+
23+
i5 *****uint8
24+
b5 *****byte
25+
26+
i6 ******uint8
27+
b6 ******byte
28+
29+
i7 *******uint8
30+
b7 *******byte
31+
32+
i8 ********uint8
33+
b8 ********byte
34+
)

0 commit comments

Comments
 (0)