Skip to content

cmd/compile: escape analysis doesn't handle interface conversions correctly #29353

Closed
@mdempsky

Description

@mdempsky

Running the program below prints something like

allocated 0xc00001c160
finalized 0xc00001c160
accessing 0xc00001c160

This is due to esc.go not recognizing that the conversion from []*T to interface{} involves a heap allocation, so x leaks to the heap. However, esc.go instead only records a flow from x to the return parameter.

This is handled correctly by my escape analysis rewrite, which I hope to submit next release cycle. But maybe someone wants to try fixing it in esc.go for this release.

package main

import "runtime"

type T [4]int

//go:noinline
func f(x []*T) interface{} {
	return x
}

func main() {
	c := make(chan int)
	p := new(T)
	println("allocated", p)
	runtime.SetFinalizer(p, func(q *T) { println("finalized", q); c <- 0 })
	var s [10]*T
	s[0] = p
	h := f(s[:])
	runtime.GC()
	<-c
	println("accessing", h.([]*T)[0])
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions