| 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 |
package main |
| 8 |
|
| 9 |
import ( |
| 10 |
"unsafe" |
| 11 |
) |
| 12 |
|
| 13 |
//go:notinheap |
| 14 |
type S struct{ x int } |
| 15 |
|
| 16 |
func main() { |
| 17 |
var i int |
| 18 |
p := (*S)(unsafe.Pointer(uintptr(unsafe.Pointer(&i)))) |
| 19 |
v := uintptr(unsafe.Pointer(p)) |
| 20 |
// p is a pointer to a go:notinheap type. Like some C libraries, |
| 21 |
// we stored an integer in that pointer. That integer just happens |
| 22 |
// to be the address of i. |
| 23 |
// v is also the address of i. |
| 24 |
// p has a base type which is marked go:notinheap, so it |
| 25 |
// should not be adjusted when the stack is copied. |
| 26 |
recurse(100, p, v) |
| 27 |
} |
| 28 |
func recurse(n int, p *S, v uintptr) { |
| 29 |
if n > 0 { |
| 30 |
recurse(n-1, p, v) |
| 31 |
} |
| 32 |
if uintptr(unsafe.Pointer(p)) != v { |
| 33 |
panic("adjusted notinheap pointer") |
| 34 |
} |
| 35 |
} |