1 /* 2 * Copyright (c) 2015, 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 /* 25 * @test ReservedStackTest 26 * @library /testlibrary 27 * @build jdk.test.lib.* 28 * @run main/othervm -XX:-Inline -XX:CompileCommand=exclude,java/util/concurrent/locks/AbstractOwnableSynchronizer.setExclusiveOwnerThread ReservedStackTest 29 */ 30 31 /* The exclusion of java.util.concurrent.locks.AbstractOwnableSynchronizer.setExclusiveOwnerThread() 32 * from the compilable methods is required to ensure that the test will be able 33 * to trigger a StackOverflowError on the right method. 34 */ 35 36 37 /* 38 * Notes about this test: 39 * This test tries to reproduce a rare but nasty corruption bug that 40 * occurs when a StackOverflowError is thrown in some critical sections 41 * of the ReentrantLock implementation. 42 * 43 * Here's the critical section where a corruption could occur 44 * (from java.util.concurrent.ReentrantLock.java) 45 * 46 * final void lock() { 47 * if (compareAndSetState(0, 1)) 48 * setExclusiveOwnerThread(Thread.currentThread()); 49 * else 50 * acquire(1); 51 * } 52 * 53 * The corruption occurs when the compareAndSetState(0, 1) 54 * successfully updates the status of the lock but the method 55 * fails to set the owner because of a stack overflow. 56 * HotSpot checks for stack overflow on method invocations. 57 * The test must trigger a stack overflow either when 58 * Thread.currentThread() or setExclusiveOwnerThread() is 59 * invoked. 60 * 61 * The test starts with a recursive invocation loop until a 62 * first StackOverflowError is thrown, the Error is caught 63 * and a few dozen frames are exited. Now the thread has 64 * little free space on its execution stack and will try 65 * to trigger a stack overflow in the critical section. 66 * The test has a huge array of ReentrantLocks instances. 67 * The thread invokes a recursive method which, at each 68 * of its invocations, tries to acquire the next lock 69 * in the array. The execution continues until a 70 * StackOverflowError is thrown or the end of the array 71 * is reached. 72 * If no StackOverflowError has been thrown, the test 73 * is non conclusive (recommendation: increase the size 74 * of the ReentrantLock array). 75 * The status of all Reentrant locks in the array is checked, 76 * if a corruption is detected, the test failed, otherwise 77 * the test passed. 78 * 79 * To have a chance that the stack overflow occurs on one 80 * of the two targeted method invocations, the test is 81 * repeated in different threads. Each Java thread has a 82 * random size area allocated at the beginning of its 83 * stack to prevent false sharing. The test relies on this 84 * to have different stack alignments when it hits the targeted 85 * methods (the test could have been written with a native 86 * method with alloca, but using different Java threads makes 87 * the test 100% Java). 88 * 89 * One additional trick is required to ensure that the stack 90 * overflow will occur on the Thread.currentThread() getter 91 * or the setExclusiveOwnerThread() setter. 92 * 93 * Potential stack overflows are detected by stack banging, 94 * at method invocation time. 95 * In interpreted code, the stack banging performed for the 96 * lock() method goes further than the stack banging performed 97 * for the getter or the setter method, so the potential stack 98 * overflow is detected before entering the critical section. 99 * In compiled code, the getter and the setter are in-lined, 100 * so the stack banging is only performed before entering the 101 * critical section. 102 * In order to have a stack banging that goes further for the 103 * getter/setter methods than for the lock() method, the test 104 * exploits the property that interpreter frames are (much) 105 * bigger than compiled code frames. When the test is run, 106 * a compiler option disables the compilation of the 107 * setExclusiveOwnerThread() method. 108 * 109 */ 110 111 import java.util.concurrent.locks.ReentrantLock; 112 import jdk.test.lib.Platform; 113 114 public class ReservedStackTest { 115 116 static class ReentrantLockTest { 117 118 private ReentrantLock lockArray[]; 119 // Frame sizes vary a lot between interpreted code and compiled code 120 // so the lock array has to be big enough to cover all cases. 121 // If test fails with message "Not conclusive test", try to increase 122 // LOCK_ARRAY_SIZE value 123 private static final int LOCK_ARRAY_SIZE = 8192; 124 private boolean stackOverflowErrorReceived; 125 StackOverflowError soe = null; 126 private int index = -1; 127 128 public void initialize() { 129 lockArray = new ReentrantLock[LOCK_ARRAY_SIZE]; 130 for (int i = 0; i < LOCK_ARRAY_SIZE; i++) { 131 lockArray[i] = new ReentrantLock(); 132 } 133 stackOverflowErrorReceived = false; 134 } 135 136 public String getResult() { 137 if (!stackOverflowErrorReceived) { 138 return "ERROR: Not conclusive test: no StackOverflowError received"; 139 } 140 for (int i = 0; i < LOCK_ARRAY_SIZE; i++) { 141 if (lockArray[i].isLocked()) { 142 if (!lockArray[i].isHeldByCurrentThread()) { 143 StringBuilder s = new StringBuilder(); 144 s.append("FAILED: ReentrantLock "); 145 s.append(i); 146 s.append(" looks corrupted"); 147 return s.toString(); 148 } 149 } 150 } 151 return "PASSED"; 152 } 153 154 public void run() { 155 try { 156 lockAndCall(0); 157 } catch (StackOverflowError e) { 158 soe = e; 159 stackOverflowErrorReceived = true; 160 } 161 } 162 163 private void lockAndCall(int i) { 164 index = i; 165 if (i < LOCK_ARRAY_SIZE) { 166 lockArray[i].lock(); 167 lockAndCall(i + 1); 168 } 169 } 170 } 171 172 static class RunWithSOEContext implements Runnable { 173 174 int counter; 175 int deframe; 176 int decounter; 177 int setupSOEFrame; 178 int testStartFrame; 179 ReentrantLockTest test; 180 181 public RunWithSOEContext(ReentrantLockTest test, int deframe) { 182 this.test = test; 183 this.deframe = deframe; 184 } 185 186 @Override 187 @jdk.internal.vm.annotation.ReservedStackAccess 188 public void run() { 189 counter = 0; 190 decounter = deframe; 191 test.initialize(); 192 recursiveCall(); 193 System.out.println("Framework got StackOverflowError at frame = " + counter); 194 System.out.println("Test started execution at frame = " + (counter - deframe)); 195 String result = test.getResult(); 196 // The feature is not fully implemented on all platforms, 197 // corruptions are still possible 198 boolean supportedPlatform = Platform.isSolaris() || Platform.isOSX() 199 || (Platform.isLinux() && (Platform.isX86() || Platform.isX64())); 200 if (supportedPlatform && !result.contains("PASSED")) { 201 System.out.println(result); 202 throw new Error(result); 203 } else { 204 // Either the test passed or this platform is not supported. 205 // On not supported platforms, we only expect the VM to 206 // not crash during the test. This is especially important 207 // on Windows where the detection of SOE in annotated 208 // sections is implemented but the reserved zone mechanism 209 // to avoid the corruption cannot be implemented yet 210 // because of JDK-8067946 211 System.out.println("PASSED"); 212 } 213 } 214 215 void recursiveCall() { 216 // Unused local variables to increase the frame size 217 long l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15, l16, l17, l18, l19; 218 long l20, l21, l22, l23, l24, l25, l26, l27, l28, l30, l31, l32, l33, l34, l35, l36, l37; 219 counter++; 220 try { 221 recursiveCall(); 222 } catch (StackOverflowError e) { 223 } 224 decounter--; 225 if (decounter == 0) { 226 setupSOEFrame = counter; 227 testStartFrame = counter - deframe; 228 test.run(); 229 } 230 } 231 } 232 233 public static void main(String[] args) { 234 for (int i = 0; i < 1000; i++) { 235 // Each iteration has to be executed by a new thread. The test 236 // relies on the random size area pushed by the VM at the beginning 237 // of the stack of each Java thread it creates. 238 Thread thread = new Thread(new RunWithSOEContext(new ReentrantLockTest(), 256)); 239 thread.start(); 240 try { 241 thread.join(); 242 } catch (InterruptedException ex) { } 243 } 244 } 245 }