Skip to content

Commit

Permalink
QueryCompiler: Use SHA-256 hash instead of String#hashCode on class body
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind committed Oct 5, 2022
1 parent 857b720 commit c5507b5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
23 changes: 23 additions & 0 deletions Util/src/main/java/io/deephaven/util/ByteUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending
*/
package io.deephaven.util;

public class ByteUtils {
private static final char[] HEX_LOOKUP = new char[] {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};

public static String byteArrToHex(byte[] bytes) {
// our output size will be exactly 2x byte-array length
final char[] buffer = new char[bytes.length * 2];

for (int ii = 0; ii < bytes.length; ii++) {
// extract the upper 4 bits
buffer[ii << 1] = HEX_LOOKUP[(bytes[ii] >> 4) & 0xF];
// extract the lower 4 bits
buffer[(ii << 1) + 1] = HEX_LOOKUP[bytes[ii] & 0xF];
}

return new String(buffer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.deephaven.datastructures.util.CollectionUtil;
import io.deephaven.internal.log.LoggerFactory;
import io.deephaven.io.logger.Logger;
import io.deephaven.util.ByteUtils;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -21,10 +22,13 @@
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
Expand Down Expand Up @@ -418,14 +422,17 @@ private Class<?> compileHelper(@NotNull final String className,
@NotNull final String packageNameRoot,
@Nullable final StringBuilder codeLog,
@NotNull final Map<String, Class<?>> parameterClasses) {
// NB: We include class name hash in order to (hopefully) account for case insensitive file systems.
final int classNameHash = className.hashCode();
final int classBodyHash = classBody.hashCode();
final MessageDigest digest;
try {
digest = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Unable to create SHA-256 hashing digest", e);
}
final String basicHashText =
ByteUtils.byteArrToHex(digest.digest(classBody.getBytes(StandardCharsets.UTF_8)));

for (int pi = 0; pi < MAX_CLASS_COLLISIONS; ++pi) {
final String packageNameSuffix = "c"
+ (classBodyHash < 0 ? "m" : "") + (classBodyHash & Integer.MAX_VALUE)
+ (classNameHash < 0 ? "n" : "") + (classNameHash & Integer.MAX_VALUE)
final String packageNameSuffix = "c_" + basicHashText
+ (pi == 0 ? "" : ("p" + pi))
+ "v" + JAVA_CLASS_VERSION;
final String packageName = (packageNameRoot.isEmpty()
Expand Down Expand Up @@ -488,7 +495,12 @@ private Class<?> compileHelper(@NotNull final String className,
knownClasses.put(identifyingFieldValue, p);
// It's also possible that some other code has already fulfilled this promise with exactly the same
// class. That's ok though: the promise code does not reject multiple sets to the identical value.
p.complete(result);
// But if the result is already set, that means that we've already been through this logic, determined
// the class, and set it (each run through this logic can be using a different ClassLoader, so it may
// be a different instance of the same Class).
if (!p.complete(result)) {
log.warn("Found pre-existing class result for " + fqClassName);
}
}

// If the class we found was indeed the class we were looking for, then return it
Expand All @@ -504,7 +516,7 @@ private Class<?> compileHelper(@NotNull final String className,
}
throw new IllegalStateException("Found too many collisions for package name root " + packageNameRoot
+ ", class name=" + className
+ ", class body hash=" + classBodyHash + " - contact Deephaven support!");
+ ", class body hash=" + basicHashText + " - contact Deephaven support!");
}

private Class<?> tryLoadClassByFqName(String fqClassName, Map<String, Class<?>> parameterClasses) {
Expand Down

0 comments on commit c5507b5

Please sign in to comment.