mastering java bytecode with asm - geecon 2012

Download Mastering java bytecode with ASM - GeeCON 2012

Post on 10-May-2015

3.363 views

Category:

Technology

4 download

Embed Size (px)

TRANSCRIPT

  • 1.Mastering Java Bytecode with ASMLearn some bytecode to yourself!

2. whoamiAnton ArhipovJava Dev / Product LeadZeroTurnaround, JRebelMessing with bytecode since 2010anton@zeroturnaround.com@antonarhipov @javarebel 3. whoamiAnton ArhipovJava Dev / Product LeadZeroTurnaround, JRebelMessing with bytecode since 2010anton@zeroturnaround.com@antonarhipov @javarebel 4. Why Bytecode? Know your platform! Build your own JVM language? Programming models (AOP, ORM) Awesome tools (like JRebel )... just bored? 5. Bytecode 101 Instrumentation API javap ObjectWeb ASM 6. Bytecode 101Gentle introduction 7. Adding Two ValuesA+B 8. Adding Two ValuesA+BAB+ 9. Adding Two ValuesA+BAB+ 10. Adding Two ValuesA+B PUSH AAAB+ 11. Adding Two ValuesA+B PUSH 1PUSH 2BAB+ A 12. Adding Two ValuesA+B PUSH 1PUSH 215AB+ ADD 13. Adding Two ValuesA+B ICONST_1ICONST_215AB+ IADD 14. TYPE OPERATION ::= b, s, c, i, l, f, d, a constant values (ldc, iconst_1) Local variables and stack interaction (load/store) Array operations (aload, astore) Math (add, sub, mul, div) Boolean/bitwise operations (iand, ixor) Comparisons & branching (cmpl, ifeq, jsr, tableswitch) Conversions (l2d, i2l) 15. Model ofExecution 16. Enter JVMJVM process 17. Enter ThreadsThread AThread B Thread C Thread D 18. Enter Frames 19. The FrameLocal variables0 1 2NOperand stack#1ConstantPool 20. Juggling The Stack 21. Juggling The StackdupApopBswapdup_x1dup2_x1 22. Juggling The StackdupApopAswap Bdup_x1dup2_x1 23. Juggling The StackdupApopBswapdup_x1dup2_x1 24. Juggling The StackdupBpopAswapdup_x1dup2_x1 25. Juggling The StackdupBpopAswap Bdup_x1dup2_x1 26. Juggling The StackdupBpopAswap Bdup_x1 Bdup2_x1A 27. Local Variables 28. Local Variables Stackvarvaluedepth value0ldc "Hello"0astore_011iconst_12 astore_1 23 aload_0344 29. Local Variables Stackvarvaluedepth value0ldc "Hello"0 "Hello"astore_011iconst_12 astore_1 23 aload_0344 30. Local Variables Stackvarvaluedepth value0ldc "Hello"0"Hello"astore_011iconst_12 astore_1 23 aload_0344 31. Local Variables Stackvarvaluedepth value0ldc "Hello"0"Hello" 1astore_011iconst_12 astore_1 23 aload_0344 32. Local Variables Stackvarvaluedepth value0ldc "Hello"0"Hello"astore_01 11iconst_12 astore_1 23 aload_0344 33. Local Variables Stackvarvaluedepth value0ldc "Hello"0"Hello""Hello"astore_01 11iconst_12 astore_1 23 aload_0344 34. loadLocalVariables StackTablestore 35. Method Invoation 36. Method Invocationobj.method(param1, param2); 37. Method Invocationobj.method(param1, param2);push objpush param1push param2invoke method 38. Method Invocationobj.method(param1, param2);objpush objpush param1push param2invoke method 39. Method Invocationobj.method(param1, param2);param1push obj objpush param1push param2invoke method 40. Method Invocationobj.method(param1, param2);param2push objparam1push param1objpush param2invoke method 41. Method Invocationobj.method(param1, param2);obj?push objpush param1push param2invoke method 42. Operator Overloading 43. Operator Overloading[int] A + B[Foo] A.plus(B) 44. Operator Overloading[int] A + B[Foo] A.plus(B) push A push A push B push B iadd invokevirtual plus 45. Operator Overloading[int] A + B[Foo] A + B push A push A push B push B iadd invokevirtual plus 46. pop push Stack 47. pop pushload LocalVariablesStackTablestore 48. pop pushload LocalVariablesStackTablestore 49. poppushload LocalVariables Stack invokeTablestore pop push loadLocal Variables Stack Table store 50. javapThe disassembler 51. javap Java class file disassembler Used with no options shows class structure only Methods, superclass, interfaces, etc -c shows the bytecode -private shows all methods and members -s prints internal signatures -l prints line numbers and local variable tables -verbose for verbosity 52. C:workgeeconclasses>javap Hello -c 53. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:the default constructor0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: return 54. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:push this to stack0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: return 55. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: return invoke on this 56. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V super()4: return 57. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: return 58. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 59. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnget static fieldpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 60. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)Vload string to the stack 61. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V invoke method with parameter 62. C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 63. Whats #1,#2, etc ?C:workgeeconclasses>javap Hello -cCompiled from "Hello.java"public class Hello extends java.lang.Object{public Hello();Code:0: aload_01: invokespecial #1; //Method java/lang/Object."":()V4: returnpublic static void main(java.lang.String[]);Code:0: getstatic#2; //Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3; //String Hello, World!5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V 64. SLIDESGOTO: IDESLIDESIDE: JAVAP DEMO 65. ASMThe de facto standard for bytecode manipulation 66. ASM All purpose bytecode manipulationand analysis framework De facto standard bytecode library http://asm.ow2.org 67. Basic Process Construct ClassWriter Stack up the visitors for: annotations, methods, fields, etc Write out bytes 68. Hello.java 69. ClassWriterClassWriter cw = new ClassWriter( ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); 70. COMPUTE_*** COMPUTE_MAXS ASM will calculate max stack/local vars COMPUTE_FRAMES ASM will calculate Java 6 stack map 71. Visit Classcv.visit(V1_6,ACC_PUBLIC,"X",null,"java/lang/Object",null); 72. Opcodes Interface full of constantsBytecodesVisibility modifiersJava versionsOther stuff 73. ACC_*** Some you knowACC_PUBLIC, ACC_ABSTRACT, etc Some you (probably) dontACC_BRIDGE, ACC_SYNTHETIC 74. Class Names"java/lang/Object"packageClass.replaceAll(., /) 75. Type Descriptors 76. Type DescriptorsBbyteCcharSstringIintJlongFfloatDdoubleZbooleanVvoid 77. Type DescriptorsLsome/Class; 78. Type Descriptors[Lsome/Class; 79. Method Signatures()Vvoid foo()(Ljava/lang/Object;)Iint foo(Object)([Ljava/lang/String;)V void main(String[]) 80. Visit MethodMethodVisitor constructor =cv.visitMethod(ACC_PUBLIC,"","()V",null,null);MethodVisitor mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC,"main","([Ljava/lang/String;)V",null,null); 81. Visit MethodMethodVisitor constructor =cv.visitMethod(ACC_PUBLIC,"",Wat!? o_O"()V",null,null);MethodVisitor mv = cv.visitMethod(ACC_PUBLIC + ACC_STATIC,"main","([Ljava/lang/String;)V",null,null); 82. Special Methods Constructor Static initializer 83. MethodVisitor Visit annotations Visit code Bytecodes, local variables, line numbers, etc Visit maxs Pass bogus values if COMPUTE_MAX 84. Constructorc.visitVarInsn(ALOAD, 0);c.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V");c.visitInsn(RETURN);c.visitMaxs(0, 0); 85. Constructorc.visitVarInsn(ALOAD, 0);c.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "", "()V");c.visitInsn(RETURN);c.visitMaxs(0, 0); aload_0 invokespecial return 86. public static void main() 87. public static void main()mv.visitFieldInsn(GETSTATIC,"java/lang/System", "out","Ljava/io/PrintStream;");mv.visitLdcInsn("Hello");mv.visitMethodInsn(INVOKEVIRTUAL,"java/io/PrintStream", "println","(Ljava/lang/String;)V");mv.visitInsn(RETURN); 88. public static void main()mv.visitFieldInsn(GETSTATIC,"java/lang/System", "out",getstat