From 255b2466a0db772e561128013563f4861a5b7d71 Mon Sep 17 00:00:00 2001 From: Zhai Mo Date: Fri, 25 Aug 2023 14:47:09 +0800 Subject: [PATCH] [Wisp] Socket.getChannel() should return null. Summary: Changing the return value of Socket.getChannel() will affect the correctness of some libraries. For example, Apache geode tests whether it is a nio adaptor channel through the return value of getChannel(). Test Plan: SocketGetChannelTest Reviewed-by: yulei Issue: https://github.com/dragonwell-project/dragonwell17/issues/140 --- .../share/classes/java/net/ServerSocket.java | 2 +- .../share/classes/java/net/Socket.java | 2 +- .../sun/nio/ch/WispServerSocketImpl.java | 8 ++---- .../classes/sun/nio/ch/WispSocketImpl.java | 10 +++---- .../com/alibaba/wisp/io/TestGlobalPoller.java | 23 ++++++++++------ .../alibaba/wisp2/SocketGetChannelTest.java | 26 +++++++++++++++++++ 6 files changed, 48 insertions(+), 23 deletions(-) create mode 100644 test/jdk/com/alibaba/wisp2/SocketGetChannelTest.java diff --git a/src/java.base/share/classes/java/net/ServerSocket.java b/src/java.base/share/classes/java/net/ServerSocket.java index 2d5c28ec9a9..648446aa8bc 100644 --- a/src/java.base/share/classes/java/net/ServerSocket.java +++ b/src/java.base/share/classes/java/net/ServerSocket.java @@ -797,7 +797,7 @@ public void close() throws IOException { * @since 1.4 */ public ServerSocketChannel getChannel() { - return WispEngine.transparentWispSwitch() ? asyncImpl.getChannel() : null; + return null; } /** diff --git a/src/java.base/share/classes/java/net/Socket.java b/src/java.base/share/classes/java/net/Socket.java index 7b9a6d7769c..62d418ef1a6 100644 --- a/src/java.base/share/classes/java/net/Socket.java +++ b/src/java.base/share/classes/java/net/Socket.java @@ -948,7 +948,7 @@ public SocketAddress getLocalSocketAddress() { * @since 1.4 */ public SocketChannel getChannel() { - return WispEngine.transparentWispSwitch() ? asyncImpl.getChannel() : null; + return null; } /** diff --git a/src/java.base/share/classes/sun/nio/ch/WispServerSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/WispServerSocketImpl.java index c4fa92098eb..72b2fb99b6a 100644 --- a/src/java.base/share/classes/sun/nio/ch/WispServerSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/WispServerSocketImpl.java @@ -15,9 +15,9 @@ public class WispServerSocketImpl { - private static WispEngineAccess WEA = SharedSecrets.getWispEngineAccess(); + private static final WispEngineAccess WEA = SharedSecrets.getWispEngineAccess(); - private WispSocketLockSupport wispSocketLockSupport = new WispSocketLockSupport(); + private final WispSocketLockSupport wispSocketLockSupport = new WispSocketLockSupport(); // The channel being adapted private ServerSocketChannelImpl ssc = null; @@ -100,10 +100,6 @@ public void close() throws IOException { } } - public ServerSocketChannel getChannel() { - return ssc; - } - public boolean isBound() { return ssc != null && ssc.isBound(); } diff --git a/src/java.base/share/classes/sun/nio/ch/WispSocketImpl.java b/src/java.base/share/classes/sun/nio/ch/WispSocketImpl.java index 400128184d3..5f2d8f1ab56 100644 --- a/src/java.base/share/classes/sun/nio/ch/WispSocketImpl.java +++ b/src/java.base/share/classes/sun/nio/ch/WispSocketImpl.java @@ -19,13 +19,13 @@ public class WispSocketImpl { - private static WispEngineAccess WEA = SharedSecrets.getWispEngineAccess(); + private static final WispEngineAccess WEA = SharedSecrets.getWispEngineAccess(); - WispSocketLockSupport wispSocketLockSupport = new WispSocketLockSupport(); + private final WispSocketLockSupport wispSocketLockSupport = new WispSocketLockSupport(); // The channel being adapted private SocketChannelImpl sc = null; // 1 verse 1 related socket - private Socket so; + private final Socket so; // Timeout "option" value for reads protected int timeout = 0; private InputStream socketInputStream = null; @@ -39,10 +39,6 @@ public WispSocketImpl(SocketChannel sc, Socket so) { this.sc = (SocketChannelImpl) sc; } - public SocketChannel getChannel() { - return sc; - } - // Override this method just to protect against changes in the superclass // public void connect(SocketAddress remote) throws IOException { diff --git a/test/jdk/com/alibaba/wisp/io/TestGlobalPoller.java b/test/jdk/com/alibaba/wisp/io/TestGlobalPoller.java index 2e73605598d..169e153a483 100644 --- a/test/jdk/com/alibaba/wisp/io/TestGlobalPoller.java +++ b/test/jdk/com/alibaba/wisp/io/TestGlobalPoller.java @@ -13,30 +13,28 @@ import jdk.internal.access.SharedSecrets; import jdk.internal.access.WispEngineAccess; import sun.nio.ch.SelChImpl; +import sun.nio.ch.WispSocketImpl; import java.lang.reflect.Field; import java.net.Socket; import java.nio.channels.SelectionKey; import java.nio.channels.SocketChannel; +import java.security.PrivilegedAction; import java.util.Properties; import static jdk.test.lib.Asserts.assertTrue; import static jdk.test.lib.Asserts.assertNotNull; public class TestGlobalPoller { - private static WispEngineAccess access = SharedSecrets.getWispEngineAccess(); + private static final WispEngineAccess access = SharedSecrets.getWispEngineAccess(); static Properties p; static String socketAddr; static { p = java.security.AccessController.doPrivileged( - new java.security.PrivilegedAction() { - public Properties run() { - return System.getProperties(); - } - } + (PrivilegedAction) System::getProperties ); - socketAddr = (String)p.get("test.wisp.socketAddress"); + socketAddr = (String) p.get("test.wisp.socketAddress"); if (socketAddr == null) { socketAddr = "www.example.com"; } @@ -50,7 +48,7 @@ public static void main(String[] args) throws Exception { // now server returns the data.. // so is readable // current task is interested in read event. - SocketChannel ch = so.getChannel(); + SocketChannel ch = getCh(so); access.registerEvent(ch, SelectionKey.OP_READ); Class clazz = Class.forName("com.alibaba.wisp.engine.WispEventPump$Pool"); @@ -75,4 +73,13 @@ public static void main(String[] args) throws Exception { so.close(); } + + private static SocketChannel getCh(Socket so) throws Exception { + Field f = Socket.class.getDeclaredField("asyncImpl"); + f.setAccessible(true); + WispSocketImpl wispSocket = (WispSocketImpl) f.get(so); + f = wispSocket.getClass().getDeclaredField("sc"); + f.setAccessible(true); + return (SocketChannel) f.get(wispSocket); + } } diff --git a/test/jdk/com/alibaba/wisp2/SocketGetChannelTest.java b/test/jdk/com/alibaba/wisp2/SocketGetChannelTest.java new file mode 100644 index 00000000000..04bd40fb6c9 --- /dev/null +++ b/test/jdk/com/alibaba/wisp2/SocketGetChannelTest.java @@ -0,0 +1,26 @@ +/* + * @test + * @library /test/lib + * @summary test Socket.getChannel returns null + * @requires os.family == "linux" + * @modules java.base/jdk.internal.access + * @modules java.base/com.alibaba.wisp.engine:+open + * @run main SocketGetChannelTest + * @run main/othervm -XX:+UseWisp2 SocketGetChannelTest + */ + +import java.net.ServerSocket; +import java.net.Socket; + +import static jdk.test.lib.Asserts.assertNull; + +public class SocketGetChannelTest { + public static void main(String[] args) throws Exception { + ServerSocket ss = new ServerSocket(0); + Socket s1 = new Socket("localhost", ss.getLocalPort()); + Socket s2 = ss.accept(); + assertNull(ss.getChannel()); + assertNull(s1.getChannel()); + assertNull(s2.getChannel()); + } +} \ No newline at end of file