1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. 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  * @test
  26  * @bug 4656449 4945125 8074355
  27  * @summary Make MutexLocker smarter about non-Java threads
  28  * @library /test/lib
  29  * @run driver/timeout=240 ShutdownTest
  30  */
  31 
  32 // This test is adapted from an old regression test for bug 4945125, where VerifyBeforeExit
  33 // crashes before exit for the regression test for bug 4656449.
  34 // The fix is to acquire the Heap_lock before exit after the JavaThread is removed from
  35 // the threads list.  This fix is still valid.  This code requires Heap_lock be acquired
  36 // without a safepoint check at exit.
  37 
  38 import jdk.test.lib.process.OutputAnalyzer;
  39 import jdk.test.lib.process.ProcessTools;
  40 import java.util.ArrayList;
  41 import java.util.Collections;
  42 import java.util.List;
  43 
  44 public class ShutdownTest {
  45    Object[] obj;
  46 
  47    ShutdownTest() {
  48        // Allocate to get some GC pressure.
  49        obj = new Object[100000];
  50    }
  51 
  52     static class ShutdownTestThread extends Thread {
  53        public void run() {
  54          while (true) {
  55            ShutdownTest st = new ShutdownTest();
  56            if (st == null) {
  57              System.out.println("st == null");
  58            }
  59          }
  60        }
  61 
  62        public static void main(String args[]) {
  63          System.out.println("- ShutdownTest -");
  64 
  65          for(int i = 0; i < 100; i++) {
  66            ShutdownTestThread st = new ShutdownTestThread();
  67            st.setDaemon(true);
  68            st.start();
  69          }
  70        }
  71     }
  72 
  73     private static void startVM(List<String> options) throws Throwable, RuntimeException {
  74         // Combine VM flags given from command-line and your additional options
  75         OutputAnalyzer output = ProcessTools.executeTestJvm(options.toArray(new String[options.size()]));
  76         output.shouldContain("- ShutdownTest -");
  77         output.shouldHaveExitValue(0);
  78 
  79     }
  80 
  81     public static void main(String[] args) throws Throwable {
  82         List<String> options = new ArrayList<>();
  83 
  84         Collections.addAll(options,
  85                 "-Xmx2500k",
  86                 "-XX:+UnlockDiagnosticVMOptions",
  87                 "-XX:+VerifyBeforeExit");
  88         options.add(ShutdownTestThread.class.getName());
  89 
  90         for (int iteration = 0; iteration < 5; ++iteration) {
  91             startVM(options);
  92         }
  93     }
  94 }