Blame SOURCES/8075484-pr3473-rh1490713.patch

045ef6
# HG changeset patch
045ef6
# User vtewari
045ef6
# Date 1508189111 -3600
045ef6
#      Mon Oct 16 22:25:11 2017 +0100
045ef6
# Node ID bcaa659478ccac2b2ad1a817e03cab777949775a
045ef6
# Parent  161fbe4c53ff12328565487e69a608e15f39bd49
045ef6
8075484, PR3473, RH1490713: SocketInputStream.socketRead0 can hang even with soTimeout set
045ef6
Reviewed-by: chegar, dsamersoff, msheppar, clanger
045ef6
045ef6
diff --git a/src/aix/native/java/net/aix_close.c b/src/aix/native/java/net/aix_close.c
045ef6
--- openjdk/jdk/src/aix/native/java/net/aix_close.c
045ef6
+++ openjdk/jdk/src/aix/native/java/net/aix_close.c
045ef6
@@ -1,5 +1,6 @@
045ef6
 /*
045ef6
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
045ef6
+ * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
045ef6
+ * Copyright (c) 2016, SAP SE and/or its affiliates. All rights reserved.
045ef6
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
045ef6
  *
045ef6
  * This code is free software; you can redistribute it and/or modify it
045ef6
@@ -328,6 +329,10 @@
045ef6
     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
045ef6
 }
045ef6
 
045ef6
+int NET_NonBlockingRead(int s, void* buf, size_t len) {
045ef6
+    BLOCKING_IO_RETURN_INT(s, recv(s, buf, len, MSG_NONBLOCK));
045ef6
+}
045ef6
+
045ef6
 int NET_ReadV(int s, const struct iovec * vector, int count) {
045ef6
     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
045ef6
 }
045ef6
@@ -429,8 +434,8 @@
045ef6
  * Auto restarts with adjusted timeout if interrupted by
045ef6
  * signal other than our wakeup signal.
045ef6
  */
045ef6
-int NET_Timeout(int s, long timeout) {
045ef6
-    long prevtime = 0, newtime;
045ef6
+int NET_Timeout0(int s, long timeout, long currentTime) {
045ef6
+    long prevtime = currentTime, newtime;
045ef6
     struct timeval t;
045ef6
     fdEntry_t *fdEntry = getFdEntry(s);
045ef6
 
045ef6
@@ -442,14 +447,6 @@
045ef6
         return -1;
045ef6
     }
045ef6
 
045ef6
-    /*
045ef6
-     * Pick up current time as may need to adjust timeout
045ef6
-     */
045ef6
-    if (timeout > 0) {
045ef6
-        gettimeofday(&t, NULL);
045ef6
-        prevtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
045ef6
-    }
045ef6
-
045ef6
     for(;;) {
045ef6
         struct pollfd pfd;
045ef6
         int rv;
045ef6
diff --git a/src/solaris/native/java/net/SocketInputStream.c b/src/solaris/native/java/net/SocketInputStream.c
045ef6
--- openjdk/jdk/src/solaris/native/java/net/SocketInputStream.c
045ef6
+++ openjdk/jdk/src/solaris/native/java/net/SocketInputStream.c
045ef6
@@ -1,5 +1,5 @@
045ef6
 /*
045ef6
- * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
045ef6
+ * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
045ef6
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
045ef6
  *
045ef6
  * This code is free software; you can redistribute it and/or modify it
045ef6
@@ -52,6 +52,42 @@
045ef6
     IO_fd_fdID = NET_GetFileDescriptorID(env);
045ef6
 }
045ef6
 
045ef6
+#if !defined(__solaris__)
045ef6
+static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
045ef6
+    int result = 0;
045ef6
+    long prevtime = NET_GetCurrentTime(), newtime;
045ef6
+    while (timeout > 0) {
045ef6
+        result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
045ef6
+        if (result <= 0) {
045ef6
+            if (result == 0) {
045ef6
+                JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");
045ef6
+            } else if (result == -1) {
045ef6
+                if (errno == EBADF) {
045ef6
+                    JNU_ThrowByName(env, "java/net/SocketException", "Socket closed");
045ef6
+                } else if (errno == ENOMEM) {
045ef6
+                    JNU_ThrowOutOfMemoryError(env, "NET_Timeout native heap allocation failed");
045ef6
+                } else {
045ef6
+                    JNU_ThrowByNameWithMessageAndLastError
045ef6
+                            (env, "java/net/SocketException", "select/poll failed");
045ef6
+                }
045ef6
+            }
045ef6
+            return -1;
045ef6
+        }
045ef6
+        result = NET_NonBlockingRead(fd, bufP, len);
045ef6
+        if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
045ef6
+            newtime = NET_GetCurrentTime();
045ef6
+            timeout -= newtime - prevtime;
045ef6
+            if (timeout > 0) {
045ef6
+                prevtime = newtime;
045ef6
+            }
045ef6
+        } else {
045ef6
+            break;
045ef6
+        }
045ef6
+    }
045ef6
+    return result;
045ef6
+}
045ef6
+#endif
045ef6
+
045ef6
 /*
045ef6
  * Class:     java_net_SocketInputStream
045ef6
  * Method:    socketRead0
045ef6
@@ -99,6 +135,7 @@
045ef6
         bufP = BUF;
045ef6
     }
045ef6
 
045ef6
+#if defined(__solaris__)
045ef6
     if (timeout) {
045ef6
         nread = NET_Timeout(fd, timeout);
045ef6
         if (nread <= 0) {
045ef6
@@ -126,7 +163,19 @@
045ef6
     }
045ef6
 
045ef6
     nread = NET_Read(fd, bufP, len);
045ef6
-
045ef6
+#else
045ef6
+    if (timeout) {
045ef6
+        nread = NET_ReadWithTimeout(env, fd, bufP, len, timeout);
045ef6
+        if ((*env)->ExceptionCheck(env)) {
045ef6
+            if (bufP != BUF) {
045ef6
+                free(bufP);
045ef6
+            }
045ef6
+            return nread;
045ef6
+        }
045ef6
+    } else {
045ef6
+        nread = NET_Read(fd, bufP, len);
045ef6
+    }
045ef6
+#endif
045ef6
     if (nread <= 0) {
045ef6
         if (nread < 0) {
045ef6
 
045ef6
diff --git a/src/solaris/native/java/net/bsd_close.c b/src/solaris/native/java/net/bsd_close.c
045ef6
--- openjdk/jdk/src/solaris/native/java/net/bsd_close.c
045ef6
+++ openjdk/jdk/src/solaris/native/java/net/bsd_close.c
045ef6
@@ -1,5 +1,5 @@
045ef6
 /*
045ef6
- * Copyright (c) 2001, 2012, Oracle and/or its affiliates. All rights reserved.
045ef6
+ * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
045ef6
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
045ef6
  *
045ef6
  * This code is free software; you can redistribute it and/or modify it
045ef6
@@ -292,6 +292,10 @@
045ef6
     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
045ef6
 }
045ef6
 
045ef6
+int NET_NonBlockingRead(int s, void* buf, size_t len) {
045ef6
+    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT));
045ef6
+}
045ef6
+
045ef6
 int NET_ReadV(int s, const struct iovec * vector, int count) {
045ef6
     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
045ef6
 }
045ef6
@@ -344,8 +348,8 @@
045ef6
  * Auto restarts with adjusted timeout if interrupted by
045ef6
  * signal other than our wakeup signal.
045ef6
  */
045ef6
-int NET_Timeout(int s, long timeout) {
045ef6
-    long prevtime = 0, newtime;
045ef6
+int NET_Timeout0(int s, long timeout, long currentTime) {
045ef6
+    long prevtime = currentTime, newtime;
045ef6
     struct timeval t, *tp = &t;
045ef6
     fd_set fds;
045ef6
     fd_set* fdsp = NULL;
045ef6
@@ -366,9 +370,6 @@
045ef6
      */
045ef6
     if (timeout > 0) {
045ef6
         /* Timed */
045ef6
-        struct timeval now;
045ef6
-        gettimeofday(&now, NULL);
045ef6
-        prevtime = now.tv_sec * 1000  +  now.tv_usec / 1000;
045ef6
         t.tv_sec = timeout / 1000;
045ef6
         t.tv_usec = (timeout % 1000) * 1000;
045ef6
     } else if (timeout < 0) {
045ef6
diff --git a/src/solaris/native/java/net/linux_close.c b/src/solaris/native/java/net/linux_close.c
045ef6
--- openjdk/jdk/src/solaris/native/java/net/linux_close.c
045ef6
+++ openjdk/jdk/src/solaris/native/java/net/linux_close.c
045ef6
@@ -1,5 +1,5 @@
045ef6
 /*
045ef6
- * Copyright (c) 2001, 2013, Oracle and/or its affiliates. All rights reserved.
045ef6
+ * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
045ef6
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
045ef6
  *
045ef6
  * This code is free software; you can redistribute it and/or modify it
045ef6
@@ -273,6 +273,10 @@
045ef6
     BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, 0) );
045ef6
 }
045ef6
 
045ef6
+int NET_NonBlockingRead(int s, void* buf, size_t len) {
045ef6
+    BLOCKING_IO_RETURN_INT( s, recv(s, buf, len, MSG_DONTWAIT) );
045ef6
+}
045ef6
+
045ef6
 int NET_ReadV(int s, const struct iovec * vector, int count) {
045ef6
     BLOCKING_IO_RETURN_INT( s, readv(s, vector, count) );
045ef6
 }
045ef6
@@ -324,8 +328,8 @@
045ef6
  * Auto restarts with adjusted timeout if interrupted by
045ef6
  * signal other than our wakeup signal.
045ef6
  */
045ef6
-int NET_Timeout(int s, long timeout) {
045ef6
-    long prevtime = 0, newtime;
045ef6
+int NET_Timeout0(int s, long timeout, long currentTime) {
045ef6
+    long prevtime = currentTime, newtime;
045ef6
     struct timeval t;
045ef6
     fdEntry_t *fdEntry = getFdEntry(s);
045ef6
 
045ef6
@@ -337,14 +341,6 @@
045ef6
         return -1;
045ef6
     }
045ef6
 
045ef6
-    /*
045ef6
-     * Pick up current time as may need to adjust timeout
045ef6
-     */
045ef6
-    if (timeout > 0) {
045ef6
-        gettimeofday(&t, NULL);
045ef6
-        prevtime = t.tv_sec * 1000  +  t.tv_usec / 1000;
045ef6
-    }
045ef6
-
045ef6
     for(;;) {
045ef6
         struct pollfd pfd;
045ef6
         int rv;
045ef6
diff --git a/src/solaris/native/java/net/net_util_md.c b/src/solaris/native/java/net/net_util_md.c
045ef6
--- openjdk/jdk/src/solaris/native/java/net/net_util_md.c
045ef6
+++ openjdk/jdk/src/solaris/native/java/net/net_util_md.c
045ef6
@@ -33,6 +33,7 @@
045ef6
 #include <netdb.h>
045ef6
 #include <stdlib.h>
045ef6
 #include <dlfcn.h>
045ef6
+#include <sys/time.h>
045ef6
 
045ef6
 #ifndef _ALLBSD_SOURCE
045ef6
 #include <values.h>
045ef6
@@ -1661,3 +1662,20 @@
045ef6
 
045ef6
     return timeout;
045ef6
 }
045ef6
+
045ef6
+#if !defined(__solaris__)
045ef6
+long NET_GetCurrentTime() {
045ef6
+    struct timeval time;
045ef6
+    gettimeofday(&time, NULL);
045ef6
+    return (time.tv_sec * 1000 + time.tv_usec / 1000);
045ef6
+}
045ef6
+
045ef6
+int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime) {
045ef6
+    return NET_Timeout0(s, timeout, currentTime);
045ef6
+}
045ef6
+
045ef6
+int NET_Timeout(int s, long timeout) {
045ef6
+    long currentTime = (timeout > 0) ? NET_GetCurrentTime() : 0;
045ef6
+    return NET_Timeout0(s, timeout, currentTime);
045ef6
+}
045ef6
+#endif
045ef6
diff --git a/src/solaris/native/java/net/net_util_md.h b/src/solaris/native/java/net/net_util_md.h
045ef6
--- openjdk/jdk/src/solaris/native/java/net/net_util_md.h
045ef6
+++ openjdk/jdk/src/solaris/native/java/net/net_util_md.h
045ef6
@@ -47,9 +47,13 @@
045ef6
    close subroutine does not return until the select call returns.
045ef6
    ...
045ef6
 */
045ef6
-#if defined(__linux__) || defined(MACOSX) || defined (_AIX)
045ef6
+#if !defined(__solaris__)
045ef6
 extern int NET_Timeout(int s, long timeout);
045ef6
+extern int NET_Timeout0(int s, long timeout, long currentTime);
045ef6
 extern int NET_Read(int s, void* buf, size_t len);
045ef6
+extern int NET_NonBlockingRead(int s, void* buf, size_t len);
045ef6
+extern int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime);
045ef6
+extern long NET_GetCurrentTime();
045ef6
 extern int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
045ef6
        struct sockaddr *from, int *fromlen);
045ef6
 extern int NET_ReadV(int s, const struct iovec * vector, int count);