]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.20] cmd/compile: fix inline static init arguments substitued...
authorCuong Manh Le <cuong.manhle.vn@gmail.com>
Sun, 5 Feb 2023 07:33:32 +0000 (14:33 +0700)
committerDavid Chase <drchase@google.com>
Thu, 9 Feb 2023 17:29:22 +0000 (17:29 +0000)
Blank node must be ignored when building arguments substitued tree.
Otherwise, it could be used to replace other blank node in left hand
side of an assignment, causing an invalid IR node.

Consider the following code:

type S1 struct {
s2 S2
}

type S2 struct{}

func (S2) Make() S2 {
return S2{}
}

func (S1) Make() S1 {
return S1{s2: S2{}.Make()}
}

var _ = S1{}.Make()

After staticAssignInlinedCall, the assignment becomes:

var _ = S1{s2: S2{}.Make()}

and the arg substitued tree is "map[*ir.Name]ir.Node{_: S1{}}". Now,
when doing static assignment, if there is any assignment to blank node,
for example:

_ := S2{}

That blank node will be replaced with "S1{}":

S1{} := S2{}

So constructing an invalid IR which causes the ICE.

Fixes #58335

Change-Id: I21b48357f669a7e02a7eb4325246aadc31f78fb9
Reviewed-on: https://go-review.googlesource.com/c/go/+/465098
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Auto-Submit: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Keith Randall <khr@golang.org>
Reviewed-by: Keith Randall <khr@google.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-on: https://go-review.googlesource.com/c/go/+/466275
Reviewed-by: Than McIntosh <thanm@google.com>
src/cmd/compile/internal/staticinit/sched.go
test/fixedbugs/issue58325.go [new file with mode: 0644]

index bd1bf4114d334ce3ac635cb064dba6bcdee660c0..2bfb5d79b22761994e57f9070d3f1a2793996068 100644 (file)
@@ -615,6 +615,9 @@ func (s *Schedule) staticAssignInlinedCall(l *ir.Name, loff int64, call *ir.Inli
        // Build tree with args substituted for params and try it.
        args := make(map[*ir.Name]ir.Node)
        for i, v := range as2init.Lhs {
+               if ir.IsBlank(v) {
+                       continue
+               }
                args[v.(*ir.Name)] = as2init.Rhs[i]
        }
        r, ok := subst(as2body.Rhs[0], args)
diff --git a/test/fixedbugs/issue58325.go b/test/fixedbugs/issue58325.go
new file mode 100644 (file)
index 0000000..d37089c
--- /dev/null
@@ -0,0 +1,23 @@
+// compile
+
+// Copyright 2023 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 p
+
+type S1 struct {
+       s2 S2
+}
+
+type S2 struct{}
+
+func (S2) Make() S2 {
+       return S2{}
+}
+
+func (S1) Make() S1 {
+       return S1{s2: S2{}.Make()}
+}
+
+var _ = S1{}.Make()