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

The following examples show how to use org.objectweb.asm.tree.InsnList#get() . 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: 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 2
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 3
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
private static boolean containsSimpleSdkCheck(@NonNull MethodNode method) {
    // Look for a compiled version of "if (Build.VERSION.SDK_INT op N) {"
    InsnList nodes = method.instructions;
    for (int i = 0, n = nodes.size(); i < n; i++) {
        AbstractInsnNode instruction = nodes.get(i);
        if (isSdkVersionLookup(instruction)) {
            AbstractInsnNode bipush = getNextInstruction(instruction);
            if (bipush != null && bipush.getOpcode() == Opcodes.BIPUSH) {
                AbstractInsnNode ifNode = getNextInstruction(bipush);
                if (ifNode != null && ifNode.getType() == AbstractInsnNode.JUMP_INSN) {
                    return true;
                }
            }
        }
    }

    return false;
}
 
Example 4
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 5
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 6
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 7
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 8
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 9
Source File: BlockSplitter.java    From radon with GNU General Public License v3.0 4 votes vote down vote up
private static void doSplit(MethodNode methodNode, AtomicInteger counter, int callStackSize) {
    InsnList insns = methodNode.instructions;

    if (insns.size() > 10 && callStackSize < LIMIT_SIZE) {
        LabelNode p1 = new LabelNode();
        LabelNode p2 = new LabelNode();

        AbstractInsnNode p2Start = insns.get((insns.size() - 1) / 2);
        AbstractInsnNode p2End = insns.getLast();

        AbstractInsnNode p1Start = insns.getFirst();

        // We can't have trap ranges mutilated by block splitting
        if (methodNode.tryCatchBlocks.stream().anyMatch(tcbn ->
                insns.indexOf(tcbn.end) >= insns.indexOf(p2Start)
                        && insns.indexOf(tcbn.start) <= insns.indexOf(p2Start)))
            return;

        ArrayList<AbstractInsnNode> insnNodes = new ArrayList<>();
        AbstractInsnNode currentInsn = p1Start;

        InsnList p1Block = new InsnList();

        while (currentInsn != p2Start) {
            insnNodes.add(currentInsn);

            currentInsn = currentInsn.getNext();
        }

        insnNodes.forEach(insn -> {
            insns.remove(insn);
            p1Block.add(insn);
        });

        p1Block.insert(p1);
        p1Block.add(new JumpInsnNode(GOTO, p2));

        insns.insert(p2End, p1Block);
        insns.insertBefore(p2Start, new JumpInsnNode(GOTO, p1));
        insns.insertBefore(p2Start, p2);

        counter.incrementAndGet();

        // We might have messed up variable ranges when rearranging the block order.
        if (methodNode.localVariables != null)
            new ArrayList<>(methodNode.localVariables).stream().filter(lvn ->
                    insns.indexOf(lvn.end) < insns.indexOf(lvn.start)
            ).forEach(methodNode.localVariables::remove);

        doSplit(methodNode, counter, callStackSize + 1);
    }
}
 
Example 10
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 11
Source File: StaticAnalysis.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
public boolean walkMethod(ClassNode clinit_cn, MethodNode mn, HashSet<MethodNode> method_callees) {
    boolean safe = true;
    InsnList insns = mn.instructions;

    for (int i = 0; safe && i < insns.size(); i++) {
        AbstractInsnNode ain = insns.get(i);

        switch (ain.getType()) {
        case AbstractInsnNode.FIELD_INSN:
            FieldInsnNode fin = (FieldInsnNode) ain;
            safe = handleField(clinit_cn, fin);
            break;

        case AbstractInsnNode.METHOD_INSN:
            MethodInsnNode min = (MethodInsnNode) ain;
            HashSet<MethodNode> invoke_callees = handleInvoke(clinit_cn, min);
            if (invoke_callees == null) {
                safe = false;
            } else {
                for (MethodNode callee : invoke_callees)
                    method_callees.add(callee);
            }
            break;

        // deals with java.lang.invoke.MethodHandle
        // I think this is like function pointer aka not safe
        case AbstractInsnNode.INVOKE_DYNAMIC_INSN:
            safe = false;
            bad_invoke_dynamic++;
            if (log_enable)
                log_ln("[unsafe] class: " + clinit_cn.name + " [cause:invoke_dynamic]");
            break;

        case AbstractInsnNode.INSN:
            // throwing things can hurt people so not safe
            // most likely throw logic depends on a reference, so we should
            // already handle this implicitly, but no harm in safety first
            if (ain.getOpcode() == ATHROW) {
                safe = false;
                bad_throw++;
                if (log_enable)
                    log_ln("[unsafe] class: " + clinit_cn.name + " [cause:throw]");
            }
            break;

        default:
            break;
        }
    }
    return safe;
}
 
Example 12
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public InsnListSection(InsnList haystack, int start, int end) {
	this(haystack.get(start), haystack.get(end));
}
 
Example 13
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public InsnListSection(InsnList haystack, int start, int end) {
	this(haystack.get(start), haystack.get(end));
}
 
Example 14
Source File: InstructionComparator.java    From NOVA-Core with GNU Lesser General Public License v3.0 4 votes vote down vote up
public InsnListSection(InsnList haystack, int start, int end) {
	this(haystack.get(start), haystack.get(end));
}
 
Example 15
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;
}
 
Example 16
Source File: InjectionPoint.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
 */
protected 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;
}