Java Code Examples for org.objectweb.asm.tree.InsnList#size()

The following examples show how to use org.objectweb.asm.tree.InsnList#size() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TransformerFieldInjector.java    From Diorite with MIT License 6 votes vote down vote up
private void injectFieldsIn(MethodNode rootNode)
{
    InsnList instructions = rootNode.instructions;
    if (instructions.size() == 0)
    {
        return;
    }
    AbstractInsnNode node = instructions.getFirst();
    while (node != null)
    {
        while (! (node instanceof FieldInsnNode) || ! AsmUtils.isPutField(node.getOpcode()))
        {
            node = node.getNext();
            if (node == null)
            {
                return;
            }
        }
        this.trackFieldToInject((FieldInsnNode) node, rootNode.instructions);
        node = node.getNext();
    }
}
 
Example 2
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static InsnList getImportantList(InsnList list) {
	if (list.size() == 0) {
		return list;
	}

	HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode) {
			labels.put((LabelNode) insn, (LabelNode) insn);
		}
	}

	InsnList importantNodeList = new InsnList();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
			continue;
		}

		importantNodeList.add(insn.clone(labels));
	}
	return importantNodeList;
}
 
Example 3
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
private static InsnListSection insnListMatchesL(InsnList haystack, InsnList needle, int start, HashSet<LabelNode> controlFlowLabels) {
	int h = start, n = 0;
	for (; h < haystack.size() && n < needle.size(); h++) {
		AbstractInsnNode insn = haystack.get(h);
		if (insn.getType() == 15) {
			continue;
		}
		if (insn.getType() == 8 && !controlFlowLabels.contains(insn)) {
			continue;
		}

		if (!insnEqual(haystack.get(h), needle.get(n))) {
			return null;
		}
		n++;
	}
	if (n != needle.size()) {
		return null;
	}

	return new InsnListSection(haystack, start, h - 1);
}
 
Example 4
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static InsnList getImportantList(InsnList list) {
	if (list.size() == 0) {
		return list;
	}

	HashMap<LabelNode, LabelNode> labels = new HashMap<LabelNode, LabelNode>();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode) {
			labels.put((LabelNode) insn, (LabelNode) insn);
		}
	}

	InsnList importantNodeList = new InsnList();
	for (AbstractInsnNode insn = list.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn instanceof LabelNode || insn instanceof LineNumberNode) {
			continue;
		}

		importantNodeList.add(insn.clone(labels));
	}
	return importantNodeList;
}
 
Example 5
Source File: ClassFileStructurePrinter.java    From scott with MIT License 6 votes vote down vote up
public static void viewByteCode(byte[] bytecode) {
	ClassReader classReader = new ClassReader(bytecode);
	ClassNode classNode = new ClassNode();
	classReader.accept(classNode, 0);
	final List<MethodNode> methodNodes = classNode.methods;
	Printer printer = new Textifier();
	TraceMethodVisitor traceMethodVisitor = new TraceMethodVisitor(printer);
	
	for (MethodNode methodNode : methodNodes) {
		InsnList insnList = methodNode.instructions;
		System.out.println(methodNode.name);
		for (int i = 0; i < insnList.size(); i++) {
			insnList.get(i).accept(traceMethodVisitor);
			StringWriter sw = new StringWriter();
			printer.print(new PrintWriter(sw));
			printer.getText().clear();
			System.out.print(sw.toString());
		}
	}
}
 
Example 6
Source File: Helper.java    From instrumentation with Apache License 2.0 6 votes vote down vote up
public static void viewByteCode(byte[] bytecode) {
    ClassReader cr = new ClassReader(bytecode);
    ClassNode cn = new ClassNode();
    cr.accept(cn, 0);
    final List<MethodNode> mns = cn.methods;
    Printer printer = new Textifier();
    TraceMethodVisitor mp = new TraceMethodVisitor(printer);
    for (MethodNode mn : mns) {
        InsnList inList = mn.instructions;
        System.out.println(mn.name);
        for (int i = 0; i < inList.size(); i++) {
            inList.get(i).accept(mp);
            StringWriter sw = new StringWriter();
            printer.print(new PrintWriter(sw));
            printer.getText().clear();
            System.out.print(sw.toString());
        }
    }
}
 
Example 7
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static MethodNode findEnumSwitchUsage(ClassNode classNode, String owner) {
    String target = ENUM_SWITCH_PREFIX + owner.replace('/', '$');
    @SuppressWarnings("rawtypes") // ASM API
    List methodList = classNode.methods;
    for (Object f : methodList) {
        MethodNode method = (MethodNode) f;
        InsnList nodes = method.instructions;
        for (int i = 0, n = nodes.size(); i < n; i++) {
            AbstractInsnNode instruction = nodes.get(i);
            if (instruction.getOpcode() == Opcodes.GETSTATIC) {
                FieldInsnNode field = (FieldInsnNode) instruction;
                if (field.name.equals(target)) {
                    return method;
                }
            }
        }
    }
    return null;
}
 
Example 8
Source File: LintDriver.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@Nullable
private static MethodInsnNode findConstructorInvocation(
        @NonNull MethodNode method,
        @NonNull String className) {
    InsnList nodes = method.instructions;
    for (int i = 0, n = nodes.size(); i < n; i++) {
        AbstractInsnNode instruction = nodes.get(i);
        if (instruction.getOpcode() == Opcodes.INVOKESPECIAL) {
            MethodInsnNode call = (MethodInsnNode) instruction;
            if (className.equals(call.owner)) {
                return call;
            }
        }
    }

    return null;
}
 
Example 9
Source File: AsmInsertImpl.java    From Robust with Apache License 2.0 6 votes vote down vote up
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();
}
 
Example 10
Source File: CustomMethodInvocationVisitor.java    From custom-bytecode-analyzer with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void process() {
  for (MethodNode methodNode : getClassNode().methods) {
    InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
      AbstractInsnNode insnNode = instructions.get(i);
      if (insnNode.getType() == AbstractInsnNode.METHOD_INSN) {
        MethodInsnNode methodInsnNode = (MethodInsnNode)insnNode;
        processMethodInsnNode(methodNode, methodInsnNode);
      }
    }
  }
}
 
Example 11
Source File: ClassCallGraph.java    From custom-bytecode-analyzer with GNU General Public License v3.0 5 votes vote down vote up
public void populateClassGraph() {
  String className = Type.getObjectType(classNode.name).getClassName();
  logger.debug("Creating graph for class {}" , className);
  for (MethodNode methodNode : classNode.methods) {
    String methodName = methodNode.name;
    MethodGraph caller = new MethodGraph(className, methodName);
    InsnList instructions = methodNode.instructions;
    for (int i = 0; i < instructions.size(); i++) {
      AbstractInsnNode insnNode = instructions.get(i);
      if (insnNode.getType() == AbstractInsnNode.METHOD_INSN) {
        MethodInsnNode methodInsnNode = (MethodInsnNode)insnNode;
        String calledOwner = Type.getObjectType(methodInsnNode.owner).getClassName();
        String calledName = methodInsnNode.name;
        MethodGraph called = new MethodGraph(calledOwner, calledName);
        Call call = new Call(caller, called);
        if (!called.getOwner().equals("java.lang.Object") && !called.getName().equals("<init>")) {
          logger.trace("Adding call graph: {}", call.toString());
          GraphHolder.addCallGraph(call);
        }
      }
    }
  }
}
 
Example 12
Source File: InsnListUtils.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
public static String insnListToString(InsnList insns) { 
	StringBuilder sb = new StringBuilder();
	for(int i = 0; i< insns.size(); i++) {
		sb.append(insnToString(insns.get(i)));
	}
	return sb.toString();
}
 
Example 13
Source File: DefaultMethodClassFixer.java    From bazel with Apache License 2.0 5 votes vote down vote up
private boolean isClinitAlreadyDesugared(
    ImmutableList<String> companionsToAccessToTriggerInterfaceClinit) {
  InsnList instructions = clInitMethodNode.instructions;
  if (instructions.size() <= companionsToAccessToTriggerInterfaceClinit.size()) {
    // The <clinit> must end with RETURN, so if the instruction count is less than or equal to
    // the companion class count, this <clinit> has not been desugared.
    return false;
  }
  Iterator<AbstractInsnNode> iterator = instructions.iterator();
  for (String companion : companionsToAccessToTriggerInterfaceClinit) {
    if (!iterator.hasNext()) {
      return false;
    }
    AbstractInsnNode insn = iterator.next();
    if (!(insn instanceof MethodInsnNode)) {
      return false;
    }
    MethodInsnNode methodInsnNode = (MethodInsnNode) insn;
    if (methodInsnNode.getOpcode() != Opcodes.INVOKESTATIC
        || !methodInsnNode.owner.equals(companion)
        || !methodInsnNode.name.equals(
            InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_NAME)) {
      return false;
    }
    checkState(
        methodInsnNode.desc.equals(
            InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC),
        "Inconsistent method desc: %s vs %s",
        methodInsnNode.desc,
        InterfaceDesugaring.COMPANION_METHOD_TO_TRIGGER_INTERFACE_CLINIT_DESC);
  }
  return true;
}
 
Example 14
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static List<Integer> insnListFind(InsnList haystack, InsnList needle) {
	LinkedList<Integer> list = new LinkedList<Integer>();
	for (int start = 0; start <= haystack.size() - needle.size(); start++) {
		if (insnListMatches(haystack, needle, start)) {
			list.add(start);
		}
	}

	return list;
}
 
Example 15
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean insnListMatches(InsnList haystack, InsnList needle, int start) {
	if (haystack.size() - start < needle.size()) {
		return false;
	}

	for (int i = 0; i < needle.size(); i++) {
		if (!insnEqual(haystack.get(i + start), needle.get(i))) {
			return false;
		}
	}
	return true;
}
 
Example 16
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static List<Integer> insnListFind(InsnList haystack, InsnList needle) {
	LinkedList<Integer> list = new LinkedList<Integer>();
	for (int start = 0; start <= haystack.size() - needle.size(); start++) {
		if (insnListMatches(haystack, needle, start)) {
			list.add(start);
		}
	}

	return list;
}
 
Example 17
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean insnListMatches(InsnList haystack, InsnList needle, int start) {
	if (haystack.size() - start < needle.size()) {
		return false;
	}

	for (int i = 0; i < needle.size(); i++) {
		if (!insnEqual(haystack.get(i + start), needle.get(i))) {
			return false;
		}
	}
	return true;
}
 
Example 18
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static List<Integer> insnListFind(InsnList haystack, InsnList needle) {
	LinkedList<Integer> list = new LinkedList<Integer>();
	for (int start = 0; start <= haystack.size() - needle.size(); start++) {
		if (insnListMatches(haystack, needle, start)) {
			list.add(start);
		}
	}

	return list;
}
 
Example 19
Source File: WakelockDetector.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
private static void checkFlow(@NonNull ClassContext context, @NonNull ClassNode classNode,
        @NonNull MethodNode method, @NonNull MethodInsnNode acquire) {
    // Track allocations such that we know whether the type of the call
    // is on a SecureRandom rather than a Random
    final InsnList instructions = method.instructions;
    MethodInsnNode release = null;

    // Find release call
    for (int i = 0, n = instructions.size(); i < n; i++) {
        AbstractInsnNode instruction = instructions.get(i);
        int type = instruction.getType();
        if (type == AbstractInsnNode.METHOD_INSN) {
            MethodInsnNode call = (MethodInsnNode) instruction;
            if (call.name.equals(RELEASE_METHOD) &&
                    call.owner.equals(WAKELOCK_OWNER)) {
                release = call;
                break;
            }
        }
    }

    if (release == null) {
        // Didn't find both acquire and release in this method; no point in doing
        // local flow analysis
        return;
    }

    try {
        MyGraph graph = new MyGraph();
        ControlFlowGraph.create(graph, classNode, method);

        if (DEBUG) {
            // Requires util package
            //ClassNode clazz = classNode;
            //clazz.accept(new TraceClassVisitor(new PrintWriter(System.out)));
            System.out.println(graph.toString(graph.getNode(acquire)));
        }

        int status = dfs(graph.getNode(acquire));
        if ((status & SEEN_RETURN) != 0) {
            String message;
            if ((status & SEEN_EXCEPTION) != 0) {
                message = "The `release()` call is not always reached (via exceptional flow)";
            } else {
                message = "The `release()` call is not always reached";
            }

            context.report(ISSUE, method, acquire,
                    context.getLocation(release), message);
        }
    } catch (AnalyzerException e) {
        context.log(e, null);
    }
}
 
Example 20
Source File: Locals.java    From Mixin with MIT License 3 votes vote down vote up
/**
 * Get the insn immediately following the specified insn, or return the same
 * insn if the insn is the last insn in the list
 * 
 * @param insns Insn list to fetch from
 * @param insn Insn node
 * @return Next insn or the same insn if last in the list
 */
private static AbstractInsnNode nextNode(InsnList insns, AbstractInsnNode insn) {
    int index = insns.indexOf(insn) + 1;
    if (index > 0 && index < insns.size()) {
        return insns.get(index);
    }
    return insn;
}