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