Copy the breakpoint site owner's collection so we can drop
authorJim Ingham <jingham@apple.com>
Thu, 28 Mar 2019 01:51:33 +0000 (01:51 +0000)
committerJim Ingham <jingham@apple.com>
Thu, 28 Mar 2019 01:51:33 +0000 (01:51 +0000)
the collection lock before we iterate over the owners calling ShouldStop.

BreakpointSite::ShouldStop can do a lot of work, and might by chance hit the same breakpoint
site again on another thread.  So instead of holding the site's owners lock
while iterating over them calling ShouldStop, I make a local copy of the list, drop the lock
and then iterate over the copy calling BreakpointLocation::ShouldStop.

It's actually quite difficult to make this cause problems because usually all the
action happens on the private state thread, and the lock is recursive.

I have a report where some code hit the ASAN error breakpoint, went to
compile the ASAN error gathering expression, in the course of compiling
that we went to fetch the ObjC runtime data, but the state of the program
was such that the ObjC runtime grubbing function triggered an ASAN error and
we were executing that function on another thread.

I couldn't figure out a way to reproduce that situation in a test.  But this is an
NFC change anyway, it just makes the locking strategy more narrowly focused.

<rdar://problem/49074093>

llvm-svn: 357141

lldb/include/lldb/Breakpoint/BreakpointLocationCollection.h
lldb/source/Breakpoint/BreakpointLocationCollection.cpp
lldb/source/Breakpoint/BreakpointSite.cpp

index 1497c30..c1b3ff9 100644 (file)
@@ -22,6 +22,8 @@ public:
   BreakpointLocationCollection();
 
   ~BreakpointLocationCollection();
+  
+  BreakpointLocationCollection &operator=(const BreakpointLocationCollection &rhs);
 
   //------------------------------------------------------------------
   /// Add the breakpoint \a bp_loc_sp to the list.
index 8ca6d2f..413c12a 100644 (file)
@@ -178,3 +178,20 @@ void BreakpointLocationCollection::GetDescription(
     (*pos)->GetDescription(s, level);
   }
 }
+
+BreakpointLocationCollection &BreakpointLocationCollection::operator=(
+    const BreakpointLocationCollection &rhs) {
+  // Same trick as in ModuleList to avoid lock inversion.
+  if (this != &rhs) {
+    if (uintptr_t(this) > uintptr_t(&rhs)) {
+      std::lock_guard<std::mutex> lhs_guard(m_collection_mutex);
+      std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex);
+      m_break_loc_collection = rhs.m_break_loc_collection;
+    } else {
+      std::lock_guard<std::mutex> lhs_guard(m_collection_mutex);
+      std::lock_guard<std::mutex> rhs_guard(rhs.m_collection_mutex);
+      m_break_loc_collection = rhs.m_break_loc_collection;
+    }
+  }
+  return *this;
+}
index 8151f30..a757a01 100644 (file)
@@ -48,9 +48,17 @@ break_id_t BreakpointSite::GetNextID() {
 // should continue.
 
 bool BreakpointSite::ShouldStop(StoppointCallbackContext *context) {
-  std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
   IncrementHitCount();
-  return m_owners.ShouldStop(context);
+  // ShouldStop can do a lot of work, and might even come come back and hit
+  // this breakpoint site again.  So don't hold the m_owners_mutex the whole
+  // while.  Instead make a local copy of the collection and call ShouldStop on
+  // the copy.
+  BreakpointLocationCollection owners_copy;
+  {
+    std::lock_guard<std::recursive_mutex> guard(m_owners_mutex);
+    owners_copy = m_owners;
+  }
+  return owners_copy.ShouldStop(context);
 }
 
 bool BreakpointSite::IsBreakpointAtThisSite(lldb::break_id_t bp_id) {