Skip to content

[BOLT] Fix memory leak in BinarySection #82520

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
Feb 21, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 13 additions & 7 deletions bolt/include/bolt/Core/BinarySection.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,7 @@ class BinarySection {
Alignment = NewAlignment;
ELFType = NewELFType;
ELFFlags = NewELFFlags;
OutputSize = NewSize;
OutputContents = StringRef(reinterpret_cast<const char *>(NewData),
NewData ? NewSize : 0);
IsFinalized = true;
updateContents(NewData, NewSize);
}

public:
Expand Down Expand Up @@ -484,9 +481,18 @@ class BinarySection {
void flushPendingRelocations(raw_pwrite_stream &OS,
SymbolResolverFuncTy Resolver);

/// Change contents of the section.
void updateContents(const uint8_t *Data, size_t NewSize) {
OutputContents = StringRef(reinterpret_cast<const char *>(Data), NewSize);
/// Change contents of the section. Unless the section has a valid SectionID,
/// the memory passed in \p NewData will be managed by the instance of
/// BinarySection.
void updateContents(const uint8_t *NewData, size_t NewSize) {
if (getOutputData() && !hasValidSectionID() &&
(!hasSectionRef() ||
OutputContents.data() != getContentsOrQuit(Section).data())) {
delete[] getOutputData();
}

OutputContents = StringRef(reinterpret_cast<const char *>(NewData),
NewData ? NewSize : 0);
OutputSize = NewSize;
IsFinalized = true;
}
Expand Down
13 changes: 1 addition & 12 deletions bolt/lib/Core/BinarySection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,7 @@ void BinarySection::flushPendingRelocations(raw_pwrite_stream &OS,
clearList(PendingRelocations);
}

BinarySection::~BinarySection() {
if (isReordered()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we dropping this condition?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we manage the memory inside updateContents() it's not needed and can lead to double-free.

delete[] getData();
return;
}

if (!isAllocatable() && !hasValidSectionID() &&
(!hasSectionRef() ||
OutputContents.data() != getContentsOrQuit(Section).data())) {
delete[] getOutputData();
}
}
BinarySection::~BinarySection() { updateContents(nullptr, 0); }

void BinarySection::clearRelocations() { clearList(Relocations); }

Expand Down
9 changes: 3 additions & 6 deletions bolt/lib/Rewrite/RewriteInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4092,12 +4092,9 @@ void RewriteInstance::rewriteNoteSections() {
return getNewValueForSymbol(S->getName());
});

// Set/modify section info.
BinarySection &NewSection = BC->registerOrUpdateNoteSection(
SectionName, SectionData, Size, Section.sh_addralign,
!BSec->isWritable(), BSec->getELFType());
NewSection.setOutputAddress(0);
NewSection.setOutputFileOffset(NextAvailableOffset);
// Section contents are no longer needed, but we need to update the size so
// that it will be reflected in the section header table.
BSec->updateContents(nullptr, Size);

NextAvailableOffset += Size;
}
Expand Down
9 changes: 5 additions & 4 deletions bolt/unittests/Core/BinaryContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,11 @@ TEST_P(BinaryContextTester, FlushPendingRelocCALL26) {
// 12: bl func2
// 16: func2

char Data[20] = {};
constexpr size_t DataSize = 20;
uint8_t *Data = new uint8_t[DataSize];
BinarySection &BS = BC->registerOrUpdateSection(
".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
(uint8_t *)Data, sizeof(Data), 4);
".text", ELF::SHT_PROGBITS, ELF::SHF_EXECINSTR | ELF::SHF_ALLOC, Data,
DataSize, 4);
MCSymbol *RelSymbol1 = BC->getOrCreateGlobalSymbol(4, "Func1");
ASSERT_TRUE(RelSymbol1);
BS.addRelocation(8, RelSymbol1, ELF::R_AARCH64_CALL26, 0, 0, true);
Expand All @@ -89,7 +90,7 @@ TEST_P(BinaryContextTester, FlushPendingRelocCALL26) {
BS.addRelocation(12, RelSymbol2, ELF::R_AARCH64_CALL26, 0, 0, true);

std::error_code EC;
SmallVector<char> Vect(sizeof(Data));
SmallVector<char> Vect(DataSize);
raw_svector_ostream OS(Vect);

BS.flushPendingRelocations(OS, [&](const MCSymbol *S) {
Expand Down