diff --git a/src/share/classes/com/sun/javatest/regtest/exec/MainAction.java b/src/share/classes/com/sun/javatest/regtest/exec/MainAction.java index 722a1f04..5623c581 100644 --- a/src/share/classes/com/sun/javatest/regtest/exec/MainAction.java +++ b/src/share/classes/com/sun/javatest/regtest/exec/MainAction.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 1998, 2025, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 1998, 2026, 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 @@ -188,6 +188,7 @@ void init(Map opts, List args, String reason, othervmOverrideReasons.add("test or library uses bootclasspath"); } + boolean seenEnableNativeAccess = false; boolean seenEnablePreview = false; // separate the arguments into the options to java, the // classname and the parameters to the named class @@ -195,6 +196,9 @@ void init(Map opts, List args, String reason, String arg = args.get(i); if (testClassName == null) { if (arg.startsWith("-")) { + if (arg.startsWith("--enable-native-access")) { + seenEnableNativeAccess = true; + } if (arg.equals("--enable-preview")) { seenEnablePreview = true; } @@ -231,6 +235,14 @@ void init(Map opts, List args, String reason, throw new ParseException(PARSE_SECURE_OTHERVM); } + if (nativeCode && !seenEnableNativeAccess) { + // TODO Only add argument on Test JDK 17 or higher + testJavaArgs.add("--enable-native-access=ALL-UNNAMED"); + if (!othervm) { + othervmOverrideReasons.add("/native test requires --enable-native-access=ALL-UNNAMED"); + } + } + if (!script.disablePreview()) { // test with explicit `@enablePreview false` take precedence boolean needsEnablePreview = script.enablePreview(); if (needsEnablePreview && !seenEnablePreview) { diff --git a/test/nativepath/NativeAccessEnabled.java b/test/nativepath/NativeAccessEnabled.java new file mode 100644 index 00000000..bd5ebc6a --- /dev/null +++ b/test/nativepath/NativeAccessEnabled.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2026, 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 + * @bug 7904172 + * @requires jdk.version.major >= 22 + * @run main/native NativeAccessEnabled + */ + +public class NativeAccessEnabled { + public static void main(String[] args) { + var module = NativeAccessEnabled.class.getClassLoader().getUnnamedModule(); + if (!module.isNativeAccessEnabled()) // Module#isNativeAccessEnabled requires Java 22+ + throw new Error("ALL-UNNAMED modules should have native access enabled"); + } +} diff --git a/test/nativepath/TestNativePath.gmk b/test/nativepath/TestNativePath.gmk index 6da78a05..c8062e45 100644 --- a/test/nativepath/TestNativePath.gmk +++ b/test/nativepath/TestNativePath.gmk @@ -1,5 +1,5 @@ # -# Copyright (c) 2014, 2025, Oracle and/or its affiliates. All rights reserved. +# Copyright (c) 2014, 2026, 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 @@ -150,3 +150,27 @@ testnativepath: \ $(BUILDTESTDIR)/TestNativePath.agentvm.ok \ $(BUILDTESTDIR)/TestNativePath.othervm.ok \ $(BUILDTESTDIR)/TestRelativeNativePath.ok + +ifdef JDK25HOME + +# verify that --enable-native-access=ALL-UNNAMED is added +# for a test with the /native argument +$(BUILDTESTDIR)/TestNativeAccessEnabled.ok: \ + $(JTREG_IMAGEDIR)/lib/javatest.jar \ + $(JTREG_IMAGEDIR)/lib/jtreg.jar \ + $(JTREG_IMAGEDIR)/bin/jtreg + + cd $(BUILDTESTDIR); $(JTREG_IMAGEDIR)/bin/jtreg $(JTREG_OPTS) \ + -jdk:$(JDK25HOME) \ + -nativepath:$(NATIVEPATH) \ + -w:$(@:%.ok=%nativeaccessenabled)/work \ + -r:$(@:%.ok=%nativeaccessenabled)/report \ + -verbose:fail \ + $(ABS_TESTDIR)/nativepath/NativeAccessEnabled.java + + echo "$@ test passed at `date`" > $@ + +TESTS.jtreg += $(BUILDTESTDIR)/TestNativeAccessEnabled.ok +testnativepath += $(BUILDTESTDIR)/TestNativeAccessEnabled.ok + +endif