use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.
the class FinallyTest method gotos.
/**
* This test studies placement of GOTO instructions.
*/
@Test
public void gotos() throws IOException {
final Source source = Source.getSourceFor("src", Finally.class);
byte[] b = TargetLoader.getClassDataAsBytes(Finally.class);
b = BytecodeVersion.downgradeIfNeeded(BytecodeVersion.get(b), b);
final ClassNode classNode = new ClassNode();
new ClassReader(b).accept(classNode, 0);
final Set<String> tags = new HashSet<String>();
for (final MethodNode m : classNode.methods) {
if ("main".equals(m.name)) {
// skip it
continue;
}
int lineNumber = -1;
for (AbstractInsnNode i = m.instructions.getFirst(); i != null; i = i.getNext()) {
if (AbstractInsnNode.LINE == i.getType()) {
lineNumber = ((LineNumberNode) i).line;
}
if (Opcodes.GOTO == i.getOpcode()) {
final String line = source.getLine(lineNumber);
if (line.indexOf('$') < 0) {
throw new AssertionError("No tag at line " + lineNumber);
}
final String tag = line.substring(line.indexOf('$') + "$line-".length(), line.lastIndexOf('$'));
tags.add(tag);
}
}
}
final Set<String> expected = new HashSet<String>();
if (isJDKCompiler) {
expected.add("example.2");
} else {
expected.add("example.0");
}
expected.add("breakStatement.for");
if (isJDKCompiler) {
if (JAVA_VERSION.isBefore("10")) {
// https://bugs.openjdk.java.net/browse/JDK-8180141
expected.add("breakStatement.1");
} else {
expected.add("breakStatement");
}
expected.add("breakStatement.2");
} else {
expected.add("breakStatement");
}
if (isJDKCompiler) {
expected.add("emptyCatch.2");
} else {
expected.add("emptyCatch");
expected.add("emptyCatch.1");
}
if (isJDKCompiler) {
expected.add("catchNotExecuted.2");
} else {
expected.add("catchNotExecuted");
expected.add("catchNotExecuted.1");
}
if (isJDKCompiler) {
expected.add("nested.5");
expected.add("nested.6");
} else {
expected.add("nested.0");
expected.add("nested.3");
}
if (isJDKCompiler && JAVA_VERSION.isBefore("1.8")) {
expected.add("emptyTry.2");
}
if (!isJDKCompiler) {
expected.add("alwaysCompletesAbruptly.0");
}
assertEquals(expected, tags);
}
use of org.objectweb.asm.tree.AbstractInsnNode in project jacoco by jacoco.
the class StructuredLockingTest method assertStructuredLocking.
private void assertStructuredLocking(String owner, MethodNode mn) throws Exception {
Analyzer<BasicValue> analyzer = new Analyzer<BasicValue>(new BasicInterpreter()) {
@Override
protected Frame<BasicValue> newFrame(int nLocals, int nStack) {
return new LockFrame(nLocals, nStack);
}
@Override
protected Frame<BasicValue> newFrame(Frame<? extends BasicValue> src) {
return new LockFrame(src);
}
};
Frame<BasicValue>[] frames = analyzer.analyze(owner, mn);
// Make sure no locks are left when method exits:
for (int i = 0; i < frames.length; i++) {
AbstractInsnNode insn = mn.instructions.get(i);
switch(insn.getOpcode()) {
case Opcodes.IRETURN:
case Opcodes.LRETURN:
case Opcodes.FRETURN:
case Opcodes.DRETURN:
case Opcodes.ARETURN:
case Opcodes.RETURN:
((LockFrame) frames[i]).assertNoLock("Exit with lock");
break;
case Opcodes.ATHROW:
List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
if (handlers == null || handlers.isEmpty()) {
((LockFrame) frames[i]).assertNoLock("Exit with lock");
}
break;
}
}
// Only instructions protected by a catch-all handler can hold locks:
for (int i = 0; i < frames.length; i++) {
AbstractInsnNode insn = mn.instructions.get(i);
if (insn.getOpcode() > 0) {
boolean catchAll = false;
List<TryCatchBlockNode> handlers = analyzer.getHandlers(i);
if (handlers != null) {
for (TryCatchBlockNode node : handlers) {
catchAll |= node.type == null;
}
}
if (!catchAll) {
((LockFrame) frames[i]).assertNoLock("No handlers for insn with lock");
}
}
}
}
use of org.objectweb.asm.tree.AbstractInsnNode in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method test_all_instructions_are_part_of_CFG.
@Test
public void test_all_instructions_are_part_of_CFG() throws Exception {
SquidClassLoader squidClassLoader = new SquidClassLoader(Lists.newArrayList(new File("target/test-classes"), new File("target/classes")));
File file = new File("src/test/java/org/sonar/java/bytecode/cfg/testdata/CFGTestData.java");
CompilationUnitTree tree = (CompilationUnitTree) JavaParser.createParser().parse(file);
SemanticModel.createFor(tree, squidClassLoader);
Symbol.TypeSymbol testClazz = ((ClassTree) tree.types().get(0)).symbol();
ClassReader cr = new ClassReader(squidClassLoader.getResourceAsStream(Convert.bytecodeName(CFGTestData.class.getCanonicalName()) + ".class"));
ClassNode classNode = new ClassNode(Opcodes.ASM5);
cr.accept(classNode, 0);
for (MethodNode method : classNode.methods) {
Multiset<String> opcodes = Arrays.stream(method.instructions.toArray()).map(AbstractInsnNode::getOpcode).filter(opcode -> opcode != -1).map(opcode -> Printer.OPCODES[opcode]).collect(Collectors.toCollection(HashMultiset::create));
Symbol methodSymbol = Iterables.getOnlyElement(testClazz.lookupSymbols(method.name));
BytecodeCFG bytecodeCFG = SETestUtils.bytecodeCFG(((JavaSymbol.MethodJavaSymbol) methodSymbol).completeSignature(), squidClassLoader);
Multiset<String> cfgOpcodes = cfgOpcodes(bytecodeCFG);
assertThat(cfgOpcodes).isEqualTo(opcodes);
}
}
use of org.objectweb.asm.tree.AbstractInsnNode in project MineCamera by PorPit.
the class CamTransformer method initTransformers.
protected void initTransformers() {
addTransformer(new Transformer("net.minecraft.client.entity.EntityPlayerSP") {
@Override
public void transform(ClassNode node) {
MethodNode m = findMethod(node, "isCurrentViewEntity", "()Z");
m.instructions.clear();
m.instructions.add(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/porpit/minecamera/transform/CamEventHandler", "shouldPlayerTakeInput", "()Z", false));
m.instructions.add(new InsnNode(Opcodes.IRETURN));
}
});
addTransformer(new Transformer("net.minecraft.client.renderer.EntityRenderer") {
@Override
public void transform(ClassNode node) {
MethodNode m = findMethod(node, "getMouseOver", "(F)V");
m.instructions.insert(new MethodInsnNode(Opcodes.INVOKESTATIC, "com/porpit/minecamera/transform/CamEventHandler", "setupMouseHandlerBefore", "()V", false));
AbstractInsnNode currentNode = null;
@SuppressWarnings("unchecked") Iterator<AbstractInsnNode> iter = m.instructions.iterator();
while (iter.hasNext()) {
currentNode = iter.next();
if (currentNode instanceof InsnNode && ((InsnNode) currentNode).getOpcode() == RETURN) {
m.instructions.insertBefore(currentNode, new MethodInsnNode(Opcodes.INVOKESTATIC, "com/porpit/minecamera/transform/CamEventHandler", "setupMouseHandlerAfter", "()V", false));
}
}
}
});
}
use of org.objectweb.asm.tree.AbstractInsnNode in project MineCamera by PorPit.
the class Transformer method replaceLabelBefore.
public void replaceLabelBefore(InsnList instructions, AbstractInsnNode node, @Nullable ArrayList<AbstractInsnNode> replaceInstructions, int labels, int labelsBefore, boolean keepFirstLabel, boolean deleteFrame) {
ListIterator<AbstractInsnNode> iterator = instructions.iterator();
LabelNode searchedLabel = null;
ArrayList<LabelNode> foundLabels = new ArrayList<LabelNode>();
while (iterator.hasNext()) {
AbstractInsnNode insn = iterator.next();
if (insn instanceof LabelNode)
foundLabels.add((LabelNode) insn);
if (areNodesEqual(insn, node)) {
int index = foundLabels.size() - 1;
index -= labelsBefore;
if (index >= 0)
searchedLabel = foundLabels.get(index);
labels += labelsBefore;
break;
}
}
if (searchedLabel != null) {
boolean found = false;
int labelCounter = 0;
iterator = instructions.iterator();
while (iterator.hasNext()) {
AbstractInsnNode insn = iterator.next();
if (found && insn instanceof LabelNode) {
labelCounter++;
if (labelCounter >= labels) {
if (replaceInstructions != null)
for (int i = 0; i < replaceInstructions.size(); i++) instructions.insertBefore(insn, replaceInstructions.get(i));
break;
}
}
if (insn == searchedLabel) {
found = true;
if (keepFirstLabel)
continue;
}
if (found)
if (deleteFrame || !(insn instanceof FrameNode))
instructions.remove(insn);
}
} else if (node instanceof LineNumberNode)
System.out.println("COULD NOT FIND NODE line=" + ((LineNumberNode) node).line);
}
Aggregations