Java Code Examples for org.objectweb.asm.tree.MethodNode#visitLdcInsn()

The following examples show how to use org.objectweb.asm.tree.MethodNode#visitLdcInsn() . 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: MarkerTransformer.java    From obfuscator with MIT License 6 votes vote down vote up
private MethodNode createMethod() {
    final MethodNode methodNode = new MethodNode(ACC_PUBLIC | ACC_STATIC, methodName, "()V", null, null);
    methodNode.visitCode();

    final Label label0 = new Label();
    methodNode.visitLabel(label0);
    methodNode.visitLineNumber(10, label0);
    methodNode.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
    methodNode.visitLdcInsn(text);
    methodNode.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);

    final Label label1 = new Label();
    methodNode.visitLabel(label1);
    methodNode.visitLineNumber(11, label1);
    methodNode.visitInsn(RETURN);

    methodNode.visitMaxs(2, 0);
    methodNode.visitEnd();
    return methodNode;
}
 
Example 2
Source File: EngineExtender.java    From 07kit with GNU General Public License v3.0 6 votes vote down vote up
public MethodNode createGetter(GetterDefinition getter) {
    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, getter.name,
            "()" + getter.signature, null, null);
    if (getter.member) {
        method.visitVarInsn(Opcodes.ALOAD, 0);
        method.visitFieldInsn(Opcodes.GETFIELD, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    } else {
        method.visitFieldInsn(Opcodes.GETSTATIC, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    }

    if (getter.multiplier != 0) {
        method.visitLdcInsn((int) getter.multiplier);
        method.visitInsn(Opcodes.IMUL);
    }

    method.visitInsn(Type.getType(getter.actualSig).getOpcode(Opcodes.IRETURN));
    method.visitMaxs(1, 1);
    method.visitEnd();
    return method;
}
 
Example 3
Source File: EngineExtender.java    From 07kit with GNU General Public License v3.0 6 votes vote down vote up
public MethodNode createSetter(GetterDefinition getter) {
    MethodNode method = new MethodNode(Opcodes.ACC_PUBLIC, getter.name.replaceFirst("get", "set"),
            "(" + getter.signature + ")V", null, null);
    method.visitVarInsn(Opcodes.ALOAD, 0);
    method.visitVarInsn(Type.getType(getter.actualSig).getOpcode(Opcodes.ILOAD), 1);

    if (getter.actualSig.equals("I") && getter.multiplier != 0) {
        method.visitLdcInsn((int) modInverse(getter.multiplier));
        method.visitInsn(Opcodes.IMUL);
    }
    if (getter.member) {
        method.visitFieldInsn(Opcodes.PUTFIELD, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    } else {
        method.visitFieldInsn(Opcodes.PUTSTATIC, getter.fieldClass,
                getter.fieldName, getter.actualSig);
    }
    method.visitInsn(Opcodes.RETURN);

    method.visitMaxs(1, 1);
    method.visitEnd();
    return method;
}
 
Example 4
Source File: StringPooler.java    From radon with GNU General Public License v3.0 5 votes vote down vote up
private MethodNode stringPool(String className, String methodName, String fieldName, ArrayList<String> strings) {
    MethodNode method = new MethodNode(ACC_PRIVATE | ACC_STATIC | ACC_SYNTHETIC | ACC_BRIDGE, methodName, "()V",
            null, null);

    method.visitCode();
    int numberOfStrings = strings.size();
    if (numberOfStrings <= 5)
        method.visitInsn(numberOfStrings + 3);
    else if (numberOfStrings <= 127)
        method.visitIntInsn(BIPUSH, strings.size());
    else if (numberOfStrings <= 32767)
        method.visitIntInsn(SIPUSH, strings.size());
    else
        method.visitLdcInsn(strings.size());


    method.visitTypeInsn(ANEWARRAY, "java/lang/String");

    for (int i = 0; i < strings.size(); i++) {
        method.visitInsn(DUP);

        if (i <= 5)
            method.visitInsn(i + 3);
        else if (i <= 127)
            method.visitIntInsn(BIPUSH, i);
        else if (i <= 32767)
            method.visitIntInsn(SIPUSH, i);
        else
            method.visitLdcInsn(i);

        method.visitLdcInsn(strings.get(i));
        method.visitInsn(AASTORE);
    }
    method.visitFieldInsn(PUTSTATIC, className, fieldName, "[Ljava/lang/String;");
    method.visitInsn(RETURN);
    method.visitMaxs(3, 0);
    method.visitEnd();

    return method;
}
 
Example 5
Source File: NbJShellAgent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Transforms an existin &lt;clinit> method. Adds bytecode before the return, if
 * exists.
 * @param className
 * @param target 
 */
private void transformClassInit(String className, MethodNode target) {
    Type classType = Type.getObjectType(className);
    if (target.instructions.size() > 0) {
        if (target.instructions.getLast().getOpcode() == Opcodes.RETURN) {
            target.instructions.remove(target.instructions.getLast());
        }
    }
    // ldc
    target.visitLdcInsn(classType);
    // invokevirtual --> classloader on stack
    target.visitMethodInsn(
            Opcodes.INVOKEVIRTUAL,
            JAVA_LANG_CLASS,
            CLASS_GET_CLASSLODER_METHOD,
            CLASS_GET_CLASSLOADER_DESC,
            false
    );
    // putstatic
    target.visitFieldInsn(
            Opcodes.PUTSTATIC,
            AGENT_CLASS,
            AGENT_CLASSLOADER_FIELD,
            AGENT_CLASSLOADER_DESC
    );
    target.visitInsn(Opcodes.RETURN);
    // ldc || classloader instance
    target.maxStack = Math.max(target.maxStack, 1);
    target.maxLocals = Math.max(target.maxLocals, 1);
    target.visitEnd();
}
 
Example 6
Source File: TrashCodeTransformer.java    From obfuscator with MIT License 4 votes vote down vote up
@NotNull
private MethodNode createMethod() {
    final MethodNode methodNode = new MethodNode((RandomUtil.nextInt(2) == 1 ? ACC_PUBLIC | ACC_STATIC : ACC_PRIVATE | ACC_STATIC), StringUtil.generateString(RandomUtil.nextInt(16) + 2), "()V", null, null);
    methodNode.visitCode();

    final Label label0 = new Label();
    methodNode.visitLabel(label0);
    methodNode.visitLineNumber(8, label0);
    methodNode.visitInsn(ICONST_0);
    methodNode.visitVarInsn(ISTORE, 0);

    final AtomicInteger index = new AtomicInteger(10);
    final Label label1 = new Label();
    methodNode.visitLabel(label1);
    methodNode.visitLineNumber(index.getAndIncrement(), label1);
    methodNode.visitVarInsn(ILOAD, 0);
    methodNode.visitLdcInsn(300000);
    methodNode.visitInsn(IADD);
    methodNode.visitVarInsn(ISTORE, 0);

    IntStream.range(0, RandomUtil.nextInt(216) + 1).mapToObj(i -> new Label()).forEachOrdered(label2 -> {
        methodNode.visitLabel(label2);
        methodNode.visitLineNumber(index.getAndIncrement(), label2);
        methodNode.visitVarInsn(ILOAD, 0);
        methodNode.visitLdcInsn(300000);
        methodNode.visitInsn(IADD);
        methodNode.visitVarInsn(ISTORE, 0);
    });

    final Label label3 = new Label();
    methodNode.visitLabel(label3);
    methodNode.visitLineNumber(66, label3);
    methodNode.visitInsn(RETURN);

    final Label label58 = new Label();
    methodNode.visitLabel(label58);
    methodNode.visitLocalVariable("i", "I", null, label1, label58, 0);
    methodNode.visitMaxs(2, 1);
    methodNode.visitEnd();

    return methodNode;
}
 
Example 7
Source File: AntiDebugTransformer.java    From obfuscator with MIT License 4 votes vote down vote up
@NotNull
private MethodNode createMethod(@NotNull ClassNode classNode) {
    final MethodNode methodNode = new MethodNode(ACC_PRIVATE | ACC_STATIC, "checkDebug", "()V", null, null);
    methodNode.visitCode();

    final Label label0 = new Label();
    methodNode.visitLabel(label0);
    methodNode.visitLineNumber(11, label0);
    methodNode.visitMethodInsn(INVOKESTATIC, "java/lang/management/ManagementFactory", "getRuntimeMXBean", "()Ljava/lang/management/RuntimeMXBean;", false);
    methodNode.visitMethodInsn(INVOKEINTERFACE, "java/lang/management/RuntimeMXBean", "getInputArguments", "()Ljava/util/List;", true);
    methodNode.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "iterator", "()Ljava/util/Iterator;", true);
    methodNode.visitVarInsn(ASTORE, 1);

    final Label label1 = new Label();
    methodNode.visitLabel(label1);
    methodNode.visitFrame(F_APPEND, 1, new Object[]{"java/util/Iterator"}, 0, null);
    methodNode.visitVarInsn(ALOAD, 1);
    methodNode.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "hasNext", "()Z", true);

    final Label label2 = new Label();
    methodNode.visitJumpInsn(IFEQ, label2);
    methodNode.visitVarInsn(ALOAD, 1);
    methodNode.visitMethodInsn(INVOKEINTERFACE, "java/util/Iterator", "next", "()Ljava/lang/Object;", true);
    methodNode.visitTypeInsn(CHECKCAST, "java/lang/String");
    methodNode.visitVarInsn(ASTORE, 2);

    final Label label3 = new Label();
    methodNode.visitLabel(label3);
    methodNode.visitLineNumber(12, label3);
    methodNode.visitVarInsn(ALOAD, 2);

    final String debugType = debugTypes[RandomUtil.nextInt(debugTypes.length)];
    methodNode.visitLdcInsn(debugType == null ? debugTypes[0] : debugType);
    methodNode.visitMethodInsn(INVOKEVIRTUAL, "java/lang/String", "startsWith", "(Ljava/lang/String;)Z", false);

    final Label label4 = new Label();
    methodNode.visitJumpInsn(IFEQ, label4);

    final Label label5 = new Label();
    methodNode.visitLabel(label5);
    methodNode.visitLineNumber(13, label5);
    methodNode.visitIntInsn(SIPUSH, 666);
    methodNode.visitMethodInsn(INVOKESTATIC, "java/lang/System", "exit", "(I)V", false);
    methodNode.visitLabel(label4);
    methodNode.visitLineNumber(15, label4);
    methodNode.visitFrame(F_SAME, 0, null, 0, null);
    methodNode.visitJumpInsn(GOTO, label1);
    methodNode.visitLabel(label2);
    methodNode.visitLineNumber(16, label2);
    methodNode.visitFrame(F_CHOP, 1, null, 0, null);
    methodNode.visitInsn(RETURN);

    final Label label6 = new Label();
    methodNode.visitLabel(label6);
    methodNode.visitLocalVariable("string", "Ljava/lang/String;", null, label3, label4, 2);
    methodNode.visitLocalVariable("this", "L" + classNode.name + ";", null, label0, label6, 0);

    methodNode.visitMaxs(2, 3);
    methodNode.visitEnd();

    return methodNode;
}