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 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame] | 22 | #include "android-base/logging.h" |
| 23 | #include "android-base/macros.h" |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 24 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 25 | // Test infrastructure |
| 26 | #include "jvmti_helper.h" |
| 27 | #include "test_env.h" |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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 | |
| 37 | static jobject CountNewGlobalRef(JNIEnv* env, jobject o) { |
| 38 | ++gGlobalRefCount; |
| 39 | return gOriginalEnv->NewGlobalRef(env, o); |
| 40 | } |
| 41 | |
Andreas Gampe | 4665167 | 2017-04-07 09:00:04 -0700 | [diff] [blame] | 42 | extern "C" JNIEXPORT void JNICALL Java_art_Test928_doJNITableTest( |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 43 | JNIEnv* env, jclass klass) { |
| 44 | // Get the current table, as the delegate. |
| 45 | jvmtiError getorig_result = jvmti_env->GetJNIFunctionTable(&gOriginalEnv); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 46 | if (JvmtiErrorToException(env, jvmti_env, getorig_result)) { |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 53 | if (JvmtiErrorToException(env, jvmti_env, getoverride_result)) { |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 62 | if (JvmtiErrorToException(env, jvmti_env, setoverride_result)) { |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 72 | if (JvmtiErrorToException(env, jvmti_env, setoverride2_result)) { |
Andreas Gampe | 6f8e4f0 | 2017-01-16 18:18:14 -0800 | [diff] [blame] | 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 |