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 <stdarg.h>
  26 #include <stdlib.h>
  27 #include <string.h>
  28 
  29 #include <jvmti.h>
  30 #include "agent_common.h"
  31 
  32 #include "nsk_tools.h"
  33 #include "JVMTITools.h"
  34 #include "jvmti_tools.h"
  35 
  36 extern "C" {
  37 
  38 #define PASSED  0
  39 #define STATUS_FAILED  2
  40 
  41 const char* CLASS_NAME = "nsk/jvmti/ClassFileLoadHook/classfloadhk001";
  42 
  43 static jint result = STATUS_FAILED;
  44 static jvmtiEnv *jvmti = NULL;
  45 static jvmtiEventCallbacks callbacks;
  46 
  47 /** callback functions **/
  48 void JNICALL
  49 ClassFileLoadHook(jvmtiEnv *jvmti_env, JNIEnv *env, jclass class_beeing_redefined,
  50         jobject loader, const char* name, jobject protection_domain,
  51         jint class_data_len, const unsigned char* class_data,
  52         jint *new_class_data_len, unsigned char** new_class_data) {
  53 
  54     if (name != NULL && (strcmp(name, CLASS_NAME) == 0)) {
  55         NSK_DISPLAY1("CHECK PASSED: ClassFileLoadHook event received for the class \"%s\" as expected\n",
  56             name);
  57         result = PASSED;
  58     }
  59 }
  60 /************************/
  61 
  62 JNIEXPORT jint JNICALL
  63 Java_nsk_jvmti_ClassFileLoadHook_classfloadhk001_check(
  64         JNIEnv *env, jobject obj) {
  65     if (result == STATUS_FAILED)
  66         NSK_COMPLAIN1("TEST FAILED: no JVMTI_EVENT_CLASS_FILE_LOAD_HOOK event for the class \"%s\"\n",
  67             CLASS_NAME);
  68 
  69     return result;
  70 }
  71 
  72 #ifdef STATIC_BUILD
  73 JNIEXPORT jint JNICALL Agent_OnLoad_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {
  74     return Agent_Initialize(jvm, options, reserved);
  75 }
  76 JNIEXPORT jint JNICALL Agent_OnAttach_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {
  77     return Agent_Initialize(jvm, options, reserved);
  78 }
  79 JNIEXPORT jint JNI_OnLoad_classfloadhk001(JavaVM *jvm, char *options, void *reserved) {
  80     return JNI_VERSION_1_8;
  81 }
  82 #endif
  83 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
  84     /* init framework and parse options */
  85     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
  86         return JNI_ERR;
  87 
  88     /* create JVMTI environment */
  89     if (!NSK_VERIFY((jvmti =
  90             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
  91         return JNI_ERR;
  92 
  93     NSK_DISPLAY0("setting event callbacks ...\n");
  94     (void) memset(&callbacks, 0, sizeof(callbacks));
  95     callbacks.ClassFileLoadHook = &ClassFileLoadHook;
  96     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
  97             jvmti, &callbacks, sizeof(callbacks))))
  98         return JNI_ERR;
  99 
 100     NSK_DISPLAY0("setting event callbacks done\nenabling ClassFileLoadHook event ...\n");
 101     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 102             jvmti, JVMTI_ENABLE, JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
 103         return JNI_ERR;
 104     NSK_DISPLAY0("enabling ClassFileLoadHook event done\n");
 105 
 106     return JNI_OK;
 107 }
 108 
 109 }