[BOLT][NFC] Check section contents before registering it
authorAmir Ayupov <aaupov@fb.com>
Tue, 8 Mar 2022 17:12:19 +0000 (09:12 -0800)
committerAmir Ayupov <aaupov@fb.com>
Tue, 8 Mar 2022 17:13:01 +0000 (09:13 -0800)
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
bolt/lib/Rewrite/RewriteInstance.cpp

index 63df47c..43049a4 100644 (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();
index d25f120..8489caa 100644 (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() {