Skip to content

Commit 8499a7f

Browse files
authored
Merge pull request #28183 from mikeash/zero-size-builtin-type-descriptors-remote-mirror
[Reflection] Ignore BuiltinTypeDescriptors with zero size, alignment, or stride.
2 parents b615db4 + ee8447e commit 8499a7f

File tree

4 files changed

+86
-3
lines changed

4 files changed

+86
-3
lines changed

stdlib/public/Reflection/TypeRefBuilder.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,11 +253,18 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {
253253

254254
for (auto Info : ReflectionInfos) {
255255
for (auto BuiltinTypeDescriptor : Info.Builtin) {
256-
assert(BuiltinTypeDescriptor->Size > 0);
257-
assert(BuiltinTypeDescriptor->getAlignment() > 0);
258-
assert(BuiltinTypeDescriptor->Stride > 0);
256+
if (BuiltinTypeDescriptor->Stride <= 0)
257+
continue;
259258
if (!BuiltinTypeDescriptor->hasMangledTypeName())
260259
continue;
260+
261+
auto Alignment = BuiltinTypeDescriptor->getAlignment();
262+
if (Alignment <= 0)
263+
continue;
264+
// Reject any alignment that's not a power of two.
265+
if (Alignment & (Alignment - 1))
266+
continue;
267+
261268
auto CandidateMangledName =
262269
readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName);
263270
if (!reflectionNameMatches(CandidateMangledName, MangledName))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct EmptyStructC {
2+
int trailing[0];
3+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module EmptyStruct {
2+
header "EmptyStruct.h"
3+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -lswiftSwiftReflectionTest -I %S/Inputs/EmptyStruct/ %s -o %t/reflect_empty_struct
3+
// RUN: %target-codesign %t/reflect_empty_struct
4+
5+
// RUN: %target-run %target-swift-reflection-test %t/reflect_empty_struct | %FileCheck %s --check-prefix=CHECK-%target-ptrsize --dump-input fail
6+
7+
// REQUIRES: objc_interop
8+
// REQUIRES: executable_test
9+
10+
import SwiftReflectionTest
11+
12+
import EmptyStruct
13+
14+
@_alignment(1) struct EmptyStruct { }
15+
class Class {
16+
var a = EmptyStruct()
17+
var b: Any = EmptyStruct()
18+
var c = EmptyStructC()
19+
var d: Any = EmptyStructC()
20+
}
21+
22+
var obj = Class()
23+
24+
reflect(object: obj)
25+
26+
// CHECK-64: Reflecting an object.
27+
// CHECK-64: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
28+
// CHECK-64: Type reference:
29+
// CHECK-64: (class reflect_empty_struct.Class)
30+
31+
// CHECK-64: Type info:
32+
// CHECK-64: (class_instance size=80 alignment=8 stride=80 num_extra_inhabitants=0 bitwise_takable=1
33+
// CHECK-64: (field name=a offset=16
34+
// CHECK-64: (builtin size=0 alignment=1 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
35+
// CHECK-64: (field name=b offset=16
36+
// CHECK-64: (opaque_existential size=32 alignment=8 stride=32 num_extra_inhabitants=2147483647 bitwise_takable=1
37+
// CHECK-64: (field name=metadata offset=24
38+
// CHECK-64: (builtin size=8 alignment=8 stride=8 num_extra_inhabitants=2147483647 bitwise_takable=1))))
39+
// CHECK-64: (field name=c offset=48
40+
// CHECK-64: (builtin size=0 alignment=4 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
41+
// CHECK-64: (field name=d offset=48
42+
// CHECK-64: (opaque_existential size=32 alignment=8 stride=32 num_extra_inhabitants=2147483647 bitwise_takable=1
43+
// CHECK-64: (field name=metadata offset=24
44+
// CHECK-64: (builtin size=8 alignment=8 stride=8 num_extra_inhabitants=2147483647 bitwise_takable=1)))))
45+
46+
// CHECK-32: Reflecting an object.
47+
// CHECK-32: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
48+
// CHECK-32: Type reference:
49+
// CHECK-32: (class reflect_empty_struct.Class)
50+
51+
// CHECK-32: Type info:
52+
// CHECK-32: (class_instance size=40 alignment=4 stride=40 num_extra_inhabitants=0 bitwise_takable=1
53+
// CHECK-32: (field name=a offset=8
54+
// CHECK-32: (builtin size=0 alignment=1 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
55+
// CHECK-32: (field name=b offset=8
56+
// CHECK-32: (opaque_existential size=16 alignment=4 stride=16 num_extra_inhabitants=4096 bitwise_takable=1
57+
// CHECK-32: (field name=metadata offset=12
58+
// CHECK-32: (builtin size=4 alignment=4 stride=4 num_extra_inhabitants=4096 bitwise_takable=1))))
59+
// CHECK-32: (field name=c offset=24
60+
// CHECK-32: (builtin size=0 alignment=4 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
61+
// CHECK-32: (field name=d offset=24
62+
// CHECK-32: (opaque_existential size=16 alignment=4 stride=16 num_extra_inhabitants=4096 bitwise_takable=1
63+
// CHECK-32: (field name=metadata offset=12
64+
// CHECK-32: (builtin size=4 alignment=4 stride=4 num_extra_inhabitants=4096 bitwise_takable=1)))))
65+
66+
doneReflecting()
67+
68+
// CHECK-64: Done.
69+
70+
// CHECK-32: Done.

0 commit comments

Comments
 (0)