| 1 |
// Copyright 2021 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package cgotest |
| 6 |
|
| 7 |
/* |
| 8 |
typedef void *HANDLE; |
| 9 |
|
| 10 |
struct HWND__{int unused;}; typedef struct HWND__ *HWND; |
| 11 |
*/ |
| 12 |
import "C" |
| 13 |
|
| 14 |
import ( |
| 15 |
"testing" |
| 16 |
"unsafe" |
| 17 |
) |
| 18 |
|
| 19 |
func test42018(t *testing.T) { |
| 20 |
// Test that Windows handles are marked go:notinheap, by growing the |
| 21 |
// stack and checking for pointer adjustments. Trick from |
| 22 |
// test/fixedbugs/issue40954.go. |
| 23 |
var i int |
| 24 |
handle := C.HANDLE(unsafe.Pointer(uintptr(unsafe.Pointer(&i)))) |
| 25 |
recurseHANDLE(100, handle, uintptr(unsafe.Pointer(&i))) |
| 26 |
hwnd := C.HWND(unsafe.Pointer(uintptr(unsafe.Pointer(&i)))) |
| 27 |
recurseHWND(400, hwnd, uintptr(unsafe.Pointer(&i))) |
| 28 |
} |
| 29 |
|
| 30 |
//go:noinline |
| 31 |
func recurseHANDLE(n int, p C.HANDLE, v uintptr) { |
| 32 |
if n > 0 { |
| 33 |
recurseHANDLE(n-1, p, v) |
| 34 |
} |
| 35 |
if uintptr(unsafe.Pointer(p)) != v { |
| 36 |
panic("adjusted notinheap pointer") |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
//go:noinline |
| 41 |
func recurseHWND(n int, p C.HWND, v uintptr) { |
| 42 |
if n > 0 { |
| 43 |
recurseHWND(n-1, p, v) |
| 44 |
} |
| 45 |
if uintptr(unsafe.Pointer(p)) != v { |
| 46 |
panic("adjusted notinheap pointer") |
| 47 |
} |
| 48 |
} |