1 /*
   2  * Copyright (c) 2007, 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 #include <string.h>
  24 #include "jvmti.h"
  25 #include "agent_common.h"
  26 #include "jni_tools.h"
  27 #include "jvmti_tools.h"
  28 #include "jvmti_FollowRefObjects.h"
  29 
  30 extern "C" {
  31 
  32 static jlong g_timeout = 0;
  33 
  34 /* ============================================================================= */
  35 
  36 /** Agent algorithm. */
  37 static void JNICALL
  38 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
  39     jvmtiError retCode;
  40     jlong tag;
  41     jint cnt;
  42     jobject * pObjs;
  43     jlong * pObjTags;
  44 
  45     printf(">>> Check that FollowReferences(), IterateThroughHeap(), GetTag(), SetTag() and GetObjectsWithTags() \n"
  46            "    return an error if env. doesn't possess can_tag_objects capability\n");
  47 
  48     retCode = NSK_CPP_STUB6(FollowReferences,
  49                               jvmti,
  50                               (jint) 0,                 /* heap filter */
  51                               NULL,                     /* class */
  52                               NULL,                     /* inital object */
  53                               &g_wrongHeapCallbacks,
  54                               (const void *) &g_fakeUserData);
  55 
  56     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  57         NSK_COMPLAIN1("FollowReferences() returned %i", retCode);
  58         nsk_jvmti_setFailStatus();
  59     }
  60 
  61     retCode = NSK_CPP_STUB5(IterateThroughHeap,
  62                               jvmti,
  63                               (jint) 0,                 /* heap filter */
  64                               NULL,                     /* class */
  65                               &g_wrongHeapCallbacks,
  66                               (const void *) &g_fakeUserData);
  67 
  68     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  69         NSK_COMPLAIN1("IterateThroughHeap() returned %i", retCode);
  70         nsk_jvmti_setFailStatus();
  71     }
  72 
  73     retCode = NSK_CPP_STUB3(GetTag,
  74                               jvmti,
  75                               (jobject) &g_wrongHeapCallbacks,
  76                               &tag);
  77 
  78     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  79         NSK_COMPLAIN1("GetTag() returned %i", retCode);
  80         nsk_jvmti_setFailStatus();
  81     }
  82 
  83     retCode = NSK_CPP_STUB3(SetTag,
  84                               jvmti,
  85                               (jobject) &g_wrongHeapCallbacks,
  86                               tag);
  87 
  88     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  89         NSK_COMPLAIN1("SetTag() returned %i", retCode);
  90         nsk_jvmti_setFailStatus();
  91     }
  92 
  93     retCode = NSK_CPP_STUB6(GetObjectsWithTags,
  94                               jvmti,
  95                               1,
  96                               &tag,
  97                               &cnt,
  98                               &pObjs,
  99                               &pObjTags);
 100 
 101     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
 102         NSK_COMPLAIN1("GetObjectsWithTags() returned %i", retCode);
 103         nsk_jvmti_setFailStatus();
 104     }
 105 
 106     printf(">>> Let debugee to finish\n");
 107     fflush(0);
 108 
 109     if (!NSK_VERIFY(nsk_jvmti_waitForSync(g_timeout))) {
 110         return;
 111     }
 112 
 113     if (!NSK_VERIFY(nsk_jvmti_resumeSync())) {
 114         return;
 115     }
 116 } /* agentProc */
 117 
 118 
 119 /* ============================================================================= */
 120 
 121 /** Agent library initialization. */
 122 #ifdef STATIC_BUILD
 123 JNIEXPORT jint JNICALL Agent_OnLoad_followref005(JavaVM *jvm, char *options, void *reserved) {
 124     return Agent_Initialize(jvm, options, reserved);
 125 }
 126 JNIEXPORT jint JNICALL Agent_OnAttach_followref005(JavaVM *jvm, char *options, void *reserved) {
 127     return Agent_Initialize(jvm, options, reserved);
 128 }
 129 JNIEXPORT jint JNI_OnLoad_followref005(JavaVM *jvm, char *options, void *reserved) {
 130     return JNI_VERSION_1_8;
 131 }
 132 #endif
 133 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 134     jvmtiEnv* jvmti = NULL;
 135 
 136     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) {
 137         return JNI_ERR;
 138     }
 139 
 140     g_timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 141 
 142     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) {
 143         return JNI_ERR;
 144     }
 145 
 146     {
 147         jvmtiCapabilities caps;
 148 
 149         memset(&caps, 0, sizeof(caps));
 150         /* Don't add can_tag_objects capability */
 151         if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
 152             return JNI_ERR;
 153         }
 154     }
 155 
 156     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) {
 157         return JNI_ERR;
 158     }
 159 
 160     return JNI_OK;
 161 } /* Agent_OnLoad */
 162 
 163 
 164 /* ============================================================================= */
 165 
 166 }