Skip to content

Commit c2aa4c3

Browse files
committed
[IRGen][backdeploy] fix infinite recursion in metadata queries
One of the places where we ask whether a type's metadata should be obtained via its mangled name was missing the newer, more robust checking for minimum deployment target.
1 parent fef225c commit c2aa4c3

File tree

3 files changed

+28
-23
lines changed

3 files changed

+28
-23
lines changed

lib/IRGen/GenReflection.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class PrintMetadataSource
173173
};
174174

175175
Optional<llvm::VersionTuple>
176-
swift::irgen::getRuntimeVersionThatSupportsDemanglingType(CanType type) {
176+
getRuntimeVersionThatSupportsDemanglingType(CanType type) {
177177
// Associated types of opaque types weren't mangled in a usable form by the
178178
// Swift 5.1 runtime, so we needed to add a new mangling in 5.2.
179179
if (type->hasOpaqueArchetype()) {
@@ -278,6 +278,20 @@ getTypeRefByFunction(IRGenModule &IGM,
278278
return {constant, 6};
279279
}
280280

281+
bool swift::irgen::mangledNameIsUnknownToDeployTarget(IRGenModule &IGM,
282+
CanType type) {
283+
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget(
284+
IGM.Context.LangOpts.Target)) {
285+
if (auto minimumSupportedRuntimeVersion =
286+
getRuntimeVersionThatSupportsDemanglingType(type)) {
287+
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
288+
return true;
289+
}
290+
}
291+
}
292+
return false;
293+
}
294+
281295
static std::pair<llvm::Constant *, unsigned>
282296
getTypeRefImpl(IRGenModule &IGM,
283297
CanType type,
@@ -294,16 +308,10 @@ getTypeRefImpl(IRGenModule &IGM,
294308
// If the minimum deployment target's runtime demangler wouldn't understand
295309
// this mangled name, then fall back to generating a "mangled name" with a
296310
// symbolic reference with a callback function.
297-
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget
298-
(IGM.Context.LangOpts.Target)) {
299-
if (auto minimumSupportedRuntimeVersion =
300-
getRuntimeVersionThatSupportsDemanglingType(type)) {
301-
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
302-
return getTypeRefByFunction(IGM, sig, type);
303-
}
304-
}
311+
if (mangledNameIsUnknownToDeployTarget(IGM, type)) {
312+
return getTypeRefByFunction(IGM, sig, type);
305313
}
306-
314+
307315
break;
308316

309317
case MangledTypeRefRole::Reflection:

lib/IRGen/IRGenMangler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,10 +634,10 @@ class IRGenMangler : public Mangle::ASTMangler {
634634
}
635635
};
636636

637-
/// Does this type require a special minimum Swift runtime version which
638-
/// supports demangling it?
639-
Optional<llvm::VersionTuple>
640-
getRuntimeVersionThatSupportsDemanglingType(CanType type);
637+
/// Determines if the minimum deployment target's runtime demangler will not
638+
/// understand the mangled name for the given type.
639+
/// \returns true iff the target's runtime does not understand the mangled name.
640+
bool mangledNameIsUnknownToDeployTarget(IRGenModule &IGM, CanType type);
641641

642642
} // end namespace irgen
643643
} // end namespace swift

lib/IRGen/MetadataRequest.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2484,7 +2484,11 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
24842484
// Never access by mangled name if we've been asked not to.
24852485
if (IGM.getOptions().DisableConcreteTypeMetadataMangledNameAccessors)
24862486
return false;
2487-
2487+
2488+
// Do not access by mangled name if the runtime won't understand it.
2489+
if (mangledNameIsUnknownToDeployTarget(IGM, type))
2490+
return false;
2491+
24882492
// A nongeneric nominal type with nontrivial metadata has an accessor
24892493
// already we can just call.
24902494
if (auto nom = dyn_cast<NominalType>(type)) {
@@ -2494,14 +2498,7 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
24942498
return false;
24952499
}
24962500
}
2497-
2498-
// The Swift 5.1 runtime fails to demangle associated types of opaque types.
2499-
if (auto minimumSwiftVersion =
2500-
getRuntimeVersionThatSupportsDemanglingType(type)) {
2501-
return IGM.getAvailabilityContext().isContainedIn(
2502-
IGM.Context.getSwift5PlusAvailability(*minimumSwiftVersion));
2503-
}
2504-
2501+
25052502
return true;
25062503

25072504
// The visitor below can be used to fine-tune a heuristic to decide whether

0 commit comments

Comments
 (0)