1 /*
2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 #include <stdio.h>
25 #include <string.h>
26 #include <jvmti.h>
27 #include "agent_common.h"
28 
29 #include "nsk_tools.h"
30 #include "jni_tools.h"
31 #include "JVMTITools.h"
32 #include "jvmti_tools.h"
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37 
38 /* number of tested methods in particular class */
39 #define METH_NUM 2
40 
41 /* overall number of all tested methods */
42 #define TOT_NUM 5
43 
44 #define STATUS_FAILED 2
45 #define PASSED 0
46 
47 /* expected class signatures are below */
48 static const char *meth_sig[][METH_NUM][TOT_NUM] = {
49     {{"methname003bMeth", "instance",
50         "(Lnsk/jvmti/GetMethodName/methname003b;)Lnsk/jvmti/GetMethodName/methname003b;",
51         "<L:Ljava/lang/String;>(Lnsk/jvmti/GetMethodName/methname003b<TL;>;)Lnsk/jvmti/GetMethodName/methname003b<Ljava/lang/String;>;"},
52      {"methname003bMethSt", "static",
53         "(Lnsk/jvmti/GetMethodName/methname003b;)Lnsk/jvmti/GetMethodName/methname003b;",
54         "<T:Ljava/lang/String;>(Lnsk/jvmti/GetMethodName/methname003b<TT;>;)Lnsk/jvmti/GetMethodName/methname003b<Ljava/lang/String;>;"}},
55 
56     {{"methname003cMeth", "instance",
57         "(Ljava/lang/Class;)Ljava/lang/Object;",
58         "<U:Ljava/lang/Object;>(Ljava/lang/Class<TU;>;)TU;"},
59      {"methname003cMethSt", "static",
60         "(Ljava/lang/Class;)Ljava/lang/Object;",
61         "<U:Ljava/lang/Object;>(Ljava/lang/Class<TU;>;)TU;"}},
62 
63     {{"methname003eMeth", "instance",
64         "(Lnsk/jvmti/GetMethodName/methname003e;)V",
65         "NULL"},
66      {"methname003eMethSt", "static",
67         "(Lnsk/jvmti/GetMethodName/methname003e;)V",
68         "NULL"}},
69 
70     {{"methname003ifMeth", "instance",
71         "()I",
72         "NULL"},
73      {"methname003ifMeth2", "instance",
74         "(Ljava/lang/Object;)I",
75         "<T:Ljava/lang/Object;>(TT;)I"}},
76 
77     {{"methname003gMeth", "instance",
78         "(Ljava/lang/Byte;Ljava/lang/Double;[Ljava/lang/Class;)V",
79         "<A:Ljava/lang/Byte;B:Ljava/lang/Double;>(TA;TB;[Ljava/lang/Class<*>;)V"},
80      {"methname003gMethSt", "static",
81         "(Ljava/lang/Byte;Ljava/lang/Double;)V",
82         "<A:Ljava/lang/Byte;B:Ljava/lang/Double;>(TA;TB;)V"}}
83 };
84 
85 static jvmtiEnv *jvmti = NULL;
86 
checkSig(JNIEnv * jni_env,jmethodID testedMeth,int instance,int clsIdx,int methIdx)87 static int checkSig(JNIEnv *jni_env, jmethodID testedMeth,
88         int instance, int clsIdx, int methIdx) {
89     int totRes = PASSED;
90     char *name;
91     char *sign;
92     char *gen_sign;
93 
94     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
95             jvmti, testedMeth, &name, &sign, &gen_sign))) {
96         NSK_COMPLAIN1("TEST FAILED: unable to get class signature for \"%s\"\n\n",
97             meth_sig[clsIdx][methIdx][0]);
98         return STATUS_FAILED;
99     } else {
100         NSK_DISPLAY1("Checking signatures for \"%s\" ...\n",
101             meth_sig[clsIdx][methIdx][0]);
102 
103         if (strcmp(meth_sig[clsIdx][methIdx][2], sign) != 0 ||
104                 strcmp(meth_sig[clsIdx][methIdx][3], (gen_sign==NULL)?"NULL":gen_sign) != 0) {
105             NSK_COMPLAIN5("TEST FAILED: class: \"%s\" \
106 has\n\tsignature: \"%s\"\n\tgeneric signature: \"%s\"\n\n\tExpected: \"%s\"\n\t\t\"%s\"\n\n",
107                 meth_sig[clsIdx][methIdx][0],
108                 sign, (gen_sign==NULL)?"NULL":gen_sign,
109                 meth_sig[clsIdx][methIdx][2], meth_sig[clsIdx][methIdx][3]);
110             totRes = STATUS_FAILED;
111         }
112         else
113             NSK_DISPLAY2("CHECK PASSED: signature: \"%s\",\n\tgeneric signature: \"%s\"\n",
114                 sign, (gen_sign==NULL)?"NULL":gen_sign);
115 
116         NSK_DISPLAY0("Deallocating name & signature arrays\n");
117         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
118                 jvmti, (unsigned char*) name))) {
119             totRes = STATUS_FAILED;
120         }
121         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
122                 jvmti, (unsigned char*) sign))) {
123             totRes = STATUS_FAILED;
124         }
125         if (gen_sign!=NULL)
126             if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(Deallocate,
127                     jvmti, (unsigned char*) gen_sign))) {
128                 totRes = STATUS_FAILED;
129             }
130     }
131 
132     return totRes;
133 }
134 
135 JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetMethodName_methname003_check(JNIEnv * jni,jobject obj,jobject testedObj,jint clsIdx)136 Java_nsk_jvmti_GetMethodName_methname003_check(
137         JNIEnv *jni, jobject obj, jobject testedObj, jint clsIdx) {
138     int res = PASSED, i, instance;
139     jmethodID testedMeth = NULL;
140     jclass objCls = NSK_CPP_STUB2(GetObjectClass,
141         jni, testedObj);
142 
143     for (i=0; i<METH_NUM; i++) {
144         instance = strcmp(meth_sig[clsIdx][i][1], "instance");
145 
146         NSK_DISPLAY2(">>> Finding %s method: %s ...\n",
147             (instance==0)?"instance":"static",
148              meth_sig[clsIdx][i][0]);
149         if (instance==0) {
150             if (!NSK_JNI_VERIFY(jni, (testedMeth = NSK_CPP_STUB4(GetMethodID,
151                     jni, objCls, meth_sig[clsIdx][i][0],
152                     meth_sig[clsIdx][i][2])) != NULL)) {
153                 NSK_COMPLAIN2("TEST FAILERE: unable to get method ID for \"%s\" \"%s\"\n\n",
154                     meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2]);
155                 res = STATUS_FAILED;
156                 continue;
157             }
158         }
159         else
160             if (!NSK_JNI_VERIFY(jni, (testedMeth = NSK_CPP_STUB4(GetStaticMethodID,
161                     jni, objCls, meth_sig[clsIdx][i][0],
162                     meth_sig[clsIdx][i][2])) != NULL)) {
163                 NSK_COMPLAIN2("TEST FAILERE: unable to get method ID for \"%s\" \"%s\"\n\n",
164                     meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2]);
165                 res = STATUS_FAILED;
166                 continue;
167             }
168 
169         NSK_DISPLAY1("\t... got methodID: 0x%p\n", (void*) testedMeth);
170 
171         if (checkSig(jni, testedMeth, instance, clsIdx, i) == STATUS_FAILED)
172             res = STATUS_FAILED;
173 
174         NSK_DISPLAY0("<<<\n");
175     }
176 
177     return res;
178 }
179 
180 #ifdef STATIC_BUILD
Agent_OnLoad_methname003(JavaVM * jvm,char * options,void * reserved)181 JNIEXPORT jint JNICALL Agent_OnLoad_methname003(JavaVM *jvm, char *options, void *reserved) {
182     return Agent_Initialize(jvm, options, reserved);
183 }
Agent_OnAttach_methname003(JavaVM * jvm,char * options,void * reserved)184 JNIEXPORT jint JNICALL Agent_OnAttach_methname003(JavaVM *jvm, char *options, void *reserved) {
185     return Agent_Initialize(jvm, options, reserved);
186 }
JNI_OnLoad_methname003(JavaVM * jvm,char * options,void * reserved)187 JNIEXPORT jint JNI_OnLoad_methname003(JavaVM *jvm, char *options, void *reserved) {
188     return JNI_VERSION_1_8;
189 }
190 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)191 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
192     /* init framework and parse options */
193     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
194         return JNI_ERR;
195 
196     /* create JVMTI environment */
197     if (!NSK_VERIFY((jvmti =
198             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
199         return JNI_ERR;
200 
201     return JNI_OK;
202 }
203 
204 #ifdef __cplusplus
205 }
206 #endif
207