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 24 package nsk.jvmti.unit.ForceEarlyReturn; 25 26 import java.io.PrintStream; 27 import nsk.share.Consts; 28 29 public class earlyretvoid { 30 31 final static int NESTING_DEPTH = 8; 32 33 static { 34 try { 35 System.loadLibrary("earlyretvoid"); 36 } catch (UnsatisfiedLinkError ule) { 37 System.err.println("Could not load earlyretvoid library"); 38 System.err.println("java.library.path:" 39 + System.getProperty("java.library.path")); 40 throw ule; 41 } 42 } 43 getReady(Class cls, int depth)44 native static void getReady(Class cls, int depth); check()45 native static int check(); 46 47 static boolean failed = false; 48 main(String args[])49 public static void main(String args[]) { 50 args = nsk.share.jvmti.JVMTITest.commonInit(args); 51 52 // produce JCK-like exit status. 53 int errCode = run(args, System.out); 54 if (failed) { 55 errCode = Consts.TEST_FAILED; 56 } 57 System.exit(errCode + Consts.JCK_STATUS_BASE); 58 } 59 run(String args[], PrintStream out)60 public static int run(String args[], PrintStream out) { 61 earlyretThread earlyretThr = new earlyretThread(); 62 getReady(earlyretThread.class, NESTING_DEPTH + 1); 63 64 earlyretThr.start(); 65 try { 66 earlyretThr.join(); 67 } catch (InterruptedException e) { 68 throw new Error("Unexpected " + e); 69 } 70 71 return check(); 72 } 73 74 static class Monitor { 75 } 76 77 78 // Tested thread class 79 static class earlyretThread extends Thread { 80 Monitor mntr = null; 81 run()82 public void run() { 83 mntr = new Monitor(); 84 /* Start a chain of recursive calls with NESTING_DEPTH. 85 * Then ForceEarlyReturn will be called in the JVMTI native 86 * agent NESTING_DEPTH times to return from all the frames. 87 * The chain of the ForceEarlyReturn calls starts at the 88 * JVMTI Breakpoint event and continues at each Step event 89 * until return from the first frame of the countDown. 90 * The breakpoint is set in the checkPoint() method. 91 */ 92 countDown(NESTING_DEPTH); 93 checkMonitor(this, "Implicit monitor"); 94 checkMonitor(mntr, "Explicit monitor"); 95 } 96 countDown(int nestingCount)97 public void countDown(int nestingCount) { 98 if (nestingCount > 0) { 99 countDown(nestingCount - 1); 100 } else { 101 // This explicitly locked monitor must be unlocked after 102 // ForceEarlyReturnVoid from the last countDown frame 103 synchronized(mntr) { 104 checkPoint(); // A breakpoint is set in this method 105 } 106 } 107 } 108 109 // Dummy method to be breakpointed in the JVMTI agent. 110 // The monitor 'this' is locked here implicitly checkPoint()111 synchronized void checkPoint() { 112 } 113 114 // Monitor must be released after last ForceEarlyReturn, 115 // so IllegalMonitorState exception is expected here. checkMonitor(Object obj, String monitorId)116 void checkMonitor(Object obj, String monitorId) { 117 try { 118 obj.wait(500); // milliseconds 119 System.out.println( 120 "Erorr: Strange state, didn't expect to be notified: " 121 + monitorId); 122 failed = true; 123 } 124 catch(InterruptedException ex) { 125 System.out.println( 126 "Time-out failure: Monitor was NOT released: " 127 + monitorId); 128 failed = true; 129 } 130 catch(IllegalMonitorStateException ex) { 131 System.out.println( 132 "Success: Monitor was released: " 133 + monitorId); 134 } 135 } 136 } 137 } 138