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 "android-base/macros.h"
20 
21 #include "jni.h"
22 #include "jvmti.h"
23 #include "scoped_local_ref.h"
24 
25 // Test infrastructure
26 #include "jni_helper.h"
27 #include "jvmti_helper.h"
28 #include "test_env.h"
29 
30 namespace art {
31 namespace Test910Methods {
32 
Java_art_Test910_getMethodName(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)33 extern "C" JNIEXPORT jobjectArray JNICALL Java_art_Test910_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);
41   if (JvmtiErrorToException(env, jvmti_env, result)) {
42     return nullptr;
43   }
44 
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);
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 
67   // Also run GetMethodName with all parameter pointers null to check for segfaults.
68   jvmtiError result2 = jvmti_env->GetMethodName(id, nullptr, nullptr, nullptr);
69   if (JvmtiErrorToException(env, jvmti_env, result2)) {
70     return nullptr;
71   }
72 
73   return ret;
74 }
75 
Java_art_Test910_getMethodDeclaringClass(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)76 extern "C" JNIEXPORT jclass JNICALL Java_art_Test910_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);
82   if (JvmtiErrorToException(env, jvmti_env, result)) {
83     return nullptr;
84   }
85 
86   return declaring_class;
87 }
88 
Java_art_Test910_getMethodModifiers(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)89 extern "C" JNIEXPORT jint JNICALL Java_art_Test910_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);
95   if (JvmtiErrorToException(env, jvmti_env, result)) {
96     return 0;
97   }
98 
99   return modifiers;
100 }
101 
Java_art_Test910_getMaxLocals(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)102 extern "C" JNIEXPORT jint JNICALL Java_art_Test910_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);
108   if (JvmtiErrorToException(env, jvmti_env, result)) {
109     return -1;
110   }
111 
112   return max_locals;
113 }
114 
Java_art_Test910_getArgumentsSize(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)115 extern "C" JNIEXPORT jint JNICALL Java_art_Test910_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);
121   if (JvmtiErrorToException(env, jvmti_env, result)) {
122     return -1;
123   }
124 
125   return arguments;
126 }
127 
Java_art_Test910_getMethodLocationStart(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)128 extern "C" JNIEXPORT jlong JNICALL Java_art_Test910_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);
135   if (JvmtiErrorToException(env, jvmti_env, result)) {
136     return -1;
137   }
138 
139   return start;
140 }
141 
Java_art_Test910_getMethodLocationEnd(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)142 extern "C" JNIEXPORT jlong JNICALL Java_art_Test910_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);
149   if (JvmtiErrorToException(env, jvmti_env, result)) {
150     return -1;
151   }
152 
153   return end;
154 }
155 
Java_art_Test910_isMethodNative(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)156 extern "C" JNIEXPORT jboolean JNICALL Java_art_Test910_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);
162   if (JvmtiErrorToException(env, jvmti_env, result)) {
163     return JNI_FALSE;
164   }
165 
166   return is_native;
167 }
168 
Java_art_Test910_isMethodObsolete(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)169 extern "C" JNIEXPORT jboolean JNICALL Java_art_Test910_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);
175   if (JvmtiErrorToException(env, jvmti_env, result)) {
176     return JNI_FALSE;
177   }
178 
179   return is_obsolete;
180 }
181 
Java_art_Test910_isMethodSynthetic(JNIEnv * env,jclass klass ATTRIBUTE_UNUSED,jobject method)182 extern "C" JNIEXPORT jboolean JNICALL Java_art_Test910_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);
188   if (JvmtiErrorToException(env, jvmti_env, result)) {
189     return JNI_FALSE;
190   }
191 
192   return is_synthetic;
193 }
194 
195 }  // namespace Test910Methods
196 }  // namespace art
197