Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recover from duplicate class definition errors #5185

Merged
merged 4 commits into from
Jan 25, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,18 @@ public MethodVisitor visitMethod(
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
// apply the following transformation to defineClass method
/*
DefineClassContext.enter();
try {
// original method body
DefineClassContext.exit();
return result;
} catch (LinkageError error) {
boolean helpersInjected = DefineClassContext.exitAndGet();
Class<?> loaded = findLoadedClass(className);
return DefineClassUtil.handleLinkageError(error, loaded);
return DefineClassUtil.handleLinkageError(error, helpersInjected, loaded);
} catch (Throwable throwable) {
DefineClassContext.exit();
throw throwable;
Comment on lines +87 to +89
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
*/
if ("defineClass".equals(name)
Expand All @@ -91,6 +98,7 @@ public MethodVisitor visitMethod(
new MethodVisitor(api, mv) {
Label start = new Label();
Label end = new Label();
Label handler = new Label();

@Override
public void visitCode() {
Expand All @@ -101,6 +109,8 @@ public void visitCode() {
"()V",
false);
mv.visitTryCatchBlock(start, end, end, "java/lang/LinkageError");
// catch other exceptions
mv.visitTryCatchBlock(start, end, handler, null);
mv.visitLabel(start);

super.visitCode();
Expand All @@ -121,6 +131,7 @@ public void visitInsn(int opcode) {

@Override
public void visitMaxs(int maxStack, int maxLocals) {
// handle LinkageError
mv.visitLabel(end);
mv.visitFrame(
Opcodes.F_FULL,
Expand Down Expand Up @@ -150,6 +161,22 @@ public void visitMaxs(int maxStack, int maxLocals) {
false);
mv.visitInsn(Opcodes.ARETURN);

// handle Throwable
mv.visitLabel(handler);
mv.visitFrame(
Opcodes.F_FULL,
2,
new Object[] {"java/lang/ClassLoader", "java/lang/String"},
1,
new Object[] {"java/lang/Throwable"});
mv.visitMethodInsn(
Opcodes.INVOKESTATIC,
Type.getInternalName(DefineClassContext.class),
"exit",
"()V",
false);
mv.visitInsn(Opcodes.ATHROW);

super.visitMaxs(maxStack, maxLocals);
}
};
Expand Down