github.com/ebitengine/purego@v0.8.0-alpha.2.0.20240512170805-6cd12240d332/callback_test.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // SPDX-FileCopyrightText: 2023 The Ebitengine Authors
     3  
     4  //go:build darwin || (linux && (amd64 || arm64))
     5  
     6  package purego_test
     7  
     8  import (
     9  	"fmt"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  	"unsafe"
    14  
    15  	"github.com/ebitengine/purego"
    16  )
    17  
    18  // TestCallGoFromSharedLib is a test that checks for stack corruption on arm64
    19  // when C calls Go code from a non-Go thread in a dynamically loaded share library.
    20  func TestCallGoFromSharedLib(t *testing.T) {
    21  	libFileName := filepath.Join(t.TempDir(), "libcbtest.so")
    22  	t.Logf("Build %v", libFileName)
    23  
    24  	if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_test.c")); err != nil {
    25  		t.Fatal(err)
    26  	}
    27  	defer os.Remove(libFileName)
    28  
    29  	lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
    30  	if err != nil {
    31  		t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
    32  	}
    33  
    34  	var callCallback func(p uintptr, s string) int
    35  	purego.RegisterLibFunc(&callCallback, lib, "callCallback")
    36  
    37  	goFunc := func(cstr *byte, n int) int {
    38  		s := string(unsafe.Slice(cstr, n))
    39  		t.Logf("FROM Go: %s\n", s)
    40  		return 1
    41  	}
    42  
    43  	const want = 10101
    44  	cb := purego.NewCallback(goFunc)
    45  	for i := 0; i < 10; i++ {
    46  		got := callCallback(cb, "a test string")
    47  		if got != want {
    48  			t.Fatalf("%d: callCallback() got %v want %v", i, got, want)
    49  		}
    50  	}
    51  }
    52  
    53  func TestNewCallbackFloat64(t *testing.T) {
    54  	// This tests the maximum number of arguments a function to NewCallback can take
    55  	const (
    56  		expectCbTotal    = -3
    57  		expectedCbTotalF = float64(36)
    58  	)
    59  	var cbTotal int
    60  	var cbTotalF float64
    61  	imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
    62  		f1, f2, f3, f4, f5, f6, f7, f8 float64,
    63  	) {
    64  		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
    65  		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8
    66  	})
    67  	var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
    68  		f1, f2, f3, f4, f5, f6, f7, f8 float64)
    69  	purego.RegisterFunc(&fn, imp)
    70  	fn(1, 2, -3, 4, -5, 6, -7, 8, -9,
    71  		1, 2, 3, 4, 5, 6, 7, 8)
    72  
    73  	if cbTotal != expectCbTotal {
    74  		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
    75  	}
    76  	if cbTotalF != expectedCbTotalF {
    77  		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
    78  	}
    79  }
    80  
    81  func TestNewCallbackFloat64AndIntMix(t *testing.T) {
    82  	// This tests interleaving float and integer arguments to NewCallback
    83  	const (
    84  		expectCbTotal = 54.75
    85  	)
    86  	var cbTotal float64
    87  	imp := purego.NewCallback(func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int) {
    88  		cbTotal = a1 + a2 + float64(a3) + float64(a4) + float64(a5) + a6 + a7 + a8 + float64(a9)
    89  	})
    90  	var fn func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int)
    91  	purego.RegisterFunc(&fn, imp)
    92  	fn(1.25, 3.25, 4, 5, 6, 7.5, 8.25, 9.5, 10)
    93  
    94  	if cbTotal != expectCbTotal {
    95  		t.Errorf("cbTotal not correct got %f but wanted %f", cbTotal, expectCbTotal)
    96  	}
    97  }
    98  
    99  func TestNewCallbackFloat32(t *testing.T) {
   100  	// This tests the maximum number of float32 arguments a function to NewCallback can take
   101  	const (
   102  		expectCbTotal    = 6
   103  		expectedCbTotalF = float32(45)
   104  	)
   105  	var cbTotal int
   106  	var cbTotalF float32
   107  	imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8 int,
   108  		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32,
   109  	) {
   110  		cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
   111  		cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9
   112  	})
   113  	var fn func(a1, a2, a3, a4, a5, a6, a7, a8 int,
   114  		f1, f2, f3, f4, f5, f6, f7, f8, f9 float32)
   115  	purego.RegisterFunc(&fn, imp)
   116  	fn(1, 2, -3, 4, -5, 6, -7, 8,
   117  		1, 2, 3, 4, 5, 6, 7, 8, 9)
   118  
   119  	if cbTotal != expectCbTotal {
   120  		t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
   121  	}
   122  	if cbTotalF != expectedCbTotalF {
   123  		t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
   124  	}
   125  }
   126  
   127  func TestNewCallbackFloat32AndFloat64(t *testing.T) {
   128  	// This tests that calling a function with a mix of float32 and float64 arguments works
   129  	const (
   130  		expectedCbTotalF32 = float32(72)
   131  		expectedCbTotalF64 = float64(48)
   132  	)
   133  	var cbTotalF32 float32
   134  	var cbTotalF64 float64
   135  	imp := purego.NewCallback(func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32) {
   136  		cbTotalF32 = f1 + f2 + f3 + f7 + f8 + f9 + f13 + f14 + f15
   137  		cbTotalF64 = f4 + f5 + f6 + f10 + f11 + f12
   138  	})
   139  	var fn func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32)
   140  	purego.RegisterFunc(&fn, imp)
   141  	fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
   142  
   143  	if cbTotalF32 != expectedCbTotalF32 {
   144  		t.Errorf("cbTotalF32 not correct got %f but wanted %f", cbTotalF32, expectedCbTotalF32)
   145  	}
   146  	if cbTotalF64 != expectedCbTotalF64 {
   147  		t.Errorf("cbTotalF64 not correct got %f but wanted %f", cbTotalF64, expectedCbTotalF64)
   148  	}
   149  }
   150  
   151  func ExampleNewCallback() {
   152  	cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int {
   153  		fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
   154  		return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15
   155  	})
   156  
   157  	var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int
   158  	purego.RegisterFunc(&fn, cb)
   159  
   160  	ret := fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
   161  	fmt.Println(ret)
   162  
   163  	// Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   164  	// 120
   165  }
   166  
   167  func ExampleNewCallback_cdecl() {
   168  	fn := func(_ purego.CDecl, a int) {
   169  		fmt.Println(a)
   170  	}
   171  	cb := purego.NewCallback(fn)
   172  	purego.SyscallN(cb, 83)
   173  
   174  	// Output: 83
   175  }