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 <string.h>
25 #include "jvmti.h"
26 #include "agent_common.h"
27 #include "jni_tools.h"
28 #include "jvmti_tools.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 /* min and max values defined in jvmtiParamTypes */
35 #define PARAM_TYPE_MIN_VALUE 101
36 #define PARAM_TYPE_MAX_VALUE 117
37 
38 /* min and max values defined in jvmtiParamKind */
39 #define PARAM_KIND_MIN_VALUE  91
40 #define PARAM_KIND_MAX_VALUE  97
41 
42 #define NAME_PREFIX "com.sun.hotspot"
43 
44 /* ============================================================================= */
45 
46 static jlong timeout = 0;
47 
48 /* ============================================================================= */
49 
50 /** Check extension functions. */
checkExtensions(jvmtiEnv * jvmti,const char phase[])51 static int checkExtensions(jvmtiEnv* jvmti, const char phase[]) {
52     int success = NSK_TRUE;
53     jint extCount = 0;
54     jvmtiExtensionFunctionInfo* extList = NULL;
55     int i;
56 
57     NSK_DISPLAY0("Get extension functions list\n");
58     if (!NSK_JVMTI_VERIFY(
59             NSK_CPP_STUB3(GetExtensionFunctions, jvmti, &extCount, &extList))) {
60         return NSK_FALSE;
61     }
62     NSK_DISPLAY1("  ... got count: %d\n", (int)extCount);
63     NSK_DISPLAY1("  ... got list:  0x%p\n", (void*)extList);
64 
65     if (extCount > 0) {
66         if (extList == NULL) {
67             NSK_COMPLAIN3("In %s phase GetExtensionFunctions() returned NULL pointer:\n"
68                           "#   extensions pointer: 0x%p\n"
69                           "#   extensions count:   %d\n",
70                             phase, (void*)extList, (int)extCount);
71             return NSK_FALSE;
72         }
73 
74         NSK_DISPLAY1("Check each extension functions: %d functions\n", (int)extCount);
75         for (i = 0; i < extCount; i++) {
76             int j;
77 
78             NSK_DISPLAY1("  function #%d:\n", i);
79             NSK_DISPLAY1("    func:        0x%p\n", (void*)extList[i].func);
80             NSK_DISPLAY1("    id:          \"%s\"\n", nsk_null_string(extList[i].id));
81             NSK_DISPLAY1("    short_desc:  \"%s\"\n", nsk_null_string(extList[i].short_description));
82             NSK_DISPLAY1("    param_count: %d\n", (int)extList[i].param_count);
83             NSK_DISPLAY1("    params:      0x%p\n", (void*)extList[i].params);
84 
85             if (extList[i].params != NULL) {
86                 for (j = 0; j < extList[i].param_count; j++) {
87                     NSK_DISPLAY1("      param #%d:\n", j);
88                     NSK_DISPLAY1("        name:      \"%s\"\n",
89                                             nsk_null_string(extList[i].params[j].name));
90                     NSK_DISPLAY1("        kind:      %d\n",
91                                             (int)extList[i].params[j].kind);
92                     NSK_DISPLAY1("        base_type: %d\n",
93                                             (int)extList[i].params[j].base_type);
94                     NSK_DISPLAY1("        null_ok:   %d\n",
95                                             (int)extList[i].params[j].null_ok);
96                 }
97             }
98 
99             NSK_DISPLAY1("    error_count: %d\n", (int)extList[i].error_count);
100             NSK_DISPLAY1("    errors:      0x%p\n", (void*)extList[i].errors);
101 
102             if (extList[i].errors != NULL) {
103                 for (j = 0; j < extList[i].error_count; j++) {
104                     NSK_DISPLAY2("      error #%d: %d\n",
105                                             j, (int)extList[i].errors[j]);
106                 }
107             }
108 
109             if (extList[i].func == NULL
110                     || extList[i].id == NULL
111                     || extList[i].short_description == NULL
112                     || (extList[i].params == NULL && extList[i].param_count > 0)
113                     || (extList[i].errors == NULL && extList[i].error_count > 0)) {
114                 NSK_COMPLAIN9("In %s phase GetExtensionFunctions() returned function #%d with NULL attribute(s):\n"
115                               "#   func:        0x%p\n"
116                               "#   id:          0x%p (%s)\n"
117                               "#   short_desc:  0x%p (%s)\n"
118                               "#   params:      0x%p\n"
119                               "#   errors:      0x%p\n",
120                                 phase, i,
121                                 (void*)extList[i].func,
122                                 (void*)extList[i].id, nsk_null_string(extList[i].id),
123                                 (void*)extList[i].short_description, nsk_null_string(extList[i].short_description),
124                                 (void*)extList[i].params, (void*)extList[i].errors);
125                 success = NSK_FALSE;
126             }
127 
128             if (extList[i].id != NULL && strlen(extList[i].id) <= 0) {
129                 NSK_COMPLAIN6("In %s phase GetExtensionFunctions() returned function #%d with empty id:\n"
130                               "#   func:        0x%p\n"
131                               "#   id:          \"%s\"\n"
132                               "#   short_desc:  \"%s\"\n"
133                               "#   param_count: %d\n",
134                                 phase, i,
135                                 extList[i].func,
136                                 nsk_null_string(extList[i].id),
137                                 nsk_null_string(extList[i].short_description),
138                                 (int)extList[i].param_count);
139                 success = NSK_FALSE;
140             } else if (strstr(extList[i].id, NAME_PREFIX) == NULL) {
141                 NSK_COMPLAIN6("In %s phase GetExtensionFunctions() returned function #%d with unexpected id:\n"
142                               "#   func:        0x%p\n"
143                               "#   id:          \"%s\"\n"
144                               "#   short_desc:  \"%s\"\n"
145                               "#   param_count: %d\n",
146                                 phase, i,
147                                 extList[i].func,
148                                 nsk_null_string(extList[i].id),
149                                 nsk_null_string(extList[i].short_description),
150                                 (int)extList[i].param_count);
151                 success = NSK_FALSE;
152             }
153 
154             if (extList[i].short_description != NULL && strlen(extList[i].short_description) <= 0) {
155                 NSK_COMPLAIN6("In %s phase GetExtensionFunctions() returned function #%d with empty desc:\n"
156                               "#   func:        0x%p\n"
157                               "#   id:          \"%s\"\n"
158                               "#   short_desc:  \"%s\"\n"
159                               "#   param_count: %d\n",
160                                 phase, i,
161                                 extList[i].func,
162                                 nsk_null_string(extList[i].id),
163                                 nsk_null_string(extList[i].short_description),
164                                 (int)extList[i].param_count);
165                 success = NSK_FALSE;
166             }
167 
168             if (extList[i].param_count > 0 && extList[i].params != NULL) {
169                 for (j = 0; j < extList[i].param_count; j++) {
170                     if (extList[i].params[j].name == NULL
171                             || strlen(extList[i].params[j].name) <= 0) {
172                         NSK_COMPLAIN9("In %s phase GetExtensionFunctions() returned function #%d with empty desc:\n"
173                                       "#   func:        0x%p\n"
174                                       "#   id:          \"%s\"\n"
175                                       "#   short_desc:  \"%s\"\n"
176                                       "#   param_count: %d\n"
177                                       "#     param #%d: \n"
178                                       "#       name:    0x%p (%s)\n",
179                                 phase, i,
180                                 extList[i].func,
181                                 nsk_null_string(extList[i].id),
182                                 nsk_null_string(extList[i].short_description),
183                                 (int)extList[i].param_count,
184                                 j,
185                                 (void*)extList[i].params[j].name,
186                                 nsk_null_string(extList[i].params[j].name));
187                         success = NSK_FALSE;
188                     }
189 
190                     if (extList[i].params[j].kind < PARAM_KIND_MIN_VALUE
191                             || extList[i].params[j].kind > PARAM_KIND_MAX_VALUE) {
192                         NSK_COMPLAIN9("In %s phase GetExtensionFunctions() returned function #%d with incorrect parameter kind:\n"
193                                       "#   func:        0x%p\n"
194                                       "#   id:          \"%s\"\n"
195                                       "#   short_desc:  \"%s\"\n"
196                                       "#   param_count: %d\n"
197                                       "#     param #%d: \n"
198                                       "#       name:    0x%p (%s)\n",
199                                 phase, i,
200                                 extList[i].func,
201                                 nsk_null_string(extList[i].id),
202                                 nsk_null_string(extList[i].short_description),
203                                 (int)extList[i].param_count,
204                                 j,
205                                 (void*)extList[i].params[j].name,
206                                 nsk_null_string(extList[i].params[j].name));
207                         success = NSK_FALSE;
208                     }
209 
210                     if (extList[i].params[j].base_type < PARAM_TYPE_MIN_VALUE
211                             || extList[i].params[j].base_type > PARAM_TYPE_MAX_VALUE) {
212                         NSK_COMPLAIN9("In %s phase GetExtensionFunctions() returned function #%d with incorrect parameter type:\n"
213                                       "#   func:        0x%p\n"
214                                       "#   id:          \"%s\"\n"
215                                       "#   short_desc:  \"%s\"\n"
216                                       "#   param_count: %d\n"
217                                       "#     param #%d: \n"
218                                       "#       name:    0x%p (%s)\n",
219                                 phase, i,
220                                 extList[i].func,
221                                 nsk_null_string(extList[i].id),
222                                 nsk_null_string(extList[i].short_description),
223                                 (int)extList[i].param_count,
224                                 j,
225                                 (void*)extList[i].params[j].name,
226                                 nsk_null_string(extList[i].params[j].name));
227                         success = NSK_FALSE;
228                     }
229                 }
230             }
231         }
232     }
233 
234     NSK_DISPLAY1("Deallocate extension functions list: 0x%p\n", (void*)extList);
235     if (!NSK_JVMTI_VERIFY(
236             NSK_CPP_STUB2(Deallocate, jvmti, (unsigned char*)extList))) {
237         return NSK_FALSE;
238     }
239     NSK_DISPLAY0("  ... deallocated\n");
240 
241     return success;
242 }
243 
244 /* ============================================================================= */
245 
246 /** Agent algorithm. */
247 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)248 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
249     NSK_DISPLAY0("Wait for debugee class ready\n");
250     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
251         return;
252 
253     NSK_DISPLAY0(">>> Testcase #2: Check extension functions in live phase\n");
254     {
255         if (!checkExtensions(jvmti, "live")) {
256             nsk_jvmti_setFailStatus();
257         }
258     }
259 
260     NSK_DISPLAY0("Let debugee to finish\n");
261     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
262         return;
263 }
264 
265 /* ============================================================================= */
266 
267 /** Agent library initialization. */
268 #ifdef STATIC_BUILD
Agent_OnLoad_extfuncs001(JavaVM * jvm,char * options,void * reserved)269 JNIEXPORT jint JNICALL Agent_OnLoad_extfuncs001(JavaVM *jvm, char *options, void *reserved) {
270     return Agent_Initialize(jvm, options, reserved);
271 }
Agent_OnAttach_extfuncs001(JavaVM * jvm,char * options,void * reserved)272 JNIEXPORT jint JNICALL Agent_OnAttach_extfuncs001(JavaVM *jvm, char *options, void *reserved) {
273     return Agent_Initialize(jvm, options, reserved);
274 }
JNI_OnLoad_extfuncs001(JavaVM * jvm,char * options,void * reserved)275 JNIEXPORT jint JNI_OnLoad_extfuncs001(JavaVM *jvm, char *options, void *reserved) {
276     return JNI_VERSION_1_8;
277 }
278 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)279 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
280     jvmtiEnv* jvmti = NULL;
281 
282     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
283         return JNI_ERR;
284 
285     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
286 
287     if (!NSK_VERIFY((jvmti =
288             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
289         return JNI_ERR;
290 
291     NSK_DISPLAY0(">>> Testcase #1: Check extension functions in OnLoad phase\n");
292     {
293         if (!checkExtensions(jvmti, "OnLoad")) {
294             nsk_jvmti_setFailStatus();
295         }
296     }
297 
298     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
299         return JNI_ERR;
300 
301     return JNI_OK;
302 }
303 
304 /* ============================================================================= */
305 
306 #ifdef __cplusplus
307 }
308 #endif
309