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