Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
8238460: Provide warnings about the use of JNI RegisterNatives to reb…
…ind native methods for boot/platform classes in other classloaders Reviewed-by: jwilhelm, lfoltan
- Loading branch information
David Holmes
committed
Feb 7, 2020
1 parent
ccbd819
commit 9f21d80
Showing
3 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
test/hotspot/jtreg/runtime/jni/registerNativesWarning/TestRegisterNativesWarning.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
import jdk.test.lib.process.OutputAnalyzer; | ||
import jdk.test.lib.process.ProcessTools; | ||
|
||
/* | ||
* @test | ||
* @bug 8238460 | ||
* @summary Check that re-registering a native method of a boot class | ||
* generates a warning when not done from a boot class | ||
* | ||
* @library /test/lib | ||
* @run main/othervm/native TestRegisterNativesWarning | ||
*/ | ||
|
||
public class TestRegisterNativesWarning { | ||
|
||
static { | ||
System.loadLibrary("registerNativesWarning"); | ||
} | ||
|
||
/* | ||
* We will replace: | ||
* java/lang/Thread.java: public static native void yield(); | ||
* | ||
* as it is simple and innocuous. | ||
*/ | ||
native static void test(Class<?> jlThread); | ||
|
||
// Using a nested class that invokes an enclosing method makes it | ||
// easier to setup and use the native library. | ||
static class Tester { | ||
public static void main(String[] args) throws Exception { | ||
System.out.println("Running test() in class loader " + | ||
Tester.class.getClassLoader()); | ||
test(Thread.class); | ||
Thread.yield(); | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws Exception { | ||
String warning = "Re-registering of platform native method: java.lang.Thread.yield()V from code in a different classloader"; | ||
|
||
OutputAnalyzer output = ProcessTools.executeTestJvm(Tester.class.getName()); | ||
output.shouldContain(warning); | ||
output.shouldHaveExitValue(0); | ||
output.reportDiagnosticSummary(); | ||
|
||
// If we run everything from the "boot" loader there should be no warning | ||
String cp = System.getProperty("test.class.path"); | ||
String libp = System.getProperty("java.library.path"); | ||
output = ProcessTools.executeTestJvm("-Xbootclasspath/a:" + cp, | ||
"-Dsun.boot.library.path=" + libp, | ||
Tester.class.getName()); | ||
output.shouldNotContain(warning); | ||
output.shouldHaveExitValue(0); | ||
output.reportDiagnosticSummary(); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
test/hotspot/jtreg/runtime/jni/registerNativesWarning/libregisterNativesWarning.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. | ||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. | ||
* | ||
* This code is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU General Public License version 2 only, as | ||
* published by the Free Software Foundation. | ||
* | ||
* This code is distributed in the hope that it will be useful, but WITHOUT | ||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | ||
* version 2 for more details (a copy is included in the LICENSE file that | ||
* accompanied this code). | ||
* | ||
* You should have received a copy of the GNU General Public License version | ||
* 2 along with this work; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | ||
* | ||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
* or visit www.oracle.com if you need additional information or have any | ||
* questions. | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "jni.h" | ||
#include "jni_util.h" | ||
|
||
/* | ||
* We will replace: | ||
* java/lang/Thread.java: public static native void yield(); | ||
* | ||
* as it is simple and innocuous. | ||
*/ | ||
static void myYield(JNIEnv* env, jclass cls) { | ||
printf("myYield executed\n"); | ||
} | ||
|
||
|
||
JNIEXPORT void JNICALL | ||
Java_TestRegisterNativesWarning_test | ||
(JNIEnv *env, jclass cls, jclass jlThread) { | ||
JNINativeMethod nativeMethods[] = { | ||
{ | ||
(char*) "yield", // name | ||
(char*) "()V", // sig | ||
(void*) myYield // native method ptr | ||
} | ||
}; | ||
(*env)->RegisterNatives(env, jlThread, nativeMethods, 1); | ||
} |