blob: a885fda4b0a5a356d2f65d8308fe35337f8a75c6 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2012 The Chromium Authors
[email protected]61c86c62011-08-02 16:11:162// 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
avib30f2402015-12-24 03:43:287#include <stddef.h>
erikchen011ad3f2018-01-26 17:54:558#include <sys/prctl.h>
avib30f2402015-12-24 03:43:289
Joshua Peraza7814da22018-07-10 21:37:5010#include "base/android/java_exception_reporter.h"
[email protected]1b46a532012-05-23 05:59:4911#include "base/android/jni_string.h"
Clark DuVallc131c502020-11-26 16:23:4912#include "base/android/jni_utils.h"
Andrew Grieved03f4ca2023-09-20 19:20:5413#include "base/android_runtime_jni_headers/Throwable_jni.h"
Scott Violet44165792018-02-22 02:08:0814#include "base/debug/debugging_buildflags.h"
Lei Zhang96f6f27c2025-06-05 19:35:5315#include "base/debug/stack_trace.h"
Etienne Dechamps8af082f82023-11-03 14:30:5216#include "base/feature_list.h"
[email protected]61c86c62011-08-02 16:11:1617#include "base/logging.h"
Andrew Grieve4ad46dc2024-06-20 19:22:0918#include "base/strings/string_util.h"
Andrew Grieve75cd62b2022-10-18 20:39:5019#include "build/build_config.h"
Andrew Grieve2f99f2872024-02-07 17:41:5620#include "build/robolectric_buildflags.h"
Sam Maier8509cc62024-02-14 21:57:1421#include "third_party/jni_zero/jni_zero.h"
[email protected]61c86c62011-08-02 16:11:1622
Andrew Grieve70a2ba22025-02-04 15:51:0323// Must come after all headers that specialize FromJniType() / ToJniType().
Mohannad Farragf1d08922025-04-16 14:52:3624#include "base/jni_android_jni/JniAndroid_jni.h"
Kyle Farnung7d4a99952024-03-26 21:15:1625
Clark DuVallc131c502020-11-26 16:23:4926namespace base {
27namespace android {
[email protected]61c86c62011-08-02 16:11:1628namespace {
[email protected]fe0f1ab2012-02-09 21:02:2729
Etienne Dechamps8af082f82023-11-03 14:30:5230// 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 Gale81f4f2c72024-04-22 19:33:3134// TODO(crbug.com/40261529): remove this switch once we are confident the
Etienne Dechamps8af082f82023-11-03 14:30:5235// new behavior is fine.
36BASE_FEATURE(kHandleExceptionsInJava,
37 "HandleJniExceptionsInJava",
38 base::FEATURE_ENABLED_BY_DEFAULT);
39
Andrew Grieve0e0938a72023-11-28 14:58:2740jclass g_out_of_memory_error_class = nullptr;
[email protected]96e7ade2011-12-05 14:42:0841
Andrew Grieve2f99f2872024-02-07 17:41:5642#if !BUILDFLAG(IS_ROBOLECTRIC)
43jmethodID 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 Maier06caed52024-01-25 17:10:4648jclass GetClassFromSplit(JNIEnv* env,
49 const char* class_name,
50 const char* split_name) {
Andrew Grieve4ad46dc2024-06-20 19:22:0951 DCHECK(IsStringASCII(class_name));
52 ScopedJavaLocalRef<jstring> j_class_name(env, env->NewStringUTF(class_name));
Sam Maier06caed52024-01-25 17:10:4653 return static_cast<jclass>(env->CallObjectMethod(
54 GetSplitClassLoader(env, split_name), g_class_loader_load_class_method_id,
Andrew Grieve4ad46dc2024-06-20 19:22:0955 j_class_name.obj()));
Clark DuVallc131c502020-11-26 16:23:4956}