1 /*
   2  * Copyright (c) 2003, 2016, 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 4954921 8009259
  27  * @library /test/lib
  28  * @modules java.base/jdk.internal.ref
  29  *          java.base/jdk.internal.misc
  30  * @run main ExitOnThrow
  31  * @summary Ensure that if a cleaner throws an exception then the VM exits
  32  */
  33 import java.util.Arrays;
  34 
  35 import jdk.internal.ref.Cleaner;
  36 import jdk.test.lib.JDKToolLauncher;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import jdk.test.lib.process.ProcessTools;
  39 
  40 public class ExitOnThrow {
  41 
  42     static final String cp = System.getProperty("test.classes", ".");
  43 
  44     public static void main(String[] args) throws Exception {
  45         if (args.length == 0) {
  46             String[] cmd = JDKToolLauncher.createUsingTestJDK("java")
  47                                           .addToolArg("-cp")
  48                                           .addToolArg(cp)
  49                                           .addToolArg("ExitOnThrow")
  50                                           .addToolArg("-executeCleaner")
  51                                           .getCommand();
  52             ProcessBuilder pb = new ProcessBuilder(cmd);
  53             OutputAnalyzer out = ProcessTools.executeProcess(pb);
  54             System.out.println("======================");
  55             System.out.println(Arrays.toString(cmd));
  56             String msg = " stdout: [" + out.getStdout() + "]\n" +
  57                          " stderr: [" + out.getStderr() + "]\n" +
  58                          " exitValue = " + out.getExitValue() + "\n";
  59             System.out.println(msg);
  60 
  61             if (out.getExitValue() != 1)
  62                 throw new RuntimeException("Unexpected exit code: " +
  63                                            out.getExitValue());
  64 
  65         } else {
  66             Cleaner.create(new Object(),
  67                            () -> { throw new RuntimeException("Foo!"); } );
  68             while (true) {
  69                 System.gc();
  70                 Thread.sleep(100);
  71             }
  72         }
  73     }
  74 
  75 }