use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class Util_jni method socketRead0.
/* Do not re-format commeted-out code: @formatter:off
// 055 /*
// 056 * Class: java_net_SocketInputStream
// 057 * Method: socketRead0
// 058 * Signature: (Ljava/io/FileDescriptor;[BIII)I
// 059 */
// 060 JNIEXPORT jint JNICALL
// 061 Java_java_net_SocketInputStream_socketRead0(JNIEnv *env, jobject this,
// 062 jobject fdObj, jbyteArray data,
// 063 jint off, jint len, jint timeout)
@Substitute
@SuppressWarnings({ "static-method", "finally" })
private int socketRead0(FileDescriptor fdObj, byte[] data, int off, int lenArg, int timeout) throws IOException, OutOfMemoryError, sun.net.ConnectionResetException {
int len = lenArg;
// 065 char BUF[MAX_BUFFER_LEN];
CCharPointer BUF = StackValue.get(JavaNetNetUtilMD.MAX_BUFFER_LEN(), SizeOf.get(CCharPointer.class));
// 066 char *bufP;
CCharPointer bufP = WordFactory.nullPointer();
// 067 jint fd, nread;
int fd;
int nread;
// 069 if (IS_NULL(fdObj)) {
if (fdObj == null) {
// 073 return -1;
throw new SocketException("Socket closed");
} else {
// 075 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
fd = Util_java_io_FileDescriptor.getFD(fdObj);
// 079 if (fd == -1) {
if (fd == -1) {
// 081 return -1;
throw new SocketException("Socket closed");
}
}
// 089 if (len > MAX_BUFFER_LEN) {
if (len > JavaNetNetUtilMD.MAX_BUFFER_LEN()) {
// 090 if (len > MAX_HEAP_BUFFER_LEN) {
if (len > JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN()) {
// 091 len = MAX_HEAP_BUFFER_LEN;
len = JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN();
}
// 093 bufP = (char *)malloc((size_t)len);
bufP = LibC.malloc(WordFactory.unsigned(len));
// 094 if (bufP == NULL) {
if (bufP.isNull()) {
// 095 bufP = BUF;
bufP = BUF;
// 096 len = MAX_BUFFER_LEN;
len = JavaNetNetUtilMD.MAX_BUFFER_LEN();
}
} else {
// 099 bufP = BUF;
bufP = BUF;
}
// 102 if (timeout) {
if (CTypeConversion.toBoolean(timeout)) {
// 103 nread = NET_Timeout(fd, timeout);
nread = JavaNetNetUtilMD.NET_Timeout(fd, timeout);
// 104 if (nread <= 0) {
if (nread <= 0) {
try {
// 105 if (nread == 0) {
if (nread == 0) {
// 107 "Read timed out");
throw new SocketTimeoutException("Read timed out");
// 108 } else if (nread == JVM_IO_ERR) {
} else if (nread == Target_jvm.JVM_IO_ERR()) {
// 109 if (errno == EBADF) {
if (Errno.errno() == Errno.EBADF()) {
// 110 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
throw new SocketException("Socket closed");
// 111 } else if (errno == ENOMEM) {
} else if (Errno.errno() == Errno.ENOMEM()) {
// 112 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
throw new OutOfMemoryError("NET_Timeout native heap allocation failed");
} else {
// 115 "select/poll failed");
throw new SocketException("select/poll failed");
}
// 117 } else if (nread == JVM_IO_INTR) {
} else if (nread == Target_jvm.JVM_IO_INTR()) {
// 119 "Operation interrupted");
throw new InterruptedException("Operation interrupted");
}
} finally {
// 121 if (bufP != BUF) {
if (bufP.notEqual(BUF) && bufP.isNonNull()) {
// 122 free(bufP);
LibC.free(bufP);
bufP = WordFactory.nullPointer();
}
// 124 return -1;
return -1;
}
}
}
try {
// 128 nread = NET_Read(fd, bufP, len);
nread = JavaNetNetUtilMD.NET_Read(fd, bufP, len);
// 130 if (nread <= 0) {
if (nread <= 0) {
// 131 if (nread < 0) {
if (nread < 0) {
// 135 case EPIPE:
if ((Errno.errno() == Errno.ECONNRESET()) || (Errno.errno() == Errno.EPIPE())) {
// 138 break;
throw new sun.net.ConnectionResetException("Connection reset");
// 140 case EBADF:
} else if (Errno.errno() == Errno.EBADF()) {
// 143 break;
throw new SocketException("Socket closed");
// 145 case EINTR:
} else if (Errno.errno() == Errno.EINTR()) {
// 148 break;
throw new InterruptedIOException("Operation interrupted");
// 150 default:
} else {
// 152 JNU_JAVANETPKG "SocketException", "Read failed");
throw new SocketException("Read failed");
}
}
} else {
// 156 (*env)->SetByteArrayRegion(env, data, off, nread, (jbyte *)bufP);
VmPrimsJNI.SetByteArrayRegion(data, off, nread, bufP);
}
} finally {
// 159 if (bufP != BUF) {
if (bufP.notEqual(BUF) && bufP.isNonNull()) {
// 160 free(bufP);
LibC.free(bufP);
bufP = WordFactory.nullPointer();
}
}
return nread;
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class JavaUtilZipSubstitutions method inflateBytes.
// Inflater.c-Java_java_util_zip_Inflater_inflateBytes(JNIEnv *env, jobject this, jlong addr,
@SuppressWarnings("hiding")
@Substitute
private int inflateBytes(long addr, byte[] b, int off, int len) throws DataFormatException {
z_stream strm = WordFactory.pointer(addr);
try (PinnedObject pinned_in_buf = PinnedObject.create(this.buf);
PinnedObject pinned_out_buf = PinnedObject.create(b)) {
strm.set_next_in(pinned_in_buf.addressOfArrayElement(this.off));
strm.set_next_out(pinned_out_buf.addressOfArrayElement(off));
strm.set_avail_in(this.len);
strm.set_avail_out(len);
int ret = ZLib.inflate(strm, ZLib.Z_PARTIAL_FLUSH());
if (ret == ZLib.Z_STREAM_END()) {
this.finished = true;
return Util_java_util_zip_Inflater.update(this, len, strm);
} else if (ret == ZLib.Z_OK()) {
return Util_java_util_zip_Inflater.update(this, len, strm);
} else if (ret == ZLib.Z_NEED_DICT()) {
needDict = true;
Util_java_util_zip_Inflater.update(this, len, strm);
return 0;
} else if (ret == ZLib.Z_BUF_ERROR()) {
return 0;
} else if (ret == ZLib.Z_DATA_ERROR()) {
throw new DataFormatException(CTypeConversion.toJavaString(strm.msg()));
} else if (ret == ZLib.Z_MEM_ERROR()) {
throw new OutOfMemoryError();
} else {
throw new InternalError();
}
}
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class JavaUtilZipSubstitutions method updateByteBuffer.
// Adler32.c-Java_java_util_zip_Adler32_updateByteBuffer(JNIEnv *env, jclass cls, jint adler,
@Substitute
private static int updateByteBuffer(int adler, long addr, int off, int len) {
CCharPointer bytes = WordFactory.pointer(addr);
CCharPointer bytesAtOffset = bytes.addressOf(off);
return (int) ZLib.adler32(WordFactory.unsigned(adler), bytesAtOffset, len).rawValue();
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class VMThreadCounterOperation method getStackTrace.
@Substitute
@NeverInline("Immediate caller must show up in stack trace and so needs its own stack frame")
private StackTraceElement[] getStackTrace() {
if (JavaThreads.fromTarget(this) == Thread.currentThread()) {
/* We can walk our own stack without a VMOperation. */
StackTraceBuilder stackTraceBuilder = new StackTraceBuilder();
JavaStackWalker.walkCurrentThread(KnownIntrinsics.readCallerStackPointer(), KnownIntrinsics.readReturnAddress(), stackTraceBuilder);
return stackTraceBuilder.getTrace();
} else {
return JavaThreads.getStackTrace(JavaThreads.fromTarget(this));
}
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class VMThreadCounterOperation method start0.
@Substitute
private void start0() {
if (!SubstrateOptions.MultiThreaded.getValue()) {
throw VMError.unsupportedFeature("Single-threaded VM cannot create new threads");
}
/* Choose a stack size based on parameters, command line flags, and system restrictions. */
long chosenStackSize = 0L;
if (stackSize != 0) {
/* If the user set a thread stack size at thread creation, then use that. */
chosenStackSize = stackSize;
} else {
/* If the user set a thread stack size on the command line, then use that. */
final int defaultThreadStackSize = (int) XOptions.getXss().getValue();
if (defaultThreadStackSize != 0L) {
chosenStackSize = defaultThreadStackSize;
}
}
/*
* The threadStatus must be set to RUNNABLE by the parent thread and before the child thread
* starts because we are creating child threads asynchronously (there is no coordination
* between parent and child threads).
*
* Otherwise, a call to Thread.join() in the parent thread could succeed even before the
* child thread starts, or it could hang in case that the child thread is already dead.
*/
threadStatus = ThreadStatus.RUNNABLE;
JavaThreads.singleton().start0(JavaThreads.fromTarget(this), chosenStackSize);
}
Aggregations