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  * @requires vm.gc.Shenandoah & !vm.graal.enabled
  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 provisioning thread pool on many core
  53             // systems.
  54             int numProcessors = Runtime.getRuntime().availableProcessors();
  55             ExecutorService pool = Executors.newFixedThreadPool(Math.max(1, numProcessors / 4));
  56             Future<?>[] fs = new Future<?>[NUM_RUNS];
  57 
  58             for (int c = 0; c < NUM_RUNS; c++) {
  59                 Callable<Void> task = () -> {
  60                     ProcessBuilder pb = ProcessTools.createJavaProcessBuilder("-Xms128m",
  61                             "-Xmx128m",
  62                             "-XX:+UnlockExperimentalVMOptions",
  63                             "-XX:+UnlockDiagnosticVMOptions",
  64                             "-XX:+UseShenandoahGC",
  65                             "-XX:ShenandoahGCHeuristics=aggressive",
  66                             "TestEvilSyncBug", "test");
  67                     OutputAnalyzer output = new OutputAnalyzer(pb.start());
  68                     output.shouldHaveExitValue(0);
  69                     return null;
  70                 };
  71                 fs[c] = pool.submit(task);
  72             }
  73 
  74             for (Future<?> f : fs) {
  75                 f.get();
  76             }
  77 
  78             pool.shutdown();
  79             pool.awaitTermination(1, TimeUnit.HOURS);
  80         }
  81     }
  82 
  83     private static void test() throws Exception {
  84 
  85         for (int t = 0; t < hooks.length; t++) {
  86             hooks[t] = new MyHook();
  87         }
  88 
  89         ExecutorService service = Executors.newFixedThreadPool(
  90                 2,
  91                 r -> {
  92                     Thread t = new Thread(r);
  93                     t.setDaemon(true);
  94                     return t;
  95                 }
  96         );
  97 
  98         List<Future<?>> futures = new ArrayList<>();
  99         for (int c = 0; c < 100; c++) {
 100             Runtime.getRuntime().addShutdownHook(hooks[c]);
 101             final Test[] tests = new Test[1000];
 102             for (int t = 0; t < tests.length; t++) {
 103                 tests[t] = new Test();
 104             }
 105 
 106             Future<?> f1 = service.submit(() -> {
 107                 Runtime.getRuntime().addShutdownHook(new MyHook());
 108                 IntResult2 r = new IntResult2();
 109                 for (Test test : tests) {
 110                     test.RL_Us(r);
 111                 }
 112             });
 113             Future<?> f2 = service.submit(() -> {
 114                 Runtime.getRuntime().addShutdownHook(new MyHook());
 115                 for (Test test : tests) {
 116                     test.WLI_Us();
 117                 }
 118             });
 119 
 120             futures.add(f1);
 121             futures.add(f2);
 122         }
 123 
 124         for (Future<?> f : futures) {
 125             f.get();
 126         }
 127     }
 128 
 129     public static class IntResult2 {
 130         int r1, r2;
 131     }
 132 
 133     public static class Test {
 134         final StampedLock lock = new StampedLock();
 135 
 136         int x, y;
 137 
 138         public void RL_Us(IntResult2 r) {
 139             StampedLock lock = this.lock;
 140             long stamp = lock.readLock();
 141             r.r1 = x;
 142             r.r2 = y;
 143             lock.unlock(stamp);
 144         }
 145 
 146         public void WLI_Us() {
 147             try {
 148                 StampedLock lock = this.lock;
 149                 long stamp = lock.writeLockInterruptibly();
 150                 x = 1;
 151                 y = 2;
 152                 lock.unlock(stamp);
 153             } catch (InterruptedException e) {
 154                 throw new RuntimeException(e);
 155             }
 156         }
 157     }
 158 
 159     private static class MyHook extends Thread {
 160         @Override
 161         public void run() {
 162             try {
 163                 Thread.sleep(10);
 164             } catch (Exception e) {}
 165         }
 166     }
 167 
 168 }