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
prepare(JNIEnv * jni)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
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)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_JVMTI_VERIFY(jvmti->SetTag(testedObject, SAMPLE_TAG))) {
101 nsk_jvmti_setFailStatus();
102 return;
103 }
104 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
105 return;
106 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
107 return;
108
109 NSK_DISPLAY0("Testcase #2: check that testedObject is tagged correctly\n");
110 if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
111 nsk_jvmti_setFailStatus();
112 return;
113 }
114 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
115 if (tag != SAMPLE_TAG) {
116 if (tag == 0) {
117 NSK_COMPLAIN0("testedObject not tagged\n");
118 } else {
119 NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
120 jlong_to_string(SAMPLE_TAG, buffer));
121 NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
122 }
123 nsk_jvmti_setFailStatus();
124 }
125 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
126 return;
127 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
128 return;
129
130 NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
131 if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
132 nsk_jvmti_setFailStatus();
133 return;
134 }
135 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
136 if (tag != SAMPLE_TAG) {
137 if (tag == 0) {
138 NSK_COMPLAIN0("testedObject not tagged\n");
139 } else {
140 NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
141 jlong_to_string(SAMPLE_TAG, buffer));
142 NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
143 }
144 nsk_jvmti_setFailStatus();
145 }
146 if (!NSK_JVMTI_VERIFY(jvmti->SetTag(testedObject, (jlong)0))) {
147 nsk_jvmti_setFailStatus();
148 return;
149 }
150 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
151 return;
152 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
153 return;
154
155 NSK_DISPLAY0("Testcase #4: check that testedObject is not tagged \n");
156 if (!NSK_JVMTI_VERIFY(jvmti->GetTag(testedObject, &tag))) {
157 nsk_jvmti_setFailStatus();
158 return;
159 }
160 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
161 if (tag != 0) {
162 NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
163 jlong_to_string(tag, buffer));
164 nsk_jvmti_setFailStatus();
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
Agent_OnLoad_ma04t001(JavaVM * jvm,char * options,void * reserved)192 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
193 return Agent_Initialize(jvm, options, reserved);
194 }
Agent_OnAttach_ma04t001(JavaVM * jvm,char * options,void * reserved)195 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001(JavaVM *jvm, char *options, void *reserved) {
196 return Agent_Initialize(jvm, options, reserved);
197 }
JNI_OnLoad_ma04t001(JavaVM * jvm,char * options,void * reserved)198 JNIEXPORT jint JNI_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
199 return JNI_VERSION_1_8;
200 }
201 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)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 }
237