2020-09-11 12:57:27 -06:00
|
|
|
// run
|
|
|
|
|
|
|
|
// Copyright 2020 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.
|
|
|
|
|
|
|
|
// This test makes sure unsafe-uintptr arguments are not
|
|
|
|
// kept alive longer than expected.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"runtime"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2020-09-13 00:22:42 -06:00
|
|
|
var done = make(chan bool)
|
2020-09-11 12:57:27 -06:00
|
|
|
|
|
|
|
func setup() unsafe.Pointer {
|
|
|
|
s := "ok"
|
2020-09-13 00:22:42 -06:00
|
|
|
runtime.SetFinalizer(&s, func(p *string) { close(done) })
|
2020-09-11 12:57:27 -06:00
|
|
|
return unsafe.Pointer(&s)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:noinline
|
|
|
|
//go:uintptrescapes
|
|
|
|
func before(p uintptr) int {
|
|
|
|
runtime.GC()
|
2020-09-13 00:22:42 -06:00
|
|
|
select {
|
|
|
|
case <-done:
|
2020-09-11 12:57:27 -06:00
|
|
|
panic("GC early")
|
2020-09-13 00:22:42 -06:00
|
|
|
default:
|
2020-09-11 12:57:27 -06:00
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func after() int {
|
|
|
|
runtime.GC()
|
2020-09-13 00:22:42 -06:00
|
|
|
runtime.GC()
|
|
|
|
<-done
|
2020-09-11 12:57:27 -06:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
_ = before(uintptr(setup())) + after()
|
|
|
|
}
|