issue42018_windows.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. package cgotest
  5. /*
  6. typedef void *HANDLE;
  7. struct HWND__{int unused;}; typedef struct HWND__ *HWND;
  8. */
  9. import "C"
  10. import (
  11. "testing"
  12. "unsafe"
  13. )
  14. func test42018(t *testing.T) {
  15. // Test that Windows handles are marked go:notinheap, by growing the
  16. // stack and checking for pointer adjustments. Trick from
  17. // test/fixedbugs/issue40954.go.
  18. var i int
  19. handle := C.HANDLE(unsafe.Pointer(uintptr(unsafe.Pointer(&i))))
  20. recurseHANDLE(100, handle, uintptr(unsafe.Pointer(&i)))
  21. hwnd := C.HWND(unsafe.Pointer(uintptr(unsafe.Pointer(&i))))
  22. recurseHWND(400, hwnd, uintptr(unsafe.Pointer(&i)))
  23. }
  24. func recurseHANDLE(n int, p C.HANDLE, v uintptr) {
  25. if n > 0 {
  26. recurseHANDLE(n-1, p, v)
  27. }
  28. if uintptr(unsafe.Pointer(p)) != v {
  29. panic("adjusted notinheap pointer")
  30. }
  31. }
  32. func recurseHWND(n int, p C.HWND, v uintptr) {
  33. if n > 0 {
  34. recurseHWND(n-1, p, v)
  35. }
  36. if uintptr(unsafe.Pointer(p)) != v {
  37. panic("adjusted notinheap pointer")
  38. }
  39. }