fix: add shared abi helper coverage
- commit
- 2cdd607074cfd684c3c6522153f542dc9032aafb
- author
- brice · · 2026-04-13 09:45 UTC
- parent
- 7850e38
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
2 files · +109 -0
A struct_abi_helpers.go +35 -0
| @@ -0,0 +1,35 @@ | |||
| 1 | + | package purego | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "math" | |
| 5 | + | "reflect" | |
| 6 | + | "unsafe" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | func packFloat32Pair(low, high uintptr) uintptr { | |
| 10 | + | return high<<32 | (low & math.MaxUint32) | |
| 11 | + | } | |
| 12 | + | ||
| 13 | + | func highFloat32Bits(word uintptr) uint32 { | |
| 14 | + | return uint32(word >> 32) | |
| 15 | + | } | |
| 16 | + | ||
| 17 | + | func flushPackedWord(val *uint64, shift *byte, flushed *bool, isFloat bool, addFloat, addInt func(uintptr)) { | |
| 18 | + | *flushed = true | |
| 19 | + | if isFloat { | |
| 20 | + | addFloat(uintptr(*val)) | |
| 21 | + | } else { | |
| 22 | + | addInt(uintptr(*val)) | |
| 23 | + | } | |
| 24 | + | *val = 0 | |
| 25 | + | *shift = 0 | |
| 26 | + | } | |
| 27 | + | ||
| 28 | + | func stableValuePointer(v reflect.Value) (unsafe.Pointer, any) { | |
| 29 | + | if v.CanAddr() { | |
| 30 | + | return v.Addr().UnsafePointer(), nil | |
| 31 | + | } | |
| 32 | + | tmp := reflect.New(v.Type()) | |
| 33 | + | tmp.Elem().Set(v) | |
| 34 | + | return tmp.UnsafePointer(), tmp.Interface() | |
| 35 | + | } | |
| @@ -0,0 +1,35 @@ | |||||
| 1 | + | package purego | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "math" | |||
| 5 | + | "reflect" | |||
| 6 | + | "unsafe" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | func packFloat32Pair(low, high uintptr) uintptr { | |||
| 10 | + | return high<<32 | (low & math.MaxUint32) | |||
| 11 | + | } | |||
| 12 | + | ||||
| 13 | + | func highFloat32Bits(word uintptr) uint32 { | |||
| 14 | + | return uint32(word >> 32) | |||
| 15 | + | } | |||
| 16 | + | ||||
| 17 | + | func flushPackedWord(val *uint64, shift *byte, flushed *bool, isFloat bool, addFloat, addInt func(uintptr)) { | |||
| 18 | + | *flushed = true | |||
| 19 | + | if isFloat { | |||
| 20 | + | addFloat(uintptr(*val)) | |||
| 21 | + | } else { | |||
| 22 | + | addInt(uintptr(*val)) | |||
| 23 | + | } | |||
| 24 | + | *val = 0 | |||
| 25 | + | *shift = 0 | |||
| 26 | + | } | |||
| 27 | + | ||||
| 28 | + | func stableValuePointer(v reflect.Value) (unsafe.Pointer, any) { | |||
| 29 | + | if v.CanAddr() { | |||
| 30 | + | return v.Addr().UnsafePointer(), nil | |||
| 31 | + | } | |||
| 32 | + | tmp := reflect.New(v.Type()) | |||
| 33 | + | tmp.Elem().Set(v) | |||
| 34 | + | return tmp.UnsafePointer(), tmp.Interface() | |||
| 35 | + | } | |||
A struct_abi_helpers_test.go +74 -0
| @@ -0,0 +1,74 @@ | |||
| 1 | + | package purego | |
| 2 | + | ||
| 3 | + | import ( | |
| 4 | + | "math" | |
| 5 | + | "reflect" | |
| 6 | + | "testing" | |
| 7 | + | ) | |
| 8 | + | ||
| 9 | + | func TestPackFloat32PairMasksLowWord(t *testing.T) { | |
| 10 | + | low := uintptr(0xDEADBEEF00000000 | uint64(math.Float32bits(1.5))) | |
| 11 | + | high := uintptr(math.Float32bits(2.5)) | |
| 12 | + | want := uintptr(uint64(math.Float32bits(2.5))<<32 | uint64(math.Float32bits(1.5))) | |
| 13 | + | if got := packFloat32Pair(low, high); got != want { | |
| 14 | + | t.Fatalf("packFloat32Pair() = %#x, want %#x", got, want) | |
| 15 | + | } | |
| 16 | + | } | |
| 17 | + | ||
| 18 | + | func TestHighFloat32Bits(t *testing.T) { | |
| 19 | + | word := uintptr(uint64(math.Float32bits(3.25)) << 32) | |
| 20 | + | if got, want := highFloat32Bits(word), math.Float32bits(3.25); got != want { | |
| 21 | + | t.Fatalf("highFloat32Bits() = %#x, want %#x", got, want) | |
| 22 | + | } | |
| 23 | + | } | |
| 24 | + | ||
| 25 | + | func TestFlushPackedWordResetsState(t *testing.T) { | |
| 26 | + | var val uint64 = 0x11223344 | |
| 27 | + | shift := byte(32) | |
| 28 | + | flushed := false | |
| 29 | + | var gotFloat uintptr | |
| 30 | + | flushPackedWord(&val, &shift, &flushed, true, func(v uintptr) { gotFloat = v }, func(uintptr) { | |
| 31 | + | t.Fatal("unexpected integer flush") | |
| 32 | + | }) | |
| 33 | + | if !flushed || val != 0 || shift != 0 { | |
| 34 | + | t.Fatalf("flushPackedWord() left state dirty: flushed=%v val=%#x shift=%d", flushed, val, shift) | |
| 35 | + | } | |
| 36 | + | if gotFloat != 0x11223344 { | |
| 37 | + | t.Fatalf("flushPackedWord() flushed %#x, want %#x", gotFloat, uintptr(0x11223344)) | |
| 38 | + | } | |
| 39 | + | } | |
| 40 | + | ||
| 41 | + | func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | |
| 42 | + | type sample struct{ X uint32 } | |
| 43 | + | ptr, keepAlive := stableValuePointer(reflect.ValueOf(sample{X: 7})) | |
| 44 | + | if keepAlive == nil { | |
| 45 | + | t.Fatal("expected keepAlive copy for unaddressable value") | |
| 46 | + | } | |
| 47 | + | if got := *(*uint32)(ptr); got != 7 { | |
| 48 | + | t.Fatalf("stableValuePointer() copied %#x, want %#x", got, uint32(7)) | |
| 49 | + | } | |
| 50 | + | } | |
| 51 | + | ||
| 52 | + | func TestIsAllSameFloatRejectsNestedMixedStruct(t *testing.T) { | |
| 53 | + | type inner struct{ X, Y float32 } | |
| 54 | + | type mixed struct { | |
| 55 | + | Inner inner | |
| 56 | + | Tail int32 | |
| 57 | + | } | |
| 58 | + | allFloats, fields := isAllSameFloat(reflect.TypeOf(mixed{})) | |
| 59 | + | if allFloats { | |
| 60 | + | t.Fatalf("isAllSameFloat() = true for mixed nested struct with %d fields", fields) | |
| 61 | + | } | |
| 62 | + | } | |
| 63 | + | ||
| 64 | + | func TestIsAllSameFloatAcceptsNestedFloatStruct(t *testing.T) { | |
| 65 | + | type inner struct{ X, Y float32 } | |
| 66 | + | type outer struct { | |
| 67 | + | Left inner | |
| 68 | + | Right inner | |
| 69 | + | } | |
| 70 | + | allFloats, fields := isAllSameFloat(reflect.TypeOf(outer{})) | |
| 71 | + | if !allFloats || fields != 4 { | |
| 72 | + | t.Fatalf("isAllSameFloat() = (%v, %d), want (true, 4)", allFloats, fields) | |
| 73 | + | } | |
| 74 | + | } | |
| @@ -0,0 +1,74 @@ | |||||
| 1 | + | package purego | |||
| 2 | + | ||||
| 3 | + | import ( | |||
| 4 | + | "math" | |||
| 5 | + | "reflect" | |||
| 6 | + | "testing" | |||
| 7 | + | ) | |||
| 8 | + | ||||
| 9 | + | func TestPackFloat32PairMasksLowWord(t *testing.T) { | |||
| 10 | + | low := uintptr(0xDEADBEEF00000000 | uint64(math.Float32bits(1.5))) | |||
| 11 | + | high := uintptr(math.Float32bits(2.5)) | |||
| 12 | + | want := uintptr(uint64(math.Float32bits(2.5))<<32 | uint64(math.Float32bits(1.5))) | |||
| 13 | + | if got := packFloat32Pair(low, high); got != want { | |||
| 14 | + | t.Fatalf("packFloat32Pair() = %#x, want %#x", got, want) | |||
| 15 | + | } | |||
| 16 | + | } | |||
| 17 | + | ||||
| 18 | + | func TestHighFloat32Bits(t *testing.T) { | |||
| 19 | + | word := uintptr(uint64(math.Float32bits(3.25)) << 32) | |||
| 20 | + | if got, want := highFloat32Bits(word), math.Float32bits(3.25); got != want { | |||
| 21 | + | t.Fatalf("highFloat32Bits() = %#x, want %#x", got, want) | |||
| 22 | + | } | |||
| 23 | + | } | |||
| 24 | + | ||||
| 25 | + | func TestFlushPackedWordResetsState(t *testing.T) { | |||
| 26 | + | var val uint64 = 0x11223344 | |||
| 27 | + | shift := byte(32) | |||
| 28 | + | flushed := false | |||
| 29 | + | var gotFloat uintptr | |||
| 30 | + | flushPackedWord(&val, &shift, &flushed, true, func(v uintptr) { gotFloat = v }, func(uintptr) { | |||
| 31 | + | t.Fatal("unexpected integer flush") | |||
| 32 | + | }) | |||
| 33 | + | if !flushed || val != 0 || shift != 0 { | |||
| 34 | + | t.Fatalf("flushPackedWord() left state dirty: flushed=%v val=%#x shift=%d", flushed, val, shift) | |||
| 35 | + | } | |||
| 36 | + | if gotFloat != 0x11223344 { | |||
| 37 | + | t.Fatalf("flushPackedWord() flushed %#x, want %#x", gotFloat, uintptr(0x11223344)) | |||
| 38 | + | } | |||
| 39 | + | } | |||
| 40 | + | ||||
| 41 | + | func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | |||
| 42 | + | type sample struct{ X uint32 } | |||
| 43 | + | ptr, keepAlive := stableValuePointer(reflect.ValueOf(sample{X: 7})) | |||
| 44 | + | if keepAlive == nil { | |||
| 45 | + | t.Fatal("expected keepAlive copy for unaddressable value") | |||
| 46 | + | } | |||
| 47 | + | if got := *(*uint32)(ptr); got != 7 { | |||
| 48 | + | t.Fatalf("stableValuePointer() copied %#x, want %#x", got, uint32(7)) | |||
| 49 | + | } | |||
| 50 | + | } | |||
| 51 | + | ||||
| 52 | + | func TestIsAllSameFloatRejectsNestedMixedStruct(t *testing.T) { | |||
| 53 | + | type inner struct{ X, Y float32 } | |||
| 54 | + | type mixed struct { | |||
| 55 | + | Inner inner | |||
| 56 | + | Tail int32 | |||
| 57 | + | } | |||
| 58 | + | allFloats, fields := isAllSameFloat(reflect.TypeOf(mixed{})) | |||
| 59 | + | if allFloats { | |||
| 60 | + | t.Fatalf("isAllSameFloat() = true for mixed nested struct with %d fields", fields) | |||
| 61 | + | } | |||
| 62 | + | } | |||
| 63 | + | ||||
| 64 | + | func TestIsAllSameFloatAcceptsNestedFloatStruct(t *testing.T) { | |||
| 65 | + | type inner struct{ X, Y float32 } | |||
| 66 | + | type outer struct { | |||
| 67 | + | Left inner | |||
| 68 | + | Right inner | |||
| 69 | + | } | |||
| 70 | + | allFloats, fields := isAllSameFloat(reflect.TypeOf(outer{})) | |||
| 71 | + | if !allFloats || fields != 4 { | |||
| 72 | + | t.Fatalf("isAllSameFloat() = (%v, %d), want (true, 4)", allFloats, fields) | |||
| 73 | + | } | |||
| 74 | + | } | |||