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 #define CLS_NUM 5 /* overall number of tested classes */
37 
38 #define STATUS_FAILED 2
39 #define PASSED 0
40 
41 /* expected class signatures are below */
42 static const char *class_sig[][CLS_NUM] = {
43     { "getclsig006", "Lnsk/jvmti/GetClassSignature/getclsig006;", "NULL" },
44     { "getclsig006b", "Lnsk/jvmti/GetClassSignature/getclsig006b;",
45       "<L:Ljava/lang/String;>Ljava/lang/Object;" },
46     { "getclsig006c", "Lnsk/jvmti/GetClassSignature/getclsig006c;",
47       "<A:Ljava/lang/Object;B:Ljava/lang/Integer;>Ljava/lang/Object;" },
48     { "getclsig006if", "Lnsk/jvmti/GetClassSignature/getclsig006if;",
49       "<I:Ljava/lang/Object;>Ljava/lang/Object;" },
50     { "getclsig006g", "Lnsk/jvmti/GetClassSignature/getclsig006g;",
51       "<E:Lnsk/jvmti/GetClassSignature/getclsig006e;:Lnsk/jvmti/GetClassSignature/getclsig006if;>Ljava/lang/Object;" }
52 };
53 
54 static jvmtiEnv *jvmti = NULL;
55 
checkSig(JNIEnv * jni_env,jclass testedCls,int idx)56 static int checkSig(JNIEnv *jni_env, jclass testedCls, int idx) {
57     int totRes = PASSED;
58     char *sign;
59     char *gen_sign;
60 
61     if (!NSK_JVMTI_VERIFY(jvmti->GetClassSignature(testedCls, &sign, &gen_sign))) {
62         NSK_COMPLAIN1("TEST FAILED: unable to get class signature for \"%s\"\n\n",
63             class_sig[idx][0]);
64         return STATUS_FAILED;
65     } else {
66         NSK_DISPLAY1(">>> Checking signatures for \"%s\" ...\n",
67             class_sig[idx][0]);
68 
69         if (strcmp(class_sig[idx][1], sign) != 0 ||
70                 strcmp(class_sig[idx][2], (gen_sign == NULL) ? "NULL" : gen_sign) != 0) {
71             NSK_COMPLAIN5(
72                 "TEST FAILED: class: \"%s\" has\n"
73                 "\tsignature: \"%s\"\n"
74                 "\tgeneric signature: \"%s\"\n\n"
75                 "\tExpected: \"%s\"\n"
76                 "\t\"%s\"\n\n",
77                 class_sig[idx][0],
78                 sign, (gen_sign == NULL) ? "NULL" : gen_sign,
79                 class_sig[idx][1], class_sig[idx][2]);
80             totRes = STATUS_FAILED;
81         }
82         else
83             NSK_DISPLAY2("CHECK PASSED: signature: \"%s\",\n\tgeneric signature: \"%s\"\n",
84                 sign, (gen_sign == NULL) ? "NULL" : gen_sign);
85 
86         NSK_DISPLAY0("Deallocating the signature array\n");
87         if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) sign))) {
88             totRes = STATUS_FAILED;
89         }
90         if (gen_sign != NULL)
91             if (!NSK_JVMTI_VERIFY(jvmti->Deallocate((unsigned char*) gen_sign))) {
92                 totRes = STATUS_FAILED;
93             }
94 
95         NSK_DISPLAY0("<<<\n");
96     }
97 
98     return totRes;
99 }
100 
101 JNIEXPORT jint JNICALL
Java_nsk_jvmti_GetClassSignature_getclsig006_check(JNIEnv * jni,jobject obj)102 Java_nsk_jvmti_GetClassSignature_getclsig006_check(
103         JNIEnv *jni, jobject obj) {
104     int res = PASSED, i;
105     jclass testedCls;
106 
107     for (i=0; i<CLS_NUM; i++) {
108         if (!NSK_JNI_VERIFY(jni, (testedCls = jni->FindClass(class_sig[i][1])) != NULL)) {
109             NSK_COMPLAIN1("TEST FAILURE: unable to find class \"%s\"\n\n",
110                 class_sig[i][0]);
111             res = STATUS_FAILED;
112             continue;
113         }
114 
115         if (checkSig(jni, testedCls, i) == STATUS_FAILED)
116             res = STATUS_FAILED;
117     }
118 
119     return res;
120 }
121 
122 #ifdef STATIC_BUILD
Agent_OnLoad_getclsig006(JavaVM * jvm,char * options,void * reserved)123 JNIEXPORT jint JNICALL Agent_OnLoad_getclsig006(JavaVM *jvm, char *options, void *reserved) {
124     return Agent_Initialize(jvm, options, reserved);
125 }
Agent_OnAttach_getclsig006(JavaVM * jvm,char * options,void * reserved)126 JNIEXPORT jint JNICALL Agent_OnAttach_getclsig006(JavaVM *jvm, char *options, void *reserved) {
127     return Agent_Initialize(jvm, options, reserved);
128 }
JNI_OnLoad_getclsig006(JavaVM * jvm,char * options,void * reserved)129 JNIEXPORT jint JNI_OnLoad_getclsig006(JavaVM *jvm, char *options, void *reserved) {
130     return JNI_VERSION_1_8;
131 }
132 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)133 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
134     /* init framework and parse options */
135     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
136         return JNI_ERR;
137 
138     /* create JVMTI environment */
139     if (!NSK_VERIFY((jvmti =
140             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
141         return JNI_ERR;
142 
143     return JNI_OK;
144 }
145 
146 }
147