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 CHECK_EQ(jvmti_env->GetThreadInfo(reinterpret_cast<jthread>(o), &jti), JVMTI_ERROR_NONE);
48 }
49 gOriginalEnv->DeleteGlobalRef(env, o);
50 }
51
Java_art_Test928_doOtherThreadTest(JNIEnv * env,jclass klass)52 extern "C" JNIEXPORT void JNICALL Java_art_Test928_doOtherThreadTest(JNIEnv* env, jclass klass) {
53 size_t start_other_thread_count = gGlobalRefCount;
54 // Make sure it still works even on another thread.
55 jobject global = env->NewGlobalRef(klass);
56 CHECK_EQ(start_other_thread_count + 1, gGlobalRefCount);
57 env->DeleteGlobalRef(global);
58 }
59
Java_art_Test928_doJNITableTest(JNIEnv * env,jclass klass)60 extern "C" JNIEXPORT void JNICALL Java_art_Test928_doJNITableTest(
61 JNIEnv* env, jclass klass) {
62 // Get the current table, as the delegate.
63 jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv);
64 if (JvmtiErrorToException(env, jvmti_env, getorig_result)) {
65 return;
66 }
67
68 // Get the current table, as the override we'll install.
69 JNINativeInterface* env_override;
70 jvmtiError getoverride_result = jvmti_env->GetJNIFunctionTable(&env_override);
71 if (JvmtiErrorToException(env, jvmti_env, getoverride_result)) {
72 return;
73 }
74
75 env_override->NewGlobalRef = CountNewGlobalRef;
76 env_override->DeleteGlobalRef = DoDeleteGlobalRef;
77 gGlobalRefCount = 0;
78
79 // Install the override.
80 jvmtiError setoverride_result = jvmti_env->SetJNIFunctionTable(env_override);
81 if (JvmtiErrorToException(env, jvmti_env, setoverride_result)) {
82 return;
83 }
84
85 jobject global = env->NewGlobalRef(klass);
86 CHECK_EQ(1u, gGlobalRefCount);
87 env->DeleteGlobalRef(global);
88
89 // Try and create and destroy a thread.
90 env->CallStaticVoidMethod(klass, env->GetStaticMethodID(klass, "runThreadTest", "()V"));
91 // Make sure something got ref'd, in the other thread we make and then clear a global ref so that
92 // should at least be present.
93 CHECK_LT(1u, gGlobalRefCount);
94
95 // Install the "original." There is no real reset.
96 size_t final_global_ref_count = gGlobalRefCount;
97 jvmtiError setoverride2_result = jvmti_env->SetJNIFunctionTable(gOriginalEnv);
98 if (JvmtiErrorToException(env, jvmti_env, setoverride2_result)) {
99 return;
100 }
101
102 jobject global2 = env->NewGlobalRef(klass);
103 CHECK_EQ(final_global_ref_count, gGlobalRefCount);
104 env->DeleteGlobalRef(global2);
105
106 // Try to install null. Should return NULL_POINTER error.
107 jvmtiError setoverride3_result = jvmti_env->SetJNIFunctionTable(nullptr);
108 if (setoverride3_result != JVMTI_ERROR_NULL_POINTER) {
109 LOG(FATAL) << "Didn't receive NULL_POINTER";
110 }
111
112 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(env_override));
113 }
114
115 } // namespace Test927JNITable
116 } // namespace art
117