Skip to content

Commit 504584c

Browse files
authored
Merge pull request #41501 from mikeash/protocol-conformance-dyld-fixes
[Runtime] Fix dyld protocol conformances.
2 parents 544cd88 + 99752c5 commit 504584c

File tree

1 file changed

+41
-52
lines changed

1 file changed

+41
-52
lines changed

stdlib/public/runtime/ProtocolConformance.cpp

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ _dyld_find_foreign_type_protocol_conformance_on_disk(
7676
// Set this to 1 to enable logging of calls to the dyld shared cache conformance
7777
// table
7878
#if 0
79-
#define SHARED_CACHE_LOG(fmt, ...) \
79+
#define DYLD_CONFORMANCES_LOG(fmt, ...) \
8080
fprintf(stderr, "PROTOCOL CONFORMANCE: " fmt "\n", __VA_ARGS__)
8181
#define SHARED_CACHE_LOG_ENABLED 1
8282
#else
83-
#define SHARED_CACHE_LOG(fmt, ...) (void)0
83+
#define DYLD_CONFORMANCES_LOG(fmt, ...) (void)0
8484
#endif
8585

8686
// Enable dyld shared cache acceleration only when it's available and we have
@@ -462,13 +462,13 @@ struct ConformanceState {
462462
dyldSharedCacheStart ? dyldSharedCacheStart + length : 0;
463463
validateDyldResults = runtime::environment::
464464
SWIFT_DEBUG_VALIDATE_SHARED_CACHE_PROTOCOL_CONFORMANCES();
465-
SHARED_CACHE_LOG("Shared cache range is %#lx-%#lx",
466-
dyldSharedCacheStart, dyldSharedCacheEnd);
465+
DYLD_CONFORMANCES_LOG("Shared cache range is %#lx-%#lx",
466+
dyldSharedCacheStart, dyldSharedCacheEnd);
467467
} else {
468-
SHARED_CACHE_LOG(
469-
"Disabling shared cache optimizations due to unknown "
470-
"optimizations version %u",
471-
_dyld_swift_optimizations_version());
468+
DYLD_CONFORMANCES_LOG("Disabling dyld protocol conformance "
469+
"optimizations due to unknown "
470+
"optimizations version %u",
471+
_dyld_swift_optimizations_version());
472472
dyldSharedCacheStart = 0;
473473
dyldSharedCacheEnd = 0;
474474
}
@@ -574,8 +574,8 @@ void swift::addImageProtocolConformanceBlockCallbackUnsafe(
574574
// Sections in the shared cache are ignored in favor of the shared cache's
575575
// pre-built tables.
576576
if (C.inSharedCache(conformances)) {
577-
SHARED_CACHE_LOG("Skipping conformances section %p in the shared cache",
578-
conformances);
577+
DYLD_CONFORMANCES_LOG(
578+
"Skipping conformances section %p in the shared cache", conformances);
579579
if (C.validateDyldResults)
580580
C.DyldOptimizedSections.push_back(
581581
ConformanceSection{conformances, conformancesSize});
@@ -584,15 +584,15 @@ void swift::addImageProtocolConformanceBlockCallbackUnsafe(
584584
} else if (_dyld_has_preoptimized_swift_protocol_conformances(
585585
reinterpret_cast<const mach_header *>(baseAddress))) {
586586
// dyld may optimize images outside the shared cache. Skip those too.
587-
SHARED_CACHE_LOG("Skipping conformances section %p optimized by dyld",
588-
conformances);
587+
DYLD_CONFORMANCES_LOG(
588+
"Skipping conformances section %p optimized by dyld", conformances);
589589
if (C.validateDyldResults)
590590
C.DyldOptimizedSections.push_back(
591591
ConformanceSection{conformances, conformancesSize});
592592
return;
593593
#endif
594594
} else {
595-
SHARED_CACHE_LOG(
595+
DYLD_CONFORMANCES_LOG(
596596
"Adding conformances section %p outside the shared cache",
597597
conformances);
598598
}
@@ -801,15 +801,15 @@ static _dyld_protocol_conformance_result getDyldSharedCacheConformance(
801801
const ClassMetadata *objcClassMetadata,
802802
const ContextDescriptor *description, llvm::StringRef foreignTypeIdentity) {
803803
if (!foreignTypeIdentity.empty()) {
804-
SHARED_CACHE_LOG(
804+
DYLD_CONFORMANCES_LOG(
805805
"_dyld_find_foreign_type_protocol_conformance(%p, %.*s, %zu)", protocol,
806806
(int)foreignTypeIdentity.size(), foreignTypeIdentity.data(),
807807
foreignTypeIdentity.size());
808808
return _dyld_find_foreign_type_protocol_conformance(
809809
protocol, foreignTypeIdentity.data(), foreignTypeIdentity.size());
810810
} else {
811-
SHARED_CACHE_LOG("_dyld_find_protocol_conformance(%p, %p, %p)", protocol,
812-
objcClassMetadata, description);
811+
DYLD_CONFORMANCES_LOG("_dyld_find_protocol_conformance(%p, %p, %p)",
812+
protocol, objcClassMetadata, description);
813813
return _dyld_find_protocol_conformance(protocol, objcClassMetadata,
814814
description);
815815
}
@@ -823,15 +823,17 @@ static _dyld_protocol_conformance_result getDyldOnDiskConformance(
823823
if (&_dyld_find_foreign_type_protocol_conformance_on_disk &&
824824
&_dyld_find_protocol_conformance_on_disk) {
825825
if (!foreignTypeIdentity.empty()) {
826-
SHARED_CACHE_LOG("_dyld_find_foreign_type_protocol_conformance_on_disk(%"
827-
"p, %.*s, %zu, 0)",
828-
protocol, (int)foreignTypeIdentity.size(),
829-
foreignTypeIdentity.data(), foreignTypeIdentity.size());
826+
DYLD_CONFORMANCES_LOG(
827+
"_dyld_find_foreign_type_protocol_conformance_on_disk(%"
828+
"p, %.*s, %zu, 0)",
829+
protocol, (int)foreignTypeIdentity.size(), foreignTypeIdentity.data(),
830+
foreignTypeIdentity.size());
830831
return _dyld_find_foreign_type_protocol_conformance_on_disk(
831832
protocol, foreignTypeIdentity.data(), foreignTypeIdentity.size(), 0);
832833
} else {
833-
SHARED_CACHE_LOG("_dyld_find_protocol_conformance_on_disk(%p, %p, %p, 0)",
834-
protocol, objcClassMetadata, description);
834+
DYLD_CONFORMANCES_LOG(
835+
"_dyld_find_protocol_conformance_on_disk(%p, %p, %p, 0)", protocol,
836+
objcClassMetadata, description);
835837
return _dyld_find_protocol_conformance_on_disk(
836838
protocol, objcClassMetadata, description, 0);
837839
}
@@ -859,8 +861,9 @@ findConformanceWithDyld(ConformanceState &C, const Metadata *type,
859861
auto objcClassMetadata = swift_getObjCClassFromMetadataConditional(type);
860862
#if SHARED_CACHE_LOG_ENABLED
861863
auto typeName = swift_getTypeName(type, true);
862-
SHARED_CACHE_LOG("Looking up conformance of %.*s to %s", (int)typeName.length,
863-
typeName.data, protocol->Name.get());
864+
DYLD_CONFORMANCES_LOG("Looking up conformance of %.*s to %s",
865+
(int)typeName.length, typeName.data,
866+
protocol->Name.get());
864867
#endif
865868
_dyld_protocol_conformance_result dyldResult;
866869
if (C.scanSectionsBackwards) {
@@ -878,19 +881,7 @@ findConformanceWithDyld(ConformanceState &C, const Metadata *type,
878881
dyldResult = getDyldOnDiskConformance(C, protocol, objcClassMetadata,
879882
description, foreignTypeIdentity);
880883
}
881-
if (!foreignTypeIdentity.empty()) {
882-
SHARED_CACHE_LOG(
883-
"_dyld_find_foreign_type_protocol_conformance(%p, %.*s, %zu)", protocol,
884-
(int)foreignTypeIdentity.size(), foreignTypeIdentity.data(),
885-
foreignTypeIdentity.size());
886-
dyldResult = _dyld_find_foreign_type_protocol_conformance(
887-
protocol, foreignTypeIdentity.data(), foreignTypeIdentity.size());
888-
} else {
889-
SHARED_CACHE_LOG("_dyld_find_protocol_conformance(%p, %p, %p)", protocol,
890-
objcClassMetadata, description);
891-
dyldResult = _dyld_find_protocol_conformance(protocol, objcClassMetadata,
892-
description);
893-
}
884+
894885
switch (dyldResult.kind) {
895886
case _dyld_protocol_conformance_result_kind_found_descriptor: {
896887
auto *conformanceDescriptor =
@@ -902,45 +893,43 @@ findConformanceWithDyld(ConformanceState &C, const Metadata *type,
902893
type, instantiateSuperclassMetadata));
903894

904895
if (conformanceDescriptor->getGenericWitnessTable()) {
905-
SHARED_CACHE_LOG(
906-
"Found generic conformance descriptor %p for %s in shared "
907-
"cache, continuing",
896+
DYLD_CONFORMANCES_LOG(
897+
"DYLD found generic conformance descriptor %p for %s, continuing",
908898
conformanceDescriptor, protocol->Name.get());
909899
return std::make_tuple(nullptr, conformanceDescriptor, false);
910900
} else {
911901
// When there are no generics, we can retrieve the witness table cheaply,
912902
// so do it up front.
913-
SHARED_CACHE_LOG("Found conformance descriptor %p for %s in shared cache",
914-
conformanceDescriptor, protocol->Name.get());
903+
DYLD_CONFORMANCES_LOG("DYLD Found conformance descriptor %p for %s",
904+
conformanceDescriptor, protocol->Name.get());
915905
auto *witnessTable = conformanceDescriptor->getWitnessTable(type);
916906
return std::make_tuple(witnessTable, conformanceDescriptor, false);
917907
}
918908
break;
919909
}
920910
case _dyld_protocol_conformance_result_kind_found_witness_table:
921911
// If we found a witness table then we're done.
922-
SHARED_CACHE_LOG(
923-
"Found witness table %p for conformance to %s in shared cache",
924-
dyldResult.value, protocol->Name.get());
912+
DYLD_CONFORMANCES_LOG("DYLD found witness table %p for conformance to %s",
913+
dyldResult.value, protocol->Name.get());
925914
return std::make_tuple(reinterpret_cast<const WitnessTable *>(dyldResult.value), nullptr,
926915
false);
927916
case _dyld_protocol_conformance_result_kind_not_found:
928917
// If nothing is found, then we'll proceed with checking the runtime's
929918
// caches and scanning conformance records.
930-
SHARED_CACHE_LOG("Conformance to %s not found in shared cache",
931-
protocol->Name.get());
919+
DYLD_CONFORMANCES_LOG("DYLD did not find conformance to %s",
920+
protocol->Name.get());
932921
return std::make_tuple(nullptr, nullptr, false);
933922
break;
934923
case _dyld_protocol_conformance_result_kind_definitive_failure:
935924
// This type is known not to conform to this protocol. Return failure
936925
// without any further checks.
937-
SHARED_CACHE_LOG("Found definitive failure for %s in shared cache",
938-
protocol->Name.get());
926+
DYLD_CONFORMANCES_LOG("DYLD found definitive failure for %s",
927+
protocol->Name.get());
939928
return std::make_tuple(nullptr, nullptr, true);
940929
default:
941930
// Other values may be added. Consider them equivalent to not_found until
942931
// we implement code to handle them.
943-
SHARED_CACHE_LOG(
932+
DYLD_CONFORMANCES_LOG(
944933
"Unknown result kind %lu from _dyld_find_protocol_conformance()",
945934
(unsigned long)dyldResult.kind);
946935
return std::make_tuple(nullptr, nullptr, false);
@@ -1015,8 +1004,8 @@ swift_conformsToProtocolMaybeInstantiateSuperclasses(
10151004
assert(matchingType);
10161005
auto witness = dyldCachedConformanceDescriptor->getWitnessTable(matchingType);
10171006
C.cacheResult(type, protocol, witness, /*always cache*/ 0);
1018-
SHARED_CACHE_LOG("Caching generic conformance to %s found in shared cache",
1019-
protocol->Name.get());
1007+
DYLD_CONFORMANCES_LOG("Caching generic conformance to %s found by DYLD",
1008+
protocol->Name.get());
10201009
return {witness, false};
10211010
}
10221011

0 commit comments

Comments
 (0)