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