diff options
| author | Youlin Feng <fengyoulin@live.com> | 2026-02-14 18:55:34 +0800 |
|---|---|---|
| committer | David Chase <drchase@google.com> | 2026-02-25 11:17:53 -0800 |
| commit | 4659d630da3f96d3f463444057848f70d420bd0c (patch) | |
| tree | f1be8613a4719beab70b4d198bf445c35e44f3ff | |
| parent | 6fb3df88d48b27c384fc3bb11de6a6ebfdc4b9e4 (diff) | |
| download | go-4659d630da3f96d3f463444057848f70d420bd0c.tar.xz | |
[release-branch.go1.26] cmd/compile: avoid folding 64-bit integers into 32-bit constants
Folding a 64-bit integer into a 32-bit constant may result in a negative
integer if the value exceeds math.MaxInt32 (the maximum value of a 32-
bit signed integer). This negative value will be sign-extended to 64
bits at runtime, leading to unexpected results when used in bitwise
AND/OR operations.
Fixes #77618
Change-Id: Idb081a3c20c28bddddcc8eff1225d62123b37a2d
Reviewed-on: https://go-review.googlesource.com/c/go/+/745581
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
(cherry picked from commit 3c8b5e673816c733f13a38b1ed1f53d7d49ea084)
Reviewed-on: https://go-review.googlesource.com/c/go/+/745840
Reviewed-by: Jorropo <jorropo.pgm@gmail.com>
Reviewed-by: Mark Freeman <markfreeman@google.com>
| -rw-r--r-- | src/cmd/compile/internal/ssa/_gen/AMD64.rules | 3 | ||||
| -rw-r--r-- | src/cmd/compile/internal/ssa/rewriteAMD64.go | 21 | ||||
| -rw-r--r-- | test/fixedbugs/issue77613.go | 28 |
3 files changed, 48 insertions, 4 deletions
diff --git a/src/cmd/compile/internal/ssa/_gen/AMD64.rules b/src/cmd/compile/internal/ssa/_gen/AMD64.rules index b49e85b53c..956077d392 100644 --- a/src/cmd/compile/internal/ssa/_gen/AMD64.rules +++ b/src/cmd/compile/internal/ssa/_gen/AMD64.rules @@ -1578,7 +1578,8 @@ (MOVBload [off] {sym} (SB) _) && symIsRO(sym) => (MOVLconst [int32(read8(sym, int64(off)))]) (MOVWload [off] {sym} (SB) _) && symIsRO(sym) => (MOVLconst [int32(read16(sym, int64(off), config.ctxt.Arch.ByteOrder))]) -(MOVLload [off] {sym} (SB) _) && symIsRO(sym) => (MOVLconst [int32(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))]) +(MOVLload <t> [off] {sym} (SB) _) && symIsRO(sym) && is32BitInt(t) => (MOVLconst [int32(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))]) +(MOVLload <t> [off] {sym} (SB) _) && symIsRO(sym) && is64BitInt(t) => (MOVQconst [int64(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))]) (MOVQload [off] {sym} (SB) _) && symIsRO(sym) => (MOVQconst [int64(read64(sym, int64(off), config.ctxt.Arch.ByteOrder))]) (MOVBQSXload [off] {sym} (SB) _) && symIsRO(sym) => (MOVQconst [int64(int8(read8(sym, int64(off))))]) (MOVWQSXload [off] {sym} (SB) _) && symIsRO(sym) => (MOVQconst [int64(int16(read16(sym, int64(off), config.ctxt.Arch.ByteOrder)))]) diff --git a/src/cmd/compile/internal/ssa/rewriteAMD64.go b/src/cmd/compile/internal/ssa/rewriteAMD64.go index b1b1c84046..d005b15a57 100644 --- a/src/cmd/compile/internal/ssa/rewriteAMD64.go +++ b/src/cmd/compile/internal/ssa/rewriteAMD64.go @@ -16399,19 +16399,34 @@ func rewriteValueAMD64_OpAMD64MOVLload(v *Value) bool { v.AddArg(val) return true } - // match: (MOVLload [off] {sym} (SB) _) - // cond: symIsRO(sym) + // match: (MOVLload <t> [off] {sym} (SB) _) + // cond: symIsRO(sym) && is32BitInt(t) // result: (MOVLconst [int32(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))]) for { + t := v.Type off := auxIntToInt32(v.AuxInt) sym := auxToSym(v.Aux) - if v_0.Op != OpSB || !(symIsRO(sym)) { + if v_0.Op != OpSB || !(symIsRO(sym) && is32BitInt(t)) { break } v.reset(OpAMD64MOVLconst) v.AuxInt = int32ToAuxInt(int32(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))) return true } + // match: (MOVLload <t> [off] {sym} (SB) _) + // cond: symIsRO(sym) && is64BitInt(t) + // result: (MOVQconst [int64(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))]) + for { + t := v.Type + off := auxIntToInt32(v.AuxInt) + sym := auxToSym(v.Aux) + if v_0.Op != OpSB || !(symIsRO(sym) && is64BitInt(t)) { + break + } + v.reset(OpAMD64MOVQconst) + v.AuxInt = int64ToAuxInt(int64(read32(sym, int64(off), config.ctxt.Arch.ByteOrder))) + return true + } return false } func rewriteValueAMD64_OpAMD64MOVLstore(v *Value) bool { diff --git a/test/fixedbugs/issue77613.go b/test/fixedbugs/issue77613.go new file mode 100644 index 0000000000..df71ed48e9 --- /dev/null +++ b/test/fixedbugs/issue77613.go @@ -0,0 +1,28 @@ +// 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 ( + "strings" +) + +func main() { + str1 := "你好世界" + if !strings.HasSuffix(str1, "世界") { + panic(1) + } + + str2 := "こんにちは" + if !strings.HasSuffix(str2, "ちは") { + panic(2) + } + + str3 := "спасибо" + if !strings.HasSuffix(str3, "ибо") { + panic(3) + } +} |
