1// run
2
3// Copyright 2020 The Go Authors. All rights reserved.
4// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// This test makes sure unsafe-uintptr arguments are not
8// kept alive longer than expected.
9
10package main
11
12import (
13	"runtime"
14	"unsafe"
15)
16
17var done = make(chan bool)
18
19func setup() unsafe.Pointer {
20	s := "ok"
21	runtime.SetFinalizer(&s, func(p *string) { close(done) })
22	return unsafe.Pointer(&s)
23}
24
25//go:noinline
26//go:uintptrescapes
27func before(p uintptr) int {
28	runtime.GC()
29	select {
30	case <-done:
31		panic("GC early")
32	default:
33	}
34	return 0
35}
36
37func after() int {
38	runtime.GC()
39	runtime.GC()
40	<-done
41	return 0
42}
43
44func main() {
45	_ = before(uintptr(setup())) + after()
46}
47