Skip to content

Commit 42fd130

Browse files
committed
cmd/compile: clean up equality generation
We're using sort.SliceStable, so no need to keep track of indexes as well. Use a more robust test for whether a node is a call. Add a test that we're actually reordering comparisons. This test fails without the alg.go changes in this CL because eqstring uses OCALLFUNC instead of OCALL for its data comparisons. Update #8606 Change-Id: Ieeec33434c72e3aa328deb11cc415cfda05632e2 Reviewed-on: https://go-review.googlesource.com/c/go/+/237921 Run-TryBot: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]>
1 parent 4f76fe8 commit 42fd130

File tree

2 files changed

+75
-19
lines changed

2 files changed

+75
-19
lines changed

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -646,17 +646,11 @@ func geneq(t *types.Type) *obj.LSym {
646646
// Build a list of conditions to satisfy.
647647
// The conditions are a list-of-lists. Conditions are reorderable
648648
// within each inner list. The outer lists must be evaluated in order.
649-
// Even within each inner list, track their order so that we can preserve
650-
// aspects of that order. (TODO: latter part needed?)
651-
type nodeIdx struct {
652-
n *Node
653-
idx int
654-
}
655-
var conds [][]nodeIdx
656-
conds = append(conds, []nodeIdx{})
649+
var conds [][]*Node
650+
conds = append(conds, []*Node{})
657651
and := func(n *Node) {
658652
i := len(conds) - 1
659-
conds[i] = append(conds[i], nodeIdx{n: n, idx: len(conds[i])})
653+
conds[i] = append(conds[i], n)
660654
}
661655

662656
// Walk the struct using memequal for runs of AMEM
@@ -674,7 +668,7 @@ func geneq(t *types.Type) *obj.LSym {
674668
if !IsRegularMemory(f.Type) {
675669
if EqCanPanic(f.Type) {
676670
// Enforce ordering by starting a new set of reorderable conditions.
677-
conds = append(conds, []nodeIdx{})
671+
conds = append(conds, []*Node{})
678672
}
679673
p := nodSym(OXDOT, np, f.Sym)
680674
q := nodSym(OXDOT, nq, f.Sym)
@@ -688,7 +682,7 @@ func geneq(t *types.Type) *obj.LSym {
688682
}
689683
if EqCanPanic(f.Type) {
690684
// Also enforce ordering after something that can panic.
691-
conds = append(conds, []nodeIdx{})
685+
conds = append(conds, []*Node{})
692686
}
693687
i++
694688
continue
@@ -713,14 +707,13 @@ func geneq(t *types.Type) *obj.LSym {
713707

714708
// Sort conditions to put runtime calls last.
715709
// Preserve the rest of the ordering.
716-
var flatConds []nodeIdx
710+
var flatConds []*Node
717711
for _, c := range conds {
712+
isCall := func(n *Node) bool {
713+
return n.Op == OCALL || n.Op == OCALLFUNC
714+
}
718715
sort.SliceStable(c, func(i, j int) bool {
719-
x, y := c[i], c[j]
720-
if (x.n.Op != OCALL) == (y.n.Op != OCALL) {
721-
return x.idx < y.idx
722-
}
723-
return x.n.Op != OCALL
716+
return !isCall(c[i]) && isCall(c[j])
724717
})
725718
flatConds = append(flatConds, c...)
726719
}
@@ -729,9 +722,9 @@ func geneq(t *types.Type) *obj.LSym {
729722
if len(flatConds) == 0 {
730723
cond = nodbool(true)
731724
} else {
732-
cond = flatConds[0].n
725+
cond = flatConds[0]
733726
for _, c := range flatConds[1:] {
734-
cond = nod(OANDAND, cond, c.n)
727+
cond = nod(OANDAND, cond, c)
735728
}
736729
}
737730

test/fixedbugs/issue8606b.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// run
2+
3+
// Copyright 2020 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
// This is an optimization check. We want to make sure that we compare
8+
// string lengths, and other scalar fields, before checking string
9+
// contents. There's no way to verify this in the language, and
10+
// codegen tests in test/codegen can't really detect ordering
11+
// optimizations like this. Instead, we generate invalid strings with
12+
// bad backing store pointers but nonzero length, so we can check that
13+
// the backing store never gets compared.
14+
//
15+
// We use two different bad strings so that pointer comparisons of
16+
// backing store pointers fail.
17+
18+
package main
19+
20+
import (
21+
"fmt"
22+
"reflect"
23+
"unsafe"
24+
)
25+
26+
func bad1() string {
27+
s := "foo"
28+
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data = 1 // write bad value to data ptr
29+
return s
30+
}
31+
func bad2() string {
32+
s := "foo"
33+
(*reflect.StringHeader)(unsafe.Pointer(&s)).Data = 2 // write bad value to data ptr
34+
return s
35+
}
36+
37+
type SI struct {
38+
s string
39+
i int
40+
}
41+
42+
type SS struct {
43+
s string
44+
t string
45+
}
46+
47+
func main() {
48+
for _, test := range []struct {
49+
a, b interface{}
50+
}{
51+
{SI{s: bad1(), i: 1}, SI{s: bad2(), i: 2}},
52+
{SS{s: bad1(), t: "a"}, SS{s: bad2(), t: "aa"}},
53+
{SS{s: "a", t: bad1()}, SS{s: "b", t: bad2()}},
54+
// This one would panic because the length of both strings match, and we check
55+
// the body of the bad strings before the body of the good strings.
56+
//{SS{s: bad1(), t: "a"}, SS{s: bad2(), t: "b"}},
57+
} {
58+
if test.a == test.b {
59+
panic(fmt.Sprintf("values %#v and %#v should not be equal", test.a, test.b))
60+
}
61+
}
62+
63+
}

0 commit comments

Comments
 (0)