1 /* 2 * Copyright (c) 2016 Red Hat, Inc. and/or its affiliates. 3 * 4 * This code is free software; you can redistribute it and/or modify it 5 * under the terms of the GNU General Public License version 2 only, as 6 * published by the Free Software Foundation. 7 * 8 * This code is distributed in the hope that it will be useful, but WITHOUT 9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 11 * version 2 for more details (a copy is included in the LICENSE file that 12 * accompanied this code). 13 * 14 * You should have received a copy of the GNU General Public License version 15 * 2 along with this work; if not, write to the Free Software Foundation, 16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 17 * 18 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 19 * or visit www.oracle.com if you need additional information or have any 20 * questions. 21 * 22 */ 23 24 /* 25 * @test EvilSyncBug 26 * @summary Tests for crash/assert when attaching init thread during shutdown 27 * @key gc 28 * @library /testlibrary 29 * @modules java.base/jdk.internal.misc 30 * java.management 31 * @run driver/timeout=480 EvilSyncBug 32 */ 33 34 import java.util.*; 35 import java.util.concurrent.*; 36 import java.util.concurrent.locks.*; 37 38 import com.oracle.java.testlibrary.*; 39 40 public class EvilSyncBug { 41 42 private static final int NUM_RUNS = 100; 43 44 static Thread[] hooks = new MyHook[10000]; 45 46 public static void main(String[] args) throws Exception { 47 if (args.length > 0) { 48 test(); 49 } else { 50 for (int i = 0; i < NUM_RUNS; i++) { 51 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xms128m", 52 "-Xmx128m", 53 "-XX:+UseShenandoahGC", 54 "-XX:+UnlockDiagnosticVMOptions", 55 "-XX:ShenandoahGCHeuristics=aggressive", 56 "-XX:+ShenandoahStoreCheck", 57 "EvilSyncBug", "test"); 58 OutputAnalyzer output = new OutputAnalyzer(pb.start()); 59 output.shouldHaveExitValue(0); 60 } 61 } 62 } 63 64 private static void test() throws Exception { 65 66 for (int t = 0; t < hooks.length; t++) { 67 hooks[t] = new MyHook(); 68 } 69 70 ExecutorService service = Executors.newFixedThreadPool( 71 2, 72 r -> { 73 Thread t = new Thread(r); 74 t.setDaemon(true); 75 return t; 76 } 77 ); 78 79 List<Future<?>> futures = new ArrayList<>(); 80 for (int c = 0; c < 100; c++) { 81 Runtime.getRuntime().addShutdownHook(hooks[c]); 82 final Test[] tests = new Test[1000]; 83 for (int t = 0; t < tests.length; t++) { 84 tests[t] = new Test(); 85 } 86 87 Future<?> f1 = service.submit(() -> { 88 Runtime.getRuntime().addShutdownHook(new MyHook()); 89 IntResult2 r = new IntResult2(); 90 for (Test test : tests) { 91 test.RL_Us(r); 92 } 93 }); 94 Future<?> f2 = service.submit(() -> { 95 Runtime.getRuntime().addShutdownHook(new MyHook()); 96 for (Test test : tests) { 97 test.WLI_Us(); 98 } 99 }); 100 101 futures.add(f1); 102 futures.add(f2); 103 } 104 105 for (Future<?> f : futures) { 106 f.get(); 107 } 108 } 109 110 public static class IntResult2 { 111 int r1, r2; 112 } 113 114 public static class Test { 115 final StampedLock lock = new StampedLock(); 116 117 int x, y; 118 119 public void RL_Us(IntResult2 r) { 120 StampedLock lock = this.lock; 121 long stamp = lock.readLock(); 122 r.r1 = x; 123 r.r2 = y; 124 lock.unlock(stamp); 125 } 126 127 public void WLI_Us() { 128 try { 129 StampedLock lock = this.lock; 130 long stamp = lock.writeLockInterruptibly(); 131 x = 1; 132 y = 2; 133 lock.unlock(stamp); 134 } catch (InterruptedException e) { 135 throw new RuntimeException(e); 136 } 137 } 138 } 139 140 private static class MyHook extends Thread { 141 @Override 142 public void run() { 143 try { 144 Thread.sleep(10); 145 } catch (Exception e) {} 146 } 147 } 148 149 } 150