Skip to content

Commit

Permalink
Cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhaeubl committed Jun 5, 2024
1 parent 1911b92 commit 5a86d64
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 175 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import static com.oracle.svm.core.annotate.RecomputeFieldValue.Kind.Custom;

import java.io.FileDescriptor;
import java.io.IOException;

import com.oracle.svm.core.util.BasedOnJDKFile;
import org.graalvm.nativeimage.StackValue;
import org.graalvm.nativeimage.c.function.CFunctionPointer;
import org.graalvm.nativeimage.c.struct.CPointerTo;
Expand All @@ -48,7 +46,7 @@
import com.oracle.svm.core.annotate.TargetClass;
import com.oracle.svm.core.c.function.CEntryPointActions;
import com.oracle.svm.core.graal.stackvalue.UnsafeStackValue;
import com.oracle.svm.core.handles.PrimitiveArrayView;
import com.oracle.svm.core.util.BasedOnJDKFile;
import com.oracle.svm.core.windows.headers.FileAPI;
import com.oracle.svm.core.windows.headers.LibLoaderAPI;
import com.oracle.svm.core.windows.headers.WinBase;
Expand Down Expand Up @@ -84,10 +82,6 @@ static void setHandle(FileDescriptor descriptor, long handle) {
SubstrateUtil.cast(descriptor, Target_java_io_FileDescriptor.class).handle = handle;
}

static boolean outOfBounds(int off, int len, byte[] array) {
return off < 0 || len < 0 || array.length - off < len;
}

/** Return the error string for the last error, or a default message. */
public static String lastErrorString(String defaultMsg) {
int error = WinBase.GetLastError();
Expand Down Expand Up @@ -133,38 +127,6 @@ static boolean flush(int handle) {
return (result != 0);
}

@SuppressWarnings("unused")
static void writeBytes(FileDescriptor descriptor, byte[] bytes, int off, int len, boolean append) throws IOException {
if (bytes == null) {
throw new NullPointerException();
} else if (WindowsUtils.outOfBounds(off, len, bytes)) {
throw new IndexOutOfBoundsException();
}
if (len == 0) {
return;
}

try (PrimitiveArrayView bytesPin = PrimitiveArrayView.createForReading(bytes)) {
CCharPointer curBuf = bytesPin.addressOfArrayElement(off);
UnsignedWord curLen = WordFactory.unsigned(len);
/** Temp fix until we complete FileDescriptor substitutions. */
int handle = FileAPI.GetStdHandle(FileAPI.STD_ERROR_HANDLE());

CIntPointer bytesWritten = UnsafeStackValue.get(CIntPointer.class);

int ret = FileAPI.WriteFile(handle, curBuf, curLen, bytesWritten, WordFactory.nullPointer());

if (ret == 0) {
throw new IOException(lastErrorString("Write error"));
}

int writtenCount = bytesWritten.read();
if (curLen.notEqual(writtenCount)) {
throw new IOException(lastErrorString("Write error"));
}
}
}

private static long performanceFrequency = 0L;
public static final long NANOSECS_PER_SEC = 1000000000L;
public static final int NANOSECS_PER_MILLISEC = 1000000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import com.oracle.svm.core.snippets.SnippetRuntime;
import com.oracle.svm.core.snippets.SnippetRuntime.SubstrateForeignCallDescriptor;
import com.oracle.svm.core.snippets.SubstrateForeignCallTarget;
import com.oracle.svm.core.util.ArrayUtil;

import jdk.graal.compiler.graph.Node;
import jdk.graal.compiler.graph.NodeClass;
Expand All @@ -50,7 +51,6 @@
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.graal.compiler.nodes.ValueNode;
import jdk.graal.compiler.nodes.extended.ForeignCallWithExceptionNode;
import jdk.graal.compiler.nodes.java.ArrayLengthNode;
import jdk.graal.compiler.nodes.spi.Lowerable;
import jdk.graal.compiler.nodes.spi.LoweringTool;
import jdk.graal.compiler.options.OptionValues;
Expand Down Expand Up @@ -88,25 +88,25 @@ private static void doArraycopy(Object fromArray, int fromIndex, Object toArray,

if (LayoutEncoding.isPrimitiveArray(fromLayoutEncoding)) {
if (fromArray == toArray && fromIndex < toIndex) {
boundsCheck(fromArray, fromIndex, toArray, toIndex, length);
ArrayUtil.boundsCheckInSnippet(fromArray, fromIndex, toArray, toIndex, length);
JavaMemoryUtil.copyPrimitiveArrayBackward(fromArray, fromIndex, fromArray, toIndex, length, fromLayoutEncoding);
return;
} else if (fromHub == toHub) {
boundsCheck(fromArray, fromIndex, toArray, toIndex, length);
ArrayUtil.boundsCheckInSnippet(fromArray, fromIndex, toArray, toIndex, length);
JavaMemoryUtil.copyPrimitiveArrayForward(fromArray, fromIndex, toArray, toIndex, length, fromLayoutEncoding);
return;
}
} else if (LayoutEncoding.isObjectArray(fromLayoutEncoding)) {
if (fromArray == toArray && fromIndex < toIndex) {
boundsCheck(fromArray, fromIndex, toArray, toIndex, length);
ArrayUtil.boundsCheckInSnippet(fromArray, fromIndex, toArray, toIndex, length);
JavaMemoryUtil.copyObjectArrayBackward(fromArray, fromIndex, fromArray, toIndex, length, fromLayoutEncoding);
return;
} else if (fromHub == toHub) {
boundsCheck(fromArray, fromIndex, toArray, toIndex, length);
ArrayUtil.boundsCheckInSnippet(fromArray, fromIndex, toArray, toIndex, length);
JavaMemoryUtil.copyObjectArrayForward(fromArray, fromIndex, toArray, toIndex, length, fromLayoutEncoding);
return;
} else if (LayoutEncoding.isObjectArray(toHub.getLayoutEncoding())) {
boundsCheck(fromArray, fromIndex, toArray, toIndex, length);
ArrayUtil.boundsCheckInSnippet(fromArray, fromIndex, toArray, toIndex, length);
if (DynamicHub.toClass(toHub).isAssignableFrom(DynamicHub.toClass(fromHub))) {
JavaMemoryUtil.copyObjectArrayForward(fromArray, fromIndex, toArray, toIndex, length, fromLayoutEncoding);
} else {
Expand All @@ -118,12 +118,6 @@ private static void doArraycopy(Object fromArray, int fromIndex, Object toArray,
throw new ArrayStoreException();
}

private static void boundsCheck(Object fromArray, int fromIndex, Object toArray, int toIndex, int length) {
if (fromIndex < 0 || toIndex < 0 || length < 0 || fromIndex > ArrayLengthNode.arrayLength(fromArray) - length || toIndex > ArrayLengthNode.arrayLength(toArray) - length) {
throw new ArrayIndexOutOfBoundsException();
}
}

static final class SubstrateArrayCopyLowering implements NodeLoweringProvider<ArrayCopyNode> {
@Override
public void lower(ArrayCopyNode node, LoweringTool tool) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,37 +24,17 @@
*/
package com.oracle.svm.core.jni;

import java.lang.reflect.Array;

import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.c.type.CCharPointer;
import org.graalvm.word.PointerBase;
import org.graalvm.word.UnsignedWord;
import org.graalvm.word.WordFactory;

import com.oracle.svm.core.JavaMemoryUtil;
import com.oracle.svm.core.Uninterruptible;
import com.oracle.svm.core.config.ConfigurationValues;
import com.oracle.svm.core.handles.PrimitiveArrayView;
import com.oracle.svm.core.jni.access.JNIAccessibleField;
import com.oracle.svm.core.jni.access.JNINativeLinkage;
import com.oracle.svm.core.jni.headers.JNIEnvironment;
import com.oracle.svm.core.jni.headers.JNIFieldId;
import com.oracle.svm.core.jni.headers.JNIObjectHandle;

import jdk.graal.compiler.api.replacements.Fold;
import jdk.internal.misc.Unsafe;
import jdk.vm.ci.meta.JavaKind;

/**
* Helper code that is used in generated JNI code via {@code JNIGraphKit}.
*/
public class JNIGeneratedMethodSupport {
@Fold
public static JNIGeneratedMethodSupport singleton() {
return ImageSingletons.lookup(JNIGeneratedMethodSupport.class);
}

public final class JNIGeneratedMethodSupport {
// Careful around here -- these methods are invoked by generated methods.

static PointerBase nativeCallAddress(JNINativeLinkage linkage) {
Expand Down Expand Up @@ -103,60 +83,4 @@ static void rethrowPendingException() throws Throwable {
throw t;
}
}

public JNIObjectHandle getObjectField(JNIObjectHandle obj, JNIFieldId fieldId) {
Object o = JNIObjectHandles.getObject(obj);
long offset = JNIAccessibleField.getOffsetFromId(fieldId).rawValue();
Object result = getObjectField0(o, offset);
return JNIObjectHandles.createLocal(result);
}

protected Object getObjectField0(Object obj, long offset) {
return Unsafe.getUnsafe().getReference(obj, offset);
}

public PointerBase createArrayViewAndGetAddress(JNIObjectHandle handle, CCharPointer isCopy) {
Object obj = JNIObjectHandles.getObject(handle);
if (!obj.getClass().isArray()) {
throw new IllegalArgumentException("Argument is not an array");
}

/* Create a view for the non-null array object. */
PrimitiveArrayView ref = JNIThreadLocalPrimitiveArrayViews.createArrayView(obj);
if (isCopy.isNonNull()) {
isCopy.write(ref.isCopy() ? (byte) 1 : (byte) 0);
}
return ref.addressOfArrayElement(0);
}

public void destroyNewestArrayViewByAddress(PointerBase address, int mode) {
JNIThreadLocalPrimitiveArrayViews.destroyNewestArrayViewByAddress(address, mode);
}

public void getPrimitiveArrayRegion(JavaKind elementKind, JNIObjectHandle handle, int start, int count, PointerBase buffer) {
Object obj = JNIObjectHandles.getObject(handle);
/* Check if we have a non-null array object and if start/count are valid. */
if (start < 0 || count < 0 || start > Array.getLength(obj) - count) {
throw new ArrayIndexOutOfBoundsException();
}
if (count > 0) {
long offset = ConfigurationValues.getObjectLayout().getArrayElementOffset(elementKind, start);
int elementSize = ConfigurationValues.getObjectLayout().sizeInBytes(elementKind);
UnsignedWord bytes = WordFactory.unsigned(count).multiply(elementSize);
JavaMemoryUtil.copyOnHeap(obj, WordFactory.unsigned(offset), null, WordFactory.unsigned(buffer.rawValue()), bytes);
}
}

public void setPrimitiveArrayRegion(JavaKind elementKind, JNIObjectHandle handle, int start, int count, PointerBase buffer) {
Object obj = JNIObjectHandles.getObject(handle);
if (start < 0 || count < 0 || start > Array.getLength(obj) - count) {
throw new ArrayIndexOutOfBoundsException();
}
if (count > 0) {
long offset = ConfigurationValues.getObjectLayout().getArrayElementOffset(elementKind, start);
int elementSize = ConfigurationValues.getObjectLayout().sizeInBytes(elementKind);
UnsignedWord bytes = WordFactory.unsigned(count).multiply(elementSize);
JavaMemoryUtil.copyOnHeap(null, WordFactory.unsigned(buffer.rawValue()), obj, WordFactory.unsigned(offset), bytes);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright (c) 2017, 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.jni;

import org.graalvm.nativeimage.ImageSingletons;

import com.oracle.svm.core.jni.access.JNIAccessibleField;
import com.oracle.svm.core.jni.headers.JNIFieldId;
import com.oracle.svm.core.jni.headers.JNIObjectHandle;

import jdk.graal.compiler.api.replacements.Fold;
import jdk.internal.misc.Unsafe;

public class JNIObjectFieldAccess {
@Fold
public static JNIObjectFieldAccess singleton() {
return ImageSingletons.lookup(JNIObjectFieldAccess.class);
}

public JNIObjectHandle getObjectField(JNIObjectHandle obj, JNIFieldId fieldId) {
Object o = JNIObjectHandles.getObject(obj);
long offset = JNIAccessibleField.getOffsetFromId(fieldId).rawValue();
Object result = getObjectField0(o, offset);
return JNIObjectHandles.createLocal(result);
}

protected Object getObjectField0(Object obj, long offset) {
return Unsafe.getUnsafe().getReference(obj, offset);
}
}
Loading

0 comments on commit 5a86d64

Please sign in to comment.