Skip to content

Commit 0db8d14

Browse files
committed
[Debug Info] Add missing caching for types with no type sugar
This avoids redundant creation and uniquing of types in the case where we only have a canonical name. Since the uniquing changes the type graph this introduced the possibility for use-after-free if IRGenDebugInfoImpl held on to a direct (non-tracking) DIType *. rdar://146327709
1 parent 8e3d88f commit 0db8d14

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,12 +1124,16 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
11241124
unsigned SizeInBits, unsigned AlignInBits, llvm::DINode::DIFlags Flags,
11251125
StringRef MangledName, StringRef Name) {
11261126
#ifndef NDEBUG
1127-
if (MangledName.empty())
1128-
assert(!Name.empty() &&
1129-
"no mangled name and no human readable name given");
1130-
else
1131-
assert(swift::Demangle::isMangledName(MangledName) &&
1132-
"UID is not a mangled name");
1127+
{
1128+
if (MangledName.empty())
1129+
assert(!Name.empty() &&
1130+
"no mangled name and no human readable name given");
1131+
else
1132+
assert(swift::Demangle::isMangledName(MangledName) &&
1133+
"UID is not a mangled name");
1134+
auto UID = llvm::MDString::get(IGM.getLLVMContext(), MangledName);
1135+
assert(DIRefMap.count(UID) == 0 && "type is already cached");
1136+
}
11331137
#endif
11341138
auto ReplaceableType = DBuilder.createReplaceableCompositeType(
11351139
llvm::dwarf::DW_TAG_structure_type, "", Scope, File, Line,
@@ -1236,14 +1240,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
12361240
SmallVector<llvm::Metadata *, 16> Members;
12371241
for (auto &Member : MemberTypes) {
12381242
unsigned OffsetInBits = 0;
1239-
Members.push_back(createMemberType(Member.DIType, Member.Name,
1243+
auto *member = createMemberType(Member.DIType, Member.Name,
12401244
OffsetInBits, Member.AlignInBits,
1241-
Scope, File, Flags));
1245+
Scope, File, Flags);
1246+
member->dump();
1247+
Members.push_back(member);
12421248
}
12431249

12441250
llvm::DICompositeType *DITy = DBuilder.createStructType(
12451251
Scope, Name, File, Line, SizeInBits, AlignInBits, Flags, DerivedFrom,
12461252
DBuilder.getOrCreateArray(Members), RuntimeLang, nullptr, UniqueID);
1253+
DITy->dump();
12471254
return DBuilder.replaceTemporary(std::move(FwdDecl), DITy);
12481255
}
12491256

@@ -1313,20 +1320,15 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13131320
if (!Decl)
13141321
return nullptr;
13151322

1316-
// This temporary forward decl seems to be redundant. Can it be removed?
1317-
StringRef Name = Decl->getName().str();
1318-
auto FwdDecl = createTemporaryReplaceableForwardDecl(
1319-
Type, Scope, File, Line, SizeInBits, AlignInBits, Flags, MangledName,
1320-
Name);
1321-
13221323
auto [IsUnsubstitued, UnsubstitutedType] =
13231324
getUnsubstitutedType(Type, MangledName);
13241325
auto UnsubstitutedDbgTy = DebugTypeInfo::getFromTypeInfo(
13251326
UnsubstitutedType, IGM.getTypeInfoForUnlowered(UnsubstitutedType), IGM);
1326-
if (IsUnsubstitued)
1327+
if (IsUnsubstitued) {
13271328
return createUnsubstitutedGenericStructOrClassType(
13281329
UnsubstitutedDbgTy, Decl, UnsubstitutedType, Scope, File, Line, Flags,
13291330
nullptr, llvm::dwarf::DW_LANG_Swift, MangledName);
1331+
}
13301332

13311333
// Force the creation of the unsubstituted type, don't create it
13321334
// directly so it goes through all the caching/verification logic.
@@ -1361,7 +1363,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13611363
Scope, Decl ? Decl->getNameStr() : "", File, Line, SizeInBits,
13621364
AlignInBits, Flags, MangledName, collectGenericParams(Type),
13631365
UnsubstitutedDITy);
1364-
return DBuilder.replaceTemporary(std::move(FwdDecl), SpecializedDITy);
1366+
return SpecializedDITy;
13651367
}
13661368

13671369
/// Create debug information for an enum with a raw type (enum E : Int {}).
@@ -2644,12 +2646,17 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
26442646
auto *DITy = cast<llvm::DIType>(CachedTy);
26452647
assert(sanityCheckCachedType(DbgTy, DITy));
26462648
return DITy;
2649+
} else {
2650+
UID = llvm::MDString::get(IGM.getLLVMContext(), Mangled.Canonical);
2651+
if (llvm::Metadata *CachedTy = DIRefMap.lookup(UID))
2652+
return cast<llvm::DIType>(CachedTy);
26472653
}
26482654
}
26492655

26502656
Scope = updateScope(Scope, DbgTy);
26512657
StringRef MangledName =
26522658
!Mangled.Sugared.empty() ? Mangled.Sugared : Mangled.Canonical;
2659+
26532660
StringRef Name = MangledName;
26542661
if (auto *Decl = DbgTy.getDecl())
26552662
Name = Decl->getName().str();
@@ -2675,7 +2682,6 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
26752682
}
26762683
llvm::DIType *DITy = createType(DbgTy, MangledName, Scope, getFile(Scope));
26772684

2678-
26792685
if (!shouldCacheDIType(DITy, DbgTy))
26802686
return DITy;
26812687

0 commit comments

Comments
 (0)