From 5579ee169f12815660eb9ee66afd5f2abc77864f Mon Sep 17 00:00:00 2001 From: Cherry Zhang Date: Thu, 1 Apr 2021 14:21:12 -0400 Subject: [PATCH] cmd/compile: in expand calls, preserve pointer store type but decompose aggregate args In CL 305672 we preserve the pointer type of a store by just not decomposing it. But this can be problematic when the source of the store is a direct interface aggregate type (e.g. struct { x map[int]int }. In this CL we take a different approach: we preserve the store type when generating the new store, but also decompose the source. Fixes #45344. Change-Id: If5dd496458dee95aa649c6d106b96a6cdcf3e60d Reviewed-on: https://go-review.googlesource.com/c/go/+/306669 Trust: Cherry Zhang Run-TryBot: Cherry Zhang TryBot-Result: Go Bot Reviewed-by: David Chase --- src/cmd/compile/internal/ssa/expand_calls.go | 11 ++++++++--- test/fixedbugs/issue45344.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) create mode 100644 test/fixedbugs/issue45344.go diff --git a/src/cmd/compile/internal/ssa/expand_calls.go b/src/cmd/compile/internal/ssa/expand_calls.go index 2935236473..a5fe6f4d29 100644 --- a/src/cmd/compile/internal/ssa/expand_calls.go +++ b/src/cmd/compile/internal/ssa/expand_calls.go @@ -589,6 +589,14 @@ func (x *expandState) decomposeArg(pos src.XPos, b *Block, source, mem *Value, t if w == nil { w = x.newArgToMemOrRegs(source, w, off, i, rt, pos) } + if t.IsPtrShaped() { + // Preserve the original store type. This ensures pointer type + // properties aren't discarded (e.g, notinheap). + if rt.Width != t.Width || len(pa.Registers) != 1 || i != loadRegOffset { + b.Func.Fatalf("incompatible store type %v and %v, i=%d", t, rt, i) + } + rt = t + } mem = x.storeArgOrLoad(pos, b, w, mem, rt, storeOffset+off, i, storeRc.next(rt)) } return mem @@ -1114,9 +1122,6 @@ func expandCalls(f *Func) { for _, v := range b.Values { if v.Op == OpStore { t := v.Aux.(*types.Type) - if t.IsPtrShaped() { // Everything already fits, and this ensures pointer type properties aren't discarded (e.g, notinheap) - continue - } source := v.Args[1] tSrc := source.Type iAEATt := x.isAlreadyExpandedAggregateType(t) diff --git a/test/fixedbugs/issue45344.go b/test/fixedbugs/issue45344.go new file mode 100644 index 0000000000..70c3d8970d --- /dev/null +++ b/test/fixedbugs/issue45344.go @@ -0,0 +1,20 @@ +// compile + +// Copyright 2021 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. + +// Issue 45344: expand_calls does not handle direct interface +// typed argument well. + +package p + +type T struct { + a map[int]int +} + +func F(t T) { + G(t) +} + +func G(...interface{})