Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [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 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
| 18 | |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame^] | 19 | #include "android-base/macros.h" |
| 20 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 21 | #include "jni.h" |
Andreas Gampe | 5e03a30 | 2017-03-13 13:10:00 -0700 | [diff] [blame] | 22 | #include "jvmti.h" |
Andreas Gampe | 027444b | 2017-03-31 12:49:07 -0700 | [diff] [blame^] | 23 | #include "scoped_local_ref.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 24 | |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 25 | // Test infrastructure |
| 26 | #include "jni_helper.h" |
| 27 | #include "jvmti_helper.h" |
| 28 | #include "test_env.h" |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 29 | |
| 30 | namespace art { |
| 31 | namespace Test910Methods { |
| 32 | |
| 33 | extern "C" JNIEXPORT jobjectArray JNICALL Java_Main_getMethodName( |
| 34 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 35 | jmethodID id = env->FromReflectedMethod(method); |
| 36 | |
| 37 | char* name; |
| 38 | char* sig; |
| 39 | char* gen; |
| 40 | jvmtiError result = jvmti_env->GetMethodName(id, &name, &sig, &gen); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 41 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 42 | return nullptr; |
| 43 | } |
| 44 | |
Andreas Gampe | 336c3c3 | 2016-11-08 17:02:19 -0800 | [diff] [blame] | 45 | auto callback = [&](jint i) { |
| 46 | if (i == 0) { |
| 47 | return name == nullptr ? nullptr : env->NewStringUTF(name); |
| 48 | } else if (i == 1) { |
| 49 | return sig == nullptr ? nullptr : env->NewStringUTF(sig); |
| 50 | } else { |
| 51 | return gen == nullptr ? nullptr : env->NewStringUTF(gen); |
| 52 | } |
| 53 | }; |
| 54 | jobjectArray ret = CreateObjectArray(env, 3, "java/lang/String", callback); |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 55 | |
| 56 | // Need to deallocate the strings. |
| 57 | if (name != nullptr) { |
| 58 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(name)); |
| 59 | } |
| 60 | if (sig != nullptr) { |
| 61 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(sig)); |
| 62 | } |
| 63 | if (gen != nullptr) { |
| 64 | jvmti_env->Deallocate(reinterpret_cast<unsigned char*>(gen)); |
| 65 | } |
| 66 | |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 67 | // Also run GetMethodName with all parameter pointers null to check for segfaults. |
| 68 | jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 69 | if (JvmtiErrorToException(env, jvmti_env, result2)) { |
Andreas Gampe | 862bdd8 | 2016-11-18 13:31:13 -0800 | [diff] [blame] | 70 | return nullptr; |
| 71 | } |
| 72 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 73 | return ret; |
| 74 | } |
| 75 | |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 76 | extern "C" JNIEXPORT jclass JNICALL Java_Main_getMethodDeclaringClass( |
| 77 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 78 | jmethodID id = env->FromReflectedMethod(method); |
| 79 | |
| 80 | jclass declaring_class; |
| 81 | jvmtiError result = jvmti_env->GetMethodDeclaringClass(id, &declaring_class); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 82 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 368a208 | 2016-10-28 17:33:13 -0700 | [diff] [blame] | 83 | return nullptr; |
| 84 | } |
| 85 | |
| 86 | return declaring_class; |
| 87 | } |
| 88 | |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 89 | extern "C" JNIEXPORT jint JNICALL Java_Main_getMethodModifiers( |
| 90 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 91 | jmethodID id = env->FromReflectedMethod(method); |
| 92 | |
| 93 | jint modifiers; |
| 94 | jvmtiError result = jvmti_env->GetMethodModifiers(id, &modifiers); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 95 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | 36bcd4f | 2016-10-28 18:07:18 -0700 | [diff] [blame] | 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | return modifiers; |
| 100 | } |
| 101 | |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 102 | extern "C" JNIEXPORT jint JNICALL Java_Main_getMaxLocals( |
| 103 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 104 | jmethodID id = env->FromReflectedMethod(method); |
| 105 | |
| 106 | jint max_locals; |
| 107 | jvmtiError result = jvmti_env->GetMaxLocals(id, &max_locals); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 108 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 109 | return -1; |
| 110 | } |
| 111 | |
| 112 | return max_locals; |
| 113 | } |
| 114 | |
| 115 | extern "C" JNIEXPORT jint JNICALL Java_Main_getArgumentsSize( |
| 116 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 117 | jmethodID id = env->FromReflectedMethod(method); |
| 118 | |
| 119 | jint arguments; |
| 120 | jvmtiError result = jvmti_env->GetArgumentsSize(id, &arguments); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 121 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 122 | return -1; |
| 123 | } |
| 124 | |
| 125 | return arguments; |
| 126 | } |
| 127 | |
| 128 | extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationStart( |
| 129 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 130 | jmethodID id = env->FromReflectedMethod(method); |
| 131 | |
| 132 | jlong start; |
| 133 | jlong end; |
| 134 | jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 135 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 136 | return -1; |
| 137 | } |
| 138 | |
| 139 | return start; |
| 140 | } |
| 141 | |
| 142 | extern "C" JNIEXPORT jlong JNICALL Java_Main_getMethodLocationEnd( |
| 143 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 144 | jmethodID id = env->FromReflectedMethod(method); |
| 145 | |
| 146 | jlong start; |
| 147 | jlong end; |
| 148 | jvmtiError result = jvmti_env->GetMethodLocation(id, &start, &end); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 149 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | f71832e | 2017-01-09 11:38:04 -0800 | [diff] [blame] | 150 | return -1; |
| 151 | } |
| 152 | |
| 153 | return end; |
| 154 | } |
| 155 | |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 156 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodNative( |
| 157 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 158 | jmethodID id = env->FromReflectedMethod(method); |
| 159 | |
| 160 | jboolean is_native; |
| 161 | jvmtiError result = jvmti_env->IsMethodNative(id, &is_native); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 162 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 163 | return JNI_FALSE; |
| 164 | } |
| 165 | |
| 166 | return is_native; |
| 167 | } |
| 168 | |
| 169 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodObsolete( |
| 170 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 171 | jmethodID id = env->FromReflectedMethod(method); |
| 172 | |
| 173 | jboolean is_obsolete; |
| 174 | jvmtiError result = jvmti_env->IsMethodObsolete(id, &is_obsolete); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 175 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 176 | return JNI_FALSE; |
| 177 | } |
| 178 | |
| 179 | return is_obsolete; |
| 180 | } |
| 181 | |
| 182 | extern "C" JNIEXPORT jboolean JNICALL Java_Main_isMethodSynthetic( |
| 183 | JNIEnv* env, jclass klass ATTRIBUTE_UNUSED, jobject method) { |
| 184 | jmethodID id = env->FromReflectedMethod(method); |
| 185 | |
| 186 | jboolean is_synthetic; |
| 187 | jvmtiError result = jvmti_env->IsMethodSynthetic(id, &is_synthetic); |
Andreas Gampe | 3f46c96 | 2017-03-30 10:26:59 -0700 | [diff] [blame] | 188 | if (JvmtiErrorToException(env, jvmti_env, result)) { |
Andreas Gampe | fdeef52 | 2017-01-09 14:40:25 -0800 | [diff] [blame] | 189 | return JNI_FALSE; |
| 190 | } |
| 191 | |
| 192 | return is_synthetic; |
| 193 | } |
| 194 | |
Andreas Gampe | 3c252f0 | 2016-10-27 18:25:17 -0700 | [diff] [blame] | 195 | } // namespace Test910Methods |
| 196 | } // namespace art |