Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8025135

Low Socket Performance With Active SecurityManager

    XMLWordPrintable

    Details

      Description

      FULL PRODUCT VERSION :
      java version "1.7.0_40"
      Java(TM) SE Runtime Environment (build 1.7.0_40-b43)
      Java HotSpot(TM) Client VM (build 24.0-b56, mixed mode, sharing)

      ADDITIONAL OS VERSION INFORMATION :
      Microsoft Windows [Version 6.1.7601] (Socket Client)
      Microsoft Windows XP [Version 5.1.2600] (Socket Server)

      A DESCRIPTION OF THE PROBLEM :
      Open a socket (TCP/IP) with active SecurityManager from a Windows 7 machine to a Windows XP machine is very slow. Profiling with JVisualVM shows that resolving the host in Security Manager costs the most time (native java.netInet6AddressImpl.getHostByAddr()).

      The Security Manager must be activated at least on the Windows 7 (client) machine. Further more the

      permission java.net.SocketPermission "*", "accept,listen,connect,resolve";

      must be at least specified.

      REGRESSION. Last worked in version 6u43

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Start attached test code on a windows xp (Server) and windows 7(client) machine.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      A time lower two seconds.
      ACTUAL -
      12 seconds.

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      ++++++++++++ Windows 7 (Client Code) +++++++++++++++
      package test;

      import java.io.InputStream;
      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.Arrays;

      import sun.security.util.SecurityConstants;

      public class TestClient {
          public static void main(final String[] args) throws UnknownHostException, Exception {
              if (!"Windows 7".equalsIgnoreCase(System.getProperty("os.name"))) {
                  throw new IllegalStateException("Os is not Win 7.");
              }
              final SecurityManager sm = System.getSecurityManager();
              if (sm == null) {
                  throw new IllegalStateException("No Security Manager is configured.");
              }
              try {
                  sm.checkPermission(SecurityConstants.ALL_PERMISSION);
                  throw new IllegalStateException("Security file specifies all permission for code base.");
              } catch (final SecurityException e) {
              }
              if (args.length != 1) {
                  throw new IllegalArgumentException("No host is given as argument");
              }
              clientSendAnyData(args[0], 7001);
          }

          private static void clientSendAnyData(final String address, final int port) throws Exception {
              while (true) {
                  final byte ba[] = new byte[100];

                  final long time = System.currentTimeMillis();
                  try (final Socket socket = new Socket(address, port)) {
                      socket.getOutputStream().write("Hello World!".getBytes());
                      final InputStream sockIn = socket.getInputStream();
                      final int length = sockIn.read(ba);
                      if (ba[0] != 0 && ba[1] != 0) {
                          System.out.println("getData: " + new String(Arrays.copyOf(ba, length)));
                      }
                      socket.close();
                      final long l = System.currentTimeMillis() - time;
                      System.out.println("Socket call time:" + l
                              + "ms");
                      if (l > 2000) {
                          throw new IllegalStateException("Test failed socket call is to high " + l);
                      }
                  }
              }
          }
      }


      ++++++++++++ Windows XP (Server Code) ++++++++++++
      package test;

      import java.io.BufferedInputStream;
      import java.io.IOException;
      import java.io.InputStream;
      import java.net.ServerSocket;
      import java.net.Socket;
      import java.net.UnknownHostException;
      import java.util.Arrays;

      public class TestServer {
          public static void main(final String[] args) throws UnknownHostException, Exception {
              server(7001);
          }

          private static void server(final int port) throws IOException, Exception {
              if (!"Windows XP".equalsIgnoreCase(System.getProperty("os.name"))) {
                  throw new IllegalStateException("Os is not Win XP.");
              }
              try (final ServerSocket socket = new ServerSocket(port)) {
                  while (true) {
                      try (final Socket s = socket.accept()) {
                          final byte[] ba = new byte[100];
                          final InputStream inputStream = new BufferedInputStream(s.getInputStream());
                          final int length = inputStream.read(ba);
                          if (ba[0] != 0 && ba[1] != 0) {
                              System.out.println("getData: " + new String(Arrays.copyOf(ba, length)));
                          }
                          s.getOutputStream().write("I am a test server!".getBytes());
                      }
                  }
              }
          }
      }


      ++++++++++++ security.policy ++++++++++++
      grant codeBase "file:${java.home}/lib/ext/*" {
          permission java.security.AllPermission;
      };

      grant {
          permission java.net.SocketPermission "*", "accept,listen,connect,resolve";
      };
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Disable SecurityManager or grant all permission.

        Attachments

          Activity

            People

            Assignee:
            weijun Weijun Wang
            Reporter:
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Due:
              Created:
              Updated:
              Resolved: