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