Allow multiple threads sharing a map to unwind.
Add a mutex in MapInfo, and a mutex in Elf. Lock the creation of an Elf
file using the MapInfo mutex, and lock when calling Step, GetFunctionName,
or GetSoname since they can modify information in the object. It might
be beneficial to use a fine grained lock in the future.
Change the Maps object to contain a vector of MapInfo pointers rather
than the total objects. This avoids copying this data around.
Add a test to libbacktrace to verify that sharing a map while doing
unwinds in different threads works.
Add concurrency tests in libunwindstack to verify the locking works.
Add always inline to the RegsGetLocal arm and aarch64 functions. I had
a case where clang did not inline the code, so make sure this is specified.
Bug: 68813077
Test: New unit tests to cover the case. Passes all unit tests.
Test: Ran a monkey test while dumping bugreports and verified that
Test: no crashes in libunwind.
Test: Remove the locking and verified that all of the concurrenty tests fail.
Change-Id: I769e728c676f6bdae9e64ce4cdc03b6749beae03
diff --git a/libunwindstack/tests/UnwindTest.cpp b/libunwindstack/tests/UnwindTest.cpp
index 9f9ca8b..66c8ba6 100644
--- a/libunwindstack/tests/UnwindTest.cpp
+++ b/libunwindstack/tests/UnwindTest.cpp
@@ -311,4 +311,39 @@
RemoteThroughSignal(SIGSEGV, SA_SIGINFO);
}
+// Verify that using the same map while unwinding multiple threads at the
+// same time doesn't cause problems.
+TEST_F(UnwindTest, multiple_threads_unwind_same_map) {
+ static constexpr size_t kNumConcurrentThreads = 100;
+
+ LocalMaps maps;
+ ASSERT_TRUE(maps.Parse());
+ auto process_memory(Memory::CreateProcessMemory(getpid()));
+
+ std::vector<std::thread*> threads;
+
+ std::atomic_bool wait;
+ wait = true;
+ size_t frames[kNumConcurrentThreads];
+ for (size_t i = 0; i < kNumConcurrentThreads; i++) {
+ std::thread* thread = new std::thread([i, &frames, &maps, &process_memory, &wait]() {
+ while (wait)
+ ;
+ std::unique_ptr<Regs> regs(Regs::CreateFromLocal());
+ RegsGetLocal(regs.get());
+
+ Unwinder unwinder(512, &maps, regs.get(), process_memory);
+ unwinder.Unwind();
+ frames[i] = unwinder.NumFrames();
+ ASSERT_LE(3U, frames[i]) << "Failed for thread " << i;
+ });
+ threads.push_back(thread);
+ }
+ wait = false;
+ for (auto thread : threads) {
+ thread->join();
+ delete thread;
+ }
+}
+
} // namespace unwindstack