v0.10.0-alpha.4 49bede1

callback_test.go RawHistoryFolder

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

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 1

Tags 102

↑↓ moveenter openesc close