Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ public String toString() {
*/
private int targetCodeSize;

/**
* bytes that make up one NOP instruction
*/
private byte[] nopCode;

/**
* number of bytes that make up one NOP instruction
*/
private int nopCodeSize;


private ArrayList<CodeAnnotation> annotations;

private Assumption[] assumptions;
Expand Down Expand Up @@ -476,6 +487,18 @@ public void setTargetCode(byte[] code, int size) {
targetCodeSize = size;
}

/**
* Sets the machine that has been generated by the compiler.
*
* @param code the machine code generated
* @param size the size of the machine code
*/
public void setNopCode(byte[] code, int size) {
checkOpen();
nopCode = code;
nopCodeSize = size;
}

/**
* Records a data patch in the code section. The data patch can refer to something in the
* {@link DataSectionReference data section} or directly to an {@link ConstantReference inlined
Expand Down Expand Up @@ -656,6 +679,20 @@ public int getTargetCodeSize() {
return targetCodeSize;
}

/**
* @return the machine code generated for this method
*/
public byte[] getNopCode() {
return nopCode;
}

/**
* @return the size of the machine code generated for this method
*/
public int getNopCodeSize() {
return nopCodeSize;
}

/**
* @return the code annotations or {@code null} if there are none
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ public static Format getNativeFormat() {
}
}

private static ObjectFile getNativeObjectFile(int pageSize, boolean runtimeDebugInfoGeneration) {
public static ObjectFile getNativeObjectFile(int pageSize, boolean runtimeDebugInfoGeneration) {
return switch (ObjectFile.getNativeFormat()) {
case ELF -> new ELFObjectFile(pageSize, runtimeDebugInfoGeneration);
case MACH_O -> new MachOObjectFile(pageSize);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
@SingletonTraits(access = BuildtimeAccessOnly.class, layeredCallbacks = NoLayeredCallbacks.class, other = Disallowed.class)
public final class AArch64ReservedRegisters extends ReservedRegisters {

public static final Register THREAD_REGISTER = AArch64.r28;
public static final Register THREAD_REGISTER = AArch64.r25;
public static final Register HEAP_BASE_REGISTER = AArch64.r27;
public static final Register CODE_BASE_REGISTER_CANDIDATE = AArch64.r26;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static com.oracle.svm.core.graal.aarch64.SubstrateAArch64RegisterConfig.fp;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_DECD_RSP;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_END;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_START;
import static com.oracle.svm.shared.util.VMError.unsupportedFeature;
import static jdk.graal.compiler.core.common.GraalOptions.ZapStackOnMethodEntry;
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
Expand All @@ -40,6 +41,7 @@
import java.util.Collection;
import java.util.function.BiConsumer;

import jdk.graal.compiler.asm.amd64.AMD64MacroAssembler;
import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.FrameAccess;
Expand Down Expand Up @@ -999,8 +1001,8 @@ public void enter(CompilationResultBuilder crb) {
final int totalFrameSize = frameMap.totalFrameSize();
assert frameSize + 2 * crb.target.arch.getWordSize() == totalFrameSize : "totalFramesize should be frameSize + 2 words";
AArch64MacroAssembler masm = (AArch64MacroAssembler) crb.asm;

crb.blockComment("[method prologue]");

makeFrame(crb, masm, totalFrameSize, frameSize);
crb.recordMark(PROLOGUE_DECD_RSP);

Expand Down Expand Up @@ -1581,19 +1583,40 @@ protected CompiledCode createCompiledCode(ResolvedJavaMethod method, Compilation
return new SubstrateCompiledCode(compilationResult);
}

private void wrapEmitLIR(CompilationResultBuilder crb) {
int nopsBeforeLabel = SubstrateOptions.nopsBeforeFunctionEntry();
AArch64MacroAssembler masm = (AArch64MacroAssembler) crb.asm;
if (nopsBeforeLabel > 0) {
int p0 = masm.position();
for (int i = 0; i < nopsBeforeLabel; i++) {
masm.nop();
}
int p1 = masm.position();
int nopSize = (p1 - p0) / nopsBeforeLabel;
byte[] nopBytes = masm.copy(p0, p0 + nopSize);
crb.compilationResult.setNopCode(nopBytes, nopSize);
}
crb.recordMark(PROLOGUE_START);
int nopsAfterLabel = SubstrateOptions.nopsAfterFunctionEntry();
for (int i = 0; i < nopsAfterLabel; i++) {
masm.nop();
}
crb.emitLIR();
}

@Override
public void emitCode(CompilationResultBuilder crb, ResolvedJavaMethod installedCodeOwner, EntryPointDecorator entryPointDecorator) {
try {
crb.buildLabelOffsets();
crb.emitLIR();
wrapEmitLIR(crb);
finalizeCode(crb);
} catch (BranchTargetOutOfBoundsException e) {
// A branch estimation was wrong, now retry with conservative label ranges, this
// should always work
resetForEmittingCode(crb);
crb.resetForEmittingCode();
crb.setConservativeLabelRanges();
crb.emitLIR();
wrapEmitLIR(crb);
finalizeCode(crb);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.impl.InternalPlatform;

import com.oracle.svm.core.c.libc.CosmoLibC;
import com.oracle.svm.core.c.libc.LibCBase;
import com.oracle.svm.core.ReservedRegisters;
import com.oracle.svm.core.aarch64.SubstrateAArch64MacroAssembler;
import com.oracle.svm.core.config.ObjectLayout;
Expand Down Expand Up @@ -163,9 +165,12 @@ public SubstrateAArch64RegisterConfig(ConfigKind config, MetaAccessProvider meta
*
* https://developer.android.com/ndk/guides/abis#arm64-v8a
*/
if (Platform.includedIn(Platform.DARWIN.class) || Platform.includedIn(InternalPlatform.WINDOWS_BASE.class) || Platform.includedIn(Platform.ANDROID.class)) {
if (LibCBase.targetLibCIs(CosmoLibC.class) || Platform.includedIn(Platform.DARWIN.class) || Platform.includedIn(InternalPlatform.WINDOWS_BASE.class) || Platform.includedIn(Platform.ANDROID.class)) {
regs.remove(r18);
}
if (LibCBase.targetLibCIs(CosmoLibC.class)) {
regs.remove(r28);
}
allocatableRegs = List.copyOf(regs);

switch (config) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_END;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_PUSH_RBP;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_SET_FRAME_POINTER;
import static com.oracle.svm.core.graal.code.SubstrateBackend.SubstrateMarkId.PROLOGUE_START;
import static com.oracle.svm.shared.util.VMError.unsupportedFeature;
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
import static jdk.graal.compiler.lir.LIRValueUtil.asConstantValue;
Expand Down Expand Up @@ -1307,7 +1308,6 @@ protected SubstrateAMD64FrameContext(SharedMethod method, CallingConvention call
@Override
public void enter(CompilationResultBuilder crb) {
AMD64MacroAssembler asm = (AMD64MacroAssembler) crb.asm;

makeFrame(crb, asm);
crb.recordMark(PROLOGUE_DECD_RSP);

Expand Down Expand Up @@ -1971,6 +1971,7 @@ public CompilationResultBuilder newCompilationResultBuilder(LIRGenerationResult
} else {
patchConsumerFactory = PatchConsumerFactory.NativePatchConsumerFactory.factory();
}

masm.setCodePatchingAnnotationConsumer(patchConsumerFactory.newConsumer(compilationResult));
SharedMethod method = ((SubstrateLIRGenerationResult) lirGenResult).getMethod();
Deoptimizer.StubType stubType = method.getDeoptStubType();
Expand Down Expand Up @@ -2025,9 +2026,30 @@ public CompiledCode createCompiledCode(ResolvedJavaMethod method, CompilationReq
return new SubstrateCompiledCode(compilationResult);
}

private void wrapEmitLIR(CompilationResultBuilder crb) {
int nopsBeforeLabel = SubstrateOptions.nopsBeforeFunctionEntry();
AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm;
if (nopsBeforeLabel > 0) {
int p0 = masm.position();
for (int i = 0; i < nopsBeforeLabel; i++) {
masm.nop();
}
int p1 = masm.position();
int nopSize = (p1 - p0) / nopsBeforeLabel;
byte[] nopBytes = masm.copy(p0, p0 + nopSize);
crb.compilationResult.setNopCode(nopBytes, nopSize);
}
crb.recordMark(PROLOGUE_START);
int nopsAfterLabel = SubstrateOptions.nopsAfterFunctionEntry();
for (int i = 0; i < nopsAfterLabel; i++) {
masm.nop();
}
crb.emitLIR();
}

@Override
public void emitCode(CompilationResultBuilder crb, ResolvedJavaMethod installedCodeOwner, EntryPointDecorator entryPointDecorator) {
crb.emitLIR();
wrapEmitLIR(crb);
if (GraalOptions.OptimizeLongJumps.getValue(crb.getOptions())) {
optimizeLongJumps(crb);
}
Expand Down Expand Up @@ -2080,7 +2102,7 @@ private void optimizeLongJumps(CompilationResultBuilder crb) {
// Triggers a reset of the assembler during which replaceable jumps are identified.
resetForEmittingCode(crb);
try {
crb.emitLIR();
wrapEmitLIR(crb);
} catch (BranchTargetOutOfBoundsException e) {
/*
* Alignments have invalidated the assumptions regarding short jumps. Trigger fail-safe
Expand All @@ -2089,7 +2111,7 @@ private void optimizeLongJumps(CompilationResultBuilder crb) {
AMD64MacroAssembler masm = (AMD64MacroAssembler) crb.asm;
masm.disableOptimizeLongJumpsAfterException();
crb.resetForEmittingCode();
crb.emitLIR();
wrapEmitLIR(crb);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@
import com.oracle.svm.guest.staging.Uninterruptible;
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.headers.LibMSupport;
import com.oracle.svm.core.posix.cosmo.NotCosmoLibCSupplier;
import com.oracle.svm.core.posix.headers.PosixLibM;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.AllAccess;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.SingleLayer;
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.InitialLayerOnly;
import com.oracle.svm.shared.singletons.traits.SingletonTraits;

@SingletonTraits(access = AllAccess.class, layeredCallbacks = SingleLayer.class, layeredInstallationKind = InitialLayerOnly.class)
@AutomaticallyRegisteredImageSingleton(LibMSupport.class)
@AutomaticallyRegisteredImageSingleton(value = LibMSupport.class, onlyWith = NotCosmoLibCSupplier.class)
public class PosixLibMSupport implements LibMSupport {
@Override
@Uninterruptible(reason = "Called from uninterruptible code.", mayBeInlined = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@
import com.oracle.svm.core.feature.AutomaticallyRegisteredImageSingleton;
import com.oracle.svm.core.handles.PrimitiveArrayView;
import com.oracle.svm.core.jdk.LoadAverageSupport;
import com.oracle.svm.core.posix.cosmo.NotCosmoLibCSupplier;
import com.oracle.svm.core.posix.headers.Stdlib;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.RuntimeAccessOnly;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.SingleLayer;
import com.oracle.svm.shared.singletons.traits.SingletonLayeredInstallationKind.InitialLayerOnly;
import com.oracle.svm.shared.singletons.traits.SingletonTraits;

@AutomaticallyRegisteredImageSingleton(LoadAverageSupport.class)
@AutomaticallyRegisteredImageSingleton(value = LoadAverageSupport.class, onlyWith = NotCosmoLibCSupplier.class)
@SingletonTraits(access = RuntimeAccessOnly.class, layeredCallbacks = SingleLayer.class, layeredInstallationKind = InitialLayerOnly.class)
class PosixLoadAverageSupport implements LoadAverageSupport {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package com.oracle.svm.core.posix;

import java.io.FileDescriptor;
import java.util.function.BooleanSupplier;

import org.graalvm.nativeimage.LogHandler;
import org.graalvm.nativeimage.c.type.CCharPointer;
Expand All @@ -36,6 +37,7 @@
import com.oracle.svm.core.headers.LibC;
import com.oracle.svm.core.imagelayer.ImageLayerBuildingSupport;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.posix.cosmo.CosmoLibCSupplier;
import com.oracle.svm.core.thread.VMThreads;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.BuildtimeAccessOnly;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.RuntimeAccessOnly;
Expand All @@ -53,6 +55,8 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {

@Override
public void beforeAnalysis(BeforeAnalysisAccess access) {
BooleanSupplier x = new CosmoLibCSupplier();
if(x.getAsBoolean()) return;
Log.finalizeDefaultLogHandler(new PosixLogHandler());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
package com.oracle.svm.core.posix;

import java.util.function.BooleanSupplier;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.nativeimage.Platforms;
Expand All @@ -45,6 +47,8 @@
import com.oracle.svm.core.jdk.NativeLibrarySupport;
import com.oracle.svm.core.jdk.PlatformNativeLibrarySupport;
import com.oracle.svm.core.log.Log;
import com.oracle.svm.core.posix.cosmo.CosmoLibCSupplier;
import com.oracle.svm.core.posix.cosmo.NotCosmoLibCSupplier;
import com.oracle.svm.core.posix.headers.Dlfcn;
import com.oracle.svm.core.posix.headers.Resource;
import com.oracle.svm.core.posix.headers.Time;
Expand All @@ -55,11 +59,15 @@
class PosixNativeLibraryFeature implements InternalFeature {
@Override
public void afterRegistration(AfterRegistrationAccess access) {
BooleanSupplier x = new CosmoLibCSupplier();
if(x.getAsBoolean()) return;
PosixNativeLibrarySupport.initialize();
}

@Override
public void duringSetup(DuringSetupAccess access) {
BooleanSupplier x = new CosmoLibCSupplier();
if(x.getAsBoolean()) return;
NativeLibrarySupport.singleton().preregisterUninitializedBuiltinLibrary("extnet");
}
}
Expand Down Expand Up @@ -241,7 +249,7 @@ public PointerBase findSymbol(String name) {
}
}

@TargetClass(className = "java.io.UnixFileSystem")
@TargetClass(className = "java.io.UnixFileSystem", onlyWith = NotCosmoLibCSupplier.class)
final class Target_java_io_UnixFileSystem_JNI {
@Alias
static native void initIDs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
package com.oracle.svm.core.posix;

import com.oracle.svm.core.posix.cosmo.NotCosmoLibCSupplier;
import org.graalvm.nativeimage.StackValue;

import com.oracle.svm.guest.staging.Uninterruptible;
Expand All @@ -36,7 +37,7 @@
import com.oracle.svm.shared.util.BasedOnJDKFile;
import com.oracle.svm.core.util.PlatformTimeUtils;

@AutomaticallyRegisteredImageSingleton(PlatformTimeUtils.class)
@AutomaticallyRegisteredImageSingleton(value = PlatformTimeUtils.class, onlyWith = NotCosmoLibCSupplier.class)
@SingletonTraits(access = RuntimeAccessOnly.class, layeredCallbacks = SingleLayer.class, layeredInstallationKind = InitialLayerOnly.class)
public final class PosixPlatformTimeUtils extends PlatformTimeUtils {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.File;
import java.nio.ByteOrder;
import java.util.function.BooleanSupplier;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
Expand All @@ -44,6 +45,7 @@
import com.oracle.svm.core.memory.UntrackedNullableNativeMemory;
import com.oracle.svm.core.os.AbstractRawFileOperationSupport;
import com.oracle.svm.core.os.AbstractRawFileOperationSupport.RawFileOperationSupportHolder;
import com.oracle.svm.core.posix.cosmo.CosmoLibCSupplier;
import com.oracle.svm.core.posix.headers.Fcntl;
import com.oracle.svm.core.posix.headers.Unistd;
import com.oracle.svm.shared.singletons.traits.BuiltinTraits.BuildtimeAccessOnly;
Expand Down Expand Up @@ -214,6 +216,8 @@ public boolean isInConfiguration(IsInConfigurationAccess access) {

@Override
public void afterRegistration(AfterRegistrationAccess access) {
BooleanSupplier x = new CosmoLibCSupplier();
if(x.getAsBoolean()) return;
ByteOrder nativeByteOrder = ByteOrder.nativeOrder();
assert nativeByteOrder == ByteOrder.LITTLE_ENDIAN || nativeByteOrder == ByteOrder.BIG_ENDIAN;

Expand Down
Loading