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 extern "C" { 31 32 33 #define PASSED 0 34 #define STATUS_FAILED 2 35 36 static jvmtiEnv *jvmti = NULL; 37 static jint result = PASSED; 38 static jboolean printdump = JNI_FALSE; 39 40 #ifdef STATIC_BUILD 41 JNIEXPORT jint JNICALL Agent_OnLoad_clsldrclss00x(JavaVM *jvm, char *options, void *reserved) { 42 return Agent_Initialize(jvm, options, reserved); 43 } 44 JNIEXPORT jint JNICALL Agent_OnAttach_clsldrclss00x(JavaVM *jvm, char *options, void *reserved) { 45 return Agent_Initialize(jvm, options, reserved); 46 } 47 JNIEXPORT jint JNI_OnLoad_clsldrclss00x(JavaVM *jvm, char *options, void *reserved) { 48 return JNI_VERSION_1_8; 49 } 50 #endif 51 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) { 52 jint res; 53 54 if (options != NULL && strcmp(options, "printdump") == 0) { 55 printdump = JNI_TRUE; 56 } 57 58 res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_1_1); 59 if (res != JNI_OK || jvmti == NULL) { 60 printf("Wrong result of a valid call to GetEnv!\n"); 61 return JNI_ERR; 62 } 63 64 return JNI_OK; 65 } 66 67 JNIEXPORT jint JNICALL 68 Java_nsk_jvmti_unit_clsldrclss00x_check(JNIEnv *env, jclass appCls, jclass objCls) { 69 jvmtiError err; 70 jobject appClassloader; 71 jobject objClassloader; 72 jclass *classes; 73 jint classCount; 74 jboolean found; 75 jint i; 76 77 if (jvmti == NULL) { 78 printf("JVMTI client was not properly loaded!\n"); 79 return STATUS_FAILED; 80 } 81 82 err = jvmti->GetClassLoader(appCls, &appClassloader); 83 if (err != JVMTI_ERROR_NONE) { 84 printf("(GetClassLoader app) unexpected error: %s (%d)\n", 85 TranslateError(err), err); 86 result = STATUS_FAILED; 87 } 88 if (appClassloader == NULL) { 89 printf("(GetClassLoader app) unexpected loader - NULL\n"); 90 result = STATUS_FAILED; 91 } 92 93 err = jvmti->GetClassLoader(objCls, &objClassloader); 94 if (err != JVMTI_ERROR_NONE) { 95 printf("(GetClassLoader obj) unexpected error: %s (%d)\n", 96 TranslateError(err), err); 97 result = STATUS_FAILED; 98 } 99 if (objClassloader != NULL) { 100 printf("(GetClassLoader obj) unexpected loader - !NULL\n"); 101 result = STATUS_FAILED; 102 } 103 104 err = jvmti->GetClassLoaderClasses(appClassloader, &classCount, &classes); 105 if (err != JVMTI_ERROR_NONE) { 106 printf("Error (GetClassLoaderClasses app): %s (%d)\n", TranslateError(err), err); 107 result = STATUS_FAILED; 108 } 109 if (printdump) { 110 printf(">>> number of classes in app class loader: %d\n", classCount); 111 if (JNI_FALSE) { 112 for (i = 0; i < classCount; ++i) { 113 char *classSig; 114 jclass k = classes[i]; 115 err = jvmti->GetClassSignature(k, &classSig, NULL); 116 if (err != JVMTI_ERROR_NONE) { 117 printf("Error (getClassSignature): %s (%d)\n", TranslateError(err), err); 118 result = STATUS_FAILED; 119 } else { 120 printf(" %s\n", classSig); 121 err = jvmti->Deallocate((unsigned char*)classSig); 122 if (err != JVMTI_ERROR_NONE) { 123 printf("Error (Deallocate): %s (%d)\n", TranslateError(err), err); 124 result = STATUS_FAILED; 125 } 126 } 127 } 128 } 129 } 130 found = JNI_FALSE; 131 for (i = 0; i < classCount; ++i) { 132 jclass k = classes[i]; 133 if ( env->IsSameObject(k, appCls) ) { 134 if (printdump) { 135 printf(">>> found app class in app class loader\n"); 136 } 137 found = JNI_TRUE; 138 break; 139 } 140 } 141 if (!found) { 142 printf("Error: didn't find app class in app class loader\n"); 143 result = STATUS_FAILED; 144 } 145 146 err = jvmti->GetClassLoaderClasses(objClassloader, &classCount, &classes); 147 if (err != JVMTI_ERROR_NONE) { 148 printf("Error (GetClassLoaderClasses obj): %s (%d)\n", TranslateError(err), err); 149 result = STATUS_FAILED; 150 } 151 if (printdump) { 152 printf(">>> number of classes in bootstrap class loader: %d\n", classCount); 153 } 154 found = JNI_FALSE; 155 for (i = 0; i < classCount; ++i) { 156 jclass k = classes[i]; 157 if ( env->IsSameObject(k, objCls) ) { 158 if (printdump) { 159 printf(">>> found Object class in bootstrap class loader\n"); 160 } 161 found = JNI_TRUE; 162 break; 163 } 164 } 165 if (!found) { 166 printf("Error: didn't find Object class in bootstrap class loader\n"); 167 result = STATUS_FAILED; 168 } 169 170 if (printdump) { 171 printf(">>> ... done\n"); 172 } 173 174 return result; 175 } 176 177 }