github.com/jwijenbergh/purego@v0.0.0-20240126093400-70ff3a61df13/callback_test.go (about)

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