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
Java_art_Test928_doJNITableTest(JNIEnv * env,jclass klass)42 extern "C" JNIEXPORT void JNICALL Java_art_Test928_doJNITableTest(
43 JNIEnv* env, jclass klass) {
44 // Get the current table, as the delegate.
45 jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv);
46 if (JvmtiErrorToException(env, jvmti_env, getorig_result)) {
47 return;
48 }
49
50 // Get the current table, as the override we'll install.
51 JNINativeInterface* env_override;
52 jvmtiError getoverride_result = jvmti_env->GetJNIFunctionTable(&env_override);
53 if (JvmtiErrorToException(env, jvmti_env, getoverride_result)) {
54 return;
55 }
56
57 env_override->NewGlobalRef = CountNewGlobalRef;
58 gGlobalRefCount = 0;
59
60 // Install the override.
61 jvmtiError setoverride_result = jvmti_env->SetJNIFunctionTable(env_override);
62 if (JvmtiErrorToException(env, jvmti_env, setoverride_result)) {
63 return;
64 }
65
66 jobject global = env->NewGlobalRef(klass);
67 CHECK_EQ(1u, gGlobalRefCount);
68 env->DeleteGlobalRef(global);
69
70 // Install the "original." There is no real reset.
71 jvmtiError setoverride2_result = jvmti_env->SetJNIFunctionTable(gOriginalEnv);
72 if (JvmtiErrorToException(env, jvmti_env, setoverride2_result)) {
73 return;
74 }
75
76 jobject global2 = env->NewGlobalRef(klass);
77 CHECK_EQ(1u, gGlobalRefCount);
78 env->DeleteGlobalRef(global2);
79
80 // Try to install null. Should return NULL_POINTER error.
81 jvmtiError setoverride3_result = jvmti_env->SetJNIFunctionTable(nullptr);
82 if (setoverride3_result != JVMTI_ERROR_NULL_POINTER) {
83 LOG(FATAL) << "Didn't receive NULL_POINTER";
84 }
85
86 jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(env_override));
87 }
88
89 } // namespace Test927JNITable
90 } // namespace art
91