Skip to content

Commit

Permalink
8317603: Improve exception messages thrown by sun.nio.ch.Net native m…
Browse files Browse the repository at this point in the history
…ethods (win)

Reviewed-by: vtewari, alanb, djelinski
  • Loading branch information
MBaesken committed Oct 11, 2023
1 parent 0fd8071 commit a9b41da
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 34 deletions.
12 changes: 8 additions & 4 deletions src/java.base/windows/native/libnio/ch/DatagramChannelImpl.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, 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
Expand Down Expand Up @@ -89,7 +89,7 @@ Java_sun_nio_ch_DatagramChannelImpl_disconnect0(JNIEnv *env, jclass clazz,

rv = connect((SOCKET)fd, &sa.sa, sa_len);
if (rv == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "connect");
} else {
/* Disable WSAECONNRESET errors as socket is no longer connected */
BOOL enable = FALSE;
Expand Down Expand Up @@ -136,7 +136,10 @@ Java_sun_nio_ch_DatagramChannelImpl_receive0(JNIEnv *env, jclass clazz,
}
} else if (theErr == WSAEWOULDBLOCK) {
return IOS_UNAVAILABLE;
} else return handleSocketError(env, theErr);
} else {
NET_ThrowNew(env, theErr, "recvfrom");
return IOS_THROWN;
}
}
} while (retry);

Expand All @@ -160,7 +163,8 @@ Java_sun_nio_ch_DatagramChannelImpl_send0(JNIEnv *env, jclass clazz,
if (theErr == WSAEWOULDBLOCK) {
return IOS_UNAVAILABLE;
}
return handleSocketError(env, (jint)WSAGetLastError());
NET_ThrowNew(env, (jint)WSAGetLastError(), "sendto");
return IOS_THROWN;
}
return rv;
}
5 changes: 2 additions & 3 deletions src/java.base/windows/native/libnio/ch/IOUtil.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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
Expand Down Expand Up @@ -149,8 +149,7 @@ Java_sun_nio_ch_IOUtil_configureBlocking(JNIEnv *env, jclass clazz,
}
result = ioctlsocket(fd, FIONBIO, &argp);
if (result == SOCKET_ERROR) {
int error = WSAGetLastError();
handleSocketError(env, (jint)error);
NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket");
}
}

Expand Down
40 changes: 17 additions & 23 deletions src/java.base/windows/native/libnio/ch/Net.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,6 @@ static void setConnectionReset(SOCKET s, BOOL enable) {
NULL, 0, &bytesReturned, NULL, NULL);
}

jint handleSocketError(JNIEnv *env, int errorValue)
{
NET_ThrowNew(env, errorValue, NULL);
return IOS_THROWN;
}

static jclass isa_class; /* java.net.InetSocketAddress */
static jmethodID isa_ctorID; /* InetSocketAddress(InetAddress, int) */

Expand Down Expand Up @@ -392,7 +386,7 @@ Java_sun_nio_ch_Net_getIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
n = getsockopt(fdval(env, fdo), level, opt, arg, &arglen);
}
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return IOS_THROWN;
}

Expand Down Expand Up @@ -436,7 +430,7 @@ Java_sun_nio_ch_Net_setIntOption0(JNIEnv *env, jclass clazz, jobject fdo,
n = setsockopt(fdval(env, fdo), level, opt, parg, arglen);
}
if (n == SOCKET_ERROR)
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt");
}

JNIEXPORT jint JNICALL
Expand Down Expand Up @@ -467,7 +461,7 @@ Java_sun_nio_ch_Net_joinOrDrop4(JNIEnv *env, jobject this, jboolean join, jobjec
if (n == SOCKET_ERROR) {
if (join && (WSAGetLastError() == WSAENOPROTOOPT))
return IOS_UNAVAILABLE;
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt");
}
return 0;
}
Expand All @@ -489,7 +483,7 @@ Java_sun_nio_ch_Net_blockOrUnblock4(JNIEnv *env, jobject this, jboolean block, j
if (n == SOCKET_ERROR) {
if (block && (WSAGetLastError() == WSAENOPROTOOPT))
return IOS_UNAVAILABLE;
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
return 0;
}
Expand Down Expand Up @@ -542,7 +536,7 @@ Java_sun_nio_ch_Net_joinOrDrop6(JNIEnv *env, jobject this, jboolean join, jobjec
}

if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
return 0;
}
Expand All @@ -554,7 +548,7 @@ Java_sun_nio_ch_Net_blockOrUnblock6(JNIEnv *env, jobject this, jboolean block, j
int opt = (block) ? MCAST_BLOCK_SOURCE : MCAST_UNBLOCK_SOURCE;
int n = setGroupSourceReqOption(env, fdo, opt, group, index, source);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsocketopt to block or unblock source");
}
return 0;
}
Expand All @@ -571,7 +565,7 @@ Java_sun_nio_ch_Net_setInterface4(JNIEnv* env, jobject this, jobject fdo, jint i
n = setsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF,
(void*)&(in.s_addr), arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
}

Expand All @@ -584,7 +578,7 @@ Java_sun_nio_ch_Net_getInterface4(JNIEnv* env, jobject this, jobject fdo)

n = getsockopt(fdval(env, fdo), IPPROTO_IP, IP_MULTICAST_IF, (void*)&in, &arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return IOS_THROWN;
}
return ntohl(in.s_addr);
Expand All @@ -600,7 +594,7 @@ Java_sun_nio_ch_Net_setInterface6(JNIEnv* env, jobject this, jobject fdo, jint i
n = setsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF,
(void*)&(index), arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "setsockopt");
}
}

Expand All @@ -613,7 +607,7 @@ Java_sun_nio_ch_Net_getInterface6(JNIEnv* env, jobject this, jobject fdo)

n = getsockopt(fdval(env, fdo), IPPROTO_IPV6, IPV6_MULTICAST_IF, (void*)&index, &arglen);
if (n == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "getsockopt");
return -1;
}
return (jint)index;
Expand All @@ -631,12 +625,12 @@ Java_sun_nio_ch_Net_shutdown(JNIEnv *env, jclass cl, jobject fdo, jint jhow) {
JNIEXPORT jint JNICALL
Java_sun_nio_ch_Net_available(JNIEnv *env, jclass cl, jobject fdo)
{
int count = 0;
if (NET_SocketAvailable(fdval(env, fdo), &count) != 0) {
handleSocketError(env, WSAGetLastError());
u_long arg;
if (ioctlsocket((SOCKET) fdval(env, fdo), FIONREAD, &arg) == SOCKET_ERROR) {
NET_ThrowNew(env, WSAGetLastError(), "ioctlsocket");
return IOS_THROWN;
}
return (jint) count;
return (jint) arg;
}

JNIEXPORT jint JNICALL
Expand Down Expand Up @@ -667,7 +661,7 @@ Java_sun_nio_ch_Net_poll(JNIEnv* env, jclass this, jobject fdo, jint events, jlo

/* save last winsock error */
if (rv == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "select");
return IOS_THROWN;
} else if (rv >= 0) {
rv = 0;
Expand Down Expand Up @@ -707,7 +701,7 @@ Java_sun_nio_ch_Net_pollConnect(JNIEnv* env, jclass this, jobject fdo, jlong tim
result = select(fd+1, 0, &wr, &ex, (timeout >= 0) ? &t : NULL);

if (result == SOCKET_ERROR) {
handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "select");
return JNI_FALSE;
} else if (result == 0) {
return JNI_FALSE;
Expand All @@ -727,7 +721,7 @@ Java_sun_nio_ch_Net_pollConnect(JNIEnv* env, jclass this, jobject fdo, jlong tim
NET_ThrowNew(env, lastError, "getsockopt");
}
} else if (optError != NO_ERROR) {
handleSocketError(env, optError);
NET_ThrowNew(env, optError, "getsockopt");
}
return JNI_FALSE;
}
Expand Down
5 changes: 3 additions & 2 deletions src/java.base/windows/native/libnio/ch/UnixDomainSockets.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2023, 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
Expand Down Expand Up @@ -158,7 +158,8 @@ Java_sun_nio_ch_UnixDomainSockets_socket0(JNIEnv *env, jclass cl)
{
SOCKET s = WSASocketW(PF_UNIX, SOCK_STREAM, 0, &provider, 0, WSA_FLAG_OVERLAPPED);
if (s == INVALID_SOCKET) {
return handleSocketError(env, WSAGetLastError());
NET_ThrowNew(env, WSAGetLastError(), "WSASocketW");
return IOS_THROWN;
}
SetHandleInformation((HANDLE)s, HANDLE_FLAG_INHERIT, 0);
return (int)s;
Expand Down
3 changes: 1 addition & 2 deletions src/java.base/windows/native/libnio/ch/nio_util.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2001, 2023, 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
Expand Down Expand Up @@ -46,7 +46,6 @@ jlong handleval(JNIEnv *env, jobject fdo);
jint convertReturnVal(JNIEnv *env, jint n, jboolean r);
jlong convertLongReturnVal(JNIEnv *env, jlong n, jboolean r);
jboolean purgeOutstandingICMP(JNIEnv *env, jclass clazz, jint fd);
jint handleSocketError(JNIEnv *env, int errorValue);

#ifdef _WIN64

Expand Down

5 comments on commit a9b41da

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk21u

@openjdk
Copy link

@openjdk openjdk bot commented on a9b41da Oct 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken the backport was successfully created on the branch MBaesken-backport-a9b41da9 in my personal fork of openjdk/jdk21u. To create a pull request with this backport targeting openjdk/jdk21u:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit a9b41da9 from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 11 Oct 2023 and was reviewed by Vyom Tewari, Alan Bateman and Daniel Jeliński.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk21u:

$ git fetch https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-a9b41da9:MBaesken-backport-a9b41da9
$ git checkout MBaesken-backport-a9b41da9
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk21u.git MBaesken-backport-a9b41da9

@MBaesken
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/backport jdk17u-dev

@openjdk
Copy link

@openjdk openjdk bot commented on a9b41da Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MBaesken the backport was successfully created on the branch MBaesken-backport-a9b41da9 in my personal fork of openjdk/jdk17u-dev. To create a pull request with this backport targeting openjdk/jdk17u-dev:master, just click the following link:

➡️ Create pull request

The title of the pull request is automatically filled in correctly and below you find a suggestion for the pull request body:

Hi all,

This pull request contains a backport of commit a9b41da9 from the openjdk/jdk repository.

The commit being backported was authored by Matthias Baesken on 11 Oct 2023 and was reviewed by Vyom Tewari, Alan Bateman and Daniel Jeliński.

Thanks!

If you need to update the source branch of the pull then run the following commands in a local clone of your personal fork of openjdk/jdk17u-dev:

$ git fetch https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-a9b41da9:MBaesken-backport-a9b41da9
$ git checkout MBaesken-backport-a9b41da9
# make changes
$ git add paths/to/changed/files
$ git commit --message 'Describe additional changes made'
$ git push https://github.com/openjdk-bots/jdk17u-dev.git MBaesken-backport-a9b41da9

Please sign in to comment.