logd: mLastWorstPidOFSystem crash

mLastWorstPidOfSystem is filled with iterator references
that are not from AID_SYSTEM to aid the performance. But
we only clear entries from the list during erase if they
are from AID_SYSTEM. Remove the filter check in erase so
the stale references will be removed.

The conditions that caused this failure are difficult to
reproduce and are rare.

Test: gTests logd-unit-tests, liblog-unit-tests and logcat-unit-tests
Bug: 32247044
Bug: 31237377
Change-Id: Ie405dd643203b816cac15eef5c97600551cee450
(cherry picked from commit fa07f9dc4b4d101a49fba5dbbf35c88cdfec4433)
diff --git a/logd/LogBuffer.cpp b/logd/LogBuffer.cpp
index 5554d53..aff8a46 100644
--- a/logd/LogBuffer.cpp
+++ b/logd/LogBuffer.cpp
@@ -222,6 +222,9 @@
     LogBufferElement *element = *it;
     log_id_t id = element->getLogId();
 
+    // Remove iterator references in the various lists that will become stale
+    // after the element is erased from the main logging list.
+
     {   // start of scope for found iterator
         int key = ((id == LOG_ID_EVENTS) || (id == LOG_ID_SECURITY)) ?
                 element->getTag() : element->getUid();
@@ -231,7 +234,8 @@
         }
     }
 
-    if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY) && (element->getUid() == AID_SYSTEM)) {
+    if ((id != LOG_ID_EVENTS) && (id != LOG_ID_SECURITY)) {
+        // element->getUid() may not be AID_SYSTEM for next-best-watermark.
         // start of scope for pid found iterator
         LogBufferPidIteratorMap::iterator found =
             mLastWorstPidOfSystem[id].find(element->getPid());
@@ -501,7 +505,7 @@
                     it = found->second;
                 }
             }
-            if (worstPid) {
+            if (worstPid) { // Only set if !LOG_ID_EVENTS and !LOG_ID_SECURITY
                 // begin scope for pid worst found iterator
                 LogBufferPidIteratorMap::iterator found
                     = mLastWorstPidOfSystem[id].find(worstPid);
@@ -534,6 +538,7 @@
                 ++it;
                 continue;
             }
+            // below this point element->getLogId() == id
 
             if (leading && (!mLastSet[id] || ((*mLast[id])->getLogId() != id))) {
                 mLast[id] = it;
@@ -590,6 +595,8 @@
                         && ((!gc && (element->getPid() == worstPid))
                            || (mLastWorstPidOfSystem[id].find(element->getPid())
                                 == mLastWorstPidOfSystem[id].end()))) {
+                    // element->getUid() may not be AID_SYSTEM, next best
+                    // watermark if current one empty.
                     mLastWorstPidOfSystem[id][element->getPid()] = it;
                 }
                 if ((!gc && !worstPid && (key == worst))
@@ -607,6 +614,8 @@
                 ++it;
                 continue;
             }
+            // key == worst below here
+            // If worstPid set, then element->getPid() == worstPid below here
 
             pruneRows--;
             if (pruneRows == 0) {
@@ -630,6 +639,8 @@
                     if (worstPid && (!gc
                                 || (mLastWorstPidOfSystem[id].find(worstPid)
                                     == mLastWorstPidOfSystem[id].end()))) {
+                        // element->getUid() may not be AID_SYSTEM, next best
+                        // watermark if current one empty.
                         mLastWorstPidOfSystem[id][worstPid] = it;
                     }
                     if ((!gc && !worstPid) ||