webrtc/sdk/android/native_unittests/stacktrace/stacktrace_unittest.cc
Mirko Bonadei f5ea3b96cd Roll chromium_revision 867b87eb29..b56d8868f3 (933713:941610)
Change log: 867b87eb29..b56d8868f3
Full diff: 867b87eb29..b56d8868f3

Changed dependencies
* src/base: 14c89123e6..b367343820
* src/build: a0368daa25..d134d68f00
* src/buildtools: aa2fb0187c..b138e6ce86
* src/buildtools/linux64: git_revision:693f9fb87e4febdd4299db9f73d8d2c958e63148..git_revision:90294ccdcf9334ed25a76ac9b67689468e506342
* src/buildtools/mac: git_revision:693f9fb87e4febdd4299db9f73d8d2c958e63148..git_revision:90294ccdcf9334ed25a76ac9b67689468e506342
* src/buildtools/third_party/libc++abi/trunk: 4c834abe6f..707d75f53e
* src/buildtools/third_party/libunwind/trunk: 10f4e4a5b5..51ffc5ed5c
* src/buildtools/win: git_revision:693f9fb87e4febdd4299db9f73d8d2c958e63148..git_revision:90294ccdcf9334ed25a76ac9b67689468e506342
* src/ios: eb1001b206..af72ede971
* src/testing: 650a2f9467..619bca0ef9
* src/third_party: bc52b47596..5f94f37f8c
* src/third_party/android_build_tools/aapt2: oJ_fhfmT6sLorimH6Eo6hwWS2R2gU0c4ZUQfXvzae1UC..GlCdqxHxlg-8YkDGgj5cie-6COsEAZga9jyq-LAYxY4C
* src/third_party/android_build_tools/bundletool: OZ4is33usuZy-deP3FpWCtbKXaIY0qBLsElWte7556YC..2ZcLVDxyRwp8FzpeYLtLT0TfSRweZxvwh1-Kx1jZ_FoC
* src/third_party/android_deps/libs/com_google_android_material_material: version:2@1.5.0-alpha02.cr0..version:2@1.5.0-alpha05.cr0
* src/third_party/android_deps/libs/com_google_errorprone_error_prone_annotation: version:2@2.9.0.cr0..version:2@2.10.0.cr0
* src/third_party/android_deps/libs/com_google_errorprone_error_prone_annotations: version:2@2.9.0.cr0..version:2@2.10.0.cr0
* src/third_party/android_deps/libs/com_google_errorprone_error_prone_check_api: version:2@2.9.0.cr0..version:2@2.10.0.cr0
* src/third_party/android_deps/libs/com_google_errorprone_error_prone_core: version:2@2.9.0.cr0..version:2@2.10.0.cr0
* src/third_party/android_deps/libs/com_google_errorprone_error_prone_type_annotations: version:2@2.9.0.cr0..version:2@2.10.0.cr0
* src/third_party/androidx: Dewv4glpYqPtnvaOzHgVF6074yDsYzzDFiv0dO1ijwgC..nCX115noKlFOaRMhmFKd1v01hnVpUAaKg2SyLFZaYZgC
* src/third_party/boringssl/src: https://boringssl.googlesource.com/boringssl.git/+log/45c8be91f3..69030a0cea
* src/third_party/breakpad/breakpad: ff5892c5da..ee2ad61263
* src/third_party/catapult: https://chromium.googlesource.com/catapult.git/+log/2d036344bd..a7ad5b5c96
* src/third_party/depot_tools: 5cffc195c9..9fcfde2499
* src/third_party/freetype/src: fde91ab8f1..d31bafcb9c
* src/third_party/googletest/src: 16f637fbf4..1b18723e87
* src/third_party/icu: eedbaf76e4..3e05d9daa9
* src/third_party/libvpx/source/libvpx: 7aabd69682..e259e6951d
* src/third_party/perfetto: 844b8662e9..5ed467a290
* src/third_party/r8: EU82Aqeu2B1PZzAmebHXfyOgeL2UZifcIJ1LPyoThw0C..nqWomZTwNDoogX26WeCSoFGg6aQN1FrwzoU4hCS0duEC
* src/third_party/turbine: 6QYCyqU5yXkAT7T-_K3sCPvfISI8ACXF_cW4OM5v9BgC..KbLQUR_KFiUEzVt1lMTORY96bz_PbMwC3GwIb1oGJmAC
* src/tools: f4333a2a39..a182115705
* src/tools/luci-go: git_revision:d1c03082ecda0148d8096f1fd8bf5491eafc7323..git_revision:0e76392e6557cc3ff8d95c3bc012540e0dbc128a
* src/tools/luci-go: git_revision:d1c03082ecda0148d8096f1fd8bf5491eafc7323..git_revision:0e76392e6557cc3ff8d95c3bc012540e0dbc128a
Added dependency
* src/third_party/android_deps/libs/com_google_flatbuffers_flatbuffers_java
DEPS diff: 867b87eb29..b56d8868f3/DEPS

Clang version changed llvmorg-14-init-6722-g0fbd3aad:llvmorg-14-init-8564-g34b903d8
Details: 867b87eb29..b56d8868f3/tools/clang/scripts/update.py

TBR=chromium-webrtc-autoroll@webrtc-ci.iam.gserviceaccount.com,marpan@webrtc.org, jianj@chromium.org,
BUG=None

No-Presubmit: True
Change-Id: I721d681c6c0a687c782fa1f34ea6cea3fba6ce02
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/237761
Reviewed-by: Xavier Lepaul‎ <xalep@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Autoroller <chromium-webrtc-autoroll@webrtc-ci.iam.gserviceaccount.com>
Reviewed-by: Artem Titov <titovartem@webrtc.org>
Commit-Queue: Mirko Bonadei <mbonadei@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#35347}
2021-11-15 17:34:08 +00:00

289 lines
8.9 KiB
C++

/*
* Copyright 2019 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "sdk/android/native_api/stacktrace/stacktrace.h"
#include <dlfcn.h>
#include <atomic>
#include <memory>
#include <vector>
#include "rtc_base/event.h"
#include "rtc_base/logging.h"
#include "rtc_base/platform_thread.h"
#include "rtc_base/string_utils.h"
#include "rtc_base/strings/string_builder.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/system/inline.h"
#include "system_wrappers/include/sleep.h"
#include "test/gtest.h"
namespace webrtc {
namespace test {
namespace {
// A simple atomic spin event. Implemented with std::atomic_flag, since the C++
// standard guarantees that that type is implemented with actual atomic
// instructions (as opposed to e.g. with a mutex). Uses sequentially consistent
// memory order since this is a test, where simplicity trumps performance.
class SimpleSpinEvent {
public:
// Initialize the event to its blocked state.
SimpleSpinEvent() {
static_cast<void>(blocked_.test_and_set(std::memory_order_seq_cst));
}
// Busy-wait for the event to become unblocked, and block it behind us as we
// leave.
void Wait() {
bool was_blocked;
do {
// Check if the event was blocked, and set it to blocked.
was_blocked = blocked_.test_and_set(std::memory_order_seq_cst);
} while (was_blocked);
}
// Unblock the event.
void Set() { blocked_.clear(std::memory_order_seq_cst); }
private:
std::atomic_flag blocked_;
};
// Returns the execution address relative to the .so base address. This matches
// the addresses we get from GetStacktrace().
RTC_NO_INLINE uint32_t GetCurrentRelativeExecutionAddress() {
void* pc = __builtin_return_address(0);
Dl_info dl_info = {};
const bool success = dladdr(pc, &dl_info);
EXPECT_TRUE(success);
return static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pc) -
reinterpret_cast<uintptr_t>(dl_info.dli_fbase));
}
// Returns true if any of the stack trace element is inside the specified
// region.
bool StackTraceContainsRange(const std::vector<StackTraceElement>& stack_trace,
uintptr_t pc_low,
uintptr_t pc_high) {
for (const StackTraceElement& stack_trace_element : stack_trace) {
if (pc_low <= stack_trace_element.relative_address &&
pc_high >= stack_trace_element.relative_address) {
return true;
}
}
return false;
}
class DeadlockInterface {
public:
virtual ~DeadlockInterface() {}
// This function should deadlock until Release() is called.
virtual void Deadlock() = 0;
// This function should release the thread stuck in Deadlock().
virtual void Release() = 0;
};
struct ThreadParams {
volatile int tid;
// Signaled when the deadlock region is entered.
SimpleSpinEvent deadlock_start_event;
DeadlockInterface* volatile deadlock_impl;
// Defines an address range within the deadlock will occur.
volatile uint32_t deadlock_region_start_address;
volatile uint32_t deadlock_region_end_address;
// Signaled when the deadlock is done.
rtc::Event deadlock_done_event;
};
class RtcEventDeadlock : public DeadlockInterface {
private:
void Deadlock() override { event.Wait(rtc::Event::kForever); }
void Release() override { event.Set(); }
rtc::Event event;
};
class RtcCriticalSectionDeadlock : public DeadlockInterface {
public:
RtcCriticalSectionDeadlock()
: mutex_lock_(std::make_unique<MutexLock>(&mutex_)) {}
private:
void Deadlock() override { MutexLock lock(&mutex_); }
void Release() override { mutex_lock_.reset(); }
Mutex mutex_;
std::unique_ptr<MutexLock> mutex_lock_;
};
class SpinDeadlock : public DeadlockInterface {
public:
SpinDeadlock() : is_deadlocked_(true) {}
private:
void Deadlock() override {
while (is_deadlocked_) {
}
}
void Release() override { is_deadlocked_ = false; }
std::atomic<bool> is_deadlocked_;
};
class SleepDeadlock : public DeadlockInterface {
private:
void Deadlock() override { sleep(1000000); }
void Release() override {
// The interrupt itself will break free from the sleep.
}
};
void TestStacktrace(std::unique_ptr<DeadlockInterface> deadlock_impl) {
// Set params that will be sent to other thread.
ThreadParams params;
params.deadlock_impl = deadlock_impl.get();
// Spawn thread.
auto thread = rtc::PlatformThread::SpawnJoinable(
[&params] {
params.tid = gettid();
params.deadlock_region_start_address =
GetCurrentRelativeExecutionAddress();
params.deadlock_start_event.Set();
params.deadlock_impl->Deadlock();
params.deadlock_region_end_address =
GetCurrentRelativeExecutionAddress();
params.deadlock_done_event.Set();
},
"StacktraceTest");
// Wait until the thread has entered the deadlock region, and take a very
// brief nap to give it time to reach the actual deadlock.
params.deadlock_start_event.Wait();
SleepMs(1);
// Acquire the stack trace of the thread which should now be deadlocking.
std::vector<StackTraceElement> stack_trace = GetStackTrace(params.tid);
// Release the deadlock so that the thread can continue.
deadlock_impl->Release();
// Wait until the thread has left the deadlock.
params.deadlock_done_event.Wait(rtc::Event::kForever);
// Assert that the stack trace contains the deadlock region.
EXPECT_TRUE(StackTraceContainsRange(stack_trace,
params.deadlock_region_start_address,
params.deadlock_region_end_address))
<< "Deadlock region: ["
<< rtc::ToHex(params.deadlock_region_start_address) << ", "
<< rtc::ToHex(params.deadlock_region_end_address)
<< "] not contained in: " << StackTraceToString(stack_trace);
}
class LookoutLogSink final : public rtc::LogSink {
public:
explicit LookoutLogSink(std::string look_for)
: look_for_(std::move(look_for)) {}
void OnLogMessage(const std::string& message) override {
if (message.find(look_for_) != std::string::npos) {
when_found_.Set();
}
}
rtc::Event& WhenFound() { return when_found_; }
private:
const std::string look_for_;
rtc::Event when_found_;
};
} // namespace
TEST(Stacktrace, TestCurrentThread) {
const uint32_t start_addr = GetCurrentRelativeExecutionAddress();
const std::vector<StackTraceElement> stack_trace = GetStackTrace();
const uint32_t end_addr = GetCurrentRelativeExecutionAddress();
EXPECT_TRUE(StackTraceContainsRange(stack_trace, start_addr, end_addr))
<< "Caller region: [" << rtc::ToHex(start_addr) << ", "
<< rtc::ToHex(end_addr)
<< "] not contained in: " << StackTraceToString(stack_trace);
}
// TODO(bugs.webrtc.org/13383): Re-enable once stack unwinding with
// compiler-rt/libunwind works on Android arm64.
#ifdef WEBRTC_ARCH_ARM64
#define MAYBE_TestSpinLock DISABLED_TestSpinLock
#else
#define MAYBE_TestSpinLock TestSpinLock
#endif
TEST(Stacktrace, MAYBE_TestSpinLock) {
TestStacktrace(std::make_unique<SpinDeadlock>());
}
// TODO(bugs.webrtc.org/13383): Re-enable once stack unwinding with
// compiler-rt/libunwind works on Android arm64.
#ifdef WEBRTC_ARCH_ARM64
#define MAYBE_TestSleep DISABLED_TestSleep
#else
#define MAYBE_TestSleep TestSleep
#endif
TEST(Stacktrace, MAYBE_TestSleep) {
TestStacktrace(std::make_unique<SleepDeadlock>());
}
// Stack traces originating from kernel space does not include user space stack
// traces for ARM 32.
#ifdef WEBRTC_ARCH_ARM64
// TODO(bugs.webrtc.org/13383): Re-enable once stack unwinding with
// compiler-rt/libunwind works on Android arm64.
TEST(Stacktrace, DISABLED_TestRtcEvent) {
TestStacktrace(std::make_unique<RtcEventDeadlock>());
}
// TODO(bugs.webrtc.org/13383): Re-enable once stack unwinding with
// compiler-rt/libunwind works on Android arm64.
TEST(Stacktrace, DISABLED_TestRtcCriticalSection) {
TestStacktrace(std::make_unique<RtcCriticalSectionDeadlock>());
}
#endif
TEST(Stacktrace, TestRtcEventDeadlockDetection) {
// Start looking for the expected log output.
LookoutLogSink sink(/*look_for=*/"Probable deadlock");
rtc::LogMessage::AddLogToStream(&sink, rtc::LS_WARNING);
// Start a thread that waits for an event.
rtc::Event ev;
auto thread = rtc::PlatformThread::SpawnJoinable(
[&ev] { ev.Wait(rtc::Event::kForever); },
"TestRtcEventDeadlockDetection");
// The message should appear after 3 sec. We'll wait up to 10 sec in an
// attempt to not be flaky.
EXPECT_TRUE(sink.WhenFound().Wait(10000));
// Unblock the thread and shut it down.
ev.Set();
thread.Finalize();
rtc::LogMessage::RemoveLogToStream(&sink);
}
} // namespace test
} // namespace webrtc