use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class PosixJavaLangSubstitutions method initStreams.
@Substitute
void initStreams(int[] fds) {
Object in = Target_java_lang_ProcessBuilder_NullOutputStream.INSTANCE;
if (fds[0] != -1) {
in = new Target_java_lang_UNIXProcess_ProcessPipeOutputStream(fds[0]);
}
stdin = KnownIntrinsics.unsafeCast(in, OutputStream.class);
Object out = Target_java_lang_ProcessBuilder_NullInputStream.INSTANCE;
if (fds[1] != -1) {
out = new Target_java_lang_UNIXProcess_ProcessPipeInputStream(fds[1]);
}
stdout = KnownIntrinsics.unsafeCast(out, InputStream.class);
Object err = Target_java_lang_ProcessBuilder_NullInputStream.INSTANCE;
if (fds[2] != -1) {
err = new Target_java_lang_UNIXProcess_ProcessPipeInputStream(fds[2]);
}
stderr = KnownIntrinsics.unsafeCast(err, InputStream.class);
Thread reaperThread = Java_lang_UNIXProcess_Supplement.reaperFactory.newThread(new Runnable() {
@Override
public void run() {
int status = waitForProcessExit(pid);
// We need to use synchronized to synchronize with non-substituted UNIXProcess code
synchronized (Target_java_lang_UNIXProcess.this) {
// Checkstyle: resume
Target_java_lang_UNIXProcess.this.exitcode = status;
Target_java_lang_UNIXProcess.this.hasExited = true;
Target_java_lang_UNIXProcess.this.notifyAll();
}
if ((Object) stdout != Target_java_lang_ProcessBuilder_NullInputStream.INSTANCE) {
KnownIntrinsics.unsafeCast(stdout, Target_java_lang_UNIXProcess_ProcessPipeInputStream.class).processExited();
}
if ((Object) stderr != Target_java_lang_ProcessBuilder_NullInputStream.INSTANCE) {
KnownIntrinsics.unsafeCast(stderr, Target_java_lang_UNIXProcess_ProcessPipeInputStream.class).processExited();
}
if ((Object) stdin != Target_java_lang_ProcessBuilder_NullOutputStream.INSTANCE) {
KnownIntrinsics.unsafeCast(stdin, Target_java_lang_UNIXProcess_ProcessPipeOutputStream.class).processExited();
}
}
});
reaperThread.start();
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class PosixJavaLangSubstitutions method environ.
// This code is derived from the C implementation of the JDK.
@Substitute
static byte[][] environ() {
CCharPointerPointer environ = LibCHelper.getEnviron();
int count = 0;
for (int i = 0; environ.read(i).isNonNull(); i++) {
/* Ignore corrupted environment variables */
if (LibC.strchr(environ.read(i), '=').isNonNull()) {
count++;
}
}
byte[][] result = new byte[count * 2][];
int j = 0;
for (int i = 0; environ.read(i).isNonNull(); i++) {
CCharPointer varBeg = environ.read(i);
CCharPointer varEnd = LibC.strchr(varBeg, '=');
/* Ignore corrupted environment variables */
if (varEnd.isNonNull()) {
CCharPointer valBeg = varEnd.addressOf(1);
int varLength = (int) PointerUtils.absoluteDifference(varEnd, varBeg).rawValue();
int valLength = (int) LibC.strlen(valBeg).rawValue();
byte[] var = new byte[varLength];
SubstrateUtil.wrapAsByteBuffer(varBeg, varLength).get(var);
result[2 * j] = var;
byte[] val = new byte[valLength];
SubstrateUtil.wrapAsByteBuffer(valBeg, valLength).get(val);
result[2 * j + 1] = val;
j++;
}
}
assert j == count;
return result;
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class Util_jni method socketWrite0.
/* Do not re-format commented-out code: @formatter:off */
// 056 /*
// 057 * Class: java_net_SocketOutputStream
// 058 * Method: socketWrite0
// 059 * Signature: (Ljava/io/FileDescriptor;[BII)V
// 060 */
// 061 JNIEXPORT void JNICALL
// 062 Java_java_net_SocketOutputStream_socketWrite0(JNIEnv *env, jobject this,
// 063 jobject fdObj,
// 064 jbyteArray data,
// 065 jint off, jint len) {
@Substitute
@SuppressWarnings({ "static-method", "finally" })
private void socketWrite0(FileDescriptor fdObj, byte[] data, int offArg, int lenArg) throws IOException {
/* Local variable copies rather than assign to formal parameter. */
int off = offArg;
int len = lenArg;
// 066 char *bufP;
CCharPointer bufP = WordFactory.nullPointer();
// 067 char BUF[MAX_BUFFER_LEN];
CCharPointer BUF = StackValue.get(JavaNetNetUtilMD.MAX_BUFFER_LEN(), SizeOf.get(CCharPointer.class));
// 068 int buflen;
int buflen;
// 069 int fd;
int fd;
// 071 if (IS_NULL(fdObj)) {
if (fdObj == null) {
// 073 return;
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;
throw new SocketException("Socket closed");
}
}
// 086 if (len <= MAX_BUFFER_LEN) {
if (len <= JavaNetNetUtilMD.MAX_BUFFER_LEN()) {
// 087 bufP = BUF;
bufP = BUF;
// 088 buflen = MAX_BUFFER_LEN;
buflen = JavaNetNetUtilMD.MAX_BUFFER_LEN();
} else {
// 090 buflen = min(MAX_HEAP_BUFFER_LEN, len);
buflen = Integer.min(JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN(), len);
// 091 bufP = (char *)malloc((size_t)buflen);
bufP = LibC.malloc(WordFactory.unsigned(buflen));
// 094 if (bufP == NULL) {
if (bufP.isNull()) {
// 095 bufP = BUF;
bufP = BUF;
// 096 buflen = MAX_BUFFER_LEN;
buflen = JavaNetNetUtilMD.MAX_HEAP_BUFFER_LEN();
}
}
try {
// 100 while(len > 0) {
while (len > 0) {
// 101 int loff = 0;
int loff = 0;
// 102 int chunkLen = min(buflen, len);
int chunkLen = Integer.min(buflen, len);
// 103 int llen = chunkLen;
int llen = chunkLen;
// 104 (*env)->GetByteArrayRegion(env, data, off, chunkLen, (jbyte *)bufP);
VmPrimsJNI.GetByteArrayRegion(data, off, chunkLen, bufP);
// 106 while(llen > 0) {
while (llen > 0) {
try {
// 107 int n = NET_Send(fd, bufP + loff, llen, 0);
int n = JavaNetNetUtilMD.NET_Send(fd, bufP.addressOf(loff), llen, 0);
// 108 if (n > 0) {
if (n > 0) {
// 109 llen -= n;
llen -= n;
// 110 loff += n;
loff += n;
// 111 continue;
continue;
}
// 113 if (n == JVM_IO_INTR) {
if (n == Target_jvm.JVM_IO_INTR()) {
// 114 JNU_ThrowByName(env, "java/io/InterruptedIOException", 0);
throw new InterruptedIOException();
} else {
// 116 if (errno == ECONNRESET) {
if (Errno.errno() == Errno.ECONNRESET()) {
// 118 "Connection reset");
throw new sun.net.ConnectionResetException("Connection reset");
} else {
// 121 "Write failed");
throw new SocketException("Write failed");
}
}
} finally {
// 124 if (bufP != BUF) {
if (bufP.notEqual(BUF) && bufP.isNonNull()) {
// 125 free(bufP);
LibC.free(bufP);
bufP = WordFactory.nullPointer();
}
// 127 return;
return;
}
}
// 129 len -= chunkLen;
len -= chunkLen;
// 130 off += chunkLen;
off += chunkLen;
}
} finally {
// 133 if (bufP != BUF) {
if (bufP.notEqual(BUF) && bufP.isNonNull()) {
// 134 free(bufP);
LibC.free(bufP);
bufP = WordFactory.nullPointer();
}
}
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class Util_jni method socketAccept.
/* @formatter:on */
/* Do not re-format commented-out code: @formatter:off */
// 644 /*
// 645 * Class: java_net_PlainSocketImpl
// 646 * Method: socketAccept
// 647 * Signature: (Ljava/net/SocketImpl;)V
// 648 */
// 649 JNIEXPORT void JNICALL
// 650 Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
// 651 jobject socket) {
@Substitute
void socketAccept(SocketImpl socket) throws IOException {
// 653 /* fields on this */
// 654 int port;
CIntPointer port_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
// 655 jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
int timeout = Util_java_net_PlainSocketImpl.as_Target_java_net_AbstractPlainSocketImpl(this).timeout;
// 656 jlong prevTime = 0;
long prevTime = 0;
// 657 jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
FileDescriptor fdObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd;
// 658
// 659 /* the FileDescriptor field on socket */
// 660 jobject socketFdObj;
FileDescriptor socketFdObj;
// 661 /* the InetAddress field on socket */
// 662 jobject socketAddressObj;
InetAddress socketAddressObj;
// 663
// 664 /* the ServerSocket fd int field on fdObj */
// 665 jint fd;
int fd;
// 666
// 667 /* accepted fd */
// 668 jint newfd;
/* Initialized for the use in the finally-clause. */
int newfd = -1;
// 669
// 670 SOCKADDR him;
Socket.sockaddr him = StackValue.get(JavaNetNetUtilMD.SOCKADDR_LEN());
// 671 int len;
CIntPointer len_Pointer = StackValue.get(SizeOf.get(CIntPointer.class));
// 672
// 673 len = SOCKADDR_LEN;
len_Pointer.write(JavaNetNetUtilMD.SOCKADDR_LEN());
// 675 if (IS_NULL(fdObj)) {
if (fdObj == null) {
// 678 return;
throw new SocketException("Socket closed");
} else {
// 680 fd = (*env)->GetIntField(env, fdObj, IO_fd_fdID);
fd = Util_java_io_FileDescriptor.getFD(fdObj);
}
// 682 if (IS_NULL(socket)) {
if (socket == null) {
// 684 return;
throw new NullPointerException("socket is null");
}
try {
// 696 for (;;) {
for (; ; ) {
// 697 int ret;
int ret;
// 700 if (prevTime == 0 && timeout > 0) {
if (prevTime == 0 && timeout > 0) {
// 701 prevTime = JVM_CurrentTimeMillis(env, 0);
prevTime = System.currentTimeMillis();
}
// 706 if (timeout <= 0) {
if (timeout <= 0) {
// 707 ret = NET_Timeout(fd, -1);
ret = JavaNetNetUtilMD.NET_Timeout(fd, -1);
} else {
// 709 ret = NET_Timeout(fd, timeout);
ret = JavaNetNetUtilMD.NET_Timeout(fd, timeout);
}
// 711 if (ret == 0) {
if (ret == 0) {
// 714 return;
throw new SocketTimeoutException("Accept timed out");
// 715 } else if (ret == JVM_IO_ERR) {
} else if (ret == Target_jvm.JVM_IO_ERR()) {
// 716 if (errno == EBADF) {
if (Errno.errno() == Errno.EBADF()) {
// 717 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
throw new SocketException("Socket closed");
// 718 } else if (errno == ENOMEM) {
} else if (Errno.errno() == Errno.ENOMEM()) {
// 719 JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
throw new OutOfMemoryError("NET_Timeout native heap allocation failed");
} else {
// 721 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
throw new SocketException("Accept failed");
}
// 723 return;
// 724 } else if (ret == JVM_IO_INTR) {
} else if (ret == Target_jvm.JVM_IO_INTR()) {
// 727 return;
throw new InterruptedIOException("operation interrupted");
}
// 729
// 730 newfd = NET_Accept(fd, (struct sockaddr *)&him, (jint*)&len);
newfd = JavaNetNetUtilMD.NET_Accept(fd, him, len_Pointer);
// 733 if (newfd >= 0) {
if (newfd >= 0) {
// 734 SET_BLOCKING(newfd);
Util_java_net_PlainSocketImpl.SET_BLOCKING(newfd);
// 735 break;
break;
}
// 739 if (!(errno == ECONNABORTED || errno == EWOULDBLOCK)) {
if (!(Errno.errno() == Errno.ECONNABORTED() || Errno.errno() == Errno.EWOULDBLOCK())) {
// 740 break;
break;
}
// 744 if (timeout) {
if (CTypeConversion.toBoolean(timeout)) {
// 745 jlong currTime = JVM_CurrentTimeMillis(env, 0);
long currTime = System.currentTimeMillis();
// 746 timeout -= (currTime - prevTime);
timeout -= (currTime - prevTime);
// 748 if (timeout <= 0) {
if (timeout <= 0) {
// 751 return;
throw new SocketTimeoutException("Accept timed out");
}
// 753 prevTime = currTime;
prevTime = currTime;
}
}
// 757 if (newfd < 0) {
if (newfd < 0) {
// 758 if (newfd == -2) {
if (newfd == -2) {
// 760 "operation interrupted");
throw new InterruptedIOException("operation interrupted");
} else {
// 762 if (errno == EINVAL) {
if (Errno.errno() == Errno.EINVAL()) {
// 763 errno = EBADF;
Errno.set_errno(Errno.EBADF());
}
// 765 if (errno == EBADF) {
if (Errno.errno() == Errno.EBADF()) {
// 766 JNU_ThrowByName(env, JNU_JAVANETPKG "SocketException", "Socket closed");
throw new SocketException("Socket closed");
} else {
// 768 NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "SocketException", "Accept failed");
throw new SocketException("Accept failed");
}
}
// 771 return;
}
} finally {
// 773
// 774 /*
// 775 * fill up the remote peer port and address in the new socket structure.
// 776 */
// 777 socketAddressObj = NET_SockaddrToInetAddress(env, (struct sockaddr *)&him, &port);
socketAddressObj = JavaNetNetUtil.NET_SockaddrToInetAddress(him, port_Pointer);
// 778 if (socketAddressObj == NULL) {
if (socketAddressObj == null) {
// 779 /* should be pending exception */
// 780 close(newfd);
Unistd.close(newfd);
// 781 return;
}
}
// 783
// 784 /*
// 785 * Populate SocketImpl.fd.fd
// 786 */
// 787 socketFdObj = (*env)->GetObjectField(env, socket, psi_fdID);
socketFdObj = Util_java_net_SocketImpl.from_SocketImpl(socket).fd;
// 788 (*env)->SetIntField(env, socketFdObj, IO_fd_fdID, newfd);
Util_java_io_FileDescriptor.setFD(socketFdObj, newfd);
// 789
// 790 (*env)->SetObjectField(env, socket, psi_addressID, socketAddressObj);
Util_java_net_SocketImpl.from_SocketImpl(socket).address = socketAddressObj;
// 791 (*env)->SetIntField(env, socket, psi_portID, port);
Util_java_net_SocketImpl.from_SocketImpl(socket).port = port_Pointer.read();
/* Not re-using the frame-local "port". */
// 792 /* also fill up the local port information */
// 793 port = (*env)->GetIntField(env, this, psi_localportID);
int thisLocalPort = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).localport;
// 794 (*env)->SetIntField(env, socket, psi_localportID, port);
Util_java_net_SocketImpl.from_SocketImpl(socket).localport = thisLocalPort;
}
use of com.oracle.svm.core.annotate.Substitute in project graal by oracle.
the class Util_jni method socketCreate.
/* Substitutions for native methods. */
/* Do not re-format commented-out code: @formatter:off */
@SuppressWarnings("finally")
@Substitute
// 182 jboolean stream) {
void socketCreate(boolean stream) throws IOException, InterruptedException {
// 183 jobject fdObj, ssObj;
FileDescriptor fdObj;
ServerSocket ssObj;
// 184 int fd;
int fd;
// 185 int type = (stream ? SOCK_STREAM : SOCK_DGRAM);
int type = (stream ? Socket.SOCK_STREAM() : Socket.SOCK_DGRAM());
// 186 #ifdef AF_INET6
int domain;
if (IsDefined.socket_AF_INET6()) {
// 187 int domain = ipv6_available() ? AF_INET6 : AF_INET;
domain = JavaNetNetUtil.ipv6_available() ? Socket.AF_INET6() : Socket.AF_INET();
} else {
// 189 int domain = AF_INET;
domain = Socket.AF_INET();
}
// 190 #endif
// 191
// 192 if (socketExceptionCls == NULL) {
// 193 jclass c = (*env)->FindClass(env, "java/net/SocketException");
// 194 CHECK_NULL(c);
// 195 socketExceptionCls = (jclass)(*env)->NewGlobalRef(env, c);
// 196 CHECK_NULL(socketExceptionCls);
// 197 }
// 198 fdObj = (*env)->GetObjectField(env, this, psi_fdID);
fdObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).fd;
// 203 }
if (fdObj == null) {
throw new SocketException("null fd object");
}
// 205 if ((fd = JVM_Socket(domain, type, 0)) == JVM_IO_ERR) {
if ((fd = Socket.socket(domain, type, 0)) == Target_jvm.JVM_IO_ERR()) {
// 206 /* note: if you run out of fds, you may not be able to load
// 207 * the exception class, and get a NoClassDefFoundError
// 208 * instead.
// 209 */
// 210 NET_ThrowNew(env, errno, "can't create socket");
JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "can't create socket");
// 211 return;
return;
}
// 214 #ifdef AF_INET6
if (IsDefined.socket_AF_INET6()) {
// 216 if (domain == AF_INET6) {
if (domain == Socket.AF_INET6()) {
// 217 int arg = 0;
CIntPointer argPointer = StackValue.get(SizeOf.get(CIntPointer.class));
argPointer.write(0);
// 219 sizeof(int)) < 0) {
if (Socket.setsockopt(fd, NetinetIn.IPPROTO_IPV6(), NetinetIn.IPV6_V6ONLY(), argPointer, SizeOf.get(CIntPointer.class)) < 0) {
try {
// 220 NET_ThrowNew(env, errno, "cannot set IPPROTO_IPV6");
JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "cannot set IPPROTO_IPV6");
} finally {
// 221 close(fd);
Unistd.close(fd);
// 222 return;
return;
}
}
}
}
// 225 #endif /* AF_INET6 */
// 226
// 227 /*
// 228 * If this is a server socket then enable SO_REUSEADDR
// 229 * automatically and set to non blocking.
// 230 */
// 231 ssObj = (*env)->GetObjectField(env, this, psi_serverSocketID);
ssObj = Util_java_net_PlainSocketImpl.as_Target_java_net_SocketImpl(this).serverSocket;
// 232 if (ssObj != NULL) {
if (ssObj != null) {
// 233 int arg = 1;
CIntPointer argPointer = StackValue.get(SizeOf.get(CIntPointer.class));
argPointer.write(1);
// 234 SET_NONBLOCKING(fd);
Util_java_net_PlainSocketImpl.SET_NONBLOCKING(fd);
// 236 sizeof(arg)) < 0) {
if (Socket.setsockopt(fd, Socket.SOL_SOCKET(), Socket.SO_REUSEADDR(), argPointer, SizeOf.get(CIntPointer.class)) < 0) {
try {
// 237 NET_ThrowNew(env, errno, "cannot set SO_REUSEADDR");
JavaNetNetUtilMD.NET_ThrowNew(Errno.errno(), "cannot set SO_REUSEADDR");
} finally {
// 238 close(fd);
Unistd.close(fd);
// 239 return;
}
}
}
// 243 (*env)->SetIntField(env, fdObj, IO_fd_fdID, fd);
PosixOSInterface.Util_java_io_FileDescriptor.setFD(fdObj, fd);
/* @formatter:on */
}
Aggregations