Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Programming
JVM Bytecode
Joe Kutner
@codefinger
JVM Langs Owner
@Heroku
.class.java
$ cat Main.class
????3
<init>()VCodeLineNumberTablemain([Ljava/lang/
String;)V
SourceFile Main.java
java/util/ArrayListHel...
javap
class Main {
public static void main(String[] args) {
(new ArrayList<String>()).add("Hello");
}
}
Compiled from "Main.java"
public class Main {
public Main();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Obje...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
[POSITION]: [OPERATION] [OPERAND]
0: new #2
[POSITION]: [OPERATION] [OPERAND]
Location of the instruction in
the raw binary data
0: new #2
[POSITION]: [OPERATION] [OPERAND]
A mnemonic that represents one of
the 256 possible opcodes
0: new #2
[POSITION]: [OPERATION] [OPERAND]
Arguments to the operation.
The number and type of these
depends on the operation in use...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
...
Constant pool:
#1 = Methodref #7.#16 // java/lang/Object...
#2 = Class #17 // java/util/ArrayList
#3 = Methodref #2.#1...
0: new #2
3: dup
4: invokespecial #3
7: ldc #4
9: invokevirtual #5
12: pop
13: return
Stack-Based Model of
Computation
Exception in thread "main" java.lang.NullPointerException
at Printer.printString(Printer.java:13)
at Printer.print(Printer...
Stack Frame
Operand StackLocal Vars Constant Pool
#1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Methodref
...
...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
invokespecial
Used to invoke an initializer, private method, or superclass method
invokeinterface
Used to invoke an interf...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
...
Constant pool:
#1 = Methodref #7.#16 // java/lang/Object...
#2 = Class #17 // java/util/ArrayList
#3 = Methodref #2.#1...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
...
Constant pool:
#1 = Methodref #7.#16 // java/lang/Object...
#2 = Class #17 // java/util/ArrayList
#3 = Methodref #2.#1...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
Stack Frame
Operand StackLocal Vars Constant Pool
<ArrayList>
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = Stri...
...
Constant pool:
#1 = Methodref #7.#16 // java/lang/Object...
#2 = Class #17 // java/util/ArrayList
#3 = Methodref #2.#1...
java/util/ArrayList.add:(Ljava/lang/Object;)Z
B - byte
C - char
S - short
I - int
J - long
F - float
D - double
Z - boolea...
Stack Frame
Operand StackLocal Vars Constant Pool
ArrayList
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String...
Object
“Hello”
...
Methodref
Target
Arguments
Operand Stack
Stack Frame
Operand StackLocal Vars Constant Pool
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Meth...
Stack Frame
Operand StackLocal Vars Constant Pool
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Meth...
Stack Frame
Operand StackLocal Vars Constant Pool
ArrayList #1 = Methodref
#2 = Class
#3 = Methodref
#4 = String
#5 = Meth...
swap
iadd
getstatic [CLASS] [FIELD]
iconst_1
Why Read Bytecode?
• Diagnose performance problems
• Reverse engineering
• Security audit
• Debug legacy code
• FindBugs p...
Writing
JVM Bytecode
000000B0: 73 01 00 0A 4C 43 6F 6D 70 69 6C 65 72 3B 01 00
000000C0: 07 63 6F 6D 70 69 6C 65 01 00 32 28 5B 4C 6A 61
000000...
ASM
http://asm.ow2.org/
MethodVisitor mv = cv.visitMethod(
Opcodes.ACC_PRIVATE,
"setupLogger",
"(Lorg/apache/maven/cli/MavenCli$CliRequest;)Lorg/c...
Jitescript
https://github.com/qmx/jitescript
An ECMAScript runtime for the JVM
JiteClass
➤ CodeBlock
➤ CodeBlock
➤ CodeBlock
Compiled from "Main.java"
public class Main {
public Main();
Code:
0: aload_...
new JiteClass("Main") {{
defineMethod("main", ACC_PUBLIC | ACC_STATIC,
sig(void.class, String[].class),
new CodeBlock() {{...
new JiteClass("Main") {{
defineMethod("main", ACC_PUBLIC | ACC_STATIC,
sig(void.class, String[].class),
new CodeBlock() {{...
new JiteClass("Main") {{
defineMethod("main", ACC_PUBLIC | ACC_STATIC,
sig(void.class, String[].class),
new CodeBlock() {{...
new JiteClass("Main") {{
defineMethod("main", ACC_PUBLIC | ACC_STATIC,
sig(void.class, String[].class),
new CodeBlock() {{...
JiteClass .class
.toBytes()
Brainfu++
> > + + + + + + [ - s + + + + + + + s ] < p
p
>
<
: print a variable
: push a 0 onto the stack
: pop the top value off of the stack
+
-
[
: increment a variable
: dec...
This is our machine:
An empty stack
> > + + + + + + [ - s + + + + + + + s ] < p
> > + + + + + + [ - s + + + + + + + s ] < p
Push two zeros
0
0
> > + + + + + + [ - s + + + + + + + s ] < p
Inc to 6
0
6
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
0
6
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
0
5
Decrement
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
5
0
Swap
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
5
7
Increment
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
7
5
Swap
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
7
5
End of loop
> > + + + + + + [ - s + + + + + + + s ] < p
Loop
42
0
Repeat until 0
> > + + + + + + [ - s + + + + + + + s ] < p
42
Pop and Print
public JiteClass compile(final String program) {
return new JiteClass("Main") {{
defineMethod("main", ACC_PUBLIC | ACC_STA...
for (String cmd : program.split(“ “)) {
if ("p".equals(cmd)) {
} else if ("+".equals(cmd)) {
} else if ("-".equals(cmd)) {...
for (String cmd : program.split(“ “)) {
if ("p".equals(cmd)) {
} else if ("+".equals(cmd)) {
} else if ("-".equals(cmd)) {...
if ("p".equals(cmd)) {
dup();
invokestatic(
p(String.class),
"valueOf",
sig(String.class, int.class));
getstatic(p(System....
if ("p".equals(cmd)) {
brainPrint();
}
else if ("+".equals(cmd)) {
iconst_1();
iadd();
}
else if (“-".equals(cmd)) {
iconst_1();
isub();
}
else if (“>".equals(cmd)) {
iconst_0();
}
else if (“<".equals(cmd)) {
pop();
}
else if (“s".equals(cmd)) {
swap();
}
Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
La...
Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
La...
Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
La...
Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>();
else if (“[“.equals(cmd)) {
LabelNode begin = new LabelNode();
La...
else if (“]“.equals(cmd)) {
LabelNode[] labelNodes = loopStack.pop();
LabelNode begin = labelNodes[0];
LabelNode end = lab...
14: dup
15: ifeq 39
...
36: goto 14
39: pop
$ java Compiler “< < + + +”
Generated Main.class
$ java Main
42
Byteman
http://byteman.jboss.org/
Javassist
http://www.javassist.org/
class ThirdParty {
public void run() {
fetchData();
processData();
invokeService();
resolveConflicts();
finalize();
}
}
ClassPool cp = ClassPool.getDefault();
CtClass cc = cp.get(“ThirdParty");
CtMethod m = cc.getDeclaredMethod(“fetchData");
...
calling ThirdParty
fetching data...
Time (ms): 55
processing data...
Time (ms): 6032
Improve performance
Improve performance
Obfuscate code
Obfuscate code
Make a DSL…
No, make a new
JVM Language
(do 10 (print (* 7 5)))
pragprog.com
The Dragon Book
Brainfu** (Jipsy)
List of JVM Bytecode Operations
http://github.com/jkutner/jipsy
http://docs.oracle.com/javase/specs/jvms...
ASM Bytecode Outline
ProGuard
https://plugins.jetbrains.com/plugin/5918
http://proguard.sourceforge.net/
Dr. Garbage
http:...
The next time you compile…
Run javap against the class file.
Thank You
Joe Kutner
@codefinger
http://www.slideshare.net/jkutner/programming-jvm-bytecode
Programming JVM Bytecode with Jitescript
Programming JVM Bytecode with Jitescript
Upcoming SlideShare
Loading in …5
×

Programming JVM Bytecode with Jitescript

842 views

Published on

Every Java developer should have a good working knowledge of JVM bytecode. It’s fun, it can help you diagnose problems, improve performance, and even empowers you to build your own languages. No matter what kind of Java application you work on, you’ll get something out of this talk. We’ll start with bytecode fundamentals. You’ll learn how the most common operations work and see visual representations of how the JVM executes that code. The second part of the talk will introduce Jitescript, a Java library for generating bytecode. You learn how to use Jitescript with some plain old Java code to create your own JVM languages.

Published in: Engineering
  • Be the first to comment

Programming JVM Bytecode with Jitescript

  1. 1. Programming JVM Bytecode
  2. 2. Joe Kutner @codefinger JVM Langs Owner @Heroku
  3. 3. .class.java
  4. 4. $ cat Main.class ????3 <init>()VCodeLineNumberTablemain([Ljava/lang/ String;)V SourceFile Main.java java/util/ArrayListHello Mainjava/lang/Objectadd(Ljava/lang/Object;)Z! *?? *?Y??W?
  5. 5. javap
  6. 6. class Main { public static void main(String[] args) { (new ArrayList<String>()).add("Hello"); } }
  7. 7. Compiled from "Main.java" public class Main { public Main(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object... 4: return public static void main(java.lang.String[]); Code: 0: new #2 // class java/util/ArrayList 3: dup 4: invokespecial #3 // Method java/util/ArrayList... 7: ldc #4 // String Hello 9: invokevirtual #5 // Method java/util/ArrayList... 12: pop 13: return } $ javac Main.java $ javap -c Main
  8. 8. 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  9. 9. [POSITION]: [OPERATION] [OPERAND] 0: new #2
  10. 10. [POSITION]: [OPERATION] [OPERAND] Location of the instruction in the raw binary data 0: new #2
  11. 11. [POSITION]: [OPERATION] [OPERAND] A mnemonic that represents one of the 256 possible opcodes 0: new #2
  12. 12. [POSITION]: [OPERATION] [OPERAND] Arguments to the operation. The number and type of these depends on the operation in use. 0: new #2
  13. 13. 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  14. 14. ... Constant pool: #1 = Methodref #7.#16 // java/lang/Object... #2 = Class #17 // java/util/ArrayList #3 = Methodref #2.#16 // java/util/ArrayList.”<init>” #4 = String #18 // Hello #5 = Methodref #2.#19 // java/util/ArrayList.add:(L... #6 = Class #20 // Main #7 = Class #21 // java/lang/Object #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 main #13 = Utf8 ([Ljava/lang/String;)V #14 = Utf8 SourceFile #15 = Utf8 Main.java #16 = NameAndType #8:#9 // "<init>":()V #17 = Utf8 java/util/ArrayList $ javap -v -c Main
  15. 15. 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  16. 16. Stack-Based Model of Computation
  17. 17. Exception in thread "main" java.lang.NullPointerException at Printer.printString(Printer.java:13) at Printer.print(Printer.java:9) at Printer.main(Printer.java:19) Stack Frame Operand Stack Local Variable Array Constant Pool #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ...
  18. 18. Stack Frame Operand StackLocal Vars Constant Pool #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  19. 19. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  20. 20. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <ArrayList>
  21. 21. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <ArrayList>
  22. 22. invokespecial Used to invoke an initializer, private method, or superclass method invokeinterface Used to invoke an interface method invokestatic Used to invoke a class-level method (i.e. static methods) invokevirtual Used to invoke an instance method invokedynamic Used to invoke methods dynamically (new shiny)
  23. 23. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <ArrayList>
  24. 24. ... Constant pool: #1 = Methodref #7.#16 // java/lang/Object... #2 = Class #17 // java/util/ArrayList #3 = Methodref #2.#16 // java/util/ArrayList.”<init>” #4 = String #18 // Hello #5 = Methodref #2.#19 // java/util/ArrayList.add:(L... #6 = Class #20 // Main #7 = Class #21 // java/lang/Object #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 main #13 = Utf8 ([Ljava/lang/String;)V #14 = Utf8 SourceFile #15 = Utf8 Main.java #16 = NameAndType #8:#9 // "<init>":()V #17 = Utf8 java/util/ArrayList $ javap -v -c Main
  25. 25. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <ArrayList> Stack Frame Operand StackLocal Vars Constant Pool #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ...
  26. 26. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  27. 27. ... Constant pool: #1 = Methodref #7.#16 // java/lang/Object... #2 = Class #17 // java/util/ArrayList #3 = Methodref #2.#16 // java/util/ArrayList.”<init>” #4 = String #18 // Hello #5 = Methodref #2.#19 // java/util/ArrayList.add:(L... #6 = Class #20 // Main #7 = Class #21 // java/lang/Object #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 main #13 = Utf8 ([Ljava/lang/String;)V #14 = Utf8 SourceFile #15 = Utf8 Main.java #16 = NameAndType #8:#9 // "<init>":()V #17 = Utf8 java/util/ArrayList #18 = Utf8 Hello #19 = NameAndType #22:#23 // add:(Ljava/lang/Object;)Z $ javap -v -c Main
  28. 28. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <“Hello”>
  29. 29. Stack Frame Operand StackLocal Vars Constant Pool <ArrayList> ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <“Hello”>
  30. 30. ... Constant pool: #1 = Methodref #7.#16 // java/lang/Object... #2 = Class #17 // java/util/ArrayList #3 = Methodref #2.#16 // java/util/ArrayList.”<init>” #4 = String #18 // Hello #5 = Methodref #2.#19 // java/util/ArrayList.add:(L... #6 = Class #20 // Main #7 = Class #21 // java/lang/Object #8 = Utf8 <init> #9 = Utf8 ()V #10 = Utf8 Code #11 = Utf8 LineNumberTable #12 = Utf8 main #13 = Utf8 ([Ljava/lang/String;)V #14 = Utf8 SourceFile #15 = Utf8 Main.java #16 = NameAndType #8:#9 // "<init>":()V #17 = Utf8 java/util/ArrayList #18 = Utf8 Hello #19 = NameAndType #22:#23 // add:(Ljava/lang/Object;)Z $ javap -v -c Main
  31. 31. java/util/ArrayList.add:(Ljava/lang/Object;)Z B - byte C - char S - short I - int J - long F - float D - double Z - boolean V - void L - object
  32. 32. Stack Frame Operand StackLocal Vars Constant Pool ArrayList ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return “Hello”
  33. 33. Object “Hello” ... Methodref Target Arguments Operand Stack
  34. 34. Stack Frame Operand StackLocal Vars Constant Pool ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return <ArrayList> <“Hello”> Stack Frame Operand StackLocal Vars Constant Pool #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... true
  35. 35. Stack Frame Operand StackLocal Vars Constant Pool ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return true
  36. 36. Stack Frame Operand StackLocal Vars Constant Pool ArrayList #1 = Methodref #2 = Class #3 = Methodref #4 = String #5 = Methodref ... 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return
  37. 37. swap
  38. 38. iadd
  39. 39. getstatic [CLASS] [FIELD]
  40. 40. iconst_1
  41. 41. Why Read Bytecode? • Diagnose performance problems • Reverse engineering • Security audit • Debug legacy code • FindBugs plugins that work across languages
  42. 42. Writing JVM Bytecode
  43. 43. 000000B0: 73 01 00 0A 4C 43 6F 6D 70 69 6C 65 72 3B 01 00 000000C0: 07 63 6F 6D 70 69 6C 65 01 00 32 28 5B 4C 6A 61 000000D0: 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 000000E0: 4C 6D 65 2F 71 6D 78 2F 6A 69 74 65 73 63 72 69 000000F0: 70 74 2F 4A 69 74 65 43 6C 61 73 73 3B 01 00 06 00000100: 74 6F 6B 65 6E 73 01 00 13 5B 4C 6A 61 76 61 2F 00000110: 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 01 00 04 6D 00000120: 61 69 6E 01 00 16 28 5B 4C 6A 61 76 61 2F 6C 61 00000130: 6E 67 2F 53 74 72 69 6E 67 3B 29 56 01 00 04 61 00000140: 72 67 73 01 00 09 6A 69 74 65 43 6C 61 73 73 01 00000150: 00 1D 4C 6D 65 2F 71 6D 78 2F 6A 69 74 65 73 63 00000160: 72 69 70 74 2F 4A 69 74 65 43 6C 61 73 73 3B 01 00000170: 00 06 6F 75 74 70 75 74 01 00 1A 4C 6A 61 76 61 00000180: 2F 69 6F 2F 46 69 6C 65 4F 75 74 70 75 74 53 74 00000190: 72 65 61 6D 3B 01 00 0A 45 78 63 65 70 74 69 6F 000001A0: 6E 73 07 00 3A 01 00 0A 53 6F 75 72 63 65 46 69 000001B0: 6C 65 01 00 0A 43 6F 6D 70 69 6C 65 72 2E 6A 61 000001C0: 76 61 0C 00 14 00 15 01 00 0A 43 6F 6D 70 69 6C 000001D0: 65 72 24 31 01 00 04 4D 61 69 6E 0C 00 14 00 3B 000001E0: 01 00 08 43 6F 6D 70 69 6C 65 72 0C 00 1B 00 1C
  44. 44. ASM http://asm.ow2.org/
  45. 45. MethodVisitor mv = cv.visitMethod( Opcodes.ACC_PRIVATE, "setupLogger", "(Lorg/apache/maven/cli/MavenCli$CliRequest;)Lorg/codehaus/ plexus/logging/Logger;", null, null); mv.visitCode(); mv.visitTypeInsn( Opcodes.NEW, "com/github/jcgay/maven/color/logger/AnsiColorLogger"); https://github.com/jcgay/maven-color
  46. 46. Jitescript https://github.com/qmx/jitescript
  47. 47. An ECMAScript runtime for the JVM
  48. 48. JiteClass ➤ CodeBlock ➤ CodeBlock ➤ CodeBlock Compiled from "Main.java" public class Main { public Main(); Code: 0: aload_0 1: invokespecial #1 4: return public static void main... Code: 0: new #2 3: dup 4: invokespecial #3 7: ldc #4 9: invokevirtual #5 12: pop 13: return }
  49. 49. new JiteClass("Main") {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ // ... }}); }};
  50. 50. new JiteClass("Main") {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ new(p(ArrayList.class)); dup(); invokestatic( p(ArrayList.class), ”<init>”, sig(void.class)); ldc(“Hello”) invokevirtual( p(ArrayList.class), “add”, sig(boolean.class, Object.class)); pop(); voidreturn(); }}); }};
  51. 51. new JiteClass("Main") {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ getstatic(p(System.class), "out", ci(PrintStream.class)); ldc(“42"); invokevirtual( p(PrintStream.class), "println", sig(void.class, Object.class)); voidreturn(); }}); }};
  52. 52. new JiteClass("Main") {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ getstatic(p(System.class), "out", ci(PrintStream.class)); ldc(“42"); invokevirtual( p(PrintStream.class), "println", sig(void.class, Object.class)); voidreturn(); }}); }}; 0: getstatic #13 3: ldc #15 5: invokevirtual #21 8: return
  53. 53. JiteClass .class .toBytes()
  54. 54. Brainfu++ > > + + + + + + [ - s + + + + + + + s ] < p
  55. 55. p > < : print a variable : push a 0 onto the stack : pop the top value off of the stack + - [ : increment a variable : decrement a variable : start a loop ] : end a loop s : swap the top two values of the stack
  56. 56. This is our machine: An empty stack
  57. 57. > > + + + + + + [ - s + + + + + + + s ] < p
  58. 58. > > + + + + + + [ - s + + + + + + + s ] < p Push two zeros 0 0
  59. 59. > > + + + + + + [ - s + + + + + + + s ] < p Inc to 6 0 6
  60. 60. > > + + + + + + [ - s + + + + + + + s ] < p Loop 0 6
  61. 61. > > + + + + + + [ - s + + + + + + + s ] < p Loop 0 5 Decrement
  62. 62. > > + + + + + + [ - s + + + + + + + s ] < p Loop 5 0 Swap
  63. 63. > > + + + + + + [ - s + + + + + + + s ] < p Loop 5 7 Increment
  64. 64. > > + + + + + + [ - s + + + + + + + s ] < p Loop 7 5 Swap
  65. 65. > > + + + + + + [ - s + + + + + + + s ] < p Loop 7 5 End of loop
  66. 66. > > + + + + + + [ - s + + + + + + + s ] < p Loop 42 0 Repeat until 0
  67. 67. > > + + + + + + [ - s + + + + + + + s ] < p 42 Pop and Print
  68. 68. public JiteClass compile(final String program) { return new JiteClass("Main") {{ defineMethod("main", ACC_PUBLIC | ACC_STATIC, sig(void.class, String[].class), new CodeBlock() {{ // ... }}); }}; }
  69. 69. for (String cmd : program.split(“ “)) { if ("p".equals(cmd)) { } else if ("+".equals(cmd)) { } else if ("-".equals(cmd)) { } else if (">".equals(cmd)) { } else if ("<".equals(cmd)) { } else if ("s".equals(cmd)) { } else if ("[".equals(cmd)) { } else if ("]".equals(cmd)) { } }
  70. 70. for (String cmd : program.split(“ “)) { if ("p".equals(cmd)) { } else if ("+".equals(cmd)) { } else if ("-".equals(cmd)) { } else if (">".equals(cmd)) { } else if ("<".equals(cmd)) { } else if ("s".equals(cmd)) { } else if ("[".equals(cmd)) { } else if ("]".equals(cmd)) { } }
  71. 71. if ("p".equals(cmd)) { dup(); invokestatic( p(String.class), "valueOf", sig(String.class, int.class)); getstatic(p(System.class), "out", ci(PrintStream.class)); swap(); invokevirtual( p(PrintStream.class), "print", sig(void.class, Object.class)); }
  72. 72. if ("p".equals(cmd)) { brainPrint(); }
  73. 73. else if ("+".equals(cmd)) { iconst_1(); iadd(); }
  74. 74. else if (“-".equals(cmd)) { iconst_1(); isub(); }
  75. 75. else if (“>".equals(cmd)) { iconst_0(); }
  76. 76. else if (“<".equals(cmd)) { pop(); }
  77. 77. else if (“s".equals(cmd)) { swap(); }
  78. 78. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>(); else if (“[“.equals(cmd)) { LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); label(begin); dup(); ifeq(end); loopStack.push(new LabelNode[] {begin, end}); }
  79. 79. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>(); else if (“[“.equals(cmd)) { LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); label(begin); dup(); ifeq(end); loopStack.push(new LabelNode[] {begin, end}); }
  80. 80. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>(); else if (“[“.equals(cmd)) { LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); label(begin); dup(); ifeq(end); loopStack.push(new LabelNode[] {begin, end}); }
  81. 81. Stack<LabelNode[]> loopStack = new Stack<LabelNode[]>(); else if (“[“.equals(cmd)) { LabelNode begin = new LabelNode(); LabelNode end = new LabelNode(); label(begin); dup(); ifeq(end); loopStack.push(new LabelNode[] {begin, end}); }
  82. 82. else if (“]“.equals(cmd)) { LabelNode[] labelNodes = loopStack.pop(); LabelNode begin = labelNodes[0]; LabelNode end = labelNodes[1]; go_to(begin); label(end); }
  83. 83. 14: dup 15: ifeq 39 ... 36: goto 14 39: pop
  84. 84. $ java Compiler “< < + + +” Generated Main.class $ java Main 42
  85. 85. Byteman http://byteman.jboss.org/
  86. 86. Javassist http://www.javassist.org/
  87. 87. class ThirdParty { public void run() { fetchData(); processData(); invokeService(); resolveConflicts(); finalize(); } }
  88. 88. ClassPool cp = ClassPool.getDefault(); CtClass cc = cp.get(“ThirdParty"); CtMethod m = cc.getDeclaredMethod(“fetchData"); m.addLocalVariable("t", CtClass.longType); m.insertBefore( "t = System.currentTimeMillis();"); m.insertAfter( “{“ + “ t = System.currentTimeMillis() - t;” + “ System.out.println("Time (ms): " + t);” + ”}"); byte[] byteCode = cc.toBytecode(); cc.detach(); http://www.tomsquest.com/blog/2014/01/intro-java-agent-and-bytecode-manipulation/
  89. 89. calling ThirdParty fetching data... Time (ms): 55 processing data... Time (ms): 6032
  90. 90. Improve performance
  91. 91. Improve performance
  92. 92. Obfuscate code
  93. 93. Obfuscate code
  94. 94. Make a DSL…
  95. 95. No, make a new JVM Language
  96. 96. (do 10 (print (* 7 5)))
  97. 97. pragprog.com
  98. 98. The Dragon Book
  99. 99. Brainfu** (Jipsy) List of JVM Bytecode Operations http://github.com/jkutner/jipsy http://docs.oracle.com/javase/specs/jvms/se8/html/ Jitescript https://github.com/qmx/jitescript
  100. 100. ASM Bytecode Outline ProGuard https://plugins.jetbrains.com/plugin/5918 http://proguard.sourceforge.net/ Dr. Garbage http://www.drgarbage.com/
  101. 101. The next time you compile… Run javap against the class file.
  102. 102. Thank You Joe Kutner @codefinger http://www.slideshare.net/jkutner/programming-jvm-bytecode

×