Skip to content

Commit 4a4b017

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 855e1e3 commit 4a4b017

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()) {
@@ -281,6 +281,20 @@ getTypeRefByFunction(IRGenModule &IGM,
281281
return {constant, 6};
282282
}
283283

284+
bool swift::irgen::mangledNameIsUnknownToDeployTarget(IRGenModule &IGM,
285+
CanType type) {
286+
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget(
287+
IGM.Context.LangOpts.Target)) {
288+
if (auto minimumSupportedRuntimeVersion =
289+
getRuntimeVersionThatSupportsDemanglingType(type)) {
290+
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
291+
return true;
292+
}
293+
}
294+
}
295+
return false;
296+
}
297+
284298
static std::pair<llvm::Constant *, unsigned>
285299
getTypeRefImpl(IRGenModule &IGM,
286300
CanType type,
@@ -297,16 +311,10 @@ getTypeRefImpl(IRGenModule &IGM,
297311
// If the minimum deployment target's runtime demangler wouldn't understand
298312
// this mangled name, then fall back to generating a "mangled name" with a
299313
// symbolic reference with a callback function.
300-
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget
301-
(IGM.Context.LangOpts.Target)) {
302-
if (auto minimumSupportedRuntimeVersion =
303-
getRuntimeVersionThatSupportsDemanglingType(type)) {
304-
if (*runtimeCompatVersion < *minimumSupportedRuntimeVersion) {
305-
return getTypeRefByFunction(IGM, sig, type);
306-
}
307-
}
314+
if (mangledNameIsUnknownToDeployTarget(IGM, type)) {
315+
return getTypeRefByFunction(IGM, sig, type);
308316
}
309-
317+
310318
break;
311319

312320
case MangledTypeRefRole::Reflection:

lib/IRGen/IRGenMangler.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -641,10 +641,10 @@ class IRGenMangler : public Mangle::ASTMangler {
641641
}
642642
};
643643

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

649649
} // end namespace irgen
650650
} // end namespace swift

lib/IRGen/MetadataRequest.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2477,7 +2477,11 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
24772477
// Never access by mangled name if we've been asked not to.
24782478
if (IGM.getOptions().DisableConcreteTypeMetadataMangledNameAccessors)
24792479
return false;
2480-
2480+
2481+
// Do not access by mangled name if the runtime won't understand it.
2482+
if (mangledNameIsUnknownToDeployTarget(IGM, type))
2483+
return false;
2484+
24812485
// A nongeneric nominal type with nontrivial metadata has an accessor
24822486
// already we can just call.
24832487
if (auto nom = dyn_cast<NominalType>(type)) {
@@ -2487,14 +2491,7 @@ static bool shouldAccessByMangledName(IRGenModule &IGM, CanType type) {
24872491
return false;
24882492
}
24892493
}
2490-
2491-
// The Swift 5.1 runtime fails to demangle associated types of opaque types.
2492-
if (auto minimumSwiftVersion =
2493-
getRuntimeVersionThatSupportsDemanglingType(type)) {
2494-
return IGM.getAvailabilityContext().isContainedIn(
2495-
IGM.Context.getSwift5PlusAvailability(*minimumSwiftVersion));
2496-
}
2497-
2494+
24982495
return true;
24992496

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

0 commit comments

Comments
 (0)