blob: 9a8b7fed556b3e3b8812d0ef007a817d664360bf [file] [log] [blame]
Andreas Gampe6f8e4f02017-01-16 18:18:14 -08001/*
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"
Andreas Gampe5e03a302017-03-13 13:10:00 -070020#include "jvmti.h"
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080021
Andreas Gampe027444b2017-03-31 12:49:07 -070022#include "android-base/logging.h"
23#include "android-base/macros.h"
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080024
Andreas Gampe3f46c962017-03-30 10:26:59 -070025// Test infrastructure
26#include "jvmti_helper.h"
27#include "test_env.h"
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080028
29namespace art {
30namespace Test927JNITable {
31
32// This test is equivalent to the jni_internal_test JNIEnvExtTableOverride.
33
34static size_t gGlobalRefCount = 0;
35static JNINativeInterface* gOriginalEnv = nullptr;
36
37static jobject CountNewGlobalRef(JNIEnv* env, jobject o) {
38 ++gGlobalRefCount;
39 return gOriginalEnv->NewGlobalRef(env, o);
40}
41
Andreas Gampe46651672017-04-07 09:00:04 -070042extern "C" JNIEXPORT void JNICALL Java_art_Test928_doJNITableTest(
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080043 JNIEnv* env, jclass klass) {
44 // Get the current table, as the delegate.
45 jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv);
Andreas Gampe3f46c962017-03-30 10:26:59 -070046 if (JvmtiErrorToException(env, jvmti_env, getorig_result)) {
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080047 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);
Andreas Gampe3f46c962017-03-30 10:26:59 -070053 if (JvmtiErrorToException(env, jvmti_env, getoverride_result)) {
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080054 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);
Andreas Gampe3f46c962017-03-30 10:26:59 -070062 if (JvmtiErrorToException(env, jvmti_env, setoverride_result)) {
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080063 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);
Andreas Gampe3f46c962017-03-30 10:26:59 -070072 if (JvmtiErrorToException(env, jvmti_env, setoverride2_result)) {
Andreas Gampe6f8e4f02017-01-16 18:18:14 -080073 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