1 /*
  2  * Copyright (c) 2016, 2020, Red Hat, Inc. 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 /*
 26  * @test
 27  * @summary Tests for crash/assert when attaching init thread during shutdown
 28  * @requires vm.gc.Shenandoah
 29  * @library /test/lib
 30  * @modules java.base/jdk.internal.misc
 31  *          java.management
 32  * @run driver/timeout=480 TestEvilSyncBug
 33  */
 34 
 35 import java.util.*;
 36 import java.util.concurrent.*;
 37 import java.util.concurrent.locks.*;
 38 
 39 import jdk.test.lib.process.ProcessTools;
 40 import jdk.test.lib.process.OutputAnalyzer;
 41 
 42 public class TestEvilSyncBug {
 43 
 44     private static final int NUM_RUNS = 100;
 45 
 46     static Thread[] hooks = new MyHook[10000];
 47 
 48     public static void main(String[] args) throws Exception {
 49         if (args.length > 0) {
 50             test();
 51         } else {
 52             // Use 1/4 of available processors to avoid over-saturation.
 53             int numJobs = Math.max(1, Runtime.getRuntime().availableProcessors() / 4);
 54             ExecutorService pool = Executors.newFixedThreadPool(numJobs);
 55             Future<?>[] fs = new Future<?>[NUM_RUNS];
 56 
 57             for (int c = 0; c < NUM_RUNS; c++) {
 58                 Callable<Void> task = () -> {
 59                     ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder("-Xms128m",
 60                             "-Xmx128m",
 61                             "-XX:+UnlockExperimentalVMOptions",
 62                             "-XX:+UnlockDiagnosticVMOptions",
 63                             "-XX:+UseShenandoahGC",
 64                             "-XX:ShenandoahGCHeuristics=aggressive",
 65                             "TestEvilSyncBug", "test");
 66                     OutputAnalyzer output = new OutputAnalyzer(pb.start());
 67                     output.shouldHaveExitValue(0);
 68                     return null;
 69                 };
 70                 fs[c] = pool.submit(task);
 71             }
 72 
 73             for (Future<?> f : fs) {
 74                 f.get();
 75             }
 76 
 77             pool.shutdown();
 78             pool.awaitTermination(1, TimeUnit.HOURS);
 79         }
 80     }
 81 
 82     private static void test() throws Exception {
 83 
 84         for (int t = 0; t < hooks.length; t++) {
 85             hooks[t] = new MyHook();
 86         }
 87 
 88         ExecutorService service = Executors.newFixedThreadPool(
 89                 2,
 90                 r -> {
 91                     Thread t = new Thread(r);
 92                     t.setDaemon(true);
 93                     return t;
 94                 }
 95         );
 96 
 97         List<Future<?>> futures = new ArrayList<>();
 98         for (int c = 0; c < 100; c++) {
 99             Runtime.getRuntime().addShutdownHook(hooks[c]);
100             final Test[] tests = new Test[1000];
101             for (int t = 0; t < tests.length; t++) {
102                 tests[t] = new Test();
103             }
104 
105             Future<?> f1 = service.submit(() -> {
106                 Runtime.getRuntime().addShutdownHook(new MyHook());
107                 IntResult2 r = new IntResult2();
108                 for (Test test : tests) {
109                     test.RL_Us(r);
110                 }
111             });
112             Future<?> f2 = service.submit(() -> {
113                 Runtime.getRuntime().addShutdownHook(new MyHook());
114                 for (Test test : tests) {
115                     test.WLI_Us();
116                 }
117             });
118 
119             futures.add(f1);
120             futures.add(f2);
121         }
122 
123         for (Future<?> f : futures) {
124             f.get();
125         }
126     }
127 
128     public static class IntResult2 {
129         int r1, r2;
130     }
131 
132     public static class Test {
133         final StampedLock lock = new StampedLock();
134 
135         int x, y;
136 
137         public void RL_Us(IntResult2 r) {
138             StampedLock lock = this.lock;
139             long stamp = lock.readLock();
140             r.r1 = x;
141             r.r2 = y;
142             lock.unlock(stamp);
143         }
144 
145         public void WLI_Us() {
146             try {
147                 StampedLock lock = this.lock;
148                 long stamp = lock.writeLockInterruptibly();
149                 x = 1;
150                 y = 2;
151                 lock.unlock(stamp);
152             } catch (InterruptedException e) {
153                 throw new RuntimeException(e);
154             }
155         }
156     }
157 
158     private static class MyHook extends Thread {
159         @Override
160         public void run() {
161             try {
162                 Thread.sleep(10);
163             } catch (Exception e) {}
164         }
165     }
166 
167 }