1 /*
 2  * Copyright (c) 2020, 2021, 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 import jdk.test.lib.Utils;
25 import jdk.test.lib.process.OutputAnalyzer;
26 import jdk.test.lib.process.ProcessTools;
27 
28 /*
29  * @test
30  * @bug 8238460
31  * @summary Check that re-registering a native method of a boot class
32  *          generates a warning when not done from a boot class
33  *
34  * @library /test/lib
35  * @run main/native TestRegisterNativesWarning
36  */
37 
38 public class TestRegisterNativesWarning {
39 
40     static {
41         System.loadLibrary("registerNativesWarning");
42     }
43 
44     /*
45      * We will replace:
46      *   java/lang/Thread.java:    private static native void yield0();
47      *
48      * as it is simple and innocuous.
49      */
50     static native void test(Class<?> jlThread);
51 
52     // Using a nested class that invokes an enclosing method makes it
53     // easier to setup and use the native library.
54     static class Tester {
55         public static void main(String[] args) throws Exception {
56             System.out.println("Running test() in class loader " +
57                                Tester.class.getClassLoader());
58             test(Thread.class);
59             Thread.yield();
60         }
61     }
62 
63     public static void main(String[] args) throws Exception {
64         String warning = "Re-registering of platform native method: java.lang.Thread.yield0()V from code in a different classloader";
65 
66         String cp = Utils.TEST_CLASS_PATH;
67         String libp = Utils.TEST_NATIVE_PATH;
68         OutputAnalyzer output = ProcessTools.executeTestJvm("-Djava.library.path=" + libp,
69                                                             Tester.class.getName());
70         output.shouldContain(warning);
71         output.shouldHaveExitValue(0);
72         output.reportDiagnosticSummary();
73 
74         // If we run everything from the "boot" loader there should be no warning
75         output = ProcessTools.executeTestJvm("-Djava.library.path=" + libp,
76                                              "-Xbootclasspath/a:" + cp,
77                                              "-Dsun.boot.library.path=" + libp,
78                                              Tester.class.getName());
79         output.shouldNotContain(warning);
80         output.shouldHaveExitValue(0);
81         output.reportDiagnosticSummary();
82     }
83 }