Skip to content

Commit 0970ab9

Browse files
committed
[Runtime] Check demangled superclass names against compiler-provided super.
Introduce a sanity check verifying that we can demangle the superclass of a class when forming type metadata, and that the result matches the compiler-provided superclass metadata.
1 parent a24729a commit 0970ab9

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2544,6 +2544,44 @@ swift::swift_initClassMetadata(ClassMetadata *self,
25442544
size_t numFields,
25452545
const TypeLayout * const *fieldTypes,
25462546
size_t *fieldOffsets) {
2547+
// If there is a mangled superclass name, demangle it to the superclass
2548+
// type.
2549+
if (auto superclassNameBase = self->getDescription()->SuperclassType.get()) {
2550+
StringRef superclassName =
2551+
Demangle::makeSymbolicMangledNameStringRef(superclassNameBase);
2552+
SubstGenericParametersFromMetadata substitutions(self);
2553+
const Metadata *superclass =
2554+
_getTypeByMangledName(superclassName, substitutions);
2555+
if (!superclass) {
2556+
fatalError(0,
2557+
"failed to demangle superclass of %s from mangled name '%s'\n",
2558+
self->getDescription()->Name.get(),
2559+
superclassName.str().c_str());
2560+
}
2561+
2562+
#if SWIFT_OBJC_INTEROP
2563+
if (auto objcWrapper = dyn_cast<ObjCClassWrapperMetadata>(superclass))
2564+
superclass = objcWrapper->Class;
2565+
#endif
2566+
2567+
if (superclass != super) {
2568+
auto superclassType = swift_getTypeName(superclass, true);
2569+
auto providedSuperclassType = swift_getTypeName(super, true);
2570+
fatalError(0,
2571+
"demangled superclass %s (@%p) of %s differs from "
2572+
"compiler-provided "
2573+
"superclass %s (@%p) in swift_initClassMetadata\n",
2574+
StringRef(superclassType.data, superclassType.length)
2575+
.str().c_str(),
2576+
superclass,
2577+
self->getDescription()->Name.get(),
2578+
StringRef(providedSuperclassType.data,
2579+
providedSuperclassType.length)
2580+
.str().c_str(),
2581+
super);
2582+
}
2583+
}
2584+
25472585
self->Superclass = super;
25482586

25492587
#if SWIFT_OBJC_INTEROP
@@ -2594,6 +2632,34 @@ swift::swift_updateClassMetadata(ClassMetadata *self,
25942632
size_t numFields,
25952633
const TypeLayout * const *fieldTypes,
25962634
size_t *fieldOffsets) {
2635+
// If there is a mangled superclass name, demangle it to the superclass
2636+
// type.
2637+
if (auto superclassNameBase = self->getDescription()->SuperclassType.get()) {
2638+
StringRef superclassName =
2639+
Demangle::makeSymbolicMangledNameStringRef(superclassNameBase);
2640+
SubstGenericParametersFromMetadata substitutions(self);
2641+
const Metadata *superclass =
2642+
_getTypeByMangledName(superclassName, substitutions);
2643+
if (!superclass) {
2644+
fatalError(0,
2645+
"failed to demangle superclass of %s from mangled name '%s'\n",
2646+
self->getDescription()->Name.get(),
2647+
superclassName.str().c_str());
2648+
}
2649+
2650+
#if SWIFT_OBJC_INTEROP
2651+
if (auto objcWrapper = dyn_cast<ObjCClassWrapperMetadata>(superclass))
2652+
superclass = objcWrapper->Class;
2653+
#endif
2654+
2655+
if (superclass != super) {
2656+
fatalError(0,
2657+
"demangled superclass %p differs from compiler-provided "
2658+
"superclass %p in swift_updateClassMetadata\n",
2659+
superclass, super);
2660+
}
2661+
}
2662+
25972663
if (!super)
25982664
assert(self->Superclass == getRootSuperclass());
25992665
else

0 commit comments

Comments
 (0)