| 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 |
|
| 6 |
package purego_test |
| 7 |
|
| 8 |
import ( |
| 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. |
| 21 |
func 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 |
|
| 54 |
func 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 |
|
| 82 |
func 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 |
|
| 100 |
func 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 |
|
| 128 |
func 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 |
|
| 157 |
func TestNewCallbackInt64Result(t *testing.T) { |
| 158 |
// A 64-bit result needs two words: EDX:EAX on 386 and R1:R0 on arm. |
| 159 |
// The trampoline must forward both halves of the value. |
| 160 |
const mask = int64(0x0123456789abcdef) |
| 161 |
imp := purego.NewCallback(func(v int64) int64 { return v ^ mask }) |
| 162 |
var fn func(v int64) int64 |
| 163 |
purego.RegisterFunc(&fn, imp) |
| 164 |
for _, v := range []int64{ |
| 165 |
0, |
| 166 |
-1, |
| 167 |
1 << 32, |
| 168 |
-0x7edcba9876543210, |
| 169 |
} { |
| 170 |
if got, want := fn(v), v^mask; got != want { |
| 171 |
t.Errorf("callback(%#x) = %#x, want %#x", uint64(v), uint64(got), uint64(want)) |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
func TestNewCallbackUint64Result(t *testing.T) { |
| 177 |
// The upper half of a uint64 result must not be dropped by the trampoline. |
| 178 |
imp := purego.NewCallback(func(lo, hi uint32) uint64 { return uint64(hi)<<32 | uint64(lo) }) |
| 179 |
var fn func(lo, hi uint32) uint64 |
| 180 |
purego.RegisterFunc(&fn, imp) |
| 181 |
const want = uint64(0x0123456789abcdef) |
| 182 |
if got := fn(0x89abcdef, 0x01234567); got != want { |
| 183 |
t.Errorf("callback() = %#x, want %#x", got, want) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func TestNewCallbackInt64ResultWithStackArgs(t *testing.T) { |
| 188 |
// On 386 all the arguments are passed on the stack, so this also checks |
| 189 |
// that the copied arguments do not overlap the result of the callback. |
| 190 |
// The number of arguments is kept small enough for ppc64le, which only |
| 191 |
// has maxArgs(15) slots in total, 8 of them in integer registers. |
| 192 |
const wantSum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 + 11 + 12 |
| 193 |
imp := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 int) int64 { |
| 194 |
return int64(a1+a2+a3+a4+a5+a6+a7+a8+a9+a10+a11+a12)<<32 | 0x0badf00d |
| 195 |
}) |
| 196 |
var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 int) int64 |
| 197 |
purego.RegisterFunc(&fn, imp) |
| 198 |
const want = int64(wantSum)<<32 | 0x0badf00d |
| 199 |
if got := fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); got != want { |
| 200 |
t.Errorf("callback() = %#x, want %#x", uint64(got), uint64(want)) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
func ExampleNewCallback() { |
| 205 |
cb := purego.NewCallback(func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int { |
| 206 |
fmt.Println(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15) |
| 207 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 + a13 + a14 + a15 |
| 208 |
}) |
| 209 |
|
| 210 |
var fn func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 int) int |
| 211 |
purego.RegisterFunc(&fn, cb) |
| 212 |
|
| 213 |
ret := fn(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15) |
| 214 |
fmt.Println(ret) |
| 215 |
|
| 216 |
// Output: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
| 217 |
// 120 |
| 218 |
} |
| 219 |
|
| 220 |
func ExampleNewCallback_cdecl() { |
| 221 |
fn := func(_ purego.CDecl, a int) { |
| 222 |
fmt.Println(a) |
| 223 |
} |
| 224 |
cb := purego.NewCallback(fn) |
| 225 |
purego.SyscallN(cb, 83) |
| 226 |
|
| 227 |
// Output: 83 |
| 228 |
} |
| 229 |
|
| 230 |
func TestCallbackInt32Packing(t *testing.T) { |
| 231 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 232 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 233 |
} |
| 234 |
|
| 235 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 236 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
defer os.Remove(libFileName) |
| 240 |
|
| 241 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 242 |
if err != nil { |
| 243 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 244 |
} |
| 245 |
|
| 246 |
var callCallback12Int32 func(cb uintptr) int32 |
| 247 |
purego.RegisterLibFunc(&callCallback12Int32, lib, "callCallback12Int32") |
| 248 |
|
| 249 |
// Go callback that sums the 12 int32 arguments (prime numbers: 2,3,5,7,11,13,17,19,23,29,31,37) |
| 250 |
goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12 int32) int32 { |
| 251 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 + a11 + a12 |
| 252 |
} |
| 253 |
|
| 254 |
cb := purego.NewCallback(goFunc) |
| 255 |
got := callCallback12Int32(cb) |
| 256 |
want := int32(2 + 3 + 5 + 7 + 11 + 13 + 17 + 19 + 23 + 29 + 31 + 37) // 197 |
| 257 |
if got != want { |
| 258 |
t.Errorf("callCallback12Int32() = %d, want %d", got, want) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func TestCallbackMixedStackPacking(t *testing.T) { |
| 263 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 264 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 265 |
} |
| 266 |
|
| 267 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 268 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 269 |
t.Fatal(err) |
| 270 |
} |
| 271 |
defer os.Remove(libFileName) |
| 272 |
|
| 273 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 274 |
if err != nil { |
| 275 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 276 |
} |
| 277 |
|
| 278 |
var callCallbackMixedStack func(cb uintptr) int64 |
| 279 |
purego.RegisterLibFunc(&callCallbackMixedStack, lib, "callCallbackMixedStack") |
| 280 |
|
| 281 |
// Go callback: 8 int64s in regs, then int32(100), int64(200), int32(300) on stack |
| 282 |
goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8 int64, s1 int32, s2 int64, s3 int32) int64 { |
| 283 |
// Return sum to verify all args received correctly |
| 284 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + int64(s1) + s2 + int64(s3) |
| 285 |
} |
| 286 |
|
| 287 |
cb := purego.NewCallback(goFunc) |
| 288 |
got := callCallbackMixedStack(cb) |
| 289 |
want := int64(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 100 + 200 + 300) // 636 |
| 290 |
if got != want { |
| 291 |
t.Errorf("callCallbackMixedStack() = %d, want %d", got, want) |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
func TestCallbackSmallTypesPacking(t *testing.T) { |
| 296 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 297 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 298 |
} |
| 299 |
|
| 300 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 301 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 302 |
t.Fatal(err) |
| 303 |
} |
| 304 |
defer os.Remove(libFileName) |
| 305 |
|
| 306 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 307 |
if err != nil { |
| 308 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 309 |
} |
| 310 |
|
| 311 |
var callCallbackSmallTypes func(cb uintptr) int64 |
| 312 |
purego.RegisterLibFunc(&callCallbackSmallTypes, lib, "callCallbackSmallTypes") |
| 313 |
|
| 314 |
// Values: 1,2,3,4,5,6,7,8 (regs), true, -42, 200, -1000, 50000, 123456 (stack) |
| 315 |
var gotBool bool |
| 316 |
var gotI8 int8 |
| 317 |
var gotU8 uint8 |
| 318 |
var gotI16 int16 |
| 319 |
var gotU16 uint16 |
| 320 |
var gotI32 int32 |
| 321 |
|
| 322 |
goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8 int64, |
| 323 |
b bool, i8 int8, u8 uint8, i16 int16, u16 uint16, i32 int32) int64 { |
| 324 |
gotBool = b |
| 325 |
gotI8 = i8 |
| 326 |
gotU8 = u8 |
| 327 |
gotI16 = i16 |
| 328 |
gotU16 = u16 |
| 329 |
gotI32 = i32 |
| 330 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 |
| 331 |
} |
| 332 |
|
| 333 |
cb := purego.NewCallback(goFunc) |
| 334 |
got := callCallbackSmallTypes(cb) |
| 335 |
|
| 336 |
// Check register args sum |
| 337 |
want := int64(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8) // 36 |
| 338 |
if got != want { |
| 339 |
t.Errorf("register args sum = %d, want %d", got, want) |
| 340 |
} |
| 341 |
|
| 342 |
// Check stack args |
| 343 |
if gotBool != true { |
| 344 |
t.Errorf("bool = %v, want true", gotBool) |
| 345 |
} |
| 346 |
if gotI8 != -42 { |
| 347 |
t.Errorf("int8 = %d, want -42", gotI8) |
| 348 |
} |
| 349 |
if gotU8 != 200 { |
| 350 |
t.Errorf("uint8 = %d, want 200", gotU8) |
| 351 |
} |
| 352 |
if gotI16 != -1000 { |
| 353 |
t.Errorf("int16 = %d, want -1000", gotI16) |
| 354 |
} |
| 355 |
if gotU16 != 50000 { |
| 356 |
t.Errorf("uint16 = %d, want 50000", gotU16) |
| 357 |
} |
| 358 |
if gotI32 != 123456 { |
| 359 |
t.Errorf("int32 = %d, want 123456", gotI32) |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
func TestCallback10Int32Packing(t *testing.T) { |
| 364 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 365 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 366 |
} |
| 367 |
|
| 368 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 369 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 370 |
t.Fatal(err) |
| 371 |
} |
| 372 |
defer os.Remove(libFileName) |
| 373 |
|
| 374 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 375 |
if err != nil { |
| 376 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 377 |
} |
| 378 |
|
| 379 |
var callCallback10Int32 func(cb uintptr) int32 |
| 380 |
purego.RegisterLibFunc(&callCallback10Int32, lib, "callCallback10Int32") |
| 381 |
|
| 382 |
goFunc := func(a1, a2, a3, a4, a5, a6, a7, a8, a9, a10 int32) int32 { |
| 383 |
return a1 + a2 + a3 + a4 + a5 + a6 + a7 + a8 + a9 + a10 |
| 384 |
} |
| 385 |
|
| 386 |
cb := purego.NewCallback(goFunc) |
| 387 |
got := callCallback10Int32(cb) |
| 388 |
want := int32(1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10) // 55 |
| 389 |
if got != want { |
| 390 |
t.Errorf("callCallback10Int32() = %d, want %d", got, want) |
| 391 |
} |
| 392 |
} |
| 393 |
|
| 394 |
func TestCallbackFloat64StackPacking(t *testing.T) { |
| 395 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 396 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 397 |
} |
| 398 |
|
| 399 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 400 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 401 |
t.Fatal(err) |
| 402 |
} |
| 403 |
defer os.Remove(libFileName) |
| 404 |
|
| 405 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 406 |
if err != nil { |
| 407 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 408 |
} |
| 409 |
|
| 410 |
var callCallback10Float64 func(cb uintptr) int64 |
| 411 |
purego.RegisterLibFunc(&callCallback10Float64, lib, "callCallback10Float64") |
| 412 |
|
| 413 |
// 10 float64s: 8 in registers, 2 on stack |
| 414 |
// Return int64 since callbacks don't support float returns |
| 415 |
goFunc := func(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10 float64) int64 { |
| 416 |
sum := f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9 + f10 |
| 417 |
return int64(sum * 10) // 60.0 * 10 = 600 |
| 418 |
} |
| 419 |
|
| 420 |
cb := purego.NewCallback(goFunc) |
| 421 |
got := callCallback10Float64(cb) |
| 422 |
want := int64(600) |
| 423 |
if got != want { |
| 424 |
t.Errorf("callCallback10Float64() = %d, want %d", got, want) |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
func TestCallbackFloat32StackPacking(t *testing.T) { |
| 429 |
if runtime.GOOS != "darwin" || runtime.GOARCH != "arm64" { |
| 430 |
t.Skip("callback tight packing only applies to darwin/arm64") |
| 431 |
} |
| 432 |
|
| 433 |
libFileName := filepath.Join(t.TempDir(), "libcbtest_packing.so") |
| 434 |
if err := buildSharedLib(t, "CC", libFileName, filepath.Join("testdata", "libcbtest", "callback_packing_test.c")); err != nil { |
| 435 |
t.Fatal(err) |
| 436 |
} |
| 437 |
defer os.Remove(libFileName) |
| 438 |
|
| 439 |
lib, err := purego.Dlopen(libFileName, purego.RTLD_NOW|purego.RTLD_GLOBAL) |
| 440 |
if err != nil { |
| 441 |
t.Fatalf("Dlopen(%q) failed: %v", libFileName, err) |
| 442 |
} |
| 443 |
|
| 444 |
var callCallback12Float32 func(cb uintptr) int64 |
| 445 |
purego.RegisterLibFunc(&callCallback12Float32, lib, "callCallback12Float32") |
| 446 |
|
| 447 |
// 12 float32s: 8 in registers, 4 on stack |
| 448 |
// Return int64 since callbacks don't support float returns |
| 449 |
goFunc := func(f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12 float32) int64 { |
| 450 |
sum := f1 + f2 + f3 + f4 + f5 + f6 + f7 + f8 + f9 + f10 + f11 + f12 |
| 451 |
return int64(sum) // 78 |
| 452 |
} |
| 453 |
|
| 454 |
cb := purego.NewCallback(goFunc) |
| 455 |
got := callCallback12Float32(cb) |
| 456 |
want := int64(78) |
| 457 |
if got != want { |
| 458 |
t.Errorf("callCallback12Float32() = %d, want %d", got, want) |
| 459 |
} |
| 460 |
} |