undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
|
|
@ -0,0 +1,85 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 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 java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.lang.Module;
|
||||
import java.lang.ModuleLayer;
|
||||
import java.lang.module.ModuleFinder;
|
||||
import java.lang.module.ModuleReference;
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
// This class creates a dynamic module layer and loads the
|
||||
// panama_module in it. enableNativeAccess on that dynamic
|
||||
// module is called depending on the command line option.
|
||||
//
|
||||
// Usage:
|
||||
// java --enable-native-access=ALL-UNNAMED NativeAccessDynamicMain <module-path> <mod/class> <true|false> [main-args]
|
||||
public class NativeAccessDynamicMain {
|
||||
public static void main(String[] args) throws Exception {
|
||||
String modulePath = args[0];
|
||||
String moduleAndClsName = args[1];
|
||||
boolean enableNativeAccess = Boolean.parseBoolean(args[2]);
|
||||
String[] mainArgs = args.length > 2? Arrays.copyOfRange(args, 3, args.length) : new String[0];
|
||||
|
||||
int idx = moduleAndClsName.indexOf('/');
|
||||
String moduleName = moduleAndClsName.substring(0, idx);
|
||||
String className = moduleAndClsName.substring(idx+1);
|
||||
|
||||
Path[] paths = Stream.of(modulePath.split(File.pathSeparator))
|
||||
.map(Paths::get)
|
||||
.toArray(Path[]::new);
|
||||
ModuleFinder mf = ModuleFinder.of(paths);
|
||||
var mrefs = mf.findAll();
|
||||
if (mrefs.isEmpty()) {
|
||||
throw new RuntimeException("No modules module path: " + modulePath);
|
||||
}
|
||||
|
||||
var rootMods = mrefs.stream().
|
||||
map(mr->mr.descriptor().name()).
|
||||
collect(Collectors.toSet());
|
||||
|
||||
ModuleLayer boot = ModuleLayer.boot();
|
||||
var conf = boot.configuration().
|
||||
resolve(mf, ModuleFinder.of(), rootMods);
|
||||
String firstMod = rootMods.iterator().next();
|
||||
URLClassLoader cl = new URLClassLoader(new URL[] { paths[0].toFile().toURL() });
|
||||
ModuleLayer.Controller controller = boot.defineModulesWithOneLoader(conf, List.of(boot), cl);
|
||||
ModuleLayer layer = controller.layer();
|
||||
Module mod = layer.findModule(firstMod).get();
|
||||
|
||||
// conditionally grant native access to the dynamic module created
|
||||
if (enableNativeAccess) {
|
||||
controller.enableNativeAccess(mod);
|
||||
}
|
||||
Class mainCls = Class.forName(mod, className);
|
||||
var main = mainCls.getMethod("main", String[].class);
|
||||
main.invoke(null, (Object)mainArgs);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,184 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @requires !vm.musl
|
||||
*
|
||||
* @library /test/lib
|
||||
* @build TestEnableNativeAccess
|
||||
* panama_module/*
|
||||
* panama_jni_load_module/*
|
||||
* panama_jni_def_module/*
|
||||
* panama_jni_use_module/*
|
||||
*
|
||||
* org.openjdk.foreigntest.unnamed.PanamaMainUnnamedModule
|
||||
* @run testng/othervm/native/timeout=180 TestEnableNativeAccess
|
||||
* @summary Basic test for java --enable-native-access
|
||||
*/
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* Basic test of --enable-native-access with expected behaviour:
|
||||
*
|
||||
* if flag present: - permit access to modules that are specified
|
||||
* - deny access to modules that are not specified
|
||||
* (throw IllegalCallerException)
|
||||
* if flag not present: - permit access to all modules and omit a warning
|
||||
* (on first access per module only)
|
||||
*/
|
||||
|
||||
@Test
|
||||
public class TestEnableNativeAccess extends TestEnableNativeAccessBase {
|
||||
|
||||
@DataProvider(name = "succeedCases")
|
||||
public Object[][] succeedCases() {
|
||||
return new Object[][] {
|
||||
{ "panama_enable_native_access", PANAMA_MAIN, successNoWarning(), new String[]{"--enable-native-access=panama_module"} },
|
||||
{ "panama_enable_native_access_reflection", PANAMA_REFLECTION, successNoWarning(), new String[]{"--enable-native-access=panama_module"} },
|
||||
{ "panama_enable_native_access_invoke", PANAMA_INVOKE, successNoWarning(), new String[]{"--enable-native-access=panama_module"} },
|
||||
|
||||
{ "panama_comma_separated_enable", PANAMA_MAIN, successNoWarning(), new String[]{"--enable-native-access=java.base,panama_module"} },
|
||||
{ "panama_comma_separated_enable_reflection", PANAMA_REFLECTION, successNoWarning(), new String[]{"--enable-native-access=java.base,panama_module"} },
|
||||
{ "panama_comma_separated_enable_invoke", PANAMA_INVOKE, successNoWarning(), new String[]{"--enable-native-access=java.base,panama_module"} },
|
||||
{ "panama_comma_separated_enable_jni", PANAMA_JNI, successNoWarning(), new String[]{"--enable-native-access=panama_jni_load_module,panama_jni_def_module,ALL-UNNAMED"} },
|
||||
|
||||
{ "panama_enable_native_access_warn", PANAMA_MAIN, successWithWarning("panama"), new String[]{} },
|
||||
{ "panama_enable_native_access_warn_reflection", PANAMA_REFLECTION, successWithWarning("panama"), new String[]{} },
|
||||
{ "panama_enable_native_access_warn_invoke", PANAMA_INVOKE, successWithWarning("panama"), new String[]{} },
|
||||
{ "panama_enable_native_access_warn_jni", PANAMA_JNI, successWithWarnings("panama_jni_load_module", "panama_jni_def_module", "ALL-UNNAMED"), new String[]{} },
|
||||
|
||||
{ "panama_enable_native_access_allow", PANAMA_MAIN, successNoWarning(), new String[]{"--illegal-native-access=allow"} },
|
||||
{ "panama_enable_native_access_allow_reflection", PANAMA_REFLECTION, successNoWarning(), new String[]{"--illegal-native-access=allow"} },
|
||||
{ "panama_enable_native_access_allow_invoke", PANAMA_INVOKE, successNoWarning(), new String[]{"--illegal-native-access=allow"} },
|
||||
{ "panama_enable_native_access_allow_jni", PANAMA_JNI, successNoWarning(), new String[]{"--illegal-native-access=allow"} },
|
||||
|
||||
{ "panama_no_unnamed_module_native_access", UNNAMED, successWithWarning("ALL-UNNAMED"), new String[]{} },
|
||||
{ "panama_all_unnamed_module_native_access", UNNAMED, successNoWarning(), new String[]{"--enable-native-access=ALL-UNNAMED"} },
|
||||
{ "panama_allow_unnamed_module_native_access", UNNAMED, successNoWarning(), new String[]{"--illegal-native-access=allow"} },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the test to execute the given test action. The VM is run with the
|
||||
* given VM options and the output checked to see that it matches the
|
||||
* expected result.
|
||||
*/
|
||||
OutputAnalyzer run(String action, String cls, Result expectedResult, String... vmopts)
|
||||
throws Exception
|
||||
{
|
||||
Stream<String> s1 = Stream.concat(
|
||||
Stream.of(vmopts),
|
||||
Stream.of("-Djava.library.path=" + System.getProperty("java.library.path")));
|
||||
Stream<String> s2 = cls.equals(UNNAMED) ? Stream.of("-p", MODULE_PATH, cls, action)
|
||||
: Stream.of("-p", MODULE_PATH, "-m", cls, action);
|
||||
String[] opts = Stream.concat(s1, s2).toArray(String[]::new);
|
||||
OutputAnalyzer outputAnalyzer = ProcessTools
|
||||
.executeTestJava(opts)
|
||||
.outputTo(System.out)
|
||||
.errorTo(System.out);
|
||||
checkResult(expectedResult, outputAnalyzer);
|
||||
return outputAnalyzer;
|
||||
}
|
||||
|
||||
@Test(dataProvider = "succeedCases")
|
||||
public void testSucceed(String action, String cls, Result expectedResult, String... vmopts) throws Exception {
|
||||
run(action, cls, expectedResult, vmopts);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests that without --enable-native-access, a multi-line warning is printed
|
||||
* on first access of a module.
|
||||
*/
|
||||
public void testWarnFirstAccess() throws Exception {
|
||||
List<String> output1 = run("panama_enable_native_access_first", PANAMA_MAIN,
|
||||
successWithWarning("panama")).asLines();
|
||||
assertTrue(count(output1, "WARNING") == 4); // 4 on first access, none on subsequent access
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies --enable-native-access more than once, each list of module names
|
||||
* is appended.
|
||||
*/
|
||||
public void testRepeatedOption() throws Exception {
|
||||
run("panama_enable_native_access_last_one_wins", PANAMA_MAIN,
|
||||
success(), "--enable-native-access=java.base", "--enable-native-access=panama_module");
|
||||
run("panama_enable_native_access_last_one_wins", PANAMA_MAIN,
|
||||
success(), "--enable-native-access=panama_module", "--enable-native-access=java.base");
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies bad value to --enable-native-access.
|
||||
*/
|
||||
public void testBadValue() throws Exception {
|
||||
run("panama_deny_bad_unknown_module", PANAMA_MAIN,
|
||||
failWithWarning("WARNING: Unknown module: BAD specified to --enable-native-access"),
|
||||
"--illegal-native-access=deny", "--enable-native-access=BAD");
|
||||
run("panama_deny_bad_all_module_path_module", PANAMA_MAIN,
|
||||
failWithWarning("WARNING: Unknown module: ALL-MODULE-PATH specified to --enable-native-access"),
|
||||
"--illegal-native-access=deny", "--enable-native-access=ALL-MODULE-PATH" );
|
||||
run("panama_deny_no_module_main", PANAMA_MAIN,
|
||||
failWithError("module panama_module"),
|
||||
"--illegal-native-access=deny");
|
||||
run("panama_deny_no_module_invoke", PANAMA_INVOKE,
|
||||
failWithError("module panama_module"),
|
||||
"--illegal-native-access=deny");
|
||||
run("panama_deny_no_module_reflection", PANAMA_REFLECTION,
|
||||
failWithError("module panama_module"),
|
||||
"--illegal-native-access=deny");
|
||||
run("panama_deny_no_module_jni", PANAMA_JNI,
|
||||
failWithError("module panama_jni_load_module"),
|
||||
"--illegal-native-access=deny");
|
||||
}
|
||||
|
||||
public void testDetailedWarningMessage() throws Exception {
|
||||
run("panama_enable_native_access_warn_jni", PANAMA_JNI,
|
||||
success()
|
||||
// call to System::loadLibrary from panama_jni_load_module
|
||||
.expect("WARNING: A restricted method in java.lang.System has been called")
|
||||
.expect("WARNING: java.lang.System::loadLibrary has been called by org.openjdk.jni.PanamaMainJNI in module panama_jni_load_module")
|
||||
// JNI native method binding in panama_jni_def_module
|
||||
.expect("WARNING: A native method in org.openjdk.jni.def.PanamaJNIDef has been bound")
|
||||
.expect("WARNING: org.openjdk.jni.def.PanamaJNIDef::nativeLinker0 is declared in module panama_jni_def_module")
|
||||
// upcall to Linker::downcallHandle from JNI code
|
||||
.expect("WARNING: A restricted method in java.lang.foreign.Linker has been called")
|
||||
.expect("WARNING: java.lang.foreign.Linker::downcallHandle has been called by code in an unnamed module"));
|
||||
}
|
||||
|
||||
private int count(Iterable<String> lines, CharSequence cs) {
|
||||
int count = 0;
|
||||
for (String line : lines) {
|
||||
if (line.contains(cs)) count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,132 @@
|
|||
/*
|
||||
* Copyright (c) 2023, 2024, 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 java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
public class TestEnableNativeAccessBase {
|
||||
static final String MODULE_PATH = System.getProperty("jdk.module.path");
|
||||
|
||||
static final String PANAMA_MAIN_CLS = "org.openjdk.foreigntest.PanamaMainDirect";
|
||||
static final String PANAMA_MAIN = "panama_module/" + PANAMA_MAIN_CLS;
|
||||
static final String PANAMA_REFLECTION_CLS = "org.openjdk.foreigntest.PanamaMainReflection";
|
||||
static final String PANAMA_REFLECTION = "panama_module/" + PANAMA_REFLECTION_CLS;
|
||||
static final String PANAMA_INVOKE_CLS = "org.openjdk.foreigntest.PanamaMainInvoke";
|
||||
static final String PANAMA_INVOKE = "panama_module/" + PANAMA_INVOKE_CLS;
|
||||
static final String PANAMA_JNI_CLS = "org.openjdk.jni.PanamaMainJNI";
|
||||
static final String PANAMA_JNI = "panama_jni_load_module/" + PANAMA_JNI_CLS;
|
||||
static final String UNNAMED = "org.openjdk.foreigntest.unnamed.PanamaMainUnnamedModule";
|
||||
|
||||
/**
|
||||
* Represents the expected result of a test.
|
||||
*/
|
||||
static final class Result {
|
||||
private final boolean success;
|
||||
private final List<String> expectedOutput = new ArrayList<>();
|
||||
private final List<String> notExpectedOutput = new ArrayList<>();
|
||||
|
||||
Result(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
Result expect(String msg) {
|
||||
expectedOutput.add(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
Result doNotExpect(String msg) {
|
||||
notExpectedOutput.add(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
boolean shouldSucceed() {
|
||||
return success;
|
||||
}
|
||||
|
||||
Stream<String> expectedOutput() {
|
||||
return expectedOutput.stream();
|
||||
}
|
||||
|
||||
Stream<String> notExpectedOutput() {
|
||||
return notExpectedOutput.stream();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = (success) ? "success" : "failure";
|
||||
for (String msg : expectedOutput) {
|
||||
s += "/" + msg;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static Result success() {
|
||||
return new Result(true);
|
||||
}
|
||||
|
||||
static Result successNoWarning() {
|
||||
return success().doNotExpect("WARNING");
|
||||
}
|
||||
|
||||
static Result successWithWarning(String moduleName) {
|
||||
return success().expect("WARNING").expect("--enable-native-access=" + moduleName);
|
||||
}
|
||||
|
||||
static Result successWithWarnings(String... moduleNames) {
|
||||
Result result = success();
|
||||
for (String moduleName : moduleNames) {
|
||||
result = result.expect("WARNING").expect("--enable-native-access=" + moduleName);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
static Result failWithWarning(String expectedOutput) {
|
||||
return new Result(false).expect(expectedOutput).expect("WARNING");
|
||||
}
|
||||
|
||||
static Result failWithError(String expectedOutput) {
|
||||
return new Result(false).expect(expectedOutput);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks an expected result with the output captured by the given
|
||||
* OutputAnalyzer.
|
||||
*/
|
||||
void checkResult(Result expectedResult, OutputAnalyzer outputAnalyzer) {
|
||||
expectedResult.expectedOutput().forEach(outputAnalyzer::shouldContain);
|
||||
expectedResult.notExpectedOutput().forEach(outputAnalyzer::shouldNotContain);
|
||||
int exitValue = outputAnalyzer.getExitValue();
|
||||
if (expectedResult.shouldSucceed()) {
|
||||
assertTrue(exitValue == 0);
|
||||
} else {
|
||||
assertTrue(exitValue != 0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,114 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 2024, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @requires !vm.musl
|
||||
*
|
||||
* @library /test/lib
|
||||
* @build TestEnableNativeAccessDynamic
|
||||
* panama_module/*
|
||||
NativeAccessDynamicMain
|
||||
* @run testng/othervm/timeout=180 TestEnableNativeAccessDynamic
|
||||
* @summary Test for dynamically setting --enable-native-access flag for a module
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
@Test
|
||||
public class TestEnableNativeAccessDynamic extends TestEnableNativeAccessBase {
|
||||
|
||||
@DataProvider(name = "succeedCases")
|
||||
public Object[][] succeedCases() {
|
||||
return new Object[][] {
|
||||
{ "panama_enable_native_access", PANAMA_MAIN, successNoWarning() },
|
||||
{ "panama_enable_native_access_reflection", PANAMA_REFLECTION, successNoWarning() },
|
||||
{ "panama_enable_native_access_invoke", PANAMA_INVOKE, successNoWarning() },
|
||||
};
|
||||
}
|
||||
|
||||
@DataProvider(name = "failureCases")
|
||||
public Object[][] failureCases() {
|
||||
String errMsg = "Illegal native access from module panama_module";
|
||||
return new Object[][] {
|
||||
{ "panama_enable_native_access_fail", PANAMA_MAIN, failWithError(errMsg) },
|
||||
{ "panama_enable_native_access_fail_reflection", PANAMA_REFLECTION, failWithError(errMsg) },
|
||||
{ "panama_enable_native_access_fail_invoke", PANAMA_INVOKE, failWithError(errMsg) },
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs the test to execute the given test action. The VM is run with the
|
||||
* given VM options and the output checked to see that it matches the
|
||||
* expected result.
|
||||
*/
|
||||
OutputAnalyzer run(String action, String moduleAndCls, boolean enableNativeAccess,
|
||||
Result expectedResult, boolean panamaModuleInBootLayer) throws Exception
|
||||
{
|
||||
List<String> list = new ArrayList<>();
|
||||
list.add("--illegal-native-access=deny");
|
||||
if (panamaModuleInBootLayer) {
|
||||
list.addAll(List.of("-p", MODULE_PATH));
|
||||
list.add("--add-modules=panama_module");
|
||||
list.add("--enable-native-access=panama_module");
|
||||
} else {
|
||||
list.add("--enable-native-access=ALL-UNNAMED");
|
||||
}
|
||||
list.addAll(List.of("NativeAccessDynamicMain", MODULE_PATH,
|
||||
moduleAndCls, Boolean.toString(enableNativeAccess), action));
|
||||
String[] opts = list.toArray(String[]::new);
|
||||
OutputAnalyzer outputAnalyzer = ProcessTools
|
||||
.executeTestJava(opts)
|
||||
.outputTo(System.out)
|
||||
.errorTo(System.out);
|
||||
checkResult(expectedResult, outputAnalyzer);
|
||||
return outputAnalyzer;
|
||||
}
|
||||
|
||||
@Test(dataProvider = "succeedCases")
|
||||
public void testSucceed(String action, String moduleAndCls,
|
||||
Result expectedResult) throws Exception {
|
||||
run(action, moduleAndCls, true, expectedResult, false);
|
||||
}
|
||||
|
||||
@Test(dataProvider = "failureCases")
|
||||
public void testFailures(String action, String moduleAndCls,
|
||||
Result expectedResult) throws Exception {
|
||||
run(action, moduleAndCls, false, expectedResult, false);
|
||||
}
|
||||
|
||||
// make sure that having a same named module in boot layer with native access
|
||||
// does not influence same named dynamic module.
|
||||
@Test(dataProvider = "failureCases")
|
||||
public void testFailuresWithPanamaModuleInBootLayer(String action, String moduleAndCls,
|
||||
Result expectedResult) throws Exception {
|
||||
run(action, moduleAndCls, false, expectedResult, true);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,140 @@
|
|||
/*
|
||||
* Copyright (c) 2023, 2025, 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.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @summary Basic test for Enable-Native-Access attribute in the
|
||||
* manifest of a main application JAR
|
||||
* @library /test/lib
|
||||
* @requires jdk.foreign.linker != "UNSUPPORTED"
|
||||
* @requires !vm.musl
|
||||
*
|
||||
* @enablePreview
|
||||
* @build TestEnableNativeAccessJarManifest
|
||||
* panama_module/*
|
||||
* org.openjdk.foreigntest.unnamed.PanamaMainUnnamedModule
|
||||
* @run testng/native TestEnableNativeAccessJarManifest
|
||||
*/
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.ArrayList;
|
||||
import java.util.jar.Attributes;
|
||||
import java.util.jar.Manifest;
|
||||
|
||||
import jdk.test.lib.process.OutputAnalyzer;
|
||||
import jdk.test.lib.process.ProcessTools;
|
||||
import jdk.test.lib.util.JarUtils;
|
||||
|
||||
import org.testng.annotations.Test;
|
||||
import org.testng.annotations.DataProvider;
|
||||
|
||||
public class TestEnableNativeAccessJarManifest extends TestEnableNativeAccessBase {
|
||||
|
||||
private static final String REINVOKER = "TestEnableNativeAccessJarManifest$Reinvoker";
|
||||
|
||||
static record Attribute(String name, String value) {}
|
||||
|
||||
@Test(dataProvider = "cases")
|
||||
public void testEnableNativeAccessInJarManifest(String action, String cls, Result expectedResult,
|
||||
List<Attribute> attributes, List<String> vmArgs, List<String> programArgs) throws Exception {
|
||||
Manifest man = new Manifest();
|
||||
Attributes attrs = man.getMainAttributes();
|
||||
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
|
||||
attrs.put(Attributes.Name.MAIN_CLASS, cls);
|
||||
|
||||
for (Attribute attrib : attributes) {
|
||||
attrs.put(new Attributes.Name(attrib.name()), attrib.value());
|
||||
}
|
||||
|
||||
// create the JAR file with Test1 and Test2
|
||||
Path jarfile = Paths.get(action + ".jar");
|
||||
Files.deleteIfExists(jarfile);
|
||||
|
||||
Path classes = Paths.get(System.getProperty("test.classes", ""));
|
||||
JarUtils.createJarFile(jarfile, man, classes, Paths.get(cls.replace('.', '/') + ".class"));
|
||||
|
||||
// java -jar test.jar
|
||||
List<String> command = new ArrayList<>(List.of(
|
||||
"-Djava.library.path=" + System.getProperty("java.library.path")
|
||||
));
|
||||
command.addAll(vmArgs);
|
||||
command.add("-jar");
|
||||
command.add(jarfile.toString());
|
||||
command.addAll(programArgs);
|
||||
OutputAnalyzer outputAnalyzer = ProcessTools.executeTestJava(command.toArray(String[]::new))
|
||||
.outputTo(System.out)
|
||||
.errorTo(System.out);
|
||||
checkResult(expectedResult, outputAnalyzer);
|
||||
}
|
||||
|
||||
@DataProvider
|
||||
public Object[][] cases() {
|
||||
return new Object[][] {
|
||||
// simple cases where a jar contains a single main class with no dependencies
|
||||
{ "panama_no_unnamed_module_native_access", UNNAMED, successWithWarning("ALL-UNNAMED"),
|
||||
List.of(), List.of(), List.of() },
|
||||
{ "panama_unnamed_module_native_access", UNNAMED, successNoWarning(),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")), List.of(), List.of() },
|
||||
{ "panama_unnamed_module_native_access_invalid", UNNAMED,
|
||||
failWithError("Error: illegal value \"asdf\" for Enable-Native-Access manifest attribute. Only ALL-UNNAMED is allowed"),
|
||||
List.of(new Attribute("Enable-Native-Access", "asdf")), List.of(), List.of() },
|
||||
|
||||
// more complex cases where a jar invokes a module on the module path that does native access
|
||||
{ "panama_enable_native_access_false", REINVOKER, successWithWarning("panama_module"),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
|
||||
List.of(PANAMA_MAIN_CLS) },
|
||||
{ "panama_enable_native_access_reflection_false", REINVOKER, successWithWarning("panama_module"),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
|
||||
List.of(PANAMA_REFLECTION_CLS) },
|
||||
{ "panama_enable_native_access_invoke_false", REINVOKER, successWithWarning("panama_module"),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module"),
|
||||
List.of(PANAMA_INVOKE_CLS) },
|
||||
|
||||
{ "panama_enable_native_access_true", REINVOKER, successNoWarning(),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
|
||||
List.of(PANAMA_MAIN_CLS) },
|
||||
{ "panama_enable_native_access_reflection_true", REINVOKER, successNoWarning(),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
|
||||
List.of(PANAMA_REFLECTION_CLS) },
|
||||
{ "panama_enable_native_access_invoke_true", REINVOKER, successNoWarning(),
|
||||
List.of(new Attribute("Enable-Native-Access", "ALL-UNNAMED")),
|
||||
List.of("-p", MODULE_PATH, "--add-modules=panama_module", "--enable-native-access=panama_module"),
|
||||
List.of(PANAMA_INVOKE_CLS) }
|
||||
};
|
||||
}
|
||||
|
||||
public class Reinvoker {
|
||||
public static void main(String[] args) throws Throwable {
|
||||
Class<?> realMainClass = Class.forName(args[0]);
|
||||
realMainClass.getMethod("main", String[].class).invoke(null, (Object) new String[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2022, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.foreigntest.unnamed;
|
||||
|
||||
import java.lang.foreign.*;
|
||||
import java.lang.foreign.Linker.Option;
|
||||
import java.lang.invoke.MethodHandle;
|
||||
import java.lang.invoke.MethodHandles;
|
||||
import java.lang.invoke.MethodType;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class PanamaMainUnnamedModule {
|
||||
|
||||
static {
|
||||
System.loadLibrary("LinkerInvokerUnnamed");
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Throwable {
|
||||
testReflection();
|
||||
testInvoke();
|
||||
testDirectAccess();
|
||||
testJNIAccess();
|
||||
}
|
||||
|
||||
public static void testReflection() throws Throwable {
|
||||
Linker linker = Linker.nativeLinker();
|
||||
Method method = Linker.class.getDeclaredMethod("downcallHandle", FunctionDescriptor.class, Option[].class);
|
||||
method.invoke(linker, FunctionDescriptor.ofVoid(), new Linker.Option[0]);
|
||||
}
|
||||
|
||||
public static void testInvoke() throws Throwable {
|
||||
var mh = MethodHandles.lookup().findVirtual(Linker.class, "downcallHandle",
|
||||
MethodType.methodType(MethodHandle.class, FunctionDescriptor.class, Linker.Option[].class));
|
||||
var downcall = (MethodHandle)mh.invokeExact(Linker.nativeLinker(), FunctionDescriptor.ofVoid(), new Linker.Option[0]);
|
||||
}
|
||||
|
||||
public static void testDirectAccess() {
|
||||
Linker.nativeLinker().downcallHandle(FunctionDescriptor.ofVoid());
|
||||
}
|
||||
|
||||
public static void testJNIAccess() {
|
||||
nativeLinker0();
|
||||
}
|
||||
|
||||
static native void nativeLinker0();
|
||||
}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 2024, 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 "jni.h"
|
||||
#include "testlib_threads.hpp"
|
||||
|
||||
void call(void* ctxt) {
|
||||
JavaVM* jvm = (JavaVM*) ctxt;
|
||||
JNIEnv* env;
|
||||
jvm->AttachCurrentThread((void**)&env, nullptr);
|
||||
jclass linkerClass = env->FindClass("java/lang/foreign/Linker");
|
||||
jmethodID nativeLinkerMethod = env->GetStaticMethodID(linkerClass, "nativeLinker", "()Ljava/lang/foreign/Linker;");
|
||||
env->CallStaticVoidMethod(linkerClass, nativeLinkerMethod);
|
||||
jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
JNIEXPORT void JNICALL
|
||||
Java_org_openjdk_foreigntest_unnamed_PanamaMainUnnamedModule_nativeLinker0(JNIEnv *env, jclass cls) {
|
||||
JavaVM* jvm;
|
||||
env->GetJavaVM(&jvm);
|
||||
run_in_new_thread_and_join(call, jvm);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
module panama_jni_def_module {
|
||||
exports org.openjdk.jni.def;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.jni.def;
|
||||
|
||||
import java.lang.foreign.FunctionDescriptor;
|
||||
import java.lang.foreign.Linker;
|
||||
|
||||
public class PanamaJNIDef {
|
||||
|
||||
public static native void nativeLinker0(Linker linker, FunctionDescriptor desc, Linker.Option[] options);
|
||||
}
|
||||
|
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
* Copyright (c) 2022, 2024, 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 "jni.h"
|
||||
#include "testlib_threads.hpp"
|
||||
|
||||
typedef struct {
|
||||
JavaVM* jvm;
|
||||
jobject linker;
|
||||
jobject desc;
|
||||
jobject opts;
|
||||
jthrowable exception;
|
||||
} Context;
|
||||
|
||||
void call(void* arg) {
|
||||
Context* context = (Context*)arg;
|
||||
JNIEnv* env;
|
||||
context->jvm->AttachCurrentThread((void**)&env, nullptr);
|
||||
jclass linkerClass = env->FindClass("java/lang/foreign/Linker");
|
||||
jmethodID nativeLinkerMethod = env->GetMethodID(linkerClass, "downcallHandle",
|
||||
"(Ljava/lang/foreign/FunctionDescriptor;[Ljava/lang/foreign/Linker$Option;)Ljava/lang/invoke/MethodHandle;");
|
||||
env->CallVoidMethod(context->linker, nativeLinkerMethod, context->desc, context->opts);
|
||||
context->exception = (jthrowable) env->NewGlobalRef(env->ExceptionOccurred());
|
||||
env->ExceptionClear();
|
||||
context->jvm->DetachCurrentThread();
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
JNIEXPORT void JNICALL
|
||||
Java_org_openjdk_jni_def_PanamaJNIDef_nativeLinker0(JNIEnv *env, jclass cls, jobject linker, jobject desc, jobjectArray opts) {
|
||||
Context context;
|
||||
env->GetJavaVM(&context.jvm);
|
||||
context.linker = env->NewGlobalRef(linker);
|
||||
context.desc = env->NewGlobalRef(desc);
|
||||
context.opts = env->NewGlobalRef(opts);
|
||||
run_in_new_thread_and_join(call, &context);
|
||||
if (context.exception != nullptr) {
|
||||
env->Throw(context.exception); // transfer exception to this thread
|
||||
}
|
||||
env->DeleteGlobalRef(context.linker);
|
||||
env->DeleteGlobalRef(context.desc);
|
||||
env->DeleteGlobalRef(context.opts);
|
||||
env->DeleteGlobalRef(context.exception);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
module panama_jni_load_module {
|
||||
exports org.openjdk.jni;
|
||||
requires panama_jni_use_module;
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.jni;
|
||||
|
||||
import org.openjdk.jni.use.PanamaJNIUse;
|
||||
|
||||
public class PanamaMainJNI {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.loadLibrary("LinkerInvokerModule");
|
||||
PanamaJNIUse.run();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
module panama_jni_use_module {
|
||||
exports org.openjdk.jni.use;
|
||||
requires panama_jni_def_module;
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* Copyright (c) 2024, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.jni.use;
|
||||
|
||||
import java.lang.foreign.FunctionDescriptor;
|
||||
import java.lang.foreign.Linker;
|
||||
|
||||
import org.openjdk.jni.def.PanamaJNIDef;
|
||||
|
||||
public class PanamaJNIUse {
|
||||
public static void run() {
|
||||
testDirectAccessCLinker();
|
||||
}
|
||||
|
||||
public static void testDirectAccessCLinker() {
|
||||
System.out.println("Trying to get downcall handle");
|
||||
PanamaJNIDef.nativeLinker0(Linker.nativeLinker(), FunctionDescriptor.ofVoid(), new Linker.Option[0]);
|
||||
System.out.println("Got downcall handle");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2022, 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.
|
||||
*/
|
||||
|
||||
module panama_module {
|
||||
exports org.openjdk.foreigntest;
|
||||
}
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2023, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.foreigntest;
|
||||
|
||||
import java.lang.foreign.*;
|
||||
import java.lang.foreign.Arena;
|
||||
|
||||
public class PanamaMainDirect {
|
||||
public static void main(String[] args) {
|
||||
testDirectAccessCLinker();
|
||||
testDirectAccessMemorySegment();
|
||||
}
|
||||
|
||||
public static void testDirectAccessCLinker() {
|
||||
System.out.println("Trying to obtain a downcall handle");
|
||||
Linker.nativeLinker().downcallHandle(FunctionDescriptor.ofVoid());
|
||||
System.out.println("Got downcall handle");
|
||||
}
|
||||
|
||||
public static void testDirectAccessMemorySegment() {
|
||||
System.out.println("Trying to get MemorySegment");
|
||||
MemorySegment.NULL.reinterpret(10);
|
||||
System.out.println("Got MemorySegment");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2023, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.foreigntest;
|
||||
|
||||
import java.lang.foreign.*;
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.invoke.*;
|
||||
|
||||
public class PanamaMainInvoke {
|
||||
public static void main(String[] args) throws Throwable {
|
||||
testInvokenativeLinker();
|
||||
testInvokeMemorySegment();
|
||||
}
|
||||
|
||||
public static void testInvokenativeLinker() throws Throwable {
|
||||
Linker linker = Linker.nativeLinker();
|
||||
System.out.println("Trying to obtain a downcall handle");
|
||||
var mh = MethodHandles.lookup().findVirtual(Linker.class, "downcallHandle",
|
||||
MethodType.methodType(MethodHandle.class, FunctionDescriptor.class, Linker.Option[].class));
|
||||
var handle = (MethodHandle)mh.invokeExact(linker, FunctionDescriptor.ofVoid(), new Linker.Option[0]);
|
||||
System.out.println("Got downcall handle");
|
||||
}
|
||||
|
||||
public static void testInvokeMemorySegment() throws Throwable {
|
||||
System.out.println("Trying to get MemorySegment");
|
||||
var mh = MethodHandles.lookup().findVirtual(MemorySegment.class, "reinterpret",
|
||||
MethodType.methodType(MemorySegment.class, long.class));
|
||||
var seg = (MemorySegment)mh.invokeExact(MemorySegment.NULL, 10L);
|
||||
System.out.println("Got MemorySegment");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,51 @@
|
|||
/*
|
||||
* Copyright (c) 2021, 2023, 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.
|
||||
*/
|
||||
|
||||
package org.openjdk.foreigntest;
|
||||
|
||||
import java.lang.foreign.*;
|
||||
import java.lang.foreign.Arena;
|
||||
import java.lang.foreign.Linker.Option;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
public class PanamaMainReflection {
|
||||
public static void main(String[] args) throws Throwable {
|
||||
testReflectionnativeLinker();
|
||||
testReflectionMemorySegment();
|
||||
}
|
||||
|
||||
public static void testReflectionnativeLinker() throws Throwable {
|
||||
Linker linker = Linker.nativeLinker();
|
||||
System.out.println("Trying to get downcall handle");
|
||||
Method method = Linker.class.getDeclaredMethod("downcallHandle", FunctionDescriptor.class, Option[].class);
|
||||
method.invoke(linker, FunctionDescriptor.ofVoid(), new Linker.Option[0]);
|
||||
System.out.println("Got downcall handle");
|
||||
}
|
||||
|
||||
public static void testReflectionMemorySegment() throws Throwable {
|
||||
System.out.println("Trying to get MemorySegment");
|
||||
Method method = MemorySegment.class.getDeclaredMethod("reinterpret", long.class);
|
||||
method.invoke(MemorySegment.NULL, 10L);
|
||||
System.out.println("Got MemorySegment");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue