[Lldb-commits] [lldb] r107539 - in /lldb/trunk/source/Plugins/Process/Utility/libunwind/src: AssemblyParser.hpp RemoteProcInfo.hpp UnwindCursor.hpp libuwind.cxx

Greg Clayton gclayton at apple.com
Fri Jul 2 16:23:14 PDT 2010


Author: gclayton
Date: Fri Jul  2 18:23:14 2010
New Revision: 107539

URL: http://llvm.org/viewvc/llvm-project?rev=107539&view=rev
Log:
Plugged 4 more leaks in the libunwind code. One leaks is still left in as it
is quite gnarly code and there is no good way to clean it up. I will have
Jason look at a fix for this.


Modified:
    lldb/trunk/source/Plugins/Process/Utility/libunwind/src/AssemblyParser.hpp
    lldb/trunk/source/Plugins/Process/Utility/libunwind/src/RemoteProcInfo.hpp
    lldb/trunk/source/Plugins/Process/Utility/libunwind/src/UnwindCursor.hpp
    lldb/trunk/source/Plugins/Process/Utility/libunwind/src/libuwind.cxx

Modified: lldb/trunk/source/Plugins/Process/Utility/libunwind/src/AssemblyParser.hpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/libunwind/src/AssemblyParser.hpp?rev=107539&r1=107538&r2=107539&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/libunwind/src/AssemblyParser.hpp (original)
+++ lldb/trunk/source/Plugins/Process/Utility/libunwind/src/AssemblyParser.hpp Fri Jul  2 18:23:14 2010
@@ -44,6 +44,8 @@
 
 class AssemblyParse_x86 {
 public:
+    enum { kMaxInstructionByteSize = 32 };
+
     AssemblyParse_x86 (RemoteProcInfo& procinfo, unw_accessors_t *acc, unw_addr_space_t as, void *arg) : fArg(arg), fAccessors(acc), fAs(as), fRemoteProcInfo(procinfo) {
         fRegisterMap = fRemoteProcInfo.getRegisterMap();
         if (fRemoteProcInfo.getTargetArch() == UNW_TARGET_X86_64) {
@@ -76,7 +78,7 @@
 private:
 
     void *fArg;
-    uint8_t*           fCurInsnByteBuf;
+    uint8_t            fCurInsnByteBuf[kMaxInstructionByteSize];
     int                fCurInsnSize;
     RemoteProcInfo&    fRemoteProcInfo;
     RemoteRegisterMap  *fRegisterMap;
@@ -285,7 +287,7 @@
             /* An error parsing the instruction; stop scanning.  */
             break;
         }
-        fCurInsnByteBuf = (uint8_t *) malloc (fCurInsnSize);
+        assert (fCurInsnSize <= kMaxInstructionByteSize);
         if (fRemoteProcInfo.getBytes (cur_addr, fCurInsnSize, fCurInsnByteBuf, fArg) == 0)
           return false;
         next_addr = cur_addr + fCurInsnSize;

Modified: lldb/trunk/source/Plugins/Process/Utility/libunwind/src/RemoteProcInfo.hpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/libunwind/src/RemoteProcInfo.hpp?rev=107539&r1=107538&r2=107539&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/libunwind/src/RemoteProcInfo.hpp (original)
+++ lldb/trunk/source/Plugins/Process/Utility/libunwind/src/RemoteProcInfo.hpp Fri Jul  2 18:23:14 2010
@@ -173,7 +173,7 @@
     void addImage (uint64_t mh, uint64_t text_start, uint64_t text_end, uint64_t eh_frame, uint64_t eh_frame_len, uint64_t compact_unwind_start, uint64_t compact_unwind_len);
     bool addProfile (RemoteProcInfo* procinfo, unw_accessors_t *acc, unw_addr_space_t as, uint64_t start, uint64_t end, void *arg);
     RemoteUnwindProfile* findProfileByTextAddr (uint64_t pc);
-    void addMemBlob (RemoteMemoryBlob *blob);
+    bool addMemBlob (RemoteMemoryBlob *blob);
     uint8_t *getMemBlobMemory (uint64_t addr, int len);
 private:
     RemoteImages();
@@ -312,14 +312,25 @@
 // as this is used today, the only duplication will be with the same
 // start address.
 
-void RemoteImages::addMemBlob (RemoteMemoryBlob *blob) { 
-    std::vector<RemoteMemoryBlob *>::iterator i;
-    for (i = fMemBlobs.begin(); i != fMemBlobs.end(); ++i) {
-        if (blob->getStartAddr() == (*i)->getStartAddr())
-            return;
+bool 
+RemoteImages::addMemBlob (RemoteMemoryBlob *blob) { 
+
+    if (fMemBlobs.empty())
+    {
+        fMemBlobs.push_back(blob);
+    }
+    else
+    {
+        std::vector<RemoteMemoryBlob *>::iterator pos;
+
+        pos = std::lower_bound (fMemBlobs.begin(), fMemBlobs.end(), blob);
+
+        if (pos != fMemBlobs.end() && (*pos)->getStartAddr() == blob->getStartAddr())
+            return false;
+
+        fMemBlobs.insert (pos, blob);
     }
-    fMemBlobs.push_back(blob); 
-    std::sort(fMemBlobs.begin(), fMemBlobs.end());
+    return true;
 }
 
 uint8_t *RemoteImages::getMemBlobMemory (uint64_t addr, int len) {
@@ -702,7 +713,7 @@
     unw_addr_space_t wrap ()                    { return (unw_addr_space_t) &fWrapper; }
     bool remoteIsLittleEndian ()                { return fLittleEndian; }
     unw_log_level_t getDebugLoggingLevel()      { return fLogLevel; }
-    void addMemBlob (RemoteMemoryBlob *blob)    { fImages.addMemBlob(blob); }
+    bool addMemBlob (RemoteMemoryBlob *blob)    { return fImages.addMemBlob(blob); }
     unw_caching_policy_t getCachingPolicy()     { return fCachingPolicy; }
 
 private:
@@ -742,14 +753,16 @@
                     uint8_t *buf = (uint8_t*) malloc (compact_unwind_len);
                     if (this->getBytes (compact_unwind, compact_unwind_len, buf, arg)) {
                         RemoteMemoryBlob *b = new RemoteMemoryBlob(buf, free, compact_unwind, compact_unwind_len, mh, NULL);
-                        fImages.addMemBlob (b);
+                        if (fImages.addMemBlob (b) == false)
+                            delete b;
                     }
                 } else if (eh_frame_len != 0) {
                     logVerbose ("Creating RemoteMemoryBlob of eh_frame for image at mh 0x%llx, %lld bytes", mh, (uint64_t) compact_unwind_len);
                     uint8_t *buf = (uint8_t*) malloc (eh_frame_len);
                     if (this->getBytes (eh_frame, eh_frame_len, buf, arg)) {
                         RemoteMemoryBlob *b = new RemoteMemoryBlob(buf, free, eh_frame, eh_frame_len, mh, NULL);
-                        fImages.addMemBlob (b);
+                        if (fImages.addMemBlob (b) == false)
+                            delete b;
                     }
                 }
             }

Modified: lldb/trunk/source/Plugins/Process/Utility/libunwind/src/UnwindCursor.hpp
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/libunwind/src/UnwindCursor.hpp?rev=107539&r1=107538&r2=107539&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/libunwind/src/UnwindCursor.hpp (original)
+++ lldb/trunk/source/Plugins/Process/Utility/libunwind/src/UnwindCursor.hpp Fri Jul  2 18:23:14 2010
@@ -1168,9 +1168,13 @@
                 // thing anyway so we might as well save it.
                 uint8_t *eh_buf = (uint8_t *)malloc (eh_frame_len);
                 if (UnwindCursor<A,R>::fAddressSpace.getBytes (eh_frame_start, eh_frame_len, eh_buf) == 0)
-                  return UNW_EUNSPEC;
+                {
+                    free (eh_buf);
+                    return UNW_EUNSPEC;
+                }
                 RemoteMemoryBlob *ehmem = new RemoteMemoryBlob(eh_buf, free, eh_frame_start, eh_frame_len, mh, NULL);
-                procinfo->addMemBlob (ehmem);
+                if (procinfo->addMemBlob (ehmem) == false)
+                    delete ehmem;
             }
             
             if (CFI_Parser<A>::functionFuncBoundsViaFDE(UnwindCursor<A,R>::fAddressSpace, eh_frame_start, eh_frame_len, func_bounds)) {
@@ -1271,9 +1275,13 @@
                     // thing anyway so we might as well save it.
                     uint8_t *eh_buf = (uint8_t *)malloc (eh_frame_len);
                     if (UnwindCursor<A,R>::fAddressSpace.getBytes (eh_frame_start, eh_frame_len, eh_buf) == 0)
-                      return UNW_EUNSPEC;
+                    {
+                        free (eh_buf);
+                        return UNW_EUNSPEC;
+                    }
                     RemoteMemoryBlob *ehmem = new RemoteMemoryBlob(eh_buf, free, eh_frame_start, eh_frame_len, mh, NULL);
-                    procinfo->addMemBlob (ehmem);
+                    if (procinfo->addMemBlob (ehmem) == false)
+                        delete ehmem;
                 }
                 if (CFI_Parser<A>::functionFuncBoundsViaFDE(UnwindCursor<A,R>::fAddressSpace, eh_frame_start, eh_frame_len, func_bounds)) {
                     procinfo->addFuncBounds(mh, func_bounds);

Modified: lldb/trunk/source/Plugins/Process/Utility/libunwind/src/libuwind.cxx
URL: http://llvm.org/viewvc/llvm-project/lldb/trunk/source/Plugins/Process/Utility/libunwind/src/libuwind.cxx?rev=107539&r1=107538&r2=107539&view=diff
==============================================================================
--- lldb/trunk/source/Plugins/Process/Utility/libunwind/src/libuwind.cxx (original)
+++ lldb/trunk/source/Plugins/Process/Utility/libunwind/src/libuwind.cxx Fri Jul  2 18:23:14 2010
@@ -235,25 +235,32 @@
     // with the rest of the code here.
     switch ( remote->ras->getTargetArch() ) {
         case UNW_TARGET_I386:
-	{
-                Registers_x86 *r = new Registers_x86;
+            {
+                Registers_x86 r;
+                // LEAK: "addrSpace" is being leaked every time here with no easy solution.
+                // The address space is in the cursor and the cursor may get 
+                // duplicated, though if it does get duplicated, it will just be
+                // memcpy'ed since unw_cursor_t is just a bunch of uint64_t types...
                 OtherAddressSpace<Pointer32<LittleEndian> > *addrSpace = new OtherAddressSpace<Pointer32<LittleEndian> >(as, arg);
-                getRemoteContext (remote->ras, *r, arg);
-                unw_context_t *context = (unw_context_t*) r;
+                getRemoteContext (remote->ras, r, arg);
+                unw_context_t *context = (unw_context_t*) &r;
                 new ((void*)cursor) RemoteUnwindCursor<OtherAddressSpace<Pointer32<LittleEndian> >, Registers_x86>(*addrSpace, context, arg);
-                break;
-	}
+            }
             break;
         case UNW_TARGET_X86_64:
-	{
-                Registers_x86_64 *r = new Registers_x86_64;
+            {
+                Registers_x86_64 r;
+                // LEAK: "addrSpace" is being leaked every time here with no easy solution.
+                // The address space is in the cursor and the cursor may get 
+                // duplicated, though if it does get duplicated, it will just be
+                // memcpy'ed since unw_cursor_t is just a bunch of uint64_t types...
                 OtherAddressSpace<Pointer64<LittleEndian> > *addrSpace = new OtherAddressSpace<Pointer64<LittleEndian> >(as, arg);
-                getRemoteContext (remote->ras, *r, arg);
-                unw_context_t *context = (unw_context_t*) r;
+                getRemoteContext (remote->ras, r, arg);
+                unw_context_t *context = (unw_context_t*) &r;
                 new ((void*)cursor) RemoteUnwindCursor<OtherAddressSpace<Pointer64<LittleEndian> >, Registers_x86_64>(*addrSpace, context, arg);
-                break;
-	}
-
+            }
+            break;
+            
         case UNW_TARGET_PPC:
               ABORT("ppc not supported for remote unwinds");
             break;





More information about the lldb-commits mailing list