Skip to content

Commit 7ac35ec

Browse files
Christian Wimmerezzarghili
Christian Wimmer
authored andcommitted
[GR-53821] Use a non-cryptographic hash for IsolatedSpeculationReasonEncoding.
PullRequest: graal/17671
2 parents 126bcc8 + 967fcee commit 7ac35ec

File tree

2 files changed

+22
-47
lines changed

2 files changed

+22
-47
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/util/Digest.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public final class Digest {
5050
private record LongLong(long l1, long l2) {
5151
}
5252

53-
private static final char[] DIGITS = {
53+
private static final byte[] DIGITS = {
5454
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
5555
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
5656
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
@@ -92,20 +92,29 @@ public static String digest(byte[] bytes) {
9292
* Hashes the passed range of the byte array parameter and returns the encoding of the hash.
9393
*/
9494
public static String digest(byte[] bytes, int offset, int length) {
95-
LongLong hash = MurmurHash3_x64_128(bytes, offset, length, HASH_SEED);
96-
97-
StringBuilder result = new StringBuilder(DIGEST_SIZE);
98-
encodeBase62(hash.l1, result);
99-
encodeBase62(hash.l2, result);
95+
String result = new String(digestAsByteArray(bytes, offset, length), StandardCharsets.UTF_8);
10096
assert result.length() == DIGEST_SIZE : "--" + result + "--";
10197
return result.toString();
10298
}
10399

104-
private static void encodeBase62(long value, StringBuilder result) {
100+
/**
101+
* Hashes the passed range of the byte array parameter and returns the encoding of the hash as a
102+
* new byte array.
103+
*/
104+
public static byte[] digestAsByteArray(byte[] bytes, int offset, int length) {
105+
LongLong hash = MurmurHash3_x64_128(bytes, offset, length, HASH_SEED);
106+
107+
byte[] array = new byte[DIGEST_SIZE];
108+
encodeBase62(hash.l1, array, 0);
109+
encodeBase62(hash.l2, array, BASE62_DIGITS_PER_LONG);
110+
return array;
111+
}
112+
113+
private static void encodeBase62(long value, byte[] result, int resultIndex) {
105114
long cur = value;
106115
int base = DIGITS.length;
107116
for (int i = 0; i < BASE62_DIGITS_PER_LONG; i++) {
108-
result.append(DIGITS[NumUtil.safeToInt(Long.remainderUnsigned(cur, base))]);
117+
result[resultIndex + i] = DIGITS[NumUtil.safeToInt(Long.remainderUnsigned(cur, base))];
109118
cur = Long.divideUnsigned(cur, base);
110119
}
111120
GraalError.guarantee(cur == 0, "Too few loop iterations processing digits");

substratevm/src/com.oracle.svm.graal/src/com/oracle/svm/graal/isolated/IsolatedSpeculationReasonEncoding.java

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,11 @@
2727
import java.io.ByteArrayOutputStream;
2828
import java.io.DataOutputStream;
2929
import java.io.IOException;
30-
import java.security.MessageDigest;
31-
import java.security.NoSuchAlgorithmException;
3230
import java.util.Arrays;
3331

3432
import com.oracle.svm.core.graal.meta.SharedRuntimeMethod;
3533

36-
import jdk.vm.ci.common.JVMCIError;
34+
import jdk.graal.compiler.util.Digest;
3735
import jdk.vm.ci.meta.ResolvedJavaMethod;
3836
import jdk.vm.ci.meta.ResolvedJavaType;
3937

@@ -43,10 +41,6 @@ final class IsolatedSpeculationReasonEncoding extends ByteArrayOutputStream impl
4341
private DataOutputStream dos = new DataOutputStream(this);
4442
private byte[] result;
4543

46-
IsolatedSpeculationReasonEncoding() {
47-
super(SHA1_LENGTH);
48-
}
49-
5044
private void checkOpen() {
5145
if (result != null) {
5246
throw new IllegalArgumentException("Cannot update closed speculation encoding");
@@ -92,8 +86,8 @@ public void addType(ResolvedJavaType type) {
9286
}
9387

9488
private void addImageHeapObject(Object object, int nullValue) {
89+
checkOpen();
9590
if (!addNull(object, nullValue)) {
96-
checkOpen();
9791
try {
9892
dos.writeLong(ImageHeapObjects.ref(object).rawValue());
9993
} catch (IOException e) {
@@ -104,8 +98,8 @@ private void addImageHeapObject(Object object, int nullValue) {
10498

10599
@Override
106100
public void addString(String value) {
101+
checkOpen();
107102
if (!addNull(value, NULL_STRING)) {
108-
checkOpen();
109103
try {
110104
dos.writeChars(value);
111105
} catch (IOException e) {
@@ -142,43 +136,15 @@ private boolean addNull(Object o, int nullValue) {
142136
return false;
143137
}
144138

145-
/**
146-
* Prototype SHA1 digest that is cloned before use.
147-
*/
148-
private static final MessageDigest SHA1 = getSHA1();
149-
private static final int SHA1_LENGTH = SHA1.getDigestLength();
150-
151-
private static MessageDigest getSHA1() {
152-
try {
153-
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
154-
sha1.clone();
155-
return sha1;
156-
} catch (CloneNotSupportedException | NoSuchAlgorithmException e) {
157-
// Should never happen given that SHA-1 is mandated in a
158-
// compliant Java platform implementation.
159-
throw new JVMCIError("Expect a cloneable implementation of a SHA-1 message digest to be available", e);
160-
}
161-
}
162-
163139
/**
164140
* Gets the final encoded byte array and closes this encoding such that any further attempts to
165141
* update it result in an {@link IllegalArgumentException}.
166142
*/
167143
byte[] getByteArray() {
168144
if (result == null) {
169-
if (count > SHA1_LENGTH) {
170-
try {
171-
MessageDigest md = (MessageDigest) SHA1.clone();
172-
md.update(buf, 0, count);
173-
result = md.digest();
174-
} catch (CloneNotSupportedException e) {
175-
throw new InternalError(e);
176-
}
145+
if (count > Digest.DIGEST_SIZE) {
146+
result = Digest.digestAsByteArray(buf, 0, count);
177147
} else {
178-
if (buf.length == count) {
179-
// No need to copy the byte array
180-
return buf;
181-
}
182148
result = Arrays.copyOf(buf, count);
183149
}
184150
dos = null;

0 commit comments

Comments
 (0)