Skip to content

Commit

Permalink
validate bytecode in case of a ClassFormatError
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 6, 2024
1 parent 242df83 commit a46d530
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
20 changes: 14 additions & 6 deletions core/src/main/java/lucee/commons/lang/PhysicalClassLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import lucee.runtime.type.Struct;
import lucee.runtime.type.StructImpl;
import lucee.runtime.type.util.KeyConstants;
import lucee.transformer.bytecode.util.ASMUtil;
import lucee.transformer.bytecode.util.ClassRenamer;

/**
Expand Down Expand Up @@ -299,14 +300,21 @@ private Class<?> rename(Class<?> clazz, byte[] barr) {
}

private Class<?> _loadClass(String name, byte[] barr, boolean rename) {
Class<?> clazz = defineClass(name, barr, 0, barr.length);
if (clazz != null) {
if (!rename) loadedClasses.put(name, barr);
allLoadedClasses.put(name, barr);
try {
Class<?> clazz = defineClass(name, barr, 0, barr.length);

if (clazz != null) {
if (!rename) loadedClasses.put(name, barr);
allLoadedClasses.put(name, barr);

resolveClass(clazz);
resolveClass(clazz);
}
return clazz;
}
catch (ClassFormatError cfe) {
if (!ASMUtil.isValidBytecode(barr)) throw new RuntimeException("given bytcode for [" + name + "] is not valid");
throw cfe;
}
return clazz;
}

public Resource[] getJarResources() {
Expand Down
18 changes: 18 additions & 0 deletions core/src/main/java/lucee/transformer/bytecode/util/ASMUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -1313,4 +1313,22 @@ public static String getDescriptor(Type type) {
}
return desc;
}

/**
* Validates the given byte array using the ASM library.
*
* @param className the name of the class being validated
* @param bytecode the byte array representing the class
*/
public static boolean isValidBytecode(byte[] bytecode) {
try {
ClassReader classReader = new ClassReader(bytecode);
// Simply parse the bytecode; will throw if invalid
classReader.accept(null, 0);
return true;
}
catch (Exception e) {
return false;
}
}
}

0 comments on commit a46d530

Please sign in to comment.