From ced5472e09b4abe4f8be863592609ee6bbebf8b2 Mon Sep 17 00:00:00 2001 From: Amir Ayupov Date: Tue, 8 Mar 2022 09:12:19 -0800 Subject: [PATCH] [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 --- bolt/include/bolt/Rewrite/RewriteInstance.h | 2 +- bolt/lib/Rewrite/RewriteInstance.cpp | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/bolt/include/bolt/Rewrite/RewriteInstance.h b/bolt/include/bolt/Rewrite/RewriteInstance.h index 63df47c..43049a4 100644 --- a/bolt/include/bolt/Rewrite/RewriteInstance.h +++ b/bolt/include/bolt/Rewrite/RewriteInstance.h @@ -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(); diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index d25f120..8489caa 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -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 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() { -- 2.7.4