private void buildHandlerMap() { handlerMap = new int[instructions.size()]; for (int ki=0; ki < handlerMap.length; ki++) handlerMap[ki] = -1; int ki = 0; for (int kh=0; kh < origHandlers.size(); kh++) { Handler ho = origHandlers.get(kh); for (; ki <= ho.from; ki++) handlerMap[ki] = kh; } }
@Override public void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack) { frameNodes.put(instructions.size(), new FrameNode(type, nLocal, local, nStack, stack)); }
@SuppressWarnings({"rawtypes", "unchecked"}) public void forEach() { for (int i = 0; i < instructions.size(); i++) { AbstractInsnNode it = instructions.get(i); for (Entry<Predicate, Consumer> consumer : consumers.entrySet()) { if (consumer.getKey().test(it)) { consumer.getValue().accept(new Insn<>(method, it)); } } } }
private void prepare(MethodNode method) { InsnList instructions = method.instructions; minLocal = 0; if ((method.access & Opcodes.ACC_STATIC) != 0) { minLocal = 1; } labelIndexes = new HashMap<>(); lineNumbers = new HashMap<>(); for (int i = 0; i < instructions.size(); ++i) { AbstractInsnNode node = instructions.get(i); if (node instanceof LabelNode) { labelIndexes.put(((LabelNode) node).getLabel(), i); } if (node instanceof LineNumberNode) { LineNumberNode lineNumberNode = (LineNumberNode) node; lineNumbers.put(lineNumberNode.start.getLabel(), lineNumberNode.line); } } for (LocalVariableNode localVar : method.localVariables) { int location = labelIndexes.get(localVar.start.getLabel()); localVariableMap.computeIfAbsent(location, k -> new ArrayList<>()).add(localVar); } targetInstructions = new ArrayList<>(instructions.size()); targetInstructions.addAll(Collections.nCopies(instructions.size(), null)); basicBlocks.addAll(Collections.nCopies(instructions.size(), null)); stackBefore = new StackFrame[instructions.size()]; stackAfter = new StackFrame[instructions.size()]; }
@Override public void visitLineNumber(int line, Label start) { LabelNode ln = getLabelNode(start); lineNumberNodes.put(instructions.size(), new LineNumberNode(line, ln)); }
private boolean checkNoBasicBlockLeftBehind() { // like "no child left behind" ArrayList<BasicBlock> bbs = basicBlocks; HashSet<BasicBlock> hs = new HashSet<BasicBlock>(bbs.size() * 2); hs.addAll(bbs); int prevBBend = -1; for (BasicBlock bb: bbs) { assert bb.isInitialized() : "BB not inited: " + bb; assert bb.startPos == prevBBend + 1; for (BasicBlock succ: bb.successors) { assert succ.isInitialized() : "Basic block not inited: " + succ +"\nSuccessor of " + bb; assert hs.contains(succ) : "BB not found:\n" + succ; } prevBBend = bb.endPos; } assert bbs.get(bbs.size()-1).endPos == instructions.size()-1; return true; }
public byte[] transformCode(byte[] b1, String className) throws IOException { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS); ClassReader cr = new ClassReader(b1); ClassNode classNode = new ClassNode(); Map<String, Boolean> methodInstructionTypeMap = new HashMap<>(); cr.accept(classNode, 0); final List<MethodNode> methods = classNode.methods; for (MethodNode m : methods) { InsnList inList = m.instructions; boolean isMethodInvoke = false; for (int i = 0; i < inList.size(); i++) { if (inList.get(i).getType() == AbstractInsnNode.METHOD_INSN) { isMethodInvoke = true; } } methodInstructionTypeMap.put(m.name + m.desc, isMethodInvoke); } InsertMethodBodyAdapter insertMethodBodyAdapter = new InsertMethodBodyAdapter(cw, className, methodInstructionTypeMap); cr.accept(insertMethodBodyAdapter, ClassReader.EXPAND_FRAMES); return cw.toByteArray(); }
private int findMethodCall(InsnList insns, LabelNode label) { boolean foundLabel = false; for (int i = 0; i < insns.size(); i++) { AbstractInsnNode n = insns.get(i); if (!foundLabel && n == label) { foundLabel = true; } else if (foundLabel && n.getOpcode() == Opcodes.INVOKEDYNAMIC) { return i; } } return -1; }
@Override public void visitLabel(Label label) { setLabel(instructions.size(), super.getLabelNode(label)); }
@Override public void visitMethodInsn(int opcode, String owner, String name, String desc,boolean itf) { super.visitMethodInsn(opcode, owner, name, desc, itf); // The only reason for adding to pausableMethods is to create a BB for pausable // method call sites. If the class is already woven, we don't need this // functionality. if (!classFlow.isWoven) { int methodStatus = detector.getPausableStatus(owner, name, desc); if (methodStatus == Detector.PAUSABLE_METHOD_FOUND) { MethodInsnNode min = (MethodInsnNode)instructions.get(instructions.size()-1); pausableMethods.add(min); } } }
TraceMethodVisitor mv = new TraceMethodVisitor(t); String format = "%05d %-" + (method.maxStack + method.maxLocals + 6) + "s|%s"; for (int j = 0; j < method.instructions.size(); ++j) { method.instructions.get(j).accept(mv);
IrMethod convert0(String owner, MethodNode methodNode) throws AnalyzerException { this.methodNode = methodNode; target = populate(owner, methodNode); if (methodNode.instructions.size() == 0) { return target; BitSet[] exBranch = new BitSet[insnList.size()]; parentCount = new int[insnList.size()]; BitSet handlers = new BitSet(insnList.size()); if (methodNode.tryCatchBlocks != null) { for (TryCatchBlockNode tcb : methodNode.tryCatchBlocks) { x = exBranch[insnList.indexOf(p)] = new BitSet(insnList.size()); frames = new JvmFrame[insnList.size()]; emitStmts = new ArrayList[insnList.size()]; BitSet access = new BitSet(insnList.size()); for (int i = 0; i < insnList.size(); i++) { AbstractInsnNode p = insnList.get(i); if (access.get(i)) {
for (int i = 0; i < methods.size(); ++i) { MethodNode method = (MethodNode) methods.get(i); if (method.instructions.size() > 0) { Analyzer a = new Analyzer(new SimpleVerifier()); try { for (int j = 0; j < method.instructions.size(); ++j) { Object insn = method.instructions.get(j); if (insn instanceof AbstractInsnNode) {
void buildBasicBlocks() { // preparatory phase int numInstructions = instructions.size(); basicBlocks = new BBList(); // Note: i modified within the loop for (int i = 0; i < numInstructions; i++) { LabelNode l = getOrCreateLabelAtPos(i); BasicBlock bb = getOrCreateBasicBlock(l); i = bb.initialize(i); // i now points to the last instruction in bb. basicBlocks.add(bb); } }
private void dataFlow() { workset = new PriorityQueue<BasicBlock>(instructions.size(), new BBComparator()); //System.out.println("Method: " + this.name); BasicBlock startBB = getBasicBlocks().get(0); assert startBB != null : "Null starting block in flowTypes()"; startBB.startFrame = new Frame(classFlow.getClassDescriptor(), this); enqueue(startBB); while (!workset.isEmpty()) { BasicBlock bb = dequeue(); bb.interpret(); } }
int nrInsn = instructions.size(); nextLocal = maxLocals; locals = new HashMap<Integer, Local>(maxLocals + (maxLocals / 2));
static void printAnalyzerResult(MethodNode method, Analyzer a, final PrintWriter pw) throws IllegalArgumentException, IllegalAccessException { Frame[] frames = a.getFrames(); Textifier t = new Textifier(); TraceMethodVisitor mv = new TraceMethodVisitor(t); String format = "%05d %-" + (method.maxStack + method.maxLocals + 6) + "s|%s"; for (int j = 0; j < method.instructions.size(); ++j) { method.instructions.get(j).accept(mv); StringBuffer s = new StringBuffer(); Frame f = frames[j]; if (f == null) { s.append('?'); } else { for (int k = 0; k < f.getLocals(); ++k) { s.append(getShortName(f.getLocal(k).toString())); } s.append(" : "); for (int k = 0; k < f.getStackSize(); ++k) { s.append(getShortName(f.getStack(k).toString())); } } pw.printf(format, j, s, buf.get(t)); // mv.text.get(j)); } for (int j = 0; j < method.tryCatchBlocks.size(); ++j) { ((TryCatchBlockNode) method.tryCatchBlocks.get(j)).accept(mv); pw.print(" " + buf.get(t)); } pw.println(); pw.flush(); }
public void restoreNonInstructionNodes() { InsnList newinsns = new InsnList(); int sz = instructions.size(); for (int i = 0; i < sz; i++) { LabelNode l = getLabelAt(i); if (l != null) { newinsns.add(l); } LineNumberNode ln = lineNumberNodes.get(i); if (ln != null) { newinsns.add(ln); } AbstractInsnNode ain = instructions.get(i); newinsns.add(ain); } LabelNode l = getLabelAt(sz); if (l != null) { newinsns.add(l); } LineNumberNode ln = lineNumberNodes.get(sz); if (ln != null) { newinsns.add(ln); } super.instructions = newinsns; }
boolean encounteredLineNode = false; int count = 0; for (int i = 0; i < insns.size(); i++) { AbstractInsnNode n = insns.get(i);
public Program parse(MethodNode method) { program = new Program(); InsnList instructions = method.instructions; if (instructions.size() == 0) { return program;