[go] cmd/compile/internal/ssa: check used const op sizes

2 views
Skip to first unread message

Chencheng Jiang (Gerrit)

unread,
Jun 6, 2026, 12:27:33 PM (13 hours ago) Jun 6
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Chencheng Jiang has uploaded the change for review

Commit message

cmd/compile/internal/ssa: check used const op sizes

SSA constants carry both an opcode width and a result type. A value like
Const32 <int> on 64-bit targets can be interpreted inconsistently by
later passes, allowing forms such as And64 x (Const32 [-1]) to escape
SSA checking.

Add a checkFunc invariant that rejects used constant values whose result
type is wider than the constant opcode can represent. Add a unit test for
the mismatched Const32 <int> case.

For #79877
Change-Id: I3488809453baba0ed39b14ec08947cf2304fdeb4

Change diff

diff --git a/src/cmd/compile/internal/ssa/check.go b/src/cmd/compile/internal/ssa/check.go
index 9396f8d..3cfefd1 100644
--- a/src/cmd/compile/internal/ssa/check.go
+++ b/src/cmd/compile/internal/ssa/check.go
@@ -282,6 +282,14 @@
f.Fatalf("unexpected floating-point type %v", v.LongString())
}

+ // Check that used constant values are not wider than their
+ // constant op. For example, Const32 <int> is invalid on
+ // 64-bit architectures because later passes may interpret
+ // it as either a 32-bit constant or a 64-bit int.
+ if constSize := constOpSize(v.Op); constSize != 0 && v.Uses != 0 && v.Type.Size() > constSize {
+ f.Fatalf("bad type size for %s: want at most %d bytes, have %s", v.LongString(), constSize, v.Type.String())
+ }
+
// Check types.
// TODO: more type checks?
switch c := f.Config; v.Op {
@@ -510,6 +518,21 @@
memCheck(f)
}

+func constOpSize(op Op) int64 {
+ switch op {
+ case OpConst8:
+ return 1
+ case OpConst16:
+ return 2
+ case OpConst32, OpConst32F:
+ return 4
+ case OpConst64, OpConst64F:
+ return 8
+ default:
+ return 0
+ }
+}
+
func memCheck(f *Func) {
// Check that if a tuple has a memory type, it is second.
for _, b := range f.Blocks {
diff --git a/src/cmd/compile/internal/ssa/check_test.go b/src/cmd/compile/internal/ssa/check_test.go
new file mode 100644
index 0000000..69069267
--- /dev/null
+++ b/src/cmd/compile/internal/ssa/check_test.go
@@ -0,0 +1,52 @@
+// 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 ssa
+
+import (
+ "cmd/compile/internal/types"
+ "cmd/internal/src"
+ "fmt"
+ "strings"
+ "testing"
+)
+
+type panicFrontend struct {
+ TestFrontend
+}
+
+func (d panicFrontend) Fatalf(_ src.XPos, msg string, args ...any) {
+ panic(fmt.Sprintf(msg, args...))
+}
+
+func TestCheckConstOpTypeSize(t *testing.T) {
+ c := testConfig(t)
+ c.fe = panicFrontend{c.Frontend().(TestFrontend)}
+
+ fun := c.Fun("entry",
+ Bloc("entry",
+ Valu("x", OpConst64, c.config.Types.Int, 1<<34, nil),
+ Valu("mask", OpConst32, c.config.Types.Int, -1, nil),
+ Valu("and", OpAnd64, c.config.Types.Int, 0, nil, "x", "mask"),
+ Valu("mem", OpInitMem, types.TypeMem, 0, nil),
+ Exit("mem")))
+
+ err := checkFuncError(fun.f)
+ if err == "" {
+ t.Fatal("expected checkFunc to fail")
+ }
+ if !strings.Contains(err, "bad type size") || !strings.Contains(err, "Const32 <int>") {
+ t.Fatalf("checkFunc failed with unexpected error: %s", err)
+ }
+}
+
+func checkFuncError(f *Func) (err string) {
+ defer func() {
+ if r := recover(); r != nil {
+ err = fmt.Sprint(r)
+ }
+ }()
+ checkFunc(f)
+ return ""
+}

Change information

Files:
  • M src/cmd/compile/internal/ssa/check.go
  • A src/cmd/compile/internal/ssa/check_test.go
Change size: M
Delta: 2 files changed, 75 insertions(+), 0 deletions(-)
Open in Gerrit

Related details

Attention set is empty
Submit Requirements:
  • requirement is not satisfiedCode-Review
  • requirement satisfiedNo-Unresolved-Comments
  • requirement is not satisfiedReview-Enforcement
  • requirement is not satisfiedTryBots-Pass
Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
Gerrit-MessageType: newchange
Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I3488809453baba0ed39b14ec08947cf2304fdeb4
Gerrit-Change-Number: 787820
Gerrit-PatchSet: 1
Gerrit-Owner: Chencheng Jiang <dorb...@gmail.com>
unsatisfied_requirement
satisfied_requirement
open
diffy

Jorropo (Gerrit)

unread,
Jun 6, 2026, 1:41:58 PM (12 hours ago) Jun 6
to Chencheng Jiang, goph...@pubsubhelper.golang.org, Martin Möhrmann, Keith Randall, Gopher Robot, golang-co...@googlegroups.com
Attention needed from Chencheng Jiang, Keith Randall and Martin Möhrmann

Jorropo added 1 comment

Patchset-level comments
File-level comment, Patchset 1 (Latest):
Jorropo . unresolved

We want a complete solution that fails on absolutely all generic ops, not just consts.

In other words you also need to handle for example `(And64 x (And32 y z))`.

Open in Gerrit

Related details

Attention is currently required from:
  • Chencheng Jiang
  • Keith Randall
  • Martin Möhrmann
Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: comment
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I3488809453baba0ed39b14ec08947cf2304fdeb4
    Gerrit-Change-Number: 787820
    Gerrit-PatchSet: 1
    Gerrit-Owner: Chencheng Jiang <dorb...@gmail.com>
    Gerrit-Reviewer: Keith Randall <k...@golang.org>
    Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Jorropo <jorro...@gmail.com>
    Gerrit-Attention: Keith Randall <k...@golang.org>
    Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
    Gerrit-Attention: Chencheng Jiang <dorb...@gmail.com>
    Gerrit-Comment-Date: Sat, 06 Jun 2026 17:41:51 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    unsatisfied_requirement
    open
    diffy

    Chencheng Jiang (Gerrit)

    unread,
    Jun 6, 2026, 3:36:41 PM (10 hours ago) Jun 6
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com
    Attention needed from Chencheng Jiang, Keith Randall and Martin Möhrmann

    Chencheng Jiang uploaded new patchset

    Chencheng Jiang uploaded patch set #2 to this change.
    Open in Gerrit

    Related details

    Attention is currently required from:
    • Chencheng Jiang
    • Keith Randall
    • Martin Möhrmann
    Submit Requirements:
    • requirement is not satisfiedCode-Review
    • requirement is not satisfiedNo-Unresolved-Comments
    • requirement is not satisfiedReview-Enforcement
    • requirement is not satisfiedTryBots-Pass
    Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
    Gerrit-MessageType: newpatchset
    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: I3488809453baba0ed39b14ec08947cf2304fdeb4
    Gerrit-Change-Number: 787820
    Gerrit-PatchSet: 2
    unsatisfied_requirement
    open
    diffy

    Chencheng Jiang (Gerrit)

    unread,
    Jun 6, 2026, 3:37:12 PM (10 hours ago) Jun 6
    to goph...@pubsubhelper.golang.org, Jorropo, Martin Möhrmann, Keith Randall, Gopher Robot, golang-co...@googlegroups.com
    Attention needed from Jorropo, Keith Randall and Martin Möhrmann

    Chencheng Jiang added 1 comment

    Patchset-level comments

    We want a complete solution that fails on absolutely all generic ops, not just consts.

    In other words you also need to handle for example `(And64 x (And32 y z))`.

    Chencheng Jiang

    Done

    Open in Gerrit

    Related details

    Attention is currently required from:
    • Jorropo
    • Keith Randall
    • Martin Möhrmann
    Submit Requirements:
      • requirement is not satisfiedCode-Review
      • requirement satisfiedNo-Unresolved-Comments
      • requirement is not satisfiedReview-Enforcement
      • requirement is not satisfiedTryBots-Pass
      Inspect html for hidden footers to help with email filtering. To unsubscribe visit settings. DiffyGerrit
      Gerrit-MessageType: comment
      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: I3488809453baba0ed39b14ec08947cf2304fdeb4
      Gerrit-Change-Number: 787820
      Gerrit-PatchSet: 2
      Gerrit-Owner: Chencheng Jiang <dorb...@gmail.com>
      Gerrit-Reviewer: Keith Randall <k...@golang.org>
      Gerrit-Reviewer: Martin Möhrmann <moeh...@google.com>
      Gerrit-CC: Gopher Robot <go...@golang.org>
      Gerrit-CC: Jorropo <jorro...@gmail.com>
      Gerrit-Attention: Keith Randall <k...@golang.org>
      Gerrit-Attention: Jorropo <jorro...@gmail.com>
      Gerrit-Attention: Martin Möhrmann <moeh...@google.com>
      Gerrit-Comment-Date: Sat, 06 Jun 2026 19:37:07 +0000
      Gerrit-HasComments: Yes
      Gerrit-Has-Labels: No
      Comment-In-Reply-To: Jorropo <jorro...@gmail.com>
      unsatisfied_requirement
      satisfied_requirement
      open
      diffy
      Reply all
      Reply to author
      Forward
      0 new messages