Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame^] | 20 | #include "jvmti.h" |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 21 | |
| 22 | #include "base/logging.h" |
| 23 | #include "base/macros.h" |
| 24 | |
| 25 | #include "ti-agent/common_helper.h" |
| 26 | #include "ti-agent/common_load.h" |
| 27 | |
| 28 | namespace art { |
| 29 | namespace Test927JNITable { |
| 30 | |
| 31 | // This test is equivalent to the jni_internal_test JNIEnvExtTableOverride. |
| 32 | |
| 33 | static size_t gGlobalRefCount = 0; |
| 34 | static JNINativeInterface* gOriginalEnv = nullptr; |
| 35 | |
| 36 | static jobject CountNewGlobalRef(JNIEnv* env, jobject o) { |
| 37 | ++gGlobalRefCount; |
| 38 | return gOriginalEnv->NewGlobalRef(env, o); |
| 39 | } |
| 40 | |
| 41 | extern "C" JNIEXPORT void JNICALL Java_Main_doJNITableTest( |
| 42 | JNIEnv* env, jclass klass) { |
| 43 | // Get the current table, as the delegate. |
| 44 | jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv); |
| 45 | if (JvmtiErrorToException(env, getorig_result)) { |
| 46 | return; |
| 47 | } |
| 48 | |
| 49 | // Get the current table, as the override we'll install. |
| 50 | JNINativeInterface* env_override; |
| 51 | jvmtiError getoverride_result = jvmti_env->GetJNIFunctionTable(&env_override); |
| 52 | if (JvmtiErrorToException(env, getoverride_result)) { |
| 53 | return; |
| 54 | } |
| 55 | |
| 56 | env_override->NewGlobalRef = CountNewGlobalRef; |
| 57 | gGlobalRefCount = 0; |
| 58 | |
| 59 | // Install the override. |
| 60 | jvmtiError setoverride_result = jvmti_env->SetJNIFunctionTable(env_override); |
| 61 | if (JvmtiErrorToException(env, setoverride_result)) { |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | jobject global = env->NewGlobalRef(klass); |
| 66 | CHECK_EQ(1u, gGlobalRefCount); |
| 67 | env->DeleteGlobalRef(global); |
| 68 | |
| 69 | // Install the "original." There is no real reset. |
| 70 | jvmtiError setoverride2_result = jvmti_env->SetJNIFunctionTable(gOriginalEnv); |
| 71 | if (JvmtiErrorToException(env, setoverride2_result)) { |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | jobject global2 = env->NewGlobalRef(klass); |
| 76 | CHECK_EQ(1u, gGlobalRefCount); |
| 77 | env->DeleteGlobalRef(global2); |
| 78 | |
| 79 | // Try to install null. Should return NULL_POINTER error. |
| 80 | jvmtiError setoverride3_result = jvmti_env->SetJNIFunctionTable(nullptr); |
| 81 | if (setoverride3_result != JVMTI_ERROR_NULL_POINTER) { |
| 82 | LOG(FATAL) << "Didn't receive NULL_POINTER"; |
| 83 | } |
| 84 | |
| 85 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(env_override)); |
| 86 | } |
| 87 | |
| 88 | } // namespace Test927JNITable |
| 89 | } // namespace art |