Skip to content

[BOLT][DWARF][NFC] Fix DebugStrOffsetsWriter #100672

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 10 commits into from
Jul 27, 2024

Conversation

sayhaan
Copy link
Member

@sayhaan sayhaan commented Jul 26, 2024

Fix DebugStrOffsetsWriter so updateAddressMap can't be called after it is finalized.

@sayhaan sayhaan force-pushed the fix-debug-str-offset branch from 4db1498 to 9027af9 Compare July 26, 2024 00:05
@sayhaan sayhaan changed the title Fix debug str offset [BOLT][DWARF][NFC] Fix DebugStrOffsetsWriter Jul 26, 2024
@sayhaan sayhaan marked this pull request as ready for review July 26, 2024 00:06
@llvmbot
Copy link
Member

llvmbot commented Jul 26, 2024

@llvm/pr-subscribers-debuginfo

@llvm/pr-subscribers-bolt

Author: Sayhaan Siddiqui (sayhaan)

Changes

Fix DebugStrOffsetsWriter so updateAddressMap can't be called after it is finalized.


Full diff: https://github.com/llvm/llvm-project/pull/100672.diff

5 Files Affected:

  • (modified) bolt/lib/Core/DIEBuilder.cpp (+2)
  • (modified) bolt/lib/Core/DebugData.cpp (+1)
  • (modified) bolt/lib/Rewrite/DWARFRewriter.cpp (+2)
  • (modified) llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h (+5-1)
  • (modified) llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp (+4-2)
diff --git a/bolt/lib/Core/DIEBuilder.cpp b/bolt/lib/Core/DIEBuilder.cpp
index b0f550fd77318..eb5b6ed31313f 100644
--- a/bolt/lib/Core/DIEBuilder.cpp
+++ b/bolt/lib/Core/DIEBuilder.cpp
@@ -77,6 +77,8 @@ static void addStringHelper(DebugStrOffsetsWriter &StrOffstsWriter,
                             DIEValue &DIEAttrInfo, StringRef Str) {
   uint32_t NewOffset = StrWriter.addString(Str);
   if (Unit.getVersion() >= 5) {
+    assert(!Unit.isDebugStrOffsetFinalized() &&
+           "debug_str_offsets was already finalized.");
     StrOffstsWriter.updateAddressMap(DIEAttrInfo.getDIEInteger().getValue(),
                                      NewOffset);
     return;
diff --git a/bolt/lib/Core/DebugData.cpp b/bolt/lib/Core/DebugData.cpp
index 002f58c474346..e96656687fdfe 100644
--- a/bolt/lib/Core/DebugData.cpp
+++ b/bolt/lib/Core/DebugData.cpp
@@ -906,6 +906,7 @@ void DebugStrOffsetsWriter::finalizeSection(DWARFUnit &Unit,
   }
 
   StrOffsetSectionWasModified = false;
+  Unit.setDebugStrOffsetFinalized();
   clear();
 }
 
diff --git a/bolt/lib/Rewrite/DWARFRewriter.cpp b/bolt/lib/Rewrite/DWARFRewriter.cpp
index 674b5f17adb3f..adf4c7a32e579 100644
--- a/bolt/lib/Rewrite/DWARFRewriter.cpp
+++ b/bolt/lib/Rewrite/DWARFRewriter.cpp
@@ -757,6 +757,8 @@ void DWARFRewriter::updateDebugInfo() {
               : std::optional<std::string>(opts::DwarfOutputPath.c_str());
       std::string DWOName = DIEBlder.updateDWONameCompDir(
           *StrOffstsWriter, *StrWriter, *CU, DwarfOutputPath, std::nullopt);
+      if (CU->getVersion() >= 5)
+        StrOffstsWriter->finalizeSection(*CU, DIEBlder);
       processSplitCU(*CU, **SplitCU, DIEBlder, *TempRangesSectionWriter,
                      AddressWriter, DWOName, DwarfOutputPath);
     }
diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
index 80c27aea89312..2642d13b16e4c 100644
--- a/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
+++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFUnit.h
@@ -230,6 +230,7 @@ class DWARFUnit {
   std::optional<uint64_t> AddrOffsetSectionBase;
   bool IsLittleEndian;
   bool IsDWO;
+  bool IsDebugStrOffsetFinalized;
   const DWARFUnitVector &UnitVector;
 
   /// Start, length, and DWARF format of the unit's contribution to the string
@@ -310,12 +311,15 @@ class DWARFUnit {
             const DWARFSection *RS, const DWARFSection *LocSection,
             StringRef SS, const DWARFSection &SOS, const DWARFSection *AOS,
             const DWARFSection &LS, bool LE, bool IsDWO,
-            const DWARFUnitVector &UnitVector);
+            const DWARFUnitVector &UnitVector,
+            bool IsDebugStrOffsetFinalized = false);
 
   virtual ~DWARFUnit();
 
   bool isLittleEndian() const { return IsLittleEndian; }
   bool isDWOUnit() const { return IsDWO; }
+  bool isDebugStrOffsetFinalized() const { return IsDebugStrOffsetFinalized; }
+  void setDebugStrOffsetFinalized() { IsDebugStrOffsetFinalized = true; }
   DWARFContext& getContext() const { return Context; }
   const DWARFSection &getInfoSection() const { return InfoSection; }
   uint64_t getOffset() const { return Header.getOffset(); }
diff --git a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
index bdd04b00f557b..d2143772946c3 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFUnit.cpp
@@ -196,11 +196,13 @@ DWARFUnit::DWARFUnit(DWARFContext &DC, const DWARFSection &Section,
                      const DWARFSection *RS, const DWARFSection *LocSection,
                      StringRef SS, const DWARFSection &SOS,
                      const DWARFSection *AOS, const DWARFSection &LS, bool LE,
-                     bool IsDWO, const DWARFUnitVector &UnitVector)
+                     bool IsDWO, const DWARFUnitVector &UnitVector,
+                     bool IsDebugStrOffsetFinalized)
     : Context(DC), InfoSection(Section), Header(Header), Abbrev(DA),
       RangeSection(RS), LineSection(LS), StringSection(SS),
       StringOffsetSection(SOS), AddrOffsetSection(AOS), IsLittleEndian(LE),
-      IsDWO(IsDWO), UnitVector(UnitVector) {
+      IsDWO(IsDWO), IsDebugStrOffsetFinalized(IsDebugStrOffsetFinalized),
+      UnitVector(UnitVector) {
   clear();
 }
 

@ayermolo
Copy link
Contributor

The check is BOLT specific, please do not modify DWARFUnit.

Copy link

github-actions bot commented Jul 26, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

@ayermolo
Copy link
Contributor

Add/modify a test.

sayhaan added 2 commits July 26, 2024 10:47
…t be called after it is finalized

Summary: 

Test Plan: 

Reviewers: 

Subscribers: 

Tasks: 

Tags: 


Differential Revision: https://phabricator.intern.facebook.com/D60293063
@sayhaan sayhaan force-pushed the fix-debug-str-offset branch from 219abd5 to 8c807df Compare July 26, 2024 18:12
@sayhaan sayhaan requested a review from ayermolo July 26, 2024 23:08
@ayermolo
Copy link
Contributor

For the test, all we care about are strings (since that's what bug is about). Please reduce the test, and name it more appropriately.

Copy link
Contributor

@ayermolo ayermolo left a comment

Choose a reason for hiding this comment

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

LGTM
Check internal diff passes before merging.

@sayhaan sayhaan merged commit 9a3e66e into llvm:main Jul 27, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants