Skip to content

[5.10][Runtime] Lazily copy ivar list in initObjCClass. #68871

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 34 additions & 27 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3439,38 +3439,45 @@ static void initObjCClass(ClassMetadata *self,
size_t *fieldOffsets) {
ClassROData *rodata = getROData(self);

// Always clone the ivar descriptors.
if (numFields) {
const ClassIvarList *dependentIvars = rodata->IvarList;
assert(dependentIvars->Count == numFields);
assert(dependentIvars->EntrySize == sizeof(ClassIvarEntry));

auto ivarListSize = sizeof(ClassIvarList) +
numFields * sizeof(ClassIvarEntry);
auto ivars = (ClassIvarList*) getResilientMetadataAllocator()
.Allocate(ivarListSize, alignof(ClassIvarList));
memcpy(ivars, dependentIvars, ivarListSize);
rodata->IvarList = ivars;
ClassIvarList *ivars = rodata->IvarList;
if (!ivars) {
assert(numFields == 0);
return;
}

for (unsigned i = 0; i != numFields; ++i) {
auto *eltLayout = fieldTypes[i];
assert(ivars->Count == numFields);
assert(ivars->EntrySize == sizeof(ClassIvarEntry));

ClassIvarEntry &ivar = ivars->getIvars()[i];
bool copiedIvarList = false;

// Fill in the field offset global, if this ivar has one.
if (ivar.Offset) {
if (*ivar.Offset != fieldOffsets[i])
*ivar.Offset = fieldOffsets[i];
}
for (unsigned i = 0; i != numFields; ++i) {
auto *eltLayout = fieldTypes[i];

// If the ivar's size doesn't match the field layout we
// computed, overwrite it and give it better type information.
if (ivar.Size != eltLayout->size) {
ivar.Size = eltLayout->size;
ivar.Type = nullptr;
ivar.Log2Alignment =
getLog2AlignmentFromMask(eltLayout->flags.getAlignmentMask());
ClassIvarEntry &ivar = ivars->getIvars()[i];

// Fill in the field offset global, if this ivar has one.
if (ivar.Offset) {
if (*ivar.Offset != fieldOffsets[i])
*ivar.Offset = fieldOffsets[i];
}

// If the ivar's size doesn't match the field layout we
// computed, overwrite it and give it better type information.
if (ivar.Size != eltLayout->size) {
// If we're going to modify the ivar list, we need to copy it first.
if (!copiedIvarList) {
auto ivarListSize = sizeof(ClassIvarList) +
numFields * sizeof(ClassIvarEntry);
ivars = (ClassIvarList*) getResilientMetadataAllocator()
.Allocate(ivarListSize, alignof(ClassIvarList));
memcpy(ivars, rodata->IvarList, ivarListSize);
rodata->IvarList = ivars;
copiedIvarList = true;
}
ivar.Size = eltLayout->size;
ivar.Type = nullptr;
ivar.Log2Alignment =
getLog2AlignmentFromMask(eltLayout->flags.getAlignmentMask());
}
}
}
Expand Down