List of usage examples for org.objectweb.asm.tree AbstractInsnNode VAR_INSN
int VAR_INSN
To view the source code for org.objectweb.asm.tree AbstractInsnNode VAR_INSN.
Click Source Link
From source file:blockphysics.asm.BPTransformer.java
License:Open Source License
private byte[] transformMinecraftServer(byte[] bytes) { /*try/*from w ww .j a v a 2 s . c o m*/ { FileOutputStream fos = new FileOutputStream("d:/MinecraftServer.orig.class"); fos.write(bytes); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ boolean bukkit = false; try { Class.forName("org.bukkit.Bukkit"); System.out.println("[BlockPhysics] Bukkit detected."); bukkit = true; } catch (ClassNotFoundException e) { } System.out.print("[BlockPhysics] Patching MinecraftServer.class ........."); boolean ok = false; boolean ok2 = false; ClassNode classNode = new ClassNode(); ClassReader classReader = new ClassReader(bytes); classReader.accept(classNode, 0); MethodNode m; Iterator<MethodNode> methods = classNode.methods.iterator(); while (methods.hasNext()) { m = methods.next(); if (m.name.equals("run") && m.desc.equals("()V")) { if (bukkit) { int index; for (index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && m.instructions.get(index).getOpcode() == INVOKESTATIC && ((MethodInsnNode) m.instructions.get(index)).owner.equals("java/lang/System") && ((MethodInsnNode) m.instructions.get(index)).name.equals("nanoTime")) { ok = true; break; } } if (ok) { while (index < m.instructions.size()) { if (m.instructions.get(index).getOpcode() == LSUB) { InsnList toInject = new InsnList(); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "setSkipMoveM", "(J)J")); m.instructions.insert(m.instructions.get(index), toInject); ok2 = true; break; } index++; } } } else { int index; for (index = 0; index < m.instructions.size(); index++) { if (m.instructions.get(index).getType() == AbstractInsnNode.METHOD_INSN && m.instructions.get(index).getOpcode() == INVOKEVIRTUAL && ((MethodInsnNode) m.instructions.get(index)).owner .equals("net/minecraft/server/MinecraftServer") && ((MethodInsnNode) m.instructions.get(index)).name.equals("s")) { ok = true; break; } } if (ok) { while (index > 0) { if (m.instructions.get(index).getOpcode() == LLOAD && m.instructions.get(index).getType() == AbstractInsnNode.VAR_INSN && ((VarInsnNode) m.instructions.get(index)).var == 3) { InsnList toInject = new InsnList(); toInject.add(new VarInsnNode(LLOAD, 7)); toInject.add(new MethodInsnNode(INVOKESTATIC, "blockphysics/BlockPhysics", "setSkipMove", "(J)V")); m.instructions.insertBefore(m.instructions.get(index), toInject); ok2 = true; break; } index--; } } } } } ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); classNode.accept(cw); if (ok && ok2) System.out.println("OK"); else System.out.println("Failed." + ok + ok2); /*try { FileOutputStream fos = new FileOutputStream("d:/MinecraftServer.class"); fos.write(cw.toByteArray()); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ return cw.toByteArray(); }
From source file:cl.inria.stiq.instrumenter.BCIUtils.java
License:Open Source License
private static void printFrames(MethodNode aNode, Frame[] aFrames) { int bcIndex = 1; for (int i = 0; i < aFrames.length; i++) { Frame theFrame = aFrames[i]; AbstractInsnNode theInsn = aNode.instructions.get(i); switch (theInsn.getType()) { case AbstractInsnNode.INSN: case AbstractInsnNode.INT_INSN: case AbstractInsnNode.VAR_INSN: case AbstractInsnNode.TYPE_INSN: case AbstractInsnNode.FIELD_INSN: case AbstractInsnNode.METHOD_INSN: case AbstractInsnNode.JUMP_INSN: case AbstractInsnNode.LDC_INSN: case AbstractInsnNode.IINC_INSN: case AbstractInsnNode.TABLESWITCH_INSN: case AbstractInsnNode.LOOKUPSWITCH_INSN: case AbstractInsnNode.MULTIANEWARRAY_INSN: TraceMethodVisitor theTraceVisitor = new TraceMethodVisitor(); theInsn.accept(theTraceVisitor); StringWriter theWriter = new StringWriter(); theTraceVisitor.print(new PrintWriter(theWriter)); String theTraced = theWriter.toString().replace("\n", ""); System.out.println(bcIndex + "\t" + frameString(theFrame) + " |\t" + theTraced); bcIndex++;/*from w ww . ja va 2s. co m*/ break; case AbstractInsnNode.FRAME: case AbstractInsnNode.LINE: case AbstractInsnNode.LABEL: break; } } }
From source file:com.android.tools.lint.checks.UnsafeBroadcastReceiverDetector.java
License:Apache License
@SuppressWarnings("rawtypes") public void checkClass(@NonNull final ClassContext context, @NonNull ClassNode classNode) { // If class isn't in mReceiversWithIntentFilter (set below by // XmlScanner), skip it. if (!mReceiversWithProtectedBroadcastIntentFilter.contains(classNode.name)) { return;/*from w ww. j a va2 s. c o m*/ } // Search for "void onReceive(android.content.Context, android.content.Intent)" method List methodList = classNode.methods; for (Object m : methodList) { MethodNode method = (MethodNode) m; if (!"onReceive".equals(method.name) || !"(Landroid/content/Context;Landroid/content/Intent;)V".equals(method.desc)) { continue; } // Search for call to getAction but also search for references to aload_2, // which indicates that the method is making use of the received intent in // some way. // // If the onReceive method doesn't call getAction but does make use of // the received intent, it is possible that it is passing it to another // method that might be performing the getAction check, so we warn that the // finding may be a false positive. (An alternative option would be to not // report a finding at all in this case.) boolean getActionEncountered = false; boolean intentParameterEncountered = false; InsnList nodes = method.instructions; for (int i = 0, n = nodes.size(); i < n; i++) { AbstractInsnNode instruction = nodes.get(i); int type = instruction.getType(); if (type == AbstractInsnNode.VAR_INSN) { VarInsnNode node = (VarInsnNode) instruction; if (node.getOpcode() == Opcodes.ALOAD) { if (node.var == 2) { intentParameterEncountered = true; } } } else if (type == AbstractInsnNode.METHOD_INSN) { MethodInsnNode node = (MethodInsnNode) instruction; if ("android/content/Intent".equals(node.owner) && "getAction".equals(node.name)) { getActionEncountered = true; break; } } } if (!getActionEncountered) { Location location = context.getLocation(method, classNode); String report; if (!intentParameterEncountered) { report = "This broadcast receiver declares an intent-filter for a protected " + "broadcast action string, which can only be sent by the system, " + "not third-party applications. However, the receiver's onReceive " + "method does not appear to call getAction to ensure that the " + "received Intent's action string matches the expected value, " + "potentially making it possible for another actor to send a " + "spoofed intent with no action string or a different action " + "string and cause undesired behavior."; } else { // An alternative implementation option is to not report a finding at all in // this case, if we are worried about false positives causing confusion or // resulting in developers ignoring other lint warnings. report = "This broadcast receiver declares an intent-filter for a protected " + "broadcast action string, which can only be sent by the system, " + "not third-party applications. However, the receiver's onReceive " + "method does not appear to call getAction to ensure that the " + "received Intent's action string matches the expected value, " + "potentially making it possible for another actor to send a " + "spoofed intent with no action string or a different action " + "string and cause undesired behavior. In this case, it is " + "possible that the onReceive method passed the received Intent " + "to another method that checked the action string. If so, this " + "finding can safely be ignored."; } context.report(ACTION_STRING, method, null, location, report); } } }
From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java
License:Open Source License
/** * This method detects what type of instruction the NodeInstruction is * representing and calls appropriate before/visit/after methods. * Subclasses could rather override methods specific for particular * instruction type./*w ww .j a v a2 s . com*/ * * @param nodeInstruction the instruction node */ public void visitNodeInstruction(NodeInstruction nodeInstruction) { AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode(); switch (asmInsn.getType()) { case AbstractInsnNode.LINE: visitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LABEL: visitLabelNode((LabelNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.FIELD_INSN: visitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.IINC_INSN: visitIincInsn((IincInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INSN: visitInsn((InsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INT_INSN: visitIntInsn((IntInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: visitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.JUMP_INSN: visitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LDC_INSN: visitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: visitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.METHOD_INSN: visitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: visitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TABLESWITCH_INSN: visitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TYPE_INSN: visitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.VAR_INSN: visitVarInsn((VarInsnNode) asmInsn, nodeInstruction); break; default: // shouldn't happen break; } }
From source file:cz.vutbr.fit.xhriba01.bc.lib.AbstractNodeVisitor.java
License:Open Source License
public void afterVisitNodeInstruction(NodeInstruction nodeInstruction) { AbstractInsnNode asmInsn = nodeInstruction.getAsmInsnNode(); switch (asmInsn.getType()) { case AbstractInsnNode.LINE: afterVisitLineNumberNode((LineNumberNode) asmInsn, nodeInstruction); break;/*from ww w. jav a2 s . c o m*/ case AbstractInsnNode.LABEL: afterVisitLabelNode((LabelNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.FIELD_INSN: afterVisitFieldInsn((FieldInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.IINC_INSN: afterVisitIincInsn((IincInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INSN: afterVisitInsn((InsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INT_INSN: afterVisitIntInsn((IntInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: afterVisitInvokeDynamicInsn((InvokeDynamicInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.JUMP_INSN: afterVisitJumpInsn((JumpInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LDC_INSN: afterVisitLdcInsn((LdcInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: afterVisitLookupSwitchInsn((LookupSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.METHOD_INSN: afterVisitMethodInsn((MethodInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: afterVisitMultiANewArrayInsn((MultiANewArrayInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TABLESWITCH_INSN: afterVisitTableSwitchInsn((TableSwitchInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.TYPE_INSN: afterVisitTypeInsn((TypeInsnNode) asmInsn, nodeInstruction); break; case AbstractInsnNode.VAR_INSN: afterVisitVarInsn((VarInsnNode) asmInsn, nodeInstruction); break; default: // shouldn't happen break; } }
From source file:de.codesourcery.asm.util.Disassembler.java
License:Apache License
private static String disassemble(AbstractInsnNode node, MethodNode method) { final int opCode = node.getOpcode(); String mnemonic = Printer.OPCODES[opCode]; switch (node.getType()) { case AbstractInsnNode.FIELD_INSN: // GETSTATIC, PUTSTATIC, GETFIELD , PUTFIELD FieldInsnNode tmp = (FieldInsnNode) node; mnemonic += " " + (tmp.owner + "#" + tmp.name); break;/*from w ww. jav a2s .c o m*/ case AbstractInsnNode.IINC_INSN: // IINC IincInsnNode tmp2 = (IincInsnNode) node; mnemonic += " " + (tmp2.var + " , " + tmp2.incr); break; case AbstractInsnNode.INSN: // regular opcodes break; case AbstractInsnNode.INT_INSN: // BIPUSH, SIPUSH or NEWARRAY IntInsnNode tmp3 = (IntInsnNode) node; mnemonic += " " + (tmp3.operand); break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: // INVOKEDYNAMIC break; case AbstractInsnNode.JUMP_INSN: // IFEQ, IFNE, IFLT, IFGE, IFGT, IFLE, IF_ICMPEQ,IF_ICMPNE, IF_ICMPLT, IF_ICMPGE, IF_ICMPGT, IF_ICMPLE,IF_ACMPEQ, IF_ACMPNE, GOTO, JSR, IFNULL or IFNONNULL JumpInsnNode tmp4 = (JumpInsnNode) node; int index = method.instructions.indexOf(tmp4.label); while (method.instructions.get(index).getOpcode() == -1) { index++; } mnemonic += " " + index; break; case AbstractInsnNode.LDC_INSN: // load constant LdcInsnNode tmp5 = (LdcInsnNode) node; Class<?> clazz = tmp5.cst.getClass(); if (clazz == String.class) { mnemonic += " \"" + tmp5.cst + "\""; } else if (clazz == org.objectweb.asm.Type.class) { org.objectweb.asm.Type type = (org.objectweb.asm.Type) tmp5.cst; mnemonic += " (a " + type.getClassName() + ")"; } else { mnemonic += " " + tmp5.cst + " (" + tmp5.cst.getClass().getName() + ")"; } break; case AbstractInsnNode.LOOKUPSWITCH_INSN: // LOOKUPSWITCH break; case AbstractInsnNode.METHOD_INSN: // INVOKEVIRTUAL, INVOKESPECIAL, INVOKESTATIC , INVOKEINTERFACE MethodInsnNode tmp6 = (MethodInsnNode) node; mnemonic += " " + (tmp6.owner + "#" + tmp6.name + "()"); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: // MULTIANEWARRAY break; case AbstractInsnNode.TABLESWITCH_INSN: // TABLESWITCH break; case AbstractInsnNode.TYPE_INSN: // NEW, ANEWARRAY, CHECKCAST , INSTANCEOF TypeInsnNode tmp8 = (TypeInsnNode) node; mnemonic += " " + tmp8.desc; break; case AbstractInsnNode.VAR_INSN: // ILOAD, LLOAD, FLOAD, DLOAD, ALOAD, ISTORE, LSTORE, FSTORE, DSTORE, ASTORE , RET VarInsnNode tmp7 = (VarInsnNode) node; mnemonic += "_" + tmp7.var; break; // -- VIRTUAL -- case AbstractInsnNode.FRAME: /* VIRTUAL */ case AbstractInsnNode.LABEL: /* VIRTUAL */ case AbstractInsnNode.LINE: /* VIRTUAL */ default: throw new RuntimeException("Internal error, unhandled node type: " + node); } return mnemonic; }
From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.PauseTracingInstrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") public void transformMethod(final MethodNode method, final ListIterator<MethodNode> methodIt, final String className) { if ((method.access & ACC_ABSTRACT) != 0 || (method.access & ACC_NATIVE) != 0) return;//from www.ja v a 2 s.c o m int tracerLocalVarIndex = (method.access & Opcodes.ACC_STATIC) == 0 ? 1 : 0; for (final Type t : Type.getArgumentTypes(method.desc)) tracerLocalVarIndex += t.getSize(); // increment number of local variables by one (for the threadtracer) ++method.maxLocals; // and increment all local variable indexes after the new one by one for (final Object o : method.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; if (localVar.index >= tracerLocalVarIndex) ++localVar.index; } final LabelNode l0 = new LabelNode(); final LabelNode l1 = new LabelNode(); final ListIterator<AbstractInsnNode> insnIt = method.instructions.iterator(); insnIt.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance", "()L" + Type.getInternalName(Tracer.class) + ";", false)); insnIt.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer", "()L" + Type.getInternalName(ThreadTracer.class) + ";", false)); insnIt.add(new InsnNode(DUP)); insnIt.add(new VarInsnNode(ASTORE, tracerLocalVarIndex)); insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "pauseTracing", "()V", true)); insnIt.add(l0); while (insnIt.hasNext()) { final AbstractInsnNode insn = insnIt.next(); switch (insn.getType()) { case AbstractInsnNode.INSN: switch (insn.getOpcode()) { case IRETURN: case LRETURN: case FRETURN: case DRETURN: case ARETURN: case RETURN: insnIt.previous(); insnIt.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); insnIt.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "resumeTracing", "()V", true)); insnIt.next(); } break; case AbstractInsnNode.IINC_INSN: if (((IincInsnNode) insn).var >= tracerLocalVarIndex) ++((IincInsnNode) insn).var; break; case AbstractInsnNode.VAR_INSN: if (((VarInsnNode) insn).var >= tracerLocalVarIndex) ++((VarInsnNode) insn).var; break; default: break; } } method.instructions.add(l1); method.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); method.instructions.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "resumeTracing", "()V", true)); method.instructions.add(new InsnNode(ATHROW)); method.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null)); // finally: create a copy of the method that gets the ThreadTracer as argument if (!"<clinit>".equals(method.name) && this.tracer.wasRedefined(className)) { final Type[] oldMethodArguments = Type.getArgumentTypes(method.desc); final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1); newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class); final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(method.desc), newMethodArguments); final MethodNode newMethod = new MethodNode(method.access, method.name, newMethodDesc, method.signature, (String[]) method.exceptions.toArray(new String[method.exceptions.size()])); methodIt.add(newMethod); final Map<LabelNode, LabelNode> newMethodLabels = new LazyLabelMap(); // copy the local variables information to the new method for (final Object o : method.localVariables) { final LocalVariableNode lv = (LocalVariableNode) o; newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature, newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index)); } newMethod.maxLocals = method.maxLocals; newMethod.maxStack = method.maxStack; // copy the try-catch-blocks for (final Object o : method.tryCatchBlocks) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start), newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type)); } // skip the first 4 instructions, replace them with this: newMethod.instructions.add(new VarInsnNode(ALOAD, tracerLocalVarIndex)); final Iterator<AbstractInsnNode> oldInsnIt = method.instructions.iterator(4); // and add all the other instructions while (oldInsnIt.hasNext()) { final AbstractInsnNode insn = oldInsnIt.next(); newMethod.instructions.add(insn.clone(newMethodLabels)); } } }
From source file:de.unisb.cs.st.javaslicer.tracer.instrumentation.TracingMethodInstrumenter.java
License:Open Source License
@SuppressWarnings("unchecked") public void transform(final ListIterator<MethodNode> methodIt) { // do not modify abstract or native methods if ((this.methodNode.access & ACC_ABSTRACT) != 0 || (this.methodNode.access & ACC_NATIVE) != 0) return;//from w w w. ja v a 2s.c o m // check out what labels are jump targets (only these have to be traced) analyze(this.methodNode); this.instructionIterator = new FixedInstructionIterator(this.methodNode.instructions); // in the old method, initialize the new local variable for the threadtracer this.instructionIterator.add(new MethodInsnNode(INVOKESTATIC, Type.getInternalName(Tracer.class), "getInstance", "()L" + Type.getInternalName(Tracer.class) + ";", false)); this.instructionIterator.add(new MethodInsnNode(INVOKEVIRTUAL, Type.getInternalName(Tracer.class), "getThreadTracer", "()L" + Type.getInternalName(ThreadTracer.class) + ";", false)); this.instructionIterator.add(new InsnNode(DUP)); this.instructionIterator.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); this.instructionIterator.add(new MethodInsnNode(INVOKEINTERFACE, Type.getInternalName(ThreadTracer.class), "isPaused", "()Z", true)); final LabelNode noTracingLabel = new LabelNode(); this.instructionIterator.add(new JumpInsnNode(IFNE, noTracingLabel)); // create a copy of the (uninstrumented) instructions (later, while iterating through the instructions) final InsnList oldInstructions = new InsnList(); final Map<LabelNode, LabelNode> labelCopies = new LazyLabelMap(); // copy the try-catch-blocks final Object[] oldTryCatchblockNodes = this.methodNode.tryCatchBlocks.toArray(); for (final Object o : oldTryCatchblockNodes) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; final TryCatchBlockNode newTcb = new TryCatchBlockNode(labelCopies.get(tcb.start), labelCopies.get(tcb.end), labelCopies.get(tcb.handler), tcb.type); this.methodNode.tryCatchBlocks.add(newTcb); } // increment number of local variables by one (for the threadtracer) ++this.methodNode.maxLocals; // and increment all local variable indexes after the new one by one for (final Object o : this.methodNode.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; if (localVar.index >= this.tracerLocalVarIndex) ++localVar.index; } // store information about local variables in the ReadMethod object List<LocalVariable> localVariables = new ArrayList<LocalVariable>(); for (final Object o : this.methodNode.localVariables) { final LocalVariableNode localVar = (LocalVariableNode) o; while (localVariables.size() <= localVar.index) localVariables.add(null); localVariables.set(localVar.index, new LocalVariable(localVar.index, localVar.name, localVar.desc)); } this.readMethod.setLocalVariables(localVariables.toArray(new LocalVariable[localVariables.size()])); localVariables = null; // each method must start with a (dedicated) label: assert this.readMethod.getInstructions().isEmpty(); traceLabel(null, InstructionType.METHODENTRY); assert this.readMethod.getInstructions().size() == 1 && this.readMethod.getInstructions().get(0) instanceof LabelMarker && ((LabelMarker) this.readMethod.getInstructions().get(0)).isAdditionalLabel(); this.readMethod.setMethodEntryLabel((LabelMarker) this.readMethod.getInstructions().get(0)); // needed later: final LabelNode l0 = new LabelNode(); this.instructionIterator.add(l0); // then, visit the instructions that were in the method before while (this.instructionIterator.hasNext()) { final AbstractInsnNode insnNode = this.instructionIterator.next(); switch (insnNode.getType()) { case AbstractInsnNode.INSN: transformInsn((InsnNode) insnNode); break; case AbstractInsnNode.INT_INSN: transformIntInsn((IntInsnNode) insnNode); break; case AbstractInsnNode.VAR_INSN: transformVarInsn((VarInsnNode) insnNode); break; case AbstractInsnNode.TYPE_INSN: transformTypeInsn((TypeInsnNode) insnNode); break; case AbstractInsnNode.FIELD_INSN: transformFieldInsn((FieldInsnNode) insnNode); break; case AbstractInsnNode.METHOD_INSN: transformMethodInsn((MethodInsnNode) insnNode); break; case AbstractInsnNode.JUMP_INSN: transformJumpInsn((JumpInsnNode) insnNode); break; case AbstractInsnNode.LABEL: transformLabel((LabelNode) insnNode); break; case AbstractInsnNode.LDC_INSN: transformLdcInsn((LdcInsnNode) insnNode); break; case AbstractInsnNode.IINC_INSN: transformIincInsn((IincInsnNode) insnNode); break; case AbstractInsnNode.TABLESWITCH_INSN: transformTableSwitchInsn((TableSwitchInsnNode) insnNode); break; case AbstractInsnNode.LOOKUPSWITCH_INSN: transformLookupSwitchInsn((LookupSwitchInsnNode) insnNode); break; case AbstractInsnNode.MULTIANEWARRAY_INSN: transformMultiANewArrayInsn((MultiANewArrayInsnNode) insnNode); break; case AbstractInsnNode.FRAME: // ignore break; case AbstractInsnNode.LINE: // ignore break; default: throw new RuntimeException("Unknown instruction type " + insnNode.getType() + " (" + insnNode.getClass().getSimpleName() + ")"); } oldInstructions.add(insnNode.clone(labelCopies)); } assert this.outstandingInitializations == 0; // add the (old) try-catch blocks to the readMethods // (can only be done down here since we use the information in the // labels map) for (final Object o : oldTryCatchblockNodes) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; this.readMethod.addTryCatchBlock(new TryCatchBlock(this.labels.get(tcb.start), this.labels.get(tcb.end), this.labels.get(tcb.handler), tcb.type)); } final LabelNode l1 = new LabelNode(); this.instructionIterator.add(l1); final int newPos = this.readMethod.getInstructions().size(); traceLabel(null, InstructionType.METHODEXIT); assert this.readMethod.getInstructions().size() == newPos + 1; final AbstractInstruction abnormalTerminationLabel = this.readMethod.getInstructions().get(newPos); assert abnormalTerminationLabel instanceof LabelMarker; this.readMethod.setAbnormalTerminationLabel((LabelMarker) abnormalTerminationLabel); this.methodNode.instructions.add(new InsnNode(ATHROW)); // add a try catch block around the method so that we can trace when this method is left // by a thrown exception this.methodNode.tryCatchBlocks.add(new TryCatchBlockNode(l0, l1, l1, null)); // now add the code that is executed if no tracing should be performed this.methodNode.instructions.add(noTracingLabel); if (this.firstLine != -1) this.methodNode.instructions.add(new LineNumberNode(this.firstLine, noTracingLabel)); this.methodNode.instructions.add(new InsnNode(ACONST_NULL)); this.methodNode.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); this.methodNode.instructions.add(oldInstructions); // finally: create a copy of the method that gets the ThreadTracer as argument // this is only necessary for private methods or "<init>" if (this.tracer.wasRedefined(this.readMethod.getReadClass().getName()) && (this.methodNode.access & ACC_PRIVATE) != 0) { final Type[] oldMethodArguments = Type.getArgumentTypes(this.methodNode.desc); final Type[] newMethodArguments = Arrays.copyOf(oldMethodArguments, oldMethodArguments.length + 1); newMethodArguments[oldMethodArguments.length] = Type.getType(ThreadTracer.class); final String newMethodDesc = Type.getMethodDescriptor(Type.getReturnType(this.methodNode.desc), newMethodArguments); final MethodNode newMethod = new MethodNode(this.methodNode.access, this.methodNode.name, newMethodDesc, this.methodNode.signature, (String[]) this.methodNode.exceptions.toArray(new String[this.methodNode.exceptions.size()])); methodIt.add(newMethod); int threadTracerParamPos = ((this.readMethod.getAccess() & Opcodes.ACC_STATIC) == 0 ? 1 : 0); for (final Type t : oldMethodArguments) threadTracerParamPos += t.getSize(); final Map<LabelNode, LabelNode> newMethodLabels = new LazyLabelMap(); // copy the local variables information to the new method for (final Object o : this.methodNode.localVariables) { final LocalVariableNode lv = (LocalVariableNode) o; newMethod.localVariables.add(new LocalVariableNode(lv.name, lv.desc, lv.signature, newMethodLabels.get(lv.start), newMethodLabels.get(lv.end), lv.index)); } newMethod.maxLocals = this.methodNode.maxLocals; newMethod.maxStack = this.methodNode.maxStack; // copy the try-catch-blocks for (final Object o : this.methodNode.tryCatchBlocks) { final TryCatchBlockNode tcb = (TryCatchBlockNode) o; newMethod.tryCatchBlocks.add(new TryCatchBlockNode(newMethodLabels.get(tcb.start), newMethodLabels.get(tcb.end), newMethodLabels.get(tcb.handler), tcb.type)); } // skip the first 6 instructions, replace them with these: newMethod.instructions.add(new VarInsnNode(ALOAD, threadTracerParamPos)); newMethod.instructions.add(new InsnNode(DUP)); newMethod.instructions.add(new VarInsnNode(ASTORE, this.tracerLocalVarIndex)); newMethod.instructions.add(new JumpInsnNode(IFNULL, newMethodLabels.get(noTracingLabel))); final Iterator<AbstractInsnNode> oldInsnIt = this.methodNode.instructions.iterator(6); // and add all the other instructions while (oldInsnIt.hasNext()) { final AbstractInsnNode insn = oldInsnIt.next(); newMethod.instructions.add(insn.clone(newMethodLabels)); } } ready(); }
From source file:jaspex.transactifier.ChangeClinitMethodVisitor.java
License:Open Source License
private static boolean clinitIsSafe(Type t) { try {//from w w w . j av a 2 s . c om ClassReader cr = new ClassReader(t.commonName()); ClassNode cNode = new ClassNode(); cr.accept(cNode, 0); for (MethodNode method : cNode.methods) { if (!method.name.equals("<clinit>")) continue; // Examinar instruces Iterator<AbstractInsnNode> it = method.instructions.iterator(); while (it.hasNext()) { AbstractInsnNode insn = it.next(); switch (insn.getType()) { case AbstractInsnNode.FRAME: case AbstractInsnNode.INT_INSN: case AbstractInsnNode.JUMP_INSN: case AbstractInsnNode.LABEL: case AbstractInsnNode.LDC_INSN: case AbstractInsnNode.LINE: case AbstractInsnNode.LOOKUPSWITCH_INSN: case AbstractInsnNode.MULTIANEWARRAY_INSN: case AbstractInsnNode.TABLESWITCH_INSN: case AbstractInsnNode.TYPE_INSN: case AbstractInsnNode.VAR_INSN: break; case AbstractInsnNode.FIELD_INSN: FieldInsnNode fieldInsn = (FieldInsnNode) insn; if (fieldInsn.getOpcode() != PUTSTATIC) { // GETSTATIC, GETFIELD, PUTFIELD return false; } break; case AbstractInsnNode.IINC_INSN: return false; case AbstractInsnNode.INSN: if (unsafeInsnBytecodes.contains(insn.getOpcode())) { Log.debug(t.commonName() + ".<clinit>() is unsafe " + "because of bytecode " + insn.getOpcode()); return false; } break; case AbstractInsnNode.METHOD_INSN: MethodInsnNode methodInsn = (MethodInsnNode) insn; if (!ClassFilter.isMethodWhitelisted(Type.fromAsm(methodInsn.owner), methodInsn.name, methodInsn.desc)) { Log.debug(t.commonName() + ".<clinit>() is unsafe " + "because it invokes " + Type.fromAsm(methodInsn.owner).commonName() + "." + methodInsn.name); return false; } break; default: throw new Error("Unexpected bytecode " + insn); } } //Log.debug(t.commonName() + ".<clinit>() for " + t + " is safe"); return true; } return false; } catch (IOException e) { throw new Error(e); } }
From source file:org.lambdamatic.analyzer.ast.LambdaExpressionReader.java
License:Open Source License
/** * Reads the bytecode from the given {@link InsnCursor}'s <strong>current position</strong>, until * there is no further instruction to proceed. It is the responsability of the caller to set the * cursor position./*w w w .j a v a 2 s. co m*/ * * @param insnCursor the instruction cursor used to read the bytecode. * @param expressionStack the expression stack to put on or pop from. * @param localVariables the local variables * @return a {@link List} of {@link Statement} containing the {@link Statement} */ private List<Statement> readStatements(final InsnCursor insnCursor, final Stack<Expression> expressionStack, final List<CapturedArgument> capturedArguments, final LocalVariables localVariables) { final List<Statement> statements = new ArrayList<>(); while (insnCursor.hasCurrent()) { final AbstractInsnNode currentInstruction = insnCursor.getCurrent(); switch (currentInstruction.getType()) { case AbstractInsnNode.VAR_INSN: final VarInsnNode varInstruction = (VarInsnNode) currentInstruction; switch (currentInstruction.getOpcode()) { // load a reference onto the stack from a local variable case Opcodes.ALOAD: case Opcodes.ILOAD: // load an int value from a local variable // Note: The 'var' operand is the index of a local variable // all captured arguments come before the local variable in the method signature, // which means that the local variables table is empty on the first slots which are // "allocated" // for the captured arguments. if (varInstruction.var < capturedArguments.size()) { // if the variable index matches a captured argument // note: not using actual captured argument but rather, use a _reference_ to it. final Object capturedArgumentValue = capturedArguments.get(varInstruction.var).getValue(); final Class<?> capturedArgumentValueType = capturedArgumentValue != null ? capturedArgumentValue.getClass() : Object.class; final CapturedArgumentRef capturedArgumentRef = new CapturedArgumentRef(varInstruction.var, capturedArgumentValueType); expressionStack.add(capturedArgumentRef); } else { // the variable index matches a local variable final LocalVariableNode var = localVariables.load(varInstruction.var); expressionStack.add(new LocalVariable(var.index, var.name, readSignature(var.desc))); } break; case Opcodes.ASTORE: // store a reference into a local variable localVariables.store(varInstruction.var); break; default: throw new AnalyzeException( "Unexpected Variable instruction code: " + varInstruction.getOpcode()); } break; case AbstractInsnNode.LDC_INSN: // let's move this instruction on top of the stack until it // is used as an argument during a method call final LdcInsnNode ldcInsnNode = (LdcInsnNode) currentInstruction; final Expression constant = ExpressionFactory.getExpression(ldcInsnNode.cst); LOGGER.trace("Stacking constant {}", constant); expressionStack.add(constant); break; case AbstractInsnNode.FIELD_INSN: final FieldInsnNode fieldInsnNode = (FieldInsnNode) currentInstruction; switch (fieldInsnNode.getOpcode()) { case Opcodes.GETSTATIC: final Type ownerType = Type.getType(fieldInsnNode.desc); final FieldAccess staticFieldAccess = new FieldAccess(new ClassLiteral(getType(ownerType)), fieldInsnNode.name); expressionStack.add(staticFieldAccess); break; case Opcodes.GETFIELD: final Expression fieldAccessParent = expressionStack.pop(); final FieldAccess fieldAccess = new FieldAccess(fieldAccessParent, fieldInsnNode.name); expressionStack.add(fieldAccess); break; case Opcodes.PUTFIELD: final Expression fieldAssignationValue = expressionStack.pop(); final Expression parentSource = expressionStack.pop(); final FieldAccess source = new FieldAccess(parentSource, fieldInsnNode.name); final Assignment assignmentExpression = new Assignment(source, fieldAssignationValue); statements.add(new ExpressionStatement(assignmentExpression)); break; default: throw new AnalyzeException("Unexpected field instruction type: " + fieldInsnNode.getOpcode()); } break; case AbstractInsnNode.METHOD_INSN: final MethodInsnNode methodInsnNode = (MethodInsnNode) currentInstruction; final Type[] argumentTypes = Type.getArgumentTypes(methodInsnNode.desc); final List<Expression> args = new ArrayList<>(); final List<Class<?>> parameterTypes = new ArrayList<>(); Stream.of(argumentTypes).forEach(argumentType -> { final Expression arg = expressionStack.pop(); final String argumentClassName = argumentType.getClassName(); args.add(castOperand(arg, argumentClassName)); try { parameterTypes.add(ClassUtils.getClass(argumentClassName)); } catch (Exception e) { throw new AnalyzeException("Failed to find class '" + argumentClassName + "'", e); } }); // arguments appear in reverse order in the bytecode Collections.reverse(args); switch (methodInsnNode.getOpcode()) { case Opcodes.INVOKEINTERFACE: case Opcodes.INVOKEVIRTUAL: case Opcodes.INVOKESPECIAL: // object instantiation if (methodInsnNode.name.equals("<init>")) { final ObjectInstanciation objectVariable = (ObjectInstanciation) expressionStack.pop(); objectVariable.setInitArguments(args); } else { final Expression sourceExpression = expressionStack.pop(); final Method javaMethod = ReflectionUtils.findJavaMethod(sourceExpression.getJavaType(), methodInsnNode.name, parameterTypes); final Class<?> returnType = findReturnType(insnCursor, javaMethod); final MethodInvocation invokedMethod = new MethodInvocation(sourceExpression, javaMethod, returnType, args); expressionStack.add(invokedMethod); } break; case Opcodes.INVOKESTATIC: final Type type = Type.getObjectType(methodInsnNode.owner); try { final Class<?> sourceClass = Class.forName(type.getClassName()); final Method javaMethod = ReflectionUtils.findJavaMethod(sourceClass, methodInsnNode.name, parameterTypes); final Class<?> returnType = findReturnType(insnCursor, javaMethod); final MethodInvocation invokedStaticMethod = new MethodInvocation( new ClassLiteral(sourceClass), javaMethod, returnType, args); expressionStack.add(invokedStaticMethod); } catch (ClassNotFoundException e) { throw new AnalyzeException("Failed to retrieve class for " + methodInsnNode.owner, e); } break; default: throw new AnalyzeException("Unexpected method invocation type: " + methodInsnNode.getOpcode()); } break; case AbstractInsnNode.INVOKE_DYNAMIC_INSN: final InvokeDynamicInsnNode invokeDynamicInsnNode = (InvokeDynamicInsnNode) currentInstruction; final Handle handle = (Handle) invokeDynamicInsnNode.bsmArgs[1]; final int argNumber = Type.getArgumentTypes(invokeDynamicInsnNode.desc).length; final List<CapturedArgumentRef> lambdaArgs = new ArrayList<>(); for (int i = 0; i < argNumber; i++) { final Expression expr = expressionStack.pop(); if (expr.getExpressionType() != ExpressionType.CAPTURED_ARGUMENT_REF) { throw new AnalyzeException("Unexpected argument type when following InvokeDynamic call: " + expr.getExpressionType()); } lambdaArgs.add((CapturedArgumentRef) expr); // , expr.getValue() } Collections.reverse(lambdaArgs); final EmbeddedSerializedLambdaInfo lambdaInfo = new EmbeddedSerializedLambdaInfo(handle.getOwner(), handle.getName(), handle.getDesc(), lambdaArgs, capturedArguments); final LambdaExpression lambdaExpression = LambdaExpressionAnalyzer.getInstance() .analyzeExpression(lambdaInfo); expressionStack.add(lambdaExpression); break; case AbstractInsnNode.JUMP_INSN: statements.addAll( readJumpInstruction(insnCursor, expressionStack, capturedArguments, localVariables)); return statements; case AbstractInsnNode.INT_INSN: readIntInstruction((IntInsnNode) currentInstruction, expressionStack, localVariables); break; case AbstractInsnNode.INSN: final List<Statement> instructionStatement = readInstruction(insnCursor, expressionStack, capturedArguments, localVariables); statements.addAll(instructionStatement); break; case AbstractInsnNode.TYPE_INSN: readTypeInstruction((TypeInsnNode) currentInstruction, expressionStack, localVariables); break; default: throw new AnalyzeException( "This is embarrassing... We've reached an unexpected instruction operator: " + currentInstruction.getType()); } insnCursor.next(); } return statements; }