Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. 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.core.imagelayer;

import java.util.function.BooleanSupplier;

import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

@Platforms(Platform.HOSTED_ONLY.class)
public class BuildingInitialLayerPredicate implements BooleanSupplier {
@Override
public boolean getAsBoolean() {
return ImageLayerBuildingSupport.buildingInitialLayer();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* Copyright (c) 2025, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. 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.core.jdk;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingleton;
import com.oracle.svm.core.util.UserError;

/**
* This singleton keeps track of the {@code Module#openPackages} and {@code Module#exportedPackages}
* from all image layers.
*/
@Platforms(Platform.HOSTED_ONLY.class)
public abstract class LayeredModuleSingleton implements LayeredImageSingleton {
public static final String ALL_UNNAMED_MODULE_NAME = "native-image-all-unnamed";
public static final String EVERYONE_MODULE_NAME = "native-image-everyone";

protected final Map<String, Map<String, Set<String>>> moduleOpenPackages;
protected final Map<String, Map<String, Set<String>>> moduleExportedPackages;

private final Map<String, Module> moduleNames = new HashMap<>();

private Module everyoneModule;
private Module allUnnamedModule;

public LayeredModuleSingleton() {
this(new HashMap<>(), new HashMap<>());
}

public LayeredModuleSingleton(Map<String, Map<String, Set<String>>> moduleOpenPackages, Map<String, Map<String, Set<String>>> moduleExportedPackages) {
this.moduleOpenPackages = moduleOpenPackages;
this.moduleExportedPackages = moduleExportedPackages;
}

public static LayeredModuleSingleton singleton() {
return ImageSingletons.lookup(LayeredModuleSingleton.class);
}

public void setUnnamedModules(Module everyoneModule, Module allUnnamedModule) {
this.everyoneModule = everyoneModule;
this.allUnnamedModule = allUnnamedModule;
}

public Map<String, Set<String>> getOpenPackages(Module module) {
return moduleOpenPackages.get(module.getName());
}

public Map<String, Set<String>> getExportedPackages(Module module) {
return moduleExportedPackages.get(module.getName());
}

public Collection<Module> getModules() {
return moduleNames.values();
}

public void setOpenPackages(Module module, Map<String, Set<Module>> openPackages) {
setPackages(module, moduleOpenPackages, openPackages, "opened");
}

public void setExportedPackages(Module module, Map<String, Set<Module>> exportedPackages) {
setPackages(module, moduleExportedPackages, exportedPackages, "exported");
}

private void setPackages(Module module, Map<String, Map<String, Set<String>>> modulePackages, Map<String, Set<Module>> packages, String mode) {
Module oldValue = moduleNames.put(module.toString(), module);
if (oldValue != null && oldValue != module) {
throw UserError.abort("Layered images require all modules to have a different name because their identity hash code is not consistent across layers. " +
"The modules %s and %s have the same name and were added to the %s packages", module, oldValue, mode);
}
Map<String, Set<String>> namesMap = modulePackages.computeIfAbsent(module.getName(), k -> new HashMap<>());
for (var entry : packages.entrySet()) {
Set<String> modules = namesMap.computeIfAbsent(entry.getKey(), k -> new HashSet<>());
modules.addAll(entry.getValue().stream().map(Module::getName).toList());
modules.remove(null);
if (entry.getValue().contains(allUnnamedModule)) {
modules.add(ALL_UNNAMED_MODULE_NAME);
}
if (entry.getValue().contains(everyoneModule)) {
modules.add(EVERYONE_MODULE_NAME);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

Expand Down Expand Up @@ -147,6 +148,9 @@ public record ModuleResourceKey(Module module, String resource) {

private GlobTrieNode<ConditionWithOrigin> resourcesTrieRoot;

@Platforms(Platform.HOSTED_ONLY.class) //
private Function<Module, Module> hostedToRuntimeModuleMapper;

Resources() {
}

Expand Down Expand Up @@ -194,12 +198,17 @@ public static ModuleResourceKey createStorageKey(Module module, String resourceN
Module m = module != null && module.isNamed() ? module : null;
if (ImageInfo.inImageBuildtimeCode()) {
if (m != null) {
m = RuntimeModuleSupport.instance().getRuntimeModuleForHostedModule(m);
m = currentLayer().hostedToRuntimeModuleMapper.apply(m);
}
}
return new ModuleResourceKey(m, resourceName);
}

@Platforms(Platform.HOSTED_ONLY.class) //
public void setHostedToRuntimeModuleMapper(Function<Module, Module> hostedToRuntimeModuleMapper) {
this.hostedToRuntimeModuleMapper = hostedToRuntimeModuleMapper;
}

@Platforms(Platform.HOSTED_ONLY.class)
public static Set<String> getIncludedResourcesModules() {
return StreamSupport.stream(currentLayer().resources.getKeys().spliterator(), false)
Expand Down Expand Up @@ -472,7 +481,7 @@ public static InputStream createInputStream(Module module, String resourceName)
* If module is not specified or is an unnamed module and entry was not found as
* classpath-resource we have to search for the resource in all modules in the image.
*/
for (Module m : RuntimeModuleSupport.instance().getBootLayer().modules()) {
for (Module m : RuntimeModuleSupport.singleton().getBootLayer().modules()) {
entry = getAtRuntime(m, resourceName, false);
if (entry != MISSING_METADATA_MARKER) {
isInMetadata = true;
Expand Down Expand Up @@ -510,7 +519,7 @@ public static Enumeration<URL> createURLs(Module module, String resourceName) {

/* If moduleName was unspecified we have to consider all modules in the image */
if (moduleName(module) == null) {
for (Module m : RuntimeModuleSupport.instance().getBootLayer().modules()) {
for (Module m : RuntimeModuleSupport.singleton().getBootLayer().modules()) {
ResourceStorageEntryBase entry = getAtRuntime(m, resourceName, false);
if (entry == MISSING_METADATA_MARKER) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@
*/
package com.oracle.svm.core.jdk;

import java.util.function.Function;
import java.util.EnumSet;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;

import com.oracle.svm.core.BuildPhaseProvider.AfterHostedUniverse;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.heap.UnknownObjectField;
import com.oracle.svm.core.imagelayer.LastImageBuildPredicate;
import com.oracle.svm.core.layeredimagesingleton.ApplicationLayerOnlyImageSingleton;
import com.oracle.svm.core.layeredimagesingleton.LayeredImageSingletonBuilderFlags;
import com.oracle.svm.core.layeredimagesingleton.UnsavedSingleton;

/**
* Runtime module support singleton, containing the runtime boot module layer. The boot module layer
Expand All @@ -42,18 +47,15 @@
* counterpart. The lookup function is implemented inside the module layer synthesis feature. See
* {@code ModuleLayerFeature} for more information.
*/
public final class RuntimeModuleSupport {

public static RuntimeModuleSupport instance() {
@AutomaticallyRegisteredImageSingleton(onlyWith = LastImageBuildPredicate.class)
public final class RuntimeModuleSupport implements ApplicationLayerOnlyImageSingleton, UnsavedSingleton {
public static RuntimeModuleSupport singleton() {
return ImageSingletons.lookup(RuntimeModuleSupport.class);
}

@UnknownObjectField(availability = AfterHostedUniverse.class) //
private ModuleLayer bootLayer;

@Platforms(Platform.HOSTED_ONLY.class) //
private Function<Module, Module> hostedToRuntimeModuleMapper;

@Platforms(Platform.HOSTED_ONLY.class) //
public void setBootLayer(ModuleLayer bootLayer) {
this.bootLayer = bootLayer;
Expand All @@ -63,13 +65,8 @@ public ModuleLayer getBootLayer() {
return bootLayer;
}

@Platforms(Platform.HOSTED_ONLY.class) //
public void setHostedToRuntimeModuleMapper(Function<Module, Module> hostedToRuntimeModuleMapper) {
this.hostedToRuntimeModuleMapper = hostedToRuntimeModuleMapper;
@Override
public EnumSet<LayeredImageSingletonBuilderFlags> getImageBuilderFlags() {
return LayeredImageSingletonBuilderFlags.ALL_ACCESS;
}

public Module getRuntimeModuleForHostedModule(Module hostedModule) {
return hostedToRuntimeModuleMapper.apply(hostedModule);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@
*/
package com.oracle.svm.core.jdk;

import java.util.Objects;
import java.util.Set;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.annotate.TargetElement;
import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport;
import com.oracle.svm.core.util.BasedOnJDKFile;

/**
Expand All @@ -54,6 +58,17 @@ public final class Target_java_lang_Module {
// @Stable (no effect currently GR-60154)
private ModuleLayer layer;

/**
* Creating an {@link Alias} directly for {@code ALL_UNNAMED_MODULE} and {@code EVERYONE_MODULE}
* makes {@code java.util.regex.Pattern} reachable, which increases the size of the binary.
*/
// Checkstyle: stop
@Alias //
private static Set<Module> ALL_UNNAMED_MODULE_SET;
@Alias //
private static Set<Module> EVERYONE_SET;
// Checkstyle: resume

@Substitute
@TargetElement(onlyWith = ForeignDisabled.class)
@SuppressWarnings("static-method")
Expand Down Expand Up @@ -93,4 +108,35 @@ private static void addExportsToAll0(Module from, String pn) {
private static void addExportsToAllUnnamed0(Module from, String pn) {
ModuleNative.addExportsToAllUnnamed(from, pn);
}

@Substitute
@SuppressWarnings("static-method")
private boolean allows(Set<Module> targets, Module module) {
if (targets != null) {
Module everyoneModule = EVERYONE_SET.stream().findFirst().get();
if (targets.contains(everyoneModule)) {
return true;
}
if (module != everyoneModule) {
if (targets.contains(module)) {
return true;
}
if (!module.isNamed() && targets.contains(ALL_UNNAMED_MODULE_SET.stream().findFirst().get())) {
return true;
}
if (ImageLayerBuildingSupport.buildingImageLayer()) {
for (var m : targets) {
/*
* This is based on the assumption that in Layered Image, all modules have
* different names. This is ensured in LayeredModuleSingleton.setPackages.
*/
if (Objects.equals(m.getName(), module.getName())) {
return true;
}
}
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,24 @@
*/
package com.oracle.svm.core.jdk;

import java.util.List;

import org.graalvm.nativeimage.hosted.FieldValueTransformer;

import com.oracle.svm.core.annotate.Alias;
import com.oracle.svm.core.annotate.RecomputeFieldValue;
import com.oracle.svm.core.annotate.Substitute;
import com.oracle.svm.core.annotate.TargetClass;
import jdk.internal.loader.ClassLoaderValue;
import org.graalvm.nativeimage.hosted.FieldValueTransformer;

import java.util.List;
import jdk.internal.loader.ClassLoaderValue;

@SuppressWarnings("unused")
@TargetClass(value = java.lang.ModuleLayer.class)
final class Target_java_lang_ModuleLayer {

@Substitute
public static ModuleLayer boot() {
return RuntimeModuleSupport.instance().getBootLayer();
return RuntimeModuleSupport.singleton().getBootLayer();
}

@Alias @RecomputeFieldValue(kind = RecomputeFieldValue.Kind.Custom, declClass = ModuleLayerCLVTransformer.class, isFinal = true) //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ struct SharedLayerSnapshot {
dynamicHubInfos @18 :List(DynamicHubInfo);
hostedMethods @19 :List(PersistedHostedMethod);
nodeClassMapLocation @20 :Text;
sharedLayerBootLayerModules @21 :List(Text);
layeredModule @22 :LayeredModule;
}

struct StaticFinalFieldFoldingSingleton {
Expand All @@ -293,6 +295,21 @@ struct LayeredRuntimeMetadataSingleton {
fields @1 :List(FieldId);
}

struct LayeredModule {
openModulePackages @0 :List(ModulePackages);
exportedModulePackages @1 :List(ModulePackages);
}

struct ModulePackages {
moduleKey @0 :Text;
packages @1 :List(Packages);
}

struct Packages {
packageKey @0 :Text;
modules @1 :List(Text);
}

struct PrimitiveValue {
typeChar @0 :Int8;
rawValue @1 :Int64;
Expand Down
Loading
Loading