Commit c58fec1c44 for golang
commit c58fec1c4493db6bff4e0196a9676bd5dadce2ca
Author: David Chase <drchase@google.com>
Date: Sat Nov 28 23:12:59 2026 -0500
cmd/compile: validate shift constant bounds in memcombine peelShift
peelShift accepted any constant shift count on OpLsh(64|32|16)x64
without checking that it lies in [0, v.Type.Size()*8). A large unsigned
shift such as ^uint64(7) is represented as a negative signed AuxInt
(-8), which caused combineLoads to merge loads using a negative shift
and silently zero the result, and caused splitPtr to panic with a
negative shift amount in imm << idx.shift.
Require 0 <= imm && imm < v.Type.Size()*8 in peelShift.
Fixes #83474
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race
Change-Id: Iec9bc9faf4c693384e1a4a96f3c0a6875c039b7b
Reviewed-on: https://go-review.googlesource.com/c/go/+/866871
Reviewed-by: Keith Randall <khr@golang.org>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@google.com>
diff --git a/src/cmd/compile/internal/ssacompile/memcombine.go b/src/cmd/compile/internal/ssacompile/memcombine.go
index f83466e840..57a174cc4c 100644
--- a/src/cmd/compile/internal/ssacompile/memcombine.go
+++ b/src/cmd/compile/internal/ssacompile/memcombine.go
@@ -138,7 +138,7 @@ func peelShift(v *ssa.Value) (exp *ssa.Value, shift int64) {
}
if v.Op == ssaop.OpLsh64x64 || v.Op == ssaop.OpLsh32x64 || v.Op == ssaop.OpLsh16x64 {
- if imm, ok := getConst(v.Args[1]); ok {
+ if imm, ok := getConst(v.Args[1]); ok && 0 <= imm && imm < v.Type.Size()*8 {
return v.Args[0], imm
}
}
diff --git a/test/fixedbugs/issue83474a.go b/test/fixedbugs/issue83474a.go
new file mode 100644
index 0000000000..ac7d5f04ae
--- /dev/null
+++ b/test/fixedbugs/issue83474a.go
@@ -0,0 +1,38 @@
+// compile
+
+// Copyright 2026 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.
+
+package main
+
+//go:noinline
+func nop() {}
+
+// triggerSplitPtrICE delays constant folding of s (^uint64(0), signed
+// AuxInt == -1) until sccp, which runs after late opt and just before
+// memcombine (with no opt pass in between):
+// 1. A plain constant shift is folded to 0 by opt before memcombine runs,
+// whereas returning s from a noinline helper hides it from memcombine too.
+// 2. The loop creates cyclic Phi nodes for k and s that opt passes cannot fold.
+// 3. k's values {0, 3} span [0, 3] (signed and unsigned) and differ in both
+// low bits, so range and known-bits analyses cannot rule out k == 1.
+// 4. Calling noinline nop() inside the if prevents branchelim from replacing
+// the branch with CondSelects, keeping the dead CFG edge for sccp.
+// 5. sccp folds k == 1 to false and exposes Const64[-1] to memcombine's
+// splitPtr -> peelShift, panicking with "negative shift amount".
+func triggerSplitPtrICE(b []byte, x int, n int) uint16 {
+ k := 0
+ s := ^uint64(0) // signed AuxInt == -1
+ for range n {
+ if k == 1 {
+ nop()
+ s = 1
+ k = 3
+ }
+ }
+ idx := int(uint64(x+1) << s)
+ return uint16(b[idx]) | (uint16(b[idx+1]) << 8)
+}
+
+func main() {}
diff --git a/test/fixedbugs/issue83474b.go b/test/fixedbugs/issue83474b.go
new file mode 100644
index 0000000000..bc1edc881b
--- /dev/null
+++ b/test/fixedbugs/issue83474b.go
@@ -0,0 +1,90 @@
+// run
+
+// Copyright 2026 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.
+
+package main
+import "fmt"
+
+//go:noinline
+func nop() {}
+
+// sccpConst returns c while delaying constant propagation until the sccp pass,
+// which runs after late opt and just before memcombine (with no opt pass in
+// between). Step by step:
+// 1. A plain constant shift is folded to 0 by opt before memcombine runs,
+// whereas returning c from a noinline helper hides the constant from
+// memcombine too (peelShift requires a Const64 shift count).
+// 2. The loop creates cyclic Phi nodes for k and c that opt, middle opt, and
+// late opt cannot simplify.
+// 3. k's reachable/syntactic values {0, 3} span [0, 3] (both signed and
+// unsigned) and differ in both low bits, so range and known-bits analyses
+// cannot rule out k == 1.
+// 4. Calling noinline nop() inside the if prevents branchelim from turning
+// the branch into CondSelects, keeping the dead CFG edge intact for sccp.
+// (Making the condition itself a noinline call would hide it from sccp too.)
+// 5. sccp optimistically assumes loop backedges are unreachable, folds
+// k == 1 to false, and replaces the Phi for c with Const64[-8] right
+// before memcombine inspects the shift.
+func sccpConst(c uint64, n int) uint64 {
+ k := 0
+ for range n {
+ if k == 1 {
+ nop()
+ c = 8
+ k = 3
+ }
+ }
+ return c
+}
+
+//go:noinline
+func loadLE16(b []byte, n int) uint64 {
+ _ = b[1]
+ s := sccpConst(^uint64(7), n) // signed AuxInt == -8
+ return (uint64(b[0]) << s) | uint64(b[1])
+}
+
+//go:noinline
+func loadBE32(b []byte, n int) uint64 {
+ _ = b[3]
+ s := sccpConst(^uint64(7), n)
+ return (uint64(b[0]) << 16) | (uint64(b[1]) << 8) | uint64(b[2]) | (uint64(b[3]) << s)
+}
+
+//go:noinline
+func loadBE64(b []byte, n int) uint64 {
+ _ = b[7]
+ s := sccpConst(^uint64(7), n)
+ return (uint64(b[0]) << 48) |
+ (uint64(b[1]) << 40) |
+ (uint64(b[2]) << 32) |
+ (uint64(b[3]) << 24) |
+ (uint64(b[4]) << 16) |
+ (uint64(b[5]) << 8) |
+ uint64(b[6]) |
+ (uint64(b[7]) << s)
+}
+
+
+var (
+ testBuf = []byte{0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0}
+ testIters = 2
+)
+
+func main() {
+ TestMemcombineNegativeShiftLoad()
+}
+
+func TestMemcombineNegativeShiftLoad() {
+ if got, want := loadLE16(testBuf, testIters), uint64(0x34); got != want {
+ fmt.Printf("FAIL: loadLE16: got %#04x, want %#04x\n", got, want)
+ }
+ if got, want := loadBE32(testBuf, testIters), uint64(0x123456); got != want {
+ fmt.Printf("FAIL: loadBE32: got %#06x, want %#06x\n", got, want)
+ }
+ if got, want := loadBE64(testBuf, testIters), uint64(0x123456789abcde); got != want {
+ fmt.Printf("FAIL: loadBE64: got %#014x, want %#014x\n", got, want)
+ }
+}