From 75db5844d7d0d4bbb1757554c47c2d17254cb5b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Thu, 31 Mar 2022 14:08:53 +0200 Subject: [PATCH 01/41] Default to running image-builder on module path --- .../src/com/oracle/svm/driver/NativeImage.java | 2 +- .../src/com/oracle/svm/util/ModuleSupportBase.java | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index 1dfa71bbff4d..c6142968aa2f 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -279,7 +279,7 @@ protected BuildConfiguration(List args) { @SuppressWarnings("deprecation") BuildConfiguration(Path rootDir, Path workDir, List args) { - modulePathBuild = Boolean.parseBoolean(System.getenv().get(ModuleSupport.ENV_VAR_USE_MODULE_SYSTEM)); + modulePathBuild = ModuleSupport.isModulePathBuild(); this.args = args; this.workDir = workDir != null ? workDir : Paths.get(".").toAbsolutePath().normalize(); if (rootDir != null) { diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java index 81db8ea69e57..2ccb6d7e5bfa 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java @@ -31,6 +31,9 @@ public class ModuleSupportBase { public static final String PROPERTY_IMAGE_EXPLICITLY_ADDED_MODULES = "org.graalvm.nativeimage.module.addmods"; - public static final boolean modulePathBuild = Boolean.parseBoolean(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); + public static final boolean modulePathBuild = isModulePathBuild(); + public static boolean isModulePathBuild() { + return !"false".equalsIgnoreCase(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); + } } From f25c9ed870db3a7867896bc3069fdd48a4ba6075 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 4 Apr 2022 16:52:29 +0200 Subject: [PATCH 02/41] Consolidate ModuleSupport methods --- .../svm/configure/config/SignatureUtil.java | 2 +- .../svm/core/graal/llvm/LLVMFeature.java | 2 +- .../com/oracle/svm/core/jfr/JfrFeature.java | 6 +- .../svm/hosted/ClassLoaderSupportImpl.java | 2 +- .../oracle/svm/hosted/ModuleLayerFeature.java | 2 +- .../hosted/NativeImageClassLoaderSupport.java | 2 +- .../hosted/NativeImageGeneratorRunner.java | 26 ++-- .../svm/hosted/SecurityServicesFeature.java | 4 +- .../svm/hosted/c/codegen/QueryCodeWriter.java | 2 +- .../phases/AnalysisGraphBuilderPhase.java | 4 +- .../hosted/snippets/ReflectionPlugins.java | 2 +- .../reflect/hosted/ReflectionDataBuilder.java | 2 +- .../svm/reflect/hosted/ReflectionFeature.java | 2 +- .../src/com/oracle/svm/test/jfr/JfrTest.java | 2 +- .../com/oracle/svm/util/ModuleSupport.java | 130 +++++++++--------- .../com/oracle/svm/util/ReflectionUtil.java | 2 +- 16 files changed, 97 insertions(+), 95 deletions(-) diff --git a/substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/SignatureUtil.java b/substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/SignatureUtil.java index 7c565ea92306..077fc21fdee5 100644 --- a/substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/SignatureUtil.java +++ b/substratevm/src/com.oracle.svm.configure/src/com/oracle/svm/configure/config/SignatureUtil.java @@ -106,7 +106,7 @@ class SignatureUtilFeature implements Feature { @Override public void afterRegistration(AfterRegistrationAccess access) { if (!access.getApplicationClassPath().isEmpty()) { - ModuleSupport.exportAndOpenPackageToClass("jdk.internal.vm.ci", "jdk.vm.ci.meta", false, SignatureUtil.class); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, SignatureUtil.class, false, "jdk.internal.vm.ci", "jdk.vm.ci.meta"); } } } diff --git a/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java b/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java index 10171fca4b3a..4eac4bdc8cb0 100644 --- a/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java +++ b/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java @@ -90,7 +90,7 @@ public boolean isInConfiguration(IsInConfigurationAccess access) { @Override public void afterRegistration(AfterRegistrationAccess access) { if (ModuleSupport.modulePathBuild) { - ModuleSupport.openModuleByClass(LLVMIntrinsicNode.class, NodeClass.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, NodeClass.class, LLVMIntrinsicNode.class); } ImageSingletons.add(SubstrateBackendFactory.class, new SubstrateBackendFactory() { diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java index 54d145c89b14..94fd523f0279 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java @@ -147,8 +147,10 @@ public List> getRequiredFeatures() { @Override public void afterRegistration(AfterRegistrationAccess access) { - ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.jfr", false); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("java.base", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "jdk.jfr"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base"); + //ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrFeature.class, false, "jdk.jfr", "jdk.jfr.events"); + //ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrEventSubstitution.class, false, "jdk.internal.vm.ci", "jdk.vm.ci.hotspot"); // Initialize some parts of JFR/JFC at image build time. List knownConfigurations = JFC.getConfigurations(); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java index 07727d9507b6..4cb74191d76d 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java @@ -222,7 +222,7 @@ public List getResourceBundle(String bundleSpec, Locale locale) } ArrayList resourceBundles = new ArrayList<>(); for (Module module : modules) { - ModuleSupport.exportAndOpenPackageToClass(module.getName(), packageName, false, ClassLoaderSupportImpl.class); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, ClassLoaderSupportImpl.class, false, module.getName(), packageName); resourceBundles.add(ResourceBundle.getBundle(bundleName, locale, module)); } return resourceBundles; diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ModuleLayerFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ModuleLayerFeature.java index 48c9cdb21b55..e1f8eac529b6 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ModuleLayerFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ModuleLayerFeature.java @@ -378,7 +378,7 @@ private static final class ModuleLayerFeatureUtils { imageClassLoader = cl; Method classGetDeclaredMethods0Method = ReflectionUtil.lookupMethod(Class.class, "getDeclaredFields0", boolean.class); try { - ModuleSupport.openModuleByClass(Module.class, ModuleLayerFeature.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, ModuleLayerFeature.class, Module.class); Field[] moduleClassFields = (Field[]) classGetDeclaredMethods0Method.invoke(Module.class, false); Field everyoneModuleField = findFieldByName(moduleClassFields, "EVERYONE_MODULE"); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index 1c264368b80c..0d65e2047b9e 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -321,7 +321,7 @@ private ClassLoader getSingleClassloader(ModuleLayer moduleLayer) { private static void implAddReadsAllUnnamed(Module module) { try { Method implAddReadsAllUnnamed = Module.class.getDeclaredMethod("implAddReadsAllUnnamed"); - ModuleSupport.openModuleByClass(Module.class, NativeImageClassLoaderSupport.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, NativeImageClassLoaderSupport.class, Module.class); implAddReadsAllUnnamed.setAccessible(true); implAddReadsAllUnnamed.invoke(module); } catch (ReflectiveOperationException | NoSuchElementException e) { diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java index f1f70a69599a..20848e189165 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageGeneratorRunner.java @@ -595,22 +595,22 @@ public int build(ImageClassLoader imageClassLoader) { public static class JDK9Plus { public static void main(String[] args) { - ModuleSupport.exportAndOpenAllPackagesToUnnamed("org.graalvm.sdk", false); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("org.graalvm.truffle", false); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.internal.vm.ci", false); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.internal.vm.compiler", false); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("jdk.internal.vm.compiler.management", true); - ModuleSupport.exportAndOpenAllPackagesToUnnamed("com.oracle.graal.graal_enterprise", true); - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "jdk.internal.loader", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "org.graalvm.sdk"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "org.graalvm.truffle"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "jdk.internal.vm.ci"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "jdk.internal.vm.compiler"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, true, "jdk.internal.vm.compiler.management"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, true, "com.oracle.graal.graal_enterprise"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "jdk.internal.loader"); if (JavaVersionUtil.JAVA_SPEC >= 17) { - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "jdk.internal.misc", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "jdk.internal.misc"); } - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "sun.text.spi", false); - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "jdk.internal.org.objectweb.asm", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "sun.text.spi"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "jdk.internal.org.objectweb.asm"); if (JavaVersionUtil.JAVA_SPEC >= 17) { - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "sun.reflect.annotation", false); - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "sun.security.jca", false); - ModuleSupport.exportAndOpenPackageToUnnamed("jdk.jdeps", "com.sun.tools.classfile", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "sun.reflect.annotation"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "sun.security.jca"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "jdk.jdeps", "com.sun.tools.classfile"); } NativeImageGeneratorRunner.main(args); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SecurityServicesFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SecurityServicesFeature.java index de20695462f7..17a68cbfd63a 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SecurityServicesFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/SecurityServicesFeature.java @@ -203,8 +203,8 @@ public static class Options { @Override public void afterRegistration(AfterRegistrationAccess a) { - ModuleSupport.exportAndOpenPackageToClass("java.base", "sun.security.x509", false, getClass()); - ModuleSupport.openModuleByClass(Security.class, getClass()); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, getClass(), false, "java.base", "sun.security.x509"); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, getClass(), Security.class); disableExperimentalFipsMode(a); ImageSingletons.add(SecurityProvidersFilter.class, this); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/QueryCodeWriter.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/QueryCodeWriter.java index c38ddd97daae..4d45f11e3dc2 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/QueryCodeWriter.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/c/codegen/QueryCodeWriter.java @@ -32,7 +32,6 @@ import java.util.Arrays; import java.util.List; -import com.oracle.svm.hosted.c.info.RawPointerToInfo; import org.graalvm.nativeimage.Platform; import org.graalvm.nativeimage.c.CContext; @@ -43,6 +42,7 @@ import com.oracle.svm.hosted.c.info.InfoTreeVisitor; import com.oracle.svm.hosted.c.info.NativeCodeInfo; import com.oracle.svm.hosted.c.info.PointerToInfo; +import com.oracle.svm.hosted.c.info.RawPointerToInfo; import com.oracle.svm.hosted.c.info.RawStructureInfo; import com.oracle.svm.hosted.c.info.SizableInfo.ElementKind; import com.oracle.svm.hosted.c.info.SizableInfo.SignednessValue; diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/AnalysisGraphBuilderPhase.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/AnalysisGraphBuilderPhase.java index 264501904c61..81bdfabc7b1b 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/AnalysisGraphBuilderPhase.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/phases/AnalysisGraphBuilderPhase.java @@ -81,8 +81,8 @@ protected boolean applyInvocationPlugin(InvokeKind invokeKind, ValueNode[] args, * bits of graal. Thus the modules that contain such plugins need to be allowed such * access. */ - ModuleSupport.exportAndOpenPackageToClass("jdk.internal.vm.ci", "jdk.vm.ci.meta", false, accessingClass); - ModuleSupport.exportAndOpenPackageToClass("jdk.internal.vm.compiler", "org.graalvm.compiler.nodes", false, accessingClass); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, accessingClass, false, "jdk.internal.vm.ci", "jdk.vm.ci.meta"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, accessingClass, false, "jdk.internal.vm.compiler", "org.graalvm.compiler.nodes"); return super.applyInvocationPlugin(invokeKind, args, targetMethod, resultType, plugin); } diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/ReflectionPlugins.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/ReflectionPlugins.java index f43f79d00560..fed44fc66652 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/ReflectionPlugins.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/snippets/ReflectionPlugins.java @@ -319,7 +319,7 @@ private boolean processClassGetClassLoader(GraphBuilderContext b, ResolvedJavaMe */ private void registerFoldInvocationPlugins(InvocationPlugins plugins, Class declaringClass, String... methodNames) { Set methodNamesSet = new HashSet<>(Arrays.asList(methodNames)); - ModuleSupport.openModuleByClass(declaringClass, ReflectionPlugins.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, ReflectionPlugins.class, declaringClass); for (Method method : declaringClass.getDeclaredMethods()) { if (methodNamesSet.contains(method.getName()) && !method.isSynthetic()) { registerFoldInvocationPlugin(plugins, method); diff --git a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionDataBuilder.java b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionDataBuilder.java index 23b00f48c568..bcf59e2618c0 100644 --- a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionDataBuilder.java +++ b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionDataBuilder.java @@ -594,7 +594,7 @@ private static void registerTypesForAnnotationValue(DuringAnalysisAccessImpl acc * annotation types at runtime. */ ImageSingletons.lookup(AnnotationTypeSupport.class).createInstance((Class) type); - ModuleSupport.openModuleByClass(type, ReflectionDataBuilder.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, ReflectionDataBuilder.class, type); ImageSingletons.lookup(DynamicProxyRegistry.class).addProxyClass(type); Annotation annotation = (Annotation) value; diff --git a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionFeature.java b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionFeature.java index 63af073e09b3..f787a8a47b6a 100644 --- a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionFeature.java +++ b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/hosted/ReflectionFeature.java @@ -200,7 +200,7 @@ private MethodPointer register(ResolvedJavaMethod method) { @Override public void afterRegistration(AfterRegistrationAccess access) { - ModuleSupport.exportAndOpenPackageToUnnamed("java.base", "jdk.internal.reflect", false); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base", "jdk.internal.reflect"); reflectionData = new ReflectionDataBuilder(); ImageSingletons.add(RuntimeReflectionSupport.class, reflectionData); diff --git a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/JfrTest.java b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/JfrTest.java index 56b5a2205519..a75203702ec4 100644 --- a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/JfrTest.java +++ b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/jfr/JfrTest.java @@ -136,6 +136,6 @@ public void afterRegistration(AfterRegistrationAccess access) { * Use of org.graalvm.compiler.serviceprovider.JavaVersionUtil.JAVA_SPEC in * com.oracle.svm.test.jfr.utils.poolparsers.ClassConstantPoolParser.parse */ - ModuleSupport.exportAndOpenPackageToClass("jdk.internal.vm.compiler", "org.graalvm.compiler.serviceprovider", false, JFRTestFeature.class); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JFRTestFeature.class, false, "jdk.internal.vm.compiler", "org.graalvm.compiler.serviceprovider"); } } diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index a9eeb19511dc..f5201779e97f 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -25,6 +25,7 @@ package com.oracle.svm.util; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -38,86 +39,85 @@ public final class ModuleSupport extends ModuleSupportBase { private ModuleSupport() { } - public static void openModuleByClass(Class declaringClass, Class accessingClass) { - Module declaringModule = declaringClass.getModule(); - String packageName = declaringClass.getPackageName(); - Module namedAccessingModule = null; - if (accessingClass != null) { - Module accessingModule = accessingClass.getModule(); - if (accessingModule.isNamed()) { - namedAccessingModule = accessingModule; + public enum Access { + OPEN { + @Override + void giveAccess(Module accessingModule, Module declaringModule, String packageName) { + if (accessingModule != null) { + if (declaringModule.isOpen(packageName, accessingModule)) { + return; + } + Modules.addOpens(declaringModule, packageName, accessingModule); + } else { + if (declaringModule.isOpen(packageName)) { + return; + } + Modules.addOpensToAllUnnamed(declaringModule, packageName); + } } - } - if (namedAccessingModule != null ? declaringModule.isOpen(packageName, namedAccessingModule) : declaringModule.isOpen(packageName)) { - return; - } - if (namedAccessingModule != null) { - Modules.addOpens(declaringModule, packageName, namedAccessingModule); - } else { - Modules.addOpensToAllUnnamed(declaringModule, packageName); - } + }, + EXPORT { + @Override + void giveAccess(Module accessingModule, Module declaringModule, String packageName) { + if (accessingModule != null) { + if (declaringModule.isExported(packageName, accessingModule)) { + return; + } + Modules.addExports(declaringModule, packageName, accessingModule); + } else { + if (declaringModule.isExported(packageName)) { + return; + } + Modules.addExportsToAllUnnamed(declaringModule, packageName); + } + } + }; + + abstract void giveAccess(Module accessingModule, Module declaringModule, String packageName); + } + + public static void accessModuleByClass(Access access, Class accessingClass, Class declaringClass) { + accessModuleByClass(access, accessingClass, declaringClass.getModule(), declaringClass.getPackageName()); } /** - * Exports and opens a single package {@code packageName} in the module named {@code moduleName} - * to all unnamed modules. + * Open or export packages {@code packageNames} in the module named {@code moduleName} to module + * of given {@code accessingClass}. If {@code accessingClass} is null packages are opened or + * exported to ALL-UNNAMED. If no packages are given, all packages of the module are opened or + * exported. */ - @SuppressWarnings("unused") - public static void exportAndOpenPackageToClass(String moduleName, String packageName, boolean optional, Class accessingClass) { - Optional value = ModuleLayer.boot().findModule(moduleName); - if (value.isEmpty()) { - if (!optional) { - throw new NoSuchElementException(moduleName); - } + public static void accessPackagesToClass(Access access, Class accessingClass, boolean optional, String moduleName, String... packageNames) { + Module declaringModule = getModule(moduleName, optional); + if (declaringModule == null) { return; } - Module declaringModule = value.get(); - Module accessingModule = accessingClass == null ? null : accessingClass.getModule(); - if (accessingModule != null && accessingModule.isNamed()) { - if (!declaringModule.isOpen(packageName, accessingModule)) { - Modules.addOpens(declaringModule, packageName, accessingModule); - } - } else { - Modules.addOpensToAllUnnamed(declaringModule, packageName); + Objects.requireNonNull(packageNames); + Set packages = packageNames.length > 0 ? Set.of(packageNames) : declaringModule.getPackages(); + for (String packageName : packages) { + accessModuleByClass(access, accessingClass, declaringModule, packageName); } - } - /** - * Exports and opens all packages in the module named {@code name} to all unnamed modules. - */ - @SuppressWarnings("unused") - public static void exportAndOpenAllPackagesToUnnamed(String name, boolean optional) { - Optional value = ModuleLayer.boot().findModule(name); - if (value.isEmpty()) { - if (!optional) { - throw new NoSuchElementException("No module in boot layer named " + name + ". Available modules: " + ModuleLayer.boot()); + private static Module getModule(String moduleName, boolean optional) { + Objects.requireNonNull(moduleName); + Optional declaringModuleOpt = ModuleLayer.boot().findModule(moduleName); + if (declaringModuleOpt.isEmpty()) { + if (optional) { + return null; } - return; - } - Module module = value.get(); - Set packages = module.getPackages(); - for (String pkg : packages) { - Modules.addExportsToAllUnnamed(module, pkg); - Modules.addOpensToAllUnnamed(module, pkg); + throw new NoSuchElementException(moduleName); } + return declaringModuleOpt.get(); } - /** - * Exports and opens a single package {@code pkg} in the module named {@code name} to all - * unnamed modules. - */ - @SuppressWarnings("unused") - public static void exportAndOpenPackageToUnnamed(String name, String pkg, boolean optional) { - Optional value = ModuleLayer.boot().findModule(name); - if (value.isEmpty()) { - if (!optional) { - throw new NoSuchElementException(name); + private static void accessModuleByClass(Access access, Class accessingClass, Module declaringModule, String packageName) { + Module namedAccessingModule = null; + if (accessingClass != null) { + Module accessingModule = accessingClass.getModule(); + if (accessingModule.isNamed()) { + namedAccessingModule = accessingModule; } - return; } - Module module = value.get(); - Modules.addExportsToAllUnnamed(module, pkg); - Modules.addOpensToAllUnnamed(module, pkg); + access.giveAccess(namedAccessingModule, declaringModule, packageName); } } diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ReflectionUtil.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ReflectionUtil.java index 092a745c2929..5bb73141430e 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ReflectionUtil.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ReflectionUtil.java @@ -51,7 +51,7 @@ private ReflectionUtil() { * declaring class. */ private static void openModule(Class declaringClass) { - ModuleSupport.openModuleByClass(declaringClass, ReflectionUtil.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, ReflectionUtil.class, declaringClass); } public static Method lookupMethod(Class declaringClass, String methodName, Class... parameterTypes) { From ee8bb44764ca947a2ff81a21d74def9121b79732 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 4 Apr 2022 19:48:21 +0200 Subject: [PATCH 03/41] Remove unqualified exports of org.graalvm.nativeimage.builder packages --- substratevm/mx.substratevm/suite.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index 0dac4c9c8c1f..7fdada667595 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -1126,13 +1126,6 @@ "moduleInfo" : { "name" : "org.graalvm.nativeimage.builder", "exports" : [ - "com.oracle.svm.core.configure", # even Feature impls on class-path need access, thus unqualified - "com.oracle.svm.core.jdk", # Uses of com.oracle.svm.core.jdk.StackTraceUtils - "com.oracle.svm.core.snippets", # Uses of com.oracle.svm.core.snippets.KnownIntrinsics - "com.oracle.svm.core", # Uses of com.oracle.svm.core.TypeResult - "com.oracle.svm.core.util", # Uses of com.oracle.svm.core.util.VMError - "com.oracle.svm.core.jni", # Uses of com.oracle.svm.core.jni.JNIRuntimeAccess - "com.oracle.svm.core.jfr", # Uses of com.oracle.svm.core.jfr.HasJfrSupport "com.oracle.svm.hosted to java.base", "com.oracle.svm.hosted.agent to java.instrument", "com.oracle.svm.truffle.api to org.graalvm.truffle", From a2a4ed1b3b5205e7f3e2b5d4f81aa28b35e77939 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 4 Apr 2022 20:21:56 +0200 Subject: [PATCH 04/41] Only use native-image API in mx cinterfacetutorial --- substratevm/mx.substratevm/mx_substratevm.py | 2 +- .../src/com/oracle/svm/tutorial/CInterfaceTutorial.java | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/substratevm/mx.substratevm/mx_substratevm.py b/substratevm/mx.substratevm/mx_substratevm.py index 4bc07176efef..057f46c2a9a0 100644 --- a/substratevm/mx.substratevm/mx_substratevm.py +++ b/substratevm/mx.substratevm/mx_substratevm.py @@ -728,7 +728,7 @@ def _cinterfacetutorial(native_image, args=None): mkpath(build_dir) # Build the shared library from Java code - native_image(['--shared', '-H:Path=' + build_dir, '-H:Name=libcinterfacetutorial', + native_image(['--shared', '-H:Path=' + build_dir, '-H:Name=libcinterfacetutorial', '-Dcom.oracle.svm.tutorial.headerfile=' + join(c_source_dir, 'mydata.h'), '-H:CLibraryPath=' + tutorial_proj.dir, '-cp', tutorial_proj.output_dir()] + args) # Build the C executable diff --git a/substratevm/src/com.oracle.svm.tutorial/src/com/oracle/svm/tutorial/CInterfaceTutorial.java b/substratevm/src/com.oracle.svm.tutorial/src/com/oracle/svm/tutorial/CInterfaceTutorial.java index f1dd64457134..9c0ba664a36e 100644 --- a/substratevm/src/com.oracle.svm.tutorial/src/com/oracle/svm/tutorial/CInterfaceTutorial.java +++ b/substratevm/src/com.oracle.svm.tutorial/src/com/oracle/svm/tutorial/CInterfaceTutorial.java @@ -24,6 +24,7 @@ */ package com.oracle.svm.tutorial; +import java.nio.file.Path; import java.util.Collections; import java.util.Date; import java.util.List; @@ -60,8 +61,6 @@ import org.graalvm.word.UnsignedWord; import org.graalvm.word.WordFactory; -import com.oracle.svm.core.c.ProjectHeaderFile; -import com.oracle.svm.core.util.VMError; import com.oracle.svm.tutorial.CInterfaceTutorial.CInterfaceTutorialDirectives; @CContext(CInterfaceTutorialDirectives.class) @@ -75,7 +74,7 @@ public List getHeaderFiles() { * The header file with the C declarations that are imported. We use a helper class that * locates the file in our project structure. */ - return Collections.singletonList(ProjectHeaderFile.resolve("com.oracle.svm.tutorial", "native/mydata.h")); + return Collections.singletonList("\"" + Path.of(System.getProperty("com.oracle.svm.tutorial.headerfile")).toAbsolutePath() + "\""); } } @@ -298,7 +297,7 @@ interface Substruct1 extends Header { */ @CFieldOffset("header") static int offsetOfHeader() { - throw VMError.shouldNotReachHere("Calls to the method are replaced with a compile time constant for the offset, so this method body is not reachable."); + throw new RuntimeException("Calls to the method are replaced with a compile time constant for the offset, so this method body is not reachable."); } @CField From d74c506bc4cfe57101631cedcf8fd3aae6243db3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 5 Apr 2022 10:27:41 +0200 Subject: [PATCH 05/41] Fold ModuleSupportBase into ModuleSupport --- .../com/oracle/svm/util/ModuleSupport.java | 11 +++++- .../oracle/svm/util/ModuleSupportBase.java | 39 ------------------- 2 files changed, 10 insertions(+), 40 deletions(-) delete mode 100644 substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index f5201779e97f..141c9c199266 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -35,10 +35,19 @@ import jdk.internal.module.Modules; @Platforms(Platform.HOSTED_ONLY.class) -public final class ModuleSupport extends ModuleSupportBase { +public final class ModuleSupport { + + public static final String ENV_VAR_USE_MODULE_SYSTEM = "USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM"; + public static final String PROPERTY_IMAGE_EXPLICITLY_ADDED_MODULES = "org.graalvm.nativeimage.module.addmods"; + public static final boolean modulePathBuild = isModulePathBuild(); + private ModuleSupport() { } + public static boolean isModulePathBuild() { + return !"false".equalsIgnoreCase(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); + } + public enum Access { OPEN { @Override diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java deleted file mode 100644 index 2ccb6d7e5bfa..000000000000 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupportBase.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (c) 2021, 2021, 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. Oracle designates this - * particular file as subject to the "Classpath" exception as provided - * by Oracle in the LICENSE file that accompanied this code. - * - * 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 com.oracle.svm.util; - -public class ModuleSupportBase { - - public static final String ENV_VAR_USE_MODULE_SYSTEM = "USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM"; - - public static final String PROPERTY_IMAGE_EXPLICITLY_ADDED_MODULES = "org.graalvm.nativeimage.module.addmods"; - - public static final boolean modulePathBuild = isModulePathBuild(); - - public static boolean isModulePathBuild() { - return !"false".equalsIgnoreCase(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); - } -} From dfefd0041262c92becdf9c00634a4823a9473e9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 5 Apr 2022 10:46:20 +0200 Subject: [PATCH 06/41] Fix mx clinittest image-builder module access --- .../com.oracle.svm.test/native-image.properties | 1 + .../src/com/oracle/svm/test/NativeImageResourceUtils.java | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties index d6f7981996ce..ff506e29885b 100644 --- a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties +++ b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties @@ -8,4 +8,5 @@ Args= \ --features=com.oracle.svm.test.jfr.JFRTestFeature \ --add-opens=java.base/java.lang=ALL-UNNAMED \ -H:+AllowVMInspection \ + --add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED \ --add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.containers=ALL-UNNAMED diff --git a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/NativeImageResourceUtils.java b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/NativeImageResourceUtils.java index 933cdd239e19..a3fbed39cb54 100644 --- a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/NativeImageResourceUtils.java +++ b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/NativeImageResourceUtils.java @@ -40,6 +40,7 @@ import org.junit.Assert; import com.oracle.svm.core.configure.ResourcesRegistry; +import com.oracle.svm.util.ModuleSupport; public class NativeImageResourceUtils { @@ -49,6 +50,11 @@ public class NativeImageResourceUtils { // Register resources. public static final class TestFeature implements Feature { + @Override + public void afterRegistration(AfterRegistrationAccess access) { + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, null, false, "org.graalvm.nativeimage.builder", "com.oracle.svm.core.configure"); + } + @Override public void beforeAnalysis(BeforeAnalysisAccess access) { ResourcesRegistry registry = ImageSingletons.lookup(ResourcesRegistry.class); From db9f15552b531cf9c7a5e742e1c6fcab51c1fbd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 5 Apr 2022 11:11:25 +0200 Subject: [PATCH 07/41] Remove unused dependency --- substratevm/mx.substratevm/suite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index 7fdada667595..33433bbef1cc 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -773,7 +773,7 @@ "com.oracle.svm.tutorial" : { "subDir": "src", "sourceDirs" : ["src"], - "dependencies" : ["com.oracle.svm.core"], + "dependencies" : ["sdk:GRAAL_SDK"], "checkstyle" : "com.oracle.svm.hosted", "javaCompliance": "11+", "annotationProcessors" : [ From 161a42d27ab1a25e19526115667ba8a113beaa9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 5 Apr 2022 12:03:53 +0200 Subject: [PATCH 08/41] Open up builder to allow whitebox testing in mx native-unittest Switch native-unittest to default to running on module-path --- .../mx.substratevm/macro-junitcp.properties | 2 +- substratevm/mx.substratevm/mx_substratevm.py | 19 +++++++++---------- .../com/oracle/svm/junit/JUnitFeature.java | 3 +++ .../native-image.properties | 4 +--- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/substratevm/mx.substratevm/macro-junitcp.properties b/substratevm/mx.substratevm/macro-junitcp.properties index 11defff0efed..9ffa49e86046 100644 --- a/substratevm/mx.substratevm/macro-junitcp.properties +++ b/substratevm/mx.substratevm/macro-junitcp.properties @@ -8,4 +8,4 @@ Args = -H:Features=com.oracle.svm.junit.JUnitFeature \ -H:Class=com.oracle.svm.junit.SVMJUnitRunner \ -H:TestFile=${*} \ --initialize-at-build-time=org.junit,com.oracle.mxtool.junit \ - --link-at-build-time=@svm-junit.packages \ No newline at end of file + --link-at-build-time=@svm-junit.packages diff --git a/substratevm/mx.substratevm/mx_substratevm.py b/substratevm/mx.substratevm/mx_substratevm.py index 057f46c2a9a0..8e0a99915dc4 100644 --- a/substratevm/mx.substratevm/mx_substratevm.py +++ b/substratevm/mx.substratevm/mx_substratevm.py @@ -326,7 +326,7 @@ def image_demo_task(extra_image_args=None, flightrecorder=True): def truffle_unittest_task(quickbuild=False): - truffle_build_args = ['--build-args', '--macro:truffle', + truffle_build_args = ['--force-builder-on-cp', '--build-args', '--macro:truffle', '-H:MaxRuntimeCompileMethods=5000', '-H:+TruffleCheckBlackListedMethods'] if quickbuild: @@ -407,7 +407,7 @@ def svm_gate_body(args, tasks): with native_image_context(IMAGE_ASSERTION_FLAGS) as native_image: testlib = mx_subst.path_substitutions.substitute('-Dnative.test.lib=/') isolation_testlib = mx_subst.path_substitutions.substitute('-Dnative.isolation.test.lib=/') - native_unittest_args = ['com.oracle.truffle.nfi.test', '--build-args', '--language:nfi', + native_unittest_args = ['com.oracle.truffle.nfi.test', '--force-builder-on-cp', '--build-args', '--language:nfi', '-H:MaxRuntimeCompileMethods=2000', '-H:+TruffleCheckBlackListedMethods', '--run-args', testlib, isolation_testlib, '--very-verbose', '--enable-timing'] @@ -418,7 +418,7 @@ def svm_gate_body(args, tasks): with native_image_context(IMAGE_ASSERTION_FLAGS) as native_image: testlib = mx_subst.path_substitutions.substitute('-Dnative.test.lib=/') isolation_testlib = mx_subst.path_substitutions.substitute('-Dnative.isolation.test.lib=/') - native_unittest_args = ['com.oracle.truffle.nfi.test', '--build-args', '--language:nfi', + native_unittest_args = ['com.oracle.truffle.nfi.test', '--force-builder-on-cp', '--build-args', '--language:nfi', '-H:MaxRuntimeCompileMethods=2000', '-H:+TruffleCheckBlackListedMethods'] + DEVMODE_FLAGS + [ '--run-args', testlib, isolation_testlib, '--very-verbose', '--enable-timing'] @@ -484,7 +484,7 @@ def native_unittests_task(extra_build_args=None): if mx.is_windows(): mx_unittest.add_global_ignore_glob('com.oracle.svm.test.SecurityServiceTest') - native_unittest(['--builder-on-modulepath', '--build-args', _native_unittest_features] + additional_build_args) + native_unittest(['--build-args', _native_unittest_features] + additional_build_args) def conditional_config_task(native_image): @@ -564,7 +564,7 @@ def javac_image_command(javac_path): ) -def _native_junit(native_image, unittest_args, build_args=None, run_args=None, blacklist=None, whitelist=None, preserve_image=False, builder_on_modulepath=False): +def _native_junit(native_image, unittest_args, build_args=None, run_args=None, blacklist=None, whitelist=None, preserve_image=False, force_builder_on_cp=False): build_args = build_args or [] javaProperties = {} for dist in suite.dists: @@ -591,7 +591,7 @@ def dummy_harness(test_deps, vm_launcher, vm_args): with open(unittest_file, 'r') as f: mx.log('Building junit image for matching: ' + ' '.join(l.rstrip() for l in f)) extra_image_args = mx.get_runtime_jvm_args(unittest_deps, jdk=mx_compiler.jdk, exclude_names=['substratevm:LIBRARY_SUPPORT']) - macro_junit = '--macro:junit' + ('' if builder_on_modulepath else 'cp') + macro_junit = '--macro:junit' + ('cp' if force_builder_on_cp else '') unittest_image = native_image(['-ea', '-esa'] + build_args + extra_image_args + [macro_junit + '=' + unittest_file, '-H:Path=' + junit_test_dir]) image_pattern_replacement = unittest_image + ".exe" if mx.is_windows() else unittest_image run_args = [arg.replace('${unittest.image}', image_pattern_replacement) for arg in run_args] @@ -617,14 +617,14 @@ def unmask(args): def _native_unittest(native_image, cmdline_args): parser = ArgumentParser(prog='mx native-unittest', description='Run unittests as native image.') - all_args = ['--build-args', '--run-args', '--blacklist', '--whitelist', '-p', '--preserve-image', '--builder-on-modulepath'] + all_args = ['--build-args', '--run-args', '--blacklist', '--whitelist', '-p', '--preserve-image', '--force-builder-on-cp'] cmdline_args = [_mask(arg, all_args) for arg in cmdline_args] parser.add_argument(all_args[0], metavar='ARG', nargs='*', default=[]) parser.add_argument(all_args[1], metavar='ARG', nargs='*', default=[]) parser.add_argument('--blacklist', help='run all testcases not specified in ', metavar='') parser.add_argument('--whitelist', help='run testcases specified in only', metavar='') parser.add_argument('-p', '--preserve-image', help='do not delete the generated native image', action='store_true') - parser.add_argument('--builder-on-modulepath', help='perform image build with builder on module-path', action='store_true') + parser.add_argument('--force-builder-on-cp', help='force image builder to run on classpath', action='store_true') parser.add_argument('unittest_args', metavar='TEST_ARG', nargs='*') pargs = parser.parse_args(cmdline_args) @@ -645,8 +645,7 @@ def _native_unittest(native_image, cmdline_args): mx.log('warning: could not read blacklist: ' + blacklist) unittest_args = unmask(pargs.unittest_args) if unmask(pargs.unittest_args) else ['com.oracle.svm.test', 'com.oracle.svm.configure.test'] - builder_on_modulepath = pargs.builder_on_modulepath - _native_junit(native_image, unittest_args, unmask(pargs.build_args), unmask(pargs.run_args), blacklist, whitelist, pargs.preserve_image, builder_on_modulepath) + _native_junit(native_image, unittest_args, unmask(pargs.build_args), unmask(pargs.run_args), blacklist, whitelist, pargs.preserve_image, pargs.force_builder_on_cp) def jvm_unittest(args): diff --git a/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java b/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java index 844dbd7e729d..f145674c32ec 100644 --- a/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java +++ b/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java @@ -28,6 +28,7 @@ import java.util.List; import java.util.function.BooleanSupplier; +import com.oracle.svm.util.ModuleSupport; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; @@ -74,6 +75,8 @@ public List> getRequiredFeatures() { @Override public void afterRegistration(AfterRegistrationAccess access) { + /* Open up builder to allow whitebox testing */ + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, null, false, "org.graalvm.nativeimage.builder"); SVMJUnitRunner svmRunner = new SVMJUnitRunner(access); ImageSingletons.add(SVMJUnitRunner.class, svmRunner); } diff --git a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties index ff506e29885b..b0c2cd6a0644 100644 --- a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties +++ b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties @@ -7,6 +7,4 @@ Args= \ --features=com.oracle.svm.test.NativeImageResourceUtils$TestFeature \ --features=com.oracle.svm.test.jfr.JFRTestFeature \ --add-opens=java.base/java.lang=ALL-UNNAMED \ - -H:+AllowVMInspection \ - --add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED \ - --add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.containers=ALL-UNNAMED + -H:+AllowVMInspection \ No newline at end of file From 9752bf557af86f44d6ef295d98426bc2edf03d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 6 Apr 2022 10:43:41 +0200 Subject: [PATCH 09/41] Make TruffleTCKFeature#findTCKTests compatible with builder on modulepath --- .../truffle/tck/tests/TruffleTCKFeature.java | 90 ++++++------------- 1 file changed, 26 insertions(+), 64 deletions(-) diff --git a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java index 8a7fa7197c55..4ef07a3072b0 100644 --- a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java +++ b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java @@ -40,24 +40,24 @@ */ package com.oracle.truffle.tck.tests; -import java.io.File; import java.io.IOException; import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.URISyntaxException; import java.net.URL; -import java.net.URLClassLoader; -import java.util.ArrayList; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collection; import java.util.Collections; -import java.util.Enumeration; -import java.util.HashSet; import java.util.List; -import java.util.Set; +import java.util.Map; import java.util.function.Function; import java.util.function.Supplier; -import java.util.jar.JarEntry; -import java.util.jar.JarFile; +import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.RuntimeReflection; import org.junit.After; @@ -122,65 +122,27 @@ private static boolean isJUnitEntryPoint(Method method) { method.isAnnotationPresent(Parameters.class) || method.isAnnotationPresent(Test.class); } + @SuppressWarnings("try") private static Collection findTCKTests() { - Set todo = new HashSet<>(); - ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); - if (!(contextClassLoader instanceof URLClassLoader)) { - throw new IllegalStateException("Context ClassLoader must be URLClassLoader"); - } - for (URL url : ((URLClassLoader) contextClassLoader).getURLs()) { - try { - final File file = new File(url.toURI()); - todo.add(file); - } catch (URISyntaxException | IllegalArgumentException e) { - throw new IllegalStateException(String.format("Unable to handle image class path element %s.", url)); - } - } - Collection result = new ArrayList<>(); - for (File cpEntry : todo) { - if (cpEntry.isDirectory()) { - result.addAll(findTCKTestsInDirectory(cpEntry)); - } else { - result.addAll(findTCKTestsInArchive(cpEntry)); - } - } - return result; - } - - private static Collection findTCKTestsInDirectory(File root) { - String pkgFqn = TruffleTCKFeature.class.getPackage().getName(); - String folderResourceName = pkgFqn.replace('.', File.separatorChar) + File.separatorChar; - File folder = new File(root, folderResourceName); - String[] names = folder.list(); - if (names == null) { - return Collections.emptySet(); - } - Collection result = new ArrayList<>(); - for (String name : names) { - if (name.endsWith("Test.class")) { - String className = pkgFqn + '.' + name.substring(0, name.length() - 6); - result.add(className); - } - } - return result; - } - - private static Collection findTCKTestsInArchive(File archive) { - String folderResourceName = TruffleTCKFeature.class.getPackage().getName().replace('.', '/') + '/'; - Collection result = new ArrayList<>(); - try (JarFile jf = new JarFile(archive)) { - Enumeration entries = jf.entries(); - while (entries.hasMoreElements()) { - JarEntry entry = entries.nextElement(); - String entryName = entry.getName(); - if (entryName.startsWith(folderResourceName) && entryName.endsWith("Test.class")) { - String className = entryName.substring(0, entryName.length() - 6).replace('/', '.'); - result.add(className); + try { + URL resource = TruffleTCKFeature.class.getResource("TruffleTCKFeature.class"); + try (FileSystem fileSystem = FileSystems.newFileSystem(resource.toURI(), Map.of())) { + Path path = Path.of(resource.toURI()); + try (Stream siblingResources = Files.list(path.getParent())) { + String packageName = TruffleTCKFeature.class.getPackageName(); + String suffix = ".class"; + return siblingResources + .map(Path::getFileName) + .map(Path::toString) + .filter(name -> name.endsWith("Test" + suffix)) + .map(name -> packageName + '.' + name.substring(0, name.length() - suffix.length())) + .collect(Collectors.toList()); } } - } catch (IOException ioe) { - // Ignore non existent image classpath entry + } catch (URISyntaxException e) { + throw new IllegalStateException("Cannot access TruffleTCKFeature class as resource"); + } catch (IOException e) { + throw new IllegalStateException("Cannot list TruffleTCKFeature class sibling resources"); } - return result; } } From b5338df64d5470b46eeabbfcbfdccf781a47efa4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 6 Apr 2022 10:57:26 +0200 Subject: [PATCH 10/41] Remove obsolete property from --macro:truffle --- truffle/mx.truffle/macro-truffle.properties | 1 - 1 file changed, 1 deletion(-) diff --git a/truffle/mx.truffle/macro-truffle.properties b/truffle/mx.truffle/macro-truffle.properties index 9efb7d4c2201..60fdfef025ce 100644 --- a/truffle/mx.truffle/macro-truffle.properties +++ b/truffle/mx.truffle/macro-truffle.properties @@ -1,5 +1,4 @@ # This file contains support for building truffle images -ImageBuilderBootClasspath8 = ${.}/../../../truffle/truffle-api.jar Args = -H:Features=com.oracle.svm.truffle.TruffleFeature,com.oracle.svm.truffle.TruffleBaseFeature,org.graalvm.home.HomeFinderFeature \ -H:MaxRuntimeCompileMethods=2200 \ --initialize-at-build-time=com.oracle.truffle \ From 5f005d1e3b0aa3b945fb7a27e0282e57d006568a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 6 Apr 2022 15:04:15 +0200 Subject: [PATCH 11/41] Ensure truffle image builds use builder on classpath (for now) --- .../oracle/svm/driver/MacroOptionHandler.java | 14 ++++++++++++++ .../src/com/oracle/svm/driver/NativeImage.java | 16 ++++++++++++---- truffle/mx.truffle/macro-truffle.properties | 1 + 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/MacroOptionHandler.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/MacroOptionHandler.java index e8bb28ebb944..5965ddcd1b01 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/MacroOptionHandler.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/MacroOptionHandler.java @@ -80,6 +80,20 @@ private void applyEnabled(MacroOption.EnabledOption enabledOption, String argume } BuildConfiguration config = nativeImage.config; + + String propertyName = "BuilderOnClasspath"; + String propertyValue = enabledOption.getProperty(config, propertyName); + if (propertyValue != null) { + boolean modulePathBuild = !Boolean.valueOf(propertyValue); + String imageBuilderModeEnforcer = enabledOption.getOption().toString(); + if (config.imageBuilderModeEnforcer != null && modulePathBuild != config.modulePathBuild) { + NativeImage.showError(String.format("Conflicting %s property values. %s (%b) vs %s (%b)", propertyName, + imageBuilderModeEnforcer, modulePathBuild, config.imageBuilderModeEnforcer, config.modulePathBuild)); + } + config.imageBuilderModeEnforcer = imageBuilderModeEnforcer; + config.modulePathBuild = modulePathBuild; + } + enabledOption.forEachPropertyValue(config, "ImageBuilderClasspath", entry -> nativeImage.addImageBuilderClasspath(ClasspathUtils.stringToClasspath(entry)), PATH_SEPARATOR_REGEX); boolean explicitImageModulePath = enabledOption.forEachPropertyValue( diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index c6142968aa2f..a0ef01d32c4c 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -261,6 +261,7 @@ private static String oR(OptionKey option) { protected static class BuildConfiguration { boolean modulePathBuild; + String imageBuilderModeEnforcer; protected final Path workDir; protected final Path rootDir; @@ -268,6 +269,7 @@ protected static class BuildConfiguration { BuildConfiguration(BuildConfiguration original) { modulePathBuild = original.modulePathBuild; + imageBuilderModeEnforcer = original.imageBuilderModeEnforcer; workDir = original.workDir; rootDir = original.rootDir; args = new ArrayList<>(original.args); @@ -280,6 +282,7 @@ protected BuildConfiguration(List args) { @SuppressWarnings("deprecation") BuildConfiguration(Path rootDir, Path workDir, List args) { modulePathBuild = ModuleSupport.isModulePathBuild(); + imageBuilderModeEnforcer = null; this.args = args; this.workDir = workDir != null ? workDir : Paths.get(".").toAbsolutePath().normalize(); if (rootDir != null) { @@ -1323,9 +1326,7 @@ protected int buildImage(List javaArgs, LinkedHashSet cp, LinkedHa try { ProcessBuilder pb = new ProcessBuilder(); pb.command(command); - if (config.modulePathBuild) { - pb.environment().put(ModuleSupport.ENV_VAR_USE_MODULE_SYSTEM, Boolean.toString(true)); - } + pb.environment().put(ModuleSupport.ENV_VAR_USE_MODULE_SYSTEM, Boolean.toString(config.modulePathBuild)); p = pb.inheritIO().start(); exitStatus = p.waitFor(); } catch (IOException | InterruptedException e) { @@ -1525,7 +1526,7 @@ void addImageModulePath(Path modulePathEntry, boolean strict) { return; } - config.modulePathBuild = true; + enableModulePathBuild(); imageModulePath.add(mpEntry); processClasspathNativeImageMetaInf(mpEntry); } @@ -1598,6 +1599,13 @@ void setJarOptionMode(boolean val) { void setModuleOptionMode(boolean val) { moduleOptionMode = val; + enableModulePathBuild(); + } + + private void enableModulePathBuild() { + if (config.modulePathBuild == false) { + NativeImage.showError("Native image module options not allowed in this image build. Reason: " + config.imageBuilderModeEnforcer); + } config.modulePathBuild = true; } diff --git a/truffle/mx.truffle/macro-truffle.properties b/truffle/mx.truffle/macro-truffle.properties index 60fdfef025ce..af2d5a92d598 100644 --- a/truffle/mx.truffle/macro-truffle.properties +++ b/truffle/mx.truffle/macro-truffle.properties @@ -1,4 +1,5 @@ # This file contains support for building truffle images +BuilderOnClasspath = true Args = -H:Features=com.oracle.svm.truffle.TruffleFeature,com.oracle.svm.truffle.TruffleBaseFeature,org.graalvm.home.HomeFinderFeature \ -H:MaxRuntimeCompileMethods=2200 \ --initialize-at-build-time=com.oracle.truffle \ From e0cbc766abf1cb4b97ca7d90d56c93c4aa366f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 6 Apr 2022 15:35:27 +0200 Subject: [PATCH 12/41] Ensure all use of -p is reported as error for truffle image building --- .../src/com/oracle/svm/driver/NativeImage.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index a0ef01d32c4c..2447aac671d2 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -1505,6 +1505,8 @@ void addImageModulePath(Path modulePathEntry) { } void addImageModulePath(Path modulePathEntry, boolean strict) { + enableModulePathBuild(); + Path mpEntry; try { mpEntry = canonicalize(modulePathEntry); @@ -1526,7 +1528,6 @@ void addImageModulePath(Path modulePathEntry, boolean strict) { return; } - enableModulePathBuild(); imageModulePath.add(mpEntry); processClasspathNativeImageMetaInf(mpEntry); } @@ -1598,13 +1599,13 @@ void setJarOptionMode(boolean val) { } void setModuleOptionMode(boolean val) { - moduleOptionMode = val; enableModulePathBuild(); + moduleOptionMode = val; } private void enableModulePathBuild() { if (config.modulePathBuild == false) { - NativeImage.showError("Native image module options not allowed in this image build. Reason: " + config.imageBuilderModeEnforcer); + NativeImage.showError("Module options not allowed in this image build. Reason: " + config.imageBuilderModeEnforcer); } config.modulePathBuild = true; } From aa8d9829447277a61761cf2eb59dad3a59a6ba0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 6 Apr 2022 16:15:07 +0200 Subject: [PATCH 13/41] Style fix --- .../src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java index 4ef07a3072b0..8acecac0e3f9 100644 --- a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java +++ b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java @@ -131,8 +131,7 @@ private static Collection findTCKTests() { try (Stream siblingResources = Files.list(path.getParent())) { String packageName = TruffleTCKFeature.class.getPackageName(); String suffix = ".class"; - return siblingResources - .map(Path::getFileName) + return siblingResources.map(Path::getFileName) .map(Path::toString) .filter(name -> name.endsWith("Test" + suffix)) .map(name -> packageName + '.' + name.substring(0, name.length() - suffix.length())) From a2ee12a8a2c0667ca4cd69fe538d50262be364d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 2 May 2022 13:07:47 +0200 Subject: [PATCH 14/41] Cleanup JfrEventFeature module access --- .../src/com/oracle/svm/core/jfr/JfrFeature.java | 2 -- .../src/com/oracle/svm/hosted/jfr/JfrEventFeature.java | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java index 94fd523f0279..33ba62456492 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jfr/JfrFeature.java @@ -149,8 +149,6 @@ public List> getRequiredFeatures() { public void afterRegistration(AfterRegistrationAccess access) { ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "jdk.jfr"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, null, false, "java.base"); - //ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrFeature.class, false, "jdk.jfr", "jdk.jfr.events"); - //ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrEventSubstitution.class, false, "jdk.internal.vm.ci", "jdk.vm.ci.hotspot"); // Initialize some parts of JFR/JFC at image build time. List knownConfigurations = JFC.getConfigurations(); diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventFeature.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventFeature.java index 24b267d75cc1..100c5e954175 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventFeature.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/jfr/JfrEventFeature.java @@ -30,7 +30,6 @@ import java.util.Collections; import java.util.List; -import com.oracle.svm.core.jfr.JfrFeature; import org.graalvm.nativeimage.ImageSingletons; import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; @@ -39,6 +38,7 @@ import com.oracle.svm.core.annotate.AutomaticFeature; import com.oracle.svm.core.hub.DynamicHub; import com.oracle.svm.core.hub.DynamicHubSupport; +import com.oracle.svm.core.jfr.JfrFeature; import com.oracle.svm.core.jfr.traceid.JfrTraceId; import com.oracle.svm.core.jfr.traceid.JfrTraceIdMap; import com.oracle.svm.core.meta.SharedType; @@ -68,8 +68,8 @@ public List> getRequiredFeatures() { @Override public void afterRegistration(AfterRegistrationAccess access) { - ModuleSupport.exportAndOpenPackageToClass("jdk.jfr", "jdk.jfr.events", false, JfrEventFeature.class); - ModuleSupport.exportAndOpenPackageToClass("jdk.internal.vm.ci", "jdk.vm.ci.hotspot", false, JfrEventSubstitution.class); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrFeature.class, false, "jdk.jfr", "jdk.jfr.events"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, JfrEventSubstitution.class, false, "jdk.internal.vm.ci", "jdk.vm.ci.hotspot"); } @Override From 5d1ad1a29588f318c2bec005cc04133a17a4cf83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 3 May 2022 14:48:35 +0200 Subject: [PATCH 15/41] Allow ModuleSupport.isModulePathBuild() to be used in image --- .../src/com/oracle/svm/util/ModuleSupport.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index 141c9c199266..eec6f92147df 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -34,7 +34,6 @@ import jdk.internal.module.Modules; -@Platforms(Platform.HOSTED_ONLY.class) public final class ModuleSupport { public static final String ENV_VAR_USE_MODULE_SYSTEM = "USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM"; @@ -48,6 +47,7 @@ public static boolean isModulePathBuild() { return !"false".equalsIgnoreCase(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); } + @Platforms(Platform.HOSTED_ONLY.class) public enum Access { OPEN { @Override @@ -85,6 +85,7 @@ void giveAccess(Module accessingModule, Module declaringModule, String packageNa abstract void giveAccess(Module accessingModule, Module declaringModule, String packageName); } + @Platforms(Platform.HOSTED_ONLY.class) public static void accessModuleByClass(Access access, Class accessingClass, Class declaringClass) { accessModuleByClass(access, accessingClass, declaringClass.getModule(), declaringClass.getPackageName()); } @@ -95,6 +96,7 @@ public static void accessModuleByClass(Access access, Class accessingClass, C * exported to ALL-UNNAMED. If no packages are given, all packages of the module are opened or * exported. */ + @Platforms(Platform.HOSTED_ONLY.class) public static void accessPackagesToClass(Access access, Class accessingClass, boolean optional, String moduleName, String... packageNames) { Module declaringModule = getModule(moduleName, optional); if (declaringModule == null) { @@ -107,6 +109,7 @@ public static void accessPackagesToClass(Access access, Class accessingClass, } } + @Platforms(Platform.HOSTED_ONLY.class) private static Module getModule(String moduleName, boolean optional) { Objects.requireNonNull(moduleName); Optional declaringModuleOpt = ModuleLayer.boot().findModule(moduleName); @@ -119,6 +122,7 @@ private static Module getModule(String moduleName, boolean optional) { return declaringModuleOpt.get(); } + @Platforms(Platform.HOSTED_ONLY.class) private static void accessModuleByClass(Access access, Class accessingClass, Module declaringModule, String packageName) { Module namedAccessingModule = null; if (accessingClass != null) { From e28b8e9bbdc64efffd2ef6cda9d2958d8b6d9441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 3 May 2022 14:49:48 +0200 Subject: [PATCH 16/41] Make org.graalvm.nativeimage.base exports qualified --- substratevm/mx.substratevm/suite.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index 33433bbef1cc..ed1ca322ab49 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -1460,8 +1460,8 @@ "moduleInfo" : { "name" : "org.graalvm.nativeimage.base", "exports" : [ - "com.oracle.svm.util", - "com.oracle.svm.common.option", + "com.oracle.svm.util to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,org.graalvm.nativeimage.junitsupport,com.oracle.svm.svm_enterprise", + "com.oracle.svm.common.option to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver", ], } }, From b7f9c07e8242bf5169ea1b063216ccc6d7934513 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 3 May 2022 14:51:57 +0200 Subject: [PATCH 17/41] Opening up org.graalvm.nativeimage.builder packages in JUnitFeature.afterRegistration has to be optional so that it still works with builder on classpath. --- .../src/com/oracle/svm/junit/JUnitFeature.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java b/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java index f145674c32ec..b4406081a8a4 100644 --- a/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java +++ b/substratevm/src/com.oracle.svm.junit/src/com/oracle/svm/junit/JUnitFeature.java @@ -76,7 +76,7 @@ public List> getRequiredFeatures() { @Override public void afterRegistration(AfterRegistrationAccess access) { /* Open up builder to allow whitebox testing */ - ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, null, false, "org.graalvm.nativeimage.builder"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, null, true, "org.graalvm.nativeimage.builder"); SVMJUnitRunner svmRunner = new SVMJUnitRunner(access); ImageSingletons.add(SVMJUnitRunner.class, svmRunner); } From 5a6a738125d68337589cb92df89b18c42519050e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 3 May 2022 14:53:08 +0200 Subject: [PATCH 18/41] Allow com.oracle.svm.test code to use com.oracle.svm.util --- .../native-image/com.oracle.svm.test/native-image.properties | 1 + 1 file changed, 1 insertion(+) diff --git a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties index b0c2cd6a0644..a9cf43fcce2f 100644 --- a/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties +++ b/substratevm/src/com.oracle.svm.test/src/META-INF/native-image/com.oracle.svm.test/native-image.properties @@ -7,4 +7,5 @@ Args= \ --features=com.oracle.svm.test.NativeImageResourceUtils$TestFeature \ --features=com.oracle.svm.test.jfr.JFRTestFeature \ --add-opens=java.base/java.lang=ALL-UNNAMED \ + --add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED \ -H:+AllowVMInspection \ No newline at end of file From 3fba91ca12e84c88a53487a07a28994f0d58ba72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 4 May 2022 10:48:55 +0200 Subject: [PATCH 19/41] Add missing access of org.graalvm.compiler.nodes.graphbuilderconf from com.oracle.svm.polyglot.scala.ScalaAnalysisPlugin Class com.oracle.svm.polyglot.scala.ScalaAnalysisPlugin is part of module org.graalvm.nativeimage.librarysupport --- compiler/mx.compiler/suite.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/mx.compiler/suite.py b/compiler/mx.compiler/suite.py index 1db1f092dc98..33c1fa4ec4fa 100644 --- a/compiler/mx.compiler/suite.py +++ b/compiler/mx.compiler/suite.py @@ -2043,7 +2043,7 @@ "org.graalvm.compiler.core.common to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.objectfile", "org.graalvm.compiler.debug to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.objectfile", "org.graalvm.compiler.hotspot to jdk.internal.vm.compiler.management", - "org.graalvm.compiler.nodes.graphbuilderconf to org.graalvm.nativeimage.driver", + "org.graalvm.compiler.nodes.graphbuilderconf to org.graalvm.nativeimage.driver,org.graalvm.nativeimage.librarysupport", "org.graalvm.compiler.options to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.junitsupport", "org.graalvm.compiler.phases.common to org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.configure", "org.graalvm.compiler.serviceprovider to jdk.internal.vm.compiler.management,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.diagnostics", From 910283bfd1ef8b40cbbffa869e236e5352758720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 4 May 2022 10:53:23 +0200 Subject: [PATCH 20/41] Fix building of LibGraal with builder on module-path --- substratevm/mx.substratevm/mx_substratevm.py | 16 ++++++++++++++++ .../graal/hotspot/libgraal/LibGraalFeature.java | 12 ++++++++++++ 2 files changed, 28 insertions(+) diff --git a/substratevm/mx.substratevm/mx_substratevm.py b/substratevm/mx.substratevm/mx_substratevm.py index 8e0a99915dc4..277a37abc5d9 100644 --- a/substratevm/mx.substratevm/mx_substratevm.py +++ b/substratevm/mx.substratevm/mx_substratevm.py @@ -1089,6 +1089,22 @@ def _native_image_launcher_extra_jvm_args(): 'compiler:GRAAL_MANAGEMENT_LIBGRAAL'] libgraal_build_args = [ + ## Pass via JVM args opening up of packages needed for image builder early on + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.compiler.hotspot=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.compiler.options=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.compiler.truffle.common.hotspot=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.compiler.truffle.common=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.jniutils=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.libgraal.jni.annotation=ALL-UNNAMED', + '-J--add-exports=jdk.internal.vm.compiler/org.graalvm.libgraal.jni=ALL-UNNAMED', + '-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.annotate=ALL-UNNAMED', + '-J--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.option=ALL-UNNAMED', + ## Packages used after option-processing can be opened by the builder (`-J`-prefix not needed) + # LibGraalFeature implements com.oracle.svm.core.graal.GraalFeature (needed to be able to instantiate LibGraalFeature) + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.graal=ALL-UNNAMED', + # Make ModuleSupport accessible to do the remaining opening-up in LibGraalFeature constructor + '--add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED', + '--initialize-at-build-time=org.graalvm.compiler,org.graalvm.libgraal,com.oracle.truffle', '-H:-UseServiceLoaderFeature', '-H:+AllowFoldMethods', diff --git a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java index 060cd9626d89..9c27e320fdb4 100644 --- a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java +++ b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java @@ -139,6 +139,7 @@ import com.oracle.svm.hosted.ImageClassLoader; import com.oracle.svm.jni.hosted.JNIFeature; import com.oracle.svm.reflect.hosted.ReflectionFeature; +import com.oracle.svm.util.ModuleSupport; import com.oracle.svm.util.ReflectionUtil; import jdk.vm.ci.code.CompilationRequest; @@ -170,6 +171,17 @@ public class LibGraalFeature implements com.oracle.svm.core.graal.InternalFeatur private HotSpotReplacementsImpl hotSpotSubstrateReplacements; + public LibGraalFeature() { + /* Open up all modules needed to build LibGraal image */ + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.ci"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.compiler"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.compiler.management"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.base"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.builder"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.llvm"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "java.base", "jdk.internal.misc"); + } + @Override public void afterImageWrite(AfterImageWriteAccess access) { } From e9632f85fb8b872216142bb81a521a8524fa0ba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 4 May 2022 11:59:52 +0200 Subject: [PATCH 21/41] Make gu buildable with builder on module-path --- .../org.graalvm.launcher/native-image.properties | 5 ++++- .../com/oracle/svm/hosted/NativeImageClassLoaderSupport.java | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/sdk/src/org.graalvm.launcher/src/META-INF/native-image/org.graalvm.launcher/native-image.properties b/sdk/src/org.graalvm.launcher/src/META-INF/native-image/org.graalvm.launcher/native-image.properties index 3531dec17ad5..680ed1b5e70c 100644 --- a/sdk/src/org.graalvm.launcher/src/META-INF/native-image/org.graalvm.launcher/native-image.properties +++ b/sdk/src/org.graalvm.launcher/src/META-INF/native-image/org.graalvm.launcher/native-image.properties @@ -1,4 +1,7 @@ -Args = --features=com.oracle.svm.thirdparty.jline.JLine3Feature \ +Args = --add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED \ + --add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED \ + --add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jni=ALL-UNNAMED \ + --features=com.oracle.svm.thirdparty.jline.JLine3Feature \ --initialize-at-build-time=org.graalvm.launcher \ -H:JNIConfigurationResources=${.}/launcher.jniconfig \ --add-modules=java.scripting \ diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index 0d65e2047b9e..ddc11d0a2e66 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -422,7 +422,7 @@ private Stream processOption(OptionKey Date: Wed, 4 May 2022 12:31:29 +0200 Subject: [PATCH 22/41] Style fix --- .../com/oracle/truffle/tck/tests/TruffleTCKFeature.java | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java index 8acecac0e3f9..dab7eef657d8 100644 --- a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java +++ b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java @@ -131,11 +131,8 @@ private static Collection findTCKTests() { try (Stream siblingResources = Files.list(path.getParent())) { String packageName = TruffleTCKFeature.class.getPackageName(); String suffix = ".class"; - return siblingResources.map(Path::getFileName) - .map(Path::toString) - .filter(name -> name.endsWith("Test" + suffix)) - .map(name -> packageName + '.' + name.substring(0, name.length() - suffix.length())) - .collect(Collectors.toList()); + return siblingResources.map(Path::getFileName).map(Path::toString).filter(name -> name.endsWith("Test" + suffix)).map( + name -> packageName + '.' + name.substring(0, name.length() - suffix.length())).collect(Collectors.toList()); } } } catch (URISyntaxException e) { From e91f1eda30d35d3719a13da7f01c416bd825698f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 4 May 2022 14:55:10 +0200 Subject: [PATCH 23/41] Fix clinittest for Windows --- .../test/clinit/TestClassInitializationMustBeSafeEarly.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java index 4485b2d597b7..60042e76a9ea 100644 --- a/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java +++ b/substratevm/src/com.oracle.svm.test/src/com/oracle/svm/test/clinit/TestClassInitializationMustBeSafeEarly.java @@ -36,6 +36,8 @@ import org.graalvm.nativeimage.hosted.Feature; import org.graalvm.nativeimage.hosted.RuntimeClassInitialization; +import com.oracle.svm.util.ModuleSupport; + import jdk.internal.misc.Unsafe; class PureMustBeSafeEarly { @@ -520,6 +522,9 @@ private static boolean shouldBeInitialized(Class c) { @Override public void afterRegistration(AfterRegistrationAccess access) { RuntimeClassInitialization.initializeAtRunTime("com.oracle.svm.test.clinit"); + + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.OPEN, TestClassInitializationMustBeSafeEarly.class, false, + "java.base", "jdk.internal.misc"); RuntimeClassInitialization.initializeAtBuildTime(UnsafeAccess.class); } From 38488e6f5467b04b4615a1842817d8b99e3c30a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 4 May 2022 15:59:14 +0200 Subject: [PATCH 24/41] Run `Run Truffle API tests on SVM` with builder-on-cp --- vm/mx.vm/mx_vm_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/mx.vm/mx_vm_gate.py b/vm/mx.vm/mx_vm_gate.py index cfc942e9d971..e0f9330c495b 100644 --- a/vm/mx.vm/mx_vm_gate.py +++ b/vm/mx.vm/mx_vm_gate.py @@ -340,7 +340,7 @@ def gate_substratevm(tasks, quickbuild=False): '--enable-url-protocols=jar', '--enable-url-protocols=http' ] - args = ['--build-args'] + truffle_with_compilation + extra_build_args + blacklist_args + ['--'] + tests + args = ['--force-builder-on-cp', '--build-args'] + truffle_with_compilation + extra_build_args + blacklist_args + ['--'] + tests native_image_context, svm = graalvm_svm() with native_image_context(svm.IMAGE_ASSERTION_FLAGS) as native_image: svm._native_unittest(native_image, args) From 697ad4f69a3f34219271eb0bd2408db8b488fa81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Thu, 5 May 2022 12:51:40 +0200 Subject: [PATCH 25/41] Fix -H:CompilerBackend=llvm with builder on module-path --- .../src/com/oracle/svm/core/graal/llvm/LLVMFeature.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java b/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java index 4eac4bdc8cb0..398d77600d6e 100644 --- a/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java +++ b/substratevm/src/com.oracle.svm.core.graal.llvm/src/com/oracle/svm/core/graal/llvm/LLVMFeature.java @@ -49,7 +49,6 @@ import com.oracle.svm.core.graal.llvm.lowering.LLVMLoadExceptionObjectLowering; import com.oracle.svm.core.graal.llvm.lowering.SubstrateLLVMLoweringProvider; import com.oracle.svm.core.graal.llvm.replacements.LLVMGraphBuilderPlugins; -import com.oracle.svm.core.graal.llvm.replacements.LLVMIntrinsicNode; import com.oracle.svm.core.graal.llvm.runtime.LLVMExceptionUnwind; import com.oracle.svm.core.graal.llvm.util.LLVMOptions; import com.oracle.svm.core.graal.llvm.util.LLVMToolchain; @@ -90,7 +89,8 @@ public boolean isInConfiguration(IsInConfigurationAccess access) { @Override public void afterRegistration(AfterRegistrationAccess access) { if (ModuleSupport.modulePathBuild) { - ModuleSupport.accessModuleByClass(ModuleSupport.Access.OPEN, NodeClass.class, LLVMIntrinsicNode.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.EXPORT, NodeClass.class, LLVMGraphBuilderPlugins.class); + ModuleSupport.accessModuleByClass(ModuleSupport.Access.EXPORT, NodeClass.class, SubstrateLLVMLoweringProvider.class); } ImageSingletons.add(SubstrateBackendFactory.class, new SubstrateBackendFactory() { From f6e55af4927383754ef6c5b55b722fa83ed347af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Thu, 5 May 2022 15:31:41 +0200 Subject: [PATCH 26/41] Fix TruffleTCKFeature.findTCKTests() for MX_BUILD_EXPLODED=true --- .../src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java index dab7eef657d8..72843289aa15 100644 --- a/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java +++ b/truffle/src/com.oracle.truffle.tck.tests/src/com/oracle/truffle/tck/tests/TruffleTCKFeature.java @@ -126,7 +126,7 @@ private static boolean isJUnitEntryPoint(Method method) { private static Collection findTCKTests() { try { URL resource = TruffleTCKFeature.class.getResource("TruffleTCKFeature.class"); - try (FileSystem fileSystem = FileSystems.newFileSystem(resource.toURI(), Map.of())) { + try (FileSystem fileSystem = "jar".equals(resource.getProtocol()) ? FileSystems.newFileSystem(resource.toURI(), Map.of()) : null) { Path path = Path.of(resource.toURI()); try (Stream siblingResources = Files.list(path.getParent())) { String packageName = TruffleTCKFeature.class.getPackageName(); From c9ddf55c15f532dced619c7a095c37a05d6303cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Thu, 5 May 2022 16:07:56 +0200 Subject: [PATCH 27/41] Ensure LibGraalFeature is allowed to instantiate OptionDescriptor subclasses --- .../com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java | 1 + 1 file changed, 1 insertion(+) diff --git a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java index 9c27e320fdb4..2814ca63293b 100644 --- a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java +++ b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java @@ -221,6 +221,7 @@ public void duringSetup(DuringSetupAccess access) { for (Class optionsClass : imageClassLoader.findSubclasses(OptionDescriptors.class, false)) { if (!Modifier.isAbstract(optionsClass.getModifiers()) && !OptionDescriptorsMap.class.isAssignableFrom(optionsClass)) { try { + ModuleSupport.accessModuleByClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, optionsClass); for (OptionDescriptor d : optionsClass.getDeclaredConstructor().newInstance()) { if (!(d.getOptionKey() instanceof HostedOptionKey)) { descriptors.put(d.getName(), d); From fd98b9aaa3edcd14bf83dc26679ec7429304804b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 9 May 2022 13:55:35 +0200 Subject: [PATCH 28/41] Run run LLVMUnittests with builder-on-cp --- sulong/mx.sulong/mx_sulong_gate.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sulong/mx.sulong/mx_sulong_gate.py b/sulong/mx.sulong/mx_sulong_gate.py index 7c8c0c557ac3..44c368f472e2 100644 --- a/sulong/mx.sulong/mx_sulong_gate.py +++ b/sulong/mx.sulong/mx_sulong_gate.py @@ -278,7 +278,7 @@ def runLLVMUnittests(unittest_runner): run_args = [libpath, libs] + java_run_props build_args = ['--language:llvm'] + java_run_props - unittest_runner(['com.oracle.truffle.llvm.tests.interop', '--run-args'] + run_args + + unittest_runner(['com.oracle.truffle.llvm.tests.interop', '--force-builder-on-cp', '--run-args'] + run_args + ['--build-args', '--initialize-at-build-time'] + build_args) From b3704e57e4fbb9d19f0f1c1aa8b7b41851e455bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 9 May 2022 14:45:25 +0200 Subject: [PATCH 29/41] Exporting org.graalvm.nativeimage.llvm to LibGraalFeature needs to be optional because the llvm component might be missing --- .../oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java index 2814ca63293b..56ed33300cf6 100644 --- a/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java +++ b/substratevm/src/com.oracle.svm.graal.hotspot.libgraal/src/com/oracle/svm/graal/hotspot/libgraal/LibGraalFeature.java @@ -173,13 +173,13 @@ public class LibGraalFeature implements com.oracle.svm.core.graal.InternalFeatur public LibGraalFeature() { /* Open up all modules needed to build LibGraal image */ + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "java.base", "jdk.internal.misc"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.ci"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.compiler"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "jdk.internal.vm.compiler.management"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.base"); ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.builder"); - ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "org.graalvm.nativeimage.llvm"); - ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, false, "java.base", "jdk.internal.misc"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, LibGraalFeature.class, true, "org.graalvm.nativeimage.llvm"); } @Override From bbfd140062fc8341c3c3962e6c02db42c893f940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 11 May 2022 18:10:32 +0200 Subject: [PATCH 30/41] Fix implementation of ClassLoaderSupport.isNativeImageClassLoader --- .../svm/hosted/ClassLoaderSupportImpl.java | 18 +++++++++++++++++- .../hosted/NativeImageClassLoaderSupport.java | 2 +- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java index 4cb74191d76d..4747aaa16513 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java @@ -76,7 +76,23 @@ class ClassLoaderSupportImpl extends ClassLoaderSupport { @Override protected boolean isNativeImageClassLoaderImpl(ClassLoader loader) { - return loader == imageClassLoader || loader instanceof NativeImageSystemClassLoader; + if (loader == imageClassLoader) { + /* Trivial case */ + return true; + } + if (loader == classLoaderSupport.classPathClassLoader) { + /* + * If imageClassLoader is a module classloader (building image with non-empty + * NativeImageClassLoaderSupport.imagemp), the passed loader could also be our + * classLoaderSupport.classPathClassLoader (building image with non-empty + * NativeImageClassLoaderSupport.imagecp). + */ + return true; + } + if (loader instanceof NativeImageSystemClassLoader) { + return true; + } + return false; } @Override diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index ddc11d0a2e66..c4c93e558529 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -95,7 +95,7 @@ public class NativeImageClassLoaderSupport { private final EconomicMap> packages; private final EconomicSet emptySet; - private final URLClassLoader classPathClassLoader; + final URLClassLoader classPathClassLoader; private final ClassLoader modulePathClassLoader; public final ModuleFinder upgradeAndSystemModuleFinder; From a791ac592282f28ccda4bb6299eca85bfb77a4eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 16 May 2022 12:43:24 +0200 Subject: [PATCH 31/41] Fix micronaut benchmarking with builder on module-path --- java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py index de5d50e267d0..328782547203 100644 --- a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py +++ b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py @@ -460,6 +460,11 @@ def build_assertions(self, benchmark, is_gate): # This method overrides NativeImageMixin.build_assertions return [] # We are skipping build assertions due to some failed asserts while building Micronaut apps. + def extra_image_build_argument(self, benchmark, args): + return [ + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED', + ] + super(BaseMicronautBenchmarkSuite, self).extra_image_build_argument(benchmark, args) + def default_stages(self): return ['instrument-image', 'instrument-run', 'image', 'run'] From 0dc299c58a5a2665e7bc7467032a9f43b7ccfa7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 16 May 2022 13:43:40 +0200 Subject: [PATCH 32/41] Fix quarkus benchmarking with builder on module-path --- java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py index 328782547203..4c82d5934cd1 100644 --- a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py +++ b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py @@ -371,6 +371,8 @@ def extra_image_build_argument(self, benchmark, args): '-H:EnableURLProtocols=http', '-H:NativeLinkerOption=-no-pie', '-H:-UseServiceLoaderFeature', + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED', + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.configure=ALL-UNNAMED', '-H:+StackTrace'] + super(BaseQuarkusBenchmarkSuite, self).extra_image_build_argument(benchmark, args) From 59a2a96e4f73fe1c0a7275ad15264569747cc34b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Mon, 16 May 2022 14:24:04 +0200 Subject: [PATCH 33/41] Make APIOptionHandler builder-on-module-path safe --- .../src/com/oracle/svm/driver/APIOptionHandler.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/APIOptionHandler.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/APIOptionHandler.java index ab8ec348dd9b..ef90f08000fa 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/APIOptionHandler.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/APIOptionHandler.java @@ -58,6 +58,7 @@ import com.oracle.svm.driver.NativeImage.ArgumentQueue; import com.oracle.svm.hosted.FeatureImpl; import com.oracle.svm.hosted.option.HostedOptionParser; +import com.oracle.svm.util.ModuleSupport; import com.oracle.svm.util.ReflectionUtil; import com.oracle.svm.util.ReflectionUtil.ReflectionUtilError; @@ -474,6 +475,12 @@ final class APIOptionSupport { @AutomaticFeature final class APIOptionFeature implements Feature { + @Override + public void afterRegistration(AfterRegistrationAccess access) { + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, APIOptionFeature.class, true, + "jdk.internal.vm.compiler", "org.graalvm.compiler.options"); + } + @Override public void duringSetup(DuringSetupAccess access) { FeatureImpl.DuringSetupAccessImpl accessImpl = (FeatureImpl.DuringSetupAccessImpl) access; From 81d0c99b257f524d3d7187db05fec27134e3178a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 18 May 2022 17:10:35 +0200 Subject: [PATCH 34/41] Fix quarkus tika-wrk benchmarking with builder on module-path --- java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py index 4c82d5934cd1..2bd64a880591 100644 --- a/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py +++ b/java-benchmarks/mx.java-benchmarks/mx_java_benchmarks.py @@ -371,8 +371,11 @@ def extra_image_build_argument(self, benchmark, args): '-H:EnableURLProtocols=http', '-H:NativeLinkerOption=-no-pie', '-H:-UseServiceLoaderFeature', - '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED', + '--add-exports=org.graalvm.nativeimage.base/com.oracle.svm.util=ALL-UNNAMED', '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.configure=ALL-UNNAMED', + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk.localization=ALL-UNNAMED', + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.jdk=ALL-UNNAMED', + '--add-exports=org.graalvm.nativeimage.builder/com.oracle.svm.core.threadlocal=ALL-UNNAMED', '-H:+StackTrace'] + super(BaseQuarkusBenchmarkSuite, self).extra_image_build_argument(benchmark, args) From dd65f0ccfaec827d79cc7d1e57f803762188184e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 18 May 2022 18:24:22 +0200 Subject: [PATCH 35/41] Fix ScalaAnalysisPlugin with builder on module-path --- substratevm/mx.substratevm/suite.py | 2 +- .../src/com/oracle/svm/polyglot/scala/ScalaFeature.java | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/substratevm/mx.substratevm/suite.py b/substratevm/mx.substratevm/suite.py index ed1ca322ab49..6169fd94d9cc 100644 --- a/substratevm/mx.substratevm/suite.py +++ b/substratevm/mx.substratevm/suite.py @@ -1460,7 +1460,7 @@ "moduleInfo" : { "name" : "org.graalvm.nativeimage.base", "exports" : [ - "com.oracle.svm.util to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,org.graalvm.nativeimage.junitsupport,com.oracle.svm.svm_enterprise", + "com.oracle.svm.util to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.librarysupport,org.graalvm.nativeimage.driver,org.graalvm.nativeimage.llvm,org.graalvm.nativeimage.agent.jvmtibase,org.graalvm.nativeimage.agent.tracing,org.graalvm.nativeimage.agent.diagnostics,org.graalvm.nativeimage.junitsupport,com.oracle.svm.svm_enterprise", "com.oracle.svm.common.option to org.graalvm.nativeimage.pointsto,org.graalvm.nativeimage.builder,org.graalvm.nativeimage.driver", ], } diff --git a/substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala/ScalaFeature.java b/substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala/ScalaFeature.java index 3bed9786eeea..bf345e9f82c1 100644 --- a/substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala/ScalaFeature.java +++ b/substratevm/src/com.oracle.svm.polyglot/src/com/oracle/svm/polyglot/scala/ScalaFeature.java @@ -41,6 +41,7 @@ import com.oracle.svm.core.graal.InternalFeature; import com.oracle.svm.core.util.UserError; import com.oracle.svm.hosted.FeatureImpl.BeforeAnalysisAccessImpl; +import com.oracle.svm.util.ModuleSupport; @AutomaticFeature public class ScalaFeature implements InternalFeature { @@ -66,10 +67,12 @@ public void beforeAnalysis(BeforeAnalysisAccess access) { RuntimeClassInitialization.initializeAtBuildTime("scala.Symbol$"); /* Initialized through an invokedynamic in `scala.Option` */ RuntimeClassInitialization.initializeAtBuildTime("scala.runtime.LambdaDeserialize"); + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, ScalaFeature.class, false, "jdk.internal.vm.compiler", "org.graalvm.compiler.nodes"); } @Override public void registerGraphBuilderPlugins(Providers providers, Plugins plugins, ParsingReason reason) { + ModuleSupport.accessPackagesToClass(ModuleSupport.Access.EXPORT, ScalaFeature.class, false, "jdk.internal.vm.ci", "jdk.vm.ci.meta"); if (SubstrateOptions.parseOnce() || reason == ParsingReason.PointsToAnalysis) { plugins.appendNodePlugin(new ScalaAnalysisPlugin()); } From 822e0e90ffb0efa673c232450fa6fc73cdcd1b11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 24 May 2022 10:31:10 +0200 Subject: [PATCH 36/41] Fix _native_unittest --force-builder-on-cp --- substratevm/mx.substratevm/mx_substratevm.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/substratevm/mx.substratevm/mx_substratevm.py b/substratevm/mx.substratevm/mx_substratevm.py index 277a37abc5d9..c8552a7b7ffa 100644 --- a/substratevm/mx.substratevm/mx_substratevm.py +++ b/substratevm/mx.substratevm/mx_substratevm.py @@ -591,8 +591,14 @@ def dummy_harness(test_deps, vm_launcher, vm_args): with open(unittest_file, 'r') as f: mx.log('Building junit image for matching: ' + ' '.join(l.rstrip() for l in f)) extra_image_args = mx.get_runtime_jvm_args(unittest_deps, jdk=mx_compiler.jdk, exclude_names=['substratevm:LIBRARY_SUPPORT']) - macro_junit = '--macro:junit' + ('cp' if force_builder_on_cp else '') - unittest_image = native_image(['-ea', '-esa'] + build_args + extra_image_args + [macro_junit + '=' + unittest_file, '-H:Path=' + junit_test_dir]) + macro_junit = '--macro:junit' + if force_builder_on_cp: + macro_junit += 'cp' + custom_env = os.environ.copy() + custom_env['USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM'] = 'false' + else: + custom_env = None + unittest_image = native_image(['-ea', '-esa'] + build_args + extra_image_args + [macro_junit + '=' + unittest_file, '-H:Path=' + junit_test_dir], env=custom_env) image_pattern_replacement = unittest_image + ".exe" if mx.is_windows() else unittest_image run_args = [arg.replace('${unittest.image}', image_pattern_replacement) for arg in run_args] mx.log('Running: ' + ' '.join(map(pipes.quote, [unittest_image] + run_args))) From 257affe41c475dfec17a5f511ef70485cd484666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 1 Jun 2022 19:10:00 +0200 Subject: [PATCH 37/41] Make NativeImageSystemClassLoader able to handle having module classloader with a parent ClassPathClassLoader --- .../hosted/NativeImageClassLoaderSupport.java | 8 +- .../hosted/NativeImageSystemClassLoader.java | 103 +++++++++++++----- 2 files changed, 82 insertions(+), 29 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index c4c93e558529..5ca72f7c1319 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -102,13 +102,19 @@ public class NativeImageClassLoaderSupport { public final ModuleLayer moduleLayerForImageBuild; public final ModuleFinder modulepathModuleFinder; + static final class ClassPathClassLoader extends URLClassLoader { + ClassPathClassLoader(URL[] urls, ClassLoader parent) { + super(urls, parent); + } + } + protected NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, String[] classpath, String[] modulePath) { classes = EconomicMap.create(); packages = EconomicMap.create(); emptySet = EconomicSet.create(); - classPathClassLoader = new URLClassLoader(Util.verifyClassPathAndConvertToURLs(classpath), defaultSystemClassLoader); + classPathClassLoader = new ClassPathClassLoader(Util.verifyClassPathAndConvertToURLs(classpath), defaultSystemClassLoader); imagecp = Arrays.stream(classPathClassLoader.getURLs()) .map(Util::urlToPath) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageSystemClassLoader.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageSystemClassLoader.java index d336adb0eb58..cf8cf8922caf 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageSystemClassLoader.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageSystemClassLoader.java @@ -25,11 +25,13 @@ package com.oracle.svm.hosted; import java.io.IOException; +import java.lang.reflect.Constructor; import java.lang.reflect.Method; import java.net.URL; import java.security.SecureClassLoader; import java.util.Collections; import java.util.Enumeration; +import java.util.List; import java.util.Set; import java.util.WeakHashMap; import java.util.jar.JarFile; @@ -135,34 +137,56 @@ public boolean isDisallowedClassLoader(ClassLoader c) { private static final Method defineClass = ReflectionUtil.lookupMethod(ClassLoader.class, "defineClass", String.class, byte[].class, int.class, int.class); - static Class loadClass(ClassLoader classLoader, String name, boolean resolve) throws ClassNotFoundException { - Class loadedClass = null; + private static final Constructor> compoundEnumerationConstructor; + static { + /* Reuse utility class defined as package-private class in java.lang.ClassLoader.java */ + String className = "java.lang.CompoundEnumeration"; try { - /* invoke the "loadClass" method on the current class loader */ - loadedClass = ((Class) loadClass.invoke(classLoader, name, resolve)); - } catch (Exception e) { - if (e.getCause() instanceof ClassNotFoundException) { - throw ((ClassNotFoundException) e.getCause()); + @SuppressWarnings("unchecked") + Class> compoundEnumerationClass = (Class>) Class.forName(className); + compoundEnumerationConstructor = ReflectionUtil.lookupConstructor(compoundEnumerationClass, Enumeration[].class); + } catch (ClassNotFoundException | ReflectionUtil.ReflectionUtilError e) { + throw VMError.shouldNotReachHere("Unable to get access to class " + className, e); + } + } + + private static Class loadClass(List activeClassLoaders, String name, boolean resolve) throws ClassNotFoundException { + ClassNotFoundException classNotFoundException = null; + for (ClassLoader loader : activeClassLoaders) { + try { + /* invoke the "loadClass" method on the current class loader */ + return ((Class) loadClass.invoke(loader, name, resolve)); + } catch (Exception e) { + if (e.getCause() instanceof ClassNotFoundException) { + classNotFoundException = ((ClassNotFoundException) e.getCause()); + } else { + String message = String.format("Can not load class: %s, with class loader: %s", name, loader); + VMError.shouldNotReachHere(message, e); + } } - String message = String.format("Can not load class: %s, with class loader: %s", name, classLoader); - VMError.shouldNotReachHere(message, e); } - return loadedClass; + VMError.guarantee(classNotFoundException != null); + throw classNotFoundException; } - static URL findResource(ClassLoader classLoader, String name) { - try { - // invoke the "findResource" method on the current class loader - return (URL) findResource.invoke(classLoader, name); - } catch (ReflectiveOperationException e) { - String message = String.format("Can not find resource: %s using class loader: %s", name, classLoader); - VMError.shouldNotReachHere(message, e); + private static URL findResource(List activeClassLoaders, String name) { + for (ClassLoader loader : activeClassLoaders) { + try { + // invoke the "findResource" method on the current class loader + Object url = findResource.invoke(loader, name); + if (url != null) { + return (URL) url; + } + } catch (ReflectiveOperationException | ClassCastException e) { + String message = String.format("Can not find resource: %s using class loader: %s", name, loader); + VMError.shouldNotReachHere(message, e); + } } return null; } @SuppressWarnings("unchecked") - static Enumeration findResources(ClassLoader classLoader, String name) { + private static Enumeration findResources(ClassLoader classLoader, String name) { try { // invoke the "findResources" method on the current class loader return (Enumeration) findResources.invoke(classLoader, name); @@ -186,22 +210,38 @@ static Class defineClass(ClassLoader classLoader, String name, byte[] b, int @Override protected Class loadClass(String name, boolean resolve) throws ClassNotFoundException { - return loadClass(getActiveClassLoader(), name, resolve); + return loadClass(getActiveClassLoaders(), name, resolve); } @Override protected URL findResource(String name) { - return findResource(getActiveClassLoader(), name); + return findResource(getActiveClassLoaders(), name); } @Override protected Enumeration findResources(String name) throws IOException { - return findResources(getActiveClassLoader(), name); + List activeClassLoaders = getActiveClassLoaders(); + assert !activeClassLoaders.isEmpty() & activeClassLoaders.size() <= 2; + ClassLoader activeClassLoader = activeClassLoaders.get(0); + ClassLoader activeClassLoaderParent = activeClassLoaders.size() > 1 ? activeClassLoaders.get(1) : null; + if (activeClassLoaderParent != null) { + return newCompoundEnumeration(findResources(activeClassLoaderParent, name), findResources(activeClassLoader, name)); + } + return findResources(activeClassLoader, name); + } + + @SuppressWarnings("unchecked") + private static Enumeration newCompoundEnumeration(Enumeration... enums) { + try { + return (Enumeration) compoundEnumerationConstructor.newInstance((Object) enums); + } catch (ReflectiveOperationException e) { + throw VMError.shouldNotReachHere("Cannot instantiate CompoundEnumeration", e); + } } public Class forNameOrNull(String name, boolean initialize) { try { - return Class.forName(name, initialize, getActiveClassLoader()); + return Class.forName(name, initialize, getActiveClassLoaders().get(0)); } catch (LinkageError | ClassNotFoundException ignored) { return null; } @@ -212,7 +252,7 @@ public Class predefineClass(String name, byte[] array, int offset, int length if (forNameOrNull(name, false) != null) { throw VMError.shouldNotReachHere("The class loader hierarchy already provides a class with the same name as the class submitted for predefinition: " + name); } - return defineClass(getActiveClassLoader(), name, array, offset, length); + return defineClass(getActiveClassLoaders().get(0), name, array, offset, length); } @Override @@ -224,11 +264,18 @@ public String toString() { '}'; } - private ClassLoader getActiveClassLoader() { - ClassLoader delegate = nativeImageClassLoader; - return delegate != null - ? delegate - : defaultSystemClassLoader; + private List getActiveClassLoaders() { + ClassLoader activeClassLoader = nativeImageClassLoader; + if (activeClassLoader != null) { + ClassLoader activeClassLoaderParent = activeClassLoader.getParent(); + if (activeClassLoaderParent instanceof NativeImageClassLoaderSupport.ClassPathClassLoader) { + return List.of(activeClassLoader, activeClassLoaderParent); + } else { + return List.of(activeClassLoader); + } + } else { + return List.of(defaultSystemClassLoader); + } } /** From c53bc18724dadd7075e69d18aefe814e38775406 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 7 Jun 2022 13:16:49 +0200 Subject: [PATCH 38/41] Remove package-private access to NativeImageClassLoaderSupport.classPathClassLoader --- .../svm/hosted/ClassLoaderSupportImpl.java | 18 +++++++++--------- .../hosted/NativeImageClassLoaderSupport.java | 10 +++++----- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java index 4747aaa16513..a1a119541fe5 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/ClassLoaderSupportImpl.java @@ -58,18 +58,25 @@ import com.oracle.svm.core.util.ClasspathUtils; import com.oracle.svm.core.util.UserError; import com.oracle.svm.core.util.VMError; +import com.oracle.svm.hosted.NativeImageClassLoaderSupport.ClassPathClassLoader; import com.oracle.svm.util.ModuleSupport; class ClassLoaderSupportImpl extends ClassLoaderSupport { private final NativeImageClassLoaderSupport classLoaderSupport; + private final ClassLoader imageClassLoader; + private final ClassPathClassLoader classPathClassLoader; private final Map> packageToModules; ClassLoaderSupportImpl(NativeImageClassLoaderSupport classLoaderSupport) { this.classLoaderSupport = classLoaderSupport; - this.imageClassLoader = classLoaderSupport.getClassLoader(); + + imageClassLoader = classLoaderSupport.getClassLoader(); + ClassLoader parent = imageClassLoader.getParent(); + classPathClassLoader = parent instanceof ClassPathClassLoader ? (ClassPathClassLoader) parent : null; + packageToModules = new HashMap<>(); buildPackageToModulesMap(classLoaderSupport); } @@ -77,16 +84,9 @@ class ClassLoaderSupportImpl extends ClassLoaderSupport { @Override protected boolean isNativeImageClassLoaderImpl(ClassLoader loader) { if (loader == imageClassLoader) { - /* Trivial case */ return true; } - if (loader == classLoaderSupport.classPathClassLoader) { - /* - * If imageClassLoader is a module classloader (building image with non-empty - * NativeImageClassLoaderSupport.imagemp), the passed loader could also be our - * classLoaderSupport.classPathClassLoader (building image with non-empty - * NativeImageClassLoaderSupport.imagecp). - */ + if (classPathClassLoader != null && loader == classPathClassLoader) { return true; } if (loader instanceof NativeImageSystemClassLoader) { diff --git a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java index 5ca72f7c1319..40750d7356b4 100644 --- a/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java +++ b/substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/NativeImageClassLoaderSupport.java @@ -95,8 +95,8 @@ public class NativeImageClassLoaderSupport { private final EconomicMap> packages; private final EconomicSet emptySet; - final URLClassLoader classPathClassLoader; - private final ClassLoader modulePathClassLoader; + private final ClassPathClassLoader classPathClassLoader; + private final ClassLoader classLoader; public final ModuleFinder upgradeAndSystemModuleFinder; public final ModuleLayer moduleLayerForImageBuild; @@ -146,7 +146,7 @@ protected NativeImageClassLoaderSupport(ClassLoader defaultSystemClassLoader, St adjustBootLayerQualifiedExports(moduleLayer); moduleLayerForImageBuild = moduleLayer; - modulePathClassLoader = getSingleClassloader(moduleLayer); + classLoader = getSingleClassloader(moduleLayer); modulepathModuleFinder = ModuleFinder.of(modulepath().toArray(Path[]::new)); } @@ -160,7 +160,7 @@ List applicationClassPath() { } public ClassLoader getClassLoader() { - return modulePathClassLoader; + return classLoader; } public void initAllClasses(ForkJoinPool executor, ImageClassLoader imageClassLoader) { @@ -507,7 +507,7 @@ private static UserError.UserException userErrorAddExportsAndOpensAndReads(Optio Class loadClassFromModule(Object module, String className) { assert module instanceof Module : "Argument `module` is not an instance of java.lang.Module"; Module m = (Module) module; - assert isModuleClassLoader(modulePathClassLoader, m.getClassLoader()) : "Argument `module` is java.lang.Module from unknown ClassLoader"; + assert isModuleClassLoader(classLoader, m.getClassLoader()) : "Argument `module` is java.lang.Module from unknown ClassLoader"; return Class.forName(m, className); } From b15a72fc4204459120cc427869d9e8b4b18da242 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Tue, 7 Jun 2022 13:27:37 +0200 Subject: [PATCH 39/41] Add GR-37582 completion to CHANGELOG --- substratevm/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/substratevm/CHANGELOG.md b/substratevm/CHANGELOG.md index 9b91fa029ef9..84828f113b6f 100644 --- a/substratevm/CHANGELOG.md +++ b/substratevm/CHANGELOG.md @@ -8,6 +8,7 @@ This changelog summarizes major changes to GraalVM Native Image. * (GR-37606) Add support for URLs and short descriptions to `Feature`. This info is shown as part of the build output. * (GR-38965) Heap dumps are now supported in Community Edition. * (GR-38951) Add `-XX:+DumpHeapAndExit` option to dump the initial heap of a native executable. +* (GR-37582) Run image-builder on module-path per default. Opt-out with env setting `USE_NATIVE_IMAGE_JAVA_PLATFORM_MODULE_SYSTEM=false`. ## Version 22.1.0 * (GR-36568) Add "Quick build" mode, enabled through option `-Ob`, for quicker native image builds. From c31bc8be52f9e97cf59b1c01ab4c4827bf6f1845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 8 Jun 2022 12:19:33 +0200 Subject: [PATCH 40/41] Make ModuleSupport.isModulePathBuild private --- .../src/com/oracle/svm/driver/NativeImage.java | 14 +++++++++++++- .../src/com/oracle/svm/util/ModuleSupport.java | 2 +- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java index 2447aac671d2..63905cad8bd7 100644 --- a/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java +++ b/substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java @@ -31,6 +31,7 @@ import java.io.InputStreamReader; import java.lang.management.ManagementFactory; import java.lang.management.OperatingSystemMXBean; +import java.lang.reflect.Method; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.InvalidPathException; @@ -89,6 +90,7 @@ import com.oracle.svm.hosted.NativeImageGeneratorRunner; import com.oracle.svm.hosted.NativeImageSystemClassLoader; import com.oracle.svm.util.ModuleSupport; +import com.oracle.svm.util.ReflectionUtil; public class NativeImage { @@ -260,6 +262,12 @@ private static String oR(OptionKey option) { protected static class BuildConfiguration { + /* + * Reuse com.oracle.svm.util.ModuleSupport.isModulePathBuild() to ensure same interpretation + * of com.oracle.svm.util.ModuleSupport.ENV_VAR_USE_MODULE_SYSTEM environment variable use. + */ + private static final Method isModulePathBuild = ReflectionUtil.lookupMethod(ModuleSupport.class, "isModulePathBuild"); + boolean modulePathBuild; String imageBuilderModeEnforcer; @@ -281,7 +289,11 @@ protected BuildConfiguration(List args) { @SuppressWarnings("deprecation") BuildConfiguration(Path rootDir, Path workDir, List args) { - modulePathBuild = ModuleSupport.isModulePathBuild(); + try { + modulePathBuild = (boolean) isModulePathBuild.invoke(null); + } catch (ReflectiveOperationException | ClassCastException e) { + VMError.shouldNotReachHere(e); + } imageBuilderModeEnforcer = null; this.args = args; this.workDir = workDir != null ? workDir : Paths.get(".").toAbsolutePath().normalize(); diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index eec6f92147df..c0d4a2debf33 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -43,7 +43,7 @@ public final class ModuleSupport { private ModuleSupport() { } - public static boolean isModulePathBuild() { + private static boolean isModulePathBuild() { return !"false".equalsIgnoreCase(System.getenv().get(ENV_VAR_USE_MODULE_SYSTEM)); } From bfb082e19aa9ed5a0df0d7c0cd9494f0b1e1263a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20W=C3=B6gerer?= Date: Wed, 8 Jun 2022 12:56:48 +0200 Subject: [PATCH 41/41] Throw ModuleSupportError if module not found and accessPackagesToClass not optional --- .../com/oracle/svm/util/ModuleSupport.java | 35 ++++++++++--------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java index c0d4a2debf33..53cb2f65fb6b 100644 --- a/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java +++ b/substratevm/src/com.oracle.svm.util/src/com/oracle/svm/util/ModuleSupport.java @@ -24,7 +24,6 @@ */ package com.oracle.svm.util; -import java.util.NoSuchElementException; import java.util.Objects; import java.util.Optional; import java.util.Set; @@ -90,6 +89,13 @@ public static void accessModuleByClass(Access access, Class accessingClass, C accessModuleByClass(access, accessingClass, declaringClass.getModule(), declaringClass.getPackageName()); } + @SuppressWarnings("serial") + public static final class ModuleSupportError extends Error { + private ModuleSupportError(String message) { + super(message); + } + } + /** * Open or export packages {@code packageNames} in the module named {@code moduleName} to module * of given {@code accessingClass}. If {@code accessingClass} is null packages are opened or @@ -98,10 +104,18 @@ public static void accessModuleByClass(Access access, Class accessingClass, C */ @Platforms(Platform.HOSTED_ONLY.class) public static void accessPackagesToClass(Access access, Class accessingClass, boolean optional, String moduleName, String... packageNames) { - Module declaringModule = getModule(moduleName, optional); - if (declaringModule == null) { - return; + Objects.requireNonNull(moduleName); + Optional module = ModuleLayer.boot().findModule(moduleName); + if (module.isEmpty()) { + if (optional) { + return; + } + String accessor = accessingClass != null ? "class " + accessingClass.getTypeName() : "ALL-UNNAMED"; + String message = access.name().toLowerCase() + " of packages from module " + moduleName + " to " + + accessor + " failed. No module named " + moduleName + " in boot layer."; + throw new ModuleSupportError(message); } + Module declaringModule = module.get(); Objects.requireNonNull(packageNames); Set packages = packageNames.length > 0 ? Set.of(packageNames) : declaringModule.getPackages(); for (String packageName : packages) { @@ -109,19 +123,6 @@ public static void accessPackagesToClass(Access access, Class accessingClass, } } - @Platforms(Platform.HOSTED_ONLY.class) - private static Module getModule(String moduleName, boolean optional) { - Objects.requireNonNull(moduleName); - Optional declaringModuleOpt = ModuleLayer.boot().findModule(moduleName); - if (declaringModuleOpt.isEmpty()) { - if (optional) { - return null; - } - throw new NoSuchElementException(moduleName); - } - return declaringModuleOpt.get(); - } - @Platforms(Platform.HOSTED_ONLY.class) private static void accessModuleByClass(Access access, Class accessingClass, Module declaringModule, String packageName) { Module namedAccessingModule = null;