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 <stdlib.h>
 25 #include <string.h>
 26 #include <jvmti.h>
 27 #include "jvmti_common.h"
 28 
 29 extern "C" {
 30 
 31 #define STATUS_FAILED 2
 32 #define PASSED 0
 33 
 34 /* counter for the wrong NativeMethodBind events
 35    received during non-start or non-live phase */
 36 static volatile int wrongBindEv = 0;
 37 
 38 static volatile jint result = PASSED;
 39 static jvmtiEnv *jvmti = NULL;
 40 static jvmtiEventCallbacks callbacks;
 41 static jrawMonitorID counter_lock;
 42 
 43 /** callback functions **/
 44 void JNICALL
 45 NativeMethodBind(jvmtiEnv *jvmti, JNIEnv *jni, jthread thread,
 46                  jmethodID method, void *addr, void **new_addr) {
 47   jvmtiPhase phase;
 48   jvmtiError err;
 49   char *methNam = NULL, *methSig = NULL;
 50 
 51   RawMonitorLocker rml(jvmti, jni, counter_lock);
 52   err = jvmti->GetPhase(&phase);
 53   if (err != JVMTI_ERROR_NONE) {
 54     LOG(">>>> Error getting phase\n");
 55     result = STATUS_FAILED;
 56     return;
 57   }
 58 
 59   if (phase != JVMTI_PHASE_LIVE && phase != JVMTI_PHASE_START) {
 60     return;
 61   }
 62 
 63   err = jvmti->GetMethodName(method, &methNam, &methSig, NULL);
 64 
 65   if (err != JVMTI_ERROR_NONE) {
 66     result = STATUS_FAILED;
 67     LOG("TEST FAILED: unable to get method name during NativeMethodBind callback\n\n");
 68     return;
 69   } else {
 70     LOG("NativeMethodBind received for \"%s %s\"\n",
 71                  methNam, methSig);
 72   }
 73 
 74   if (methNam != NULL) {
 75     err = jvmti->Deallocate((unsigned char *) methNam);
 76     if (err != JVMTI_ERROR_NONE) {
 77       result = STATUS_FAILED;
 78       LOG("TEST FAILED: unable to deallocate memory pointed to method name\n\n");
 79     }
 80   }
 81 
 82   if (methSig != NULL) {
 83     err = jvmti->Deallocate((unsigned char *) methSig);
 84     if (err != JVMTI_ERROR_NONE) {
 85       result = STATUS_FAILED;
 86       LOG("TEST FAILED: unable to deallocate memory pointed to method signature\n\n");
 87     }
 88   }
 89 
 90 }
 91 
 92 void JNICALL
 93 VMDeath(jvmtiEnv *jvmti, JNIEnv *jni) {
 94   LOG("VMDeath event received\n");
 95 
 96   if (wrongBindEv != 0) {
 97     NSK_COMPLAIN1(
 98         "TEST FAILED: there are %d NativeMethodBind events\n"
 99         "sent during non-start or non-live phase of the VM execution\n",
100         wrongBindEv);
101   }
102 
103   if (result == STATUS_FAILED) {
104     jni->FatalError("Result is failed.");
105   }
106 }
107 /************************/
108 
109 /* dummy method used only to provoke NativeMethodBind events */
110 JNIEXPORT jint JNICALL
111 Java_nativemethbind02_nativeMethod(JNIEnv *jni, jobject obj) {
112   LOG("inside the nativeMethod()\n\n");
113 
114   return PASSED;
115 }
116 
117 #ifdef STATIC_BUILD
118 JNIEXPORT jint JNICALL Agent_OnLoad_nativemethbind02(JavaVM *jvm, char *options, void *reserved) {
119     return Agent_Initialize(jvm, options, reserved);
120 }
121 JNIEXPORT jint JNICALL Agent_OnAttach_nativemethbind02(JavaVM *jvm, char *options, void *reserved) {
122     return Agent_Initialize(jvm, options, reserved);
123 }
124 JNIEXPORT jint JNI_OnLoad_nativemethbind02(JavaVM *jvm, char *options, void *reserved) {
125     return JNI_VERSION_1_8;
126 }
127 #endif
128 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
129   jvmtiCapabilities caps;
130   jvmtiError err;
131   jint res;
132 
133   res = jvm->GetEnv((void **) &jvmti, JVMTI_VERSION_9);
134   if (res != JNI_OK || jvmti == NULL) {
135     LOG("Wrong result of a valid call to GetEnv!\n");
136     return JNI_ERR;
137   }
138 
139 
140   /* create a raw monitor */
141   counter_lock = create_raw_monitor(jvmti, "_counter_lock");
142 
143   /* add capability to generate compiled method events */
144   memset(&caps, 0, sizeof(jvmtiCapabilities));
145   caps.can_generate_native_method_bind_events = 1;
146 
147   // TODO Fix!!
148   err = jvmti->AddCapabilities(&caps);
149   if (err != JVMTI_ERROR_NONE) {
150     return JNI_ERR;
151   }
152 
153   err = jvmti->GetCapabilities(&caps);
154   if (err != JVMTI_ERROR_NONE) {
155     return JNI_ERR;
156   }
157   if (!caps.can_generate_native_method_bind_events)
158     LOG("Warning: generation of native method bind events is not implemented\n");
159 
160   /* set event callback */
161   LOG("setting event callbacks ...\n");
162   (void) memset(&callbacks, 0, sizeof(callbacks));
163   callbacks.NativeMethodBind = &NativeMethodBind;
164   callbacks.VMDeath = &VMDeath;
165   err = jvmti->SetEventCallbacks(&callbacks, sizeof(callbacks));
166   if (err != JVMTI_ERROR_NONE)
167     return JNI_ERR;
168 
169   LOG("setting event callbacks done\nenabling JVMTI events ...\n");
170   err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
171                                         JVMTI_EVENT_NATIVE_METHOD_BIND,
172                                         NULL);
173   if (err != JVMTI_ERROR_NONE) {
174     return JNI_ERR;
175   }
176   err = jvmti->SetEventNotificationMode(JVMTI_ENABLE,
177                                         JVMTI_EVENT_VM_DEATH,
178                                         NULL);
179   if (err != JVMTI_ERROR_NONE) {
180     return JNI_ERR;
181   }
182   LOG("enabling the events done\n\n");
183 
184   return JNI_OK;
185 }
186 
187 JNIEXPORT jint JNICALL Agent_OnLoad(JavaVM *jvm, char *options, void *reserved) {
188   return Agent_Initialize(jvm, options, reserved);
189 }
190 
191 JNIEXPORT jint JNICALL Agent_OnAttach(JavaVM *jvm, char *options, void *reserved) {
192   return Agent_Initialize(jvm, options, reserved);
193 }
194 
195 }