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