test: harden abi helper coverage
- commit
- 90cebfbd3ff850bfc390973db0ed14be0697e56b
- author
- brice · · 2026-04-13 09:50 UTC
- parent
- 2cdd607
- signature
- GPG Verified
CD88F9E564E0102646FB6077A1A07C7C7C3E349D· arch-gpg Verified for the committer email
2 files · +36 -2
M struct_abi_helpers.go +5 -2
| @@ -1,3 +1,6 @@ | |||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | + | // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | |
| 3 | + | ||
| 1 | 4 | package purego | |
| 2 | 5 | ||
| 3 | 6 | import ( | |
| @@ -7,11 +10,11 @@ import ( | |||
| 7 | 10 | ) | |
| 8 | 11 | ||
| 9 | 12 | func packFloat32Pair(low, high uintptr) uintptr { | |
| 10 | - | return high<<32 | (low & math.MaxUint32) | |
| 13 | + | return uintptr((uint64(high) << 32) | (uint64(low) & math.MaxUint32)) | |
| 11 | 14 | } | |
| 12 | 15 | ||
| 13 | 16 | func highFloat32Bits(word uintptr) uint32 { | |
| 14 | - | return uint32(word >> 32) | |
| 17 | + | return uint32(uint64(word) >> 32) | |
| 15 | 18 | } | |
| 16 | 19 | ||
| 17 | 20 | func flushPackedWord(val *uint64, shift *byte, flushed *bool, isFloat bool, addFloat, addInt func(uintptr)) { | |
| @@ -1,3 +1,6 @@ | |||||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |||
| 2 | + | // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | |||
| 3 | + | ||||
| 1 | package purego | 4 | package purego | ||
| 2 | 5 | ||||
| 3 | import ( | 6 | import ( | ||
| @@ -7,11 +10,11 @@ import ( | |||||
| 7 | ) | 10 | ) | ||
| 8 | 11 | ||||
| 9 | func packFloat32Pair(low, high uintptr) uintptr { | 12 | func packFloat32Pair(low, high uintptr) uintptr { | ||
| 10 | - | return high<<32 | (low & math.MaxUint32) | 13 | + | return uintptr((uint64(high) << 32) | (uint64(low) & math.MaxUint32)) |
| 11 | } | 14 | } | ||
| 12 | 15 | ||||
| 13 | func highFloat32Bits(word uintptr) uint32 { | 16 | func highFloat32Bits(word uintptr) uint32 { | ||
| 14 | - | return uint32(word >> 32) | 17 | + | return uint32(uint64(word) >> 32) |
| 15 | } | 18 | } | ||
| 16 | 19 | ||||
| 17 | func flushPackedWord(val *uint64, shift *byte, flushed *bool, isFloat bool, addFloat, addInt func(uintptr)) { | 20 | func flushPackedWord(val *uint64, shift *byte, flushed *bool, isFloat bool, addFloat, addInt func(uintptr)) { | ||
M struct_abi_helpers_test.go +31 -0
| @@ -1,3 +1,6 @@ | |||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |
| 2 | + | // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | |
| 3 | + | ||
| 1 | 4 | package purego | |
| 2 | 5 | ||
| 3 | 6 | import ( | |
| @@ -38,6 +41,22 @@ func TestFlushPackedWordResetsState(t *testing.T) { | |||
| 38 | 41 | } | |
| 39 | 42 | } | |
| 40 | 43 | ||
| 44 | + | func TestFlushPackedWordResetsStateForIntegerBranch(t *testing.T) { | |
| 45 | + | var val uint64 = 0x55667788 | |
| 46 | + | shift := byte(16) | |
| 47 | + | flushed := false | |
| 48 | + | var gotInt uintptr | |
| 49 | + | flushPackedWord(&val, &shift, &flushed, false, func(uintptr) { | |
| 50 | + | t.Fatal("unexpected float flush") | |
| 51 | + | }, func(v uintptr) { gotInt = v }) | |
| 52 | + | if !flushed || val != 0 || shift != 0 { | |
| 53 | + | t.Fatalf("flushPackedWord() left state dirty: flushed=%v val=%#x shift=%d", flushed, val, shift) | |
| 54 | + | } | |
| 55 | + | if gotInt != 0x55667788 { | |
| 56 | + | t.Fatalf("flushPackedWord() flushed %#x, want %#x", gotInt, uintptr(0x55667788)) | |
| 57 | + | } | |
| 58 | + | } | |
| 59 | + | ||
| 41 | 60 | func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | |
| 42 | 61 | type sample struct{ X uint32 } | |
| 43 | 62 | ptr, keepAlive := stableValuePointer(reflect.ValueOf(sample{X: 7})) | |
| @@ -49,6 +68,18 @@ func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | |||
| 49 | 68 | } | |
| 50 | 69 | } | |
| 51 | 70 | ||
| 71 | + | func TestStableValuePointerUsesAddressableValue(t *testing.T) { | |
| 72 | + | type sample struct{ X uint32 } | |
| 73 | + | value := sample{X: 9} | |
| 74 | + | ptr, keepAlive := stableValuePointer(reflect.ValueOf(&value).Elem()) | |
| 75 | + | if keepAlive != nil { | |
| 76 | + | t.Fatal("expected nil keepAlive for addressable value") | |
| 77 | + | } | |
| 78 | + | if got := *(*uint32)(ptr); got != 9 { | |
| 79 | + | t.Fatalf("stableValuePointer() pointed to %#x, want %#x", got, uint32(9)) | |
| 80 | + | } | |
| 81 | + | } | |
| 82 | + | ||
| 52 | 83 | func TestIsAllSameFloatRejectsNestedMixedStruct(t *testing.T) { | |
| 53 | 84 | type inner struct{ X, Y float32 } | |
| 54 | 85 | type mixed struct { | |
| @@ -1,3 +1,6 @@ | |||||
| 1 | + | // SPDX-License-Identifier: Apache-2.0 | |||
| 2 | + | // SPDX-FileCopyrightText: 2026 The Ebitengine Authors | |||
| 3 | + | ||||
| 1 | package purego | 4 | package purego | ||
| 2 | 5 | ||||
| 3 | import ( | 6 | import ( | ||
| @@ -38,6 +41,22 @@ func TestFlushPackedWordResetsState(t *testing.T) { | |||||
| 38 | } | 41 | } | ||
| 39 | } | 42 | } | ||
| 40 | 43 | ||||
| 44 | + | func TestFlushPackedWordResetsStateForIntegerBranch(t *testing.T) { | |||
| 45 | + | var val uint64 = 0x55667788 | |||
| 46 | + | shift := byte(16) | |||
| 47 | + | flushed := false | |||
| 48 | + | var gotInt uintptr | |||
| 49 | + | flushPackedWord(&val, &shift, &flushed, false, func(uintptr) { | |||
| 50 | + | t.Fatal("unexpected float flush") | |||
| 51 | + | }, func(v uintptr) { gotInt = v }) | |||
| 52 | + | if !flushed || val != 0 || shift != 0 { | |||
| 53 | + | t.Fatalf("flushPackedWord() left state dirty: flushed=%v val=%#x shift=%d", flushed, val, shift) | |||
| 54 | + | } | |||
| 55 | + | if gotInt != 0x55667788 { | |||
| 56 | + | t.Fatalf("flushPackedWord() flushed %#x, want %#x", gotInt, uintptr(0x55667788)) | |||
| 57 | + | } | |||
| 58 | + | } | |||
| 59 | + | ||||
| 41 | func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | 60 | func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | ||
| 42 | type sample struct{ X uint32 } | 61 | type sample struct{ X uint32 } | ||
| 43 | ptr, keepAlive := stableValuePointer(reflect.ValueOf(sample{X: 7})) | 62 | ptr, keepAlive := stableValuePointer(reflect.ValueOf(sample{X: 7})) | ||
| @@ -49,6 +68,18 @@ func TestStableValuePointerCopiesUnaddressableValue(t *testing.T) { | |||||
| 49 | } | 68 | } | ||
| 50 | } | 69 | } | ||
| 51 | 70 | ||||
| 71 | + | func TestStableValuePointerUsesAddressableValue(t *testing.T) { | |||
| 72 | + | type sample struct{ X uint32 } | |||
| 73 | + | value := sample{X: 9} | |||
| 74 | + | ptr, keepAlive := stableValuePointer(reflect.ValueOf(&value).Elem()) | |||
| 75 | + | if keepAlive != nil { | |||
| 76 | + | t.Fatal("expected nil keepAlive for addressable value") | |||
| 77 | + | } | |||
| 78 | + | if got := *(*uint32)(ptr); got != 9 { | |||
| 79 | + | t.Fatalf("stableValuePointer() pointed to %#x, want %#x", got, uint32(9)) | |||
| 80 | + | } | |||
| 81 | + | } | |||
| 82 | + | ||||
| 52 | func TestIsAllSameFloatRejectsNestedMixedStruct(t *testing.T) { | 83 | func TestIsAllSameFloatRejectsNestedMixedStruct(t *testing.T) { | ||
| 53 | type inner struct{ X, Y float32 } | 84 | type inner struct{ X, Y float32 } | ||
| 54 | type mixed struct { | 85 | type mixed struct { | ||