8179602: Backout fix for JDK-8165437 due to breakage on 32-bit Linux

Reviewed-by: chegar
This commit is contained in:
Michael McMahon 2017-05-04 18:13:42 +01:00
parent efae4e9064
commit aa284fd51d
9 changed files with 129 additions and 107 deletions

View File

@ -1,6 +1,6 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2017, SAP SE and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, SAP SE 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
@ -65,8 +65,6 @@
#include <unistd.h>
#include <errno.h>
#include <sys/poll.h>
#include "jvm.h"
#include "net_util.h"
/*
* Stack allocated by thread when doing blocking operation
@ -509,9 +507,9 @@ int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
* Auto restarts with adjusted timeout if interrupted by
* signal other than our wakeup signal.
*/
int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
long prevNanoTime = nanoTimeStamp;
long nanoTimeout = timeout * NET_NSEC_PER_MSEC;
int NET_Timeout0(int s, long timeout, long currentTime) {
long prevtime = currentTime, newtime;
struct timeval t;
fdEntry_t *fdEntry = getFdEntry(s);
/*
@ -535,7 +533,7 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
pfd.events = POLLIN | POLLERR;
startOp(fdEntry, &self);
rv = poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
rv = poll(&pfd, 1, timeout);
endOp(fdEntry, &self);
/*
@ -543,14 +541,18 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
* has expired return 0 (indicating timeout expired).
*/
if (rv < 0 && errno == EINTR) {
long newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= newNanoTime - prevNanoTime;
if (nanoTimeout < NET_NSEC_PER_MSEC) {
return 0;
if (timeout > 0) {
gettimeofday(&t, NULL);
newtime = t.tv_sec * 1000 + t.tv_usec / 1000;
timeout -= newtime - prevtime;
if (timeout <= 0) {
return 0;
}
prevtime = newtime;
}
prevNanoTime = newNanoTime;
} else {
return rv;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, 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
@ -37,8 +37,6 @@
#include <unistd.h>
#include <errno.h>
#include <sys/poll.h>
#include "jvm.h"
#include "net_util.h"
/*
* Stack allocated by thread when doing blocking operation
@ -412,9 +410,9 @@ int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
* Auto restarts with adjusted timeout if interrupted by
* signal other than our wakeup signal.
*/
int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
long prevNanoTime = nanoTimeStamp;
long nanoTimeout = timeout * NET_NSEC_PER_MSEC;
int NET_Timeout0(int s, long timeout, long currentTime) {
long prevtime = currentTime, newtime;
struct timeval t;
fdEntry_t *fdEntry = getFdEntry(s);
/*
@ -438,7 +436,7 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
pfd.events = POLLIN | POLLERR;
startOp(fdEntry, &self);
rv = poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
rv = poll(&pfd, 1, timeout);
endOp(fdEntry, &self);
/*
@ -446,14 +444,18 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
* has expired return 0 (indicating timeout expired).
*/
if (rv < 0 && errno == EINTR) {
long newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= newNanoTime - prevNanoTime;
if (nanoTimeout < NET_NSEC_PER_MSEC) {
return 0;
if (timeout > 0) {
gettimeofday(&t, NULL);
newtime = t.tv_sec * 1000 + t.tv_usec / 1000;
timeout -= newtime - prevtime;
if (timeout <= 0) {
return 0;
}
prevtime = newtime;
}
prevNanoTime = newNanoTime;
} else {
return rv;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2016, 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
@ -39,8 +39,6 @@
#include <unistd.h>
#include <errno.h>
#include <sys/poll.h>
#include "jvm.h"
#include "net_util.h"
/*
* Stack allocated by thread when doing blocking operation
@ -416,7 +414,8 @@ int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
* Auto restarts with adjusted timeout if interrupted by
* signal other than our wakeup signal.
*/
int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
int NET_Timeout0(int s, long timeout, long currentTime) {
long prevtime = currentTime, newtime;
struct timeval t, *tp = &t;
fd_set fds;
fd_set* fdsp = NULL;
@ -461,8 +460,6 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
}
FD_SET(s, fdsp);
long prevNanoTime = nanoTimeStamp;
long nanoTimeout = timeout * NET_NSEC_PER_MSEC;
for(;;) {
int rv;
@ -480,21 +477,25 @@ int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
* has expired return 0 (indicating timeout expired).
*/
if (rv < 0 && errno == EINTR) {
long newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= newNanoTime - prevNanoTime;
if (nanoTimeout < NET_NSEC_PER_MSEC) {
if (allocated != 0)
free(fdsp);
return 0;
if (timeout > 0) {
struct timeval now;
gettimeofday(&now, NULL);
newtime = now.tv_sec * 1000 + now.tv_usec / 1000;
timeout -= newtime - prevtime;
if (timeout <= 0) {
if (allocated != 0)
free(fdsp);
return 0;
}
prevtime = newtime;
t.tv_sec = timeout / 1000;
t.tv_usec = (timeout % 1000) * 1000;
}
prevNanoTime = newNanoTime;
t.tv_sec = nanoTimeout / NET_NSEC_PER_SEC;
t.tv_usec = (nanoTimeout % NET_NSEC_PER_SEC) / NET_NSEC_PER_USEC;
} else {
if (allocated != 0)
free(fdsp);
return rv;
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 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
@ -27,8 +27,6 @@
#include <sys/socket.h>
#include <stropts.h>
#include <unistd.h>
#include "jvm.h"
#include "net_util.h"
/* Support for restartable system calls on Solaris. */
@ -92,22 +90,25 @@ int NET_Poll(struct pollfd *ufds, unsigned int nfds, int timeout) {
RESTARTABLE_RETURN_INT(poll(ufds, nfds, timeout));
}
int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp) {
int NET_Timeout0(int s, long timeout, long currentTime) {
int result;
long prevNanoTime = nanoTimeStamp;
long nanoTimeout = timeout * NET_NSEC_PER_MSEC;
struct timeval t;
long prevtime = currentTime, newtime;
struct pollfd pfd;
pfd.fd = s;
pfd.events = POLLIN;
for(;;) {
result = poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
result = poll(&pfd, 1, timeout);
if (result < 0 && errno == EINTR) {
long newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= newNanoTime - prevNanoTime;
if (nanoTimeout < NET_NSEC_PER_MSEC)
return 0;
prevNanoTime = newNanoTime;
if (timeout > 0) {
gettimeofday(&t, NULL);
newtime = (t.tv_sec * 1000) + t.tv_usec /1000;
timeout -= newtime - prevtime;
if (timeout <= 0)
return 0;
prevtime = newtime;
}
} else {
return result;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -485,7 +485,7 @@ Java_java_net_PlainDatagramSocketImpl_peek(JNIEnv *env, jobject this,
return -1;
}
if (timeout) {
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
int ret = NET_Timeout(fd, timeout);
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Peek timed out");
@ -576,7 +576,7 @@ Java_java_net_PlainDatagramSocketImpl_peekData(JNIEnv *env, jobject this,
packetBufferOffset = (*env)->GetIntField(env, packet, dp_offsetID);
packetBufferLen = (*env)->GetIntField(env, packet, dp_bufLengthID);
if (timeout) {
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
int ret = NET_Timeout(fd, timeout);
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Receive timed out");
@ -789,7 +789,7 @@ Java_java_net_PlainDatagramSocketImpl_receive0(JNIEnv *env, jobject this,
retry = JNI_FALSE;
if (timeout) {
int ret = NET_Timeout(env, fd, timeout, JVM_NanoTime(env, 0));
int ret = NET_Timeout(fd, timeout);
if (ret <= 0) {
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -24,7 +24,6 @@
*/
#include <errno.h>
#include "jvm.h"
#include "net_util.h"
#include "java_net_SocketOptions.h"
@ -232,6 +231,7 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
{
jint localport = (*env)->GetIntField(env, this, psi_localportID);
int len = 0;
/* fdObj is the FileDescriptor field on this */
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
@ -325,8 +325,8 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
/* connection not established immediately */
if (connect_rv != 0) {
socklen_t optlen;
jlong nanoTimeout = timeout * NET_NSEC_PER_MSEC;
jlong prevNanoTime = JVM_NanoTime(env, 0);
jlong prevTime = JVM_CurrentTimeMillis(env, 0);
if (errno != EINPROGRESS) {
NET_ThrowByNameWithLastError(env, JNU_JAVANETPKG "ConnectException",
"connect failed");
@ -341,13 +341,13 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
* this thread.
*/
while (1) {
jlong newNanoTime;
jlong newTime;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = POLLOUT;
errno = 0;
connect_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
connect_rv = NET_Poll(&pfd, 1, timeout);
if (connect_rv >= 0) {
break;
@ -360,13 +360,13 @@ Java_java_net_PlainSocketImpl_socketConnect(JNIEnv *env, jobject this,
* The poll was interrupted so adjust timeout and
* restart
*/
newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= (newNanoTime - prevNanoTime);
if (nanoTimeout < NET_NSEC_PER_MSEC) {
newTime = JVM_CurrentTimeMillis(env, 0);
timeout -= (newTime - prevTime);
if (timeout <= 0) {
connect_rv = 0;
break;
}
prevNanoTime = newNanoTime;
prevTime = newTime;
} /* while */
@ -593,7 +593,7 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
/* fields on this */
int port;
jint timeout = (*env)->GetIntField(env, this, psi_timeoutID);
jlong prevNanoTime = 0, nanoTimeout = timeout * NET_NSEC_PER_MSEC;
jlong prevTime = 0;
jobject fdObj = (*env)->GetObjectField(env, this, psi_fdID);
/* the FileDescriptor field on socket */
@ -633,18 +633,18 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
*/
for (;;) {
int ret;
jlong currNanoTime;
/* first usage pick up current time */
if (prevNanoTime == 0 && nanoTimeout > 0) {
prevNanoTime = JVM_NanoTime(env, 0);
if (prevTime == 0 && timeout > 0) {
prevTime = JVM_CurrentTimeMillis(env, 0);
}
/* passing a timeout of 0 to poll will return immediately,
but in the case of ServerSocket 0 means infinite. */
if (timeout <= 0) {
ret = NET_Timeout(env, fd, -1, 0);
ret = NET_Timeout(fd, -1);
} else {
ret = NET_Timeout(env, fd, nanoTimeout / NET_NSEC_PER_MSEC, prevNanoTime);
ret = NET_Timeout(fd, timeout);
}
if (ret == 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
@ -676,14 +676,17 @@ Java_java_net_PlainSocketImpl_socketAccept(JNIEnv *env, jobject this,
}
/* ECONNABORTED or EWOULDBLOCK error so adjust timeout if there is one. */
currNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= (currNanoTime - prevNanoTime);
if (nanoTimeout < NET_NSEC_PER_MSEC) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Accept timed out");
return;
if (timeout) {
jlong currTime = JVM_CurrentTimeMillis(env, 0);
timeout -= (currTime - prevTime);
if (timeout <= 0) {
JNU_ThrowByName(env, JNU_JAVANETPKG "SocketTimeoutException",
"Accept timed out");
return;
}
prevTime = currTime;
}
prevNanoTime = currNanoTime;
}
if (newfd < 0) {

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -26,7 +26,6 @@
#include <stdlib.h>
#include <string.h>
#include "jvm.h"
#include "net_util.h"
#include "java_net_SocketInputStream.h"
@ -49,10 +48,9 @@ Java_java_net_SocketInputStream_init(JNIEnv *env, jclass cls) {
static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long timeout) {
int result = 0;
long prevNanoTime = JVM_NanoTime(env, 0);
long nanoTimeout = timeout * NET_NSEC_PER_MSEC;
while (nanoTimeout > NET_NSEC_PER_MSEC) {
result = NET_Timeout(env, fd, nanoTimeout / NET_NSEC_PER_MSEC, prevNanoTime);
long prevtime = NET_GetCurrentTime(), newtime;
while (timeout > 0) {
result = NET_TimeoutWithCurrentTime(fd, timeout, prevtime);
if (result <= 0) {
if (result == 0) {
JNU_ThrowByName(env, "java/net/SocketTimeoutException", "Read timed out");
@ -70,10 +68,10 @@ static int NET_ReadWithTimeout(JNIEnv *env, int fd, char *bufP, int len, long ti
}
result = NET_NonBlockingRead(fd, bufP, len);
if (result == -1 && ((errno == EAGAIN) || (errno == EWOULDBLOCK))) {
long newtNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= newtNanoTime - prevNanoTime;
if (nanoTimeout >= NET_NSEC_PER_MSEC) {
prevNanoTime = newtNanoTime;
newtime = NET_GetCurrentTime();
timeout -= newtime - prevtime;
if (timeout > 0) {
prevtime = newtime;
}
} else {
break;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -49,7 +49,6 @@
#include <sys/sysctl.h>
#endif
#include "jvm.h"
#include "net_util.h"
#include "java_net_SocketOptions.h"
@ -1544,12 +1543,11 @@ NET_Bind(int fd, SOCKETADDRESS *sa, int len)
jint
NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
{
jlong prevNanoTime = JVM_NanoTime(env, 0);
jlong nanoTimeout = timeout * NET_NSEC_PER_MSEC;
jlong prevTime = JVM_CurrentTimeMillis(env, 0);
jint read_rv;
while (1) {
jlong newNanoTime;
jlong newTime;
struct pollfd pfd;
pfd.fd = fd;
pfd.events = 0;
@ -1561,18 +1559,36 @@ NET_Wait(JNIEnv *env, jint fd, jint flags, jint timeout)
pfd.events |= POLLOUT;
errno = 0;
read_rv = NET_Poll(&pfd, 1, nanoTimeout / NET_NSEC_PER_MSEC);
read_rv = NET_Poll(&pfd, 1, timeout);
newNanoTime = JVM_NanoTime(env, 0);
nanoTimeout -= (newNanoTime - prevNanoTime);
if (nanoTimeout < NET_NSEC_PER_MSEC) {
newTime = JVM_CurrentTimeMillis(env, 0);
timeout -= (newTime - prevTime);
if (timeout <= 0) {
return read_rv > 0 ? 0 : -1;
}
prevNanoTime = newNanoTime;
prevTime = newTime;
if (read_rv > 0) {
break;
}
} /* while */
return (nanoTimeout / NET_NSEC_PER_MSEC);
return timeout;
}
long NET_GetCurrentTime() {
struct timeval time;
gettimeofday(&time, NULL);
return (time.tv_sec * 1000 + time.tv_usec / 1000);
}
int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime) {
return NET_Timeout0(s, timeout, currentTime);
}
int NET_Timeout(int s, long timeout) {
long currentTime = (timeout > 0) ? NET_GetCurrentTime() : 0;
return NET_Timeout0(s, timeout, currentTime);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2017, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2016, 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
@ -34,10 +34,6 @@
* Macros and constants
*/
#define NET_NSEC_PER_MSEC 1000*1000
#define NET_NSEC_PER_SEC 1000*1000*1000
#define NET_NSEC_PER_USEC 1000
/* Defines SO_REUSEPORT */
#ifndef SO_REUSEPORT
#ifdef __linux__
@ -72,9 +68,12 @@ typedef union {
* Functions
*/
int NET_Timeout(JNIEnv *env, int s, long timeout, long nanoTimeStamp);
int NET_Timeout(int s, long timeout);
int NET_Timeout0(int s, long timeout, long currentTime);
int NET_Read(int s, void* buf, size_t len);
int NET_NonBlockingRead(int s, void* buf, size_t len);
int NET_TimeoutWithCurrentTime(int s, long timeout, long currentTime);
long NET_GetCurrentTime();
int NET_RecvFrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, socklen_t *fromlen);
int NET_ReadV(int s, const struct iovec * vector, int count);