Make memunreachable_test more robust against false negative leaks

For some reason, the memunreachable tests are rock solid on the
devices covered by APCT, but catch a ton of false-negatives on
hikey960, which show up as failures that look like:
system/core/libmemunreachable/tests/MemUnreachable_test.cpp:200: Failure
Expected equality of these values:
  1U
    Which is: 1
  info.leaks.size()
    Which is: 0

These happen when a stray copy of a pointer is lying around that
points to the memory it is expected to leak.  The stray pointers
can be on the stack or in the jemalloc thread cache of freed
allocations, which is always considered active memory.

Add some extra cleanups to get rid of old pointers.
1.  Clear the tcache when destructing UnreachableMemoryInfo
2.  Clear the stack and tcache before and after each test
3.  Make MemunreachbleTest.twice match MemunreachableTest.stack

Also fix MemunreachableTest.notdumpable, which was only passing
when run as root, which was bypassing what the test was trying
to cover.  Make the test pass when run as non-root, and skip
when the test is running as root.

Bug: 79701104
Test: memunreachable_test
Test: memunreachable_test as root
Change-Id: Ia6c6df11e76405d08118afcc19c1fe80a6684c56
Merged-In: Ia6c6df11e76405d08118afcc19c1fe80a6684c56
(cherry picked from commit ca71f170b78e4707dbaa5bf4ddace47a455b4699)
diff --git a/libmemunreachable/MemUnreachable.cpp b/libmemunreachable/MemUnreachable.cpp
index 24fdc7f..529a043 100644
--- a/libmemunreachable/MemUnreachable.cpp
+++ b/libmemunreachable/MemUnreachable.cpp
@@ -495,6 +495,21 @@
   return oss.str();
 }
 
+UnreachableMemoryInfo::~UnreachableMemoryInfo() {
+  // Clear the memory that holds the leaks, otherwise the next attempt to
+  // detect leaks may find the old data (for example in the jemalloc tcache)
+  // and consider all the leaks to be referenced.
+  memset(leaks.data(), 0, leaks.capacity() * sizeof(Leak));
+
+  std::vector<Leak> tmp;
+  leaks.swap(tmp);
+
+  // Disable and re-enable malloc to flush the jemalloc tcache to make sure
+  // there are no copies of the leaked pointer addresses there.
+  malloc_disable();
+  malloc_enable();
+}
+
 std::string GetUnreachableMemoryString(bool log_contents, size_t limit) {
   UnreachableMemoryInfo info;
   if (!GetUnreachableMemory(info, limit)) {