Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/android/jni_android.h" |
| 6 | |
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 7 | #include <stddef.h> |
erikchen | 011ad3f | 2018-01-26 17:54:55 | [diff] [blame] | 8 | #include <sys/prctl.h> |
avi | b30f240 | 2015-12-24 03:43:28 | [diff] [blame] | 9 | |
Joshua Peraza | 7814da2 | 2018-07-10 21:37:50 | [diff] [blame] | 10 | #include "base/android/java_exception_reporter.h" |
[email protected] | 1b46a53 | 2012-05-23 05:59:49 | [diff] [blame] | 11 | #include "base/android/jni_string.h" |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame] | 12 | #include "base/android/jni_utils.h" |
Andrew Grieve | d03f4ca | 2023-09-20 19:20:54 | [diff] [blame] | 13 | #include "base/android_runtime_jni_headers/Throwable_jni.h" |
Scott Violet | 4416579 | 2018-02-22 02:08:08 | [diff] [blame] | 14 | #include "base/debug/debugging_buildflags.h" |
Lei Zhang | 96f6f27c | 2025-06-05 19:35:53 | [diff] [blame] | 15 | #include "base/debug/stack_trace.h" |
Etienne Dechamps | 8af082f8 | 2023-11-03 14:30:52 | [diff] [blame] | 16 | #include "base/feature_list.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 17 | #include "base/logging.h" |
Andrew Grieve | 4ad46dc | 2024-06-20 19:22:09 | [diff] [blame] | 18 | #include "base/strings/string_util.h" |
Andrew Grieve | 75cd62b | 2022-10-18 20:39:50 | [diff] [blame] | 19 | #include "build/build_config.h" |
Andrew Grieve | 2f99f287 | 2024-02-07 17:41:56 | [diff] [blame] | 20 | #include "build/robolectric_buildflags.h" |
Sam Maier | 8509cc6 | 2024-02-14 21:57:14 | [diff] [blame] | 21 | #include "third_party/jni_zero/jni_zero.h" |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 22 | |
Andrew Grieve | 70a2ba2 | 2025-02-04 15:51:03 | [diff] [blame] | 23 | // Must come after all headers that specialize FromJniType() / ToJniType(). |
Mohannad Farrag | f1d0892 | 2025-04-16 14:52:36 | [diff] [blame] | 24 | #include "base/jni_android_jni/JniAndroid_jni.h" |
Kyle Farnung | 7d4a9995 | 2024-03-26 21:15:16 | [diff] [blame] | 25 | |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame] | 26 | namespace base { |
| 27 | namespace android { |
[email protected] | 61c86c6 | 2011-08-02 16:11:16 | [diff] [blame] | 28 | namespace { |
[email protected] | fe0f1ab | 2012-02-09 21:02:27 | [diff] [blame] | 29 | |
Etienne Dechamps | 8af082f8 | 2023-11-03 14:30:52 | [diff] [blame] | 30 | // If disabled, we LOG(FATAL) immediately in native code when faced with an |
| 31 | // uncaught Java exception (historical behavior). If enabled, we give the Java |
| 32 | // uncaught exception handler a chance to handle the exception first, so that |
| 33 | // the crash is (hopefully) seen as a Java crash, not a native crash. |
Alison Gale | 81f4f2c7 | 2024-04-22 19:33:31 | [diff] [blame] | 34 | // TODO(crbug.com/40261529): remove this switch once we are confident the |
Etienne Dechamps | 8af082f8 | 2023-11-03 14:30:52 | [diff] [blame] | 35 | // new behavior is fine. |
| 36 | BASE_FEATURE(kHandleExceptionsInJava, |
| 37 | "HandleJniExceptionsInJava", |
| 38 | base::FEATURE_ENABLED_BY_DEFAULT); |
| 39 | |
Andrew Grieve | 0e0938a7 | 2023-11-28 14:58:27 | [diff] [blame] | 40 | jclass g_out_of_memory_error_class = nullptr; |
[email protected] | 96e7ade | 2011-12-05 14:42:08 | [diff] [blame] | 41 | |
Andrew Grieve | 2f99f287 | 2024-02-07 17:41:56 | [diff] [blame] | 42 | #if !BUILDFLAG(IS_ROBOLECTRIC) |
| 43 | jmethodID g_class_loader_load_class_method_id = nullptr; |
| 44 | // ClassLoader.loadClass() accepts either slashes or dots on Android, but JVM |
| 45 | // requires dots. We could translate, but there is no need to go through |
| 46 | // ClassLoaders in Robolectric anyways. |
| 47 | // https://cs.android.com/search?q=symbol:DexFile_defineClassNative |
Sam Maier | 06caed5 | 2024-01-25 17:10:46 | [diff] [blame] | 48 | jclass GetClassFromSplit(JNIEnv* env, |
| 49 | const char* class_name, |
| 50 | const char* split_name) { |
Andrew Grieve | 4ad46dc | 2024-06-20 19:22:09 | [diff] [blame] | 51 | DCHECK(IsStringASCII(class_name)); |
| 52 | ScopedJavaLocalRef<jstring> j_class_name(env, env->NewStringUTF(class_name)); |
Sam Maier | 06caed5 | 2024-01-25 17:10:46 | [diff] [blame] | 53 | return static_cast<jclass>(env->CallObjectMethod( |
| 54 | GetSplitClassLoader(env, split_name), g_class_loader_load_class_method_id, |
Andrew Grieve | 4ad46dc | 2024-06-20 19:22:09 | [diff] [blame] | 55 | j_class_name.obj())); |
Clark DuVall | c131c50 | 2020-11-26 16:23:49 | [diff] [blame] | 56 | } |
|
|