Skip to content

[Reflection] Ignore BuiltinTypeDescriptors with zero size, alignment, or stride. #28183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions stdlib/public/Reflection/TypeRefBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,11 +253,18 @@ TypeRefBuilder::getBuiltinTypeInfo(const TypeRef *TR) {

for (auto Info : ReflectionInfos) {
for (auto BuiltinTypeDescriptor : Info.Builtin) {
assert(BuiltinTypeDescriptor->Size > 0);
assert(BuiltinTypeDescriptor->getAlignment() > 0);
assert(BuiltinTypeDescriptor->Stride > 0);
if (BuiltinTypeDescriptor->Stride <= 0)
continue;
if (!BuiltinTypeDescriptor->hasMangledTypeName())
continue;

auto Alignment = BuiltinTypeDescriptor->getAlignment();
if (Alignment <= 0)
continue;
// Reject any alignment that's not a power of two.
if (Alignment & (Alignment - 1))
continue;

auto CandidateMangledName =
readTypeRef(BuiltinTypeDescriptor, BuiltinTypeDescriptor->TypeName);
if (!reflectionNameMatches(CandidateMangledName, MangledName))
Expand Down
3 changes: 3 additions & 0 deletions validation-test/Reflection/Inputs/EmptyStruct/EmptyStruct.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
struct EmptyStructC {
int trailing[0];
};
3 changes: 3 additions & 0 deletions validation-test/Reflection/Inputs/EmptyStruct/module.map
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module EmptyStruct {
header "EmptyStruct.h"
}
70 changes: 70 additions & 0 deletions validation-test/Reflection/reflect_empty_struct.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -lswiftSwiftReflectionTest -I %S/Inputs/EmptyStruct/ %s -o %t/reflect_empty_struct
// RUN: %target-codesign %t/reflect_empty_struct

// RUN: %target-run %target-swift-reflection-test %t/reflect_empty_struct | %FileCheck %s --check-prefix=CHECK-%target-ptrsize --dump-input fail

// REQUIRES: objc_interop
// REQUIRES: executable_test

import SwiftReflectionTest

import EmptyStruct

@_alignment(1) struct EmptyStruct { }
class Class {
var a = EmptyStruct()
var b: Any = EmptyStruct()
var c = EmptyStructC()
var d: Any = EmptyStructC()
}

var obj = Class()

reflect(object: obj)

// CHECK-64: Reflecting an object.
// CHECK-64: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
// CHECK-64: Type reference:
// CHECK-64: (class reflect_empty_struct.Class)

// CHECK-64: Type info:
// CHECK-64: (class_instance size=80 alignment=8 stride=80 num_extra_inhabitants=0 bitwise_takable=1
// CHECK-64: (field name=a offset=16
// CHECK-64: (builtin size=0 alignment=1 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
// CHECK-64: (field name=b offset=16
// CHECK-64: (opaque_existential size=32 alignment=8 stride=32 num_extra_inhabitants=2147483647 bitwise_takable=1
// CHECK-64: (field name=metadata offset=24
// CHECK-64: (builtin size=8 alignment=8 stride=8 num_extra_inhabitants=2147483647 bitwise_takable=1))))
// CHECK-64: (field name=c offset=48
// CHECK-64: (builtin size=0 alignment=4 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
// CHECK-64: (field name=d offset=48
// CHECK-64: (opaque_existential size=32 alignment=8 stride=32 num_extra_inhabitants=2147483647 bitwise_takable=1
// CHECK-64: (field name=metadata offset=24
// CHECK-64: (builtin size=8 alignment=8 stride=8 num_extra_inhabitants=2147483647 bitwise_takable=1)))))

// CHECK-32: Reflecting an object.
// CHECK-32: Instance pointer in child address space: 0x{{[0-9a-fA-F]+}}
// CHECK-32: Type reference:
// CHECK-32: (class reflect_empty_struct.Class)

// CHECK-32: Type info:
// CHECK-32: (class_instance size=40 alignment=4 stride=40 num_extra_inhabitants=0 bitwise_takable=1
// CHECK-32: (field name=a offset=8
// CHECK-32: (builtin size=0 alignment=1 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
// CHECK-32: (field name=b offset=8
// CHECK-32: (opaque_existential size=16 alignment=4 stride=16 num_extra_inhabitants=4096 bitwise_takable=1
// CHECK-32: (field name=metadata offset=12
// CHECK-32: (builtin size=4 alignment=4 stride=4 num_extra_inhabitants=4096 bitwise_takable=1))))
// CHECK-32: (field name=c offset=24
// CHECK-32: (builtin size=0 alignment=4 stride=1 num_extra_inhabitants=0 bitwise_takable=1))
// CHECK-32: (field name=d offset=24
// CHECK-32: (opaque_existential size=16 alignment=4 stride=16 num_extra_inhabitants=4096 bitwise_takable=1
// CHECK-32: (field name=metadata offset=12
// CHECK-32: (builtin size=4 alignment=4 stride=4 num_extra_inhabitants=4096 bitwise_takable=1)))))

doneReflecting()

// CHECK-64: Done.

// CHECK-32: Done.