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) 222222)
  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 = jni->FindClass(CLASS_NAME)) != NULL))
  57         return NSK_FALSE;
  58 
  59     NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE);
  60     if (!NSK_JNI_VERIFY(jni, (fid =
  61             jni->GetStaticFieldID(cls, FIELD_NAME, FIELD_SIGNATURE)) != NULL))
  62         return NSK_FALSE;
  63 
  64     if (!NSK_JNI_VERIFY(jni, (testedObject = jni->GetStaticObjectField(cls, fid)) != NULL))
  65         return NSK_FALSE;
  66 
  67     if (!NSK_JNI_VERIFY(jni, (testedObject = jni->NewGlobalRef(testedObject)) != NULL))
  68         return NSK_FALSE;
  69 
  70     return NSK_TRUE;
  71 }
  72 
  73 /* ========================================================================== */
  74 
  75 /** Agent algorithm. */
  76 static void JNICALL
  77 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  78     jlong tag = -1;
  79     char buffer[32];
  80 
  81     if (!nsk_jvmti_waitForSync(timeout))
  82         return;
  83 
  84     if (!prepare(jni)) {
  85         nsk_jvmti_setFailStatus();
  86         return;
  87     }
  88 
  89     NSK_DISPLAY0("Testcase #1: check that testedObject is not tagged\n");
  90     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
  91         nsk_jvmti_setFailStatus();
  92         return;
  93     }
  94     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
  95     if (tag != 0) {
  96         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
  97             jlong_to_string(tag, buffer));
  98         nsk_jvmti_setFailStatus();
  99     }
 100     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 101         return;
 102     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 103         return;
 104 
 105     NSK_DISPLAY0("Testcase #2: check that testedObject is not tagged\n");
 106     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 107         nsk_jvmti_setFailStatus();
 108         return;
 109     }
 110     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 111     if (tag != 0) {
 112         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 113             jlong_to_string(tag, buffer));
 114         nsk_jvmti_setFailStatus();
 115     }
 116     if (!NSK_JVMTI_VERIFY(jvmti->SetTag(testedObject, SAMPLE_TAG))) {
 117         nsk_jvmti_setFailStatus();
 118         return;
 119     }
 120     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 121         return;
 122     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 123         return;
 124 
 125     NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
 126     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 127         nsk_jvmti_setFailStatus();
 128         return;
 129     }
 130     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 131     if (tag != SAMPLE_TAG) {
 132         if (tag == 0) {
 133             NSK_COMPLAIN0("testedObject not tagged\n");
 134         } else {
 135             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 136                 jlong_to_string(SAMPLE_TAG, buffer));
 137             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 138         }
 139         nsk_jvmti_setFailStatus();
 140     }
 141     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 142         return;
 143     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 144         return;
 145 
 146     NSK_DISPLAY0("Testcase #4: check that testedObject is tagged correctly\n");
 147     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 148         nsk_jvmti_setFailStatus();
 149         return;
 150     }
 151     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 152     if (tag != SAMPLE_TAG) {
 153         if (tag == 0) {
 154             NSK_COMPLAIN0("testedObject not tagged\n");
 155         } else {
 156             NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
 157                 jlong_to_string(SAMPLE_TAG, buffer));
 158             NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
 159         }
 160         nsk_jvmti_setFailStatus();
 161     }
 162     if (!NSK_JVMTI_VERIFY(jvmti->SetTag(testedObject, (jlong)0))) {
 163         nsk_jvmti_setFailStatus();
 164         return;
 165     }
 166     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 167         return;
 168     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
 169         return;
 170 
 171     NSK_DISPLAY0("Testcase #5: check that testedObject is not tagged\n");
 172     if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
 173         nsk_jvmti_setFailStatus();
 174         return;
 175     }
 176     NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
 177     if (tag != 0) {
 178         NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
 179             jlong_to_string(tag, buffer));
 180         nsk_jvmti_setFailStatus();
 181     }
 182     NSK_TRACE(jni->DeleteGlobalRef(testedObject));
 183 
 184     if (!nsk_jvmti_resumeSync())
 185         return;
 186 }
 187 
 188 /* ========================================================================== */
 189 
 190 /** Agent library initialization. */
 191 #ifdef STATIC_BUILD
 192 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001a(JavaVM *jvm, char *options, void *reserved) {
 193     return Agent_Initialize(jvm, options, reserved);
 194 }
 195 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001a(JavaVM *jvm, char *options, void *reserved) {
 196     return Agent_Initialize(jvm, options, reserved);
 197 }
 198 JNIEXPORT jint JNI_OnLoad_ma04t001a(JavaVM *jvm, char *options, void *reserved) {
 199     return JNI_VERSION_1_8;
 200 }
 201 #endif
 202 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 203     jvmtiEnv* jvmti = NULL;
 204     jvmtiEventCallbacks callbacks;
 205     jvmtiCapabilities caps;
 206 
 207     NSK_DISPLAY0("Agent_OnLoad\n");
 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     memset(&caps, 0, sizeof(caps));
 219     caps.can_tag_objects = 1;
 220     if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 221         return JNI_ERR;
 222     }
 223 
 224     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 225         return JNI_ERR;
 226 
 227     memset(&callbacks, 0, sizeof(callbacks));
 228     if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
 229         return JNI_ERR;
 230 
 231     return JNI_OK;
 232 }
 233 
 234 /* ========================================================================== */
 235 
 236 }