1 /*
2 * Copyright (c) 2003, 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 package nsk.jvmti.PopFrame;
25
26 import nsk.share.Consts;
27
28 import nsk.share.Wicket;
29 import java.io.PrintStream;
30
31 /**
32 * This test checks that after popping a method's frame by the JVMTI
33 * function <code>PopFrame()</code>:
34 * <li>lock acquired by the popped frame will be released
35 * <li>no JVMTI events will be generated by the function <code>PopFrame()</code>
36 */
37 public class popframe005 {
38 static final int WAIT_TIME = 2000;
39
40 static boolean DEBUG_MODE = false;
41 static volatile int testedStep = 0; /* 0- action no yet started
42 1- a frame is to be popped
43 2- checking monitors state
44 3- finishing the test */
45 static volatile int result = Consts.TEST_PASSED;
46 private PrintStream out;
47 private popFrameCls popFrameClsThr;
48 private objWaiter objWaiterThr1, objWaiterThr2;
49 private Object allThreadsStoppedBarrier = new Object();
50 private Wicket startGuarantee;
51 private Wicket finishGuarantee;
52 private Wicket allThreadsStoppedGuarantee;
53
54 static {
55 try {
56 System.loadLibrary("popframe005");
57 } catch (UnsatisfiedLinkError e) {
58 System.err.println("Could not load popframe005 library");
59 System.err.println("java.library.path:" +
60 System.getProperty("java.library.path"));
61 throw e;
62 }
63 }
64
65 native static boolean doPopFrame(popFrameCls popFrameClsThr);
66
67 public static void main(String[] argv) {
68 argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
69
70 System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
71 }
72
73 public static int run(String argv[], PrintStream out) {
74 return new popframe005().runIt(argv, out);
75 }
76
77 private int runIt(String argv[], PrintStream out) {
78 Object lockObj[] = new Object[2];
79
80 this.out = out;
81 for (int i = 0; i < argv.length; i++) {
82 if (argv[i].equals("-v")) // verbose mode
83 DEBUG_MODE = true;
84 }
85
86 lockObj[0] = new Object();
87 lockObj[1] = new Object();
88
89 startGuarantee = new Wicket("startGuarantee", 1, (DEBUG_MODE ? System.out : null));
90 finishGuarantee = new Wicket("finishGuarantee", 1, (DEBUG_MODE ? System.out : null));
91
92 allThreadsStoppedGuarantee =
93 new Wicket("allThreadsStoppedGuarantee", 3, (DEBUG_MODE ? System.out : null));
94
95 // start a thread whose top frame is to be popped
96 popFrameClsThr = new popFrameCls("Tested Thread", lockObj);
97 popFrameClsThr.start();
98
99 startGuarantee.waitFor();
100
101 // start a thread waiting for locked Object lockObj[0]
102 objWaiterThr1 = new objWaiter(lockObj[0], 2);
103 objWaiterThr1.start();
104
105 // start a thread waiting for locked Object lockObj[1]
106 objWaiterThr2 = new objWaiter(lockObj[1], 0);
107 objWaiterThr2.start();
108
109 // pause until the first thread exit notification-block
110 allThreadsStoppedGuarantee.waitFor();
111 synchronized (allThreadsStoppedBarrier) {
112 if (DEBUG_MODE) {
113 out.println("Passed barrier in main thread");
114 out.flush();
115 }
116 }
117
118 /////////////////////// popping a frame ////////////////////////
119 testedStep = 1;
120
121 if (DEBUG_MODE) {
122 out.println("State transition: testedStep: 0->1");
123 out.println("Going to pop the frame...");
124 out.flush();
125 }
126
127 boolean retValue = doPopFrame(popFrameClsThr);
128
129 popFrameClsThr.letItGo();
130 if (!retValue)
131 return Consts.TEST_FAILED;
132
133 ///////////////////// check monitors state /////////////////////
134 testedStep = 2;
135
136 if (DEBUG_MODE) {
137 out.println("State transition: testedStep: 1->2");
138 out.flush();
139 }
140
141 if (!popFrameClsThr.isAlive()) {
142 out.println("TEST FAILURE: thread with the method's popped frame is dead");
143 return Consts.TEST_FAILED;
144 }
145
146 try {
147 objWaiterThr2.join(WAIT_TIME);
148 } catch (InterruptedException e) {
149 if (DEBUG_MODE)
150 out.println("Joining the objWaiterThr2's thread: caught " + e);
151 }
152
153 if (objWaiterThr2.isAlive()) { // objWaiterThr2 should be dead
154 out.println("TEST FAILED: Lock acquired by a popped frame wasn't released\n" + "\twhen the frame had been popped");
155 result = Consts.TEST_FAILED;
156 }
157
158 try {
159 objWaiterThr1.join(WAIT_TIME);
160 } catch (InterruptedException e) {
161 if (DEBUG_MODE) {
162 out.println("Joining the objWaiterThr1's thread: caught " + e);
163 out.flush();
164 }
165 }
166 if (!objWaiterThr1.isAlive()) { // objWaiterThr2 should be alive
167 out.println("TEST FAILED: Lock acquired by a frame, different from the popped one,\n"
168 + "\twas released when the popped frame had been popped");
169 result = Consts.TEST_FAILED;
170 }
171
172 /////////////////////// finish the test ///////////////////////
173 testedStep = 3;
174
175 if (DEBUG_MODE) {
176 out.println("State transition: testedStep: 2->3");
177 out.flush();
178 }
179
180 finishGuarantee.unlock();
181
182 try {
183 if (popFrameClsThr.isAlive())
184 popFrameClsThr.join(WAIT_TIME);
185 } catch (InterruptedException e) {
186 out.println("TEST INCOMPLETE: caught " + e);
187 return Consts.TEST_FAILED;
188 }
189 if (popFrameClsThr.isAlive()) {
190 out.println("TEST FAILED: thread with " +
191 "the popped frame's method is still alive");
192 result = Consts.TEST_FAILED;
193 }
194
195 return result;
196 }
197
198 // a thread whose top frame is to be popped and which locks two Objects
199 class popFrameCls extends Thread {
200 private volatile boolean flag = true;
201 private Object[] lockObj;
202
203 popFrameCls(String name, Object[] obj) {
204 super(name);
205 lockObj = obj;
206 }
207
208 public void run() {
209 synchronized(lockObj[0]) {
210 activeMethod();
211 }
212 if (DEBUG_MODE)
213 out.println("popFrameCls (" + this +
214 "): exiting..."); out.flush();
215 }
216
217 public void activeMethod() {
218 boolean compl = true;
219
220 if (popframe005.testedStep != 0) { // popping has been done
221 if (DEBUG_MODE) {
222 out.println("popFrameCls (" + this +
223 "): enter activeMethod() after popping");
224 out.flush();
225 }
226
227 // wait for checking monitors state by the main thread
228 finishGuarantee.waitFor();
229
230 if (DEBUG_MODE) {
231 out.println("popFrameCls (" + this +
232 "): leaving activeMethod()");
233 out.flush();
234 }
235
236 return;
237 }
238
239 try {
240 synchronized(lockObj[1]) {
241 synchronized(allThreadsStoppedBarrier) {
242 if (DEBUG_MODE) {
243 out.println("popFrameCls (" + this + "): inside activeMethod()");
244 out.flush();
245 }
246
247 // notify the main thread
248 startGuarantee.unlock();
249
250 allThreadsStoppedGuarantee.unlock();
251 }
252
253 // loop until the main thread pops us
254 int i = 0;
255 int n = 1000;
256 while (flag) {
257 if (n <= 0) {
258 n = 1000;
259 }
260 if (i > n) {
261 i = 0;
262 n--;
263 }
264 i++;
265 }
284 }
285
286 // a thread which waits for a specified Object which is locked
287 class objWaiter extends Thread {
288 private Object lockObj;
289 private String objIdent;
290 private int contrVal;
291
292 objWaiter(Object obj, int stepVal) {
293 lockObj = obj;
294 contrVal = stepVal;
295 if (stepVal == 2)
296 objIdent = "[0]";
297 else
298 objIdent = "[1]";
299 }
300
301 public void run() {
302 // notify the main thread
303 synchronized(allThreadsStoppedBarrier) {
304 if (DEBUG_MODE) {
305 out.println("objWaiter(" + this +
306 "): waiting for a lockObj" + objIdent +
307 "'s monitor; testedStep=" + testedStep);
308 out.flush();
309 }
310
311 allThreadsStoppedGuarantee.unlock();
312 }
313
314 try {
315 synchronized(lockObj) {
316 if (testedStep <= contrVal) {
317 out.println("TEST FAILED: the lockObj" + objIdent +
318 "'s monitor became free too early");
319 result = Consts.TEST_FAILED;
320 } else if (DEBUG_MODE) {
321 out.println("Check PASSED: objWaiter(" + this +
322 "): enter the lockObj" + objIdent + "'s monitor");
323 out.flush();
324 }
325 }
326
327 if (DEBUG_MODE) {
328 out.println("objWaiter (" + this + "): exiting...");
329 out.flush();
330 }
331 } catch (Exception e) {
332 out.println("TEST FAILURE: objWaiter (" + this + "): caught " + e);
333 }
334 }
335 }
336 }
|
1 /*
2 * Copyright (c) 2003, 2019, 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 package nsk.jvmti.PopFrame;
25
26 import jdk.test.lib.Utils;
27 import nsk.share.Consts;
28 import nsk.share.Wicket;
29 import java.io.PrintStream;
30
31 /**
32 * This test checks that after popping a method's frame by the JVMTI
33 * function <code>PopFrame()</code>:
34 * <li>lock acquired by the popped frame will be released
35 * <li>no JVMTI events will be generated by the function <code>PopFrame()</code>
36 */
37 public class popframe005 {
38 static final long WAIT_TIME = Utils.adjustTimeout(2000);
39
40 static volatile int testedStep = 0; /* 0- action no yet started
41 1- a frame is to be popped
42 2- checking monitors state
43 3- finishing the test */
44 static volatile int result = Consts.TEST_PASSED;
45 private PrintStream out;
46 private popFrameCls popFrameClsThr;
47 private objWaiter objWaiterThr1, objWaiterThr2;
48 private Object allThreadsStoppedBarrier = new Object();
49 private Wicket startGuarantee;
50 private Wicket finishGuarantee;
51 private Wicket allThreadsStoppedGuarantee;
52
53 static {
54 try {
55 System.loadLibrary("popframe005");
56 } catch (UnsatisfiedLinkError e) {
57 System.err.println("Could not load popframe005 library");
58 System.err.println("java.library.path:" +
59 System.getProperty("java.library.path"));
60 throw e;
61 }
62 }
63
64 native static boolean doPopFrame(popFrameCls popFrameClsThr);
65
66 public static void main(String[] argv) {
67 argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
68
69 System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
70 }
71
72 public static int run(String argv[], PrintStream out) {
73 return new popframe005().runIt(argv, out);
74 }
75
76 private int runIt(String argv[], PrintStream out) {
77 Object lockObj[] = new Object[2];
78
79 this.out = out;
80
81 lockObj[0] = new Object();
82 lockObj[1] = new Object();
83
84 startGuarantee = new Wicket("startGuarantee", 1, System.out);
85 finishGuarantee = new Wicket("finishGuarantee", 1, System.out);
86
87 allThreadsStoppedGuarantee =
88 new Wicket("allThreadsStoppedGuarantee", 3, System.out);
89
90 // start a thread whose top frame is to be popped
91 popFrameClsThr = new popFrameCls("Tested Thread", lockObj);
92 popFrameClsThr.start();
93
94 startGuarantee.waitFor();
95
96 // start a thread waiting for locked Object lockObj[0]
97 objWaiterThr1 = new objWaiter(lockObj[0], 2);
98 objWaiterThr1.start();
99
100 // start a thread waiting for locked Object lockObj[1]
101 objWaiterThr2 = new objWaiter(lockObj[1], 0);
102 objWaiterThr2.start();
103
104 // pause until the first thread exit notification-block
105 allThreadsStoppedGuarantee.waitFor();
106 synchronized (allThreadsStoppedBarrier) {
107 out.println("Passed barrier in main thread");
108 out.flush();
109 }
110
111 /////////////////////// popping a frame ////////////////////////
112 testedStep = 1;
113
114 out.println("State transition: testedStep: 0->1");
115 out.println("Going to pop the frame...");
116 out.flush();
117
118 boolean retValue = doPopFrame(popFrameClsThr);
119
120 popFrameClsThr.letItGo();
121 if (!retValue)
122 return Consts.TEST_FAILED;
123
124 ///////////////////// check monitors state /////////////////////
125 testedStep = 2;
126
127 out.println("State transition: testedStep: 1->2");
128 out.flush();
129
130 if (!popFrameClsThr.isAlive()) {
131 out.println("TEST FAILURE: thread with the method's popped frame is dead");
132 return Consts.TEST_FAILED;
133 }
134
135 try {
136 objWaiterThr2.join(WAIT_TIME);
137 } catch (InterruptedException e) {
138 out.println("Joining the objWaiterThr2's thread: caught " + e);
139 }
140
141 if (objWaiterThr2.isAlive()) { // objWaiterThr2 should be dead
142 out.println("TEST FAILED: Lock acquired by a popped frame wasn't released\n" + "\twhen the frame had been popped");
143 result = Consts.TEST_FAILED;
144 }
145
146 try {
147 // don't want to wait too much if timeout.factor is big
148 objWaiterThr1.join(Math.min(WAIT_TIME, 5000));
149 } catch (InterruptedException e) {
150 out.println("Joining the objWaiterThr1's thread: caught " + e);
151 out.flush();
152 }
153 if (!objWaiterThr1.isAlive()) { // objWaiterThr2 should be alive
154 out.println("TEST FAILED: Lock acquired by a frame, different from the popped one,\n"
155 + "\twas released when the popped frame had been popped");
156 result = Consts.TEST_FAILED;
157 }
158
159 /////////////////////// finish the test ///////////////////////
160 testedStep = 3;
161
162 out.println("State transition: testedStep: 2->3");
163 out.flush();
164
165 finishGuarantee.unlock();
166
167 try {
168 if (popFrameClsThr.isAlive())
169 popFrameClsThr.join(WAIT_TIME);
170 } catch (InterruptedException e) {
171 out.println("TEST INCOMPLETE: caught " + e);
172 return Consts.TEST_FAILED;
173 }
174 if (popFrameClsThr.isAlive()) {
175 out.println("TEST FAILED: thread with " +
176 "the popped frame's method is still alive");
177 result = Consts.TEST_FAILED;
178 }
179
180 return result;
181 }
182
183 // a thread whose top frame is to be popped and which locks two Objects
184 class popFrameCls extends Thread {
185 private volatile boolean flag = true;
186 private Object[] lockObj;
187
188 popFrameCls(String name, Object[] obj) {
189 super(name);
190 lockObj = obj;
191 }
192
193 public void run() {
194 synchronized(lockObj[0]) {
195 activeMethod();
196 }
197 out.println("popFrameCls (" + this + "): exiting...");
198 out.flush();
199 }
200
201 public void activeMethod() {
202 boolean compl = true;
203
204 if (popframe005.testedStep != 0) { // popping has been done
205 out.println("popFrameCls (" + this + "): enter activeMethod() after popping");
206 out.flush();
207
208 // wait for checking monitors state by the main thread
209 finishGuarantee.waitFor();
210
211 out.println("popFrameCls (" + this + "): leaving activeMethod()");
212 out.flush();
213
214 return;
215 }
216
217 try {
218 synchronized(lockObj[1]) {
219 synchronized(allThreadsStoppedBarrier) {
220 out.println("popFrameCls (" + this + "): inside activeMethod()");
221 out.flush();
222
223 // notify the main thread
224 startGuarantee.unlock();
225
226 allThreadsStoppedGuarantee.unlock();
227 }
228
229 // loop until the main thread pops us
230 int i = 0;
231 int n = 1000;
232 while (flag) {
233 if (n <= 0) {
234 n = 1000;
235 }
236 if (i > n) {
237 i = 0;
238 n--;
239 }
240 i++;
241 }
260 }
261
262 // a thread which waits for a specified Object which is locked
263 class objWaiter extends Thread {
264 private Object lockObj;
265 private String objIdent;
266 private int contrVal;
267
268 objWaiter(Object obj, int stepVal) {
269 lockObj = obj;
270 contrVal = stepVal;
271 if (stepVal == 2)
272 objIdent = "[0]";
273 else
274 objIdent = "[1]";
275 }
276
277 public void run() {
278 // notify the main thread
279 synchronized(allThreadsStoppedBarrier) {
280 out.println("objWaiter(" + this +
281 "): waiting for a lockObj" + objIdent +
282 "'s monitor; testedStep=" + testedStep);
283 out.flush();
284
285 allThreadsStoppedGuarantee.unlock();
286 }
287
288 try {
289 synchronized(lockObj) {
290 if (testedStep <= contrVal) {
291 out.println("TEST FAILED: the lockObj" + objIdent +
292 "'s monitor became free too early");
293 result = Consts.TEST_FAILED;
294 } else {
295 out.println("Check PASSED: objWaiter(" + this +
296 "): enter the lockObj" + objIdent + "'s monitor");
297 out.flush();
298 }
299 }
300
301 out.println("objWaiter (" + this + "): exiting...");
302 out.flush();
303 } catch (Exception e) {
304 out.println("TEST FAILURE: objWaiter (" + this + "): caught " + e);
305 }
306 }
307 }
308 }
|