| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
package purego |
| 7 |
|
| 8 |
import ( |
| 9 |
"fmt" |
| 10 |
"math" |
| 11 |
"reflect" |
| 12 |
"runtime" |
| 13 |
"unsafe" |
| 14 |
|
| 15 |
"github.com/ebitengine/purego/internal/strings" |
| 16 |
"github.com/ebitengine/purego/internal/xreflect" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
align8ByteMask = 7 |
| 21 |
align8ByteSize = 8 |
| 22 |
) |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func RegisterLibFunc(fptr any, handle uintptr, name string) { |
| 27 |
sym, err := loadSymbol(handle, name) |
| 28 |
if err != nil { |
| 29 |
panic(err) |
| 30 |
} |
| 31 |
RegisterFunc(fptr, sym) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func RegisterFunc(fptr any, cfn uintptr) { |
| 118 |
const is32bit = unsafe.Sizeof(uintptr(0)) == 4 |
| 119 |
fn := reflect.ValueOf(fptr).Elem() |
| 120 |
ty := fn.Type() |
| 121 |
if ty.Kind() != reflect.Func { |
| 122 |
panic("purego: fptr must be a function pointer") |
| 123 |
} |
| 124 |
if ty.NumOut() > 1 { |
| 125 |
panic("purego: function can only return zero or one values") |
| 126 |
} |
| 127 |
if cfn == 0 { |
| 128 |
panic("purego: cfn is nil") |
| 129 |
} |
| 130 |
if ty.NumOut() == 1 && (ty.Out(0).Kind() == reflect.Float32 || ty.Out(0).Kind() == reflect.Float64) && |
| 131 |
runtime.GOARCH != "arm" && runtime.GOARCH != "arm64" && runtime.GOARCH != "386" && runtime.GOARCH != "amd64" && runtime.GOARCH != "loong64" && runtime.GOARCH != "ppc64le" && runtime.GOARCH != "riscv64" && runtime.GOARCH != "s390x" { |
| 132 |
panic("purego: float returns are not supported") |
| 133 |
} |
| 134 |
{ |
| 135 |
|
| 136 |
|
| 137 |
var ints int |
| 138 |
var floats int |
| 139 |
var stack int |
| 140 |
for i := 0; i < ty.NumIn(); i++ { |
| 141 |
arg := ty.In(i) |
| 142 |
switch arg.Kind() { |
| 143 |
case reflect.Func: |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
for j := 0; j < arg.NumIn(); j++ { |
| 148 |
in := arg.In(j) |
| 149 |
if !in.AssignableTo(reflect.TypeOf(CDecl{})) { |
| 150 |
continue |
| 151 |
} |
| 152 |
if j != 0 { |
| 153 |
panic("purego: CDecl must be the first argument") |
| 154 |
} |
| 155 |
} |
| 156 |
case reflect.String, reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 157 |
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Ptr, reflect.UnsafePointer, |
| 158 |
reflect.Slice, reflect.Bool: |
| 159 |
if ints < numOfIntegerRegisters() { |
| 160 |
ints++ |
| 161 |
} else { |
| 162 |
stack++ |
| 163 |
} |
| 164 |
case reflect.Float32, reflect.Float64: |
| 165 |
if floats < numOfFloatRegisters() { |
| 166 |
floats++ |
| 167 |
} else { |
| 168 |
stack++ |
| 169 |
} |
| 170 |
case reflect.Struct: |
| 171 |
ensureStructSupportedForRegisterFunc() |
| 172 |
if arg.Size() == 0 { |
| 173 |
continue |
| 174 |
} |
| 175 |
addInt := func(u uintptr) { |
| 176 |
ints++ |
| 177 |
} |
| 178 |
addFloat := func(u uintptr) { |
| 179 |
floats++ |
| 180 |
} |
| 181 |
addStack := func(u uintptr) { |
| 182 |
stack++ |
| 183 |
} |
| 184 |
_ = addStruct(reflect.New(arg).Elem(), &ints, &floats, &stack, addInt, addFloat, addStack, nil) |
| 185 |
default: |
| 186 |
panic("purego: unsupported kind " + arg.Kind().String()) |
| 187 |
} |
| 188 |
} |
| 189 |
if ty.NumOut() == 1 && ty.Out(0).Kind() == reflect.Struct { |
| 190 |
ensureStructSupportedForRegisterFunc() |
| 191 |
outType := ty.Out(0) |
| 192 |
checkStructFieldsSupported(outType) |
| 193 |
if runtime.GOARCH == "amd64" && outType.Size() > maxRegAllocStructSize { |
| 194 |
|
| 195 |
|
| 196 |
ints++ |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
sizeOfStack := maxArgs - numOfIntegerRegisters() |
| 201 |
|
| 202 |
|
| 203 |
if runtime.GOOS == "darwin" && runtime.GOARCH == "arm64" { |
| 204 |
stackBytes := estimateStackBytes(ty) |
| 205 |
maxStackBytes := sizeOfStack * 8 |
| 206 |
if stackBytes > maxStackBytes { |
| 207 |
panic("purego: too many stack arguments") |
| 208 |
} |
| 209 |
} else { |
| 210 |
if stack > sizeOfStack { |
| 211 |
panic("purego: too many stack arguments") |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
v := reflect.MakeFunc(ty, func(args []reflect.Value) (results []reflect.Value) { |
| 217 |
var sysargs [maxArgs]uintptr |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
var floats [maxArgs]uintptr |
| 222 |
var numInts int |
| 223 |
var numFloats int |
| 224 |
var numStack int |
| 225 |
var addStack, addInt, addFloat func(x uintptr) |
| 226 |
if runtime.GOARCH == "arm64" || runtime.GOOS != "windows" { |
| 227 |
|
| 228 |
addStack = func(x uintptr) { |
| 229 |
sysargs[numOfIntegerRegisters()+numStack] = x |
| 230 |
numStack++ |
| 231 |
} |
| 232 |
addInt = func(x uintptr) { |
| 233 |
if numInts >= numOfIntegerRegisters() { |
| 234 |
addStack(x) |
| 235 |
} else { |
| 236 |
sysargs[numInts] = x |
| 237 |
numInts++ |
| 238 |
} |
| 239 |
} |
| 240 |
addFloat = func(x uintptr) { |
| 241 |
if numFloats < numOfFloatRegisters() { |
| 242 |
floats[numFloats] = x |
| 243 |
numFloats++ |
| 244 |
} else { |
| 245 |
addStack(x) |
| 246 |
} |
| 247 |
} |
| 248 |
} else { |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
addStack = func(x uintptr) { |
| 255 |
sysargs[numStack] = x |
| 256 |
numStack++ |
| 257 |
} |
| 258 |
addInt = addStack |
| 259 |
addFloat = addStack |
| 260 |
} |
| 261 |
|
| 262 |
var keepAlive []any |
| 263 |
defer func() { |
| 264 |
runtime.KeepAlive(keepAlive) |
| 265 |
runtime.KeepAlive(args) |
| 266 |
}() |
| 267 |
|
| 268 |
var arm64_r8 uintptr |
| 269 |
if ty.NumOut() == 1 && ty.Out(0).Kind() == reflect.Struct { |
| 270 |
outType := ty.Out(0) |
| 271 |
if (runtime.GOARCH == "amd64" || runtime.GOARCH == "loong64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "riscv64" || runtime.GOARCH == "s390x") && outType.Size() > maxRegAllocStructSize { |
| 272 |
val := reflect.New(outType) |
| 273 |
keepAlive = append(keepAlive, val) |
| 274 |
addInt(val.Pointer()) |
| 275 |
} else if runtime.GOARCH == "arm64" && outType.Size() > maxRegAllocStructSize { |
| 276 |
isAllFloats, numFields := isAllSameFloat(outType) |
| 277 |
if !isAllFloats || numFields > 4 { |
| 278 |
val := reflect.New(outType) |
| 279 |
keepAlive = append(keepAlive, val) |
| 280 |
arm64_r8 = val.Pointer() |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
for i, v := range args { |
| 285 |
if variadic, ok := xreflect.TypeAssert[[]any](args[i]); ok { |
| 286 |
if i != len(args)-1 { |
| 287 |
panic("purego: can only expand last parameter") |
| 288 |
} |
| 289 |
for _, x := range variadic { |
| 290 |
keepAlive = addValue(reflect.ValueOf(x), keepAlive, addInt, addFloat, addStack, &numInts, &numFloats, &numStack) |
| 291 |
} |
| 292 |
continue |
| 293 |
} |
| 294 |
|
| 295 |
if runtime.GOARCH == "arm64" && runtime.GOOS == "darwin" && shouldBundleStackArgs(v, numInts, numFloats) { |
| 296 |
|
| 297 |
stackArgs, newKeepAlive := collectStackArgs(args, i, numInts, numFloats, |
| 298 |
keepAlive, addInt, addFloat, addStack, &numInts, &numFloats, &numStack) |
| 299 |
keepAlive = newKeepAlive |
| 300 |
|
| 301 |
|
| 302 |
bundleStackArgs(stackArgs, addStack) |
| 303 |
break |
| 304 |
} |
| 305 |
keepAlive = addValue(v, keepAlive, addInt, addFloat, addStack, &numInts, &numFloats, &numStack) |
| 306 |
} |
| 307 |
|
| 308 |
syscall := &syscall15Args{} |
| 309 |
|
| 310 |
if runtime.GOARCH == "loong64" || runtime.GOARCH == "ppc64le" || runtime.GOARCH == "riscv64" || runtime.GOARCH == "s390x" { |
| 311 |
syscall.Set(cfn, sysargs[:], floats[:], 0) |
| 312 |
runtime_cgocall(syscall15XABI0, unsafe.Pointer(syscall)) |
| 313 |
} else if runtime.GOARCH == "arm64" || runtime.GOOS != "windows" { |
| 314 |
|
| 315 |
syscall.Set(cfn, sysargs[:], floats[:], arm64_r8) |
| 316 |
runtime_cgocall(syscall15XABI0, unsafe.Pointer(syscall)) |
| 317 |
} else { |
| 318 |
|
| 319 |
syscall.a1, syscall.a2, _ = syscall_syscall15X(cfn, sysargs[0], sysargs[1], sysargs[2], sysargs[3], sysargs[4], |
| 320 |
sysargs[5], sysargs[6], sysargs[7], sysargs[8], sysargs[9], sysargs[10], sysargs[11], |
| 321 |
sysargs[12], sysargs[13], sysargs[14]) |
| 322 |
syscall.f1 = syscall.a2 |
| 323 |
} |
| 324 |
if ty.NumOut() == 0 { |
| 325 |
return nil |
| 326 |
} |
| 327 |
outType := ty.Out(0) |
| 328 |
v := reflect.New(outType).Elem() |
| 329 |
switch outType.Kind() { |
| 330 |
case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 331 |
v.SetUint(uint64(syscall.a1)) |
| 332 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 333 |
v.SetInt(int64(syscall.a1)) |
| 334 |
case reflect.Bool: |
| 335 |
v.SetBool(byte(syscall.a1) != 0) |
| 336 |
case reflect.UnsafePointer: |
| 337 |
|
| 338 |
v.SetPointer(*(*unsafe.Pointer)(unsafe.Pointer(&syscall.a1))) |
| 339 |
case reflect.Ptr: |
| 340 |
v = reflect.NewAt(outType, unsafe.Pointer(&syscall.a1)).Elem() |
| 341 |
case reflect.Func: |
| 342 |
|
| 343 |
v = reflect.New(outType) |
| 344 |
RegisterFunc(v.Interface(), syscall.a1) |
| 345 |
case reflect.String: |
| 346 |
v.SetString(strings.GoString(syscall.a1)) |
| 347 |
case reflect.Float32: |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
switch runtime.GOARCH { |
| 354 |
case "386": |
| 355 |
v.SetFloat(math.Float64frombits(uint64(syscall.f1) | (uint64(syscall.f2) << 32))) |
| 356 |
case "ppc64le": |
| 357 |
v.SetFloat(math.Float64frombits(uint64(syscall.f1))) |
| 358 |
case "s390x": |
| 359 |
|
| 360 |
v.SetFloat(float64(math.Float32frombits(uint32(syscall.f1 >> 32)))) |
| 361 |
default: |
| 362 |
v.SetFloat(float64(math.Float32frombits(uint32(syscall.f1)))) |
| 363 |
} |
| 364 |
case reflect.Float64: |
| 365 |
|
| 366 |
|
| 367 |
if is32bit { |
| 368 |
v.SetFloat(math.Float64frombits(uint64(syscall.f1) | (uint64(syscall.f2) << 32))) |
| 369 |
} else { |
| 370 |
v.SetFloat(math.Float64frombits(uint64(syscall.f1))) |
| 371 |
} |
| 372 |
case reflect.Struct: |
| 373 |
v = getStruct(outType, *syscall) |
| 374 |
default: |
| 375 |
panic("purego: unsupported return kind: " + outType.Kind().String()) |
| 376 |
} |
| 377 |
if len(args) > 0 { |
| 378 |
|
| 379 |
args[0] = v |
| 380 |
return args[:1] |
| 381 |
} else { |
| 382 |
return []reflect.Value{v} |
| 383 |
} |
| 384 |
}) |
| 385 |
fn.Set(v) |
| 386 |
} |
| 387 |
|
| 388 |
func addValue(v reflect.Value, keepAlive []any, addInt func(x uintptr), addFloat func(x uintptr), addStack func(x uintptr), numInts *int, numFloats *int, numStack *int) []any { |
| 389 |
const is32bit = unsafe.Sizeof(uintptr(0)) == 4 |
| 390 |
switch v.Kind() { |
| 391 |
case reflect.String: |
| 392 |
ptr := strings.CString(v.String()) |
| 393 |
keepAlive = append(keepAlive, ptr) |
| 394 |
addInt(uintptr(unsafe.Pointer(ptr))) |
| 395 |
case reflect.Uintptr, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: |
| 396 |
addInt(uintptr(v.Uint())) |
| 397 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 398 |
addInt(uintptr(v.Int())) |
| 399 |
case reflect.Ptr, reflect.UnsafePointer, reflect.Slice: |
| 400 |
|
| 401 |
addInt(v.Pointer()) |
| 402 |
case reflect.Func: |
| 403 |
addInt(NewCallback(v.Interface())) |
| 404 |
case reflect.Bool: |
| 405 |
if v.Bool() { |
| 406 |
addInt(1) |
| 407 |
} else { |
| 408 |
addInt(0) |
| 409 |
} |
| 410 |
case reflect.Float32: |
| 411 |
|
| 412 |
if runtime.GOARCH == "s390x" { |
| 413 |
addFloat(uintptr(math.Float32bits(float32(v.Float()))) << 32) |
| 414 |
} else { |
| 415 |
addFloat(uintptr(math.Float32bits(float32(v.Float())))) |
| 416 |
} |
| 417 |
case reflect.Float64: |
| 418 |
if is32bit { |
| 419 |
bits := math.Float64bits(v.Float()) |
| 420 |
addFloat(uintptr(bits)) |
| 421 |
addFloat(uintptr(bits >> 32)) |
| 422 |
} else { |
| 423 |
addFloat(uintptr(math.Float64bits(v.Float()))) |
| 424 |
} |
| 425 |
case reflect.Struct: |
| 426 |
keepAlive = addStruct(v, numInts, numFloats, numStack, addInt, addFloat, addStack, keepAlive) |
| 427 |
default: |
| 428 |
panic("purego: unsupported kind: " + v.Kind().String()) |
| 429 |
} |
| 430 |
return keepAlive |
| 431 |
} |
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
const maxRegAllocStructSize = 16 |
| 439 |
|
| 440 |
func isAllSameFloat(ty reflect.Type) (allFloats bool, numFields int) { |
| 441 |
allFloats = true |
| 442 |
root := ty.Field(0).Type |
| 443 |
for root.Kind() == reflect.Struct { |
| 444 |
root = root.Field(0).Type |
| 445 |
} |
| 446 |
first := root.Kind() |
| 447 |
if first != reflect.Float32 && first != reflect.Float64 { |
| 448 |
allFloats = false |
| 449 |
} |
| 450 |
for i := 0; i < ty.NumField(); i++ { |
| 451 |
f := ty.Field(i).Type |
| 452 |
if f.Kind() == reflect.Struct { |
| 453 |
var structNumFields int |
| 454 |
allFloats, structNumFields = isAllSameFloat(f) |
| 455 |
numFields += structNumFields |
| 456 |
continue |
| 457 |
} |
| 458 |
numFields++ |
| 459 |
if f.Kind() != first { |
| 460 |
allFloats = false |
| 461 |
} |
| 462 |
} |
| 463 |
return allFloats, numFields |
| 464 |
} |
| 465 |
|
| 466 |
func checkStructFieldsSupported(ty reflect.Type) { |
| 467 |
for i := 0; i < ty.NumField(); i++ { |
| 468 |
f := ty.Field(i).Type |
| 469 |
if f.Kind() == reflect.Array { |
| 470 |
f = f.Elem() |
| 471 |
} else if f.Kind() == reflect.Struct { |
| 472 |
checkStructFieldsSupported(f) |
| 473 |
continue |
| 474 |
} |
| 475 |
switch f.Kind() { |
| 476 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, |
| 477 |
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, |
| 478 |
reflect.Uintptr, reflect.Ptr, reflect.UnsafePointer, reflect.Float64, reflect.Float32, |
| 479 |
reflect.Bool: |
| 480 |
default: |
| 481 |
panic(fmt.Sprintf("purego: struct field type %s is not supported", f)) |
| 482 |
} |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
func ensureStructSupportedForRegisterFunc() { |
| 487 |
if runtime.GOARCH != "amd64" && runtime.GOARCH != "arm64" { |
| 488 |
panic("purego: struct arguments are only supported on amd64 and arm64") |
| 489 |
} |
| 490 |
if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { |
| 491 |
panic("purego: struct arguments are only supported on darwin and linux") |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
func roundUpTo8(val uintptr) uintptr { |
| 496 |
return (val + align8ByteMask) &^ align8ByteMask |
| 497 |
} |
| 498 |
|
| 499 |
func numOfFloatRegisters() int { |
| 500 |
switch runtime.GOARCH { |
| 501 |
case "amd64", "arm64", "loong64", "ppc64le", "riscv64": |
| 502 |
return 8 |
| 503 |
case "s390x": |
| 504 |
return 4 |
| 505 |
case "arm": |
| 506 |
return 16 |
| 507 |
case "386": |
| 508 |
|
| 509 |
return 0 |
| 510 |
default: |
| 511 |
|
| 512 |
|
| 513 |
return 8 |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
func numOfIntegerRegisters() int { |
| 518 |
switch runtime.GOARCH { |
| 519 |
case "arm64", "loong64", "ppc64le", "riscv64": |
| 520 |
return 8 |
| 521 |
case "amd64": |
| 522 |
return 6 |
| 523 |
case "s390x": |
| 524 |
|
| 525 |
return 5 |
| 526 |
case "arm": |
| 527 |
return 4 |
| 528 |
case "386": |
| 529 |
|
| 530 |
return 0 |
| 531 |
default: |
| 532 |
|
| 533 |
|
| 534 |
return maxArgs |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
func estimateStackBytes(ty reflect.Type) int { |
| 541 |
var numInts, numFloats int |
| 542 |
var stackBytes int |
| 543 |
|
| 544 |
for i := 0; i < ty.NumIn(); i++ { |
| 545 |
arg := ty.In(i) |
| 546 |
size := int(arg.Size()) |
| 547 |
|
| 548 |
|
| 549 |
usesInt := arg.Kind() != reflect.Float32 && arg.Kind() != reflect.Float64 |
| 550 |
if usesInt && numInts < numOfIntegerRegisters() { |
| 551 |
numInts++ |
| 552 |
} else if !usesInt && numFloats < numOfFloatRegisters() { |
| 553 |
numFloats++ |
| 554 |
} else { |
| 555 |
|
| 556 |
stackBytes += size |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
if stackBytes > 0 && stackBytes%align8ByteSize != 0 { |
| 561 |
stackBytes = int(roundUpTo8(uintptr(stackBytes))) |
| 562 |
} |
| 563 |
return stackBytes |
| 564 |
} |