|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: 2026 The Goffi Authors |
| 3 | + |
| 4 | +//go:build (linux || darwin || freebsd) && amd64 |
| 5 | + |
| 6 | +package ffi |
| 7 | + |
| 8 | +import ( |
| 9 | + "os" |
| 10 | + "os/exec" |
| 11 | + "path/filepath" |
| 12 | + "runtime" |
| 13 | + "testing" |
| 14 | + "unsafe" |
| 15 | + |
| 16 | + "github.com/go-webgpu/goffi/types" |
| 17 | +) |
| 18 | + |
| 19 | +var structTestLib unsafe.Pointer |
| 20 | + |
| 21 | +func TestMain(m *testing.M) { |
| 22 | + if err := buildStructTestLib(); err != nil { |
| 23 | + // If gcc not available, skip struct e2e tests gracefully. |
| 24 | + // Other tests still run. |
| 25 | + structTestLib = nil |
| 26 | + } |
| 27 | + code := m.Run() |
| 28 | + if structTestLib != nil { |
| 29 | + FreeLibrary(structTestLib) |
| 30 | + } |
| 31 | + os.Exit(code) |
| 32 | +} |
| 33 | + |
| 34 | +func buildStructTestLib() error { |
| 35 | + srcPath := filepath.Join("testdata", "structtest.c") |
| 36 | + soPath := filepath.Join("testdata", "libstructtest.so") |
| 37 | + if runtime.GOOS == "darwin" { |
| 38 | + soPath = filepath.Join("testdata", "libstructtest.dylib") |
| 39 | + } |
| 40 | + |
| 41 | + cc := os.Getenv("CC") |
| 42 | + if cc == "" { |
| 43 | + cc = "gcc" |
| 44 | + } |
| 45 | + cmd := exec.Command(cc, "-shared", "-fPIC", "-O2", "-o", soPath, srcPath) |
| 46 | + cmd.Stderr = os.Stderr |
| 47 | + if err := cmd.Run(); err != nil { |
| 48 | + return err |
| 49 | + } |
| 50 | + |
| 51 | + absPath, err := filepath.Abs(soPath) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + lib, err := LoadLibrary(absPath) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + structTestLib = lib |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func requireStructLib(t *testing.T) { |
| 64 | + t.Helper() |
| 65 | + if structTestLib == nil { |
| 66 | + t.Skip("structtest library not available (gcc required)") |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// TestStructArg8B_IntegerPair tests issue #33: struct {int32, uint32} passed by value. |
| 71 | +func TestStructArg8B_IntegerPair(t *testing.T) { |
| 72 | + requireStructLib(t) |
| 73 | + |
| 74 | + sym, err := GetSymbol(structTestLib, "take_struct_8") |
| 75 | + if err != nil { |
| 76 | + t.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + structType := &types.TypeDescriptor{ |
| 80 | + Kind: types.StructType, |
| 81 | + Size: 8, |
| 82 | + Alignment: 4, |
| 83 | + Members: []*types.TypeDescriptor{ |
| 84 | + types.SInt32TypeDescriptor, |
| 85 | + types.UInt32TypeDescriptor, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + var cif types.CallInterface |
| 90 | + if err := PrepareCallInterface(&cif, types.DefaultCall, types.SInt64TypeDescriptor, |
| 91 | + []*types.TypeDescriptor{structType}); err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
| 94 | + |
| 95 | + type Pair struct { |
| 96 | + A int32 |
| 97 | + B uint32 |
| 98 | + } |
| 99 | + s := Pair{A: 42, B: 19} |
| 100 | + args := []unsafe.Pointer{unsafe.Pointer(&s)} |
| 101 | + var result int64 |
| 102 | + if err := CallFunction(&cif, sym, unsafe.Pointer(&result), args); err != nil { |
| 103 | + t.Fatal(err) |
| 104 | + } |
| 105 | + |
| 106 | + expected := int64(42)*1000 + int64(19) |
| 107 | + if result != expected { |
| 108 | + t.Errorf("take_struct_8({42, 19}) = %d, want %d", result, expected) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +// TestStructArg8B_FloatPair tests SSE classification: struct {float, float}. |
| 113 | +func TestStructArg8B_FloatPair(t *testing.T) { |
| 114 | + requireStructLib(t) |
| 115 | + |
| 116 | + sym, err := GetSymbol(structTestLib, "take_struct_2floats") |
| 117 | + if err != nil { |
| 118 | + t.Fatal(err) |
| 119 | + } |
| 120 | + |
| 121 | + structType := &types.TypeDescriptor{ |
| 122 | + Kind: types.StructType, |
| 123 | + Size: 8, |
| 124 | + Alignment: 4, |
| 125 | + Members: []*types.TypeDescriptor{ |
| 126 | + types.FloatTypeDescriptor, |
| 127 | + types.FloatTypeDescriptor, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + var cif types.CallInterface |
| 132 | + if err := PrepareCallInterface(&cif, types.DefaultCall, types.FloatTypeDescriptor, |
| 133 | + []*types.TypeDescriptor{structType}); err != nil { |
| 134 | + t.Fatal(err) |
| 135 | + } |
| 136 | + |
| 137 | + type PairF32 struct { |
| 138 | + X float32 |
| 139 | + Y float32 |
| 140 | + } |
| 141 | + s := PairF32{X: 2.5, Y: 3.5} |
| 142 | + args := []unsafe.Pointer{unsafe.Pointer(&s)} |
| 143 | + var result float32 |
| 144 | + if err := CallFunction(&cif, sym, unsafe.Pointer(&result), args); err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + |
| 148 | + if result != 6.0 { |
| 149 | + t.Errorf("take_struct_2floats({2.5, 3.5}) = %f, want 6.0", result) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +// TestStructArg16B tests two-eightbyte struct: {int64, int64}. |
| 154 | +func TestStructArg16B(t *testing.T) { |
| 155 | + requireStructLib(t) |
| 156 | + |
| 157 | + sym, err := GetSymbol(structTestLib, "take_struct_16") |
| 158 | + if err != nil { |
| 159 | + t.Fatal(err) |
| 160 | + } |
| 161 | + |
| 162 | + structType := &types.TypeDescriptor{ |
| 163 | + Kind: types.StructType, |
| 164 | + Size: 16, |
| 165 | + Alignment: 8, |
| 166 | + Members: []*types.TypeDescriptor{ |
| 167 | + types.SInt64TypeDescriptor, |
| 168 | + types.SInt64TypeDescriptor, |
| 169 | + }, |
| 170 | + } |
| 171 | + |
| 172 | + var cif types.CallInterface |
| 173 | + if err := PrepareCallInterface(&cif, types.DefaultCall, types.SInt64TypeDescriptor, |
| 174 | + []*types.TypeDescriptor{structType}); err != nil { |
| 175 | + t.Fatal(err) |
| 176 | + } |
| 177 | + |
| 178 | + type PairI64 struct { |
| 179 | + A int64 |
| 180 | + B int64 |
| 181 | + } |
| 182 | + s := PairI64{A: 1000000, B: 2000000} |
| 183 | + args := []unsafe.Pointer{unsafe.Pointer(&s)} |
| 184 | + var result int64 |
| 185 | + if err := CallFunction(&cif, sym, unsafe.Pointer(&result), args); err != nil { |
| 186 | + t.Fatal(err) |
| 187 | + } |
| 188 | + |
| 189 | + if result != 3000000 { |
| 190 | + t.Errorf("take_struct_16({1000000, 2000000}) = %d, want 3000000", result) |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +// TestStructArg24B_MemoryClass tests > 16B struct passed on stack (MEMORY class). |
| 195 | +func TestStructArg24B_MemoryClass(t *testing.T) { |
| 196 | + requireStructLib(t) |
| 197 | + |
| 198 | + sym, err := GetSymbol(structTestLib, "take_struct_24") |
| 199 | + if err != nil { |
| 200 | + t.Fatal(err) |
| 201 | + } |
| 202 | + |
| 203 | + structType := &types.TypeDescriptor{ |
| 204 | + Kind: types.StructType, |
| 205 | + Size: 24, |
| 206 | + Alignment: 8, |
| 207 | + Members: []*types.TypeDescriptor{ |
| 208 | + types.SInt64TypeDescriptor, |
| 209 | + types.SInt64TypeDescriptor, |
| 210 | + types.SInt64TypeDescriptor, |
| 211 | + }, |
| 212 | + } |
| 213 | + |
| 214 | + var cif types.CallInterface |
| 215 | + if err := PrepareCallInterface(&cif, types.DefaultCall, types.SInt64TypeDescriptor, |
| 216 | + []*types.TypeDescriptor{structType}); err != nil { |
| 217 | + t.Fatal(err) |
| 218 | + } |
| 219 | + |
| 220 | + type TripleI64 struct { |
| 221 | + A int64 |
| 222 | + B int64 |
| 223 | + C int64 |
| 224 | + } |
| 225 | + s := TripleI64{A: 100, B: 200, C: 300} |
| 226 | + args := []unsafe.Pointer{unsafe.Pointer(&s)} |
| 227 | + var result int64 |
| 228 | + if err := CallFunction(&cif, sym, unsafe.Pointer(&result), args); err != nil { |
| 229 | + t.Fatal(err) |
| 230 | + } |
| 231 | + |
| 232 | + if result != 600 { |
| 233 | + t.Errorf("take_struct_24({100, 200, 300}) = %d, want 600", result) |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +// TestStructArgWithScalar tests struct + scalar argument (register allocation). |
| 238 | +func TestStructArgWithScalar(t *testing.T) { |
| 239 | + requireStructLib(t) |
| 240 | + |
| 241 | + sym, err := GetSymbol(structTestLib, "take_struct_and_int") |
| 242 | + if err != nil { |
| 243 | + t.Fatal(err) |
| 244 | + } |
| 245 | + |
| 246 | + structType := &types.TypeDescriptor{ |
| 247 | + Kind: types.StructType, |
| 248 | + Size: 8, |
| 249 | + Alignment: 4, |
| 250 | + Members: []*types.TypeDescriptor{ |
| 251 | + types.SInt32TypeDescriptor, |
| 252 | + types.UInt32TypeDescriptor, |
| 253 | + }, |
| 254 | + } |
| 255 | + |
| 256 | + var cif types.CallInterface |
| 257 | + if err := PrepareCallInterface(&cif, types.DefaultCall, types.SInt64TypeDescriptor, |
| 258 | + []*types.TypeDescriptor{structType, types.SInt64TypeDescriptor}); err != nil { |
| 259 | + t.Fatal(err) |
| 260 | + } |
| 261 | + |
| 262 | + type Pair struct { |
| 263 | + A int32 |
| 264 | + B uint32 |
| 265 | + } |
| 266 | + s := Pair{A: 10, B: 20} |
| 267 | + extra := int64(1000) |
| 268 | + args := []unsafe.Pointer{unsafe.Pointer(&s), unsafe.Pointer(&extra)} |
| 269 | + var result int64 |
| 270 | + if err := CallFunction(&cif, sym, unsafe.Pointer(&result), args); err != nil { |
| 271 | + t.Fatal(err) |
| 272 | + } |
| 273 | + |
| 274 | + expected := int64(10) + int64(20) + int64(1000) |
| 275 | + if result != expected { |
| 276 | + t.Errorf("take_struct_and_int({10, 20}, 1000) = %d, want %d", result, expected) |
| 277 | + } |
| 278 | +} |
0 commit comments