org.objectweb.asm.tree.AbstractInsnNode Java Examples

The following examples show how to use org.objectweb.asm.tree.AbstractInsnNode. 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: EntityPlayerSPPatch.java    From ForgeHax with MIT License 6 votes vote down vote up
@Inject(description = "Add hook to override returned value of isRowingBoat")
public void inject(MethodNode main) {
  AbstractInsnNode preNode = main.instructions.getFirst();
  
  Objects.requireNonNull(preNode, "Find pattern failed for pre node");
  
  LabelNode jump = new LabelNode();
  
  InsnList insnPre = new InsnList();
  // insnPre.add(ASMHelper.call(GETSTATIC,
  // TypesHook.Fields.ForgeHaxHooks_isNotRowingBoatActivated));
  // insnPre.add(new JumpInsnNode(IFEQ, jump));
  
  insnPre.add(new InsnNode(ICONST_0));
  insnPre.add(new InsnNode(IRETURN)); // return false
  // insnPre.add(jump);
  
  main.instructions.insert(insnPre);
}
 
Example #2
Source File: ForEachLoopFilter.java    From pitest with Apache License 2.0 6 votes vote down vote up
private static SequenceQuery<AbstractInsnNode> arrayConditionalAtEnd() {
  final Slot<LabelNode> loopStart = Slot.create(LabelNode.class);
  final Slot<LabelNode> loopEnd = Slot.create(LabelNode.class);
  final Slot<Integer> counter = Slot.create(Integer.class);
  return QueryStart
      .any(AbstractInsnNode.class)
      .zeroOrMore(QueryStart.match(anyInstruction()))
      .then(opCode(Opcodes.ARRAYLENGTH).and(mutationPoint()))
      .then(opCode(Opcodes.ISTORE))
      .then(opCode(Opcodes.ICONST_0).and(mutationPoint()))
      .then(anIStore(counter.write()).and(debug("store")))
      .then(gotoLabel(loopEnd.write()))
      .then(aLabelNode(loopStart.write()))
      .zeroOrMore(QueryStart.match(anyInstruction()))
      .then(incrementsVariable(counter.read()).and(mutationPoint()))
      .then(labelNode(loopEnd.read()))
      .then(opCode(Opcodes.ILOAD))
      .then(opCode(Opcodes.ILOAD))
      .then(aConditionalJumpTo(loopStart).and(mutationPoint()))
      .zeroOrMore(QueryStart.match(anyInstruction()));
}
 
Example #3
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 6 votes vote down vote up
public static AbstractInsnNode getRandomValue(Type type) {
    switch (type.getSort()) {
        case Type.BOOLEAN:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(0, 2));
        case Type.CHAR:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Character.MIN_VALUE, Character.MAX_VALUE));
        case Type.BYTE:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Byte.MIN_VALUE, Byte.MAX_VALUE));
        case Type.SHORT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt(Short.MIN_VALUE, Short.MAX_VALUE));
        case Type.INT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomInt());
        case Type.FLOAT:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomFloat());
        case Type.LONG:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomLong());
        case Type.DOUBLE:
            return ASMUtils.getNumberInsn(RandomUtils.getRandomDouble());
        case Type.ARRAY:
        case Type.OBJECT:
            return new InsnNode(Opcodes.ACONST_NULL);
        default:
            throw new AssertionError();
    }
}
 
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: ClickableViewAccessibilityDetector.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked") // ASM API
public static void scanForAndCheckSetOnTouchListenerCalls(
        ClassContext context,
        ClassNode classNode) {
    List<MethodNode> methods = classNode.methods;
    for (MethodNode methodNode : methods) {
        ListIterator<AbstractInsnNode> iterator = methodNode.instructions.iterator();
        while (iterator.hasNext()) {
            AbstractInsnNode abstractInsnNode = iterator.next();
            if (abstractInsnNode.getType() == AbstractInsnNode.METHOD_INSN) {
                MethodInsnNode methodInsnNode = (MethodInsnNode) abstractInsnNode;
                if (methodInsnNode.name.equals(SET_ON_TOUCH_LISTENER)
                        && methodInsnNode.desc.equals(SET_ON_TOUCH_LISTENER_SIG)) {
                    checkSetOnTouchListenerCall(context, methodNode, methodInsnNode);
                }
            }
        }
    }
}
 
Example #6
Source File: ReflectionObfuscationVMT11.java    From zelixkiller with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void transform(JarArchive ja, ClassNode node) {
	if (twoLongType) {
		// init surroundings before decryption
		Outer: for (ClassNode cn : ja.getClasses().values()) {
			for (MethodNode mn : cn.methods) {
				for (AbstractInsnNode ain : mn.instructions.toArray()) {
					if (ain.getOpcode() == INVOKESPECIAL) {
						MethodInsnNode min = (MethodInsnNode) ain;
						if (min.owner.equals(node.name) && min.name.equals("<init>")) {
							try {
								Class.forName(cn.name.replace("/", "."), true, vm);
							} catch (ClassNotFoundException e) {
							}
							continue Outer;
						}
					}
				}
			}
		}
	}
	node.methods.forEach(mn -> removeDynamicCalls(node, mn));
}
 
Example #7
Source File: LintUtils.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Returns the previous instruction prior to the given node, ignoring label
 * and line number nodes.
 *
 * @param node the node to look up the previous instruction for
 * @return the previous instruction, or null if no previous node was found
 */
@Nullable
public static AbstractInsnNode getPrevInstruction(@NonNull AbstractInsnNode node) {
    AbstractInsnNode prev = node;
    while (true) {
        prev = prev.getPrevious();
        if (prev == null) {
            return null;
        } else {
            int type = prev.getType();
            if (type != AbstractInsnNode.LINE && type != AbstractInsnNode.LABEL
                    && type != AbstractInsnNode.FRAME) {
                return prev;
            }
        }
    }
}
 
Example #8
Source File: SourceInterpreter.java    From Cafebabe with GNU General Public License v3.0 6 votes vote down vote up
@Override
public SourceValue newOperation(final AbstractInsnNode insn) {
	int size;
	switch (insn.getOpcode()) {
	case LCONST_0:
	case LCONST_1:
	case DCONST_0:
	case DCONST_1:
		size = 2;
		break;
	case LDC:
		Object value = ((LdcInsnNode) insn).cst;
		size = value instanceof Long || value instanceof Double ? 2 : 1;
		break;
	case GETSTATIC:
		size = Type.getType(((FieldInsnNode) insn).desc).getSize();
		break;
	default:
		size = 1;
		break;
	}
	return new SourceValue(size, insn);
}
 
Example #9
Source File: BlockLogMethodAdapter.java    From jumbune with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * <p>
 * This method gets the end index for the provided Jump instruction
 * </p>
 * 
 * @param ain
 *            The given jump node
 * @return int end index
 */
private int getEndIndexForBlock(AbstractInsnNode ain) {
	int retIndex = 0;
	if (ain instanceof JumpInsnNode) {
		JumpInsnNode jin = (JumpInsnNode) ain;
		LabelNode targetAIN = jin.label;
		if (targetAIN.getPrevious() instanceof JumpInsnNode
				&& Opcodes.GOTO == targetAIN.getPrevious().getOpcode()) {
			retIndex = CollectionUtil.getObjectIndexInArray(this.insnArr, targetAIN
					.getPrevious().getPrevious());
		} else {
			retIndex = CollectionUtil.getObjectIndexInArray(this.insnArr,
					targetAIN.getPrevious());
		}
	}
	return retIndex;
}
 
Example #10
Source File: JumpInsnPoint.java    From Mixin with MIT License 6 votes vote down vote up
@Override
public boolean find(String desc, InsnList insns, Collection<AbstractInsnNode> nodes) {
    boolean found = false;
    int ordinal = 0;

    ListIterator<AbstractInsnNode> iter = insns.iterator();
    while (iter.hasNext()) {
        AbstractInsnNode insn = iter.next();

        if (insn instanceof JumpInsnNode && (this.opCode == -1 || insn.getOpcode() == this.opCode)) {
            if (this.ordinal == -1 || this.ordinal == ordinal) {
                nodes.add(insn);
                found = true;
            }

            ordinal++;
        }
    }

    return found;
}
 
Example #11
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 #12
Source File: InvocationStep.java    From deobfuscator with Apache License 2.0 6 votes vote down vote up
@Override
public AbstractInsnNode tryMatch(InstructionMatcher matcher, AbstractInsnNode now) {
    if (opcode != -1 && now.getOpcode() != opcode) {
        return null;
    }
    if (!(now instanceof MethodInsnNode)) {
        return null;
    }
    MethodInsnNode methodInsnNode = (MethodInsnNode) now;
    boolean ownerMatches = owner == null || methodInsnNode.owner.equals(owner);
    boolean nameMatches = name == null || methodInsnNode.name.equals(name);
    boolean descMatches = desc == null || (basic ? TransformerHelper.basicType(methodInsnNode.desc) : methodInsnNode.desc).equals(desc);
    if (!ownerMatches || !nameMatches || !descMatches) {
        return null;
    }
    return now.getNext();
}
 
Example #13
Source File: CallSiteFinder.java    From tascalate-javaflow with Apache License 2.0 6 votes vote down vote up
private boolean isVarBetweenBounds(AbstractInsnNode var, LabelNode lo, LabelNode hi) {
    AbstractInsnNode x;
    boolean loFound = false;
    for (x = var; !(x == null || loFound); x = x.getPrevious()) {
        loFound = x == lo;
    }
    if (!loFound)
        return false;

    boolean hiFound = false;
    for (x = var; !(x == null || hiFound); x = x.getNext()) {
        hiFound = x == hi;
    }

    return hiFound;

}
 
Example #14
Source File: Asm.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Replaces class references in super constructor invocations.
 * Must not replace references in this() constructor invocations.
 * 
 * @param theClass the class being patched
 * @param extenderClass the injected superclass
 * @param mn method to process
 */
private static void replaceSuperCtorCalls(final ClassNode theClass, final ClassNode extenderClass, MethodNode mn) {
    for (Iterator<AbstractInsnNode> it = mn.instructions.iterator(); it.hasNext(); ) {
        AbstractInsnNode aIns = it.next();
        if (aIns.getOpcode() == Opcodes.INVOKESPECIAL) {
            MethodInsnNode mins = (MethodInsnNode)aIns;
            if (CONSTRUCTOR_NAME.equals(mins.name) && mins.owner.equals(extenderClass.superName)) {
                // replace with the extender class name
                mins.owner = extenderClass.name;
            }
            break;
        }
    }
}
 
Example #15
Source File: MethodTree.java    From pitest with Apache License 2.0 5 votes vote down vote up
private List<AbstractInsnNode> createInstructionList() {
  final List<AbstractInsnNode> list = new ArrayList<>();
  for (AbstractInsnNode abstractInsnNode : this.rawNode.instructions) {
    list.add(abstractInsnNode);
  }
  this.lazyInstructions = list;
  return this.lazyInstructions;
}
 
Example #16
Source File: ASMHelper.java    From NOVA-Core with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Map<LabelNode, LabelNode> cloneLabels(InsnList insns) {
	HashMap<LabelNode, LabelNode> labelMap = new HashMap<>();
	for (AbstractInsnNode insn = insns.getFirst(); insn != null; insn = insn.getNext()) {
		if (insn.getType() == 8) {
			labelMap.put((LabelNode) insn, new LabelNode());
		}
	}
	return labelMap;
}
 
Example #17
Source File: ApiDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void add(@NonNull AbstractInsnNode from, @NonNull AbstractInsnNode to) {
    if (from.getType() == AbstractInsnNode.JUMP_INSN &&
            from.getPrevious() != null &&
            from.getPrevious().getType() == AbstractInsnNode.INT_INSN) {
        IntInsnNode intNode = (IntInsnNode) from.getPrevious();
        if (intNode.getPrevious() != null && isSdkVersionLookup(intNode.getPrevious())) {
            JumpInsnNode jumpNode = (JumpInsnNode) from;
            int api = intNode.operand;
            boolean isJumpEdge = to == jumpNode.label;
            boolean includeEdge;
            switch (from.getOpcode()) {
                case Opcodes.IF_ICMPNE:
                    includeEdge = api < mRequiredApi || isJumpEdge;
                    break;
                case Opcodes.IF_ICMPLE:
                    includeEdge = api < mRequiredApi - 1 || isJumpEdge;
                    break;
                case Opcodes.IF_ICMPLT:
                    includeEdge = api < mRequiredApi || isJumpEdge;
                    break;

                case Opcodes.IF_ICMPGE:
                    includeEdge = api < mRequiredApi || !isJumpEdge;
                    break;
                case Opcodes.IF_ICMPGT:
                    includeEdge = api < mRequiredApi - 1 || !isJumpEdge;
                    break;
                default:
                    // unexpected comparison for int API level
                    includeEdge = true;
            }
            if (!includeEdge) {
                return;
            }
        }
    }

    super.add(from, to);
}
 
Example #18
Source File: BasicBlock.java    From Concurnas with MIT License 5 votes vote down vote up
public boolean isGetCurrentTask() {
    AbstractInsnNode ain = getInstruction(startPos);
    if (ain.getOpcode() == INVOKESTATIC) {
        MethodInsnNode min = (MethodInsnNode)ain;
        return min.owner.equals(TASK_CLASS) && min.name.equals("getCurrentTask");
    }
    return false;
}
 
Example #19
Source File: InvalidPackageDetector.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void record(ClassContext context, MethodNode method,
        AbstractInsnNode instruction, String owner) {
    if (owner.indexOf('$') != -1) {
        // Don't report inner classes too; there will pretty much always be an outer class
        // reference as well
        return;
    }

    if (mCandidates == null) {
        mCandidates = Lists.newArrayList();
    }
    mCandidates.add(new Candidate(owner, context.getClassNode().name, context.getJarFile()));
}
 
Example #20
Source File: ASMHelper.java    From jaop with Apache License 2.0 5 votes vote down vote up
public static void loadNode(ListIterator<AbstractInsnNode> iterator, Object paramType, int index) {
    if (Opcodes.INTEGER.equals(paramType)) {
        iterator.add(new VarInsnNode(Opcodes.ILOAD, index));
    } else if (Opcodes.LONG.equals(paramType)) {
        iterator.add(new VarInsnNode(Opcodes.LLOAD, index));
    } else if (Opcodes.FLOAT.equals(paramType)) {
        iterator.add(new VarInsnNode(Opcodes.FLOAD, index));
    } else if (Opcodes.DOUBLE.equals(paramType)) {
        iterator.add(new VarInsnNode(Opcodes.DLOAD, index));
    } else {
        iterator.add(new VarInsnNode(Opcodes.ALOAD, index));
    }
}
 
Example #21
Source File: IntInstructionFilter.java    From maple-ir with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean accept(AbstractInsnNode t) {
	if (!(t instanceof IntInsnNode))
		return false;
	if (!opcodeFilter.accept(t))
		return false;
	IntInsnNode fin = (IntInsnNode) t;
	if (!numberFilter.accept(fin.operand))
		return false;
	return true;
}
 
Example #22
Source File: LightFlowObfuscationTransformer.java    From deobfuscator with Apache License 2.0 5 votes vote down vote up
@Override 
   public boolean transform() throws Throwable {
   	DelegatingProvider provider = new DelegatingProvider();
       provider.register(new JVMMethodProvider());
       provider.register(new JVMComparisonProvider());
       provider.register(new MappedMethodProvider(classes));
       provider.register(new MappedFieldProvider());

       AtomicInteger fixed = new AtomicInteger();

       System.out.println("[Allatori] [LightFlowObfuscationTransformer] Starting");
       for(ClassNode classNode : classNodes())
       	for(MethodNode method : classNode.methods)
       	{
       		boolean modified;
       		do
       		{
       			modified = false;
        		for(AbstractInsnNode ain : method.instructions.toArray())
        			if((willPush(ain) || ain.getOpcode() == Opcodes.DUP) && ain.getNext() != null 
        			&& (willPush(ain.getNext()) || ain.getNext().getOpcode() == Opcodes.DUP)
        				&& ain.getNext().getNext() != null && ain.getNext().getNext().getOpcode() == Opcodes.POP2)
        			{
        				method.instructions.remove(ain.getNext().getNext());
        				method.instructions.remove(ain.getNext());
        				method.instructions.remove(ain);
        				modified = true;
        				fixed.incrementAndGet();
        			}
       		}while(modified);
       	}
       System.out.println("[Allatori] [LightFlowObfuscationTransformer] Removed " + fixed + " dead instructions");
       System.out.println("[Allatori] [LightFlowObfuscationTransformer] Done");
       return true;
}
 
Example #23
Source File: StackHelper.java    From zelixkiller with GNU General Public License v3.0 5 votes vote down vote up
public InsnValue checkNull(AbstractInsnNode insn, InsnValue value) {
	switch (insn.getOpcode()) {
	case Opcodes.IFNULL:
		return InsnValue.intValue(value.getValue() == null);
	case Opcodes.IFNONNULL:
		return InsnValue.intValue(value.getValue() != null);
	}

	return null;
}
 
Example #24
Source File: ASMUtils.java    From radon with GNU General Public License v3.0 5 votes vote down vote up
public static long getLongFromInsn(AbstractInsnNode insn) {
    int opcode = insn.getOpcode();

    if (opcode >= Opcodes.LCONST_0 && opcode <= Opcodes.LCONST_1) {
        return opcode - 9;
    } else if (insn instanceof LdcInsnNode
            && ((LdcInsnNode) insn).cst instanceof Long) {
        return (Long) ((LdcInsnNode) insn).cst;
    }

    throw new RadonException("Unexpected instruction");
}
 
Example #25
Source File: ForEachLoopFilter.java    From pitest with Apache License 2.0 5 votes vote down vote up
private Predicate<MutationDetails> mutatesIteratorLoopPlumbing() {
  return a -> {
    final int instruction = a.getInstructionIndex();
    final MethodTree method = ForEachLoopFilter.this.currentClass.methods().stream()
        .filter(MethodMatchers.forLocation(a.getId().getLocation()))
        .findFirst()
        .get();
    final AbstractInsnNode mutatedInstruction = method.instruction(instruction);

    final Context<AbstractInsnNode> context = Context.start(method.instructions(), DEBUG);
    context.store(MUTATED_INSTRUCTION.write(), mutatedInstruction);
    return ITERATOR_LOOP.matches(method.instructions(), context);
  };
}
 
Example #26
Source File: InstructionMatcher.java    From deobfuscator with Apache License 2.0 5 votes vote down vote up
public AbstractInsnNode getCapturedInstruction(String id) {
    List<AbstractInsnNode> captured = getCapturedInstructions(id);
    if (captured == null || captured.size() > 1) {
        return null;
    }
    return captured.get(0);
}
 
Example #27
Source File: JClassPatcher.java    From rscplus with GNU General Public License v3.0 5 votes vote down vote up
private String decodeInstruction(AbstractInsnNode insnNode) {
  insnNode.accept(mp);
  StringWriter sw = new StringWriter();
  printer.print(new PrintWriter(sw));
  printer.getText().clear();
  return sw.toString();
}
 
Example #28
Source File: OrStep.java    From deobfuscator with Apache License 2.0 5 votes vote down vote up
@Override
public AbstractInsnNode tryMatch(InstructionMatcher matcher, AbstractInsnNode now) {
    for (Step step : steps) {
        AbstractInsnNode next = step.tryMatch(matcher, now);
        if (next != null) {
            return next;
        }
    }
    return null;
}
 
Example #29
Source File: ConversionMethods.java    From rembulan with Apache License 2.0 5 votes vote down vote up
public static AbstractInsnNode booleanValueOf() {
	return new MethodInsnNode(
			INVOKESTATIC,
			Type.getInternalName(Conversions.class),
			"booleanValueOf",
			Type.getMethodDescriptor(
					Type.BOOLEAN_TYPE,
					Type.getType(Object.class)),
			false);
}
 
Example #30
Source File: DetourLoader.java    From android-perftracking with MIT License 5 votes vote down vote up
private MethodInsnNode getMethodInstruction(MethodNode mn, String owner, String method) {
  for (int i = 0; i < mn.instructions.size(); i++) {
    AbstractInsnNode ins = mn.instructions.get(i);
    if (ins instanceof MethodInsnNode) {
      MethodInsnNode mi = (MethodInsnNode) ins;
      if (mi.name.equals(method)) {
        if ((owner == null) || mi.owner.equals(owner)) {
          return mi;
        }
      }
    }
  }
  return null;
}