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 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 /* ========================================================================== */
39
40 /* scaffold objects */
41 static jlong timeout = 0;
42
43 /* test objects */
44 static jobject testedObject = NULL;
45
46 /* ========================================================================== */
47
prepare(JNIEnv * jni)48 static int prepare(JNIEnv* jni) {
49 const char* CLASS_NAME = "nsk/jvmti/scenarios/multienv/MA04/ma04t001";
50 const char* FIELD_NAME = "testedObject";
51 const char* FIELD_SIGNATURE = "Ljava/lang/Object;";
52 jclass cls = NULL;
53 jfieldID fid = NULL;
54
55 NSK_DISPLAY0("Obtain tested object from a static field of debugee class\n");
56
57 NSK_DISPLAY1("Find class: %s\n", CLASS_NAME);
58 if (!NSK_JNI_VERIFY(jni, (cls =
59 NSK_CPP_STUB2(FindClass, jni, CLASS_NAME)) != NULL))
60 return NSK_FALSE;
61
62 NSK_DISPLAY2("Find field: %s:%s\n", FIELD_NAME, FIELD_SIGNATURE);
63 if (!NSK_JNI_VERIFY(jni, (fid =
64 NSK_CPP_STUB4(GetStaticFieldID, jni, cls,
65 FIELD_NAME, FIELD_SIGNATURE)) != NULL))
66 return NSK_FALSE;
67
68 if (!NSK_JNI_VERIFY(jni, (testedObject =
69 NSK_CPP_STUB3(GetStaticObjectField, jni, cls, fid)) != NULL))
70 return NSK_FALSE;
71
72 if (!NSK_JNI_VERIFY(jni, (testedObject =
73 NSK_CPP_STUB2(NewGlobalRef, jni, testedObject)) != NULL))
74 return NSK_FALSE;
75
76 return NSK_TRUE;
77 }
78
79 /* ========================================================================== */
80
81 /** Agent algorithm. */
82 static void JNICALL
agentProc(jvmtiEnv * jvmti,JNIEnv * jni,void * arg)83 agentProc(jvmtiEnv* jvmti, JNIEnv* jni, void* arg) {
84 jlong tag = -1;
85 char buffer[32];
86
87 if (!nsk_jvmti_waitForSync(timeout))
88 return;
89
90 if (!prepare(jni)) {
91 nsk_jvmti_setFailStatus();
92 return;
93 }
94
95 NSK_DISPLAY0("Testcase #1: check that testedObject is not tagged \n");
96 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
97 nsk_jvmti_setFailStatus();
98 return;
99 }
100 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
101 if (tag != 0) {
102 NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
103 jlong_to_string(tag, buffer));
104 nsk_jvmti_setFailStatus();
105 }
106 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject,
107 SAMPLE_TAG))) {
108 nsk_jvmti_setFailStatus();
109 return;
110 }
111 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
112 return;
113 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
114 return;
115
116 NSK_DISPLAY0("Testcase #2: check that testedObject is tagged correctly\n");
117 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
118 nsk_jvmti_setFailStatus();
119 return;
120 }
121 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
122 if (tag != SAMPLE_TAG) {
123 if (tag == 0) {
124 NSK_COMPLAIN0("testedObject not tagged\n");
125 } else {
126 NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
127 jlong_to_string(SAMPLE_TAG, buffer));
128 NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
129 }
130 nsk_jvmti_setFailStatus();
131 }
132 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
133 return;
134 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
135 return;
136
137 NSK_DISPLAY0("Testcase #3: check that testedObject is tagged correctly\n");
138 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
139 nsk_jvmti_setFailStatus();
140 return;
141 }
142 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
143 if (tag != SAMPLE_TAG) {
144 if (tag == 0) {
145 NSK_COMPLAIN0("testedObject not tagged\n");
146 } else {
147 NSK_COMPLAIN1("testedObject tagged incorrectly, expected=%s,",
148 jlong_to_string(SAMPLE_TAG, buffer));
149 NSK_COMPLAIN1(" got=%s\n", jlong_to_string(tag, buffer));
150 }
151 nsk_jvmti_setFailStatus();
152 }
153 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(SetTag, jvmti, testedObject, (jlong)0))) {
154 nsk_jvmti_setFailStatus();
155 return;
156 }
157 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
158 return;
159 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
160 return;
161
162 NSK_DISPLAY0("Testcase #4: check that testedObject is not tagged \n");
163 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
164 nsk_jvmti_setFailStatus();
165 return;
166 }
167 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
168 if (tag != 0) {
169 NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
170 jlong_to_string(tag, buffer));
171 nsk_jvmti_setFailStatus();
172 }
173 if (!NSK_VERIFY(nsk_jvmti_resumeSync()))
174 return;
175 if (!NSK_VERIFY(nsk_jvmti_waitForSync(timeout)))
176 return;
177
178 NSK_DISPLAY0("Testcase #5: check that testedObject is not tagged\n");
179 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB3(GetTag, jvmti, testedObject, &tag))) {
180 nsk_jvmti_setFailStatus();
181 return;
182 }
183 NSK_DISPLAY1("tag = %s\n", jlong_to_string(tag, buffer));
184 if (tag != 0) {
185 NSK_COMPLAIN1("testedObject is unexpectedly tagged: %s\n",
186 jlong_to_string(tag, buffer));
187 nsk_jvmti_setFailStatus();
188 }
189 NSK_TRACE(NSK_CPP_STUB2(DeleteGlobalRef, jni, testedObject));
190
191 if (!nsk_jvmti_resumeSync())
192 return;
193 }
194
195 /* ========================================================================== */
196
197 /** Agent library initialization. */
198 #ifdef STATIC_BUILD
Agent_OnLoad_ma04t001(JavaVM * jvm,char * options,void * reserved)199 JNIEXPORT jint JNICALL Agent_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
200 return Agent_Initialize(jvm, options, reserved);
201 }
Agent_OnAttach_ma04t001(JavaVM * jvm,char * options,void * reserved)202 JNIEXPORT jint JNICALL Agent_OnAttach_ma04t001(JavaVM *jvm, char *options, void *reserved) {
203 return Agent_Initialize(jvm, options, reserved);
204 }
JNI_OnLoad_ma04t001(JavaVM * jvm,char * options,void * reserved)205 JNIEXPORT jint JNI_OnLoad_ma04t001(JavaVM *jvm, char *options, void *reserved) {
206 return JNI_VERSION_1_8;
207 }
208 #endif
Agent_Initialize(JavaVM * jvm,char * options,void * reserved)209 jint Agent_Initialize(JavaVM *jvm, char *options, void *reserved) {
210 jvmtiEnv* jvmti = NULL;
211 jvmtiEventCallbacks callbacks;
212 jvmtiCapabilities caps;
213
214 NSK_DISPLAY0("Agent_OnLoad\n");
215
216 if (!NSK_VERIFY(nsk_jvmti_parseOptions(options)))
217 return JNI_ERR;
218
219 timeout = nsk_jvmti_getWaitTime() * 60 * 1000;
220
221 if (!NSK_VERIFY((jvmti =
222 nsk_jvmti_createJVMTIEnv(jvm, reserved)) != NULL))
223 return JNI_ERR;
224
225 memset(&caps, 0, sizeof(caps));
226 caps.can_tag_objects = 1;
227 if (!NSK_JVMTI_VERIFY(NSK_CPP_STUB2(AddCapabilities, jvmti, &caps))) {
228 return JNI_ERR;
229 }
230
231 if (!NSK_VERIFY(nsk_jvmti_setAgentProc(agentProc, NULL)))
232 return JNI_ERR;
233
234 memset(&callbacks, 0, sizeof(callbacks));
235 if (!NSK_VERIFY(nsk_jvmti_init_MA(&callbacks)))
236 return JNI_ERR;
237
238 return JNI_OK;
239 }
240
241 /* ========================================================================== */
242
243 #ifdef __cplusplus
244 }
245 #endif
246