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 "jni_tools.h"
  27 #include "agent_common.h"
  28 #include "jvmti_tools.h"
  29 
  30 #define PASSED 0
  31 #define STATUS_FAILED 2
  32 #define SAMPLE_TAG ((jlong) 111111)
  33 
  34 extern "C" {
  35 
  36 /* ========================================================================== */
  37 
  38 /* scaffold objects */
  39 static jlong timeout = 0;
  40 
  41 /* test objects */
  42 static jobject testedObject = NULL;
  43 
  44 /* ========================================================================== */
  45 
  46 static int prepare(JNIEnv* jni) {
  47     const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA04/ma04t001";
  48     const char* FIELD_NAME = "testedObject";
  49     const char* FIELD_SIGNATURE = "Ljava/lang/Object;";
  50     jclass cls = NULL;
  51     jfieldID fid = NULL;
  52 
  53     NSK_DISPLAY0("Obtain tested object from a static field of debugee class\n");
  54 
  55     NSK_DISPLAY1("Find class: %s\n", CLASS_NAME);
  56     if (!NSK_JNI_VERIFY(jni, (cls =
  57             NSK_CPP_STUB2(FindClass, jni, CLASS_NAME)) != NULL))
  58         return NSK_FALSE;
  59 
  60     NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE);
  61     if (!NSK_JNI_VERIFY(jni, (fid =
  62             NSK_CPP_STUB4(GetStaticFieldID, jni, cls,
  63                 FIELD_NAME, FIELD_SIGNATURE)) != NULL))
  64         return NSK_FALSE;
  65 
  66     if (!NSK_JNI_VERIFY(jni, (testedObject =
  67             NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fid)) != NULL))
  68         return NSK_FALSE;
  69 
  70     if (!NSK_JNI_VERIFY(jni, (testedObject =
  71             NSK_CPP_STUB2(NewGlobalRef, jni, testedObject)) != NULL))
  72         return NSK_FALSE;
  73 
  74     return NSK_TRUE;
  75 }
  76 
  77 /* ========================================================================== */
  78 
  79 /** Agent algorithm. */
  80 static void JNICALL
  81 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  82     jlong tag = -1;
  83     char buffer[32];
  84 
  85     if (!nsk_jvmti_waitForSync(timeout))
  86         return;
  87 
  88     if (!prepare(jni)) {
  89         nsk_jvmti_setFailStatus();
  90         return;
  91     }
  92 
  93     NSK_DISPLAY0("Testcase #1: check that testedObject is not tagged \n");
  94     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
  95         nsk_jvmti_setFailStatus();
  96         return;
  97     }
  98     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
  99     if (tag != 0) {
 100         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 101             jlong_to_string(tag, buffer));
 102         nsk_jvmti_setFailStatus();
 103     }
 104     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject,
 105             SAMPLE_TAG))) {
 106         nsk_jvmti_setFailStatus();
 107         return;
 108     }
 109     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 110         return;
 111     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 112         return;
 113 
 114     NSK_DISPLAY0("Testcase #2: check that testedObject is tagged correctly\n");
 115     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 116         nsk_jvmti_setFailStatus();
 117         return;
 118     }
 119     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 120     if (tag != SAMPLE_TAG) {
 121         if (tag == 0) {
 122             NSK_COMPLAIN0("testedObject not tagged\n");
 123         } else {
 124             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 125                 jlong_to_string(SAMPLE_TAG, buffer));
 126             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 127         }
 128         nsk_jvmti_setFailStatus();
 129     }
 130     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 131         return;
 132     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 133         return;
 134 
 135     NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
 136     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 137         nsk_jvmti_setFailStatus();
 138         return;
 139     }
 140     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 141     if (tag != SAMPLE_TAG) {
 142         if (tag == 0) {
 143             NSK_COMPLAIN0("testedObject not tagged\n");
 144         } else {
 145             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 146                 jlong_to_string(SAMPLE_TAG, buffer));
 147             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 148         }
 149         nsk_jvmti_setFailStatus();
 150     }
 151     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject, (jlong)0))) {
 152         nsk_jvmti_setFailStatus();
 153         return;
 154     }
 155     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 156         return;
 157     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 158         return;
 159 
 160     NSK_DISPLAY0("Testcase #4: check that testedObject is not tagged \n");
 161     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 162         nsk_jvmti_setFailStatus();
 163         return;
 164     }
 165     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 166     if (tag != 0) {
 167         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 168             jlong_to_string(tag, buffer));
 169         nsk_jvmti_setFailStatus();
 170     }
 171     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 172         return;
 173     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 174         return;
 175 
 176     NSK_DISPLAY0("Testcase #5: check that testedObject is not tagged\n");
 177     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
 178         nsk_jvmti_setFailStatus();
 179         return;
 180     }
 181     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 182     if (tag != 0) {
 183         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 184             jlong_to_string(tag, buffer));
 185         nsk_jvmti_setFailStatus();
 186     }
 187     NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedObject));
 188 
 189     if (!nsk_jvmti_resumeSync())
 190         return;
 191 }
 192 
 193 /* ========================================================================== */
 194 
 195 /** Agent library initialization. */
 196 #ifdef STATIC_BUILD
 197 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 198     return Agent_Initialize(jvm, options, reserved);
 199 }
 200 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 201     return Agent_Initialize(jvm, options, reserved);
 202 }
 203 JNIEXPORT jint JNI_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
 204     return JNI_VERSION_1_8;
 205 }
 206 #endif
 207 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 208     jvmtiEnv* jvmti = NULL;
 209     jvmtiEventCallbacks callbacks;
 210     jvmtiCapabilities caps;
 211 
 212     NSK_DISPLAY0("Agent_OnLoad\n");
 213 
 214     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 215         return JNI_ERR;
 216 
 217     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 218 
 219     if (!NSK_VERIFY((jvmti =
 220             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 221         return JNI_ERR;
 222 
 223     memset(&caps, 0, sizeof(caps));
 224     caps.can_tag_objects = 1;
 225     if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 226         return JNI_ERR;
 227     }
 228 
 229     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 230         return JNI_ERR;
 231 
 232     memset(&callbacks, 0, sizeof(callbacks));
 233     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 234         return JNI_ERR;
 235 
 236     return JNI_OK;
 237 }
 238 
 239 /* ========================================================================== */
 240 
 241 }