2020-08-26 15:17:35 -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.
|
|
|
|
|
2022-09-01 17:31:04 -06:00
|
|
|
//go:build cgo
|
2022-08-07 11:14:43 -06:00
|
|
|
|
2020-08-26 15:17:35 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-08-07 11:14:43 -06:00
|
|
|
"runtime/cgo"
|
2020-08-26 15:17:35 -06:00
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2022-08-07 11:14:43 -06:00
|
|
|
type S struct {
|
|
|
|
_ cgo.Incomplete
|
|
|
|
x int
|
|
|
|
}
|
2020-08-26 15:17:35 -06:00
|
|
|
|
|
|
|
func main() {
|
|
|
|
var i int
|
|
|
|
p := (*S)(unsafe.Pointer(uintptr(unsafe.Pointer(&i))))
|
|
|
|
v := uintptr(unsafe.Pointer(p))
|
2022-08-07 11:14:43 -06:00
|
|
|
// p is a pointer to a not-in-heap type. Like some C libraries,
|
2020-08-26 15:17:35 -06:00
|
|
|
// we stored an integer in that pointer. That integer just happens
|
|
|
|
// to be the address of i.
|
|
|
|
// v is also the address of i.
|
2022-08-07 11:14:43 -06:00
|
|
|
// p has a base type which is marked not-in-heap, so it
|
2020-08-26 15:17:35 -06:00
|
|
|
// should not be adjusted when the stack is copied.
|
|
|
|
recurse(100, p, v)
|
|
|
|
}
|
|
|
|
func recurse(n int, p *S, v uintptr) {
|
|
|
|
if n > 0 {
|
|
|
|
recurse(n-1, p, v)
|
|
|
|
}
|
|
|
|
if uintptr(unsafe.Pointer(p)) != v {
|
|
|
|
panic("adjusted notinheap pointer")
|
|
|
|
}
|
|
|
|
}
|