1 /*
   2  * Copyright (c) 2004, 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 "agent_common.h"
  28 #include "jni_tools.h"
  29 #include "jvmti_tools.h"
  30 
  31 extern "C" {
  32 
  33 
  34 /* ============================================================================= */
  35 
  36 static jlong timeout = 0;
  37 static jvmtiEnv *jvmti_2;
  38 
  39 #define STATUS_FAIL     97
  40 #define EVENTS_COUNT    2
  41 
  42 static jvmtiEvent events[EVENTS_COUNT] = {
  43     JVMTI_EVENT_VM_INIT,
  44     JVMTI_EVENT_VM_DEATH
  45 };
  46 
  47 /* storage data */
  48 
  49 #define STORAGE_DATA_SIZE       1024
  50 #define STORAGE_DATA_CHAR       'X'
  51 
  52 typedef struct _StorageStructure {
  53     char data[STORAGE_DATA_SIZE];
  54 } StorageStructure;
  55 
  56 static StorageStructure storageData;
  57 static StorageStructure* initialStorage = &storageData;
  58 
  59 
  60 /* ============================================================================= */
  61 
  62 /** Fill storage data with given char */
  63 static void fillEnvStorage(StorageStructure* storage) {
  64     NSK_DISPLAY2("Fill storage data with char %c for size: %d bytes\n",
  65                 (char)STORAGE_DATA_CHAR, (int)STORAGE_DATA_SIZE);
  66     memset(storage->data, STORAGE_DATA_CHAR, STORAGE_DATA_SIZE);
  67     NSK_DISPLAY0("  ... ok\n");
  68 }
  69 
  70 /**
  71  * Check JVMTI environment local storage.
  72  * @returns NSK_FALSE if any error occured.
  73  */
  74 static int checkEnvStorage(jvmtiEnv* jvmti, const char where[]) {
  75     void* storage = NULL;
  76 
  77     NSK_DISPLAY0("Calling GetEnvironmentLocalStorage():");
  78     if (!NSK_JVMTI_VERIFY(
  79             NSK_CPP_STUB2(GetEnvironmentLocalStorage, jvmti, &storage))) {
  80         return NSK_FALSE;
  81     }
  82     NSK_DISPLAY1("  ... got storage: 0x%p\n", (void*)storage);
  83 
  84     if (storage != NULL) {
  85         NSK_COMPLAIN2("GetEnvironmentLocalStorage() returned NOT NULL storage in %s:\n"
  86                       "#   storage pointer: 0x%p\n",
  87                         where, (void*)storage);
  88         return NSK_FALSE;
  89     }
  90 
  91     return NSK_TRUE;
  92 }
  93 
  94 /* ============================================================================= */
  95 
  96 /** Agent algorithm. */
  97 static void JNICALL
  98 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  99     NSK_DISPLAY0("Wait for debugee to become ready\n");
 100     if (!nsk_jvmti_waitForSync(timeout))
 101         return;
 102 
 103     NSK_DISPLAY0("CASE #3: Check local storage in agent thread for second JVMTI env.\n");
 104     if (!checkEnvStorage(jvmti, "agent thread")) {
 105         nsk_jvmti_setFailStatus();
 106     }
 107 
 108     NSK_DISPLAY0("Let debugee to finish\n");
 109     if (!nsk_jvmti_resumeSync())
 110         return;
 111 }
 112 
 113 jthread getEnvThread(JNIEnv *env) {
 114     jclass thrClass;
 115     jmethodID cid;
 116     jthread res;
 117 
 118     thrClass = NSK_CPP_STUB2(FindClass, env, "java/lang/Thread");
 119     cid = NSK_CPP_STUB4(GetMethodID, env, thrClass, "<init>", "()V");
 120     res = NSK_CPP_STUB3(NewObject, env, thrClass, cid);
 121     return res;
 122 }
 123 
 124 /* ============================================================================= */
 125 
 126 /**
 127  * Callback for VM_INIT event.
 128  */
 129 JNIEXPORT void JNICALL
 130 callbackVMInit(jvmtiEnv* jvmti, JNIEnv* jni, jthread thread) {
 131 
 132     NSK_DISPLAY0("CASE #2: Check local storage in VM_INIT callback for second JVMTI env.\n");
 133     if (!checkEnvStorage(jvmti, "VM_INIT callback")) {
 134         nsk_jvmti_setFailStatus();
 135     }
 136 
 137     NSK_DISPLAY0("Set agentProc for second JVMTI env.\n");
 138     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 139         nsk_jvmti_setFailStatus();
 140 }
 141 
 142 /**
 143  * Callback for VM_DEATH event.
 144  */
 145 JNIEXPORT void JNICALL
 146 callbackVMDeath(jvmtiEnv* jvmti, JNIEnv* jni) {
 147     int success = NSK_TRUE;
 148 
 149     NSK_DISPLAY0("CASE #4: Check local storage in VM_DEATH callback for second JVMTI env.\n");
 150     success = checkEnvStorage(jvmti, "VM_DEATH callback");
 151 
 152     NSK_DISPLAY1("Disable events: %d events\n", EVENTS_COUNT);
 153     if (!nsk_jvmti_enableEvents(JVMTI_DISABLE, EVENTS_COUNT, events, NULL)) {
 154         success = NSK_FALSE;
 155     } else {
 156         NSK_DISPLAY0("  ... disabled\n");
 157     }
 158 
 159     if (!success) {
 160         NSK_DISPLAY1("Exit with FAIL exit status: %d\n", STATUS_FAIL);
 161         NSK_BEFORE_TRACE(exit(STATUS_FAIL));
 162     }
 163 }
 164 
 165 /* ============================================================================= */
 166 
 167 /** Agent library initialization. */
 168 #ifdef STATIC_BUILD
 169 JNIEXPORT jint JNICALL Agent_OnLoad_gf06t001(JavaVM *jvm, char *options, void *reserved) {
 170     return Agent_Initialize(jvm, options, reserved);
 171 }
 172 JNIEXPORT jint JNICALL Agent_OnAttach_gf06t001(JavaVM *jvm, char *options, void *reserved) {
 173     return Agent_Initialize(jvm, options, reserved);
 174 }
 175 JNIEXPORT jint JNI_OnLoad_gf06t001(JavaVM *jvm, char *options, void *reserved) {
 176     return JNI_VERSION_1_8;
 177 }
 178 #endif
 179 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 180     jint res;
 181     jvmtiEnv* jvmti_1 = NULL;
 182 
 183     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 184         return JNI_ERR;
 185 
 186     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 187 
 188     NSK_DISPLAY0("Create first JVMTI env.\n");
 189     res = jvm->GetEnv((void **) &jvmti_1, JVMTI_VERSION_1_1);
 190     if (res < 0) {
 191         NSK_COMPLAIN0("Wrong result of a valid call to GetEnv!\n");
 192         return JNI_ERR;
 193     }
 194 
 195     NSK_DISPLAY1("Set local storage in JVM_OnLoad() for first JVMTI env: 0x%p\n", (void*)initialStorage);
 196     if (!NSK_JVMTI_VERIFY(
 197             NSK_CPP_STUB2(SetEnvironmentLocalStorage, jvmti_1, initialStorage))) {
 198         return JNI_ERR;
 199     }
 200     NSK_DISPLAY0("  ... ok\n");
 201 
 202     /* Create second environment */
 203     if (!NSK_VERIFY((jvmti_2 =
 204             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 205         return JNI_ERR;
 206 
 207     /* Set callbacks for second environment */
 208     {
 209         jvmtiEventCallbacks eventCallbacks;
 210 
 211         memset(&eventCallbacks, 0, sizeof(eventCallbacks));
 212         eventCallbacks.VMInit = callbackVMInit;
 213         eventCallbacks.VMDeath = callbackVMDeath;
 214         if (!NSK_JVMTI_VERIFY(
 215                 NSK_CPP_STUB3(SetEventCallbacks, jvmti_2,
 216                                     &eventCallbacks, sizeof(eventCallbacks)))) {
 217             return JNI_ERR;
 218         }
 219 
 220     }
 221 
 222 
 223     NSK_DISPLAY1("Prepare storage data at pointer: 0x%p\n", initialStorage);
 224     fillEnvStorage(initialStorage);
 225 
 226     NSK_DISPLAY0("CASE #1: Check local storage in JVM_OnLoad() for second JVMTI env.\n");
 227     if (!checkEnvStorage(jvmti_2, "JVM_OnLoad()")) {
 228         nsk_jvmti_setFailStatus();
 229     }
 230 
 231     NSK_DISPLAY1("Enable events: %d events\n", EVENTS_COUNT);
 232     if (nsk_jvmti_enableEvents(JVMTI_ENABLE, EVENTS_COUNT, events, NULL)) {
 233         NSK_DISPLAY0("  ... enabled\n");
 234     }
 235 
 236     return JNI_OK;
 237 }
 238 
 239 /* ============================================================================= */
 240 
 241 }