1 /*
  2  * Copyright (c) 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 <stdlib.h>
 26 #include <string.h>
 27 
 28 #include <jvmti.h>
 29 
 30 #include "jvmti_common.h"
 31 
 32 extern "C" {
 33 
 34 #define PASSED  0
 35 #define STATUS_FAILED  2
 36 
 37 /* classes which must have the class load event */
 38 static const char *expSigs[] = {
 39     "Lclassload01;",
 40     "Lclassload01$TestedClass;",
 41     "LTestedClassVirtual;",
 42 };
 43 #define EXP_SIG_NUM (sizeof(expSigs)/sizeof(char*))
 44 
 45 static const jboolean expThreadIsVirtual[] = {
 46     JNI_FALSE,
 47     JNI_FALSE,
 48     JNI_TRUE
 49 };
 50 
 51 /* classes which must not have the class load event */
 52 static const char *unexpSigs[] = {
 53     "Z", /* boolean */
 54     "B", /* byte */
 55     "C", /* char */
 56     "D", /* double */
 57     "F", /* float */
 58     "I", /* integer */
 59     "J", /* long */
 60     "S", /* short */
 61 
 62     "[Z", /* boolean array */
 63     "[B", /* byte array */
 64     "[C", /* char array */
 65     "[D", /* double array */
 66     "[F", /* float array */
 67     "[I", /* integer array */
 68     "[J", /* long array */
 69     "[S", /* short array */
 70     "[Lclassload01$TestedClass;"
 71 };
 72 #define UNEXP_SIG_NUM (sizeof(unexpSigs)/sizeof(char*))
 73 
 74 static volatile int clsEvents[EXP_SIG_NUM];
 75 static volatile int primClsEvents[UNEXP_SIG_NUM];
 76 
 77 static jint result = PASSED;
 78 static jvmtiEnv *jvmti = NULL;
 79 static jvmtiEventCallbacks callbacks;
 80 static jrawMonitorID counter_lock;
 81 
 82 static void initCounters() {
 83   size_t i;
 84 
 85   for (i = 0; i < EXP_SIG_NUM; i++) {
 86     clsEvents[i] = 0;
 87   }
 88 
 89   for (i = 0; i < UNEXP_SIG_NUM; i++) {
 90     primClsEvents[i] = 0;
 91   }
 92 }
 93 
 94 static int findSig(char *sig, int expected) {
 95   unsigned int i;
 96 
 97   for (i = 0; i < ((expected == 1) ? EXP_SIG_NUM : UNEXP_SIG_NUM); i++) {
 98     if (sig != NULL &&
 99         strcmp(((expected == 1) ? expSigs[i] : unexpSigs[i]), sig) == 0) {
100       return i; /* the signature found, return index */
101     }
102   }
103   return -1; /* the signature not found */
104 }
105 
106 /** callback functions **/
107 void JNICALL
108 ClassLoad(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread, jclass klass) {
109   int i = 0;
110   char *sig, *generic;
111   jvmtiError err;
112 
113   RawMonitorLocker rml(jvmti, jni, counter_lock);
114 
115   err = jvmti->GetClassSignature(klass, &sig, &generic);
116   if (err != JVMTI_ERROR_NONE) {
117     result = STATUS_FAILED;
118     LOG("TEST FAILURE: unable to obtain a class signature. Error %d\n", err);
119     return;
120   }
121 
122   i = findSig(sig, 1);
123   if (i != -1) {
124     jboolean is_virtual_thread = jni->IsVirtualThread(thread);
125     print_thread_info(jvmti, jni, thread);
126     if (is_virtual_thread != expThreadIsVirtual[i]) {
127       LOG("TEST FAILED: IsVirtualThread(thread) is not expected: %d\n", is_virtual_thread);
128       result = STATUS_FAILED;
129     } else {
130       clsEvents[i]++;
131       LOG("CHECK PASSED: ClassLoad event received for the class \"%s\" as expected\n", sig);
132     }
133   } else {
134     i = findSig(sig, 0);
135     if (i != -1) {
136       result = STATUS_FAILED;
137       primClsEvents[i]++;
138       LOG(
139           "TEST FAILED: JVMTI_EVENT_CLASS_LOAD event received for\n"
140           "\t a primitive class/array of primitive types with the signature \"%s\"\n",
141           sig);
142     }
143   }
144 }
145 /************************/
146 
147 JNIEXPORT jint JNICALL
148 Java_classload01_check(JNIEnv *jni, jobject obj) {
149   size_t i;
150 
151   for (i = 0; i < EXP_SIG_NUM; i++) {
152     if (clsEvents[i] != 1) {
153       result = STATUS_FAILED;
154       LOG("TEST FAILED: wrong number of JVMTI_EVENT_CLASS_LOAD events for \"%s\":\n\tgot: %d\texpected: 1\n",
155              expSigs[i], clsEvents[i]);
156     }
157   }
158 
159   for (i = 0; i < UNEXP_SIG_NUM; i++) {
160     if (primClsEvents[i] != 0) {
161       LOG("TEST FAILED: there are JVMTI_EVENT_CLASS_LOAD events for the primitive classes\n");
162     }
163   }
164   return result;
165 }
166 
167 #ifdef STATIC_BUILD
168 JNIEXPORT jint JNICALL Agent_OnLoad_classload01(JavaVM *jvm, char *options, void *reserved) {
169     return Agent_Initialize(jvm, options, reserved);
170 }
171 JNIEXPORT jint JNICALL Agent_OnAttach_classload01(JavaVM *jvm, char *options, void *reserved) {
172     return Agent_Initialize(jvm, options, reserved);
173 }
174 JNIEXPORT jint JNI_OnLoad_classload01(JavaVM *jvm, char *options, void *reserved) {
175     return JNI_VERSION_1_8;
176 }
177 #endif
178 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
179   jvmtiCapabilities caps;
180   jvmtiError err;
181   jint res;
182 
183   res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_9);
184   if (res != JNI_OK || jvmti == NULL) {
185     LOG("Wrong result of a valid call to GetEnv!\n");
186     return JNI_ERR;
187   }
188 
189   memset(&caps, 0, sizeof(jvmtiCapabilities));
190   caps.can_support_virtual_threads = 1;
191 
192   err = jvmti->AddCapabilities(&caps);
193   if (err != JVMTI_ERROR_NONE) {
194     return JNI_ERR;
195   }
196 
197   err = jvmti->GetCapabilities(&caps);
198   if (err != JVMTI_ERROR_NONE) {
199     return JNI_ERR;
200   }
201 
202   if (!caps.can_support_virtual_threads) {
203     LOG("ERROR: virtual thread support is not implemented.\n");
204     return JNI_ERR;
205   }
206 
207 
208   initCounters();
209 
210   counter_lock = create_raw_monitor(jvmti, "_counter_lock");
211 
212   LOG("setting event callbacks ...\n");
213   (void) memset(&callbacks, 0, sizeof(callbacks));
214   callbacks.ClassLoad = &ClassLoad;
215   err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
216   if (err != JVMTI_ERROR_NONE) {
217     LOG("Error in SetEventCallbacks %d/n", err);
218     return JNI_ERR;
219   }
220 
221   LOG("setting event callbacks done\nenabling ClassLoad event ...\n");
222   err = jvmti->SetEventNotificationMode(JVMTI_ENABLE, JVMTI_EVENT_CLASS_LOAD, NULL);
223   if (err != JVMTI_ERROR_NONE) {
224     LOG("Error in SetEventNotificationMode: %d\n", err);
225     return JNI_ERR;
226   }
227   LOG("the event enabled\n");
228 
229   return JNI_OK;
230 }
231 
232 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
233   return Agent_Initialize(jvm, options, reserved);
234 }
235 
236 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
237   return Agent_Initialize(jvm, options, reserved);
238 }
239 
240 }