QCon NY 2014
Living in the Matrix with
Bytecode Manipulation
Wednesday, June 11, 14
InfoQ.com: News & Community Site
• 750,000 unique visitors/month
• Published in 4 languages (English, Chinese, Japanese an...
Presented at QCon New York
www.qconnewyork.com
Purpose of QCon
- to empower software development by facilitating the sprea...
Ashley Puls
Senior Software Engineer
New Relic, Inc.
Wednesday, June 11, 14
Follow Along
http://slidesha.re/1kZwCXr
Wednesday, June 11, 14
Outline
• What is bytecode?
• Why manipulate bytecode?
• What frameworks can be used to
manipulate bytecode?
• Examine two...
What is Java bytecode?
.java file .class file
Java compiler
(javac)
Class Loader
Bytecode
Verifier
Java Runtime
System
Native...
What is Java bytecode?
.java file .class file
Java compiler
(javac)
Class Loader
Bytecode
Verifier
Java Runtime
System
Native...
What is Java bytecode?
.java file .class file
Java compiler
(javac)
Class Loader
Bytecode
Verifier
Java Runtime
System
Native...
Why learn about Java bytecode
manipulation frameworks ?
Wednesday, June 11, 14
Why learn about Java bytecode
manipulation frameworks ?
• A bytecode manipulation framework is likely already
on your stac...
• A bytecode manipulation framework is likely already
on your stack
• Spring
• Hibernate
• Groovy
• Clojure
• Eclipse
• JR...
Why learn about Java bytecode
manipulation frameworks ?
• program analysis
• find bugs in code
• examine code complexity
• ...
Why learn about Java bytecode
manipulation frameworks ?
• program analysis
• find bugs in code
• examine code complexity
• ...
Logging
Source: http://www.vforteachers.com/About_NetSupport.htm
Wednesday, June 11, 14
Logging
Source: http://www.vforteachers.com/About_NetSupport.htm, http://inzolo.com/blog/tutorials/bank-accounts
Wednesday...
Logging
Source: http://www.vforteachers.com/About_NetSupport.htm, http://upload.wikimedia.org/wikipedia/commons/d/d3/49024...
Logging
Audit Log: a message should be
logged every time an important
method is called
Source: http://www.vforteachers.com...
Logging
Source: http://www.vforteachers.com/About_NetSupport.htm
log important input parameters
to the method
Audit Log: a...
Logging
public class BankTransactions {
	 public static void main(String[] args) {
	 	 BankTransactions bank = new BankTra...
Logging
public class BankTransactions {
	 public static void main(String[] args) {
	 	 BankTransactions bank = new BankTra...
Logging
/**
* A method annotation which should be used to indicate important methods whose
* invocations should be logged....
Logging
	 public void login(String password, String accountId, String userName) {
	 	 // login logic
	 }
	 public void fin...
Logging
	 @ImportantLog(fields = { "1", "2" })
	 public void login(String password, String accountId, String userName) {
	...
Logging
	 @ImportantLog(fields = { "1", "2" })
	 public void login(String password, String accountId, String userName) {
	...
Logging
	 @ImportantLog(fields = { "1", "2" })
	 public void login(String password, String accountId, String userName) {
	...
Java Agent
• Ability to modify the bytecode without modifying the
application source code
• Added in Java 1.5
• Docs: http...
Typical Java Process
JVM
public static void main(String[] args)
BankTransactions.class
Classloader
java com/example/qcon/m...
Java Agent
JVMAgent
java -javaagent:/path/to/agent.jar com/example/qcon/mains/
BankTransactions
Wednesday, June 11, 14
Java Agent
JVM
java -javaagent:/path/to/agent.jar com/example/qcon/mains/
BankTransactions
Wednesday, June 11, 14
Java Agent
JVMAgent
java -javaagent:/path/to/agent.jar com/example/qcon/mains/
BankTransactions
Wednesday, June 11, 14
Java Agent
JVM
void premain(String agentArgs, Instrumentation inst)
Agent.class
MyTransformer.class
byte[] transform( . . ...
Java Agent
JVM
void premain(String agentArgs, Instrumentation inst)
Agent.class
MyTransformer.class
byte[] transform( . . ...
Java Agent
JVM
void main(String[] args)
BankTransactions.class
void premain(String agentArgs, Instrumentation inst)
Agent....
lets code
Manifest:
Premain-Class: com.example.qcon.agent.Agent
Wednesday, June 11, 14
lets code
package com.example.qcon.agent;
public class Agent {
	 public static void premain(String agentArgs, Instrumentat...
lets code
package com.example.qcon.agent;
public class Agent {
	 public static void premain(String agentArgs, Instrumentat...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
Starting the agent
Loading class: java/lang/invoke/MethodHandleImpl
Loading class: java/lang/invoke/MemberName$F...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
Bytecode Manipulation Frameworks
• ASM: http://asm.ow2.org/
• BCEL: http://commons.apache.org/proper/commons-bcel/
• CGLib...
Bytecode Manipulation Frameworks
• ASM: http://asm.ow2.org/
• BCEL: http://commons.apache.org/proper/commons-bcel/
• CGLib...
Javassist
• Java Programming Assistant
• Subproject of JBoss
• Actively updated
• Version 3.18.2 released May 2014
• Sourc...
Javassist
ArrayList
HashMap
CtClass: represents a compile time class
Wednesday, June 11, 14
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
CtClass: represents a compile time class
Wednesday, Ju...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
ArrayList
HashMap
CtClass: represents a compile time c...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
ArrayList
HashMap
CtClass: represents a comp...
Javassist
CtClass: represents a compile time class
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
Arr...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
ArrayList
HashMap
Classpool.getDefault().ins...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
ArrayList
HashMap
BankTrans
Classpool.getDef...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
ArrayList
HashMap
BankTrans
Classpool.getDef...
Javassist
ClassPool: container of CtClass objects
ArrayList
HashMap
BankTrans
ArrayList
HashMap
BankTrans
ByteArrayClassPa...
Javassist
BankTrans
CtClass: represents a compile time class
Wednesday, June 11, 14
Javassist
CtConstructor: represents a compile time constructor
init
BankTrans
CtClass: represents a compile time class
Wed...
Javassist
CtConstructor: represents a compile time constructor
init
BankTrans
CtClass: represents a compile time class
CtM...
Javassist
CtConstructor: represents a compile time constructor
init
CtClass: represents a compile time class
CtMethod: rep...
Javassist
CtConstructor: represents a compile time constructor
init
CtClass: represents a compile time class
CtMethod: rep...
Javassist
login
CtMethod: represents a compile time method
Wednesday, June 11, 14
Javassist
Modifications:
login
CtMethod: represents a compile time method
Wednesday, June 11, 14
Javassist
Modifications:
login
CtMethod: represents a compile time method
putMethod.insertAfter(“System.out.println(”after ...
Javassist
Modifications:
System.out.println(“before method”);
CtMethod: represents a compile time method
putMethod.insertAf...
Javassist
Modifications:
CtMethod: represents a compile time method
CtClass exceptionType = classpool.get(“java.io.IOExcept...
Javassist
Modifications:
CtMethod: represents a compile time method
CtClass exceptionType = classpool.get(“java.io.IOExcept...
Javassist
Modifications:
CtMethod: represents a compile time method
putMethod.insertAt(5, true, “System.out.println(“hello”...
Javassist
Modifications:
CtMethod: represents a compile time method
putMethod.insertAt(5, true, “System.out.println(“hello”...
Javassist
Modifications:
CtMethod: represents a compile time method
insertBefore insertAfter insertAt addCatch
System.out.p...
Javassist
Modifications:
CtMethod: represents a compile time method
insertBefore insertAfter insertAt addCatch
Freeze Class...
Javassist
Modifications:
CtMethod: represents a compile time method
insertBefore insertAfter insertAt addCatch
Freeze Class...
lets code
Wednesday, June 11, 14
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
	 	 private Annotation getAnnotation(CtMethod method) {
	 	 MethodInfo mInfo = method.getMethodInfo();
	 	 // th...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
	 private List<String> getParamIndexes(Annotation annotation) {
	 	 ArrayMemberValue fields = (ArrayMemberValue)...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
lets code
	 private String createJavaString(CtMethod currentMethod, String className,
	 	 	 List<String> indexParameters) ...
lets code
	 private String createJavaString(CtMethod currentMethod, String className,
	 	 	 List<String> indexParameters) ...
lets code
	 private String createJavaString(CtMethod currentMethod, String className,
	 	 	 List<String> indexParameters) ...
lets code
	 private String createJavaString(CtMethod currentMethod, String className,
	 	 	 List<String> indexParameters) ...
lets code
public byte[] transform(ClassLoader loader, String className,
	 	 Class classBeingRedefined, ProtectionDomain pr...
Javassist
• Positives
• Simplicity
• Do not have to write actual bytecode
• Decent documentation
• Negatives
• Generally s...
ASM
• Released in Open Source in 2002
• Actively updated
• Version 5.0.3 released May 2014
• Two ASM libraries
• Event bas...
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
ClassReader: given a byte[], parses a compiled class
ClassReader:
event producer
BankTrans
ClassWriter:
event consumer...
ASM
ClassReader: given a byte[], parses a compiled class
ClassReader:
event producer
BankTrans
ClassWriter:
event consumer...
ASM
ClassVisitor: delegates class events, event filter
visitAttribute visitField visitMethod visitInnerClass visitEnd
Class...
ASM
ClassVisitor: delegates class events, event filter
visitAttribute visitField visitMethod visitInnerClass visitEnd
Class...
lets code
package com.example.qcon.agent;
import java.lang.instrument.Instrumentation;
public class Agent {
	 public stati...
lets code
package com.example.qcon.agent;
import java.lang.instrument.Instrumentation;
public class Agent {
	 public stati...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
package com.example.qcon.agent;
import java.lang.instrument.ClassFileTransformer;
import java.lang.instrument.Il...
lets code
	 public byte[] transform(ClassLoader loader, String className,
	 	 	 Class<?> classBeingRedefined, ProtectionDo...
lets code
	 public byte[] transform(ClassLoader loader, String className,
	 	 	 Class<?> classBeingRedefined, ProtectionDo...
ASM
ClassVisitor: delegates class events, event filter
visitAttribute visitField visitMethod visitInnerClass visitEnd
Class...
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
lets code
public class LogMethodClassVisitor extends ClassVisitor {
	 private String className;
	 public LogMethodIfAnnota...
lets code
public class LogMethodClassVisitor extends ClassVisitor {
	 private String className;
	 public LogMethodIfAnnota...
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
ASM
MethodVisitor: method event filter
MethodVisitor MethodVisitor
visitCode
login method
visitor
login method
visitor
visi...
ASM
Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf
Wednesday, June 11, 14
lets code
public class LogMethodClassVisitor extends ClassVisitor {
	 private String className;
	 public LogMethodIfAnnota...
lets code
public class PrintMessageMethodVisitor extends MethodVisitor {
	 	 private String methodName;
	 	 private String...
lets code
public class PrintMessageMethodVisitor extends MethodVisitor {
	 	 private String methodName;
	 	 private String...
lets code
	
	 public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
	 	 if ("Lcom/example/qcon/mains/Im...
lets code
public class PrintMessageMethodVisitor extends MethodVisitor {
	 	 private String methodName;
	 	 private String...
lets code
public void visitCode() {		
if (isAnnotationPresent) {
	 	 	 // create string builder
	 	 	 	 mv.visitFieldInsn(...
What is Java bytecode?
use javap to examine the bytecode
Wednesday, June 11, 14
What is Java bytecode?
public class PrintMessage {
	 private String methodName;
	 private String className;
	 public Print...
What is Java bytecode?
javap -c bin/com/example/qcon/mains/PrintMessage
Wednesday, June 11, 14
What is Java bytecode?
javap -c bin/com/example/qcon/mains/PrintMessage
public void print();
Code:
0: new #38 // class jav...
What is Java bytecode?
each line represents one JVM instruction
public void print();
Code:
0: new #38 // class java/lang/S...
What is Java bytecode?
JVM instruction = 1 opcode with 0 or more operands
public void print();
Code:
0: new #38 // class j...
What is Java bytecode?
Opcode Description
load
load a reference onto the stack from a local variable
iload, lload, fload, d...
What is Java bytecode?
Opcode Description
invokestatic invoke a class (static) method on a named class
athrow throws an ex...
What is Java bytecode?
Wednesday, June 11, 14
What is Java bytecode?
Wednesday, June 11, 14
What is Java bytecode?
Wednesday, June 11, 14
lets code
public void visitCode() {		
if (isAnnotationPresent) {
	 	 	 // create string builder
	 	 	 	 mv.visitFieldInsn(...
ASM
• Positives
• Speed and Size
• Decent documentation
• Depth of Functionality
• Number of people using framework
• Nega...
Why learn about Java bytecode
manipulation frameworks ?
• program analysis
• find bugs in code
• examine code complexity
• ...
Bytecode Manipulation Frameworks
• ASM: http://asm.ow2.org/
• BCEL: http://commons.apache.org/proper/commons-bcel/
• CGLib...
Source: http://www.kulfoto.com/dog-pictures/15799/mans-best-friend
Make bytecode manipulation frameworks your friend.
They...
Wednesday, June 11, 14
Source: http://alangregerman.typepad.com/.a/6a00d83516c0ad53ef017c3329aba8970b-800wi
Questions?
Wednesday, June 11, 14
Watch the video with slide synchronization on
InfoQ.com!
http://www.infoq.com/presentations/asm-cglib-
javassist
Upcoming SlideShare
Loading in...5
×

Living in the Matrix with Bytecode Manipulation

184

Published on

Video and slides synchronized, mp3 and slide download available at URL http://bit.ly/1jtTJtd.

Sponsored by New Relic. Ashley Puls introduces three common byte code manipulation frameworks: ASM, CGLib, and Javassist, providing enough detail to get one started. Filmed at qconnewyork.com.

Ashley Puls is a senior software engineer at New Relic Inc. which provides an all-in one web application performance tool. She works on the Java Agent team in Portland which focuses on instrumenting Java applications.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
184
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Transcript of "Living in the Matrix with Bytecode Manipulation"

  1. 1. QCon NY 2014 Living in the Matrix with Bytecode Manipulation Wednesday, June 11, 14
  2. 2. InfoQ.com: News & Community Site • 750,000 unique visitors/month • Published in 4 languages (English, Chinese, Japanese and Brazilian Portuguese) • Post content from our QCon conferences • News 15-20 / week • Articles 3-4 / week • Presentations (videos) 12-15 / week • Interviews 2-3 / week • Books 1 / month Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations /asm-cglib-javassist
  3. 3. Presented at QCon New York www.qconnewyork.com Purpose of QCon - to empower software development by facilitating the spread of knowledge and innovation Strategy - practitioner-driven conference designed for YOU: influencers of change and innovation in your teams - speakers and topics driving the evolution and innovation - connecting and catalyzing the influencers and innovators Highlights - attended by more than 12,000 delegates since 2007 - held in 9 cities worldwide
  4. 4. Ashley Puls Senior Software Engineer New Relic, Inc. Wednesday, June 11, 14
  5. 5. Follow Along http://slidesha.re/1kZwCXr Wednesday, June 11, 14
  6. 6. Outline • What is bytecode? • Why manipulate bytecode? • What frameworks can be used to manipulate bytecode? • Examine two frameworks in depth Wednesday, June 11, 14
  7. 7. What is Java bytecode? .java file .class file Java compiler (javac) Class Loader Bytecode Verifier Java Runtime System Native OS Java Virtual Machine Source: http://www.techlila.com/write-programs-linux/ Wednesday, June 11, 14
  8. 8. What is Java bytecode? .java file .class file Java compiler (javac) Class Loader Bytecode Verifier Java Runtime System Native OS Java Virtual Machine Source: http://www.techlila.com/write-programs-linux/ Wednesday, June 11, 14
  9. 9. What is Java bytecode? .java file .class file Java compiler (javac) Class Loader Bytecode Verifier Java Runtime System Native OS Java Virtual Machine Source: http://www.techlila.com/write-programs-linux/ Instruction set of the Java Virtual Machine Wednesday, June 11, 14
  10. 10. Why learn about Java bytecode manipulation frameworks ? Wednesday, June 11, 14
  11. 11. Why learn about Java bytecode manipulation frameworks ? • A bytecode manipulation framework is likely already on your stack • Spring • Hibernate • Groovy • Clojure • Eclipse • JRuby Wednesday, June 11, 14
  12. 12. • A bytecode manipulation framework is likely already on your stack • Spring • Hibernate • Groovy • Clojure • Eclipse • JRuby • Its really fun!!! Why learn about Java bytecode manipulation frameworks ? Wednesday, June 11, 14
  13. 13. Why learn about Java bytecode manipulation frameworks ? • program analysis • find bugs in code • examine code complexity • generate classes • proxies • remove access to certain APIs • compiler for another language like Scala • transform classes without Java source code • profilers • optimization and obfuscation • additional logging Wednesday, June 11, 14
  14. 14. Why learn about Java bytecode manipulation frameworks ? • program analysis • find bugs in code • examine code complexity • generate classes • proxies • remove access to certain APIs • compiler for another language like Scala • transform classes without Java source code • profilers • optimization and obfuscation • additional logging Wednesday, June 11, 14
  15. 15. Logging Source: http://www.vforteachers.com/About_NetSupport.htm Wednesday, June 11, 14
  16. 16. Logging Source: http://www.vforteachers.com/About_NetSupport.htm, http://inzolo.com/blog/tutorials/bank-accounts Wednesday, June 11, 14
  17. 17. Logging Source: http://www.vforteachers.com/About_NetSupport.htm, http://upload.wikimedia.org/wikipedia/commons/d/d3/49024-SOS-ATM.JPG Wednesday, June 11, 14
  18. 18. Logging Audit Log: a message should be logged every time an important method is called Source: http://www.vforteachers.com/About_NetSupport.htm Wednesday, June 11, 14
  19. 19. Logging Source: http://www.vforteachers.com/About_NetSupport.htm log important input parameters to the method Audit Log: a message should be logged every time an important method is called Wednesday, June 11, 14
  20. 20. Logging public class BankTransactions { public static void main(String[] args) { BankTransactions bank = new BankTransactions(); // login and add i dollars to account for (int i = 0; i < 100; i++) { String accountId = "account" + i; bank.login("password", accountId, "Ashley"); bank.unimportantProcessing(accountId); bank.finalizeTransaction(accountId, Double.valueOf(i)); } System.out.println(“Transactions completed”); } } Wednesday, June 11, 14
  21. 21. Logging public class BankTransactions { public static void main(String[] args) { BankTransactions bank = new BankTransactions(); // login and add i dollars to each account for (int i = 0; i < 100; i++) { String accountId = "account" + i; bank.login("password", accountId, "Ashley"); bank.unimportantProcessing(accountId); bank.finalizeTransaction(accountId, Double.valueOf(i)); } System.out.println(“Transactions completed”); } } Wednesday, June 11, 14
  22. 22. Logging /** * A method annotation which should be used to indicate important methods whose * invocations should be logged. * */ public @interface ImportantLog { /** * The method parameter indexes whose values should be logged. For example, * if we have the method hello(int paramA, int paramB, int paramC), and we * wanted to log the values of paramA and paramC, then fields would be ["0", * "2"]. If we only want to log the value of paramB, then fields would be * ["1"]. */ String[] fields(); } Wednesday, June 11, 14
  23. 23. Logging public void login(String password, String accountId, String userName) { // login logic } public void finalizeTransaction(String accountId, Double moneyToAdd) { // transaction logic } Wednesday, June 11, 14
  24. 24. Logging @ImportantLog(fields = { "1", "2" }) public void login(String password, String accountId, String userName) { // login logic } @ImportantLog(fields = { "0", "1" }) public void finalizeTransaction(String accountId, Double moneyToAdd) { // transaction logic } Wednesday, June 11, 14
  25. 25. Logging @ImportantLog(fields = { "1", "2" }) public void login(String password, String accountId, String userName) { // login logic } @ImportantLog(fields = { "0", "1" }) public void finalizeTransaction(String accountId, Double moneyToAdd) { // transaction logic } A call was made to method "login" on class "com/example/qcon/mains/BankTransactions". Important params: Index 1 value: ${accountId} Index 2 value: ${userName} Wednesday, June 11, 14
  26. 26. Logging @ImportantLog(fields = { "1", "2" }) public void login(String password, String accountId, String userName) { // login logic } @ImportantLog(fields = { "0", "1" }) public void finalizeTransaction(String accountId, Double moneyToAdd) { // transaction logic } A call was made to method "finalizeTransaction" on class "com/example/qcon/ mains/BankTransactions". Important params: Index 0 value: ${accountId} Index 1 value: ${moneyToAdd} A call was made to method "login" on class "com/example/qcon/mains/BankTransactions". Important params: Index 1 value: ${accountId} Index 2 value: ${userName} Wednesday, June 11, 14
  27. 27. Java Agent • Ability to modify the bytecode without modifying the application source code • Added in Java 1.5 • Docs: http://docs.oracle.com/javase/8/docs/api/ java/lang/instrument/package-summary.html Wednesday, June 11, 14
  28. 28. Typical Java Process JVM public static void main(String[] args) BankTransactions.class Classloader java com/example/qcon/mains/BankTransactions Transactions completed Wednesday, June 11, 14
  29. 29. Java Agent JVMAgent java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Wednesday, June 11, 14
  30. 30. Java Agent JVM java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Wednesday, June 11, 14
  31. 31. Java Agent JVMAgent java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Wednesday, June 11, 14
  32. 32. Java Agent JVM void premain(String agentArgs, Instrumentation inst) Agent.class MyTransformer.class byte[] transform( . . . , byte[] bankTransBytes) Agent 1. call Agent premain in manifest 2. JVM registers my transformer java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Wednesday, June 11, 14
  33. 33. Java Agent JVM void premain(String agentArgs, Instrumentation inst) Agent.class MyTransformer.class byte[] transform( . . . , byte[] bankTransBytes) Agent 1. call Agent premain in manifest 2. JVM registers my transformer 3. Give BankTransactions bytes to MyTransformer 4. MyTransformer provides bytes to load java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Wednesday, June 11, 14
  34. 34. Java Agent JVM void main(String[] args) BankTransactions.class void premain(String agentArgs, Instrumentation inst) Agent.class MyTransformer.class byte[] transform( . . . , byte[] bankTransBytes) Agent 1. call Agent premain in manifest 2. JVM registers my transformer 3. Give BankTransactions bytes to MyTransformer 4. MyTransformer provides bytes to load 5. BankTransactions loaded and main runs java -javaagent:/path/to/agent.jar com/example/qcon/mains/ BankTransactions Transactions completed Wednesday, June 11, 14
  35. 35. lets code Manifest: Premain-Class: com.example.qcon.agent.Agent Wednesday, June 11, 14
  36. 36. lets code package com.example.qcon.agent; public class Agent { public static void premain(String agentArgs, Instrumentation inst) { System.out.println("Starting the agent"); inst.addTransformer(new ImportantLogClassTransformer()); } } Manifest: Premain-Class: com.example.qcon.agent.Agent Wednesday, June 11, 14
  37. 37. lets code package com.example.qcon.agent; public class Agent { public static void premain(String agentArgs, Instrumentation inst) { System.out.println("Starting the agent"); inst.addTransformer(new ImportantLogClassTransformer()); } } Manifest: Premain-Class: com.example.qcon.agent.Agent Wednesday, June 11, 14
  38. 38. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  39. 39. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  40. 40. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { System.out.println(" Loading class: " + className); return null; } } Wednesday, June 11, 14
  41. 41. lets code Starting the agent Loading class: java/lang/invoke/MethodHandleImpl Loading class: java/lang/invoke/MemberName$Factory Loading class: java/lang/invoke/LambdaForm$NamedFunction Loading class: java/lang/invoke/MethodType$ConcurrentWeakInternSet Loading class: java/lang/invoke/MethodHandleStatics Loading class: java/lang/invoke/MethodHandleStatics$1 Loading class: java/lang/invoke/MethodTypeForm Loading class: java/lang/invoke/Invokers Loading class: java/lang/invoke/MethodType$ConcurrentWeakInternSet$WeakEntry Loading class: java/lang/Void Loading class: java/lang/IllegalAccessException Loading class: sun/misc/PostVMInitHook Loading class: sun/launcher/LauncherHelper Loading class: java/util/concurrent/ConcurrentHashMap$ForwardingNode Loading class: sun/misc/URLClassPath$FileLoader$1 Loading class: com/example/qcon/mains/BankTransactions Loading class: sun/launcher/LauncherHelper$FXHelper Wednesday, June 11, 14
  42. 42. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { System.out.println(" Loading class: " + className); return null; } } Wednesday, June 11, 14
  43. 43. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { // manipulate the bytes here and then return them return null; } } Wednesday, June 11, 14
  44. 44. Bytecode Manipulation Frameworks • ASM: http://asm.ow2.org/ • BCEL: http://commons.apache.org/proper/commons-bcel/ • CGLib: https://github.com/cglib/cglib • Javassist: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ • Serp: http://serp.sourceforge.net/ • Cojen: https://github.com/cojen/Cojen/wiki • Soot: http://www.sable.mcgill.ca/soot/ Wednesday, June 11, 14
  45. 45. Bytecode Manipulation Frameworks • ASM: http://asm.ow2.org/ • BCEL: http://commons.apache.org/proper/commons-bcel/ • CGLib: https://github.com/cglib/cglib • Javassist: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ • Serp: http://serp.sourceforge.net/ • Cojen: https://github.com/cojen/Cojen/wiki • Soot: http://www.sable.mcgill.ca/soot/ Wednesday, June 11, 14
  46. 46. Javassist • Java Programming Assistant • Subproject of JBoss • Actively updated • Version 3.18.2 released May 2014 • Source code: https://github.com/jboss-javassist/ javassist • Documentation: http://www.csg.ci.i.u-tokyo.ac.jp/ ~chiba/javassist/tutorial/tutorial.html Wednesday, June 11, 14
  47. 47. Javassist ArrayList HashMap CtClass: represents a compile time class Wednesday, June 11, 14
  48. 48. Javassist ClassPool: container of CtClass objects ArrayList HashMap CtClass: represents a compile time class Wednesday, June 11, 14
  49. 49. Javassist ClassPool: container of CtClass objects ArrayList HashMap ArrayList HashMap CtClass: represents a compile time class Wednesday, June 11, 14
  50. 50. Javassist ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap CtClass: represents a compile time class Wednesday, June 11, 14
  51. 51. Javassist CtClass: represents a compile time class ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap ByteArrayClassPath: converts a byte[] to a CtClass Wednesday, June 11, 14
  52. 52. Javassist ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap Classpool.getDefault().insertClassPath(new ByteArrayClassPath(“BankTrans”, classBytes); ByteArrayClassPath: converts a byte[] to a CtClass CtClass: represents a compile time class Wednesday, June 11, 14
  53. 53. Javassist ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap BankTrans Classpool.getDefault().insertClassPath(new ByteArrayClassPath(“BankTrans”, classBytes); ByteArrayClassPath: converts a byte[] to a CtClass CtClass: represents a compile time class Wednesday, June 11, 14
  54. 54. Javassist ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap BankTrans Classpool.getDefault().insertClassPath(new ByteArrayClassPath(“BankTrans”, classBytes); ByteArrayClassPath: converts a byte[] to a CtClass CtClass: represents a compile time class CtClass bank = Classpool.getDefault().get(“BankTrans”); Wednesday, June 11, 14
  55. 55. Javassist ClassPool: container of CtClass objects ArrayList HashMap BankTrans ArrayList HashMap BankTrans ByteArrayClassPath: converts a byte[] to a CtClass CtClass: represents a compile time class BankTrans Classpool.getDefault().insertClassPath(new ByteArrayClassPath(“BankTrans”, classBytes); CtClass bank = Classpool.getDefault().get(“BankTrans”); Wednesday, June 11, 14
  56. 56. Javassist BankTrans CtClass: represents a compile time class Wednesday, June 11, 14
  57. 57. Javassist CtConstructor: represents a compile time constructor init BankTrans CtClass: represents a compile time class Wednesday, June 11, 14
  58. 58. Javassist CtConstructor: represents a compile time constructor init BankTrans CtClass: represents a compile time class CtMethod: represents a compile time class login process Wednesday, June 11, 14
  59. 59. Javassist CtConstructor: represents a compile time constructor init CtClass: represents a compile time class CtMethod: represents a compile time method CtMethod loginMethod = loginCtClass.getDeclaredMethod(“login”); BankTrans login process Wednesday, June 11, 14
  60. 60. Javassist CtConstructor: represents a compile time constructor init CtClass: represents a compile time class CtMethod: represents a compile time method CtMethod loginMethod = loginCtClass.getDeclaredMethod(“login”); login BankTrans login process Wednesday, June 11, 14
  61. 61. Javassist login CtMethod: represents a compile time method Wednesday, June 11, 14
  62. 62. Javassist Modifications: login CtMethod: represents a compile time method Wednesday, June 11, 14
  63. 63. Javassist Modifications: login CtMethod: represents a compile time method putMethod.insertAfter(“System.out.println(”after method”);”); putMethod.insertBefore(“System.out.println(“before method”);”); Wednesday, June 11, 14
  64. 64. Javassist Modifications: System.out.println(“before method”); CtMethod: represents a compile time method putMethod.insertAfter(“System.out.println(”after method”);”); putMethod.insertBefore(“System.out.println(“before method”);”); System.out.println(“after method”); login Wednesday, June 11, 14
  65. 65. Javassist Modifications: CtMethod: represents a compile time method CtClass exceptionType = classpool.get(“java.io.IOException”); putMethod.addCatch(“{System.out.println($e); throw $e}”, exceptionType); login Wednesday, June 11, 14
  66. 66. Javassist Modifications: CtMethod: represents a compile time method CtClass exceptionType = classpool.get(“java.io.IOException”); System.out.println(“exception”); throw exception; putMethod.addCatch(“{System.out.println($e); throw $e}”, exceptionType); login Wednesday, June 11, 14
  67. 67. Javassist Modifications: CtMethod: represents a compile time method putMethod.insertAt(5, true, “System.out.println(“hello”);”); login Wednesday, June 11, 14
  68. 68. Javassist Modifications: CtMethod: represents a compile time method putMethod.insertAt(5, true, “System.out.println(“hello”);”); System.out.println(“hello”); login Wednesday, June 11, 14
  69. 69. Javassist Modifications: CtMethod: represents a compile time method insertBefore insertAfter insertAt addCatch System.out.println(“before method”); System.out.println(“after method”); System.out.println(“hello”); System.out.println(“exception”); throw exception; login Wednesday, June 11, 14
  70. 70. Javassist Modifications: CtMethod: represents a compile time method insertBefore insertAfter insertAt addCatch Freeze Class: System.out.println(“before method”); System.out.println(“after method”); System.out.println(“hello”); System.out.println(“exception”); throw exception; login Wednesday, June 11, 14
  71. 71. Javassist Modifications: CtMethod: represents a compile time method insertBefore insertAfter insertAt addCatch Freeze Class: System.out.println(“before method”); System.out.println(“after method”); System.out.println(“hello”); System.out.println(“exception”); throw exception; writeFile() toClass() toBytecode() login Wednesday, June 11, 14
  72. 72. lets code Wednesday, June 11, 14
  73. 73. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  74. 74. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  75. 75. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { 1. convert byte array to a ct class object 2. check each method of ct class for annotation @ImportantLog 3. if @ImportantLog annotation present on method, then a. get important method parameter indexes b. add logging statement to beginning of the method return null; } Wednesday, June 11, 14
  76. 76. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); 2. check each method of ct class for annotation @ImportantLog 3. if @ImportantLog annotation present on method, then a. get important method parameter indexes b. add logging statement to beginning of the method return null; } Wednesday, June 11, 14
  77. 77. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); 3. if @ImportantLog annotation present on method, then a. get important method parameter indexes b. add logging statement to beginning of the method } } return null; } Wednesday, June 11, 14
  78. 78. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); b. add logging statement to beginning of the method } } } return null; } Wednesday, June 11, 14
  79. 79. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); currentMethod.insertBefore(createJavaString( currentMethod, className, parameterIndexes)); } } return cclass.toBytecode(); } return null; } Wednesday, June 11, 14
  80. 80. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); currentMethod.insertBefore(createJavaString( currentMethod, className, parameterIndexes)); } } return cclass.toBytecode(); } return null; } Wednesday, June 11, 14
  81. 81. lets code private Annotation getAnnotation(CtMethod method) { MethodInfo mInfo = method.getMethodInfo(); // the attribute we are looking for is a runtime invisible attribute // use Retention(RetentionPolicy.RUNTIME) on the annotation to make it // visible at runtime AnnotationsAttribute attInfo = (AnnotationsAttribute) mInfo .getAttribute(AnnotationsAttribute.invisibleTag); if (attInfo != null) { // this is the type name meaning use dots instead of slashes return attInfo.getAnnotation("com.example.qcon.mains.ImportantLog"); } return null; } Wednesday, June 11, 14
  82. 82. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); currentMethod.insertBefore(createJavaString( currentMethod, className, parameterIndexes)); } } return cclass.toBytecode(); } return null; } Wednesday, June 11, 14
  83. 83. lets code private List<String> getParamIndexes(Annotation annotation) { ArrayMemberValue fields = (ArrayMemberValue) annotation .getMemberValue(“fields”); if (fields != null) { MemberValue[] values = (MemberValue[]) fields.getValue(); List<String> parameterIndexes = new ArrayList<String>(); for (MemberValue val : values) { parameterIndexes.add(((StringMemberValue) val).getValue()); } return parameterIndexes; } return Collections.emptyList(); } Wednesday, June 11, 14
  84. 84. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); currentMethod.insertBefore(createJavaString( currentMethod, className, parameterIndexes)); } } return cclass.toBytecode(); } return null; } Wednesday, June 11, 14
  85. 85. lets code private String createJavaString(CtMethod currentMethod, String className, List<String> indexParameters) { StringBuilder sb = new StringBuilder(); sb.append("{StringBuilder sb = new StringBuilder"); sb.append("("A call was made to method '");"); sb.append("sb.append(""); sb.append(currentMethod.getName()); sb.append("");sb.append("' on class '");"); sb.append("sb.append(""); sb.append(className); sb.append("");sb.append("'.");"); sb.append("sb.append("n Important params:");"); . . . Wednesday, June 11, 14
  86. 86. lets code private String createJavaString(CtMethod currentMethod, String className, List<String> indexParameters) { StringBuilder sb = new StringBuilder(); sb.append("{StringBuilder sb = new StringBuilder"); sb.append("("A call was made to method '");"); sb.append("sb.append(""); sb.append(currentMethod.getName()); sb.append("");sb.append("' on class '");"); sb.append("sb.append(""); sb.append(className); sb.append("");sb.append("'.");"); sb.append("sb.append("n Important params:");"); . . . Put multiple statements inside brackets {} Wednesday, June 11, 14
  87. 87. lets code private String createJavaString(CtMethod currentMethod, String className, List<String> indexParameters) { . . . for (String index : indexParameters) { try { int localVar = Integer.parseInt(index) + 1; sb.append("sb.append("n Index: ");"); sb.append("sb.append(""); sb.append(index); sb.append("");sb.append(" value: ");"); sb.append("sb.append($" + localVar + ");"); } catch (NumberFormatException e) { e.printStackTrace(); } } sb.append("System.out.println(sb.toString());}"); return sb.toString(); } Wednesday, June 11, 14
  88. 88. lets code private String createJavaString(CtMethod currentMethod, String className, List<String> indexParameters) { . . . for (String index : indexParameters) { try { int localVar = Integer.parseInt(index) + 1; sb.append("sb.append("n Index: ");"); sb.append("sb.append(""); sb.append(index); sb.append("");sb.append(" value: ");"); sb.append("sb.append($" + localVar + ");"); } catch (NumberFormatException e) { e.printStackTrace(); } } sb.append("System.out.println(sb.toString());}"); return sb.toString(); } $0, $1, $2, ... can be used to access “this” and the actual method parameters Wednesday, June 11, 14
  89. 89. lets code public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] cfbuffer) throws IllegalClassFormatException { pool.insertClassPath(new ByteArrayClassPath(className, classfileBuffer)); CtClass cclass = pool.get(className.replaceAll("/", ".")); if (!cclass.isFrozen()) { for (CtMethod currentMethod : cclass.getDeclaredMethods()) { Annotation annotation = getAnnotation(currentMethod); if (annotation != null) { List<String> parameterIndexes = getParamIndexes(annotation); currentMethod.insertBefore(createJavaString( currentMethod, className, parameterIndexes)); } } return cclass.toBytecode(); } return null; } Wednesday, June 11, 14
  90. 90. Javassist • Positives • Simplicity • Do not have to write actual bytecode • Decent documentation • Negatives • Generally slower than ASM • Less functionality Wednesday, June 11, 14
  91. 91. ASM • Released in Open Source in 2002 • Actively updated • Version 5.0.3 released May 2014 • Two ASM libraries • Event based (SAX like) • Visitor design pattern • Object based (DOM like) • Documentation: http://download.forge.objectweb.org/ asm/asm4-guide.pdf Wednesday, June 11, 14
  92. 92. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  93. 93. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  94. 94. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  95. 95. ASM ClassReader: given a byte[], parses a compiled class ClassReader: event producer BankTrans ClassWriter: event consumer ClassVisitor: event filter Wednesday, June 11, 14
  96. 96. ASM ClassReader: given a byte[], parses a compiled class ClassReader: event producer BankTrans ClassWriter: event consumer visitField visitMethod ClassVisitor: event filter Wednesday, June 11, 14
  97. 97. ASM ClassVisitor: delegates class events, event filter visitAttribute visitField visitMethod visitInnerClass visitEnd ClassReader: given a byte[], parses a compiled class ClassReader: event producer BankTrans ClassWriter: event consumer visitField visitMethod ClassVisitor: event filter Wednesday, June 11, 14
  98. 98. ASM ClassVisitor: delegates class events, event filter visitAttribute visitField visitMethod visitInnerClass visitEnd ClassWriter: produces output byte[] ClassReader: given a byte[], parses a compiled class ClassReader: event producer BankTrans ClassWriter: event consumer visitField visitMethod BankTrans ClassVisitor: event filter Wednesday, June 11, 14
  99. 99. lets code package com.example.qcon.agent; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String agentArgs, Instrumentation inst) { System.out.println("Starting the agent"); inst.addTransformer(new ImportantLogClassTransformer()); } } Wednesday, June 11, 14
  100. 100. lets code package com.example.qcon.agent; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String agentArgs, Instrumentation inst) { System.out.println("Starting the agent"); inst.addTransformer(new ImportantLogClassTransformer()); } } Wednesday, June 11, 14
  101. 101. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  102. 102. lets code package com.example.qcon.agent; import java.lang.instrument.ClassFileTransformer; import java.lang.instrument.IllegalClassFormatException; public class ImportantLogClassTransformer implements ClassFileTransformer { public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { //TODO return null; } } Wednesday, June 11, 14
  103. 103. lets code public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { ClassReader cr = new ClassReader(classfileBuffer); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); cr.accept(cw, 0); return cw.toByteArray(); } Wednesday, June 11, 14
  104. 104. lets code public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException { ClassReader cr = new ClassReader(classfileBuffer); ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_FRAMES); ClassVisitor cv = new LogMethodClassVisitor(cw, className); cr.accept(cv, 0); return cw.toByteArray(); } Wednesday, June 11, 14
  105. 105. ASM ClassVisitor: delegates class events, event filter visitAttribute visitField visitMethod visitInnerClass visitEnd ClassWriter: produces output byte[] ClassReader: given a byte[], parses a compiled class ClassReader: event producer BankTrans ClassWriter: event consumer visitMethod BankTrans LogMethod ClassVisitor: event filter Wednesday, June 11, 14
  106. 106. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  107. 107. lets code public class LogMethodClassVisitor extends ClassVisitor { private String className; public LogMethodIfAnnotationVisitor(ClassVisitor cv, String pClassName) { super(Opcodes.ASM5, cv); className = pClassName; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { // put our logic in here } } Wednesday, June 11, 14
  108. 108. lets code public class LogMethodClassVisitor extends ClassVisitor { private String className; public LogMethodIfAnnotationVisitor(ClassVisitor cv, String pClassName) { super(Opcodes.ASM5, cv); className = pClassName; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { // put our logic in here } } Wednesday, June 11, 14
  109. 109. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  110. 110. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  111. 111. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  112. 112. ASM MethodVisitor: method event filter MethodVisitor MethodVisitor visitCode login method visitor login method visitor visitAnnotation visitEnd visitCode visitTryCatchBlock visitEnd visitAttribute Wednesday, June 11, 14
  113. 113. ASM Source: http://download.forge.objectweb.org/asm/asm4-guide.pdf Wednesday, June 11, 14
  114. 114. lets code public class LogMethodClassVisitor extends ClassVisitor { private String className; public LogMethodIfAnnotationVisitor(ClassVisitor cv, String pClassName) { super(Opcodes.ASM5, cv); className = pClassName; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { MethodVisitor mv = super.visitMethod(access, name, desc, signature, exceptions); return new PrintMessageMethodVisitor(mv, name, className); } } Wednesday, June 11, 14
  115. 115. lets code public class PrintMessageMethodVisitor extends MethodVisitor { private String methodName; private String className; private boolean isAnnotationPresent; private List<String> parameterIndexes; public PrintMessageMethodVisitor(MethodVisitor mv, String methodName, String className) { } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { } @Override public void visitCode() { } } Wednesday, June 11, 14
  116. 116. lets code public class PrintMessageMethodVisitor extends MethodVisitor { private String methodName; private String className; private boolean isAnnotationPresent; private List<String> parameterIndexes; public PrintMessageMethodVisitor(MethodVisitor mv, String methodName, String className) { // initialize instance variables } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 1. check method for annotation @ImportantLog 2. if annotation present, then get important method param indexes } @Override public void visitCode() { 3. if annotation present, add logging to beginning of the method } } Wednesday, June 11, 14
  117. 117. lets code public AnnotationVisitor visitAnnotation(String desc, boolean visible) { if ("Lcom/example/qcon/mains/ImportantLog;".equals(desc)) { isAnnotationPresent = true; return new AnnotationVisitor(Opcodes.ASM5, super.visitAnnotation(desc, visible)) { public AnnotationVisitor visitArray(String name) { if (“fields”.equals(name)) { return new AnnotationVisitor(Opcodes.ASM5, super.visitArray(name)) { public void visit(String name, Object value) { parameterIndexes.add((String) value); super.visit(name, value); } }; } else { return super.visitArray(name); } } }; } return super.visitAnnotation(desc, visible); } Wednesday, June 11, 14
  118. 118. lets code public class PrintMessageMethodVisitor extends MethodVisitor { private String methodName; private String className; private boolean isAnnotationPresent; private List<String> parameterIndexes; public PrintMessageMethodVisitor(MethodVisitor mv, String methodName, String className) { // initialize instance variables } @Override public AnnotationVisitor visitAnnotation(String desc, boolean visible) { 1. check method for annotation @ImportantLog 2. if annotation present, then get important method param indexes } @Override public void visitCode() { 3. if annotation present, add logging statement to beginning of the method } } Wednesday, June 11, 14
  119. 119. lets code public void visitCode() { if (isAnnotationPresent) { // create string builder mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); mv.visitInsn(Opcodes.DUP); // add everything to the string builder mv.visitLdcInsn("A call was made to method ""); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitLdcInsn(methodName); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); . . . Wednesday, June 11, 14
  120. 120. What is Java bytecode? use javap to examine the bytecode Wednesday, June 11, 14
  121. 121. What is Java bytecode? public class PrintMessage { private String methodName; private String className; public PrintMessage(String mName, String cName) { methodName = mName; className = cName; } public void print() { StringBuilder sb = new StringBuilder(); sb.append("A call was made to method ""); sb.append(methodName); sb.append("" on class ""); sb.append(className); sb.append(""."); System.out.println(sb.toString()); } Wednesday, June 11, 14
  122. 122. What is Java bytecode? javap -c bin/com/example/qcon/mains/PrintMessage Wednesday, June 11, 14
  123. 123. What is Java bytecode? javap -c bin/com/example/qcon/mains/PrintMessage public void print(); Code: 0: new #38 // class java/lang/StringBuilder 3: dup 4: invokespecial #40 // Method java/lang/ StringBuilder."<init>":()V 7: astore_1 8: aload_1 9: ldc #41 // String A call was made to method " 11: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: pop 15: aload_1 16: aload_0 17: getfield #14 // Field methodName:Ljava/lang/String; 20: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; . . . Wednesday, June 11, 14
  124. 124. What is Java bytecode? each line represents one JVM instruction public void print(); Code: 0: new #38 // class java/lang/StringBuilder 3: dup 4: invokespecial #40 // Method java/lang/ StringBuilder."<init>":()V 7: astore_1 8: aload_1 9: ldc #41 // String A call was made to method " 11: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: pop 15: aload_1 16: aload_0 17: getfield #14 // Field methodName:Ljava/lang/String; 20: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; . . . Wednesday, June 11, 14
  125. 125. What is Java bytecode? JVM instruction = 1 opcode with 0 or more operands public void print(); Code: 0: new #38 // class java/lang/StringBuilder 3: dup 4: invokespecial #40 // Method java/lang/ StringBuilder."<init>":()V 7: astore_1 8: aload_1 9: ldc #41 // String A call was made to method " 11: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 14: pop 15: aload_1 16: aload_0 17: getfield #14 // Field methodName:Ljava/lang/String; 20: invokevirtual #43 // Method java/lang/ StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; . . . Wednesday, June 11, 14
  126. 126. What is Java bytecode? Opcode Description load load a reference onto the stack from a local variable iload, lload, fload, dload, aload store stores a value from the stack into a local variable istore, lstore, fstore, dstore, astore lcd push a constant from a constant pool onto the stack return method return instruction ireturn, lreturn, freturn, dreturn, areturn, return invokevirtual invoke an instance method on an object invokespecial invokes an instance method requiring special handling such as an initialization method See the spec for more info: http://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf Wednesday, June 11, 14
  127. 127. What is Java bytecode? Opcode Description invokestatic invoke a class (static) method on a named class athrow throws an exception new create a new class newarray create a new array if<cond> branch if int comparison with 0 succeeds ifeq, ifne, iflt, ifge, ifgt, ifle dup duplicate the top operand stack value See the spec for more info: http://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf Wednesday, June 11, 14
  128. 128. What is Java bytecode? Wednesday, June 11, 14
  129. 129. What is Java bytecode? Wednesday, June 11, 14
  130. 130. What is Java bytecode? Wednesday, June 11, 14
  131. 131. lets code public void visitCode() { if (isAnnotationPresent) { // create string builder mv.visitFieldInsn(Opcodes.GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); mv.visitTypeInsn(Opcodes.NEW, "java/lang/StringBuilder"); mv.visitInsn(Opcodes.DUP); // add everything to the string builder mv.visitLdcInsn("A call was made to method ""); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/StringBuilder", "<init>", "(Ljava/lang/String;)V", false); mv.visitLdcInsn(methodName); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/StringBuilder", "append", "(Ljava/lang/String;)Ljava/lang/StringBuilder;", false); . . . Wednesday, June 11, 14
  132. 132. ASM • Positives • Speed and Size • Decent documentation • Depth of Functionality • Number of people using framework • Negatives • Need to write actual byte code • longer developer ramp-up Wednesday, June 11, 14
  133. 133. Why learn about Java bytecode manipulation frameworks ? • program analysis • find bugs in code • examine code complexity • generate classes • proxies • remove access to certain APIs • compiler for another language like Scala • transform classes without Java source code • profilers • optimization and obfuscation • additional logging Wednesday, June 11, 14
  134. 134. Bytecode Manipulation Frameworks • ASM: http://asm.ow2.org/ • BCEL: http://commons.apache.org/proper/commons-bcel/ • CGLib: https://github.com/cglib/cglib • Javassist: http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/ • Serp: http://serp.sourceforge.net/ • Cojen: https://github.com/cojen/Cojen/wiki • Soot: http://www.sable.mcgill.ca/soot/ Wednesday, June 11, 14
  135. 135. Source: http://www.kulfoto.com/dog-pictures/15799/mans-best-friend Make bytecode manipulation frameworks your friend. They might save your job one day. Wednesday, June 11, 14
  136. 136. Wednesday, June 11, 14
  137. 137. Source: http://alangregerman.typepad.com/.a/6a00d83516c0ad53ef017c3329aba8970b-800wi Questions? Wednesday, June 11, 14
  138. 138. Watch the video with slide synchronization on InfoQ.com! http://www.infoq.com/presentations/asm-cglib- javassist

×