[Lldb-commits] [lldb] da601ea - [lldb/Test] Assert that no targets or modules remain after a test completes.

Jonas Devlieghere via lldb-commits lldb-commits at lists.llvm.org
Fri Jun 12 15:17:51 PDT 2020


Author: Jonas Devlieghere
Date: 2020-06-12T15:17:44-07:00
New Revision: da601ea731ece45d8e4d283942f90dce5b2d386a

URL: https://github.com/llvm/llvm-project/commit/da601ea731ece45d8e4d283942f90dce5b2d386a
DIFF: https://github.com/llvm/llvm-project/commit/da601ea731ece45d8e4d283942f90dce5b2d386a.diff

LOG: [lldb/Test] Assert that no targets or modules remain after a test completes.

The reproducer intentionally leak every object allocated during replay,
which means that modules never get orphaned. If this were to happen for
another reason, we might not be testing what we think we are. Assert
that there are no targets left at the end of a test and that the global
module cache is empty in the non-reproducer scenario.

Differential revision: https://reviews.llvm.org/D81612

Added: 
    

Modified: 
    lldb/bindings/interface/SBModule.i
    lldb/include/lldb/API/SBModule.h
    lldb/packages/Python/lldbsuite/test/lldbtest.py
    lldb/source/API/SBModule.cpp

Removed: 
    


################################################################################
diff  --git a/lldb/bindings/interface/SBModule.i b/lldb/bindings/interface/SBModule.i
index a9d9480cd7cf..e902af0c49ce 100644
--- a/lldb/bindings/interface/SBModule.i
+++ b/lldb/bindings/interface/SBModule.i
@@ -344,6 +344,15 @@ public:
     lldb::SBAddress
     GetObjectFileEntryPointAddress() const;
 
+    %feature("docstring", "
+    Returns the number of modules in the module cache. This is an
+    implementation detail exposed for testing and should not be relied upon.
+
+    @return
+        The number of modules in the module cache.") GetNumberAllocatedModules;
+    static uint32_t
+    GetNumberAllocatedModules();
+
     STRING_EXTENSION(SBModule)
 
 #ifdef SWIGPYTHON

diff  --git a/lldb/include/lldb/API/SBModule.h b/lldb/include/lldb/API/SBModule.h
index 183e451662eb..859eaffe89a0 100644
--- a/lldb/include/lldb/API/SBModule.h
+++ b/lldb/include/lldb/API/SBModule.h
@@ -288,6 +288,9 @@ class LLDB_API SBModule {
   lldb::SBAddress GetObjectFileHeaderAddress() const;
   lldb::SBAddress GetObjectFileEntryPointAddress() const;
 
+  /// Get the number of global modules.
+  static uint32_t GetNumberAllocatedModules();
+
 private:
   friend class SBAddress;
   friend class SBFrame;

diff  --git a/lldb/packages/Python/lldbsuite/test/lldbtest.py b/lldb/packages/Python/lldbsuite/test/lldbtest.py
index cad7a127e752..7190be6be9b3 100644
--- a/lldb/packages/Python/lldbsuite/test/lldbtest.py
+++ b/lldb/packages/Python/lldbsuite/test/lldbtest.py
@@ -2018,10 +2018,18 @@ def tearDown(self):
                 process = target.GetProcess()
                 if process:
                     rc = self.invoke(process, "Kill")
-                    self.assertTrue(rc.Success(), PROCESS_KILLED)
+                    assert rc.Success()
         for target in targets:
             self.dbg.DeleteTarget(target)
 
+        # Modules are not orphaned during reproducer replay because they're
+        # leaked on purpose.
+        if not configuration.is_reproducer():
+            # Assert that all targets are deleted.
+            assert self.dbg.GetNumTargets() == 0
+            # Assert that the global module cache is empty.
+            assert lldb.SBModule.GetNumberAllocatedModules() == 0
+
         # Do this last, to make sure it's in reverse order from how we setup.
         Base.tearDown(self)
 

diff  --git a/lldb/source/API/SBModule.cpp b/lldb/source/API/SBModule.cpp
index 0811a3a9b234..c30529b37eb1 100644
--- a/lldb/source/API/SBModule.cpp
+++ b/lldb/source/API/SBModule.cpp
@@ -683,6 +683,13 @@ lldb::SBAddress SBModule::GetObjectFileEntryPointAddress() const {
   return LLDB_RECORD_RESULT(sb_addr);
 }
 
+uint32_t SBModule::GetNumberAllocatedModules() {
+  LLDB_RECORD_STATIC_METHOD_NO_ARGS(uint32_t, SBModule,
+                                    GetNumberAllocatedModules);
+
+  return Module::GetNumberAllocatedModules();
+}
+
 namespace lldb_private {
 namespace repro {
 
@@ -757,6 +764,8 @@ void RegisterMethods<SBModule>(Registry &R) {
                              GetObjectFileHeaderAddress, ());
   LLDB_REGISTER_METHOD_CONST(lldb::SBAddress, SBModule,
                              GetObjectFileEntryPointAddress, ());
+  LLDB_REGISTER_STATIC_METHOD(uint32_t, SBModule, GetNumberAllocatedModules,
+                              ());
 }
 
 }


        


More information about the lldb-commits mailing list