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 
  29 #include "nsk_tools.h"
  30 #include "JVMTITools.h"
  31 #include "jvmti_tools.h"
  32 
  33 extern "C" {
  34 
  35 #define STATUS_FAILED 2
  36 #define PASSED 0
  37 
  38 static volatile jint result = PASSED;
  39 static volatile int compunload = 0;
  40 static volatile int class_unloaded = 0;
  41 static jvmtiEnv *jvmti = NULL;
  42 static jvmtiEventCallbacks callbacks;
  43 static jvmtiCapabilities caps;
  44 
  45 /** callback functions **/
  46 void JNICALL
  47 VMInit(jvmtiEnv *jvmti_env, JNIEnv *env, jthread thr) {
  48     NSK_DISPLAY0("VMInit event received\n\n");
  49 
  50     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GenerateEvents,
  51             jvmti_env, JVMTI_EVENT_COMPILED_METHOD_LOAD))) {
  52         NSK_COMPLAIN0("TEST FAILED: unable to generate events to represent the current state of the VM\n");
  53         result = STATUS_FAILED;
  54     }
  55 }
  56 
  57 void JNICALL
  58 CompiledMethodLoad(jvmtiEnv *jvmti_env, jmethodID method, jint code_size,
  59         const void* code_addr,  jint map_length, const jvmtiAddrLocationMap* map,
  60         const void* compile_info) {
  61     char *name;
  62     char *sig;
  63     char *generic;
  64 
  65     NSK_DISPLAY0("CompiledMethodLoad event received for:\n");
  66 
  67     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB5(GetMethodName,
  68             jvmti_env, method, &name, &sig, &generic))) {
  69         result = STATUS_FAILED;
  70         NSK_COMPLAIN0("TEST FAILURE: unable to obtain method info\n\n");
  71         return;
  72     }
  73     NSK_DISPLAY4("\tmethod: name=\"%s\" signature=\"%s\"\n\tcompiled code size=%d\n\tnumber of address location map entries=%d\n\n",
  74         name, sig, code_size, map_length);
  75 }
  76 
  77 void JNICALL
  78 CompiledMethodUnload(jvmtiEnv *jvmti_env, jmethodID method,
  79         const void* code_addr) {
  80     char *name;
  81     char *sig;
  82     jvmtiPhase phase;
  83     jvmtiError err;
  84 
  85     compunload++;
  86 
  87     /* jmethodID: Identifies a Java programming language method, initializer, or constructor.
  88      * jmethodIDs returned by JVM TI functions and events may be safely stored. However, if
  89      * the class is unloaded, they become invalid and must not be used. */
  90 
  91     NSK_DISPLAY0("CompiledMethodUnload event received\n");
  92     // Check for the case that the class has been unloaded
  93     err = jvmti_env->GetMethodName(method, &name, &sig, NULL);
  94     if (err == JVMTI_ERROR_NONE) {
  95         NSK_DISPLAY3("for: \tmethod: name=\"%s\" signature=\"%s\"\n\tnative address=0x%p\n",
  96           name, sig, code_addr);
  97         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)name);
  98         NSK_CPP_STUB2(Deallocate, jvmti_env, (unsigned char*)sig);
  99     } else {
 100         // The class metadata has been completely unloaded so the name is not available.
 101         NSK_DISPLAY0("for: \tmethod: name=<not available>\n");
 102     }
 103 
 104     // Count unloaded events
 105     class_unloaded++;
 106 
 107     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase,
 108             jvmti_env, &phase))) {
 109         result = STATUS_FAILED;
 110         NSK_COMPLAIN0("TEST FAILURE: unable to obtain phase of the VM execution\n");
 111         return;
 112     }
 113 
 114     if (phase != JVMTI_PHASE_LIVE) {
 115         result = STATUS_FAILED;
 116         NSK_COMPLAIN1("TEST FAILED: CompiledMethodUnload event received during non-live phase %s\n",
 117             TranslatePhase(phase));
 118     }
 119     else
 120         NSK_DISPLAY0("CHECK PASSED: CompiledMethodUnload event received during the live phase as expected\n\n");
 121 }
 122 /************************/
 123 
 124 JNIEXPORT jint JNICALL
 125 Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_check(
 126         JNIEnv *env, jobject obj) {
 127     if (!caps.can_generate_compiled_method_load_events)
 128         return PASSED;
 129 
 130     if (compunload == 0)
 131         NSK_DISPLAY0("Warning: no CompiledMethodUnload events\n\tthe test has no results\n");
 132 
 133     return result;
 134 }
 135 
 136 JNIEXPORT jint JNICALL
 137 Java_nsk_jvmti_CompiledMethodUnload_compmethunload001_unloaded(
 138         JNIEnv *env, jobject obj) {
 139     if (!caps.can_generate_compiled_method_load_events)
 140         return 1;
 141 
 142     return class_unloaded;
 143 }
 144 
 145 #ifdef STATIC_BUILD
 146 JNIEXPORT jint JNICALL Agent_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 147     return Agent_Initialize(jvm, options, reserved);
 148 }
 149 JNIEXPORT jint JNICALL Agent_OnAttach_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 150     return Agent_Initialize(jvm, options, reserved);
 151 }
 152 JNIEXPORT jint JNI_OnLoad_compmethunload001(JavaVM *jvm, char *options, void *reserved) {
 153     return JNI_VERSION_1_8;
 154 }
 155 #endif
 156 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 157     /* init framework and parse options */
 158     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 159         return JNI_ERR;
 160 
 161     /* create JVMTI environment */
 162     if (!NSK_VERIFY((jvmti =
 163             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 164         return JNI_ERR;
 165 
 166     /* add capability to generate compiled method events */
 167     memset(&caps, 0, sizeof(jvmtiCapabilities));
 168     caps.can_generate_compiled_method_load_events = 1;
 169     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities,
 170             jvmti, &caps)))
 171         return JNI_ERR;
 172 
 173     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetCapabilities,
 174             jvmti, &caps)))
 175         return JNI_ERR;
 176 
 177     if (!caps.can_generate_compiled_method_load_events)
 178         NSK_DISPLAY0("Warning: generation of compiled method events is not implemented\n");
 179 
 180     /* set event callback */
 181     NSK_DISPLAY0("setting event callbacks ...\n");
 182     (void) memset(&callbacks, 0, sizeof(callbacks));
 183     callbacks.VMInit = &VMInit;
 184     callbacks.CompiledMethodLoad = &CompiledMethodLoad;
 185     callbacks.CompiledMethodUnload = &CompiledMethodUnload;
 186     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetEventCallbacks,
 187             jvmti, &callbacks, sizeof(callbacks))))
 188         return JNI_ERR;
 189 
 190     NSK_DISPLAY0("setting event callbacks done\nenabling JVMTI events ...\n");
 191     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 192             jvmti, JVMTI_ENABLE, JVMTI_EVENT_VM_INIT, NULL)))
 193         return JNI_ERR;
 194     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 195             jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_LOAD, NULL)))
 196         return JNI_ERR;
 197     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB4(SetEventNotificationMode,
 198             jvmti, JVMTI_ENABLE, JVMTI_EVENT_COMPILED_METHOD_UNLOAD, NULL)))
 199         return JNI_ERR;
 200     NSK_DISPLAY0("enabling the events done\n\n");
 201 
 202     return JNI_OK;
 203 }
 204 
 205 }