Commit 74f57fa2fc for golang
commit 74f57fa2fc09dd419c7ec6e907f212c09de5edbf
Author: David Chase <drchase@google.com>
Date: Sun Nov 29 23:07:35 2026 -0500
cmd/compile: check for negative shift values in combineStores
In combineStores, the shift helper returns -1 as a sentinel when a
stored value does not come from a constant right-shift, and also
returns val.AuxInt without rejecting negative shift constants. Neither
the little-endian nor big-endian check rejected negative return values,
allowing a -1 sentinel paired with a shift of 7 to satisfy -1 + 8 == 7
(little-endian) or 7 - 8 == -1 (big-endian). combineStores then emitted
a combined store with a negative right-shift, silently zeroing the
written bytes.
Check for and reject negative shift values.
Fixes #83505
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race
Change-Id: I02ce108e5b2d634e0547b9abe789f520d0303e23
Reviewed-on: https://go-review.googlesource.com/c/go/+/867962
Reviewed-by: Keith Randall <khr@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Keith Randall <khr@golang.org>
diff --git a/src/cmd/compile/internal/ssacompile/memcombine.go b/src/cmd/compile/internal/ssacompile/memcombine.go
index 57a174cc4c..9f6afc7595 100644
--- a/src/cmd/compile/internal/ssacompile/memcombine.go
+++ b/src/cmd/compile/internal/ssacompile/memcombine.go
@@ -628,6 +628,7 @@ func combineStores(root *ssa.Value, ptrAlignments []int8) {
if val.Op != ssaop.OpConst64 {
return -1
}
+ // could be negative, callers will check
return val.AuxInt
}
@@ -860,8 +861,15 @@ func combineStores(root *ssa.Value, ptrAlignments []int8) {
// Check for writes in little-endian or big-endian order.
isLittleEndian := true
shift0 := shift(a[0].store, shiftBase)
+ if shift0 < 0 {
+ return
+ }
for i := 1; i < len(a); i++ {
- if shift(a[i].store, shiftBase) != shift0+(a[i].offset-a[0].offset)*8 {
+ s := shift(a[i].store, shiftBase)
+ if s < 0 {
+ return
+ }
+ if s != shift0+(a[i].offset-a[0].offset)*8 {
isLittleEndian = false
break
}
@@ -870,7 +878,11 @@ func combineStores(root *ssa.Value, ptrAlignments []int8) {
shiftedSize := int64(0)
for i := 1; i < len(a); i++ {
shiftedSize += a[i].size
- if shift(a[i].store, shiftBase) != shift0-shiftedSize*8 {
+ s := shift(a[i].store, shiftBase)
+ if s < 0 {
+ return
+ }
+ if s != shift0-shiftedSize*8 {
isBigEndian = false
break
}
diff --git a/test/fixedbugs/issue83505.go b/test/fixedbugs/issue83505.go
new file mode 100644
index 0000000000..3900491a68
--- /dev/null
+++ b/test/fixedbugs/issue83505.go
@@ -0,0 +1,89 @@
+// 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"
+"os"
+)
+
+//go:noinline
+func writeLE16(dst *[2]byte, word uint64, dynShift uint) {
+ dst[0] = byte(word >> dynShift)
+ dst[1] = byte(word >> 7)
+}
+
+//go:noinline
+func writeBE16(dst *[2]byte, word uint64, dynShift uint) {
+ dst[0] = byte(word >> 7)
+ dst[1] = byte(word >> dynShift)
+}
+
+//go:noinline
+func writeLE32(dst *[4]byte, word uint64, dynShift uint) {
+ dst[0] = byte(word >> dynShift)
+ dst[1] = byte(word >> 7)
+ dst[2] = byte(word >> 15)
+ dst[3] = byte(word >> 23)
+}
+
+//go:noinline
+func writeLE64(dst *[8]byte, word uint64, dynShift uint) {
+ dst[0] = byte(word >> dynShift)
+ dst[1] = byte(word >> 7)
+ dst[2] = byte(word >> 15)
+ dst[3] = byte(word >> 23)
+ dst[4] = byte(word >> 31)
+ dst[5] = byte(word >> 39)
+ dst[6] = byte(word >> 47)
+ dst[7] = byte(word >> 55)
+}
+
+var (
+ testWord = uint64(0xfedcba9876543280)
+ testShift = uint(1)
+)
+
+func main() {
+ TestCombineStoresShiftSentinel()
+}
+
+func TestCombineStoresShiftSentinel() {
+ w, s := testWord, testShift
+ fail := false
+
+ var le16 [2]byte
+ writeLE16(&le16, w, s)
+ if want := [2]byte{0x40, 0x65}; le16 != want {
+ fmt.Printf("FAIL: writeLE16: got %x, want %x\n", le16, want)
+ fail = true
+ }
+
+ var be16 [2]byte
+ writeBE16(&be16, w, s)
+ if want := [2]byte{0x65, 0x40}; be16 != want {
+ fmt.Printf("FAIL: writeBE16: got %x, want %x\n", be16, want)
+ fail = true
+ }
+
+ var le32 [4]byte
+ writeLE32(&le32, w, s)
+ if want := [4]byte{0x40, 0x65, 0xa8, 0xec}; le32 != want {
+ fmt.Printf("FAIL: writeLE32: got %x, want %x\n", le32, want)
+ fail = true
+ }
+
+ var le64 [8]byte
+ writeLE64(&le64, w, s)
+ if want := [8]byte{0x40, 0x65, 0xa8, 0xec, 0x30, 0x75, 0xb9, 0xfd}; le64 != want {
+ fmt.Printf("FAIL: writeLE64: got %x, want %x\n", le64, want)
+ fail = true
+ }
+ if fail {
+ os.Exit(1)
+ }
+}
\ No newline at end of file