Skip to content

Commit 4bfdaed

Browse files
committed
[NFC] Runtime: Extract function.
Break this chunk of functionality out into a separate function in preparation for adding another caller.
1 parent 5d53d3b commit 4bfdaed

File tree

1 file changed

+38
-31
lines changed

1 file changed

+38
-31
lines changed

stdlib/public/runtime/Metadata.cpp

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3644,6 +3644,43 @@ static void copySuperclassMetadataToSubclass(ClassMetadata *theClass,
36443644
#endif
36453645
}
36463646

3647+
static void installOverrideInVTable(
3648+
ClassDescriptor::MethodOverrideDescriptor const &descriptor,
3649+
ClassDescriptor::OverrideTableHeader const *overrideTable,
3650+
void **classWords) {
3651+
// Get the base class and method.
3652+
auto *baseClass = cast_or_null<ClassDescriptor>(descriptor.Class.get());
3653+
auto *baseMethod = descriptor.Method.get();
3654+
3655+
// If the base method is null, it's an unavailable weak-linked
3656+
// symbol.
3657+
if (baseClass == nullptr || baseMethod == nullptr)
3658+
return;
3659+
3660+
// Calculate the base method's vtable offset from the
3661+
// base method descriptor. The offset will be relative
3662+
// to the base class's vtable start offset.
3663+
auto baseClassMethods = baseClass->getMethodDescriptors();
3664+
3665+
// If the method descriptor doesn't land within the bounds of the
3666+
// method table, abort.
3667+
if (baseMethod < baseClassMethods.begin() ||
3668+
baseMethod >= baseClassMethods.end()) {
3669+
fatalError(0,
3670+
"resilient vtable at %p contains out-of-bounds "
3671+
"method descriptor %p\n",
3672+
overrideTable, baseMethod);
3673+
}
3674+
3675+
// Install the method override in our vtable.
3676+
auto baseVTable = baseClass->getVTableDescriptor();
3677+
auto offset = (baseVTable->getVTableOffset(baseClass) +
3678+
(baseMethod - baseClassMethods.data()));
3679+
swift_ptrauth_init_code_or_data(&classWords[offset], descriptor.getImpl(),
3680+
baseMethod->Flags.getExtraDiscriminator(),
3681+
!baseMethod->Flags.isData());
3682+
}
3683+
36473684
/// Using the information in the class context descriptor, fill in in the
36483685
/// immediate vtable entries for the class and install overrides of any
36493686
/// superclass vtable entries.
@@ -3667,40 +3704,10 @@ static void initClassVTable(ClassMetadata *self) {
36673704
if (description->hasOverrideTable()) {
36683705
auto *overrideTable = description->getOverrideTable();
36693706
auto overrideDescriptors = description->getMethodOverrideDescriptors();
3670-
36713707
for (unsigned i = 0, e = overrideTable->NumEntries; i < e; ++i) {
36723708
auto &descriptor = overrideDescriptors[i];
36733709

3674-
// Get the base class and method.
3675-
auto *baseClass = cast_or_null<ClassDescriptor>(descriptor.Class.get());
3676-
auto *baseMethod = descriptor.Method.get();
3677-
3678-
// If the base method is null, it's an unavailable weak-linked
3679-
// symbol.
3680-
if (baseClass == nullptr || baseMethod == nullptr)
3681-
continue;
3682-
3683-
// Calculate the base method's vtable offset from the
3684-
// base method descriptor. The offset will be relative
3685-
// to the base class's vtable start offset.
3686-
auto baseClassMethods = baseClass->getMethodDescriptors();
3687-
3688-
// If the method descriptor doesn't land within the bounds of the
3689-
// method table, abort.
3690-
if (baseMethod < baseClassMethods.begin() ||
3691-
baseMethod >= baseClassMethods.end()) {
3692-
fatalError(0, "resilient vtable at %p contains out-of-bounds "
3693-
"method descriptor %p\n",
3694-
overrideTable, baseMethod);
3695-
}
3696-
3697-
// Install the method override in our vtable.
3698-
auto baseVTable = baseClass->getVTableDescriptor();
3699-
auto offset = (baseVTable->getVTableOffset(baseClass) +
3700-
(baseMethod - baseClassMethods.data()));
3701-
swift_ptrauth_init_code_or_data(&classWords[offset], descriptor.getImpl(),
3702-
baseMethod->Flags.getExtraDiscriminator(),
3703-
!baseMethod->Flags.isData());
3710+
installOverrideInVTable(descriptor, overrideTable, classWords);
37043711
}
37053712
}
37063713
}

0 commit comments

Comments
 (0)