You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cmd/compile: struct interface{} value lost passing by value #30956
package main
import "fmt"
type X struct {
V interface{}
a uint64
b uint64
c uint64
}
func pr(x X) {
fmt.Println(x.V)
}
func main() {
pr(X{
V: struct {
A int
}{42},
})
}
What did you expect to see?
{42} printed.
What did you see instead?
With Go 1.12.1, this program prints <nil>. With Go 1.11.6, it prints {42} as expected.
changed the title [-]struct interface{} value lost passing by value[/-][+]cmd/compile: struct interface{} value lost passing by value[/+]on Mar 21, 2019
Thanks for bisecting! Based on where that landed, I suspect that that commit uncovered a latent bug. Looking at sinit.go on my phone, it looks like we need to add special handling for pointer-to-zero-width-val when constructing static interface data.
It looks to me that walk decided to use fixedlit to generate the struct literal X, because it isStaticCompositeLiteral. However, fixedlit doesn't know how to handle CONVIFACE.
Maybe we should teach fixedlit or anylit handle CONVIFACE.
Alternatively, maybe Order.addrTemp should handle that. It already handles CONVIFACE of LITERAL, but not composite literal. And there is a TODO: // TODO: expand this to all static composite literal nodes?.
diff --git a/src/cmd/compile/internal/gc/order.go b/src/cmd/compile/internal/gc/order.go
index 7b86537a21..78a8d12db3 100644
--- a/src/cmd/compile/internal/gc/order.go
+++ b/src/cmd/compile/internal/gc/order.go
@@ -1054,7 +1054,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
if n.Left.Type.IsInterface() {
break
}
- if _, needsaddr := convFuncName(n.Left.Type, n.Type); needsaddr || consttype(n.Left) > 0 {
+ if _, needsaddr := convFuncName(n.Left.Type, n.Type); needsaddr || consttype(n.Left) > 0 || n.Left.Type.IsStruct() {
// Need a temp if we need to pass the address to the conversion function.
// We also process constants here, making a named static global whose
// address we can put directly in an interface (see OCONVIFACE case in walk).
The problem gone, go-tip tool dist test passed. But I'm afraid it won't cover all the cases.
If both of you feel ok with that fix, I will send a CL.
I think this is probably on the right direction. But special-casing structs seems not the best way. At least, I think we should also handle arrays. And not all structs need addrTemp.
Also, ideally, I think this code is supposed to handle static data and generate static data. With your change, it doesn't generate static data for X. This may be fine, as Go 1.11 doesn't either.
Maybe conditioning on isStaticCompositeLiteral is better. I'm not really sure at the moment.
Activity
elagergren-spideroak commentedon Mar 20, 2019
Note that it only occurs when not assigned to a variable.
It also seems to occur only when certain fields are added to
Xas well.bradfitz commentedon Mar 20, 2019
/cc @randall77 @josharian @griesemer @ianlancetaylor
cherrymui commentedon Mar 20, 2019
It seems that now we generate static data for X. However, the symbol
..stmp_0doesn't seem to contain any data.(no content for the stmp_0 symbol)
(the address is overlapping with the next symbol.)
[-]struct interface{} value lost passing by value[/-][+]cmd/compile: struct interface{} value lost passing by value[/+]cuonglm commentedon Mar 21, 2019
git bisect points to 0e9f8a2 as the first bad commit
josharian commentedon Mar 21, 2019
Thanks for bisecting! Based on where that landed, I suspect that that commit uncovered a latent bug. Looking at sinit.go on my phone, it looks like we need to add special handling for pointer-to-zero-width-val when constructing static interface data.
josharian commentedon Mar 21, 2019
Looks like somewhere around line 522 in sinit.go we need to use runtime.zerobase for this case.
cherrymui commentedon Mar 21, 2019
It looks to me that
walkdecided to usefixedlitto generate the struct literal X, because itisStaticCompositeLiteral. However,fixedlitdoesn't know how to handleCONVIFACE.Maybe we should teach
fixedlitoranylithandleCONVIFACE.Alternatively, maybe
Order.addrTempshould handle that. It already handlesCONVIFACEofLITERAL, but not composite literal. And there is a TODO:// TODO: expand this to all static composite literal nodes?.cuonglm commentedon Mar 22, 2019
@josharian @cherrymui I did some investigation, and found the culprit is this line 0e9f8a2#diff-f429d083e18b2e96d5c8ebeae38f3c54R1052
Before that change, everything except interface will be added temporary address. So when
fixedlitruns ingo/src/cmd/compile/internal/gc/sinit.go
Line 739 in 4906a00
With that change, the struct does not have temp address, the process become:
Interestingly, if I added an addressable field, then problem gone, see: https://play.golang.org/p/DnCxNoFUB0P
I simply fix by this change:
The problem gone,
go-tip tool dist testpassed. But I'm afraid it won't cover all the cases.If both of you feel ok with that fix, I will send a CL.
Any idea?
cc @randall77
cherrymui commentedon Mar 22, 2019
@Gnouc Thanks for digging into this!
I think this is probably on the right direction. But special-casing structs seems not the best way. At least, I think we should also handle arrays. And not all structs need addrTemp.
Also, ideally, I think this code is supposed to handle static data and generate static data. With your change, it doesn't generate static data for X. This may be fine, as Go 1.11 doesn't either.
Maybe conditioning on
isStaticCompositeLiteralis better. I'm not really sure at the moment.cherrymui commentedon Mar 22, 2019
I think this is because with the map field, X is not static any more.
cuonglm commentedon Mar 22, 2019
@cherrymui
isStaticCompositeLiteralworks 👍 I'm sending a CLgopherbot commentedon Mar 22, 2019
Change https://golang.org/cl/168858 mentions this issue:
cmd/compile: fix literal struct interface {} lost passing by valuerandall77 commentedon Apr 2, 2019
@gopherbot, please consider this for backport to 1.12, it's a regression.
gopherbot commentedon Apr 2, 2019
Backport issue(s) opened: #31209 (for 1.12).
Remember to create the cherry-pick CL(s) as soon as the patch is submitted to master, according to https://golang.org/wiki/MinorReleases.