Skip to content

Commit ae0d088

Browse files
cliveverghesePaul Hohensee
authored and
Paul Hohensee
committed
8270317: Large Allocation in CipherSuite
Backport-of: e627cae
1 parent c88867c commit ae0d088

File tree

2 files changed

+106
-60
lines changed

2 files changed

+106
-60
lines changed

src/java.base/share/classes/sun/security/ssl/CipherSuite.java

+48-60
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,8 @@
2525

2626
package sun.security.ssl;
2727

28-
import java.util.ArrayList;
29-
import java.util.Arrays;
30-
import java.util.Collection;
31-
import java.util.Collections;
32-
import java.util.LinkedList;
33-
import java.util.List;
28+
import java.util.*;
29+
3430
import static sun.security.ssl.CipherSuite.HashAlg.*;
3531
import static sun.security.ssl.CipherSuite.KeyExchange.*;
3632
import static sun.security.ssl.CipherSuite.MacAlg.*;
@@ -861,6 +857,39 @@ enum CipherSuite {
861857

862858
final boolean exportable;
863859

860+
private static final Map<Integer, CipherSuite> cipherSuiteIds;
861+
private static final Map<String, CipherSuite> cipherSuiteNames;
862+
private static final List<CipherSuite> allowedCipherSuites;
863+
private static final List<CipherSuite> defaultCipherSuites;
864+
865+
static {
866+
Map<Integer, CipherSuite> ids = new HashMap<>();
867+
Map<String, CipherSuite> names = new HashMap<>();
868+
List<CipherSuite> allowedCS = new ArrayList<>();
869+
List<CipherSuite> defaultCS = new ArrayList<>();
870+
871+
for(CipherSuite cs : CipherSuite.values()) {
872+
ids.put(cs.id, cs);
873+
names.put(cs.name, cs);
874+
for (String alias : cs.aliases) {
875+
names.put(alias, cs);
876+
}
877+
878+
if (!cs.supportedProtocols.isEmpty()) {
879+
allowedCS.add(cs);
880+
}
881+
882+
if (cs.isDefaultEnabled) {
883+
defaultCS.add(cs);
884+
}
885+
}
886+
887+
cipherSuiteIds = Map.copyOf(ids);
888+
cipherSuiteNames = Map.copyOf(names);
889+
allowedCipherSuites = List.copyOf(allowedCS);
890+
defaultCipherSuites = List.copyOf(defaultCS);
891+
}
892+
864893
// known but unsupported cipher suite
865894
private CipherSuite(String name, int id) {
866895
this(id, false, name, "",
@@ -898,62 +927,29 @@ private CipherSuite(int id, boolean isDefaultEnabled,
898927
}
899928

900929
static CipherSuite nameOf(String ciperSuiteName) {
901-
for (CipherSuite cs : CipherSuite.values()) {
902-
if (cs.name.equals(ciperSuiteName) ||
903-
cs.aliases.contains(ciperSuiteName)) {
904-
return cs;
905-
}
906-
}
907-
908-
return null;
930+
return cipherSuiteNames.get(ciperSuiteName);
909931
}
910932

911933
static CipherSuite valueOf(int id) {
912-
for (CipherSuite cs : CipherSuite.values()) {
913-
if (cs.id == id) {
914-
return cs;
915-
}
916-
}
917-
918-
return null;
934+
return cipherSuiteIds.get(id);
919935
}
920936

921937
static String nameOf(int id) {
922-
for (CipherSuite cs : CipherSuite.values()) {
923-
if (cs.id == id) {
924-
return cs.name;
925-
}
938+
CipherSuite cs = cipherSuiteIds.get(id);
939+
940+
if (cs != null) {
941+
return cs.name;
926942
}
927943

928944
return "UNKNOWN-CIPHER-SUITE(" + Utilities.byte16HexString(id) + ")";
929945
}
930946

931947
static Collection<CipherSuite> allowedCipherSuites() {
932-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
933-
for (CipherSuite cs : CipherSuite.values()) {
934-
if (!cs.supportedProtocols.isEmpty()) {
935-
cipherSuites.add(cs);
936-
} else {
937-
// values() is ordered, remaining cipher suites are
938-
// not supported.
939-
break;
940-
}
941-
}
942-
return cipherSuites;
948+
return allowedCipherSuites;
943949
}
944950

945951
static Collection<CipherSuite> defaultCipherSuites() {
946-
Collection<CipherSuite> cipherSuites = new LinkedList<>();
947-
for (CipherSuite cs : CipherSuite.values()) {
948-
if (cs.isDefaultEnabled) {
949-
cipherSuites.add(cs);
950-
} else {
951-
// values() is ordered, remaining cipher suites are
952-
// not enabled.
953-
break;
954-
}
955-
}
956-
return cipherSuites;
952+
return defaultCipherSuites;
957953
}
958954

959955
/**
@@ -976,19 +972,11 @@ static List<CipherSuite> validValuesOf(String[] names) {
976972
}
977973

978974
boolean found = false;
979-
for (CipherSuite cs : CipherSuite.values()) {
980-
if (!cs.supportedProtocols.isEmpty()) {
981-
if (cs.name.equals(name) ||
982-
cs.aliases.contains(name)) {
983-
cipherSuites.add(cs);
984-
found = true;
985-
break;
986-
}
987-
} else {
988-
// values() is ordered, remaining cipher suites are
989-
// not supported.
990-
break;
991-
}
975+
CipherSuite cs;
976+
if ((cs = cipherSuiteNames.get(name)) != null
977+
&& !cs.supportedProtocols.isEmpty()) {
978+
cipherSuites.add(cs);
979+
found = true;
992980
}
993981
if (!found) {
994982
throw new IllegalArgumentException(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
package org.openjdk.bench.java.security;
25+
26+
import org.openjdk.jmh.annotations.*;
27+
28+
import java.lang.reflect.InvocationTargetException;
29+
import java.lang.reflect.Method;
30+
import java.util.concurrent.TimeUnit;
31+
32+
33+
@Fork(jvmArgsAppend = {"--add-exports", "java.base/sun.security.ssl=ALL-UNNAMED", "--add-opens", "java.base/sun.security.ssl=ALL-UNNAMED"})
34+
@State(Scope.Benchmark)
35+
@OutputTimeUnit(TimeUnit.MICROSECONDS)
36+
@BenchmarkMode(Mode.Throughput)
37+
public class CipherSuiteBench {
38+
39+
Method nameOf;
40+
41+
@Param({"TLS_AES_256_GCM_SHA384",
42+
"TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384",
43+
"TLS_DHE_DSS_WITH_AES_128_CBC_SHA256",
44+
"TLS_DHE_RSA_WITH_AES_256_CBC_SHA" })
45+
String cipherSuite;
46+
47+
@Setup
48+
public void initilizeClass() throws ClassNotFoundException, NoSuchMethodException {
49+
Class<?> cs = Class.forName("sun.security.ssl.CipherSuite");
50+
nameOf = cs.getDeclaredMethod("nameOf", String.class);
51+
nameOf.setAccessible(true);
52+
}
53+
54+
@Benchmark
55+
public Object benchmarkCipherSuite() throws InvocationTargetException, IllegalAccessException {
56+
return nameOf.invoke(null,cipherSuite);
57+
}
58+
}

0 commit comments

Comments
 (0)