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 = jvmti->FollowReferences((jint) 0,                 /* heap filter */
  49                                       NULL,                     /* class */
  50                                       NULL,                     /* inital object */
  51                                       &g_wrongHeapCallbacks,
  52                                       (const void *) &g_fakeUserData);
  53 
  54     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  55         NSK_COMPLAIN1("FollowReferences() returned %i", retCode);
  56         nsk_jvmti_setFailStatus();
  57     }
  58 
  59     retCode = jvmti->IterateThroughHeap((jint) 0,                 /* heap filter */
  60                                         NULL,                     /* class */
  61                                         &g_wrongHeapCallbacks,
  62                                         (const void *) &g_fakeUserData);
  63 
  64     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  65         NSK_COMPLAIN1("IterateThroughHeap() returned %i", retCode);
  66         nsk_jvmti_setFailStatus();
  67     }
  68 
  69     retCode = jvmti->GetTag((jobject) &g_wrongHeapCallbacks, &tag);
  70 
  71     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  72         NSK_COMPLAIN1("GetTag() returned %i", retCode);
  73         nsk_jvmti_setFailStatus();
  74     }
  75 
  76     retCode = jvmti->SetTag((jobject) &g_wrongHeapCallbacks, tag);
  77 
  78     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  79         NSK_COMPLAIN1("SetTag() returned %i", retCode);
  80         nsk_jvmti_setFailStatus();
  81     }
  82 
  83     retCode = jvmti->GetObjectsWithTags(1, &tag, &cnt, &pObjs, &pObjTags);
  84 
  85     if ( ! NSK_VERIFY(retCode == JVMTI_ERROR_MUST_POSSESS_CAPABILITY ) ) {
  86         NSK_COMPLAIN1("GetObjectsWithTags() returned %i", retCode);
  87         nsk_jvmti_setFailStatus();
  88     }
  89 
  90     printf(">>> Let debugee to finish\n");
  91     fflush(0);
  92 
  93     if (!NSK_VERIFY(nsk_jvmti_waitForSync(g_timeout))) {
  94         return;
  95     }
  96 
  97     if (!NSK_VERIFY(nsk_jvmti_resumeSync())) {
  98         return;
  99     }
 100 } /* agentProc */
 101 
 102 
 103 /* ============================================================================= */
 104 
 105 /** Agent library initialization. */
 106 #ifdef STATIC_BUILD
 107 JNIEXPORT jint JNICALL Agent_OnLoad_followref005(JavaVM *jvm, char *options, void *reserved) {
 108     return Agent_Initialize(jvm, options, reserved);
 109 }
 110 JNIEXPORT jint JNICALL Agent_OnAttach_followref005(JavaVM *jvm, char *options, void *reserved) {
 111     return Agent_Initialize(jvm, options, reserved);
 112 }
 113 JNIEXPORT jint JNI_OnLoad_followref005(JavaVM *jvm, char *options, void *reserved) {
 114     return JNI_VERSION_1_8;
 115 }
 116 #endif
 117 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
 118     jvmtiEnv* jvmti = NULL;
 119 
 120     if (!NSK_VERIFY(nsk_jvmti_parseOptions(options))) {
 121         return JNI_ERR;
 122     }
 123 
 124     g_timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
 125 
 126     if (!NSK_VERIFY((jvmti = nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL)) {
 127         return JNI_ERR;
 128     }
 129 
 130     {
 131         jvmtiCapabilities caps;
 132 
 133         memset(&caps, 0, sizeof(caps));
 134         /* Don't add can_tag_objects capability */
 135         if (!NSK_JVMTI_VERIFY(jvmti->AddCapabilities(&caps))) {
 136             return JNI_ERR;
 137         }
 138     }
 139 
 140     if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL))) {
 141         return JNI_ERR;
 142     }
 143 
 144     return JNI_OK;
 145 } /* Agent_OnLoad */
 146 
 147 
 148 /* ============================================================================= */
 149 
 150 }