Skip to content
Merged
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
Expand Up @@ -40,6 +40,7 @@
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.FIELD_READ_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.FIELD_WRITTEN_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.ID_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.IMAGE_HEAP_SIZE_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.INSTANCE_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.INTERFACES_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.IS_ENUM_TAG;
Expand Down Expand Up @@ -252,6 +253,8 @@ public class ImageLayerLoader {

protected EconomicMap<String, Object> jsonMap;

private long imageHeapSize;

public ImageLayerLoader() {
this(new ImageLayerSnapshotUtil(), List.of());
}
Expand Down Expand Up @@ -308,6 +311,8 @@ private void loadLayerAnalysis0() {
int nextFieldId = get(jsonMap, NEXT_FIELD_ID_TAG);
universe.setStartFieldId(nextFieldId);

imageHeapSize = Long.parseLong(get(jsonMap, IMAGE_HEAP_SIZE_TAG));

/* This mapping allows one to get the base layer information from a type id */
EconomicMap<String, Object> typesMap = get(jsonMap, TYPES_TAG);
MapCursor<String, Object> typesCursor = typesMap.getEntries();
Expand Down Expand Up @@ -985,4 +990,8 @@ private ImageHeapConstant getTaggedImageHeapConstant(String tag) {
int id = get(jsonMap, tag);
return constants.get(id);
}

public long getImageHeapSize() {
return imageHeapSize;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public class ImageLayerSnapshotUtil {
public static final String NEXT_TYPE_ID_TAG = "next type id";
public static final String NEXT_METHOD_ID_TAG = "next method id";
public static final String NEXT_FIELD_ID_TAG = "next field id";
public static final String IMAGE_HEAP_SIZE_TAG = "image heap size";
public static final String VALUE_TAG = "value";
public static final String ENUM_CLASS_TAG = "enum class";
public static final String ENUM_NAME_TAG = "enum name";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.FIELD_WRITTEN_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.IDENTITY_HASH_CODE_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.ID_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.IMAGE_HEAP_SIZE_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.INSTANCE_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.INTERFACES_TAG;
import static com.oracle.graal.pointsto.heap.ImageLayerSnapshotUtil.IS_ENUM_TAG;
Expand Down Expand Up @@ -145,6 +146,10 @@ public void dumpFile() {
FileDumpingUtil.dumpFile(fileInfo.layerSnapshotPath, fileInfo.fileName, fileInfo.suffix, writer -> JSONFormatter.printJSON(jsonMap, writer));
}

public void persistImageHeapSize(long imageHeapSize) {
jsonMap.put(IMAGE_HEAP_SIZE_TAG, String.valueOf(imageHeapSize));
}

public void persistAnalysisInfo(Universe hostedUniverse, AnalysisUniverse analysisUniverse) {
persistHook(hostedUniverse, analysisUniverse);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -775,9 +775,6 @@ public Type[] getGenericParameterTypes() {

@Override
public boolean canBeInlined() {
if (isInBaseLayer) {
return false;
}
return !hasNeverInlineDirective();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -433,15 +433,28 @@ public int getPreferredAddressSpaceAlignment() {
@Fold
@Override
public int getImageHeapOffsetInAddressSpace() {
int imageHeapOffset = 0;
if (SubstrateOptions.SpawnIsolates.getValue() && SubstrateOptions.UseNullRegion.getValue()) {
/*
* The image heap will be mapped in a way that there is a memory protected gap between
* the heap base and the start of the image heap. The gap won't need any memory in the
* native image file.
*/
return NumUtil.safeToInt(SerialAndEpsilonGCOptions.AlignedHeapChunkSize.getValue());
imageHeapOffset = NumUtil.safeToInt(SerialAndEpsilonGCOptions.AlignedHeapChunkSize.getValue());
}
return 0;
/*
* GR-53993: With ImageLayerSupport, it would be possible to fetch the startOffset from the
* loader instead of storing it in the Heap.
*/
if (SubstrateOptions.LoadImageLayer.hasBeenSet()) {
/*
* GR-53964: The page size used to round up the start offset should be the same as the
* one used in run time.
*/
int runtimePageSize = 4096;
imageHeapOffset = NumUtil.roundUp(imageHeapOffset + NumUtil.safeToInt(startOffset), runtimePageSize);
}
return imageHeapOffset;
}

@Fold
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,20 @@
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_RELOCATABLE_END;
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_WRITABLE_BEGIN;
import static com.oracle.svm.core.Isolates.IMAGE_HEAP_WRITABLE_END;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_A_RELOCATABLE_POINTER;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_BEGIN;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_END;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_RELOCATABLE_BEGIN;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_RELOCATABLE_END;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_WRITABLE_BEGIN;
import static com.oracle.svm.core.layeredimage.LayeredImageHeapSymbols.SECOND_IMAGE_HEAP_WRITABLE_END;
import static com.oracle.svm.core.posix.linux.ProcFSSupport.findMapping;
import static com.oracle.svm.core.util.PointerUtils.roundDown;
import static com.oracle.svm.core.util.UnsignedUtils.isAMultiple;
import static com.oracle.svm.core.util.UnsignedUtils.roundUp;
import static org.graalvm.word.WordFactory.signed;

import java.nio.ByteBuffer;
import java.util.concurrent.ThreadLocalRandom;

import org.graalvm.nativeimage.StackValue;
Expand All @@ -47,8 +56,10 @@
import org.graalvm.word.PointerBase;
import org.graalvm.word.SignedWord;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordBase;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.SubstrateOptions;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;
Expand All @@ -68,6 +79,7 @@
import com.oracle.svm.core.util.VMError;

import jdk.graal.compiler.word.Word;
import jdk.vm.ci.code.Architecture;

/**
* An optimal image heap provider for Linux which creates isolate image heaps that retain the
Expand All @@ -91,11 +103,41 @@ public class LinuxImageHeapProvider extends AbstractImageHeapProvider {

private static final SignedWord UNASSIGNED_FD = signed(-1);
private static final SignedWord CANNOT_OPEN_FD = signed(-2);
private static final CGlobalData<WordPointer> CACHED_IMAGE_FD = CGlobalDataFactory.createWord(UNASSIGNED_FD);
private static final CGlobalData<WordPointer> CACHED_IMAGE_HEAP_OFFSET_IN_FILE = CGlobalDataFactory.createWord();

private static final int IMAGE_COUNT = 2;
private static final CGlobalData<WordPointer> CACHED_IMAGE_FDS = CGlobalDataFactory.createBytes(() -> createWords(IMAGE_COUNT, UNASSIGNED_FD));
private static final CGlobalData<WordPointer> CACHED_IMAGE_HEAP_OFFSETS = CGlobalDataFactory.createBytes(() -> createWords(IMAGE_COUNT, WordFactory.zero()));

private static final int MAX_PATHLEN = 4096;

private static byte[] createWords(int count, WordBase initialValue) {
Architecture arch = ConfigurationValues.getTarget().arch;
assert arch.getWordSize() == Long.BYTES : "currently hard-coded for 8 byte words";
ByteBuffer buffer = ByteBuffer.allocate(count * Long.BYTES).order(arch.getByteOrder());
for (int i = 0; i < count; i++) {
buffer.putLong(initialValue.rawValue());
}
return buffer.array();
}

@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
protected UnsignedWord getImageHeapAddressSpaceSize() {
if (SubstrateOptions.ImageLayer.hasBeenSet() || SubstrateOptions.LoadImageLayer.hasBeenSet()) {
int imageHeapOffset = Heap.getHeap().getImageHeapOffsetInAddressSpace();
assert imageHeapOffset >= 0;
UnsignedWord size = WordFactory.unsigned(imageHeapOffset);
UnsignedWord granularity = VirtualMemoryProvider.get().getGranularity();
assert isAMultiple(size, granularity);
size = size.add(getImageHeapSizeInFile(IMAGE_HEAP_BEGIN.get(), IMAGE_HEAP_END.get()));
size = roundUp(size, granularity);
size = size.add(getImageHeapSizeInFile(SECOND_IMAGE_HEAP_BEGIN.get(), SECOND_IMAGE_HEAP_END.get()));
return size;
} else {
return super.getImageHeapAddressSpaceSize();
}
}

@Override
@Uninterruptible(reason = "Called during isolate initialization.")
public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, WordPointer basePointer, WordPointer endPointer) {
Expand Down Expand Up @@ -143,15 +185,30 @@ public int initialize(Pointer reservedAddressSpace, UnsignedWord reservedSize, W

int imageHeapOffsetInAddressSpace = Heap.getHeap().getImageHeapOffsetInAddressSpace();
basePointer.write(heapBase);
Pointer imageHeapStart = heapBase.add(imageHeapOffsetInAddressSpace);
Pointer firstHeapStart = heapBase.add(imageHeapOffsetInAddressSpace);
remainingSize = remainingSize.subtract(imageHeapOffsetInAddressSpace);
int result = initializeImageHeap(imageHeapStart, remainingSize, endPointer,
CACHED_IMAGE_FD.get(), CACHED_IMAGE_HEAP_OFFSET_IN_FILE.get(), MAGIC.get(),
boolean layeredImage = SubstrateOptions.ImageLayer.hasBeenSet() || SubstrateOptions.LoadImageLayer.hasBeenSet();
WordPointer firstEndPtr = layeredImage ? StackValue.get(WordPointer.class) : endPointer;
int result = initializeImageHeap(firstHeapStart, remainingSize, firstEndPtr,
CACHED_IMAGE_FDS.get().addressOf(0), CACHED_IMAGE_HEAP_OFFSETS.get().addressOf(0), MAGIC.get(),
IMAGE_HEAP_BEGIN.get(), IMAGE_HEAP_END.get(),
IMAGE_HEAP_RELOCATABLE_BEGIN.get(), IMAGE_HEAP_A_RELOCATABLE_POINTER.get(), IMAGE_HEAP_RELOCATABLE_END.get(),
IMAGE_HEAP_WRITABLE_BEGIN.get(), IMAGE_HEAP_WRITABLE_END.get());
if (result != CEntryPointErrors.NO_ERROR) {
freeImageHeap(selfReservedHeapBase);
return result;
}
if (SubstrateOptions.ImageLayer.hasBeenSet() || SubstrateOptions.LoadImageLayer.hasBeenSet()) {
Pointer secondHeapStart = firstEndPtr.read(); // aligned
remainingSize = remainingSize.subtract(secondHeapStart.subtract(firstHeapStart));
result = initializeImageHeap(secondHeapStart, remainingSize, endPointer,
CACHED_IMAGE_FDS.get().addressOf(1), CACHED_IMAGE_HEAP_OFFSETS.get().addressOf(1), MAGIC.get(),
SECOND_IMAGE_HEAP_BEGIN.get(), SECOND_IMAGE_HEAP_END.get(),
SECOND_IMAGE_HEAP_RELOCATABLE_BEGIN.get(), SECOND_IMAGE_HEAP_A_RELOCATABLE_POINTER.get(), SECOND_IMAGE_HEAP_RELOCATABLE_END.get(),
SECOND_IMAGE_HEAP_WRITABLE_BEGIN.get(), SECOND_IMAGE_HEAP_WRITABLE_END.get());
if (result != CEntryPointErrors.NO_ERROR) {
freeImageHeap(selfReservedHeapBase);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ protected void onValueUpdate(EconomicMap<OptionKey<?>, Object> values, Boolean o
ClosedTypeWorld.update(values, !newValue);
PersistImageLayerAnalysis.update(values, newValue);
PersistImageLayerSingletons.update(values, newValue);
DeleteLocalSymbols.update(values, !newValue);
StripDebugInfo.update(values, !newValue);
InternalSymbolsAreGlobal.update(values, newValue);
AOTTrivialInline.update(values, !newValue);
if (imageLayerEnabledHandler != null) {
imageLayerEnabledHandler.onOptionEnabled(values);
Expand Down Expand Up @@ -722,8 +720,6 @@ public static boolean useLIRBackend() {
*/
@Option(help = "Use linker option to prevent unreferenced symbols in image.")//
public static final HostedOptionKey<Boolean> RemoveUnusedSymbols = new HostedOptionKey<>(OS.getCurrent() != OS.DARWIN);
@Option(help = "Keep all undefined symbols.")//
public static final HostedOptionKey<Boolean> PreserveUndefinedSymbols = new HostedOptionKey<>(false);
@Option(help = "Ignore undefined symbols referenced from the built image.")//
public static final HostedOptionKey<Boolean> IgnoreUndefinedReferences = new HostedOptionKey<>(false);
@Option(help = "Use linker option to remove all local symbols from image.")//
Expand Down Expand Up @@ -1152,7 +1148,6 @@ public static boolean closedTypeWorld() {
public void update(EconomicMap<OptionKey<?>, Object> values, Object boxedValue) {
super.update(values, boxedValue);
ClosedTypeWorld.update(values, false);
PreserveUndefinedSymbols.update(values, true);
/* Ignore any potential undefined references caused by inlining in base layer. */
IgnoreUndefinedReferences.update(values, true);
AOTTrivialInline.update(values, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import jdk.graal.compiler.api.replacements.Fold;

public abstract class Heap {
protected long startOffset;

@Fold
public static Heap getHeap() {
return ImageSingletons.lookup(Heap.class);
Expand Down Expand Up @@ -250,4 +252,11 @@ public Pointer getImageHeapStart() {
*/
@Uninterruptible(reason = "Ensure that no GC can occur between this call and usage of the salt.", callerMustBe = true)
public abstract long getIdentityHashSalt(Object obj);

/**
* Sets the start offset of the heap.
*/
public void setStartOffset(long startOffset) {
this.startOffset = startOffset;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* 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.layeredimage;

import com.oracle.svm.core.c.CGlobalData;
import com.oracle.svm.core.c.CGlobalDataFactory;

import jdk.graal.compiler.word.Word;

/**
* Stores the heap symbols for the application layer. This is a temporary solution and should be
* fixed with GR-53995.
*/
public class LayeredImageHeapSymbols {
public static final String SECOND_IMAGE_HEAP_BEGIN_SYMBOL_NAME = "__svm_second_heap_begin";
public static final String SECOND_IMAGE_HEAP_END_SYMBOL_NAME = "__svm_second_heap_end";
public static final String SECOND_IMAGE_HEAP_RELOCATABLE_BEGIN_SYMBOL_NAME = "__svm_second_heap_relocatable_begin";
public static final String SECOND_IMAGE_HEAP_RELOCATABLE_END_SYMBOL_NAME = "__svm_second_heap_relocatable_end";
public static final String SECOND_IMAGE_HEAP_A_RELOCATABLE_POINTER_SYMBOL_NAME = "__svm_second_heap_a_relocatable_pointer";
public static final String SECOND_IMAGE_HEAP_WRITABLE_BEGIN_SYMBOL_NAME = "__svm_second_heap_writable_begin";
public static final String SECOND_IMAGE_HEAP_WRITABLE_END_SYMBOL_NAME = "__svm_second_heap_writable_end";

public static final CGlobalData<Word> SECOND_IMAGE_HEAP_BEGIN = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_BEGIN_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_END = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_END_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_RELOCATABLE_BEGIN = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_RELOCATABLE_BEGIN_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_RELOCATABLE_END = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_RELOCATABLE_END_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_A_RELOCATABLE_POINTER = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_A_RELOCATABLE_POINTER_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_WRITABLE_BEGIN = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_WRITABLE_BEGIN_SYMBOL_NAME);
public static final CGlobalData<Word> SECOND_IMAGE_HEAP_WRITABLE_END = CGlobalDataFactory.forSymbol(SECOND_IMAGE_HEAP_WRITABLE_END_SYMBOL_NAME);
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
import com.oracle.svm.core.graal.word.SubstrateWordOperationPlugins;
import com.oracle.svm.core.graal.word.SubstrateWordTypes;
import com.oracle.svm.core.heap.BarrierSetProvider;
import com.oracle.svm.core.heap.Heap;
import com.oracle.svm.core.heap.RestrictHeapAccessCallees;
import com.oracle.svm.core.hub.DynamicHub;
import com.oracle.svm.core.hub.LayoutEncoding;
Expand Down Expand Up @@ -1042,6 +1043,10 @@ protected void setupNativeImage(OptionValues options, Map<Method, CEntryPointDat
featureHandler.forEachFeature(feature -> feature.duringSetup(config));
}

if (SVMImageLayerSupport.singleton().loadAnalysis()) {
Heap.getHeap().setStartOffset(SVMImageLayerSupport.singleton().getLoader().getImageHeapSize());
}

initializeBigBang(bb, options, featureHandler, nativeLibraries, debug, aMetaAccess, aUniverse.getSubstitutions(), loader, true,
new SubstrateClassInitializationPlugin((SVMHost) aUniverse.hostVM()), this.isStubBasedPluginsSupported(), aProviders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public List<String> getImageSymbols(boolean onlyGlobal) {
Set<String> globalHiddenSymbols = CGlobalDataFeature.singleton().getGlobalHiddenSymbols();
stream = stream.filter(symbol -> symbol.isGlobal() && !globalHiddenSymbols.contains(symbol.getName()));
}
if (!(SubstrateOptions.useLLVMBackend() || SubstrateOptions.PreserveUndefinedSymbols.getValue())) {
if (!SubstrateOptions.useLLVMBackend()) {
stream = stream.filter(ObjectFile.Symbol::isDefined);
}
return stream.map(this::getSymbolName).collect(Collectors.toList());
Expand Down
Loading