Source file misc/cgo/test/issue42018_windows.go

     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  func recurseHANDLE(n int, p C.HANDLE, v uintptr) {
    31  	if n > 0 {
    32  		recurseHANDLE(n-1, p, v)
    33  	}
    34  	if uintptr(unsafe.Pointer(p)) != v {
    35  		panic("adjusted notinheap pointer")
    36  	}
    37  }
    38  
    39  func recurseHWND(n int, p C.HWND, v uintptr) {
    40  	if n > 0 {
    41  		recurseHWND(n-1, p, v)
    42  	}
    43  	if uintptr(unsafe.Pointer(p)) != v {
    44  		panic("adjusted notinheap pointer")
    45  	}
    46  }
    47  

View as plain text