@@ -600,6 +600,13 @@ class MetadataReader {
600
600
if (Cached != TypeCache.end ())
601
601
return Cached->second ;
602
602
603
+ // If we see garbage data in the process of building a BuiltType, and get
604
+ // the same metadata address again, we will hit an infinite loop.
605
+ // Insert a negative result into the cache now so that, if we recur with
606
+ // the same address, we will return the negative result with the check
607
+ // just above.
608
+ TypeCache.insert ({MetadataAddress, BuiltType ()});
609
+
603
610
auto Meta = readMetadata (MetadataAddress);
604
611
if (!Meta) return BuiltType ();
605
612
@@ -639,8 +646,10 @@ class MetadataReader {
639
646
!Reader->readString (RemoteAddress (tupleMeta->Labels ), labels))
640
647
return BuiltType ();
641
648
642
- return Builder.createTupleType (elementTypes, std::move (labels),
643
- /* variadic*/ false );
649
+ auto BuiltTuple = Builder.createTupleType (elementTypes, std::move (labels),
650
+ /* variadic*/ false );
651
+ TypeCache[MetadataAddress] = BuiltTuple;
652
+ return BuiltTuple;
644
653
}
645
654
case MetadataKind::Function: {
646
655
auto Function = cast<TargetFunctionTypeMetadata<Runtime>>(Meta);
@@ -673,8 +682,11 @@ class MetadataReader {
673
682
674
683
auto flags = FunctionTypeFlags ().withConvention (Function->getConvention ())
675
684
.withThrows (Function->throws ());
676
- return Builder.createFunctionType (Arguments, ArgumentIsInOut,
677
- Result, flags);
685
+ auto BuiltFunction = Builder.createFunctionType (Arguments,
686
+ ArgumentIsInOut,
687
+ Result, flags);
688
+ TypeCache[MetadataAddress] = BuiltFunction;
689
+ return BuiltFunction;
678
690
}
679
691
case MetadataKind::Existential: {
680
692
auto Exist = cast<TargetExistentialTypeMetadata<Runtime>>(Meta);
@@ -684,7 +696,7 @@ class MetadataReader {
684
696
auto ProtocolDescriptor = readProtocolDescriptor (ProtocolAddress);
685
697
if (!ProtocolDescriptor)
686
698
return BuiltType ();
687
-
699
+
688
700
std::string MangledName;
689
701
if (!Reader->readString (RemoteAddress (ProtocolDescriptor->Name ),
690
702
MangledName))
@@ -696,13 +708,17 @@ class MetadataReader {
696
708
697
709
Protocols.push_back (Protocol);
698
710
}
699
- return Builder.createProtocolCompositionType (Protocols);
711
+ auto BuiltExist = Builder.createProtocolCompositionType (Protocols);
712
+ TypeCache[MetadataAddress] = BuiltExist;
713
+ return BuiltExist;
700
714
}
701
715
case MetadataKind::Metatype: {
702
716
auto Metatype = cast<TargetMetatypeMetadata<Runtime>>(Meta);
703
717
auto Instance = readTypeFromMetadata (Metatype->InstanceType );
704
718
if (!Instance) return BuiltType ();
705
- return Builder.createMetatypeType (Instance);
719
+ auto BuiltMetatype = Builder.createMetatypeType (Instance);
720
+ TypeCache[MetadataAddress] = BuiltMetatype;
721
+ return BuiltMetatype;
706
722
}
707
723
case MetadataKind::ObjCClassWrapper: {
708
724
auto objcWrapper = cast<TargetObjCClassWrapperMetadata<Runtime>>(Meta);
@@ -712,13 +728,17 @@ class MetadataReader {
712
728
if (!readObjCClassName (classAddress, className))
713
729
return BuiltType ();
714
730
715
- return Builder.createObjCClassType (std::move (className));
731
+ auto BuiltObjCClass = Builder.createObjCClassType (std::move (className));
732
+ TypeCache[MetadataAddress] = BuiltObjCClass;
733
+ return BuiltObjCClass;
716
734
}
717
735
case MetadataKind::ExistentialMetatype: {
718
736
auto Exist = cast<TargetExistentialMetatypeMetadata<Runtime>>(Meta);
719
737
auto Instance = readTypeFromMetadata (Exist->InstanceType );
720
738
if (!Instance) return BuiltType ();
721
- return Builder.createExistentialMetatypeType (Instance);
739
+ auto BuiltExist = Builder.createExistentialMetatypeType (Instance);
740
+ TypeCache[MetadataAddress] = BuiltExist;
741
+ return BuiltExist;
722
742
}
723
743
case MetadataKind::ForeignClass: {
724
744
auto namePtrAddress =
@@ -730,15 +750,20 @@ class MetadataReader {
730
750
std::string name;
731
751
if (!Reader->readString (RemoteAddress (namePtr), name))
732
752
return BuiltType ();
733
- return Builder.createForeignClassType (std::move (name));
753
+ auto BuiltForeign = Builder.createForeignClassType (std::move (name));
754
+ TypeCache[MetadataAddress] = BuiltForeign;
755
+ return BuiltForeign;
734
756
}
735
757
case MetadataKind::HeapLocalVariable:
736
758
case MetadataKind::HeapGenericLocalVariable:
737
759
case MetadataKind::ErrorObject:
738
760
// Treat these all as Builtin.NativeObject for type lowering purposes.
739
761
return Builder.createBuiltinType (" Bo" );
740
- case MetadataKind::Opaque:
741
- return Builder.getOpaqueType ();
762
+ case MetadataKind::Opaque: {
763
+ auto BuiltOpaque = Builder.getOpaqueType ();
764
+ TypeCache[MetadataAddress] = BuiltOpaque;
765
+ return BuiltOpaque;
766
+ }
742
767
}
743
768
}
744
769
@@ -1165,7 +1190,7 @@ class MetadataReader {
1165
1190
}
1166
1191
if (!nominal) return BuiltType ();
1167
1192
1168
- TypeCache. insert ({ metadata.getAddress (), nominal}) ;
1193
+ TypeCache[ metadata.getAddress ()] = nominal;
1169
1194
return nominal;
1170
1195
}
1171
1196
0 commit comments