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