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 "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* ========================================================================== */
  33 
  34 /* scaffold objects */
  35 static jlong timeout = 0;
  36 
  37 /* test objects */
  38 static jrawMonitorID access_lock;
  39 static jvmtiPhase phase;
  40 
  41 /* ========================================================================== */
  42 
  43 /* Check SetVerboseFlag function
  44  */
  45 static int checkSetVerboseFlag(jvmtiEnv *jvmti) {
  46     if (!NSK_JVMTI_VERIFY(
  47             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_OTHER, JNI_TRUE)))
  48         return NSK_FALSE;
  49 
  50     if (!NSK_JVMTI_VERIFY(
  51             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_OTHER, JNI_FALSE)))
  52         return NSK_FALSE;
  53 
  54     if (!NSK_JVMTI_VERIFY(
  55             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_GC, JNI_TRUE)))
  56         return NSK_FALSE;
  57 
  58     if (!NSK_JVMTI_VERIFY(
  59             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_GC, JNI_FALSE)))
  60         return NSK_FALSE;
  61 
  62     if (!NSK_JVMTI_VERIFY(
  63             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_CLASS, JNI_TRUE)))
  64         return NSK_FALSE;
  65 
  66     if (!NSK_JVMTI_VERIFY(
  67             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_CLASS, JNI_FALSE)))
  68         return NSK_FALSE;
  69 
  70     if (!NSK_JVMTI_VERIFY(
  71             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_JNI, JNI_TRUE)))
  72         return NSK_FALSE;
  73 
  74     if (!NSK_JVMTI_VERIFY(
  75             NSK_CPP_STUB3(SetVerboseFlag, jvmti, JVMTI_VERBOSE_JNI, JNI_FALSE)))
  76         return NSK_FALSE;
  77 
  78     return NSK_TRUE;
  79 }
  80 
  81 /* ========================================================================== */
  82 
  83 void JNICALL
  84 VMInit(jvmtiEnv *jvmti, JNIEnv* jni, jthread thread) {
  85 
  86     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase, jvmti, &phase)))
  87         nsk_jvmti_setFailStatus();
  88 
  89     NSK_DISPLAY1("Phase: %s\n", TranslatePhase(phase));
  90 
  91     /* testcase #3: check SetVerboseFlag in VMInit */
  92     NSK_DISPLAY0("Testcase #3: check SetVerboseFlag in VMInit\n");
  93     if (!checkSetVerboseFlag(jvmti))
  94         nsk_jvmti_setFailStatus();
  95 }
  96 
  97 void JNICALL
  98 ClassFileLoadHook(jvmtiEnv *jvmti, JNIEnv *jni,
  99                   jclass class_being_redefined,
 100                   jobject loader, const char* name,
 101                   jobject protection_domain,
 102                   jint class_data_len,
 103                   const unsigned char* class_data,
 104                   jint* new_class_data_len,
 105                   unsigned char** new_class_data) {
 106     jvmtiPhase curr_phase;
 107 
 108     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorEnter, jvmti, access_lock)))
 109         nsk_jvmti_setFailStatus();
 110 
 111     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase, jvmti, &curr_phase)))
 112         nsk_jvmti_setFailStatus();
 113 
 114     if (phase != curr_phase) {
 115         phase = curr_phase;
 116         NSK_DISPLAY1("Phase: %s\n", TranslatePhase(phase));
 117 
 118         /* testcase #2: check SetVerboseFlag in ClassFileLoadHook */
 119         NSK_DISPLAY0("Testcase #2: check SetVerboseFlag in ClassFileLoadHook\n");
 120         if (!checkSetVerboseFlag(jvmti))
 121             nsk_jvmti_setFailStatus();
 122     }
 123 
 124     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(RawMonitorExit, jvmti, access_lock)))
 125         nsk_jvmti_setFailStatus();
 126 }
 127 
 128 /* ========================================================================== */
 129 
 130 /* agent algorithm */
 131 static void JNICALL
 132 agentProc(jvmtiEnv *jvmti, JNIEnv* jni, void* arg) {
 133 
 134     /* wait for debuggee start */
 135     if (!nsk_jvmti_waitForSync(timeout))
 136         return;
 137 
 138     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase, jvmti, &phase)))
 139         nsk_jvmti_setFailStatus();
 140 
 141     NSK_DISPLAY1("Phase: %s\n", TranslatePhase(phase));
 142 
 143     /* testcase #4: check SetVerboseFlag in agentProc */
 144     NSK_DISPLAY0("Testcase #4: check SetVerboseFlag in agentProc\n");
 145     if (!checkSetVerboseFlag(jvmti))
 146         nsk_jvmti_setFailStatus();
 147 
 148     /* resume debugee after last sync */
 149     if (!nsk_jvmti_resumeSync())
 150         return;
 151 }
 152 
 153 /* ========================================================================== */
 154 
 155 /* agent library initialization */
 156 #ifdef STATIC_BUILD
 157 JNIEXPORT jint JNICALL Agent_OnLoad_setvrbflag001(JavaVM *jvm, char *options, void *reserved) {
 158     return Agent_Initialize(jvm, options, reserved);
 159 }
 160 JNIEXPORT jint JNICALL Agent_OnAttach_setvrbflag001(JavaVM *jvm, char *options, void *reserved) {
 161     return Agent_Initialize(jvm, options, reserved);
 162 }
 163 JNIEXPORT jint JNI_OnLoad_setvrbflag001(JavaVM *jvm, char *options, void *reserved) {
 164     return JNI_VERSION_1_8;
 165 }
 166 #endif
 167 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 168     jvmtiEnv *jvmti = NULL;
 169     jvmtiEventCallbacks callbacks;
 170 
 171     /* init framework and parse options */
 172     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 173         return JNI_ERR;
 174 
 175     timeout = nsk_jvmti_getWaitTime() * 60000;
 176     NSK_DISPLAY1("Timeout: %d msc\n", (int)timeout);
 177 
 178     /* create JVMTI environment */
 179     if (!NSK_VERIFY((jvmti =
 180             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 181         return JNI_ERR;
 182 
 183     /* Create data access lock */
 184     if (!NSK_JVMTI_VERIFY(
 185             NSK_CPP_STUB3(CreateRawMonitor, jvmti,
 186                 "_access_lock", &access_lock)))
 187         return JNI_ERR;
 188 
 189     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(GetPhase, jvmti, &phase)))
 190         return JNI_ERR;
 191 
 192     NSK_DISPLAY1("Phase: %s\n", TranslatePhase(phase));
 193 
 194     /* testcase #1: check SetVerboseFlag in Agent_OnLoad */
 195     NSK_DISPLAY0("Testcase #1: check SetVerboseFlag in Agent_OnLoad\n");
 196     if (!checkSetVerboseFlag(jvmti))
 197         nsk_jvmti_setFailStatus();
 198 
 199     memset(&callbacks, 0, sizeof(callbacks));
 200     callbacks.VMInit = &VMInit;
 201     callbacks.ClassFileLoadHook = &ClassFileLoadHook;
 202     if (!NSK_JVMTI_VERIFY(
 203             NSK_CPP_STUB3(SetEventCallbacks, jvmti,
 204                 &callbacks, sizeof(callbacks))))
 205         return JNI_ERR;
 206 
 207     /* enable VMInit event */
 208     if (!NSK_JVMTI_VERIFY(
 209             NSK_CPP_STUB4(SetEventNotificationMode, jvmti, JVMTI_ENABLE,
 210                 JVMTI_EVENT_VM_INIT, NULL)))
 211         return JNI_ERR;
 212 
 213     /* enable ClassFileLoadHook event */
 214     if (!NSK_JVMTI_VERIFY(
 215             NSK_CPP_STUB4(SetEventNotificationMode, jvmti, JVMTI_ENABLE,
 216                 JVMTI_EVENT_CLASS_FILE_LOAD_HOOK, NULL)))
 217         return JNI_ERR;
 218 
 219     /* register agent proc and arg */
 220     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 221         return JNI_ERR;
 222 
 223     return JNI_OK;
 224 }
 225 
 226 /* ========================================================================== */
 227 
 228 }