1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 
19 #include "jni.h"
20 #include "jvmti.h"
21 
22 #include "android-base/logging.h"
23 #include "android-base/macros.h"
24 
25 // Test infrastructure
26 #include "jvmti_helper.h"
27 #include "test_env.h"
28 
29 namespace art {
30 namespace Test927JNITable {
31 
32 // This test is equivalent to the jni_internal_test JNIEnvExtTableOverride.
33 
34 static size_t gGlobalRefCount = 0;
35 static JNINativeInterface* gOriginalEnv = nullptr;
36 
CountNewGlobalRef(JNIEnv * env,jobject o)37 static jobject CountNewGlobalRef(JNIEnv* env, jobject o) {
38   ++gGlobalRefCount;
39   return gOriginalEnv->NewGlobalRef(env, o);
40 }
41 
DoDeleteGlobalRef(JNIEnv * env,jobject o)42 static void DoDeleteGlobalRef(JNIEnv* env, jobject o) {
43   jclass thr = env->FindClass("java/lang/Thread");
44   CHECK(thr != nullptr);
45   if (env->IsInstanceOf(o, thr)) {
46     jvmtiThreadInfo jti;
47     // b/146170834: This could cause DCHECK failures.
48     CHECK_EQ(jvmti_env->GetThreadInfo(reinterpret_cast<jthread>(o), &jti), JVMTI_ERROR_NONE);
49   }
50   gOriginalEnv->DeleteGlobalRef(env, o);
51 }
52 
Java_art_Test928_doOtherThreadTest(JNIEnv * env,jclass klass)53 extern "C" JNIEXPORT void JNICALL Java_art_Test928_doOtherThreadTest(JNIEnv* env, jclass klass) {
54   size_t start_other_thread_count = gGlobalRefCount;
55   // Make sure it still works even on another thread.
56   jobject global = env->NewGlobalRef(klass);
57   CHECK_EQ(start_other_thread_count + 1, gGlobalRefCount);
58   env->DeleteGlobalRef(global);
59 }
60 
Java_art_Test928_doJNITableTest(JNIEnv * env,jclass klass)61 extern "C" JNIEXPORT void JNICALL Java_art_Test928_doJNITableTest(
62     JNIEnv* env, jclass klass) {
63   // Get the current table, as the delegate.
64   jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv);
65   if (JvmtiErrorToException(env, jvmti_env, getorig_result)) {
66     return;
67   }
68 
69   // Get the current table, as the override we'll install.
70   JNINativeInterface* env_override;
71   jvmtiError getoverride_result = jvmti_env->GetJNIFunctionTable(&env_override);
72   if (JvmtiErrorToException(env, jvmti_env, getoverride_result)) {
73     return;
74   }
75 
76   env_override->NewGlobalRef = CountNewGlobalRef;
77   env_override->DeleteGlobalRef = DoDeleteGlobalRef;
78   gGlobalRefCount = 0;
79 
80   // Install the override.
81   jvmtiError setoverride_result = jvmti_env->SetJNIFunctionTable(env_override);
82   if (JvmtiErrorToException(env, jvmti_env, setoverride_result)) {
83     return;
84   }
85 
86   jobject global = env->NewGlobalRef(klass);
87   CHECK_EQ(1u, gGlobalRefCount);
88   env->DeleteGlobalRef(global);
89 
90   // Try and create and destroy a thread.
91   env->CallStaticVoidMethod(klass, env->GetStaticMethodID(klass, "runThreadTest", "()V"));
92   // Make sure something got ref'd, in the other thread we make and then clear a global ref so that
93   // should at least be present.
94   CHECK_LT(1u, gGlobalRefCount);
95 
96   // Install the "original." There is no real reset.
97   size_t final_global_ref_count = gGlobalRefCount;
98   jvmtiError setoverride2_result = jvmti_env->SetJNIFunctionTable(gOriginalEnv);
99   if (JvmtiErrorToException(env, jvmti_env, setoverride2_result)) {
100     return;
101   }
102 
103   jobject global2 = env->NewGlobalRef(klass);
104   CHECK_EQ(final_global_ref_count, gGlobalRefCount);
105   env->DeleteGlobalRef(global2);
106 
107   // Try to install null. Should return NULL_POINTER error.
108   jvmtiError setoverride3_result = jvmti_env->SetJNIFunctionTable(nullptr);
109   if (setoverride3_result != JVMTI_ERROR_NULL_POINTER) {
110     LOG(FATAL) << "Didn't receive NULL_POINTER";
111   }
112 
113   jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(env_override));
114 }
115 
116 }  // namespace Test927JNITable
117 }  // namespace art
118