1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
|
/*
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.jvmti.PopFrame;
import jdk.test.lib.Utils;
import nsk.share.Consts;
import nsk.share.Wicket;
import java.io.PrintStream;
/**
* This test checks that after popping a method's frame by the JVMTI
* function <code>PopFrame()</code>:
* <li>lock acquired by the popped frame will be released
* <li>no JVMTI events will be generated by the function <code>PopFrame()</code>
*/
public class popframe005 {
static final long WAIT_TIME = Utils.adjustTimeout(2000);
static volatile int testedStep = 0; /* 0- action no yet started
1- a frame is to be popped
2- checking monitors state
3- finishing the test */
static volatile int result = Consts.TEST_PASSED;
private PrintStream out;
private popFrameCls popFrameClsThr;
private objWaiter objWaiterThr1, objWaiterThr2;
private Object allThreadsStoppedBarrier = new Object();
private Wicket startGuarantee;
private Wicket finishGuarantee;
private Wicket allThreadsStoppedGuarantee;
static {
try {
System.loadLibrary("popframe005");
} catch (UnsatisfiedLinkError e) {
System.err.println("Could not load popframe005 library");
System.err.println("java.library.path:" +
System.getProperty("java.library.path"));
throw e;
}
}
native static boolean doPopFrame(popFrameCls popFrameClsThr);
public static void main(String[] argv) {
argv = nsk.share.jvmti.JVMTITest.commonInit(argv);
System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);
}
public static int run(String argv[], PrintStream out) {
return new popframe005().runIt(argv, out);
}
private int runIt(String argv[], PrintStream out) {
Object lockObj[] = new Object[2];
this.out = out;
lockObj[0] = new Object();
lockObj[1] = new Object();
startGuarantee = new Wicket("startGuarantee", 1, System.out);
finishGuarantee = new Wicket("finishGuarantee", 1, System.out);
allThreadsStoppedGuarantee =
new Wicket("allThreadsStoppedGuarantee", 3, System.out);
// start a thread whose top frame is to be popped
popFrameClsThr = new popFrameCls("Tested Thread", lockObj);
popFrameClsThr.start();
startGuarantee.waitFor();
// start a thread waiting for locked Object lockObj[0]
objWaiterThr1 = new objWaiter(lockObj[0], 2);
objWaiterThr1.start();
// start a thread waiting for locked Object lockObj[1]
objWaiterThr2 = new objWaiter(lockObj[1], 0);
objWaiterThr2.start();
// pause until the first thread exit notification-block
allThreadsStoppedGuarantee.waitFor();
synchronized (allThreadsStoppedBarrier) {
out.println("Passed barrier in main thread");
out.flush();
}
/////////////////////// popping a frame ////////////////////////
testedStep = 1;
out.println("State transition: testedStep: 0->1");
out.println("Going to pop the frame...");
out.flush();
boolean retValue = doPopFrame(popFrameClsThr);
popFrameClsThr.letItGo();
if (!retValue)
return Consts.TEST_FAILED;
///////////////////// check monitors state /////////////////////
testedStep = 2;
out.println("State transition: testedStep: 1->2");
out.flush();
if (!popFrameClsThr.isAlive()) {
out.println("TEST FAILURE: thread with the method's popped frame is dead");
return Consts.TEST_FAILED;
}
try {
objWaiterThr2.join(WAIT_TIME);
} catch (InterruptedException e) {
out.println("Joining the objWaiterThr2's thread: caught " + e);
}
if (objWaiterThr2.isAlive()) { // objWaiterThr2 should be dead
out.println("TEST FAILED: Lock acquired by a popped frame wasn't released\n" + "\twhen the frame had been popped");
result = Consts.TEST_FAILED;
}
try {
// don't want to wait too much if timeout.factor is big
objWaiterThr1.join(Math.min(WAIT_TIME, 5000));
} catch (InterruptedException e) {
out.println("Joining the objWaiterThr1's thread: caught " + e);
out.flush();
}
if (!objWaiterThr1.isAlive()) { // objWaiterThr2 should be alive
out.println("TEST FAILED: Lock acquired by a frame, different from the popped one,\n"
+ "\twas released when the popped frame had been popped");
result = Consts.TEST_FAILED;
}
/////////////////////// finish the test ///////////////////////
testedStep = 3;
out.println("State transition: testedStep: 2->3");
out.flush();
finishGuarantee.unlock();
try {
if (popFrameClsThr.isAlive())
popFrameClsThr.join(WAIT_TIME);
} catch (InterruptedException e) {
out.println("TEST INCOMPLETE: caught " + e);
return Consts.TEST_FAILED;
}
if (popFrameClsThr.isAlive()) {
out.println("TEST FAILED: thread with " +
"the popped frame's method is still alive");
result = Consts.TEST_FAILED;
}
return result;
}
// a thread whose top frame is to be popped and which locks two Objects
class popFrameCls extends Thread {
private volatile boolean flag = true;
private Object[] lockObj;
popFrameCls(String name, Object[] obj) {
super(name);
lockObj = obj;
}
public void run() {
synchronized(lockObj[0]) {
activeMethod();
}
out.println("popFrameCls (" + this + "): exiting...");
out.flush();
}
public void activeMethod() {
boolean compl = true;
if (popframe005.testedStep != 0) { // popping has been done
out.println("popFrameCls (" + this + "): enter activeMethod() after popping");
out.flush();
// wait for checking monitors state by the main thread
finishGuarantee.waitFor();
out.println("popFrameCls (" + this + "): leaving activeMethod()");
out.flush();
return;
}
try {
synchronized(lockObj[1]) {
synchronized(allThreadsStoppedBarrier) {
out.println("popFrameCls (" + this + "): inside activeMethod()");
out.flush();
// notify the main thread
startGuarantee.unlock();
allThreadsStoppedGuarantee.unlock();
}
// loop until the main thread pops us
int i = 0;
int n = 1000;
while (flag) {
if (n <= 0) {
n = 1000;
}
if (i > n) {
i = 0;
n--;
}
i++;
}
}
} catch (Exception e) {
out.println("FAILURE: popFrameCls (" + this + "): caught " + e);
out.flush();
compl = false;
} finally {
if (compl) {
out.println("TEST FAILED: finally block was executed after popping");
out.flush();
}
popframe005.result = Consts.TEST_FAILED;
}
}
public void letItGo() {
flag = false;
}
}
// a thread which waits for a specified Object which is locked
class objWaiter extends Thread {
private Object lockObj;
private String objIdent;
private int contrVal;
objWaiter(Object obj, int stepVal) {
lockObj = obj;
contrVal = stepVal;
if (stepVal == 2)
objIdent = "[0]";
else
objIdent = "[1]";
}
public void run() {
// notify the main thread
synchronized(allThreadsStoppedBarrier) {
out.println("objWaiter(" + this +
"): waiting for a lockObj" + objIdent +
"'s monitor; testedStep=" + testedStep);
out.flush();
allThreadsStoppedGuarantee.unlock();
}
try {
synchronized(lockObj) {
if (testedStep <= contrVal) {
out.println("TEST FAILED: the lockObj" + objIdent +
"'s monitor became free too early");
result = Consts.TEST_FAILED;
} else {
out.println("Check PASSED: objWaiter(" + this +
"): enter the lockObj" + objIdent + "'s monitor");
out.flush();
}
}
out.println("objWaiter (" + this + "): exiting...");
out.flush();
} catch (Exception e) {
out.println("TEST FAILURE: objWaiter (" + this + "): caught " + e);
}
}
}
}
|