1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2023 The Ebitengine Authors
3
4//go:build darwin || (linux && (386 || amd64 || arm || arm64 || loong64 || ppc64le || riscv64 || s390x))
5
6package purego_test
7
8import (
9 "fmt"
10 "os"
11 "path/filepath"
12 "runtime"
13 "testing"
14 "unsafe"
15
16 "github.com/bnema/purego"
17)
18
19// TestCallGoFromSharedLib is a test that checks for stack corruption on arm64
20// when C calls Go code from a non-Go thread in a dynamically loaded share library.
21func TestCallGoFromSharedLib(t *testing.T) {
22 libFileName := filepath.Join(t.TempDir(), "libcbtest.so")
23 t.Logf("Build %v", libFileName)
24
25 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_test.c")); err != nil {
26 t.Fatal(err)
27 }
28 defer os.Remove(libFileName)
29
30 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
31 if err != nil {
32 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
33 }
34
35 var callCallback func(p uintptr, s string) int
36 purego.RegisterLibFunc(&callCallback, lib, "callCallback")
37
38 goFunc := func(cstr *byte, n int) int {
39 s := string(unsafe.Slice(cstr, n))
40 t.Logf("FROM Go: %s\n", s)
41 return 1
42 }
43
44 const want = 10101
45 cb := purego.NewCallback(goFunc)
46 for i := 0; i < 10; i++ {
47 got := callCallback(cb, "a test string")
48 if got != want {
49 t.Fatalf("%d: callCallback() got %v want %v", i, got, want)
50 }
51 }
52}
53
54// TestCallbackStringConversion tests that C char* arguments are correctly
55// converted to Go strings when the callback signature uses string type.
56func TestCallbackStringConversion(t *testing.T) {
57 libFileName := filepath.Join(t.TempDir(), "libcbtest.so")
58 t.Logf("Build %v", libFileName)
59
60 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_test.c")); err != nil {
61 t.Fatal(err)
62 }
63 defer os.Remove(libFileName)
64
65 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
66 if err != nil {
67 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
68 }
69
70 var callCallback func(p uintptr, s string) int
71 purego.RegisterLibFunc(&callCallback, lib, "callCallback")
72
73 const expected = "a test string"
74 var got string
75 goFunc := func(s string, n int) int {
76 got = s
77 if len(s) != n {
78 t.Errorf("string length mismatch: got %d, want %d", len(s), n)
79 }
80 return 1
81 }
82
83 cb := purego.NewCallback(goFunc)
84 callCallback(cb, expected)
85
86 if got != expected {
87 t.Errorf("string callback got %q, want %q", got, expected)
88 }
89}
90
91func TestNewCallbackFloat64(t *testing.T) {
92 // This tests the maximum number of arguments a function to NewCallback can take
93 const (
94 expectCbTotal = -3
95 expectedCbTotalF = float64(36)
96 )
97 var cbTotal int
98 var cbTotalF float64
99 imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
100 f1, f2, f3, f4, f5, f6, f7, f8 float64,
101 ) {
102 cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9
103 cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8
104 })
105 var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9 int,
106 f1, f2, f3, f4, f5, f6, f7, f8 float64)
107 purego.RegisterFunc(&fn, imp)
108 fn(1, 2, -3, 4, -5, 6, -7, 8, -9,
109 1, 2, 3, 4, 5, 6, 7, 8)
110
111 if cbTotal != expectCbTotal {
112 t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
113 }
114 if cbTotalF != expectedCbTotalF {
115 t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
116 }
117}
118
119func TestNewCallbackFloat64AndIntMix(t *testing.T) {
120 // This tests interleaving float and integer arguments to NewCallback
121 const (
122 expectCbTotal = 54.75
123 )
124 var cbTotal float64
125 imp := purego.NewCallback(func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int) {
126 cbTotal = a1 + a2 + float64(a3) + float64(a4) + float64(a5) + a6 + a7 + a8 + float64(a9)
127 })
128 var fn func(a1, a2 float64, a3, a4, a5 int, a6, a7, a8 float64, a9 int)
129 purego.RegisterFunc(&fn, imp)
130 fn(1.25, 3.25, 4, 5, 6, 7.5, 8.25, 9.5, 10)
131
132 if cbTotal != expectCbTotal {
133 t.Errorf("cbTotal not correct got %f but wanted %f", cbTotal, expectCbTotal)
134 }
135}
136
137func TestNewCallbackFloat32(t *testing.T) {
138 // This tests the maximum number of float32 arguments a function to NewCallback can take
139 const (
140 expectCbTotal = 6
141 expectedCbTotalF = float32(45)
142 )
143 var cbTotal int
144 var cbTotalF float32
145 imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8 int,
146 f1, f2, f3, f4, f5, f6, f7, f8, f9 float32,
147 ) {
148 cbTotal = a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
149 cbTotalF = f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9
150 })
151 var fn func(a1, a2, a3, a4, a5, a6, a7, a8 int,
152 f1, f2, f3, f4, f5, f6, f7, f8, f9 float32)
153 purego.RegisterFunc(&fn, imp)
154 fn(1, 2, -3, 4, -5, 6, -7, 8,
155 1, 2, 3, 4, 5, 6, 7, 8, 9)
156
157 if cbTotal != expectCbTotal {
158 t.Errorf("cbTotal not correct got %d but wanted %d", cbTotal, expectCbTotal)
159 }
160 if cbTotalF != expectedCbTotalF {
161 t.Errorf("cbTotalF not correct got %f but wanted %f", cbTotalF, expectedCbTotalF)
162 }
163}
164
165func TestNewCallbackFloat32AndFloat64(t *testing.T) {
166 if runtime.GOARCH == "s390x" {
167 // S390X has only 4 float registers (F0,F2,F4,F6), so 15 floats requires
168 // 11 stack slots, but only 10 are available (maxArgs - numIntRegs = 15 - 5)
169 t.Skip("Test requires too many stack arguments for s390x")
170 }
171 // This tests that calling a function with a mix of float32 and float64 arguments works
172 const (
173 expectedCbTotalF32 = float32(72)
174 expectedCbTotalF64 = float64(48)
175 )
176 var cbTotalF32 float32
177 var cbTotalF64 float64
178 imp := purego.NewCallback(func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32) {
179 cbTotalF32 = f1 + f2 + f3 + f7 + f8 + f9 + f13 + f14 + f15
180 cbTotalF64 = f4 + f5 + f6 + f10 + f11 + f12
181 })
182 var fn func(f1, f2, f3 float32, f4, f5, f6 float64, f7, f8, f9 float32, f10, f11, f12 float64, f13, f14, f15 float32)
183 purego.RegisterFunc(&fn, imp)
184 fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
185
186 if cbTotalF32 != expectedCbTotalF32 {
187 t.Errorf("cbTotalF32 not correct got %f but wanted %f", cbTotalF32, expectedCbTotalF32)
188 }
189 if cbTotalF64 != expectedCbTotalF64 {
190 t.Errorf("cbTotalF64 not correct got %f but wanted %f", cbTotalF64, expectedCbTotalF64)
191 }
192}
193
194func ExampleNewCallback() {
195 cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int {
196 fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
197 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15
198 })
199
200 var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int
201 purego.RegisterFunc(&fn, cb)
202
203 ret := fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15)
204 fmt.Println(ret)
205
206 // Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
207 // 120
208}
209
210func ExampleNewCallback_cdecl() {
211 fn := func(_ purego.CDecl, a int) {
212 fmt.Println(a)
213 }
214 cb := purego.NewCallback(fn)
215 purego.SyscallN(cb, 83)
216
217 // Output: 83
218}
219
220func TestCallbackInt32Packing(t *testing.T) {
221 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
222 t.Skip("callback tight packing only applies to darwin/arm64")
223 }
224
225 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
226 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
227 t.Fatal(err)
228 }
229 defer os.Remove(libFileName)
230
231 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
232 if err != nil {
233 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
234 }
235
236 var callCallback12Int32 func(cb uintptr) int32
237 purego.RegisterLibFunc(&callCallback12Int32, lib, "callCallback12Int32")
238
239 // Go callback that sums the 12 int32 arguments (prime numbers: 2,3,5,7,11,13,17,19,23,29,31,37)
240 goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 int32) int32 {
241 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12
242 }
243
244 cb := purego.NewCallback(goFunc)
245 got := callCallback12Int32(cb)
246 want := int32(2 + 3 + 5 + 7 + 11 + 13 + 17 + 19 + 23 + 29 + 31 + 37) // 197
247 if got != want {
248 t.Errorf("callCallback12Int32() = %d, want %d", got, want)
249 }
250}
251
252func TestCallbackMixedStackPacking(t *testing.T) {
253 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
254 t.Skip("callback tight packing only applies to darwin/arm64")
255 }
256
257 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
258 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
259 t.Fatal(err)
260 }
261 defer os.Remove(libFileName)
262
263 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
264 if err != nil {
265 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
266 }
267
268 var callCallbackMixedStack func(cb uintptr) int64
269 purego.RegisterLibFunc(&callCallbackMixedStack, lib, "callCallbackMixedStack")
270
271 // Go callback: 8 int64s in regs, then int32(100), int64(200), int32(300) on stack
272 goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8 int64, s1 int32, s2 int64, s3 int32) int64 {
273 // Return sum to verify all args received correctly
274 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + int64(s1) + s2 + int64(s3)
275 }
276
277 cb := purego.NewCallback(goFunc)
278 got := callCallbackMixedStack(cb)
279 want := int64(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 100 + 200 + 300) // 636
280 if got != want {
281 t.Errorf("callCallbackMixedStack() = %d, want %d", got, want)
282 }
283}
284
285func TestCallbackSmallTypesPacking(t *testing.T) {
286 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
287 t.Skip("callback tight packing only applies to darwin/arm64")
288 }
289
290 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
291 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
292 t.Fatal(err)
293 }
294 defer os.Remove(libFileName)
295
296 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
297 if err != nil {
298 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
299 }
300
301 var callCallbackSmallTypes func(cb uintptr) int64
302 purego.RegisterLibFunc(&callCallbackSmallTypes, lib, "callCallbackSmallTypes")
303
304 // Values: 1,2,3,4,5,6,7,8 (regs), true, -42, 200, -1000, 50000, 123456 (stack)
305 var gotBool bool
306 var gotI8 int8
307 var gotU8 uint8
308 var gotI16 int16
309 var gotU16 uint16
310 var gotI32 int32
311
312 goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8 int64,
313 b bool, i8 int8, u8 uint8, i16 int16, u16 uint16, i32 int32) int64 {
314 gotBool = b
315 gotI8 = i8
316 gotU8 = u8
317 gotI16 = i16
318 gotU16 = u16
319 gotI32 = i32
320 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8
321 }
322
323 cb := purego.NewCallback(goFunc)
324 got := callCallbackSmallTypes(cb)
325
326 // Check register args sum
327 want := int64(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) // 36
328 if got != want {
329 t.Errorf("register args sum = %d, want %d", got, want)
330 }
331
332 // Check stack args
333 if gotBool != true {
334 t.Errorf("bool = %v, want true", gotBool)
335 }
336 if gotI8 != -42 {
337 t.Errorf("int8 = %d, want -42", gotI8)
338 }
339 if gotU8 != 200 {
340 t.Errorf("uint8 = %d, want 200", gotU8)
341 }
342 if gotI16 != -1000 {
343 t.Errorf("int16 = %d, want -1000", gotI16)
344 }
345 if gotU16 != 50000 {
346 t.Errorf("uint16 = %d, want 50000", gotU16)
347 }
348 if gotI32 != 123456 {
349 t.Errorf("int32 = %d, want 123456", gotI32)
350 }
351}
352
353func TestCallback10Int32Packing(t *testing.T) {
354 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
355 t.Skip("callback tight packing only applies to darwin/arm64")
356 }
357
358 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
359 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
360 t.Fatal(err)
361 }
362 defer os.Remove(libFileName)
363
364 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
365 if err != nil {
366 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
367 }
368
369 var callCallback10Int32 func(cb uintptr) int32
370 purego.RegisterLibFunc(&callCallback10Int32, lib, "callCallback10Int32")
371
372 goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 int32) int32 {
373 return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10
374 }
375
376 cb := purego.NewCallback(goFunc)
377 got := callCallback10Int32(cb)
378 want := int32(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) // 55
379 if got != want {
380 t.Errorf("callCallback10Int32() = %d, want %d", got, want)
381 }
382}
383
384func TestCallbackFloat64StackPacking(t *testing.T) {
385 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
386 t.Skip("callback tight packing only applies to darwin/arm64")
387 }
388
389 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
390 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
391 t.Fatal(err)
392 }
393 defer os.Remove(libFileName)
394
395 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
396 if err != nil {
397 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
398 }
399
400 var callCallback10Float64 func(cb uintptr) int64
401 purego.RegisterLibFunc(&callCallback10Float64, lib, "callCallback10Float64")
402
403 // 10 float64s: 8 in registers, 2 on stack
404 // Return int64 since callbacks don't support float returns
405 goFunc := func(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10 float64) int64 {
406 sum := f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9 + f10
407 return int64(sum * 10) // 60.0 * 10 = 600
408 }
409
410 cb := purego.NewCallback(goFunc)
411 got := callCallback10Float64(cb)
412 want := int64(600)
413 if got != want {
414 t.Errorf("callCallback10Float64() = %d, want %d", got, want)
415 }
416}
417
418func TestCallbackFloat32StackPacking(t *testing.T) {
419 if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" {
420 t.Skip("callback tight packing only applies to darwin/arm64")
421 }
422
423 libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so")
424 if err := buildSharedLib("CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil {
425 t.Fatal(err)
426 }
427 defer os.Remove(libFileName)
428
429 lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL)
430 if err != nil {
431 t.Fatalf("Dlopen(%q) failed: %v", libFileName, err)
432 }
433
434 var callCallback12Float32 func(cb uintptr) int64
435 purego.RegisterLibFunc(&callCallback12Float32, lib, "callCallback12Float32")
436
437 // 12 float32s: 8 in registers, 4 on stack
438 // Return int64 since callbacks don't support float returns
439 goFunc := func(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12 float32) int64 {
440 sum := f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9 + f10 + f11 + f12
441 return int64(sum) // 78
442 }
443
444 cb := purego.NewCallback(goFunc)
445 got := callCallback12Float32(cb)
446 want := int64(78)
447 if got != want {
448 t.Errorf("callCallback12Float32() = %d, want %d", got, want)
449 }
450}