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 #include "JVMTITools.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #ifndef JNI_ENV_ARG
35 
36 #ifdef __cplusplus
37 #define JNI_ENV_ARG(x, y) y
38 #define JNI_ENV_PTR(x) x
39 #else
40 #define JNI_ENV_ARG(x,y) x, y
41 #define JNI_ENV_PTR(x) (*x)
42 #endif
43 
44 #endif
45 
46 #define PASSED 0
47 #define STATUS_FAILED 2
48 
49 typedef struct {
50     char *name;
51     char *t_cls;
52     char *t_name;
53     char *t_sig;
54     jlocation t_loc;
55     char *c_cls;
56     char *c_name;
57     char *c_sig;
58     jlocation c_loc;
59 } exceptionInfo;
60 
61 static jvmtiEnv *jvmti = NULL;
62 static jvmtiCapabilities caps;
63 static jvmtiEventCallbacks callbacks;
64 static jint result = PASSED;
65 static jboolean printdump = JNI_FALSE;
66 static exceptionInfo exs[] = {
67   {"Lnsk/jvmti/Exception/exception001c;",
68    "Lnsk/jvmti/Exception/exception001b;", "meth1", "()V", 7,
69    "Lnsk/jvmti/Exception/exception001a;", "run", "()V", 14},
70   {"Ljava/lang/ArithmeticException;",
71    "Lnsk/jvmti/Exception/exception001b;", "meth2", "(I)I", 3,
72    "Lnsk/jvmti/Exception/exception001a;", "run", "()V", 24},
73   {"Ljava/lang/ArrayIndexOutOfBoundsException;",
74    "Lnsk/jvmti/Exception/exception001b;", "meth3", "(I)I", 10,
75    "Lnsk/jvmti/Exception/exception001a;", "run", "()V", 34}
76 };
77 static int eventsCount = 0;
78 static int eventsExpected = 0;
79 
80 void JNICALL
Exception(jvmtiEnv * jvmti_env,JNIEnv * env,jthread thr,jmethodID method,jlocation location,jobject exception,jmethodID catch_method,jlocation catch_location)81 Exception(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr,
82         jmethodID method, jlocation location, jobject exception,
83         jmethodID catch_method, jlocation catch_location) {
84     jvmtiError err;
85     exceptionInfo ex;
86     jclass cls;
87     char *generic;
88     size_t i;
89 
90     if (printdump == JNI_TRUE) {
91         printf(">>> retrieving Exception info ...\n");
92     }
93     cls = JNI_ENV_PTR(env)->GetObjectClass(JNI_ENV_ARG((JNIEnv *)env, exception));
94     err = (*jvmti_env)->GetClassSignature(jvmti_env, cls, &ex.name, &generic);
95     if (err != JVMTI_ERROR_NONE) {
96         printf("(GetClassSignature) unexpected error: %s (%d)\n",
97                TranslateError(err), err);
98         result = STATUS_FAILED;
99     }
100     err = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, method, &cls);
101     if (err != JVMTI_ERROR_NONE) {
102         printf("(GetMethodDeclaringClass#t) unexpected error: %s (%d)\n",
103                TranslateError(err), err);
104         result = STATUS_FAILED;
105     }
106     err = (*jvmti_env)->GetClassSignature(jvmti_env, cls, &ex.t_cls, &generic);
107     if (err != JVMTI_ERROR_NONE) {
108         printf("(GetClassSignature#t) unexpected error: %s (%d)\n",
109                TranslateError(err), err);
110         result = STATUS_FAILED;
111     }
112     err = (*jvmti_env)->GetMethodName(jvmti_env, method,
113         &ex.t_name, &ex.t_sig, &generic);
114     if (err != JVMTI_ERROR_NONE) {
115         printf("(GetMethodName#t) unexpected error: %s (%d)\n",
116                TranslateError(err), err);
117         result = STATUS_FAILED;
118     }
119     ex.t_loc = location;
120     err = (*jvmti_env)->GetMethodDeclaringClass(jvmti_env, catch_method, &cls);
121     if (err != JVMTI_ERROR_NONE) {
122         printf("(GetMethodDeclaringClass#c) unexpected error: %s (%d)\n",
123                TranslateError(err), err);
124         result = STATUS_FAILED;
125     }
126     err = (*jvmti_env)->GetClassSignature(jvmti_env, cls, &ex.c_cls, &generic);
127     if (err != JVMTI_ERROR_NONE) {
128         printf("(GetClassSignature#c) unexpected error: %s (%d)\n",
129                TranslateError(err), err);
130         result = STATUS_FAILED;
131     }
132     err = (*jvmti_env)->GetMethodName(jvmti_env, catch_method,
133         &ex.c_name, &ex.c_sig, &generic);
134     if (err != JVMTI_ERROR_NONE) {
135         printf("(GetMethodName#c) unexpected error: %s (%d)\n",
136                TranslateError(err), err);
137         result = STATUS_FAILED;
138     }
139     ex.c_loc = catch_location;
140     if (printdump == JNI_TRUE) {
141         printf(">>> %s\n", ex.name);
142         printf(">>>   thrown at %s.%s%s:0x%x%08x\n",
143                ex.t_cls, ex.t_name, ex.t_sig,
144                (jint)(ex.t_loc >> 32), (jint)ex.t_loc);
145         printf(">>>    catch at %s.%s%s:0x%x%08x\n",
146                ex.c_cls, ex.c_name, ex.c_sig,
147                (jint)(ex.c_loc >> 32), (jint)ex.c_loc);
148         printf(">>> ... done\n");
149     }
150     for (i = 0; i < sizeof(exs)/sizeof(exceptionInfo); i++) {
151         if (ex.name != NULL && strcmp(ex.name, exs[i].name) == 0
152          && ex.t_cls != NULL && strcmp(ex.t_cls, exs[i].t_cls) == 0
153          && ex.t_name != NULL && strcmp(ex.t_name, exs[i].t_name) == 0
154          && ex.t_sig != NULL && strcmp(ex.t_sig, exs[i].t_sig) == 0
155          && ex.c_cls != NULL && strcmp(ex.c_cls, exs[i].c_cls) == 0
156          && ex.c_name != NULL && strcmp(ex.c_name, exs[i].c_name) == 0
157          && ex.c_sig != NULL && strcmp(ex.c_sig, exs[i].c_sig) == 0
158          && ex.t_loc == exs[i].t_loc && ex.c_loc == exs[i].c_loc) {
159             eventsCount++;
160             break;
161         }
162     }
163     if (i == sizeof(exs)/sizeof(exceptionInfo)) {
164         printf("Unexpected exception event:\n");
165         printf("  %s\n", ex.name);
166         printf("    thrown at %s.%s%s:0x%x%08x\n",
167                ex.t_cls, ex.t_name, ex.t_sig,
168                (jint)(ex.t_loc >> 32), (jint)ex.t_loc);
169         printf("     catch at %s.%s%s:0x%x%08x\n",
170                ex.c_cls, ex.c_name, ex.c_sig,
171                (jint)(ex.c_loc >> 32), (jint)ex.c_loc);
172         result = STATUS_FAILED;
173     }
174 }
175 
176 #ifdef STATIC_BUILD
Agent_OnLoad_exception001(JavaVM * jvm,char * options,void * reserved)177 JNIEXPORT jint JNICALL Agent_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {
178     return Agent_Initialize(jvm, options, reserved);
179 }
Agent_OnAttach_exception001(JavaVM * jvm,char * options,void * reserved)180 JNIEXPORT jint JNICALL Agent_OnAttach_exception001(JavaVM *jvm, char *options, void *reserved) {
181     return Agent_Initialize(jvm, options, reserved);
182 }
JNI_OnLoad_exception001(JavaVM * jvm,char * options,void * reserved)183 JNIEXPORT jint JNI_OnLoad_exception001(JavaVM *jvm, char *options, void *reserved) {
184     return JNI_VERSION_1_8;
185 }
186 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)187 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
188     jvmtiError err;
189     jint res;
190 
191     if (options != NULL && strcmp(options, "printdump") == 0) {
192         printdump = JNI_TRUE;
193     }
194 
195     res = JNI_ENV_PTR(jvm)->GetEnv(JNI_ENV_ARG(jvm, (void **) &jvmti),
196         JVMTI_VERSION_1_1);
197     if (res != JNI_OK || jvmti == NULL) {
198         printf("Wrong result of a valid call to GetEnv!\n");
199         return JNI_ERR;
200     }
201 
202     err = (*jvmti)->GetPotentialCapabilities(jvmti, &caps);
203     if (err != JVMTI_ERROR_NONE) {
204         printf("(GetPotentialCapabilities) unexpected error: %s (%d)\n",
205                TranslateError(err), err);
206         return JNI_ERR;
207     }
208 
209     err = (*jvmti)->AddCapabilities(jvmti, &caps);
210     if (err != JVMTI_ERROR_NONE) {
211         printf("(AddCapabilities) unexpected error: %s (%d)\n",
212                TranslateError(err), err);
213         return JNI_ERR;
214     }
215 
216     err = (*jvmti)->GetCapabilities(jvmti, &caps);
217     if (err != JVMTI_ERROR_NONE) {
218         printf("(GetCapabilities) unexpected error: %s (%d)\n",
219                TranslateError(err), err);
220         return JNI_ERR;
221     }
222 
223     if (caps.can_generate_exception_events) {
224         callbacks.Exception = &Exception;
225         err = (*jvmti)->SetEventCallbacks(jvmti, &callbacks, sizeof(callbacks));
226         if (err != JVMTI_ERROR_NONE) {
227             printf("(SetEventCallbacks) unexpected error: %s (%d)\n",
228                    TranslateError(err), err);
229             return JNI_ERR;
230         }
231     } else {
232         printf("Warning: Exception event is not implemented\n");
233     }
234 
235     return JNI_OK;
236 }
237 
238 JNIEXPORT jint JNICALL
Java_nsk_jvmti_Exception_exception001_check(JNIEnv * env,jclass cls)239 Java_nsk_jvmti_Exception_exception001_check(JNIEnv *env, jclass cls) {
240     jvmtiError err;
241     jthread thread;
242     jclass clz;
243     jmethodID mid;
244 
245     if (jvmti == NULL) {
246         printf("JVMTI client was not properly loaded!\n");
247         return STATUS_FAILED;
248     }
249 
250     if (!caps.can_generate_exception_events) {
251         return result;
252     }
253 
254     clz = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env,
255         "nsk/jvmti/Exception/exception001c"));
256     if (clz == NULL) {
257         printf("Cannot find exception001c class!\n");
258         return STATUS_FAILED;
259     }
260     clz = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env,
261         "nsk/jvmti/Exception/exception001b"));
262     if (clz == NULL) {
263         printf("Cannot find exception001b class!\n");
264         return STATUS_FAILED;
265     }
266     clz = JNI_ENV_PTR(env)->FindClass(JNI_ENV_ARG(env,
267         "nsk/jvmti/Exception/exception001a"));
268     if (clz == NULL) {
269         printf("Cannot find exception001a class!\n");
270         return STATUS_FAILED;
271     }
272     mid = JNI_ENV_PTR(env)->GetStaticMethodID(JNI_ENV_ARG(env, clz),
273         "run", "()V");
274     if (mid == NULL) {
275         printf("Cannot find method run!\n");
276         return STATUS_FAILED;
277     }
278 
279     if ((err = ((*jvmti)->GetCurrentThread(jvmti, &thread))) != JVMTI_ERROR_NONE) {
280         printf("Failed to get current thread: %s (%d)\n", TranslateError(err), err);
281         result = STATUS_FAILED;
282         return STATUS_FAILED;
283     }
284 
285     err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_ENABLE,
286             JVMTI_EVENT_EXCEPTION, thread);
287     if (err == JVMTI_ERROR_NONE) {
288         eventsExpected = sizeof(exs)/sizeof(exceptionInfo);
289     } else {
290         printf("Failed to enable JVMTI_EVENT_EXCEPTION: %s (%d)\n",
291                TranslateError(err), err);
292         result = STATUS_FAILED;
293     }
294 
295     JNI_ENV_PTR(env)->CallStaticVoidMethod(JNI_ENV_ARG(env, clz), mid);
296 
297     err = (*jvmti)->SetEventNotificationMode(jvmti, JVMTI_DISABLE,
298             JVMTI_EVENT_EXCEPTION, thread);
299     if (err != JVMTI_ERROR_NONE) {
300         printf("Failed to disable JVMTI_EVENT_EXCEPTION: %s (%d)\n",
301                TranslateError(err), err);
302         result = STATUS_FAILED;
303     }
304 
305     if (eventsCount != eventsExpected) {
306         printf("Wrong number of exception events: %d, expected: %d\n",
307             eventsCount, eventsExpected);
308         result = STATUS_FAILED;
309     }
310     return result;
311 }
312 
313 #ifdef __cplusplus
314 }
315 #endif
316