blob: ded4f09df7936b6221b84be217efd7a8e0178480 [file] [log] [blame]
Andreas Gampe3c252f02016-10-27 18:25:17 -07001/*
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 Gampe3c252f02016-10-27 18:25:17 -070017#include <stdio.h>
18
Andreas Gampe027444b2017-03-31 12:49:07 -070019#include "android-base/macros.h"
20
Andreas Gampe3c252f02016-10-27 18:25:17 -070021#include "jni.h"
Andreas Gampe5e03a302017-03-13 13:10:00 -070022#include "jvmti.h"
Andreas Gampe027444b2017-03-31 12:49:07 -070023#include "scoped_local_ref.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070024
Andreas Gampe3f46c962017-03-30 10:26:59 -070025// Test infrastructure
26#include "jni_helper.h"
27#include "jvmti_helper.h"
28#include "test_env.h"
Andreas Gampe3c252f02016-10-27 18:25:17 -070029
30namespace art {
31namespace Test910Methods {
32
33extern "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 Gampe3f46c962017-03-30 10:26:59 -070041 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe3c252f02016-10-27 18:25:17 -070042 return nullptr;
43 }
44
Andreas Gampe336c3c32016-11-08 17:02:19 -080045 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 Gampe3c252f02016-10-27 18:25:17 -070055
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 Gampe862bdd82016-11-18 13:31:13 -080067 // Also run GetMethodName with all parameter pointers null to check for segfaults.
68 jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr);
Andreas Gampe3f46c962017-03-30 10:26:59 -070069 if (JvmtiErrorToException(env, jvmti_env, result2)) {
Andreas Gampe862bdd82016-11-18 13:31:13 -080070 return nullptr;
71 }
72
Andreas Gampe3c252f02016-10-27 18:25:17 -070073 return ret;
74}
75
Andreas Gampe368a2082016-10-28 17:33:13 -070076extern "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 Gampe3f46c962017-03-30 10:26:59 -070082 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe368a2082016-10-28 17:33:13 -070083 return nullptr;
84 }
85
86 return declaring_class;
87}
88
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070089extern "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 Gampe3f46c962017-03-30 10:26:59 -070095 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampe36bcd4f2016-10-28 18:07:18 -070096 return 0;
97 }
98
99 return modifiers;
100}
101
Andreas Gampef71832e2017-01-09 11:38:04 -0800102extern "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 Gampe3f46c962017-03-30 10:26:59 -0700108 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800109 return -1;
110 }
111
112 return max_locals;
113}
114
115extern "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 Gampe3f46c962017-03-30 10:26:59 -0700121 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800122 return -1;
123 }
124
125 return arguments;
126}
127
128extern "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 Gampe3f46c962017-03-30 10:26:59 -0700135 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800136 return -1;
137 }
138
139 return start;
140}
141
142extern "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 Gampe3f46c962017-03-30 10:26:59 -0700149 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampef71832e2017-01-09 11:38:04 -0800150 return -1;
151 }
152
153 return end;
154}
155
Andreas Gampefdeef522017-01-09 14:40:25 -0800156extern "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 Gampe3f46c962017-03-30 10:26:59 -0700162 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800163 return JNI_FALSE;
164 }
165
166 return is_native;
167}
168
169extern "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 Gampe3f46c962017-03-30 10:26:59 -0700175 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800176 return JNI_FALSE;
177 }
178
179 return is_obsolete;
180}
181
182extern "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 Gampe3f46c962017-03-30 10:26:59 -0700188 if (JvmtiErrorToException(env, jvmti_env, result)) {
Andreas Gampefdeef522017-01-09 14:40:25 -0800189 return JNI_FALSE;
190 }
191
192 return is_synthetic;
193}
194
Andreas Gampe3c252f02016-10-27 18:25:17 -0700195} // namespace Test910Methods
196} // namespace art