[PATCH 2/2] covoar: Prevent source file name cleanup in DWARF

Alex White alex.white at oarcorp.com
Fri Apr 9 16:13:29 UTC 2021


This changes the rld::dwarf::sources class to store a copy of the
source file names obtained from the DWARF information rather than
storing pointers to the source file names. This allows the source file
names to be referenced even after the DWARF library does its cleanup.

Closes #4383
---
 rtemstoolkit/rld-dwarf.cpp      | 41 +++++++++++++++++----------------
 rtemstoolkit/rld-dwarf.h        |  8 +++----
 tester/covoar/ExecutableInfo.cc |  5 +---
 3 files changed, 26 insertions(+), 28 deletions(-)

diff --git a/rtemstoolkit/rld-dwarf.cpp b/rtemstoolkit/rld-dwarf.cpp
index 2fce0e4..0665be9 100644
--- a/rtemstoolkit/rld-dwarf.cpp
+++ b/rtemstoolkit/rld-dwarf.cpp
@@ -504,17 +504,25 @@ namespace rld
 
     sources::sources (file& debug, dwarf_offset die_offset)
       : debug (debug),
-        source (nullptr),
+        source (),
         count (0),
         die_offset (die_offset)
     {
       debug_info_entry die (debug, die_offset);
-      die.source_files (source, count);
+      char** source_ptr;
+      die.source_files (source_ptr, count);
+      /*
+       * Copy the source file strings so that they aren't cleaned up with the
+       * rest of the DWARF objects.
+       */
+      for (int i = 0; i < count; i++) {
+        source.push_back(source_ptr[i]);
+      }
     }
 
     sources::sources (const sources& orig)
       : debug (orig.debug),
-        source (nullptr),
+        source (),
         count (0),
         die_offset (orig.die_offset)
     {
@@ -523,7 +531,15 @@ namespace rld
        * do that we need to get the DIE at the offset in the original.
        */
       debug_info_entry die (debug, die_offset);
-      die.source_files (source, count);
+      char** source_ptr;
+      /*
+       * Copy the source file strings so that they aren't cleaned up with the
+       * rest of the DWARF objects.
+       */
+      die.source_files (source_ptr, count);
+      for (int i = 0; i < count; i++) {
+        source.push_back(source_ptr[i]);
+      }
     }
 
     sources::~sources ()
@@ -542,20 +558,8 @@ namespace rld
     void
     sources::dealloc ()
     {
-      if (source && count > 0)
+      if (count > 0)
       {
-        /*
-         * The elftoolchain cleans the memory up and there is no compatible
-         * call we can put here so adding the required code causes is a double
-         * free resulting in a crash.
-         */
-        if (false)
-        {
-          for (int s = 0; s < count; ++s)
-            ::dwarf_dealloc (debug, source[s], DW_DLA_STRING);
-          ::dwarf_dealloc (debug, source, DW_DLA_LIST);
-        }
-        source = nullptr;
         count = 0;
       }
     }
@@ -569,7 +573,6 @@ namespace rld
         source = rhs.source;
         count = rhs.count;
         die_offset = rhs.die_offset;
-        rhs.source = nullptr;
         rhs.count = 0;
       }
       return *this;
@@ -2036,8 +2039,6 @@ namespace rld
         if (rld::verbose () >= RLD_VERBOSE_FULL_DEBUG)
           std::cout << "dwarf::end: " << name () << std::endl;
 
-        cus.clear ();
-
         ::dwarf_finish (debug, 0);
         if (elf_)
           elf_->reference_release ();
diff --git a/rtemstoolkit/rld-dwarf.h b/rtemstoolkit/rld-dwarf.h
index 1210813..b745b59 100644
--- a/rtemstoolkit/rld-dwarf.h
+++ b/rtemstoolkit/rld-dwarf.h
@@ -270,10 +270,10 @@ namespace rld
 
     private:
 
-      file&        debug;
-      char**       source;
-      dwarf_signed count;
-      dwarf_offset die_offset;
+      file&                    debug;
+      std::vector<std::string> source;
+      dwarf_signed             count;
+      dwarf_offset             die_offset;
     };
 
     /**
diff --git a/tester/covoar/ExecutableInfo.cc b/tester/covoar/ExecutableInfo.cc
index 861e60d..f6bf6b1 100644
--- a/tester/covoar/ExecutableInfo.cc
+++ b/tester/covoar/ExecutableInfo.cc
@@ -87,14 +87,11 @@ namespace Coverage {
       throw;
     }
 
-    // Can't cleanup handles until the destructor because the information is
-    // referenced elsewhere. NOTE: This could cause problems from too many open
-    // file descriptors.
+    debug.end();
   }
 
   ExecutableInfo::~ExecutableInfo()
   {
-    debug.end();
   }
 
   void ExecutableInfo::dumpCoverageMaps( void ) {
-- 
2.27.0



More information about the devel mailing list