[BOLT][NFC] Check section contents before registering it

Address fuzzer crash on malformed input:
```
BOLT-ERROR: cannot get section contents for .dynsym: The end of the file was unexpectedly encountered.
```

Reviewed By: rafauler

Differential Revision: https://reviews.llvm.org/D121068
This commit is contained in:
Amir Ayupov 2022-03-08 09:12:19 -08:00
parent f4939d5618
commit ced5472e09
2 changed files with 7 additions and 3 deletions

View File

@ -96,7 +96,7 @@ private:
/// Read info from special sections. E.g. eh_frame and .gcc_except_table
/// for exception and stack unwinding information.
void readSpecialSections();
Error readSpecialSections();
/// Adjust supplied command-line options based on input data.
void adjustCommandLineOptions();

View File

@ -765,7 +765,8 @@ Error RewriteInstance::run() {
if (Error E = discoverStorage())
return E;
readSpecialSections();
if (Error E = readSpecialSections())
return E;
adjustCommandLineOptions();
discoverFileObjects();
@ -1540,7 +1541,7 @@ ArrayRef<uint8_t> RewriteInstance::getLSDAData() {
uint64_t RewriteInstance::getLSDAAddress() { return LSDASection->getAddress(); }
void RewriteInstance::readSpecialSections() {
Error RewriteInstance::readSpecialSections() {
NamedRegionTimer T("readSpecialSections", "read special sections",
TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
@ -1555,6 +1556,8 @@ void RewriteInstance::readSpecialSections() {
// Only register sections with names.
if (!SectionName.empty()) {
if (Error E = Section.getContents().takeError())
return E;
BC->registerSection(Section);
LLVM_DEBUG(
dbgs() << "BOLT-DEBUG: registering section " << SectionName << " @ 0x"
@ -1633,6 +1636,7 @@ void RewriteInstance::readSpecialSections() {
// Read .dynamic/PT_DYNAMIC.
readELFDynamic();
return Error::success();
}
void RewriteInstance::adjustCommandLineOptions() {