| 6 |
| 7 | #include <stddef.h> |
| 8 | #include <sys/prctl.h> |
| 9 |
| 10 | #include <map> |
| 11 |
| 12 | #include "base/android/java_exception_reporter.h" |
| 13 | #include "base/android/jni_string.h" |
| 14 | #include "base/debug/debugging_buildflags.h" |
| 15 | #include "base/lazy_instance.h" |
| 16 | #include "base/logging.h" |
| 17 | #include "base/threading/thread_local.h" |
| 18 |
| 19 | namespace { |
| 20 | using base::android::GetClass; |
| 21 | using base::android::MethodID; |
| 22 | using base::android::ScopedJavaLocalRef; |
| 23 |
| 24 | JavaVM* g_jvm = NULL; |
| 25 | base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject>>::Leaky |
| 26 | g_class_loader = LAZY_INSTANCE_INITIALIZER; |
| 27 | jmethodID g_class_loader_load_class_method_id = 0; |
| 28 |
| 29 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 30 | base::LazyInstance<base::ThreadLocalPointer<void>>::Leaky |
| 31 | g_stack_frame_pointer = LAZY_INSTANCE_INITIALIZER; |
| 32 | #endif |
| 33 |
| 34 | bool g_fatal_exception_occurred = false; |
| 35 |
| 36 | } // namespace |
| 37 |
| 38 | namespace base { |
| 39 | namespace android { |
| 40 |
| 41 | JNIEnv* AttachCurrentThread() { |
| 42 | DCHECK(g_jvm); |
| 43 | JNIEnv* env = nullptr; |
| 44 | jint ret = g_jvm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_2); |
| 45 | if (ret == JNI_EDETACHED || !env) { |
| 46 | JavaVMAttachArgs args; |
| 47 | args.version = JNI_VERSION_1_2; |
| 48 | args.group = nullptr; |
| 49 |
| 50 | // 16 is the maximum size for thread names on Android. |
| 51 | char thread_name[16]; |
| 52 | int err = prctl(PR_GET_NAME, thread_name); |
| 53 | if (err < 0) { |
| 54 | DPLOG(ERROR) << "prctl(PR_GET_NAME)"; |
| 55 | args.name = nullptr; |
| 56 | } else { |
| 57 | args.name = thread_name; |
| 58 | } |
| 59 |
| 60 | ret = g_jvm->AttachCurrentThread(&env, &args); |
| 61 | CHECK_EQ(JNI_OK, ret); |
| 62 | } |
| 63 | return env; |
| 64 | } |
| 65 |
| 66 | JNIEnv* AttachCurrentThreadWithName(const std::string& thread_name) { |
| 67 | DCHECK(g_jvm); |
| 68 | JavaVMAttachArgs args; |
| 69 | args.version = JNI_VERSION_1_2; |
| 70 | args.name = thread_name.c_str(); |
| 71 | args.group = NULL; |
| 72 | JNIEnv* env = NULL; |
| 73 | jint ret = g_jvm->AttachCurrentThread(&env, &args); |
| 74 | CHECK_EQ(JNI_OK, ret); |
| 75 | return env; |
| 76 | } |
| 77 |
| 78 | void DetachFromVM() { |
| 79 | // Ignore the return value, if the thread is not attached, DetachCurrentThread |
| 80 | // will fail. But it is ok as the native thread may never be attached. |
| 81 | if (g_jvm) |
| 82 | g_jvm->DetachCurrentThread(); |
| 83 | } |
| 84 |
| 85 | void InitVM(JavaVM* vm) { |
| 86 | DCHECK(!g_jvm || g_jvm == vm); |
| 87 | g_jvm = vm; |
| 88 | } |
| 89 |
| 90 | bool IsVMInitialized() { |
| 91 | return g_jvm != NULL; |
| 92 | } |
| 93 |
| 94 | void InitReplacementClassLoader(JNIEnv* env, |
| 95 | const JavaRef<jobject>& class_loader) { |
| 96 | DCHECK(g_class_loader.Get().is_null()); |
| 97 | DCHECK(!class_loader.is_null()); |
| 98 |
| 99 | ScopedJavaLocalRef<jclass> class_loader_clazz = |
| 100 | GetClass(env, "java/lang/ClassLoader"); |
| 101 | CHECK(!ClearException(env)); |
| 102 | g_class_loader_load_class_method_id = |
| 103 | env->GetMethodID(class_loader_clazz.obj(), |
| 104 | "loadClass", |
| 105 | "(Ljava/lang/String;)Ljava/lang/Class;"); |
| 106 | CHECK(!ClearException(env)); |
| 107 |
| 108 | DCHECK(env->IsInstanceOf(class_loader.obj(), class_loader_clazz.obj())); |
| 109 | g_class_loader.Get().Reset(class_loader); |
| 110 | } |
| 111 |
| 112 | ScopedJavaLocalRef<jclass> GetClass(JNIEnv* env, const char* class_name) { |
| 113 | jclass clazz; |
| 114 | if (!g_class_loader.Get().is_null()) { |
| 115 | // ClassLoader.loadClass expects a classname with components separated by |
| 116 | // dots instead of the slashes that JNIEnv::FindClass expects. The JNI |
| 117 | // generator generates names with slashes, so we have to replace them here. |
| 118 | // TODO(torne): move to an approach where we always use ClassLoader except |
| 119 | // for the special case of base::android::GetClassLoader(), and change the |
| 120 | // JNI generator to generate dot-separated names. http://crbug.com/461773 |
| 121 | size_t bufsize = strlen(class_name) + 1; |
| 122 | char dotted_name[bufsize]; |
| 123 | memmove(dotted_name, class_name, bufsize); |
| 124 | for (size_t i = 0; i < bufsize; ++i) { |
| 125 | if (dotted_name[i] == '/') { |
| 126 | dotted_name[i] = '.'; |
| 127 | } |
| 128 | } |
| 129 |
| 130 | clazz = static_cast<jclass>( |
| 131 | env->CallObjectMethod(g_class_loader.Get().obj(), |
| 132 | g_class_loader_load_class_method_id, |
| 133 | ConvertUTF8ToJavaString(env, dotted_name).obj())); |
| 134 | } else { |
| 135 | clazz = env->FindClass(class_name); |
| 136 | } |
| 137 | if (ClearException(env) || !clazz) { |
| 138 | LOG(FATAL) << "Failed to find class " << class_name; |
| 139 | } |
| 140 | return ScopedJavaLocalRef<jclass>(env, clazz); |
| 141 | } |
| 142 |
| 143 | jclass LazyGetClass( |
| 144 | JNIEnv* env, |
| 145 | const char* class_name, |
| 146 | std::atomic<jclass>* atomic_class_id) { |
| 147 | const jclass value = std::atomic_load(atomic_class_id); |
| 148 | if (value) |
| 149 | return value; |
| 150 | ScopedJavaGlobalRef<jclass> clazz; |
| 151 | clazz.Reset(GetClass(env, class_name)); |
| 152 | jclass cas_result = nullptr; |
| 153 | if (std::atomic_compare_exchange_strong(atomic_class_id, &cas_result, |
| 154 | clazz.obj())) { |
| 155 | // We intentionally leak the global ref since we now storing it as a raw |
| 156 | // pointer in |atomic_class_id|. |
| 157 | return clazz.Release(); |
| 158 | } else { |
| 159 | return cas_result; |
| 160 | } |
| 161 | } |
| 162 |
| 163 | template<MethodID::Type type> |
| 164 | jmethodID MethodID::Get(JNIEnv* env, |
| 165 | jclass clazz, |
| 166 | const char* method_name, |
| 167 | const char* jni_signature) { |
| 168 | auto get_method_ptr = type == MethodID::TYPE_STATIC ? |
| 169 | &JNIEnv::GetStaticMethodID : |
| 170 | &JNIEnv::GetMethodID; |
| 171 | jmethodID id = (env->*get_method_ptr)(clazz, method_name, jni_signature); |
| 172 | if (base::android::ClearException(env) || !id) { |
| 173 | LOG(FATAL) << "Failed to find " << |
| 174 | (type == TYPE_STATIC ? "static " : "") << |
| 175 | "method " << method_name << " " << jni_signature; |
| 176 | } |
| 177 | return id; |
| 178 | } |
| 179 |
| 180 | // If |atomic_method_id| set, it'll return immediately. Otherwise, it'll call |
| 181 | // into ::Get() above. If there's a race, it's ok since the values are the same |
| 182 | // (and the duplicated effort will happen only once). |
| 183 | template<MethodID::Type type> |
| 184 | jmethodID MethodID::LazyGet(JNIEnv* env, |
| 185 | jclass clazz, |
| 186 | const char* method_name, |
| 187 | const char* jni_signature, |
| 188 | std::atomic<jmethodID>* atomic_method_id) { |
| 189 | const jmethodID value = std::atomic_load(atomic_method_id); |
| 190 | if (value) |
| 191 | return value; |
| 192 | jmethodID id = MethodID::Get<type>(env, clazz, method_name, jni_signature); |
| 193 | std::atomic_store(atomic_method_id, id); |
| 194 | return id; |
| 195 | } |
| 196 |
| 197 | // Various template instantiations. |
| 198 | template jmethodID MethodID::Get<MethodID::TYPE_STATIC>( |
| 199 | JNIEnv* env, jclass clazz, const char* method_name, |
| 200 | const char* jni_signature); |
| 201 |
| 202 | template jmethodID MethodID::Get<MethodID::TYPE_INSTANCE>( |
| 203 | JNIEnv* env, jclass clazz, const char* method_name, |
| 204 | const char* jni_signature); |
| 205 |
| 206 | template jmethodID MethodID::LazyGet<MethodID::TYPE_STATIC>( |
| 207 | JNIEnv* env, jclass clazz, const char* method_name, |
| 208 | const char* jni_signature, std::atomic<jmethodID>* atomic_method_id); |
| 209 |
| 210 | template jmethodID MethodID::LazyGet<MethodID::TYPE_INSTANCE>( |
| 211 | JNIEnv* env, jclass clazz, const char* method_name, |
| 212 | const char* jni_signature, std::atomic<jmethodID>* atomic_method_id); |
| 213 |
| 214 | bool HasException(JNIEnv* env) { |
| 215 | return env->ExceptionCheck() != JNI_FALSE; |
| 216 | } |
| 217 |
| 218 | bool ClearException(JNIEnv* env) { |
| 219 | if (!HasException(env)) |
| 220 | return false; |
| 221 | env->ExceptionDescribe(); |
| 222 | env->ExceptionClear(); |
| 223 | return true; |
| 224 | } |
| 225 |
| 226 | void CheckException(JNIEnv* env) { |
| 227 | if (!HasException(env)) |
| 228 | return; |
| 229 |
| 230 | jthrowable java_throwable = env->ExceptionOccurred(); |
| 231 | if (java_throwable) { |
| 232 | // Clear the pending exception, since a local reference is now held. |
| 233 | env->ExceptionDescribe(); |
| 234 | env->ExceptionClear(); |
| 235 |
| 236 | if (g_fatal_exception_occurred) { |
| 237 | // Another exception (probably OOM) occurred during GetJavaExceptionInfo. |
| 238 | base::android::SetJavaException( |
| 239 | "Java OOM'ed in exception handling, check logcat"); |
| 240 | } else { |
| 241 | g_fatal_exception_occurred = true; |
| 242 | // RVO should avoid any extra copies of the exception string. |
| 243 | base::android::SetJavaException( |
| 244 | GetJavaExceptionInfo(env, java_throwable).c_str()); |
| 245 | } |
| 246 | } |
| 247 |
| 248 | // Now, feel good about it and die. |
| 249 | LOG(FATAL) << "Please include Java exception stack in crash report"; |
| 250 | } |
| 251 |
| 252 | std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { |
| 253 | ScopedJavaLocalRef<jclass> log_clazz = GetClass(env, "android/util/Log"); |
| 254 | jmethodID log_getstacktracestring = MethodID::Get<MethodID::TYPE_STATIC>( |
| 255 | env, log_clazz.obj(), "getStackTraceString", |
| 256 | "(Ljava/lang/Throwable;)Ljava/lang/String;"); |
| 257 |
| 258 | // Call Log.getStackTraceString() |
| 259 | ScopedJavaLocalRef<jstring> exception_string( |
| 260 | env, static_cast<jstring>(env->CallStaticObjectMethod( |
| 261 | log_clazz.obj(), log_getstacktracestring, java_throwable))); |
| 262 | CheckException(env); |
| 263 |
| 264 | ScopedJavaLocalRef<jclass> piielider_clazz = |
| 265 | GetClass(env, "org/chromium/base/PiiElider"); |
| 266 | jmethodID piielider_sanitize_stacktrace = |
| 267 | MethodID::Get<MethodID::TYPE_STATIC>( |
| 268 | env, piielider_clazz.obj(), "sanitizeStacktrace", |
| 269 | "(Ljava/lang/String;)Ljava/lang/String;"); |
| 270 | ScopedJavaLocalRef<jstring> sanitized_exception_string( |
| 271 | env, static_cast<jstring>(env->CallStaticObjectMethod( |
| 272 | piielider_clazz.obj(), piielider_sanitize_stacktrace, |
| 273 | exception_string.obj()))); |
| 274 | CheckException(env); |
| 275 |
| 276 | return ConvertJavaStringToUTF8(sanitized_exception_string); |
| 277 | } |
| 278 |
| 279 | #if BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 280 |
| 281 | JNIStackFrameSaver::JNIStackFrameSaver(void* current_fp) { |
| 282 | previous_fp_ = g_stack_frame_pointer.Pointer()->Get(); |
| 283 | g_stack_frame_pointer.Pointer()->Set(current_fp); |
| 284 | } |
| 285 |
| 286 | JNIStackFrameSaver::~JNIStackFrameSaver() { |
| 287 | g_stack_frame_pointer.Pointer()->Set(previous_fp_); |
| 288 | } |
| 289 |
| 290 | void* JNIStackFrameSaver::SavedFrame() { |
| 291 | return g_stack_frame_pointer.Pointer()->Get(); |
| 292 | } |
| 293 |
| 294 | #endif // BUILDFLAG(CAN_UNWIND_WITH_FRAME_POINTERS) |
| 295 |
| 296 | } // namespace android |
| 297 | } // namespace base |
| 298 |