[llvm-branch-commits] [lldb] r270066 - Fix FILE * leak in Python API

Francis Ricci via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Thu May 19 07:36:30 PDT 2016


Author: fjricci
Date: Thu May 19 09:36:28 2016
New Revision: 270066

URL: http://llvm.org/viewvc/llvm-project?rev=270066&view=rev
Log:
Fix FILE * leak in Python API

Summary:
This fixes a leak introduced by some of these changes:
r257644
r250530
r250525

The changes made in these patches result in leaking the FILE* passed
to SetImmediateOutputFile. GetStream() will dup() the fd held by the
python caller and create a new FILE*. It will then pass this FILE*
to SetImmediateOutputFile, which always uses the flag
transfer_ownership=false when it creates a File from the FILE*.

Since transfer_ownership is false, the lldb File destructor will not
close the underlying FILE*. Because this FILE* came from a dup-ed fd,
it will also not be closed when the python caller closes its file.

Leaking the FILE* causes issues if the same file is used multiple times
by different python callers during the same lldb run, even if these
callers open and close the python file properly, as you can end up
with issues due to multiple buffered writes to the same file.

Reviewers: granata.enrico, zturner, clayborg

Subscribers: zturner, lldb-commits, sas

Differential Revision: http://reviews.llvm.org/D18459

Change by Francis Ricci <fjricci at fb.com>

Modified:
    lldb/branches/release_38/include/lldb/API/SBCommandReturnObject.h
    lldb/branches/release_38/scripts/interface/SBCommandReturnObject.i
    lldb/branches/release_38/source/API/SBCommandReturnObject.cpp

Modified: lldb/branches/release_38/include/lldb/API/SBCommandReturnObject.h
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/include/lldb/API/SBCommandReturnObject.h?rev=270066&r1=270065&r2=270066&view=diff
==============================================================================
--- lldb/branches/release_38/include/lldb/API/SBCommandReturnObject.h (original)
+++ lldb/branches/release_38/include/lldb/API/SBCommandReturnObject.h Thu May 19 09:36:28 2016
@@ -83,7 +83,9 @@ public:
     
     bool
     GetDescription (lldb::SBStream &description);
-    
+   
+    // deprecated, these two functions do not take
+    // ownership of file handle
     void
     SetImmediateOutputFile (FILE *fh);
     
@@ -91,6 +93,12 @@ public:
     SetImmediateErrorFile (FILE *fh);
     
     void
+    SetImmediateOutputFile (FILE *fh, bool transfer_ownership);
+    
+    void
+    SetImmediateErrorFile (FILE *fh, bool transfer_ownership);
+    
+    void
     PutCString(const char* string, int len = -1);
     
     size_t

Modified: lldb/branches/release_38/scripts/interface/SBCommandReturnObject.i
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/scripts/interface/SBCommandReturnObject.i?rev=270066&r1=270065&r2=270066&view=diff
==============================================================================
--- lldb/branches/release_38/scripts/interface/SBCommandReturnObject.i (original)
+++ lldb/branches/release_38/scripts/interface/SBCommandReturnObject.i Thu May 19 09:36:28 2016
@@ -84,11 +84,17 @@ public:
     bool
     GetDescription (lldb::SBStream &description);
     
-    void
-    SetImmediateOutputFile (FILE *fh);
-    
-    void
-    SetImmediateErrorFile (FILE *fh);
+
+    // wrapping here so that lldb takes ownership of the 
+    // new FILE* created inside of the swig interface
+    %extend {
+        void SetImmediateOutputFile(FILE *fh) {
+            self->SetImmediateOutputFile(fh, true);
+        }
+        void SetImmediateErrorFile(FILE *fh) {
+            self->SetImmediateErrorFile(fh, true);
+        }
+    }
 
 	void
 	PutCString(const char* string, int len);

Modified: lldb/branches/release_38/source/API/SBCommandReturnObject.cpp
URL: http://llvm.org/viewvc/llvm-project/lldb/branches/release_38/source/API/SBCommandReturnObject.cpp?rev=270066&r1=270065&r2=270066&view=diff
==============================================================================
--- lldb/branches/release_38/source/API/SBCommandReturnObject.cpp (original)
+++ lldb/branches/release_38/source/API/SBCommandReturnObject.cpp Thu May 19 09:36:28 2016
@@ -258,15 +258,27 @@ SBCommandReturnObject::GetDescription (S
 void
 SBCommandReturnObject::SetImmediateOutputFile(FILE *fh)
 {
-    if (m_opaque_ap)
-        m_opaque_ap->SetImmediateOutputFile(fh);
+    SetImmediateOutputFile(fh, false);
 }
 
 void
 SBCommandReturnObject::SetImmediateErrorFile(FILE *fh)
 {
+    SetImmediateErrorFile(fh, false);
+}
+
+void
+SBCommandReturnObject::SetImmediateOutputFile(FILE *fh, bool transfer_ownership)
+{
+    if (m_opaque_ap)
+        m_opaque_ap->SetImmediateOutputFile(fh, transfer_ownership);
+}
+
+void
+SBCommandReturnObject::SetImmediateErrorFile(FILE *fh, bool transfer_ownership)
+{
     if (m_opaque_ap)
-        m_opaque_ap->SetImmediateErrorFile(fh);
+        m_opaque_ap->SetImmediateErrorFile(fh, transfer_ownership);
 }
 
 void




More information about the llvm-branch-commits mailing list