Skip to content

Commit

Permalink
[Wisp] Socket.getChannel() should return null.
Browse files Browse the repository at this point in the history
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:
dragonwell-project#140
  • Loading branch information
ZhaiMo15 committed Aug 25, 2023
1 parent 9a783f5 commit 255b246
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/ServerSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ public void close() throws IOException {
* @since 1.4
*/
public ServerSocketChannel getChannel() {
return WispEngine.transparentWispSwitch() ? asyncImpl.getChannel() : null;
return null;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/java.base/share/classes/java/net/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ public SocketAddress getLocalSocketAddress() {
* @since 1.4
*/
public SocketChannel getChannel() {
return WispEngine.transparentWispSwitch() ? asyncImpl.getChannel() : null;
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -100,10 +100,6 @@ public void close() throws IOException {
}
}

public ServerSocketChannel getChannel() {
return ssc;
}

public boolean isBound() {
return ssc != null && ssc.isBound();
}
Expand Down
10 changes: 3 additions & 7 deletions src/java.base/share/classes/sun/nio/ch/WispSocketImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down
23 changes: 15 additions & 8 deletions test/jdk/com/alibaba/wisp/io/TestGlobalPoller.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Properties>() {
public Properties run() {
return System.getProperties();
}
}
(PrivilegedAction<Properties>) System::getProperties
);
socketAddr = (String)p.get("test.wisp.socketAddress");
socketAddr = (String) p.get("test.wisp.socketAddress");
if (socketAddr == null) {
socketAddr = "www.example.com";
}
Expand All @@ -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");
Expand All @@ -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);
}
}
26 changes: 26 additions & 0 deletions test/jdk/com/alibaba/wisp2/SocketGetChannelTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 255b246

Please sign in to comment.