8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl
authoralanb
Sat, 16 Mar 2019 19:44:12 +0000
changeset 54155 b5a73f22b2bd
parent 54154 1caf2daef7cf
child 54156 56e9781e6044
child 57268 adcdd45830a0
8220493: Prepare Socket/ServerSocket for alternative platform SocketImpl Reviewed-by: chegar Contributed-by: alan.bateman@oracle.com, michael.x.mcmahon@oracle.com
src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java
src/java.base/share/classes/java/net/DelegatingSocketImpl.java
src/java.base/share/classes/java/net/HttpConnectSocketImpl.java
src/java.base/share/classes/java/net/ServerSocket.java
src/java.base/share/classes/java/net/Socket.java
src/java.base/share/classes/java/net/SocketImpl.java
src/java.base/share/classes/java/net/SocksSocketImpl.java
src/java.base/share/classes/sun/net/PlatformSocketImpl.java
src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketInputStreamInstrumentor.java
src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketOutputStreamInstrumentor.java
test/jdk/java/net/SocketImpl/SocketImplCombinations.java
test/jdk/jdk/jfr/event/io/TestInstrumentation.java
--- a/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/AbstractPlainSocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2019, 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
@@ -30,12 +30,16 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
 import sun.net.ConnectionResetException;
 import sun.net.NetHooks;
+import sun.net.PlatformSocketImpl;
 import sun.net.ResourceManager;
 import sun.net.util.SocketExceptions;
 
@@ -46,7 +50,7 @@
  *
  * @author  Steven B. Byrne
  */
-abstract class AbstractPlainSocketImpl extends SocketImpl {
+abstract class AbstractPlainSocketImpl extends SocketImpl implements PlatformSocketImpl {
     /* instance variable for SO_TIMEOUT */
     int timeout;   // timeout in millisec
     // traffic class
@@ -450,15 +454,17 @@
 
     /**
      * Accepts connections.
-     * @param s the connection
+     * @param si the socket impl
      */
-    protected void accept(SocketImpl s) throws IOException {
+    protected void accept(SocketImpl si) throws IOException {
+        si.fd = new FileDescriptor();
         acquireFD();
         try {
-            socketAccept(s);
+            socketAccept(si);
         } finally {
             releaseFD();
         }
+        SocketCleanable.register(si.fd);
     }
 
     /**
@@ -470,8 +476,14 @@
                 throw new IOException("Socket Closed");
             if (shut_rd)
                 throw new IOException("Socket input is shutdown");
-            if (socketInputStream == null)
-                socketInputStream = new SocketInputStream(this);
+            if (socketInputStream == null) {
+                PrivilegedExceptionAction<SocketInputStream> pa = () -> new SocketInputStream(this);
+                try {
+                    socketInputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketInputStream;
     }
@@ -489,8 +501,14 @@
                 throw new IOException("Socket Closed");
             if (shut_wr)
                 throw new IOException("Socket output is shutdown");
-            if (socketOutputStream == null)
-                socketOutputStream = new SocketOutputStream(this);
+            if (socketOutputStream == null) {
+                PrivilegedExceptionAction<SocketOutputStream> pa = () -> new SocketOutputStream(this);
+                try {
+                    socketOutputStream = AccessController.doPrivileged(pa);
+                } catch (PrivilegedActionException e) {
+                    throw (IOException) e.getCause();
+                }
+            }
         }
         return socketOutputStream;
     }
@@ -589,15 +607,10 @@
         }
     }
 
-    void reset() throws IOException {
-        if (fd != null) {
-            socketClose();
-        }
-        fd = null;
-        super.reset();
+    void reset() {
+        throw new InternalError("should not get here");
     }
 
-
     /**
      * Shutdown read-half of the socket connection;
      */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/java/net/DelegatingSocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -0,0 +1,171 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package java.net;
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Objects;
+import java.util.Set;
+
+import sun.net.PlatformSocketImpl;
+
+/**
+ * A SocketImpl that delegates all methods to another SocketImpl.
+ */
+
+class DelegatingSocketImpl extends SocketImpl {
+    protected final SocketImpl delegate;
+
+    DelegatingSocketImpl(SocketImpl delegate) {
+        assert delegate instanceof PlatformSocketImpl;
+        this.delegate = Objects.requireNonNull(delegate);
+    }
+
+    final SocketImpl delegate() {
+        return delegate;
+    }
+
+    @Override
+    protected FileDescriptor getFileDescriptor() {
+        return delegate.getFileDescriptor();
+    }
+
+    @Override
+    protected InetAddress getInetAddress() {
+        return delegate.getInetAddress();
+    }
+
+    @Override
+    protected int getPort() {
+        return delegate.getPort();
+    }
+
+    @Override
+    protected int getLocalPort() {
+        return delegate.getLocalPort();
+    }
+
+    @Override
+    protected void create(boolean stream) throws IOException {
+        delegate.create(stream);
+    }
+
+    @Override
+    protected void connect(String host, int port) throws IOException {
+        delegate.connect(host, port);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        delegate.connect(address, port);
+    }
+
+    @Override
+    protected void connect(SocketAddress address, int timeout) throws IOException {
+        delegate.connect(address, timeout);
+    }
+
+    @Override
+    protected void bind(InetAddress host, int port) throws IOException {
+        delegate.bind(host, port);
+    }
+
+    @Override
+    protected void listen(int backlog) throws IOException {
+        delegate.listen(backlog);
+    }
+
+    @Override
+    protected void accept(SocketImpl s) throws IOException {
+        delegate.accept(s);
+    }
+
+    @Override
+    protected InputStream getInputStream() throws IOException {
+        return delegate.getInputStream();
+    }
+
+    @Override
+    protected OutputStream getOutputStream() throws IOException {
+        return delegate.getOutputStream();
+    }
+
+    @Override
+    protected int available() throws IOException {
+        return delegate.available();
+    }
+
+    @Override
+    protected void close() throws IOException {
+        delegate.close();
+    }
+
+    @Override
+    protected boolean supportsUrgentData() {
+        return delegate.supportsUrgentData();
+    }
+
+    @Override
+    protected void sendUrgentData(int data) throws IOException {
+        delegate.sendUrgentData(data);
+    }
+
+    @Override
+    protected Set<SocketOption<?>> supportedOptions() {
+        return delegate.supportedOptions();
+    }
+
+    @Override
+    protected <T> void setOption(SocketOption<T> opt, T value) throws IOException {
+        delegate.setOption(opt, value);
+    }
+
+    @Override
+    protected <T> T getOption(SocketOption<T> opt) throws IOException {
+        return delegate.getOption(opt);
+    }
+
+    @Override
+    public void setOption(int optID, Object value) throws SocketException {
+        delegate.setOption(optID, value);
+    }
+
+    @Override
+    public Object getOption(int optID) throws SocketException {
+        return delegate.getOption(optID);
+    }
+
+    @Override
+    protected void shutdownInput() throws IOException {
+        delegate.shutdownInput();
+    }
+
+    @Override
+    protected void shutdownOutput() throws IOException {
+        delegate.shutdownOutput();
+    }
+}
--- a/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/HttpConnectSocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2019, 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,14 +34,13 @@
 
 /**
  * Basic SocketImpl that relies on the internal HTTP protocol handler
- * implementation to perform the HTTP tunneling and authentication. The
- * sockets impl is swapped out and replaced with the socket from the HTTP
- * handler after the tunnel is successfully setup.
+ * implementation to perform the HTTP tunneling and authentication. Once
+ * connected, all socket operations delegate to a platform SocketImpl.
  *
  * @since 1.8
  */
 
-/*package*/ class HttpConnectSocketImpl extends PlainSocketImpl {
+/*package*/ class HttpConnectSocketImpl extends DelegatingSocketImpl {
 
     private static final String httpURLClazzStr =
                                   "sun.net.www.protocol.http.HttpURLConnection";
@@ -76,12 +75,8 @@
         }
     }
 
-    HttpConnectSocketImpl(String server, int port) {
-        this.server = server;
-        this.port = port;
-    }
-
-    HttpConnectSocketImpl(Proxy proxy) {
+    HttpConnectSocketImpl(Proxy proxy, SocketImpl delegate) {
+        super(delegate);
         SocketAddress a = proxy.address();
         if ( !(a instanceof InetSocketAddress) )
             throw new IllegalArgumentException("Unsupported address type");
@@ -92,6 +87,27 @@
     }
 
     @Override
+    protected void connect(String host, int port) throws IOException {
+        connect(new InetSocketAddress(host, port), 0);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        connect(new InetSocketAddress(address, port), 0);
+    }
+
+    @Override
+    void setSocket(Socket socket) {
+        delegate.socket = socket;
+        super.setSocket(socket);
+    }
+
+    @Override
+    void setServerSocket(ServerSocket socket) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
     protected void connect(SocketAddress endpoint, int timeout)
         throws IOException
     {
@@ -117,21 +133,37 @@
         close();
 
         // update the Sockets impl to the impl from the http Socket
-        AbstractPlainSocketImpl psi = (AbstractPlainSocketImpl) httpSocket.impl;
-        this.getSocket().impl = psi;
+        SocketImpl si = httpSocket.impl;
+        getSocket().setImpl(si);
 
         // best effort is made to try and reset options previously set
         Set<Map.Entry<Integer,Object>> options = optionsMap.entrySet();
         try {
             for(Map.Entry<Integer,Object> entry : options) {
-                psi.setOption(entry.getKey(), entry.getValue());
+                si.setOption(entry.getKey(), entry.getValue());
             }
         } catch (IOException x) {  /* gulp! */  }
     }
 
+
+    @Override
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
+    }
+
+    @Override
+    void reset() {
+        throw new InternalError("should not get here");
+    }
+
     @Override
     public void setOption(int opt, Object val) throws SocketException {
-        super.setOption(opt, val);
+        delegate.setOption(opt, val);
 
         if (external_address != null)
             return;  // we're connected, just return
@@ -163,7 +195,10 @@
         URL destURL = new URL(urlString);
         HttpURLConnection conn = (HttpURLConnection) destURL.openConnection(proxy);
         conn.setConnectTimeout(connectTimeout);
-        conn.setReadTimeout(this.timeout);
+        int timeout = (int) getOption(SocketOptions.SO_TIMEOUT);
+        if (timeout > 0) {
+            conn.setReadTimeout(timeout);
+        }
         conn.connect();
         doTunneling(conn);
         try {
@@ -174,10 +209,14 @@
         }
     }
 
-    private void doTunneling(HttpURLConnection conn) {
+    private void doTunneling(HttpURLConnection conn) throws IOException {
         try {
             doTunneling.invoke(conn);
         } catch (ReflectiveOperationException x) {
+            Throwable cause = x.getCause();
+            if (cause instanceof IOException) {
+                throw (IOException) cause;
+            }
             throw new InternalError("Should not reach here", x);
         }
     }
@@ -187,7 +226,7 @@
         if (external_address != null)
             return external_address.getAddress();
         else
-            return super.getInetAddress();
+            return delegate.getInetAddress();
     }
 
     @Override
@@ -195,6 +234,6 @@
         if (external_address != null)
             return external_address.getPort();
         else
-            return super.getPort();
+            return delegate.getPort();
     }
 }
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2019, 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
@@ -25,9 +25,6 @@
 
 package java.net;
 
-import jdk.internal.access.JavaNetSocketAccess;
-import jdk.internal.access.SharedSecrets;
-
 import java.io.FileDescriptor;
 import java.io.IOException;
 import java.lang.reflect.Constructor;
@@ -38,6 +35,10 @@
 import java.util.Set;
 import java.util.Collections;
 
+import jdk.internal.access.JavaNetSocketAccess;
+import jdk.internal.access.SharedSecrets;
+import sun.net.PlatformSocketImpl;
+
 /**
  * This class implements server sockets. A server socket waits for
  * requests to come in over the network. It performs some operation
@@ -290,13 +291,12 @@
     }
 
     private void setImpl() {
+        SocketImplFactory factory = ServerSocket.factory;
         if (factory != null) {
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = new SocksSocketImpl();
+            impl = SocketImpl.createPlatformSocketImpl(true);
         }
         if (impl != null)
             impl.setServerSocket(this);
@@ -542,38 +542,134 @@
      * @spec JSR-51
      */
     protected final void implAccept(Socket s) throws IOException {
-        SocketImpl si = null;
-        try {
-            if (s.impl == null)
-              s.setImpl();
-            else {
-                s.impl.reset();
+        SocketImpl si = s.impl;
+
+        // Socket has no SocketImpl
+        if (si == null) {
+            si = implAccept();
+            s.setImpl(si);
+            s.postAccept();
+            return;
+        }
+
+        // Socket has a SOCKS or HTTP SocketImpl, need delegate
+        if (si instanceof DelegatingSocketImpl) {
+            si = ((DelegatingSocketImpl) si).delegate();
+            assert si instanceof PlatformSocketImpl;
+        }
+
+        // Accept connection with a platform or custom SocketImpl.
+        // For the platform SocketImpl case:
+        // - the connection is accepted with a new SocketImpl
+        // - the SO_TIMEOUT socket option is copied to the new SocketImpl
+        // - the Socket is connected to the new SocketImpl
+        // - the existing/old SocketImpl is closed
+        // For the custom SocketImpl case, the connection is accepted with the
+        // existing custom SocketImpl.
+        ensureCompatible(si);
+        if (impl instanceof PlatformSocketImpl) {
+            SocketImpl psi = platformImplAccept();
+            si.copyOptionsTo(psi);
+            s.setImpl(psi);
+            si.closeQuietly();
+        } else {
+            s.impl = null; // temporarily break connection to impl
+            try {
+                customImplAccept(si);
+            } finally {
+                s.impl = si;  // restore connection to impl
             }
-            si = s.impl;
-            s.impl = null;
-            si.address = new InetAddress();
-            si.fd = new FileDescriptor();
-            getImpl().accept(si);
-            SocketCleanable.register(si.fd);   // raw fd has been set
+        }
+        s.postAccept();
+    }
 
-            SecurityManager security = System.getSecurityManager();
-            if (security != null) {
-                security.checkAccept(si.getInetAddress().getHostAddress(),
-                                     si.getPort());
+    /**
+     * Accepts a connection with a new SocketImpl.
+     * @return the new SocketImpl
+     */
+    private SocketImpl implAccept() throws IOException {
+        if (impl instanceof PlatformSocketImpl) {
+            return platformImplAccept();
+        } else {
+            // custom server SocketImpl, client SocketImplFactory must be set
+            SocketImplFactory factory = Socket.socketImplFactory();
+            if (factory == null) {
+                throw new IOException("An instance of " + impl.getClass() +
+                    " cannot accept connection with 'null' SocketImpl:" +
+                    " client socket implementation factory not set");
             }
-        } catch (IOException e) {
-            if (si != null)
-                si.reset();
-            s.impl = si;
-            throw e;
-        } catch (SecurityException e) {
-            if (si != null)
-                si.reset();
-            s.impl = si;
+            SocketImpl si = factory.createSocketImpl();
+            customImplAccept(si);
+            return si;
+        }
+    }
+
+    /**
+     * Accepts a connection with a new platform SocketImpl.
+     * @return the new platform SocketImpl
+     */
+    private SocketImpl platformImplAccept() throws IOException {
+        assert impl instanceof PlatformSocketImpl;
+
+        // create a new platform SocketImpl and accept the connection
+        SocketImpl psi = SocketImpl.createPlatformSocketImpl(false);
+        implAccept(psi);
+        return psi;
+    }
+
+    /**
+     * Accepts a new connection with the given custom SocketImpl.
+     */
+    private void customImplAccept(SocketImpl si) throws IOException {
+        assert !(impl instanceof PlatformSocketImpl)
+                && !(si instanceof PlatformSocketImpl);
+
+        si.reset();
+        try {
+            // custom SocketImpl may expect fd/address objects to be created
+            si.fd = new FileDescriptor();
+            si.address = new InetAddress();
+            implAccept(si);
+        } catch (Exception e) {
+            si.reset();
             throw e;
         }
-        s.impl = si;
-        s.postAccept();
+    }
+
+    /**
+     * Accepts a new connection so that the given SocketImpl is connected to
+     * the peer. The SocketImpl and connection are closed if the connection is
+     * denied by the security manager.
+     * @throws IOException if an I/O error occurs
+     * @throws SecurityException if the security manager's checkAccept method fails
+     */
+    private void implAccept(SocketImpl si) throws IOException {
+        assert !(si instanceof DelegatingSocketImpl);
+
+        // accept a connection
+        impl.accept(si);
+
+        // check permission, close SocketImpl/connection if denied
+        SecurityManager sm = System.getSecurityManager();
+        if (sm != null) {
+            try {
+                sm.checkAccept(si.getInetAddress().getHostAddress(), si.getPort());
+            } catch (SecurityException se) {
+                si.close();
+                throw se;
+            }
+        }
+    }
+
+    /**
+     * Throws IOException if the server SocketImpl and the given client
+     * SocketImpl are not both platform or custom SocketImpls.
+     */
+    private void ensureCompatible(SocketImpl si) throws IOException {
+        if ((impl instanceof PlatformSocketImpl) != (si instanceof PlatformSocketImpl)) {
+            throw new IOException("An instance of " + impl.getClass() +
+                " cannot accept a connection with an instance of " + si.getClass());
+        }
     }
 
     /**
@@ -778,7 +874,7 @@
     /**
      * The factory for all server sockets.
      */
-    private static SocketImplFactory factory = null;
+    private static volatile SocketImplFactory factory;
 
     /**
      * Sets the server socket implementation factory for the
--- a/src/java.base/share/classes/java/net/Socket.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/Socket.java	Sat Mar 16 19:44:12 2019 +0000
@@ -28,9 +28,10 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.IOException;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
 import java.nio.channels.SocketChannel;
 import java.security.AccessController;
-import java.security.PrivilegedExceptionAction;
 import java.security.PrivilegedAction;
 import java.util.Set;
 import java.util.Collections;
@@ -76,6 +77,22 @@
     private boolean oldImpl = false;
 
     /**
+     * Socket input/output streams
+     */
+    private volatile InputStream in;
+    private volatile OutputStream out;
+    private static final VarHandle IN, OUT;
+    static {
+        try {
+            MethodHandles.Lookup l = MethodHandles.lookup();
+            IN = l.findVarHandle(Socket.class, "in", InputStream.class);
+            OUT = l.findVarHandle(Socket.class, "out", OutputStream.class);
+        } catch (Exception e) {
+            throw new InternalError(e);
+        }
+    }
+
+    /**
      * Creates an unconnected socket, with the
      * system-default type of SocketImpl.
      *
@@ -137,16 +154,22 @@
                     security.checkConnect(epoint.getAddress().getHostAddress(),
                                   epoint.getPort());
             }
-            impl = type == Proxy.Type.SOCKS ? new SocksSocketImpl(p)
-                                            : new HttpConnectSocketImpl(p);
+
+            // create a SOCKS or HTTP SocketImpl that delegates to a platform SocketImpl
+            SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
+            impl = (type == Proxy.Type.SOCKS) ? new SocksSocketImpl(p, delegate)
+                                              : new HttpConnectSocketImpl(p, delegate);
             impl.setSocket(this);
         } else {
             if (p == Proxy.NO_PROXY) {
+                // create a platform or custom SocketImpl for the DIRECT case
+                SocketImplFactory factory = Socket.factory;
                 if (factory == null) {
-                    impl = new PlainSocketImpl();
-                    impl.setSocket(this);
-                } else
-                    setImpl();
+                    impl = SocketImpl.createPlatformSocketImpl(false);
+                } else {
+                    impl = factory.createSocketImpl();
+                }
+                impl.setSocket(this);
             } else
                 throw new IllegalArgumentException("Invalid Proxy");
         }
@@ -491,24 +514,29 @@
         });
     }
 
+    void setImpl(SocketImpl si) {
+         impl = si;
+         impl.setSocket(this);
+    }
+
     /**
      * Sets impl to the system-default type of SocketImpl.
      * @since 1.4
      */
     void setImpl() {
+        SocketImplFactory factory = Socket.factory;
         if (factory != null) {
             impl = factory.createSocketImpl();
             checkOldImpl();
         } else {
-            // No need to do a checkOldImpl() here, we know it's an up to date
-            // SocketImpl!
-            impl = new SocksSocketImpl();
+            // create a SOCKS SocketImpl that delegates to a platform SocketImpl
+            SocketImpl delegate = SocketImpl.createPlatformSocketImpl(false);
+            impl = new SocksSocketImpl(delegate);
         }
         if (impl != null)
             impl.setSocket(this);
     }
 
-
     /**
      * Get the {@code SocketImpl} attached to this socket, creating
      * it if necessary.
@@ -907,18 +935,49 @@
             throw new SocketException("Socket is not connected");
         if (isInputShutdown())
             throw new SocketException("Socket input is shutdown");
-        InputStream is = null;
-        try {
-            is = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<>() {
-                    public InputStream run() throws IOException {
-                        return impl.getInputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
+        InputStream in = this.in;
+        if (in == null) {
+            // wrap the input stream so that the close method closes this socket
+            in = new SocketInputStream(this, impl.getInputStream());
+            if (!IN.compareAndSet(this, null, in)) {
+                in = this.in;
+            }
         }
-        return is;
+        return in;
+    }
+
+    /**
+     * An InputStream that delegates read/available operations to an underlying
+     * input stream. The close method is overridden to close the Socket.
+     *
+     * This class is instrumented by Java Flight Recorder (JFR) to get socket
+     * I/O events.
+     */
+    private static class SocketInputStream extends InputStream {
+        private final Socket parent;
+        private final InputStream in;
+        SocketInputStream(Socket parent, InputStream in) {
+            this.parent = parent;
+            this.in = in;
+        }
+        @Override
+        public int read() throws IOException {
+            byte[] a = new byte[1];
+            int n = read(a, 0, 1);
+            return (n > 0) ? (a[0] & 0xff) : -1;
+        }
+        @Override
+        public int read(byte b[], int off, int len) throws IOException {
+            return in.read(b, off, len);
+        }
+        @Override
+        public int available() throws IOException {
+            return in.available();
+        }
+        @Override
+        public void close() throws IOException {
+            parent.close();
+        }
     }
 
     /**
@@ -946,18 +1005,44 @@
             throw new SocketException("Socket is not connected");
         if (isOutputShutdown())
             throw new SocketException("Socket output is shutdown");
-        OutputStream os = null;
-        try {
-            os = AccessController.doPrivileged(
-                new PrivilegedExceptionAction<>() {
-                    public OutputStream run() throws IOException {
-                        return impl.getOutputStream();
-                    }
-                });
-        } catch (java.security.PrivilegedActionException e) {
-            throw (IOException) e.getException();
+        OutputStream out = this.out;
+        if (out == null) {
+            // wrap the output stream so that the close method closes this socket
+            out = new SocketOutputStream(this, impl.getOutputStream());
+            if (!OUT.compareAndSet(this, null, out)) {
+                out = this.out;
+            }
         }
-        return os;
+        return out;
+    }
+
+    /**
+     * An OutputStream that delegates write operations to an underlying output
+     * stream. The close method is overridden to close the Socket.
+     *
+     * This class is instrumented by Java Flight Recorder (JFR) to get socket
+     * I/O events.
+     */
+    private static class SocketOutputStream extends OutputStream {
+        private final Socket parent;
+        private final OutputStream out;
+        SocketOutputStream(Socket parent, OutputStream out) {
+            this.parent = parent;
+            this.out = out;
+        }
+        @Override
+        public void write(int b) throws IOException {
+            byte[] a = new byte[] { (byte) b };
+            write(a, 0, 1);
+        }
+        @Override
+        public void write(byte b[], int off, int len) throws IOException {
+            out.write(b, off, len);
+        }
+        @Override
+        public void close() throws IOException {
+            parent.close();
+        }
     }
 
     /**
@@ -1644,7 +1729,11 @@
     /**
      * The factory for all client sockets.
      */
-    private static SocketImplFactory factory = null;
+    private static volatile SocketImplFactory factory;
+
+    static SocketImplFactory socketImplFactory() {
+        return factory;
+    }
 
     /**
      * Sets the client socket implementation factory for the
--- a/src/java.base/share/classes/java/net/SocketImpl.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1995, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1995, 2019, 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
@@ -31,6 +31,8 @@
 import java.io.FileDescriptor;
 import java.util.Set;
 
+import sun.net.PlatformSocketImpl;
+
 /**
  * The abstract class {@code SocketImpl} is a common superclass
  * of all classes that actually implement sockets. It is used to
@@ -43,6 +45,15 @@
  * @since   1.0
  */
 public abstract class SocketImpl implements SocketOptions {
+
+    /**
+     * Creates an instance of platform's SocketImpl
+     */
+    @SuppressWarnings("unchecked")
+    static <S extends SocketImpl & PlatformSocketImpl> S createPlatformSocketImpl(boolean server) {
+        return (S) new PlainSocketImpl();
+    }
+
     /**
      * The actual Socket object.
      */
@@ -178,6 +189,15 @@
     protected abstract void close() throws IOException;
 
     /**
+     * Closes this socket, ignoring any IOException that is thrown by close.
+     */
+    void closeQuietly() {
+        try {
+            close();
+        } catch (IOException ignore) { }
+    }
+
+    /**
      * Places the input stream for this socket at "end of stream".
      * Any data sent to this socket is acknowledged and then
      * silently discarded.
@@ -306,7 +326,8 @@
             ",port=" + getPort() + ",localport=" + getLocalPort()  + "]";
     }
 
-    void reset() throws IOException {
+    void reset() {
+        fd = null;
         address = null;
         port = 0;
         localport = 0;
@@ -443,6 +464,19 @@
         }
     }
 
+    /**
+     * Attempts to copy socket options from this SocketImpl to a target SocketImpl.
+     * At this time, only the SO_TIMEOUT make sense to copy.
+     */
+    void copyOptionsTo(SocketImpl target) {
+        try {
+            Object timeout = getOption(SocketOptions.SO_TIMEOUT);
+            if (timeout instanceof Integer) {
+                target.setOption(SocketOptions.SO_TIMEOUT, timeout);
+            }
+        } catch (IOException ignore) { }
+    }
+
     private static final Set<SocketOption<?>> socketOptions;
 
     private static final Set<SocketOption<?>> serverSocketOptions;
--- a/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/java.base/share/classes/java/net/SocksSocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2000, 2019, 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
@@ -23,6 +23,7 @@
  * questions.
  */
 package java.net;
+
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
@@ -36,11 +37,9 @@
 
 /**
  * SOCKS (V4 & V5) TCP socket implementation (RFC 1928).
- * This is a subclass of PlainSocketImpl.
- * Note this class should <b>NOT</b> be public.
  */
 
-class SocksSocketImpl extends PlainSocketImpl implements SocksConsts {
+class SocksSocketImpl extends DelegatingSocketImpl implements SocksConsts {
     private String server = null;
     private int serverPort = DEFAULT_PORT;
     private InetSocketAddress external_address;
@@ -49,11 +48,12 @@
     private InputStream cmdIn = null;
     private OutputStream cmdOut = null;
 
-    SocksSocketImpl() {
-        // Nothing needed
+    SocksSocketImpl(SocketImpl delegate) {
+        super(delegate);
     }
 
-    SocksSocketImpl(Proxy proxy) {
+    SocksSocketImpl(Proxy proxy, SocketImpl delegate) {
+        super(delegate);
         SocketAddress a = proxy.address();
         if (a instanceof InetSocketAddress) {
             InetSocketAddress ad = (InetSocketAddress) a;
@@ -75,7 +75,7 @@
     private synchronized void privilegedConnect(final String host,
                                               final int port,
                                               final int timeout)
-         throws IOException
+        throws IOException
     {
         try {
             AccessController.doPrivileged(
@@ -94,7 +94,7 @@
 
     private void superConnectServer(String host, int port,
                                     int timeout) throws IOException {
-        super.connect(new InetSocketAddress(host, port), timeout);
+        delegate.connect(new InetSocketAddress(host, port), timeout);
     }
 
     private static int remainingMillis(long deadlineMillis) throws IOException {
@@ -111,16 +111,23 @@
     private int readSocksReply(InputStream in, byte[] data, long deadlineMillis) throws IOException {
         int len = data.length;
         int received = 0;
-        while (received < len) {
-            int count;
-            try {
-                count = ((SocketInputStream)in).read(data, received, len - received, remainingMillis(deadlineMillis));
-            } catch (SocketTimeoutException e) {
-                throw new SocketTimeoutException("Connect timed out");
+        int originalTimeout = (int) getOption(SocketOptions.SO_TIMEOUT);
+        try {
+            while (received < len) {
+                int count;
+                int remaining = remainingMillis(deadlineMillis);
+                setOption(SocketOptions.SO_TIMEOUT, remaining);
+                try {
+                    count = in.read(data, received, len - received);
+                } catch (SocketTimeoutException e) {
+                    throw new SocketTimeoutException("Connect timed out");
+                }
+                if (count < 0)
+                    throw new SocketException("Malformed reply from SOCKS server");
+                received += count;
             }
-            if (count < 0)
-                throw new SocketException("Malformed reply from SOCKS server");
-            received += count;
+        } finally {
+            setOption(SocketOptions.SO_TIMEOUT, originalTimeout);
         }
         return received;
     }
@@ -239,6 +246,27 @@
         }
     }
 
+    @Override
+    protected void connect(String host, int port) throws IOException {
+        connect(new InetSocketAddress(host, port), 0);
+    }
+
+    @Override
+    protected void connect(InetAddress address, int port) throws IOException {
+        connect(new InetSocketAddress(address, port), 0);
+    }
+
+    @Override
+    void setSocket(Socket soc) {
+        delegate.socket = soc;
+        super.setSocket(soc);
+    }
+
+    @Override
+    void setServerSocket(ServerSocket soc) {
+        throw new InternalError("should not get here");
+    }
+
     /**
      * Connects the Socks Socket to the specified endpoint. It will first
      * connect to the SOCKS proxy and negotiate the access. If the proxy
@@ -290,7 +318,7 @@
                 /*
                  * No default proxySelector --> direct connection
                  */
-                super.connect(epoint, remainingMillis(deadlineMillis));
+                delegate.connect(epoint, remainingMillis(deadlineMillis));
                 return;
             }
             URI uri;
@@ -313,13 +341,13 @@
             java.util.Iterator<Proxy> iProxy = null;
             iProxy = sel.select(uri).iterator();
             if (iProxy == null || !(iProxy.hasNext())) {
-                super.connect(epoint, remainingMillis(deadlineMillis));
+                delegate.connect(epoint, remainingMillis(deadlineMillis));
                 return;
             }
             while (iProxy.hasNext()) {
                 p = iProxy.next();
                 if (p == null || p.type() != Proxy.Type.SOCKS) {
-                    super.connect(epoint, remainingMillis(deadlineMillis));
+                    delegate.connect(epoint, remainingMillis(deadlineMillis));
                     return;
                 }
 
@@ -509,7 +537,15 @@
         external_address = epoint;
     }
 
+    @Override
+    protected void listen(int backlog) {
+        throw new InternalError("should not get here");
+    }
 
+    @Override
+    protected void accept(SocketImpl s) {
+        throw new InternalError("should not get here");
+    }
 
     /**
      * Returns the value of this socket's {@code address} field.
@@ -522,7 +558,7 @@
         if (external_address != null)
             return external_address.getAddress();
         else
-            return super.getInetAddress();
+            return delegate.getInetAddress();
     }
 
     /**
@@ -536,7 +572,7 @@
         if (external_address != null)
             return external_address.getPort();
         else
-            return super.getPort();
+            return delegate.getPort();
     }
 
     @Override
@@ -544,10 +580,15 @@
         if (cmdsock != null)
             cmdsock.close();
         cmdsock = null;
-        super.close();
+        delegate.close();
     }
 
     private String getUserName() {
         return StaticProperty.userName();
     }
+
+    @Override
+    void reset() {
+        throw new InternalError("should not get here");
+    }
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/java.base/share/classes/sun/net/PlatformSocketImpl.java	Sat Mar 16 19:44:12 2019 +0000
@@ -0,0 +1,32 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.  Oracle designates this
+ * particular file as subject to the "Classpath" exception as provided
+ * by Oracle in the LICENSE file that accompanied this code.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+package sun.net;
+
+/**
+ * Implemented by the platform's SocketImpl implementations.
+ */
+
+public interface PlatformSocketImpl {
+}
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketInputStreamInstrumentor.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketInputStreamInstrumentor.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2019, 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,15 +27,14 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Socket;
 
 import jdk.jfr.events.SocketReadEvent;
 
 /**
  * See {@link JITracer} for an explanation of this code.
  */
-@JIInstrumentationTarget("java.net.SocketInputStream")
-@JITypeMapping(from = "jdk.jfr.internal.instrument.SocketInputStreamInstrumentor$AbstractPlainSocketImpl",
-            to = "java.net.AbstractPlainSocketImpl")
+@JIInstrumentationTarget("java.net.Socket$SocketInputStream")
 final class SocketInputStreamInstrumentor {
 
     private SocketInputStreamInstrumentor() {
@@ -43,30 +42,28 @@
 
     @SuppressWarnings("deprecation")
     @JIInstrumentationMethod
-    int read(byte b[], int off, int length, int timeout) throws IOException {
+    public int read(byte b[], int off, int length) throws IOException {
         SocketReadEvent event = SocketReadEvent.EVENT.get();
         if (!event.isEnabled()) {
-            return read(b, off, length, timeout);
+            return read(b, off, length);
         }
         int bytesRead = 0;
         try {
             event.begin();
-            bytesRead = read(b, off, length, timeout);
+            bytesRead = read(b, off, length);
         } finally {
             event.end();
             if (event.shouldCommit()) {
-                String hostString  = impl.address.toString();
-                int delimiterIndex = hostString.lastIndexOf('/');
-
-                event.host      = hostString.substring(0, delimiterIndex);
-                event.address   = hostString.substring(delimiterIndex + 1);
-                event.port      = impl.port;
+                InetAddress remote = parent.getInetAddress();
+                event.host = remote.getHostName();
+                event.address = remote.getHostAddress();
+                event.port = parent.getPort();
                 if (bytesRead < 0) {
                     event.endOfStream = true;
                 } else {
                     event.bytesRead = bytesRead;
                 }
-                event.timeout   = timeout;
+                event.timeout = parent.getSoTimeout();
 
                 event.commit();
                 event.reset();
@@ -75,14 +72,6 @@
         return bytesRead;
     }
 
-    private AbstractPlainSocketImpl impl = null;
-
-    void silenceFindBugsUnwrittenField(InetAddress dummy) {
-        impl.address = dummy;
-    }
-
-    static class AbstractPlainSocketImpl {
-        InetAddress address;
-        int port;
-    }
+    // private field in java.net.Socket$SocketInputStream
+    private Socket parent;
 }
--- a/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketOutputStreamInstrumentor.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/src/jdk.jfr/share/classes/jdk/jfr/internal/instrument/SocketOutputStreamInstrumentor.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2013, 2019, 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,15 +27,14 @@
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.net.Socket;
 
 import jdk.jfr.events.SocketWriteEvent;
 
 /**
  * See {@link JITracer} for an explanation of this code.
  */
-@JIInstrumentationTarget("java.net.SocketOutputStream")
-@JITypeMapping(from = "jdk.jfr.internal.instrument.SocketOutputStreamInstrumentor$AbstractPlainSocketImpl",
-            to = "java.net.AbstractPlainSocketImpl")
+@JIInstrumentationTarget("java.net.Socket$SocketOutputStream")
 final class SocketOutputStreamInstrumentor {
 
     private SocketOutputStreamInstrumentor() {
@@ -43,27 +42,25 @@
 
     @SuppressWarnings("deprecation")
     @JIInstrumentationMethod
-    private void socketWrite(byte b[], int off, int len) throws IOException {
+    public void write(byte b[], int off, int len) throws IOException {
         SocketWriteEvent event = SocketWriteEvent.EVENT.get();
         if (!event.isEnabled()) {
-            socketWrite(b, off, len);
+            write(b, off, len);
             return;
         }
         int bytesWritten = 0;
         try {
             event.begin();
-            socketWrite(b, off, len);
+            write(b, off, len);
             bytesWritten = len;
         } finally {
             event.end() ;
             if (event.shouldCommit()) {
-                String hostString  = impl.address.toString();
-                int delimiterIndex = hostString.lastIndexOf('/');
-
-                event.host         = hostString.substring(0, delimiterIndex);
-                event.address      = hostString.substring(delimiterIndex + 1);
-                event.port         = impl.port;
-                event.bytesWritten = bytesWritten < 0 ? 0 : bytesWritten;
+                InetAddress remote = parent.getInetAddress();
+                event.host = remote.getHostName();
+                event.address = remote.getHostAddress();
+                event.port = parent.getPort();
+                event.bytesWritten = bytesWritten;
 
                 event.commit();
                 event.reset();
@@ -71,14 +68,6 @@
         }
     }
 
-    private AbstractPlainSocketImpl impl = null;
-
-    void silenceFindBugsUnwrittenField(InetAddress dummy) {
-        impl.address = dummy;
-    }
-
-    static class AbstractPlainSocketImpl {
-        InetAddress address;
-        int port;
-    }
+    // private field in java.net.Socket$SocketOutputStream
+    private Socket parent;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/jdk/java/net/SocketImpl/SocketImplCombinations.java	Sat Mar 16 19:44:12 2019 +0000
@@ -0,0 +1,844 @@
+/*
+ * Copyright (c) 2019, 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
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 8220493
+ * @modules java.base/java.net:+open java.base/sun.nio.ch:+open
+ * @run testng/othervm SocketImplCombinations
+ * @summary Test Socket and ServerSocket with combinations of SocketImpls
+ */
+
+import java.io.FileDescriptor;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.lang.reflect.Field;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.net.ServerSocket;
+import java.net.Socket;
+import java.net.SocketAddress;
+import java.net.SocketImpl;
+import java.net.SocketImplFactory;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.SocketChannel;
+import java.util.function.BiConsumer;
+
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+@Test
+public class SocketImplCombinations {
+
+    /**
+     * Test creating an unconnected Socket, it should be created with a platform SocketImpl.
+     */
+    public void testNewSocket1() throws IOException {
+        try (Socket s = new Socket()) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isSocksSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        }
+    }
+
+    /**
+     * Test creating a connected Socket, it should be created with a platform SocketImpl.
+     */
+    public void testNewSocket2() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            try (Socket s = new Socket(ss.getInetAddress(), ss.getLocalPort())) {
+                SocketImpl si = getSocketImpl(s);
+                assertTrue(isSocksSocketImpl(si));
+                SocketImpl delegate = getDelegate(si);
+                assertTrue(isPlatformSocketImpl(delegate));
+            }
+        }
+    }
+
+    /**
+     * Test creating a Socket for a DIRECT connection, it should be created with a
+     * platform SocketImpl.
+     */
+    public void testNewSocket3() throws IOException {
+        try (Socket s = new Socket(Proxy.NO_PROXY)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    /**
+     * Test creating a Socket for a SOCKS connection, it should be created with a
+     * SOCKS SocketImpl.
+     */
+    public void testNewSocket4() throws IOException {
+        var address = new InetSocketAddress("127.0.0.1", 1080);
+        var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
+        try (Socket s = new Socket(socksProxy)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isSocksSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        }
+    }
+
+    /**
+     * Test creating a Socket for a HTTP proxy connection, it should be created with
+     * a HTTP proxy SocketImpl.
+     */
+    public void testNewSocket5() throws IOException {
+        var address = new InetSocketAddress("127.0.0.1", 8080);
+        var httpProxy = new Proxy(Proxy.Type.HTTP, address);
+        try (Socket s = new Socket(httpProxy)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isHttpConnectSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        }
+    }
+
+    /**
+     * Test creating a Socket no SocketImpl. A platform SocketImpl should be
+     * created lazily.
+     */
+    public void testNewSocket6() throws IOException {
+        Socket s = new Socket((SocketImpl) null) { };
+        try (s) {
+            assertTrue(getSocketImpl(s) == null);
+            s.bind(new InetSocketAddress(0));   // force SocketImpl to be created
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isSocksSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        }
+    }
+
+    /**
+     * Test creating a Socket with a custom SocketImpl.
+     */
+    public void testNewSocket7() throws IOException {
+        Socket s = new Socket(new CustomSocketImpl(false)) { };
+        try (s) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(si instanceof CustomSocketImpl);
+        }
+    }
+
+    /**
+     * Test creating a Socket when there is a SocketImplFactory set.
+     */
+    public void testNewSocket8() throws IOException {
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try (Socket s = new Socket()) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(si instanceof CustomSocketImpl);
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating a Socket for a DIRECT connection when there is a
+     * SocketImplFactory set.
+     */
+    public void testNewSocket9() throws IOException {
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try (Socket s = new Socket(Proxy.NO_PROXY)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(si instanceof CustomSocketImpl);
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating a Socket for a SOCKS connection when there is a
+     * SocketImplFactory set.
+     */
+    public void testNewSocket10() throws IOException {
+        var address = new InetSocketAddress("127.0.0.1", 1080);
+        var socksProxy = new Proxy(Proxy.Type.SOCKS, address);
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try (Socket s = new Socket(socksProxy)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isSocksSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating a Socket for a HTTP proxy connection when there is a
+     * SocketImplFactory set.
+     */
+    public void testNewSocket11() throws IOException {
+        var address = new InetSocketAddress("127.0.0.1", 8080);
+        var httpProxy = new Proxy(Proxy.Type.HTTP, address);
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try (Socket s = new Socket(httpProxy)) {
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isHttpConnectSocketImpl(si));
+            SocketImpl delegate = getDelegate(si);
+            assertTrue(isPlatformSocketImpl(delegate));
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating a Socket no SocketImpl when there is a SocketImplFactory set.
+     */
+    public void testNewSocket12() throws IOException {
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try {
+            Socket s = new Socket((SocketImpl) null) { };
+            try (s) {
+                assertTrue(getSocketImpl(s) == null);
+                s.bind(new InetSocketAddress(0));   // force SocketImpl to be created
+                assertTrue(getSocketImpl(s) instanceof CustomSocketImpl);
+            }
+        } finally {
+            setSocketSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating an unbound ServerSocket, it should be created with a platform
+     * SocketImpl.
+     */
+    public void testNewServerSocket1() throws IOException {
+        try (ServerSocket ss = new ServerSocket()) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    /**
+     * Test creating a bound ServerSocket, it should be created with a platform
+     * SocketImpl.
+     */
+    public void testNewServerSocket2() throws IOException {
+        try (ServerSocket ss = new ServerSocket(0)) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(isPlatformSocketImpl(si));
+        }
+    }
+
+    /**
+     * Test creating a ServerSocket with a custom SocketImpl.
+     */
+    public void testNewServerSocket3() throws IOException {
+        ServerSocket ss = new ServerSocket(new CustomSocketImpl(true)) { };
+        try (ss) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(si instanceof CustomSocketImpl);
+        }
+    }
+
+    /**
+     * Test creating an unbound ServerSocket when there is a SocketImplFactory set.
+     */
+    public void testNewServerSocket4() throws IOException {
+        setServerSocketImplFactory(() -> new CustomSocketImpl(true));
+        try (ServerSocket ss = new ServerSocket()) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(si instanceof CustomSocketImpl);
+        } finally {
+            setServerSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test creating a bound ServerSocket when there is a SocketImplFactory set.
+     */
+    public void testNewServerSocket5() throws IOException {
+        setServerSocketImplFactory(() -> new CustomSocketImpl(true));
+        try (ServerSocket ss = new ServerSocket(0)) {
+            SocketImpl si = getSocketImpl(ss);
+            assertTrue(si instanceof CustomSocketImpl);
+        } finally {
+            setServerSocketImplFactory(null);
+        }
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
+     * the Socket to accept is created with no SocketImpl.
+     */
+    public void testServerSocketAccept1() throws IOException {
+        var socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        serverSocketAccept(socket, (ss, s) -> {
+            assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
+            assertTrue(s == socket);
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isPlatformSocketImpl(si));
+            checkFields(si);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
+     * the Socket to accept is created with no SocketImpl, and there is a custom
+     * client SocketImplFactory set.
+     */
+    public void testServerSocketAccept2() throws IOException {
+        var socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        serverSocketAccept(socket, () -> new CustomSocketImpl(false), (ss, s) -> {
+            assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
+            assertTrue(s == socket);
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(isPlatformSocketImpl(si));
+            checkFields(si);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
+     * the Socket to accept is created with a SocketImpl that delegates to a
+     * platform SocketImpl.
+     */
+    public void testServerSocketAccept3() throws IOException {
+        var socket = new Socket();
+        SocketImpl si = getSocketImpl(socket);
+        assertTrue(isSocksSocketImpl(si));
+        SocketImpl delegate = getDelegate(si);
+        assertTrue(isPlatformSocketImpl(delegate));
+
+        serverSocketAccept(socket, (ss, s) -> {
+            assertTrue(isPlatformSocketImpl(getSocketImpl(ss)));
+            assertTrue(s == socket);
+            SocketImpl psi = getSocketImpl(socket);
+            assertTrue(isPlatformSocketImpl(psi));
+            checkFields(psi);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a platform SocketImpl,
+     * the Socket to accept is created with a custom SocketImpl.
+     */
+    public void testServerSocketAccept4a() throws IOException {
+        SocketImpl clientImpl = new CustomSocketImpl(false);
+        Socket socket = new Socket(clientImpl) { };
+        assertTrue(getSocketImpl(socket) == clientImpl);
+
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            socket.close();
+        }
+    }
+
+    public void testServerSocketAccept4b() throws IOException {
+        SocketImpl clientImpl = new CustomSocketImpl(false);
+        Socket socket = new Socket(clientImpl) { };
+        assertTrue(getSocketImpl(socket) == clientImpl);
+
+        setSocketSocketImplFactory(() -> new CustomSocketImpl(false));
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            setSocketSocketImplFactory(null);
+            socket.close();
+        }
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
+     * the Socket to accept is created no SocketImpl.
+     */
+    public void testServerSocketAccept5a() throws IOException {
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        try (ServerSocket ss = new ServerSocket(serverImpl) { }) {
+            ss.bind(new InetSocketAddress(0));
+            expectThrows(IOException.class, ss::accept);
+        }
+    }
+
+    public void testServerSocketAccept5b() throws IOException {
+        var socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            socket.close();
+        }
+    }
+
+    public void testServerSocketAccept5c() throws IOException {
+        setServerSocketImplFactory(() -> new CustomSocketImpl(true));
+        try (ServerSocket ss = new ServerSocket(0)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            setServerSocketImplFactory(null);
+        }
+    }
+
+    public void testServerSocketAccept5d() throws IOException {
+        var socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        setServerSocketImplFactory(() -> new CustomSocketImpl(true));
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            setServerSocketImplFactory(null);
+            socket.close();
+        }
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
+     * the Socket to accept is created with no SocketImpl, and there is a custom
+     * client SocketImplFactory set.
+     */
+    public void testServerSocketAccept6() throws Exception {
+        var socket = new Socket((SocketImpl) null) { };
+        assertTrue(getSocketImpl(socket) == null);
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
+        serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
+            assertTrue(getSocketImpl(ss) == serverImpl);
+            SocketImpl si = getSocketImpl(s);
+            assertTrue(si instanceof CustomSocketImpl);
+            checkFields(si);
+        });
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
+     * the Socket to accept is created with a SocketImpl that delegates to a
+     * platform SocketImpl.
+     */
+    public void testServerSocketAccept7a() throws IOException {
+        var socket = new Socket();
+        SocketImpl si = getSocketImpl(socket);
+        assertTrue(isSocksSocketImpl(si));
+        SocketImpl delegate = getDelegate(si);
+        assertTrue(isPlatformSocketImpl(delegate));
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        try (ServerSocket ss = serverSocketToAccept(serverImpl, socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            socket.close();
+        }
+    }
+
+    public void testServerSocketAccept7b() throws IOException {
+        var socket = new Socket();
+        SocketImpl si = getSocketImpl(socket);
+        assertTrue(isSocksSocketImpl(si));
+        SocketImpl delegate = getDelegate(si);
+        assertTrue(isPlatformSocketImpl(delegate));
+
+        setServerSocketImplFactory(() -> new CustomSocketImpl(true));
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            expectThrows(IOException.class, ss::accept);
+        } finally {
+            setServerSocketImplFactory(null);
+            socket.close();
+        }
+    }
+
+    /**
+     * Test ServerSocket.accept. The ServerSocket uses a custom SocketImpl,
+     * the Socket to accept is created with a custom SocketImpl.
+     */
+    public void testServerSocketAccept8() throws Exception {
+        SocketImpl clientImpl = new CustomSocketImpl(false);
+        Socket socket = new Socket(clientImpl) { };
+        assertTrue(getSocketImpl(socket) == clientImpl);
+
+        SocketImpl serverImpl = new CustomSocketImpl(true);
+        SocketImplFactory clientFactory = () -> new CustomSocketImpl(false);
+        serverSocketAccept(serverImpl, socket, clientFactory, (ss, s) -> {
+            assertTrue(getSocketImpl(ss) == serverImpl);
+            assertTrue(getSocketImpl(s) == clientImpl);
+            checkFields(clientImpl);
+        });
+    }
+
+    /**
+     * Creates a ServerSocket that returns the given Socket from accept.
+     * The consumer is invoked with the server socket and the accepted socket.
+     */
+    static void serverSocketAccept(Socket socket,
+                                   BiConsumer<ServerSocket, Socket> consumer)
+        throws IOException
+    {
+        Socket s1 = null;
+        Socket s2 = null;
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
+            s2 = ss.accept();
+            consumer.accept(ss, s2);
+        } finally {
+            if (s1 != null) s1.close();
+            if (s2 != null) s2.close();
+        }
+    }
+
+    /**
+     * Creates a ServerSocket that returns the given Socket from accept. The
+     * given SocketImplFactory is set during the accept and the consumer is
+     * invoked when the server socket and the accepted socket.
+     */
+    static void serverSocketAccept(Socket socket,
+                                   SocketImplFactory factory,
+                                   BiConsumer<ServerSocket, Socket> consumer)
+        throws IOException
+    {
+        Socket s1 = null;
+        Socket s2 = null;
+        try (ServerSocket ss = serverSocketToAccept(socket)) {
+            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
+            setSocketSocketImplFactory(factory);
+            try {
+                s2 = ss.accept();
+            } finally {
+                setSocketSocketImplFactory(null);
+            }
+            consumer.accept(ss, s2);
+        } finally {
+            if (s1 != null) s1.close();
+            if (s2 != null) s2.close();
+        }
+    }
+
+    /**
+     * Creates a ServerSocket with a SocketImpl returns the given Socket from
+     * accept. The given SocketImplFactory is set during the accept and the
+     * consumer is invoked when the server socket and the accepted socket.
+     */
+    static void serverSocketAccept(SocketImpl impl,
+                                   Socket socket,
+                                   SocketImplFactory factory,
+                                   BiConsumer<ServerSocket, Socket> consumer)
+        throws IOException
+    {
+        Socket s1 = null;
+        Socket s2 = null;
+        try (ServerSocket ss = serverSocketToAccept(impl, socket)) {
+            s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
+            setSocketSocketImplFactory(factory);
+            try {
+                s2 = ss.accept();
+            } finally {
+                setSocketSocketImplFactory(null);
+            }
+            consumer.accept(ss, s2);
+        } finally {
+            if (s1 != null) s1.close();
+            if (s2 != null) s2.close();
+        }
+    }
+
+    /**
+     * Creates a ServerSocket that returns the given Socket from accept.
+     */
+    static ServerSocket serverSocketToAccept(Socket s) throws IOException {
+        return new ServerSocket(0) {
+            @Override
+            public Socket accept() throws IOException {
+                implAccept(s);
+                return s;
+            }
+        };
+    }
+
+    /**
+     * Creates a ServerSocket with a SocketImpl that returns the given Socket
+     * from accept.
+     */
+    static ServerSocket serverSocketToAccept(SocketImpl impl, Socket s) throws IOException {
+        ServerSocket ss = new ServerSocket(impl) {
+            @Override
+            public Socket accept() throws IOException {
+                implAccept(s);
+                return s;
+            }
+        };
+        ss.bind(new InetSocketAddress(0));
+        return ss;
+    }
+
+    /**
+     * Returns the socket's SocketImpl
+     */
+    static SocketImpl getSocketImpl(Socket s) {
+        try {
+            Field f = Socket.class.getDeclaredField("impl");
+            f.setAccessible(true);
+            return (SocketImpl) f.get(s);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns the server socket's SocketImpl
+     */
+    static SocketImpl getSocketImpl(ServerSocket ss) {
+        try {
+            Field f = ServerSocket.class.getDeclaredField("impl");
+            f.setAccessible(true);
+            return (SocketImpl) f.get(ss);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns the SocketImpl that the given SocketImpl delegates to
+     */
+    static SocketImpl getDelegate(SocketImpl si) {
+        try {
+            Class<?> clazz = Class.forName("java.net.DelegatingSocketImpl");
+            Field f = clazz.getDeclaredField("delegate");
+            f.setAccessible(true);
+            return (SocketImpl) f.get(si);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns the value of a SocketImpl field
+     */
+    static <T> T get(SocketImpl si, String name) {
+        try {
+            Field f = SocketImpl.class.getDeclaredField(name);
+            f.setAccessible(true);
+            return (T) f.get(si);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Sets the value of SocketImpl field
+     */
+    static void set(SocketImpl si, String name, Object value) {
+        try {
+            Field f = SocketImpl.class.getDeclaredField(name);
+            f.setAccessible(true);
+            f.set(si, value);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns true if the SocketImpl is a PlatformSocketImpl
+     */
+    static boolean isPlatformSocketImpl(SocketImpl si) {
+        try {
+            Class<?> clazz = Class.forName("sun.net.PlatformSocketImpl");
+            return clazz.isInstance(si);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns true if the SocketImpl is a SocksSocketImpl
+     */
+    static boolean isSocksSocketImpl(SocketImpl si) {
+        try {
+            Class<?> clazz = Class.forName("java.net.SocksSocketImpl");
+            return clazz.isInstance(si);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Returns true if the SocketImpl is a HttpConnectSocketImpl
+     */
+    static boolean isHttpConnectSocketImpl(SocketImpl si) {
+        try {
+            Class<?> clazz = Class.forName("java.net.HttpConnectSocketImpl");
+            return clazz.isInstance(si);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Socket.setSocketImplFactory(SocketImplFactory)
+     */
+    static void setSocketSocketImplFactory(SocketImplFactory factory) {
+        try {
+            Field f = Socket.class.getDeclaredField("factory");
+            f.setAccessible(true);
+            f.set(null, factory);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * ServerSocket.setSocketFactory(SocketImplFactory)
+     */
+    static void setServerSocketImplFactory(SocketImplFactory factory) {
+        try {
+            Field f = ServerSocket.class.getDeclaredField("factory");
+            f.setAccessible(true);
+            f.set(null, factory);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * Checks the 4 protected fields of a SocketImpl to make sure that they
+     * have been initialized.
+     */
+    static void checkFields(SocketImpl si) {
+        FileDescriptor fd = get(si, "fd");
+        InetAddress address = get(si, "address");
+        int port = get(si, "port");
+        int localport = get(si, "localport");
+        assertTrue(fd.valid() && address != null && port != 0 && localport != 0);
+    }
+
+    /**
+     * Custom SocketImpl that is layed on a SocketChannel or ServerSocketChannel
+     */
+    static class CustomSocketImpl extends SocketImpl {
+        private final boolean server;
+        private ServerSocketChannel ssc;
+        private SocketChannel sc;
+
+        CustomSocketImpl(boolean server) {
+            this.server = server;
+        }
+
+        @Override
+        protected void create(boolean stream) throws IOException {
+            if (server) {
+                ssc = ServerSocketChannel.open();
+            } else {
+                sc = SocketChannel.open();
+            }
+        }
+
+        @Override
+        protected void connect(String host, int port) throws IOException {
+            connect(new InetSocketAddress(host, port), 0);
+        }
+
+        @Override
+        protected void connect(InetAddress address, int port) throws IOException {
+            connect(new InetSocketAddress(address, port), 0);
+        }
+
+        @Override
+        protected void connect(SocketAddress remote, int timeout) throws IOException {
+            sc.connect(remote);
+            super.address = ((InetSocketAddress) remote).getAddress();
+            super.port = ((InetSocketAddress) remote).getPort();
+        }
+
+        @Override
+        protected void bind(InetAddress address, int port) throws IOException {
+            if (server) {
+                ssc.bind(new InetSocketAddress(address, port));
+                super.localport = ssc.socket().getLocalPort();
+            } else {
+                sc.bind(new InetSocketAddress(address, port));
+                super.localport = sc.socket().getLocalPort();
+            }
+            super.address = address;
+        }
+
+        @Override
+        protected void listen(int backlog) {
+            // do nothing
+        }
+
+        @Override
+        protected void accept(SocketImpl si) throws IOException {
+            SocketChannel peer = ssc.accept();
+            FileDescriptor fd;
+            try {
+                Class<?> clazz = Class.forName("sun.nio.ch.SocketChannelImpl");
+                Field f = clazz.getDeclaredField("fd");
+                f.setAccessible(true);
+                fd = (FileDescriptor) f.get(peer);
+            } catch (Exception e) {
+                throw new RuntimeException(e);
+            }
+            set(si, "fd", fd);
+            set(si, "address", peer.socket().getInetAddress());
+            set(si, "port", peer.socket().getPort());
+            set(si, "localport", peer.socket().getLocalPort());
+        }
+
+        @Override
+        protected InputStream getInputStream() {
+            throw new RuntimeException();
+        }
+
+        @Override
+        protected OutputStream getOutputStream() {
+            throw new RuntimeException();
+        }
+
+        @Override
+        protected int available() {
+            return 0;
+        }
+
+        @Override
+        protected void close() {
+        }
+
+        @Override
+        protected void sendUrgentData(int data) {
+            throw new RuntimeException();
+        }
+
+        @Override
+        public void setOption(int option, Object value) {
+            throw new RuntimeException();
+        }
+
+        @Override
+        public Object getOption(int option) {
+            throw new RuntimeException();
+        }
+    }
+}
--- a/test/jdk/jdk/jfr/event/io/TestInstrumentation.java	Sat Mar 16 12:31:31 2019 +0000
+++ b/test/jdk/jdk/jfr/event/io/TestInstrumentation.java	Sat Mar 16 19:44:12 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, 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
@@ -105,14 +105,14 @@
         "java/io/FileOutputStream::write::(I)V",
         "java/io/FileOutputStream::write::([B)V",
         "java/io/FileOutputStream::write::([BII)V",
-        "java/net/SocketInputStream::read::()I",
-        "java/net/SocketInputStream::read::([B)I",
-        "java/net/SocketInputStream::read::([BII)I",
-        "java/net/SocketInputStream::close::()V",
-        "java/net/SocketOutputStream::write::(I)V",
-        "java/net/SocketOutputStream::write::([B)V",
-        "java/net/SocketOutputStream::write::([BII)V",
-        "java/net/SocketOutputStream::close::()V",
+        "java/net/Socket$SocketInputStream::read::()I",
+        "java/net/Socket$SocketInputStream::read::([B)I",
+        "java/net/Socket$SocketInputStream::read::([BII)I",
+        "java/net/Socket$SocketInputStream::close::()V",
+        "java/net/Socket$SocketOutputStream::write::(I)V",
+        "java/net/Socket$SocketOutputStream::write::([B)V",
+        "java/net/Socket$SocketOutputStream::write::([BII)V",
+        "java/net/Socket$SocketOutputStream::close::()V",
         "java/nio/channels/FileChannel::read::([Ljava/nio/ByteBuffer;)J",
         "java/nio/channels/FileChannel::write::([Ljava/nio/ByteBuffer;)J",
         "java/nio/channels/SocketChannel::open::()Ljava/nio/channels/SocketChannel;",