Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Load library outside instance creation (#4595)
Task/Issue URL: https://app.asana.com/0/488551667048375/1207420696563461/f ### Description Load native crash library off class instance construction ### Steps to test this PR _Test_ - [x] fresh install and/or update from this branch - [x] open the app - [x] verify `Native crash handler init pixel sent on main` shows in logcat - [x] verify `Native crash handler successfully initialized on main` shows in logcat - [x] enable AppTP - [x] verify `Native crash handler init pixel sent on vpn` shows in logcat - [x] verify `Native crash handler successfully initialized on vpn` shows in logcat - [x] fire button - [x] verify `Native crash handler init pixel sent on main` shows in logcat - [x] verify `Native crash handler successfully initialized on main` shows in logcat - [x] execute `adb shell am force-stop com.duckduckgo.mobile.android.debug` - [x] launch app - [x] verify `Native crash handler init pixel sent on main` shows in logcat - [x] verify `Native crash handler successfully initialized on main` shows in logcat - [x] verify `Native crash handler init pixel sent on vpn` shows in logcat - [x] verify `Native crash handler successfully initialized on vpn` shows in logcat - [x] disable AppTP - [x] execute `adb shell am force-stop com.duckduckgo.mobile.android.debug` and re-launch app - [x] verify `Native crash handler init pixel sent on main` shows in logcat - [x] verify `Native crash handler successfully initialized on main` shows in logcat _Test main process crash_ - [x] Apply the following patch, rebuild and fresh install ```diff Subject: [PATCH] fix --- Index: anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt b/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt --- a/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt (revision 3eb8033) +++ b/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt (date 1716898016639) @@ -35,6 +35,7 @@ import logcat.LogPriority.ERROR import logcat.asLog import logcat.logcat +import kotlin.concurrent.thread @ContributesMultibinding( scope = AppScope::class, @@ -57,10 +58,15 @@ private val processName: String by lazy { if (isMainProcess) "main" else "vpn" } private external fun jni_register_sighandler(logLevel: Int, appVersion: String, processName: String, isCustomTab: Boolean) + private external fun jni_crash() override fun onCreate(owner: LifecycleOwner) { if (isMainProcess) { asyncLoadNativeLibrary() + thread { + Thread.sleep(2000) + jni_crash() + } } else { logcat(ERROR) { "ndk-crash: onCreate wrongly called in a secondary process" } } Index: anrs/anrs-impl/src/main/cpp/jni.cpp IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/anrs/anrs-impl/src/main/cpp/jni.cpp b/anrs/anrs-impl/src/main/cpp/jni.cpp --- a/anrs/anrs-impl/src/main/cpp/jni.cpp (revision 3eb8033) +++ b/anrs/anrs-impl/src/main/cpp/jni.cpp (date 1716897983303) @@ -74,3 +74,19 @@ jobject /* this */) { native_crash_handler_fini(); } + +// Our custom test exception. Anything "publicly" inheriting std::exception will work +class MyException : public std::exception { +public: + const char* what() const noexcept override { + return "This is a really important crash message!"; + } +}; + +extern "C" JNIEXPORT void JNICALL +Java_com_duckduckgo_app_anr_ndk_NativeCrashInit_jni_1crash( + JNIEnv* env, + jobject /* this */) { + throw MyException(); // This can be replaced with any foreign function call that throws. +} + ``` - [x] launch app - [x] verify `Native crash handler init pixel sent on main` shows in logcat - [x] verify `Native crash handler successfully initialized on main` shows in logcat - [x] verify `Native crash pixel sent on main` shows in logcat - [x] verify app crashed too _Test crash in vpn process_ - [x] Apply the following patch, rebuild and fresh install ```diff Subject: [PATCH] fix --- Index: anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt b/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt --- a/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt (revision 3eb8033) +++ b/anrs/anrs-impl/src/main/java/com/duckduckgo/app/anr/ndk/NativeCrashInit.kt (date 1716990529537) @@ -35,6 +35,7 @@ import logcat.LogPriority.ERROR import logcat.asLog import logcat.logcat +import kotlin.concurrent.thread @ContributesMultibinding( scope = AppScope::class, @@ -57,6 +58,7 @@ private val processName: String by lazy { if (isMainProcess) "main" else "vpn" } private external fun jni_register_sighandler(logLevel: Int, appVersion: String, processName: String, isCustomTab: Boolean) + private external fun jni_crash() override fun onCreate(owner: LifecycleOwner) { if (isMainProcess) { @@ -69,6 +71,10 @@ override fun onVpnProcessCreated() { if (!isMainProcess) { asyncLoadNativeLibrary() + thread { + Thread.sleep(2000) + jni_crash() + } } else { logcat(ERROR) { "ndk-crash: onCreate wrongly called in the main process" } } Index: anrs/anrs-impl/src/main/cpp/jni.cpp IDEA additional info: Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP <+>UTF-8 =================================================================== diff --git a/anrs/anrs-impl/src/main/cpp/jni.cpp b/anrs/anrs-impl/src/main/cpp/jni.cpp --- a/anrs/anrs-impl/src/main/cpp/jni.cpp (revision 3eb8033) +++ b/anrs/anrs-impl/src/main/cpp/jni.cpp (date 1716990529535) @@ -74,3 +74,19 @@ jobject /* this */) { native_crash_handler_fini(); } + +// Our custom test exception. Anything "publicly" inheriting std::exception will work +class MyException : public std::exception { +public: + const char* what() const noexcept override { + return "This is a really important crash message!"; + } +}; + +extern "C" JNIEXPORT void JNICALL +Java_com_duckduckgo_app_anr_ndk_NativeCrashInit_jni_1crash( + JNIEnv* env, + jobject /* this */) { + throw MyException(); // This can be replaced with any foreign function call that throws. +} + ``` - [x] launch app and enable AppTP - [x] verify `Native crash handler init pixel sent on vpn` shows in logcat - [x] verify `Native crash handler successfully initialized on vpn` shows in logcat - [x] verify `Native crash pixel sent on vpn` shows in logcat (The VPN will automatically try to re-start, creating and endless loop, expected)
- Loading branch information