Skip to content

Commit

Permalink
8294974: Convert jdk.jshell/jdk.jshell.execution.LocalExecutionContro…
Browse files Browse the repository at this point in the history
…l to use the Classfile API to instrument classes

Reviewed-by: jlahoda
  • Loading branch information
asotona committed Mar 13, 2023
1 parent f835aaa commit a95bc7a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 44 deletions.
8 changes: 5 additions & 3 deletions src/java.base/share/classes/module-info.java
Expand Up @@ -193,15 +193,17 @@
java.logging;
exports jdk.internal.classfile to
jdk.jartool,
jdk.jlink;
jdk.jlink,
jdk.jshell;
exports jdk.internal.classfile.attribute to
jdk.jartool;
exports jdk.internal.classfile.constantpool to
jdk.jartool;
exports jdk.internal.classfile.instruction to
jdk.jshell;
exports jdk.internal.org.objectweb.asm to
jdk.jfr,
jdk.jlink,
jdk.jshell;
jdk.jlink;
exports jdk.internal.org.objectweb.asm.tree to
jdk.jfr,
jdk.jlink;
Expand Down
Expand Up @@ -24,17 +24,17 @@
*/
package jdk.jshell.execution;

import java.lang.constant.ClassDesc;
import java.lang.constant.ConstantDescs;
import java.lang.constant.MethodTypeDesc;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Stream;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.Label;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Opcodes;
import jdk.internal.classfile.Classfile;
import jdk.internal.classfile.ClassTransform;
import jdk.internal.classfile.instruction.BranchInstruction;

/**
* An implementation of {@link jdk.jshell.spi.ExecutionControl} which executes
Expand Down Expand Up @@ -74,51 +74,39 @@ public void load(ClassBytecodes[] cbcs)
.toArray(ClassBytecodes[]::new));
}

private static final String CANCEL_CLASS = "REPL.$Cancel$";
private static final ClassDesc CD_Cancel = ClassDesc.of(CANCEL_CLASS);
private static final ClassDesc CD_ThreadDeath = ClassDesc.of("java.lang.ThreadDeath");
private static final MethodTypeDesc MTD_void = MethodTypeDesc.of(ConstantDescs.CD_void);

private static byte[] instrument(byte[] classFile) {
var reader = new ClassReader(classFile);
var writer = new ClassWriter(reader, 0);
reader.accept(new ClassVisitor(Opcodes.ASM9, writer) {
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature, String[] exceptions) {
return new MethodVisitor(Opcodes.ASM9, super.visitMethod(access, name, descriptor, signature, exceptions)) {
@Override
public void visitJumpInsn(int opcode, Label label) {
visitMethodInsn(Opcodes.INVOKESTATIC, "REPL/$Cancel$", "stopCheck", "()V", false);
super.visitJumpInsn(opcode, label);
}
};
}
}, 0);
return writer.toByteArray();
return Classfile.parse(classFile)
.transform(ClassTransform.transformingMethodBodies((cob, coe) -> {
if (coe instanceof BranchInstruction)
cob.invokestatic(CD_Cancel, "stopCheck", MTD_void);
cob.with(coe);
}));
}

private static ClassBytecodes genCancelClass() {
var cancelWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cancelWriter.visit(Opcodes.V19, Opcodes.ACC_PUBLIC, "REPL/$Cancel$", null, "java/lang/Object", null);
cancelWriter.visitField(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC | Opcodes.ACC_VOLATILE, "allStop", "Z", null, null);
var checkVisitor = cancelWriter.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC, "stopCheck", "()V", null, null);
checkVisitor.visitCode();
checkVisitor.visitFieldInsn(Opcodes.GETSTATIC, "REPL/$Cancel$", "allStop", "Z");
var skip = new Label();
checkVisitor.visitJumpInsn(Opcodes.IFEQ, skip);
checkVisitor.visitTypeInsn(Opcodes.NEW, "java/lang/ThreadDeath");
checkVisitor.visitInsn(Opcodes.DUP);
checkVisitor.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/ThreadDeath", "<init>", "()V", false);
checkVisitor.visitInsn(Opcodes.ATHROW);
checkVisitor.visitLabel(skip);
checkVisitor.visitInsn(Opcodes.RETURN);
checkVisitor.visitMaxs(0, 0);
checkVisitor.visitEnd();
cancelWriter.visitEnd();
return new ClassBytecodes("REPL.$Cancel$", cancelWriter.toByteArray());
return new ClassBytecodes(CANCEL_CLASS, Classfile.build(CD_Cancel, clb ->
clb.withFlags(Classfile.ACC_PUBLIC)
.withField("allStop", ConstantDescs.CD_boolean, Classfile.ACC_PUBLIC | Classfile.ACC_STATIC | Classfile.ACC_VOLATILE)
.withMethodBody("stopCheck", MTD_void, Classfile.ACC_PUBLIC | Classfile.ACC_STATIC, cob ->
cob.getstatic(CD_Cancel, "allStop", ConstantDescs.CD_boolean)
.ifThenElse(tb -> tb.new_(CD_ThreadDeath)
.dup()
.invokespecial(CD_ThreadDeath, "<init>", MTD_void)
.athrow(),
eb -> eb.return_()))));
}

@Override
@SuppressWarnings("removal")
protected String invoke(Method doitMethod) throws Exception {
if (allStop == null) {
super.load(new ClassBytecodes[]{ genCancelClass() });
allStop = findClass("REPL.$Cancel$").getDeclaredField("allStop");
allStop = findClass(CANCEL_CLASS).getDeclaredField("allStop");
}
allStop.set(null, false);

Expand Down
2 changes: 1 addition & 1 deletion test/jdk/jdk/classfile/TEST.properties
@@ -1,4 +1,4 @@
maxOutputSize = 500000
maxOutputSize = 2500000
enablePreview = true
modules = \
java.base/jdk.internal.classfile \
Expand Down

1 comment on commit a95bc7a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.