Skip to content

Commit

Permalink
[release-branch.go1.16] cmd/compile: correct type of pointer differen…
Browse files Browse the repository at this point in the history
…ce 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 #51198.

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/+/386475
  • Loading branch information
cherrymui authored and dmitshur committed Feb 18, 2022
1 parent 8a24f67 commit a222963
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/cmd/compile/internal/ssa/gen/RISCV64.rules
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,15 @@
(Leq64F ...) => (FLED ...)
(Leq32F ...) => (FLES ...)

(EqPtr x y) => (SEQZ (SUB <x.Type> x y))
(EqPtr x y) => (SEQZ (SUB <typ.Uintptr> x y))
(Eq64 x y) => (SEQZ (SUB <x.Type> x y))
(Eq32 x y) => (SEQZ (SUBW <x.Type> x y))
(Eq16 x y) => (SEQZ (SUB <x.Type> (ZeroExt16to64 x) (ZeroExt16to64 y)))
(Eq8 x y) => (SEQZ (SUB <x.Type> (ZeroExt8to64 x) (ZeroExt8to64 y)))
(Eq64F ...) => (FEQD ...)
(Eq32F ...) => (FEQS ...)

(NeqPtr x y) => (SNEZ (SUB <x.Type> x y))
(NeqPtr x y) => (SNEZ (SUB <typ.Uintptr> x y))
(Neq64 x y) => (SNEZ (SUB <x.Type> x y))
(Neq32 x y) => (SNEZ (SUBW <x.Type> x y))
(Neq16 x y) => (SNEZ (SUB <x.Type> (ZeroExt16to64 x) (ZeroExt16to64 y)))
Expand Down
10 changes: 6 additions & 4 deletions src/cmd/compile/internal/ssa/rewriteRISCV64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions test/fixedbugs/issue51101.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// run

// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Issue 51101: on RISCV64, difference of two pointers
// was marked as pointer and crashes GC.

package main

var a, b int

func main() {
F(&b, &a)
}

//go:noinline
func F(a, b *int) bool {
x := a == b
G(x)
y := a != b
return y
}

//go:noinline
func G(bool) {
grow([1000]int{20})
}

func grow(x [1000]int) {
if x[0] != 0 {
x[0]--
grow(x)
}
}

0 comments on commit a222963

Please sign in to comment.