Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(76)

Side by Side Diff: base/android/jni_android.cc

Issue 86013003: android: Add CHECK() calls to better understand memory corruption. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/android/jni_android.h" 5 #include "base/android/jni_android.h"
6 6
7 #include <map> 7 #include <map>
8 8
9 #include "base/android/build_info.h" 9 #include "base/android/build_info.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/lazy_instance.h" 11 #include "base/lazy_instance.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 13
14 namespace { 14 namespace {
15 using base::android::GetClass; 15 using base::android::GetClass;
16 using base::android::MethodID; 16 using base::android::MethodID;
17 using base::android::ScopedJavaLocalRef; 17 using base::android::ScopedJavaLocalRef;
18 18
19 JavaVM* g_jvm = NULL; 19 JavaVM* g_jvm = NULL;
20 const JNIInvokeInterface* g_jvm_functions = NULL;
joth 2013/11/25 17:08:42 Is the layout of JNIInvokeInterface guaranteed to
digit1 2013/11/25 17:34:02 Yes, since the structure is defined in <jni.h>, it
digit1 2013/11/25 17:35:53 Done.
21
20 // Leak the global app context, as it is used from a non-joinable worker thread 22 // Leak the global app context, as it is used from a non-joinable worker thread
21 // that may still be running at shutdown. There is no harm in doing this. 23 // that may still be running at shutdown. There is no harm in doing this.
22 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky 24 base::LazyInstance<base::android::ScopedJavaGlobalRef<jobject> >::Leaky
23 g_application_context = LAZY_INSTANCE_INITIALIZER; 25 g_application_context = LAZY_INSTANCE_INITIALIZER;
24 26
25 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) { 27 std::string GetJavaExceptionInfo(JNIEnv* env, jthrowable java_throwable) {
26 ScopedJavaLocalRef<jclass> throwable_clazz = 28 ScopedJavaLocalRef<jclass> throwable_clazz =
27 GetClass(env, "java/lang/Throwable"); 29 GetClass(env, "java/lang/Throwable");
28 jmethodID throwable_printstacktrace = 30 jmethodID throwable_printstacktrace =
29 MethodID::Get<MethodID::TYPE_INSTANCE>( 31 MethodID::Get<MethodID::TYPE_INSTANCE>(
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
69 } 71 }
70 72
71 } // namespace 73 } // namespace
72 74
73 namespace base { 75 namespace base {
74 namespace android { 76 namespace android {
75 77
76 JNIEnv* AttachCurrentThread() { 78 JNIEnv* AttachCurrentThread() {
77 DCHECK(g_jvm); 79 DCHECK(g_jvm);
78 JNIEnv* env = NULL; 80 JNIEnv* env = NULL;
79 jint ret = g_jvm->AttachCurrentThread(&env, NULL); 81 // See http://crbug.com/322200 for the reasons for these CHECKs.
82 CHECK(g_jvm);
83 CHECK_EQ(g_jvm_functions, g_jvm->functions);
84 jint ret = g_jvm_functions->AttachCurrentThread(g_jvm, &env, NULL);
80 DCHECK_EQ(JNI_OK, ret); 85 DCHECK_EQ(JNI_OK, ret);
81 return env; 86 return env;
82 } 87 }
83 88
84 void DetachFromVM() { 89 void DetachFromVM() {
85 // Ignore the return value, if the thread is not attached, DetachCurrentThread 90 // Ignore the return value, if the thread is not attached, DetachCurrentThread
86 // will fail. But it is ok as the native thread may never be attached. 91 // will fail. But it is ok as the native thread may never be attached.
87 if (g_jvm) 92 if (g_jvm) {
93 // See http://crbug.com/322200 for the reasons for these CHECKs.
94 CHECK(g_jvm);
95 CHECK_EQ(g_jvm_functions, g_jvm->functions);
88 g_jvm->DetachCurrentThread(); 96 g_jvm->DetachCurrentThread();
97 }
89 } 98 }
90 99
91 void InitVM(JavaVM* vm) { 100 void InitVM(JavaVM* vm) {
92 DCHECK(!g_jvm); 101 DCHECK(!g_jvm);
93 g_jvm = vm; 102 g_jvm = vm;
103 g_jvm_functions = vm->functions;
94 } 104 }
95 105
96 bool IsVMInitialized() { 106 bool IsVMInitialized() {
97 return g_jvm != NULL; 107 return g_jvm != NULL;
98 } 108 }
99 109
100 void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) { 110 void InitApplicationContext(JNIEnv* env, const JavaRef<jobject>& context) {
101 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) { 111 if (env->IsSameObject(g_application_context.Get().obj(), context.obj())) {
102 // It's safe to set the context more than once if it's the same context. 112 // It's safe to set the context more than once if it's the same context.
103 return; 113 return;
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 // RVO should avoid any extra copies of the exception string. 209 // RVO should avoid any extra copies of the exception string.
200 base::android::BuildInfo::GetInstance()->set_java_exception_info( 210 base::android::BuildInfo::GetInstance()->set_java_exception_info(
201 GetJavaExceptionInfo(env, java_throwable)); 211 GetJavaExceptionInfo(env, java_throwable));
202 212
203 // Now, feel good about it and die. 213 // Now, feel good about it and die.
204 CHECK(false); 214 CHECK(false);
205 } 215 }
206 216
207 } // namespace android 217 } // namespace android
208 } // namespace base 218 } // namespace base
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698