Skip to content

Commit

Permalink
Add missing normalization, cache normalization
Browse files Browse the repository at this point in the history
  • Loading branch information
sfPlayer1 committed Sep 6, 2024
1 parent 9e6fc1b commit c628430
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
// - accessing classes too early (game libs shouldn't be used until Loader is ready)
// - using jars that are only transient (deobfuscation input or pass-through installers)
String msg = String.format("can't load class %s at %s as it hasn't been exposed to the game (yet? The system property "+SystemProperties.PATH_GROUPS+" may not be set correctly in-dev)",
name, getCodeSource(url, fileName));
name, getCodeSource(url, fileName, true));
if (LOG_CLASS_LOAD_ERRORS) Log.warn(LogCategory.KNOT, msg);
throw new ClassNotFoundException(msg);
} else { // load from system cl
Expand Down Expand Up @@ -266,7 +266,7 @@ private boolean isValidParentUrl(URL url, String fileName) {
if (DISABLE_ISOLATION) return true;
if (!hasRegularCodeSource(url)) return true;

Path codeSource = getCodeSource(url, fileName);
Path codeSource = getCodeSource(url, fileName, true);
Set<Path> validParentCodeSources = this.validParentCodeSources;

if (validParentCodeSources != null) { // explicit whitelist (in addition to platform cl classes)
Expand All @@ -286,7 +286,7 @@ Class<?> tryLoadClass(String name, boolean allowFromParent) throws ClassNotFound
URL url = classLoader.getResource(fileName);

if (url != null && hasRegularCodeSource(url)) {
Path codeSource = getCodeSource(url, fileName);
Path codeSource = getCodeSource(url, fileName, false);
String[] prefixes = allowedPrefixes.get(codeSource);

if (prefixes != null) {
Expand Down Expand Up @@ -360,7 +360,7 @@ private Metadata getMetadata(String name) {
URL url = classLoader.getResource(fileName);
if (url == null || !hasRegularCodeSource(url)) return Metadata.EMPTY;

return getMetadata(getCodeSource(url, fileName));
return getMetadata(getCodeSource(url, fileName, false));
}

private Metadata getMetadata(Path codeSource) {
Expand Down Expand Up @@ -486,7 +486,7 @@ private byte[] getRawClassByteArray(String name, boolean allowFromParent) throws
url = parentClassLoader.getResource(name);

if (!isValidParentUrl(url, name)) {
if (LOG_CLASS_LOAD) Log.info(LogCategory.KNOT, "refusing to load class %s at %s from parent class loader", name, getCodeSource(url, name));
if (LOG_CLASS_LOAD) Log.info(LogCategory.KNOT, "refusing to load class %s at %s from parent class loader", name, getCodeSource(url, name, true));

return null;
}
Expand All @@ -510,10 +510,15 @@ private static boolean hasRegularCodeSource(URL url) {
return url.getProtocol().equals("file") || url.getProtocol().equals("jar");
}

private static Path getCodeSource(URL url, String fileName) {
private static Path getCodeSource(URL url, String fileName, boolean normalize) {
try {
Path ret = UrlUtil.getCodeSource(url, fileName);
assert ret.equals(LoaderUtil.normalizeExistingPath(ret)); // ret should already be normalized

if (normalize) {
ret = LoaderUtil.normalizeExistingPath(ret);
} else {
assert ret.equals(LoaderUtil.normalizeExistingPath(ret)); // ret should already be normalized
}

return ret;
} catch (UrlConversionException e) {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/net/fabricmc/loader/impl/util/LoaderUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,13 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.stream.Collectors;

public final class LoaderUtil {
private static final ConcurrentMap<Path, Path> pathNormalizationCache = new ConcurrentHashMap<>();

public static String getClassFileName(String className) {
return className.replace('.', '/').concat(".class");
}
Expand All @@ -40,6 +44,10 @@ public static Path normalizePath(Path path) {
}

public static Path normalizeExistingPath(Path path) {
return pathNormalizationCache.computeIfAbsent(path, LoaderUtil::normalizeExistingPath0);
}

private static Path normalizeExistingPath0(Path path) {
try {
return path.toRealPath();
} catch (IOException e) {
Expand Down

0 comments on commit c628430

Please sign in to comment.