Commit a1f6d2d72a for golang
commit a1f6d2d72ab299738b8fc1244f1000df7cb39a7c
Author: David Chase <drchase@google.com>
Date: Sun Nov 29 22:02:37 2026 -0500
cmd/compile: check shift bounds in ((x >> c) & d) << e rewrite rules
The ((x >> c) & d) << e rules in generic.rules compared c >= e and
c < e as signed int64 values without checking that both shift counts
are less than the operand bit width. When c is a large unsigned shift
count (represented as a negative int64 in AuxInt, such as -1 for
^uint64(0)), c < e holds and the rule rewrites the expression into
(x << (e-c)) & (d << e), producing an incorrect value and violating
bounds established earlier by prove.
Guard all eight rules with uint64(c) < W && uint64(e) < W for the
corresponding bit width W.
Fixes #83502
Cq-Include-Trybots: luci.golang.try:gotip-darwin-arm64-longtest,gotip-linux-amd64-longtest,gotip-linux-amd64-longtest-race
Change-Id: Ib1265540abde5ca2f49613a471df82b6b1cb8e83
Reviewed-on: https://go-review.googlesource.com/c/go/+/866177
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>
Reviewed-by: Keith Randall <khr@google.com>
diff --git a/src/cmd/compile/internal/ssa/_gen/generic.rules b/src/cmd/compile/internal/ssa/_gen/generic.rules
index 4b706f7434..903f288698 100644
--- a/src/cmd/compile/internal/ssa/_gen/generic.rules
+++ b/src/cmd/compile/internal/ssa/_gen/generic.rules
@@ -604,14 +604,14 @@
(Rsh64x64 (Lsh64x64 x (Const64 [32])) (Const64 [32])) => (SignExt32to64 (Trunc64to32 <typ.Int32> x))
// ((x >> c) & d) << e
-(Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c >= e => (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
-(Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c >= e => (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
-(Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c >= e => (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
-(Lsh8x64 (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e])) && c >= e => (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c-e])) (Const8 <t> [d<<e]))
-(Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && c < e => (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
-(Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && c < e => (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
-(Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && c < e => (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
-(Lsh8x64 (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e])) && c < e => (And8 (Lsh8x64 <t> x (Const64 <t2> [e-c])) (Const8 <t> [d<<e]))
+(Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && uint64(c) < 64 && uint64(e) < 64 && c >= e => (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
+(Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && uint64(c) < 32 && uint64(e) < 32 && c >= e => (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
+(Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && uint64(c) < 16 && uint64(e) < 16 && c >= e => (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
+(Lsh8x64 (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e])) && uint64(c) < 8 && uint64(e) < 8 && c >= e => (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c-e])) (Const8 <t> [d<<e]))
+(Lsh64x64 (And64 (Rsh(64|64U)x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e])) && uint64(c) < 64 && uint64(e) < 64 && c < e => (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
+(Lsh32x64 (And32 (Rsh(32|32U)x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e])) && uint64(c) < 32 && uint64(e) < 32 && c < e => (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
+(Lsh16x64 (And16 (Rsh(16|16U)x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e])) && uint64(c) < 16 && uint64(e) < 16 && c < e => (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
+(Lsh8x64 (And8 (Rsh(8|8U)x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e])) && uint64(c) < 8 && uint64(e) < 8 && c < e => (And8 (Lsh8x64 <t> x (Const64 <t2> [e-c])) (Const8 <t> [d<<e]))
// constant comparisons
(Eq(64|32|16|8) (Const(64|32|16|8) [c]) (Const(64|32|16|8) [d])) => (ConstBool [c == d])
diff --git a/src/cmd/compile/internal/ssa/rewrite/generic/rewritegeneric.go b/src/cmd/compile/internal/ssa/rewrite/generic/rewritegeneric.go
index 29affc940f..3e26b04f77 100644
--- a/src/cmd/compile/internal/ssa/rewrite/generic/rewritegeneric.go
+++ b/src/cmd/compile/internal/ssa/rewrite/generic/rewritegeneric.go
@@ -15939,7 +15939,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
return true
}
// match: (Lsh16x64 (And16 (Rsh16x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 16 && uint64(e) < 16 && c >= e
// result: (And16 (Rsh16x64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd16 {
@@ -15969,7 +15969,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 16 && uint64(e) < 16 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd16)
@@ -15985,7 +15985,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
break
}
// match: (Lsh16x64 (And16 (Rsh16Ux64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 16 && uint64(e) < 16 && c >= e
// result: (And16 (Rsh16Ux64 <t> x (Const64 <t2> [c-e])) (Const16 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd16 {
@@ -16015,7 +16015,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 16 && uint64(e) < 16 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd16)
@@ -16031,7 +16031,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
break
}
// match: (Lsh16x64 (And16 (Rsh16x64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 16 && uint64(e) < 16 && c < e
// result: (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd16 {
@@ -16061,7 +16061,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 16 && uint64(e) < 16 && c < e) {
continue
}
v.Reset(ssaop.OpAnd16)
@@ -16077,7 +16077,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
break
}
// match: (Lsh16x64 (And16 (Rsh16Ux64 <t> x (Const64 <t2> [c])) (Const16 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 16 && uint64(e) < 16 && c < e
// result: (And16 (Lsh16x64 <t> x (Const64 <t2> [e-c])) (Const16 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd16 {
@@ -16107,7 +16107,7 @@ func rewriteValue_OpLsh16x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 16 && uint64(e) < 16 && c < e) {
continue
}
v.Reset(ssaop.OpAnd16)
@@ -16467,7 +16467,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
return true
}
// match: (Lsh32x64 (And32 (Rsh32x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 32 && uint64(e) < 32 && c >= e
// result: (And32 (Rsh32x64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd32 {
@@ -16497,7 +16497,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 32 && uint64(e) < 32 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd32)
@@ -16513,7 +16513,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
break
}
// match: (Lsh32x64 (And32 (Rsh32Ux64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 32 && uint64(e) < 32 && c >= e
// result: (And32 (Rsh32Ux64 <t> x (Const64 <t2> [c-e])) (Const32 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd32 {
@@ -16543,7 +16543,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 32 && uint64(e) < 32 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd32)
@@ -16559,7 +16559,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
break
}
// match: (Lsh32x64 (And32 (Rsh32x64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 32 && uint64(e) < 32 && c < e
// result: (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd32 {
@@ -16589,7 +16589,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 32 && uint64(e) < 32 && c < e) {
continue
}
v.Reset(ssaop.OpAnd32)
@@ -16605,7 +16605,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
break
}
// match: (Lsh32x64 (And32 (Rsh32Ux64 <t> x (Const64 <t2> [c])) (Const32 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 32 && uint64(e) < 32 && c < e
// result: (And32 (Lsh32x64 <t> x (Const64 <t2> [e-c])) (Const32 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd32 {
@@ -16635,7 +16635,7 @@ func rewriteValue_OpLsh32x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 32 && uint64(e) < 32 && c < e) {
continue
}
v.Reset(ssaop.OpAnd32)
@@ -16995,7 +16995,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
return true
}
// match: (Lsh64x64 (And64 (Rsh64x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 64 && uint64(e) < 64 && c >= e
// result: (And64 (Rsh64x64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd64 {
@@ -17025,7 +17025,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 64 && uint64(e) < 64 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd64)
@@ -17041,7 +17041,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
break
}
// match: (Lsh64x64 (And64 (Rsh64Ux64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 64 && uint64(e) < 64 && c >= e
// result: (And64 (Rsh64Ux64 <t> x (Const64 <t2> [c-e])) (Const64 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd64 {
@@ -17071,7 +17071,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 64 && uint64(e) < 64 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd64)
@@ -17087,7 +17087,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
break
}
// match: (Lsh64x64 (And64 (Rsh64x64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 64 && uint64(e) < 64 && c < e
// result: (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd64 {
@@ -17117,7 +17117,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 64 && uint64(e) < 64 && c < e) {
continue
}
v.Reset(ssaop.OpAnd64)
@@ -17133,7 +17133,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
break
}
// match: (Lsh64x64 (And64 (Rsh64Ux64 <t> x (Const64 <t2> [c])) (Const64 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 64 && uint64(e) < 64 && c < e
// result: (And64 (Lsh64x64 <t> x (Const64 <t2> [e-c])) (Const64 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd64 {
@@ -17163,7 +17163,7 @@ func rewriteValue_OpLsh64x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 64 && uint64(e) < 64 && c < e) {
continue
}
v.Reset(ssaop.OpAnd64)
@@ -17523,7 +17523,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
return true
}
// match: (Lsh8x64 (And8 (Rsh8x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 8 && uint64(e) < 8 && c >= e
// result: (And8 (Rsh8x64 <t> x (Const64 <t2> [c-e])) (Const8 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd8 {
@@ -17553,7 +17553,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 8 && uint64(e) < 8 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd8)
@@ -17569,7 +17569,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
break
}
// match: (Lsh8x64 (And8 (Rsh8Ux64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e]))
- // cond: c >= e
+ // cond: uint64(c) < 8 && uint64(e) < 8 && c >= e
// result: (And8 (Rsh8Ux64 <t> x (Const64 <t2> [c-e])) (Const8 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd8 {
@@ -17599,7 +17599,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c >= e) {
+ if !(uint64(c) < 8 && uint64(e) < 8 && c >= e) {
continue
}
v.Reset(ssaop.OpAnd8)
@@ -17615,7 +17615,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
break
}
// match: (Lsh8x64 (And8 (Rsh8x64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 8 && uint64(e) < 8 && c < e
// result: (And8 (Lsh8x64 <t> x (Const64 <t2> [e-c])) (Const8 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd8 {
@@ -17645,7 +17645,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 8 && uint64(e) < 8 && c < e) {
continue
}
v.Reset(ssaop.OpAnd8)
@@ -17661,7 +17661,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
break
}
// match: (Lsh8x64 (And8 (Rsh8Ux64 <t> x (Const64 <t2> [c])) (Const8 [d])) (Const64 [e]))
- // cond: c < e
+ // cond: uint64(c) < 8 && uint64(e) < 8 && c < e
// result: (And8 (Lsh8x64 <t> x (Const64 <t2> [e-c])) (Const8 <t> [d<<e]))
for {
if v_0.Op != ssaop.OpAnd8 {
@@ -17691,7 +17691,7 @@ func rewriteValue_OpLsh8x64(v *ssa.Value) bool {
continue
}
e := ssa.AuxIntToInt64(v_1.AuxInt)
- if !(c < e) {
+ if !(uint64(c) < 8 && uint64(e) < 8 && c < e) {
continue
}
v.Reset(ssaop.OpAnd8)
diff --git a/test/fixedbugs/issue83502.go b/test/fixedbugs/issue83502.go
new file mode 100644
index 0000000000..bf19562096
--- /dev/null
+++ b/test/fixedbugs/issue83502.go
@@ -0,0 +1,58 @@
+// 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"
+)
+
+const (
+ goodVal = 0x12345678
+ badVal = 0xdeadbeef
+)
+
+type pair struct {
+ buf [4]uintptr
+ after uintptr
+}
+
+//go:noinline
+func readAt(s []uintptr, x int64, c uint64) uintptr {
+ if len(s) < 3 || c != ^uint64(0) {
+ return 0
+ }
+ r := x >> c
+ if uint64(r) > 1 {
+ return 0
+ }
+ idx := (r & 0x3ffffffffffffffe) << 1
+ return s[idx]
+}
+
+//go:noinline
+func constShift(x int64) int64 {
+ const c = ^uint64(1)
+ return ((x >> c) & 1) << 4
+}
+
+func main() {
+ TestShiftMaskSignedBounds()
+}
+
+func TestShiftMaskSignedBounds() {
+ if got := constShift(-1); got != 16 {
+ fmt.Printf("constShift(-1): got %d, want 16\n", got)
+ os.Exit(1)
+ }
+
+ p := &pair{buf: [4]uintptr{goodVal}, after: badVal}
+ if got := readAt(p.buf[:3], 1, ^uint64(0)); got != goodVal {
+ fmt.Printf("readAt(p.buf[:3], 1, ^uint64(0)): got %#x, want %#x (out-of-bounds read)\n", got, uint64(goodVal))
+ os.Exit(1)
+ }
+}