issue4029.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2012 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. //go:build !windows && !static && (!darwin || (!internal_pie && !arm64))
  5. // +build !windows
  6. // +build !static
  7. // +build !darwin !internal_pie,!arm64
  8. // Excluded in darwin internal linking PIE mode, as dynamic export is not
  9. // supported.
  10. // Excluded in internal linking mode on darwin/arm64, as it is always PIE.
  11. package cgotest
  12. /*
  13. #include <stdint.h>
  14. #include <dlfcn.h>
  15. #cgo linux LDFLAGS: -ldl
  16. extern uintptr_t dlopen4029(char*, int);
  17. extern uintptr_t dlsym4029(uintptr_t, char*);
  18. extern int dlclose4029(uintptr_t);
  19. extern void call4029(uintptr_t arg);
  20. */
  21. import "C"
  22. import (
  23. "testing"
  24. )
  25. var callbacks int
  26. //export IMPIsOpaque
  27. func IMPIsOpaque() {
  28. callbacks++
  29. }
  30. //export IMPInitWithFrame
  31. func IMPInitWithFrame() {
  32. callbacks++
  33. }
  34. //export IMPDrawRect
  35. func IMPDrawRect() {
  36. callbacks++
  37. }
  38. //export IMPWindowResize
  39. func IMPWindowResize() {
  40. callbacks++
  41. }
  42. func test4029(t *testing.T) {
  43. loadThySelf(t, "IMPWindowResize")
  44. loadThySelf(t, "IMPDrawRect")
  45. loadThySelf(t, "IMPInitWithFrame")
  46. loadThySelf(t, "IMPIsOpaque")
  47. if callbacks != 4 {
  48. t.Errorf("got %d callbacks, expected 4", callbacks)
  49. }
  50. }
  51. func loadThySelf(t *testing.T, symbol string) {
  52. this_process := C.dlopen4029(nil, C.RTLD_NOW)
  53. if this_process == 0 {
  54. t.Error("dlopen:", C.GoString(C.dlerror()))
  55. return
  56. }
  57. defer C.dlclose4029(this_process)
  58. symbol_address := C.dlsym4029(this_process, C.CString(symbol))
  59. if symbol_address == 0 {
  60. t.Error("dlsym:", C.GoString(C.dlerror()))
  61. return
  62. }
  63. t.Log(symbol, symbol_address)
  64. C.call4029(symbol_address)
  65. }