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 <string.h>
  25 #include "jvmti.h"
  26 #include "agent_common.h"
  27 #include "jni_tools.h"
  28 #include "jvmti_tools.h"
  29 
  30 extern "C" {
  31 
  32 /* ============================================================================= */
  33 
  34 static jlong timeout = 0;
  35 static int fakeUserData = 0, objCounter = 0;
  36 static jvmtiEnv* st_jvmti = NULL;
  37 static const char *storage_data = "local_storage_data";
  38 static void *storage_ptr = NULL;
  39 static const char* debugeeClassSignature = "Lnsk/jvmti/IterateOverObjectsReachableFromObject/iterobjreachobj004;";
  40 static const char* objectFieldName = "object";
  41 
  42 /* ============================================================================= */
  43 
  44 jvmtiIterationControl JNICALL
  45 objectReferenceCallback( jvmtiObjectReferenceKind reference_kind,
  46                          jlong  class_tag,
  47                          jlong  size,
  48                          jlong* tag_ptr,
  49                          jlong  referrer_tag,
  50                          jint   referrer_index,
  51                          void*  user_data) {
  52 
  53     objCounter++;
  54 
  55     if (!NSK_JVMTI_VERIFY(st_jvmti->SetEnvironmentLocalStorage(storage_data))) {
  56         nsk_jvmti_setFailStatus();
  57     }
  58 
  59     if (!NSK_JVMTI_VERIFY(st_jvmti->GetEnvironmentLocalStorage(&storage_ptr))) {
  60         nsk_jvmti_setFailStatus();
  61     }
  62 
  63     /*  Iterate over only first object */
  64     return JVMTI_ITERATION_ABORT;
  65 }
  66 
  67 /* ============================================================================= */
  68 
  69 /** Agent algorithm. */
  70 static void JNICALL
  71 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  72 
  73     NSK_DISPLAY0("Wait for debugee start\n");
  74     if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
  75         return;
  76 
  77     do {
  78         jclass debugeeClass = NULL;
  79         jfieldID objectField = NULL;
  80         jobject object = NULL;
  81 
  82         NSK_DISPLAY1("Find debugee class: %s\n", debugeeClassSignature);
  83         debugeeClass = nsk_jvmti_classBySignature(debugeeClassSignature);
  84         if (debugeeClass == NULL) {
  85             nsk_jvmti_setFailStatus();
  86             break;
  87         }
  88 
  89         NSK_DISPLAY1("Find static field in debugee class: %s\n", objectFieldName);
  90         if (!NSK_JNI_VERIFY(jni, (objectField = jni->GetStaticFieldID(
  91                 debugeeClass, objectFieldName, debugeeClassSignature)) != NULL)) {
  92             nsk_jvmti_setFailStatus();
  93             break;
  94         }
  95 
  96         NSK_DISPLAY1("Find value of static field in debugee class: %s\n", objectFieldName);
  97         if (!NSK_JNI_VERIFY(jni, (object =
  98                 jni->GetStaticObjectField(debugeeClass, objectField)) != NULL)) {
  99             nsk_jvmti_setFailStatus();
 100             break;
 101         }
 102 
 103         NSK_DISPLAY0("Calling IterateOverObjectsReachableFromObject with filter JVMTI_HEAP_OBJECT_EITHER\n");
 104         {
 105             if (!NSK_JVMTI_VERIFY(jvmti->IterateOverObjectsReachableFromObject(
 106                   object, objectReferenceCallback, &fakeUserData))) {
 107                 nsk_jvmti_setFailStatus();
 108             }
 109         }
 110 
 111         if (objCounter == 0) {
 112             NSK_COMPLAIN0("IterateOverObjectsReachableFromObject call had not visited any object\n");
 113             nsk_jvmti_setFailStatus();
 114         }
 115 
 116         if (storage_data != storage_ptr) {
 117             NSK_COMPLAIN2("Local storage address was corrupted: %p ,\n\texpected value: %p\n",
 118                              storage_ptr, storage_data);
 119             nsk_jvmti_setFailStatus();
 120         }
 121 
 122         if (strcmp(storage_data, (char *)storage_ptr) != 0) {
 123             NSK_COMPLAIN2("Local storage was corrupted: %s ,\n\texpected value: %s\n",
 124                              (char *)storage_ptr, storage_data );
 125             nsk_jvmti_setFailStatus();
 126         }
 127     } while (0);
 128 
 129     NSK_DISPLAY0("Let debugee to finish\n");
 130     if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
 131         return;
 132 }
 133 
 134 /* ============================================================================= */
 135 
 136 /** Agent library initialization. */
 137 #ifdef STATIC_BUILD
 138 JNIEXPORT jint JNICALL Agent_OnLoad_iterobjreachobj004(JavaVM *jvm, char *options, void *reserved) {
 139     return Agent_Initialize(jvm, options, reserved);
 140 }
 141 JNIEXPORT jint JNICALL Agent_OnAttach_iterobjreachobj004(JavaVM *jvm, char *options, void *reserved) {
 142     return Agent_Initialize(jvm, options, reserved);
 143 }
 144 JNIEXPORT jint JNI_OnLoad_iterobjreachobj004(JavaVM *jvm, char *options, void *reserved) {
 145     return JNI_VERSION_1_8;
 146 }
 147 #endif
 148 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 149     jvmtiEnv* jvmti = NULL;
 150 
 151     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
 152         return JNI_ERR;
 153 
 154     timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 155 
 156     if (!NSK_VERIFY((jvmti =
 157             nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
 158         return JNI_ERR;
 159 
 160     /* save pointer to environment to use it in callbacks */
 161     st_jvmti = jvmti;
 162 
 163     {
 164         jvmtiCapabilities caps;
 165 
 166         memset(&caps, 0, sizeof(caps));
 167         caps.can_tag_objects = 1;
 168         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 169             return JNI_ERR;
 170         }
 171     }
 172 
 173     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
 174         return JNI_ERR;
 175 
 176     return JNI_OK;
 177 }
 178 
 179 /* ============================================================================= */
 180 
 181 }