1 /*
2  * Copyright (c) 2020, 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 import jdk.test.lib.process.OutputAnalyzer;
24 import jdk.test.lib.process.ProcessTools;
25 
26 /*
27  * @test
28  * @bug 8238676
29  * @summary Check that attempting to use the JNI invocation API from an
30  *          atexit handler fails as expected without crashing.
31  *
32  * @library /test/lib
33  * @run main/othervm/native TestAtExit
34  */
35 
36 public class TestAtExit {
37 
38     // Using a nested class that invokes an enclosing method makes it
39     // easier to setup and use the native library.
40     static class Tester {
41         static {
42             System.loadLibrary("atExit");
43         }
44 
45         // Record the fact we are using System.exit for termination
setUsingSystemExit()46         static native void setUsingSystemExit();
47 
main(String[] args)48         public static void main(String[] args) throws Exception {
49             if (args.length > 0) {
50                 setUsingSystemExit();
51                 System.exit(0);
52             }
53         }
54     }
55 
main(String[] args)56     public static void main(String[] args) throws Exception {
57         // We mustn't load Tester in this VM so we exec by name.
58         String main = "TestAtExit$Tester";
59 
60         String jlp = "-Djava.library.path=" + System.getProperty("test.nativepath");
61         // First run will terminate via DestroyJavaVM
62         OutputAnalyzer output = ProcessTools.executeTestJvm(jlp, main);
63         output.shouldNotContain("Unexpected");
64         output.shouldHaveExitValue(0);
65         output.reportDiagnosticSummary();
66 
67         // Second run will terminate via System.exit()
68         output = ProcessTools.executeTestJvm(jlp, main, "doExit");
69         output.shouldNotContain("Unexpected");
70         output.shouldHaveExitValue(0);
71         output.reportDiagnosticSummary();
72     }
73 }
74