Skip to content

Commit

Permalink
[GR-53821] Use a non-cryptographic hash for IsolatedSpeculationReason…
Browse files Browse the repository at this point in the history
…Encoding.

PullRequest: graal/17671
  • Loading branch information
christianwimmer authored and ezzarghili committed May 9, 2024
2 parents 126bcc8 + 967fcee commit 7ac35ec
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class Digest {
private record LongLong(long l1, long l2) {
}

private static final char[] DIGITS = {
private static final byte[] DIGITS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
Expand Down Expand Up @@ -92,20 +92,29 @@ public static String digest(byte[] bytes) {
* Hashes the passed range of the byte array parameter and returns the encoding of the hash.
*/
public static String digest(byte[] bytes, int offset, int length) {
LongLong hash = MurmurHash3_x64_128(bytes, offset, length, HASH_SEED);

StringBuilder result = new StringBuilder(DIGEST_SIZE);
encodeBase62(hash.l1, result);
encodeBase62(hash.l2, result);
String result = new String(digestAsByteArray(bytes, offset, length), StandardCharsets.UTF_8);
assert result.length() == DIGEST_SIZE : "--" + result + "--";
return result.toString();
}

private static void encodeBase62(long value, StringBuilder result) {
/**
* Hashes the passed range of the byte array parameter and returns the encoding of the hash as a
* new byte array.
*/
public static byte[] digestAsByteArray(byte[] bytes, int offset, int length) {
LongLong hash = MurmurHash3_x64_128(bytes, offset, length, HASH_SEED);

byte[] array = new byte[DIGEST_SIZE];
encodeBase62(hash.l1, array, 0);
encodeBase62(hash.l2, array, BASE62_DIGITS_PER_LONG);
return array;
}

private static void encodeBase62(long value, byte[] result, int resultIndex) {
long cur = value;
int base = DIGITS.length;
for (int i = 0; i < BASE62_DIGITS_PER_LONG; i++) {
result.append(DIGITS[NumUtil.safeToInt(Long.remainderUnsigned(cur, base))]);
result[resultIndex + i] = DIGITS[NumUtil.safeToInt(Long.remainderUnsigned(cur, base))];
cur = Long.divideUnsigned(cur, base);
}
GraalError.guarantee(cur == 0, "Too few loop iterations processing digits");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,11 @@
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

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

import jdk.vm.ci.common.JVMCIError;
import jdk.graal.compiler.util.Digest;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.ResolvedJavaType;

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

IsolatedSpeculationReasonEncoding() {
super(SHA1_LENGTH);
}

private void checkOpen() {
if (result != null) {
throw new IllegalArgumentException("Cannot update closed speculation encoding");
Expand Down Expand Up @@ -92,8 +86,8 @@ public void addType(ResolvedJavaType type) {
}

private void addImageHeapObject(Object object, int nullValue) {
checkOpen();
if (!addNull(object, nullValue)) {
checkOpen();
try {
dos.writeLong(ImageHeapObjects.ref(object).rawValue());
} catch (IOException e) {
Expand All @@ -104,8 +98,8 @@ private void addImageHeapObject(Object object, int nullValue) {

@Override
public void addString(String value) {
checkOpen();
if (!addNull(value, NULL_STRING)) {
checkOpen();
try {
dos.writeChars(value);
} catch (IOException e) {
Expand Down Expand Up @@ -142,43 +136,15 @@ private boolean addNull(Object o, int nullValue) {
return false;
}

/**
* Prototype SHA1 digest that is cloned before use.
*/
private static final MessageDigest SHA1 = getSHA1();
private static final int SHA1_LENGTH = SHA1.getDigestLength();

private static MessageDigest getSHA1() {
try {
MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
sha1.clone();
return sha1;
} catch (CloneNotSupportedException | NoSuchAlgorithmException e) {
// Should never happen given that SHA-1 is mandated in a
// compliant Java platform implementation.
throw new JVMCIError("Expect a cloneable implementation of a SHA-1 message digest to be available", e);
}
}

/**
* Gets the final encoded byte array and closes this encoding such that any further attempts to
* update it result in an {@link IllegalArgumentException}.
*/
byte[] getByteArray() {
if (result == null) {
if (count > SHA1_LENGTH) {
try {
MessageDigest md = (MessageDigest) SHA1.clone();
md.update(buf, 0, count);
result = md.digest();
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
if (count > Digest.DIGEST_SIZE) {
result = Digest.digestAsByteArray(buf, 0, count);
} else {
if (buf.length == count) {
// No need to copy the byte array
return buf;
}
result = Arrays.copyOf(buf, count);
}
dos = null;
Expand Down

0 comments on commit 7ac35ec

Please sign in to comment.