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