Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid possible crashes in the ANR handler where the local reference table is exhausted #1988

Merged
merged 1 commit into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## TBD

### Bug fixes

* Use PushLocalFrame/PopLocalFrame instead of DeleteLocalRef to avoid creating "holes" in the local ref table that are not always reused, leading to possible crashes in the ANR handler
[]()

## 6.2.0 (2024-02-08)

### Enhancements
Expand Down
25 changes: 18 additions & 7 deletions bugsnag-plugin-android-anr/src/main/jni/anr_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ static void safe_delete_local_ref(JNIEnv *env, jobject obj) {
}
}

static jint safe_push_local_frame(JNIEnv *env, const int size) {
if (env != NULL) {
return (*env)->PushLocalFrame(env, size);
}
return 0;
}

static void safe_pop_local_frame(JNIEnv *env) {
if (env != NULL) {
(*env)->PopLocalFrame(env, NULL);
}
}

// End of duplication

static bool configure_anr_jni_impl(JNIEnv *env) {
Expand Down Expand Up @@ -296,6 +309,10 @@ static void notify_anr_detected() {
return;
}
for (ssize_t i = 0; i < anr_stacktrace_length; i++) {
if (safe_push_local_frame(env, 7) != 0) {
// there is a pending error, so we exit rather than trying to continue
break;
}
bugsnag_stackframe *frame = anr_stacktrace + i;
jobject jmethod = safe_new_string_utf(env, frame->method);
jobject jfilename = safe_new_string_utf(env, frame->filename);
Expand All @@ -314,13 +331,7 @@ static void notify_anr_detected() {
(*env)->CallBooleanMethod(env, jlist, list_add, jframe);
check_and_clear_exc(env);
}
safe_delete_local_ref(env, jmethod);
safe_delete_local_ref(env, jfilename);
safe_delete_local_ref(env, jline_number);
safe_delete_local_ref(env, jframe_address);
safe_delete_local_ref(env, jsymbol_address);
safe_delete_local_ref(env, jload_address);
safe_delete_local_ref(env, jframe);
safe_pop_local_frame(env);
}

(*env)->CallVoidMethod(env, obj_plugin, mthd_notify_anr_detected, jlist);
Expand Down
Loading