0

I am using ASM to modify the java bytecode but I have an error that I can not resolve.

When I compile the original method it works, I've re-created the original method in createDecryptMethod, however I get an if_icmplt error, but everything looks the same.

Error:

Exception Details:
  Location:
    tk/netindev/Main.randomString(Ljava/lang/String;)Ljava/lang/String; @55: if_icmplt
  Reason:
    Expected stackmap frame at this location.
  Bytecode:
    0x0000000: 1279 4c03 3dbb 007b 592b b800 81b7 0082
    0x0000010: 2a1c b600 8610 3064 1288 1c12 88b6 008c
    0x0000020: 0464 70b6 0086 8292 b600 90b6 0094 4c84
    0x0000030: 0201 1c2a b600 8ca1 ffce 2bb0

ASM Method:

private MethodNode createDecryptMethod() {
    MethodNode methodNode = new MethodNode(Opcodes.ACC_PRIVATE | Opcodes.ACC_STATIC,
            Miscellaneous.getRandomString(), "(Ljava/lang/String;)Ljava/lang/String;", null, null);
    methodNode.visitCode();
    methodNode.visitLdcInsn("");
    methodNode.visitVarInsn(Opcodes.ASTORE, 2);
    methodNode.visitInsn(Opcodes.ICONST_0);
    methodNode.visitVarInsn(Opcodes.ISTORE, 3);
    Label ifLabel = new Label();
    methodNode.visitLabel(ifLabel);
    methodNode.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder");
    methodNode.visitInsn(Opcodes.DUP);
    methodNode.visitVarInsn(Opcodes.ALOAD, 2);
    methodNode.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "valueOf",
            "(Ljava/lang/Object;)Ljava/lang/String;", false);
    methodNode.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V",
            false);
    methodNode.visitVarInsn(Opcodes.ALOAD, 1);
    methodNode.visitVarInsn(Opcodes.ILOAD, 3);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    methodNode.visitIntInsn(Opcodes.BIPUSH, 48);
    methodNode.visitInsn(Opcodes.ISUB);
    methodNode.visitLdcInsn(this.randomKey);
    methodNode.visitVarInsn(Opcodes.ILOAD, 3);
    methodNode.visitLdcInsn(this.randomKey);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
    methodNode.visitInsn(Opcodes.ICONST_1);
    methodNode.visitInsn(Opcodes.ISUB);
    methodNode.visitInsn(Opcodes.IREM);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "charAt", "(I)C", false);
    methodNode.visitInsn(Opcodes.IXOR);
    methodNode.visitInsn(Opcodes.I2C);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
            "(C)Ljava/lang/StringBuilder;", false);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "toString",
            "()Ljava/lang/String;", false);
    methodNode.visitVarInsn(Opcodes.ASTORE, 2);
    methodNode.visitIincInsn(2, 1);
    methodNode.visitVarInsn(Opcodes.ILOAD, 3);
    methodNode.visitVarInsn(Opcodes.ALOAD, 1);
    methodNode.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "length", "()I", false);
    methodNode.visitJumpInsn(Opcodes.IF_ICMPLT, ifLabel); // XXX
    methodNode.visitVarInsn(Opcodes.ALOAD, 2);
    methodNode.visitInsn(Opcodes.ARETURN);
    methodNode.visitEnd();
    return methodNode;
}

Original method:

private String decrypt(String input) {
    String output = "";
    int i = 0;
    do {
        output += (char) ((input.charAt(i) - 48) ^ (int) "a".charAt(i % ("a".length() - 1)));
    } while (i++ < input.length());
    return output;
}

Sorry for my bad english :(

Michael Petch
  • 46,082
  • 8
  • 107
  • 198
  • Take a look at this questions https://stackoverflow.com/questions/26733946/java-lang-verifyerror-expecting-a-stackmap-frame-at-branch-target and https://stackoverflow.com/questions/32904113/java-lang-verifyerror-expecting-a-stackmap-frame-occuring-with-asm-generated-by – Nikolay Aug 05 '17 at 19:41
  • 1
    You have a `methodNode.visitVarInsn(Opcodes.ALOAD, 1);` without any corresponding store. Keep in mind that for `static` methods, the parameter indices start with zero, as there is no `this`. Also, your `methodNode.visitIincInsn(2, 1);` is wrong, as the index of your `int` variable is `3` (unless you decrement all indices for the `static` method). – Holger Aug 07 '17 at 15:43

0 Answers0