Last active
January 1, 2019 23:05
-
-
Save bhugueney/cbdb044e03c32c627ff4c863b04a135b to your computer and use it in GitHub Desktop.
Using ASM for control flow : example from asmifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class NestedIfs { | |
public static int run(int a, int b){ | |
int res= 0; | |
if( (a-b) == 0){ | |
int c= a+b; | |
if(c != 0){ | |
c= 5; | |
}else{ | |
c= 2; | |
} | |
res= -c; | |
}else{ | |
res= 1; | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import org.objectweb.asm.*; | |
import org.objectweb.asm.attrs.*; | |
public class NestedIfsDump implements Opcodes { | |
public static byte[] dump () throws Exception { | |
ClassWriter cw = new ClassWriter(0); | |
FieldVisitor fv; | |
MethodVisitor mv; | |
AnnotationVisitor av0; | |
cw.visit(52, ACC_PUBLIC + ACC_SUPER, "NestedIfs", null, "java/lang/Object", null); | |
{ | |
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); | |
mv.visitCode(); | |
mv.visitVarInsn(ALOAD, 0); | |
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); | |
mv.visitInsn(RETURN); | |
mv.visitMaxs(1, 1); | |
mv.visitEnd(); | |
} | |
{ | |
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "run", "(II)I", null, null); | |
mv.visitCode(); | |
mv.visitInsn(ICONST_0); | |
mv.visitVarInsn(ISTORE, 2); | |
mv.visitVarInsn(ILOAD, 0); | |
mv.visitVarInsn(ILOAD, 1); | |
mv.visitInsn(ISUB); | |
Label l0 = new Label(); | |
mv.visitJumpInsn(IFNE, l0); | |
mv.visitVarInsn(ILOAD, 0); | |
mv.visitVarInsn(ILOAD, 1); | |
mv.visitInsn(IADD); | |
mv.visitVarInsn(ISTORE, 3); | |
mv.visitVarInsn(ILOAD, 3); | |
Label l1 = new Label(); | |
mv.visitJumpInsn(IFEQ, l1); | |
mv.visitInsn(ICONST_5); | |
mv.visitVarInsn(ISTORE, 3); | |
Label l2 = new Label(); | |
mv.visitJumpInsn(GOTO, l2); | |
mv.visitLabel(l1); | |
mv.visitFrame(Opcodes.F_APPEND,2, new Object[] {Opcodes.INTEGER, Opcodes.INTEGER}, 0, null); | |
mv.visitInsn(ICONST_2); | |
mv.visitVarInsn(ISTORE, 3); | |
mv.visitLabel(l2); | |
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); | |
mv.visitVarInsn(ILOAD, 3); | |
mv.visitInsn(INEG); | |
mv.visitVarInsn(ISTORE, 2); | |
Label l3 = new Label(); | |
mv.visitJumpInsn(GOTO, l3); | |
mv.visitLabel(l0); | |
mv.visitFrame(Opcodes.F_CHOP,1, null, 0, null); | |
mv.visitInsn(ICONST_1); | |
mv.visitVarInsn(ISTORE, 2); | |
mv.visitLabel(l3); | |
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); | |
mv.visitVarInsn(ILOAD, 2); | |
mv.visitInsn(IRETURN); | |
mv.visitMaxs(2, 4); | |
mv.visitEnd(); | |
} | |
cw.visitEnd(); | |
return cw.toByteArray(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TestLoop{ | |
public static int run(int i){ | |
int res; | |
if(i !=0){ | |
res= i; | |
}else{ | |
res= -i; | |
} | |
return res; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import org.objectweb.asm.*; | |
import org.objectweb.asm.attrs.*; | |
public class TestLoopDump implements Opcodes { | |
public static byte[] dump () throws Exception { | |
ClassWriter cw = new ClassWriter(0); | |
FieldVisitor fv; | |
MethodVisitor mv; | |
AnnotationVisitor av0; | |
cw.visit(52, ACC_PUBLIC + ACC_SUPER, "TestLoop", null, "java/lang/Object", null); | |
{ | |
mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); | |
mv.visitCode(); | |
mv.visitVarInsn(ALOAD, 0); | |
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); | |
mv.visitInsn(RETURN); | |
mv.visitMaxs(1, 1); | |
mv.visitEnd(); | |
} | |
{ | |
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "run", "(I)I", null, null); | |
mv.visitCode(); | |
mv.visitVarInsn(ILOAD, 0); | |
Label l0 = new Label(); | |
mv.visitJumpInsn(IFEQ, l0); | |
mv.visitVarInsn(ILOAD, 0); | |
mv.visitVarInsn(ISTORE, 1); | |
Label l1 = new Label(); | |
mv.visitJumpInsn(GOTO, l1); | |
mv.visitLabel(l0); | |
mv.visitFrame(Opcodes.F_SAME, 0, null, 0, null); | |
mv.visitVarInsn(ILOAD, 0); | |
mv.visitInsn(INEG); | |
mv.visitVarInsn(ISTORE, 1); | |
mv.visitLabel(l1); | |
mv.visitFrame(Opcodes.F_APPEND,1, new Object[] {Opcodes.INTEGER}, 0, null); | |
mv.visitVarInsn(ILOAD, 1); | |
mv.visitInsn(IRETURN); | |
mv.visitMaxs(1, 2); | |
mv.visitEnd(); | |
} | |
cw.visitEnd(); | |
return cw.toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment