Skip to content

Commit 6e0adde

Browse files
committed
cmd/compile: do not reuse dead value in expand_calls pass
We reuse a value for the same selector on the same arg. But if the value is already marked dead, don't reuse it. A use of an OpInvalid will confuse the compiler. Fixes #48916. Change-Id: I15b9e15b49f6e1991fe91df246cd12a193385e85 Reviewed-on: https://go-review.googlesource.com/c/go/+/355409 Trust: Cherry Mui <[email protected]> Run-TryBot: Cherry Mui <[email protected]> Reviewed-by: Cuong Manh Le <[email protected]> Reviewed-by: David Chase <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 36a265a commit 6e0adde

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1678,7 +1678,7 @@ func (x *expandState) rewriteArgToMemOrRegs(v *Value) *Value {
16781678
t := v.Type
16791679
key := selKey{v, 0, t.Size(), t}
16801680
w := x.commonArgs[key]
1681-
if w != nil {
1681+
if w != nil && w.Uses != 0 { // do not reuse dead value
16821682
v.copyOf(w)
16831683
break
16841684
}
@@ -1709,9 +1709,15 @@ func (x *expandState) newArgToMemOrRegs(baseArg, toReplace *Value, offset int64,
17091709
}
17101710
key := selKey{baseArg, offset, t.Size(), t}
17111711
w := x.commonArgs[key]
1712-
if w != nil {
1712+
if w != nil && w.Uses != 0 { // do not reuse dead value
17131713
if toReplace != nil {
17141714
toReplace.copyOf(w)
1715+
if x.debug > 1 {
1716+
x.Printf("...replace %s\n", toReplace.LongString())
1717+
}
1718+
}
1719+
if x.debug > 1 {
1720+
x.Printf("-->%s\n", w.LongString())
17151721
}
17161722
return w
17171723
}

test/fixedbugs/issue48916.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// compile
2+
3+
// Copyright 2021 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+
// Issue 48916: expand_calls pass crashes due to a (dead)
8+
// use of an OpInvalid value.
9+
10+
package p
11+
12+
type T struct {
13+
num int64
14+
}
15+
16+
func foo(vs map[T]struct{}, d T) error {
17+
_, ok := vs[d]
18+
if !ok {
19+
return nil
20+
}
21+
22+
switch d.num {
23+
case 0:
24+
case 1:
25+
case 2:
26+
case 3:
27+
case 4:
28+
case 5:
29+
case 6:
30+
var num float64
31+
if num != 0 {
32+
return nil
33+
}
34+
}
35+
36+
return nil
37+
}

0 commit comments

Comments
 (0)