Skip to content

Commit 7d8fa65

Browse files
cherrymuidmitshur
authored andcommitted
[release-branch.go1.17] cmd/compile: correct type of pointer difference on RISCV64
Pointer comparison is lowered to the following on RISCV64 (EqPtr x y) => (SEQZ (SUB <x.Type> x y)) The difference of two pointers (the SUB) should not be pointer type. Otherwise it can cause the GC to find a bad pointer. Updates #51101. Fixes #51199. Change-Id: I7e73c2155c36ff403c032981a9aa9cccbfdf0f64 Reviewed-on: https://go-review.googlesource.com/c/go/+/385655 Trust: Cherry Mui <cherryyz@google.com> Run-TryBot: Cherry Mui <cherryyz@google.com> Reviewed-by: Keith Randall <khr@golang.org> TryBot-Result: Gopher Robot <gobot@golang.org> (cherry picked from commit 1ed30ca) Reviewed-on: https://go-review.googlesource.com/c/go/+/386474
1 parent 1ba25fa commit 7d8fa65

File tree

3 files changed

+44
-6
lines changed

3 files changed

+44
-6
lines changed

src/cmd/compile/internal/ssa/gen/RISCV64.rules

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@
250250
(Leq64F ...) => (FLED ...)
251251
(Leq32F ...) => (FLES ...)
252252

253-
(EqPtr x y) => (SEQZ (SUB <x.Type> x y))
253+
(EqPtr x y) => (SEQZ (SUB <typ.Uintptr> x y))
254254
(Eq64 x y) => (SEQZ (SUB <x.Type> x y))
255255
(Eq32 x y) => (SEQZ (SUB <x.Type> (ZeroExt32to64 x) (ZeroExt32to64 y)))
256256
(Eq16 x y) => (SEQZ (SUB <x.Type> (ZeroExt16to64 x) (ZeroExt16to64 y)))
257257
(Eq8 x y) => (SEQZ (SUB <x.Type> (ZeroExt8to64 x) (ZeroExt8to64 y)))
258258
(Eq64F ...) => (FEQD ...)
259259
(Eq32F ...) => (FEQS ...)
260260

261-
(NeqPtr x y) => (SNEZ (SUB <x.Type> x y))
261+
(NeqPtr x y) => (SNEZ (SUB <typ.Uintptr> x y))
262262
(Neq64 x y) => (SNEZ (SUB <x.Type> x y))
263263
(Neq32 x y) => (SNEZ (SUB <x.Type> (ZeroExt32to64 x) (ZeroExt32to64 y)))
264264
(Neq16 x y) => (SNEZ (SUB <x.Type> (ZeroExt16to64 x) (ZeroExt16to64 y)))

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

Lines changed: 6 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue51101.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// run
2+
3+
// Copyright 2022 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 51101: on RISCV64, difference of two pointers
8+
// was marked as pointer and crashes GC.
9+
10+
package main
11+
12+
var a, b int
13+
14+
func main() {
15+
F(&b, &a)
16+
}
17+
18+
//go:noinline
19+
func F(a, b *int) bool {
20+
x := a == b
21+
G(x)
22+
y := a != b
23+
return y
24+
}
25+
26+
//go:noinline
27+
func G(bool) {
28+
grow([1000]int{20})
29+
}
30+
31+
func grow(x [1000]int) {
32+
if x[0] != 0 {
33+
x[0]--
34+
grow(x)
35+
}
36+
}

0 commit comments

Comments
 (0)