Skip to content

Commit c55e280

Browse files
committed
Add explicit check for Throwable and Exception in PatchedClassWriter
1 parent 6735c59 commit c55e280

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

newrelic-weaver/src/main/java/com/newrelic/weave/utils/PatchedClassWriter.java

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* the class structure without actually loading any classes.
2222
*/
2323
class PatchedClassWriter extends ClassWriter {
24+
private static final String JAVA_LANG_THROWABLE = "java/lang/Throwable";
25+
private static final String JAVA_LANG_EXCEPTION = "java/lang/Exception";
2426
private final ClassInformationFinder classInfoFinder;
2527

2628
PatchedClassWriter(int flags, ClassInformationFinder classInfoFinder) {
@@ -105,9 +107,27 @@ protected String getCommonSuperClass(final String type1, final String type2) {
105107
}
106108

107109
if (null == commonSuperType) {
110+
//Required since Java 24
111+
if (areThrowableAndException(type1, type2)) {
112+
return JAVA_LANG_THROWABLE;
113+
}
108114
return WeaveUtils.JAVA_LANG_OBJECT_NAME;
109115
}
110116

111117
return commonSuperType;
112118
}
119+
120+
/**
121+
* getCommonSuperClass may have problematic behavior during the weaving of ClassLoaders.
122+
* It designates Object as the common supertype of all types, because we're actively hijacking the ClassLoaders at this time and the full class hierarchy may not be available.
123+
*
124+
* Starting in Java 24, a new class cast was introduced in ClassLoader.initSystemClassLoader that added a type check between Throwable and Exception. Because of the behavior
125+
* described above, the common supertype of Throwable and Exception was designated Object by the agent. This led to a VerifyError.
126+
*
127+
* This method specifically handles that edge case to prevent the VerifyError for JDK24+.
128+
*/
129+
private static boolean areThrowableAndException(String type1, String type2){
130+
return (type1.equals(JAVA_LANG_THROWABLE) && type2.equals(JAVA_LANG_EXCEPTION)) || (type2.equals(JAVA_LANG_THROWABLE) && type1.equals(
131+
JAVA_LANG_EXCEPTION));
132+
}
113133
}

0 commit comments

Comments
 (0)