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 extern "C" {
35 
36 /* number of tested methods in particular class */
37 #define METH_NUM 2
38 
39 /* overall number of all tested methods */
40 #define TOT_NUM 5
41 
42 #define STATUS_FAILED 2
43 #define PASSED 0
44 
45 /* expected class signatures are below */
46 static const char *meth_sig[][METH_NUM][TOT_NUM] = {
47     { { "methname003bMeth", "instance",
48         "(Lnsk/jvmti/GetMethodName/methname003b;)Lnsk/jvmti/GetMethodName/methname003b;",
49         "<L:Ljava/lang/String;>(Lnsk/jvmti/GetMethodName/methname003b<TL;>;)Lnsk/jvmti/GetMethodName/methname003b<Ljava/lang/String;>;" },
50      { "methname003bMethSt", "static",
51        "(Lnsk/jvmti/GetMethodName/methname003b;)Lnsk/jvmti/GetMethodName/methname003b;",
52        "<T:Ljava/lang/String;>(Lnsk/jvmti/GetMethodName/methname003b<TT;>;)Lnsk/jvmti/GetMethodName/methname003b<Ljava/lang/String;>;" } },
53 
54     { { "methname003cMeth", "instance",
55         "(Ljava/lang/Class;)Ljava/lang/Object;",
56         "<U:Ljava/lang/Object;>(Ljava/lang/Class<TU;>;)TU;" },
57      { "methname003cMethSt", "static",
58        "(Ljava/lang/Class;)Ljava/lang/Object;",
59        "<U:Ljava/lang/Object;>(Ljava/lang/Class<TU;>;)TU;" } },
60 
61     { { "methname003eMeth", "instance",
62         "(Lnsk/jvmti/GetMethodName/methname003e;)V",
63         "NULL" },
64      { "methname003eMethSt", "static",
65        "(Lnsk/jvmti/GetMethodName/methname003e;)V",
66        "NULL" } },
67 
68     { { "methname003ifMeth", "instance",
69         "()I",
70         "NULL" },
71      { "methname003ifMeth2", "instance",
72        "(Ljava/lang/Object;)I",
73        "<T:Ljava/lang/Object;>(TT;)I" } },
74 
75     { { "methname003gMeth", "instance",
76         "(Ljava/lang/Byte;Ljava/lang/Double;[Ljava/lang/Class;)V",
77         "<A:Ljava/lang/Byte;B:Ljava/lang/Double;>(TA;TB;[Ljava/lang/Class<*>;)V" },
78      { "methname003gMethSt", "static",
79        "(Ljava/lang/Byte;Ljava/lang/Double;)V",
80        "<A:Ljava/lang/Byte;B:Ljava/lang/Double;>(TA;TB;)V" } }
81 };
82 
83 static jvmtiEnv *jvmti = NULL;
84 
checkSig(JNIEnv * jni_env,jmethodID testedMeth,int instance,int clsIdx,int methIdx)85 static int checkSig(JNIEnv *jni_env, jmethodID testedMeth,
86         int instance, int clsIdx, int methIdx) {
87     int totRes = PASSED;
88     char *name;
89     char *sign;
90     char *gen_sign;
91 
92     if (!NSK_JVMTI_VERIFY(jvmti->GetMethodName(testedMeth, &name, &sign, &gen_sign))) {
93         NSK_COMPLAIN1("TEST FAILED: unable to get class signature for \"%s\"\n\n",
94             meth_sig[clsIdx][methIdx][0]);
95         return STATUS_FAILED;
96     } else {
97         NSK_DISPLAY1("Checking signatures for \"%s\" ...\n",
98             meth_sig[clsIdx][methIdx][0]);
99 
100         if (strcmp(meth_sig[clsIdx][methIdx][2], sign) != 0 ||
101                 strcmp(meth_sig[clsIdx][methIdx][3], (gen_sign == NULL) ? "NULL" : gen_sign) != 0) {
102             NSK_COMPLAIN5("TEST FAILED: class: \"%s\" \
103 has\n\tsignature: \"%s\"\n\tgeneric signature: \"%s\"\n\n\tExpected: \"%s\"\n\t\t\"%s\"\n\n",
104                 meth_sig[clsIdx][methIdx][0],
105                 sign, (gen_sign == NULL) ? "NULL" : gen_sign,
106                 meth_sig[clsIdx][methIdx][2], meth_sig[clsIdx][methIdx][3]);
107             totRes = STATUS_FAILED;
108         }
109         else
110             NSK_DISPLAY2("CHECK PASSED: signature: \"%s\",\n\tgeneric signature: \"%s\"\n",
111                 sign, (gen_sign == NULL) ? "NULL" : gen_sign);
112 
113         NSK_DISPLAY0("Deallocating name & signature arrays\n");
114         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) name))) {
115             totRes = STATUS_FAILED;
116         }
117         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) sign))) {
118             totRes = STATUS_FAILED;
119         }
120         if (gen_sign != NULL)
121             if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) gen_sign))) {
122                 totRes = STATUS_FAILED;
123             }
124     }
125 
126     return totRes;
127 }
128 
129 JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetMethodName_methname003_check(JNIEnv * jni,jobject obj,jobject testedObj,jint clsIdx)130 Java_nsk_jvmti_GetMethodName_methname003_check(
131         JNIEnv *jni, jobject obj, jobject testedObj, jint clsIdx) {
132     int res = PASSED, i, instance;
133     jmethodID testedMeth = NULL;
134     jclass objCls = jni->GetObjectClass(testedObj);
135 
136     for (i=0; i<METH_NUM; i++) {
137         instance = strcmp(meth_sig[clsIdx][i][1], "instance");
138 
139         NSK_DISPLAY2(">>> Finding %s method: %s ...\n",
140             (instance == 0) ? "instance" : "static",
141              meth_sig[clsIdx][i][0]);
142         if (instance == 0) {
143             if (!NSK_JNI_VERIFY(jni, (testedMeth = jni->GetMethodID(objCls, meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2])) != NULL)) {
144                 NSK_COMPLAIN2("TEST FAILERE: unable to get method ID for \"%s\" \"%s\"\n\n",
145                     meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2]);
146                 res = STATUS_FAILED;
147                 continue;
148             }
149         }
150         else
151             if (!NSK_JNI_VERIFY(jni, (testedMeth = jni->GetStaticMethodID(objCls, meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2])) != NULL)) {
152                 NSK_COMPLAIN2("TEST FAILERE: unable to get method ID for \"%s\" \"%s\"\n\n",
153                     meth_sig[clsIdx][i][0], meth_sig[clsIdx][i][2]);
154                 res = STATUS_FAILED;
155                 continue;
156             }
157 
158         NSK_DISPLAY1("\t... got methodID: 0x%p\n", (void*) testedMeth);
159 
160         if (checkSig(jni, testedMeth, instance, clsIdx, i) == STATUS_FAILED)
161             res = STATUS_FAILED;
162 
163         NSK_DISPLAY0("<<<\n");
164     }
165 
166     return res;
167 }
168 
169 #ifdef STATIC_BUILD
Agent_OnLoad_methname003(JavaVM * jvm,char * options,void * reserved)170 JNIEXPORT jint JNICALL Agent_OnLoad_methname003(JavaVM *jvm, char *options, void *reserved) {
171     return Agent_Initialize(jvm, options, reserved);
172 }
Agent_OnAttach_methname003(JavaVM * jvm,char * options,void * reserved)173 JNIEXPORT jint JNICALL Agent_OnAttach_methname003(JavaVM *jvm, char *options, void *reserved) {
174     return Agent_Initialize(jvm, options, reserved);
175 }
JNI_OnLoad_methname003(JavaVM * jvm,char * options,void * reserved)176 JNIEXPORT jint JNI_OnLoad_methname003(JavaVM *jvm, char *options, void *reserved) {
177     return JNI_VERSION_1_8;
178 }
179 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)180 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
181     /* init framework and parse options */
182     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
183         return JNI_ERR;
184 
185     /* create JVMTI environment */
186     if (!NSK_VERIFY((jvmti =
187             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
188         return JNI_ERR;
189 
190     return JNI_OK;
191 }
192 
193 }
194