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