-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from seeker89/jvm
Chapter 8: JVM
- Loading branch information
Showing
13 changed files
with
225 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
CFLAGS=-O0 | ||
|
||
all: FizzBuzzEnterpriseEdition/bin/FizzBuzzEnterpriseEdition byteman-download-4.0.11 byte-monkey.jar | ||
|
||
src: | ||
git clone https://github.com/EnterpriseQualityCoding/FizzBuzzEnterpriseEdition.git src | ||
|
||
byteman-download-4.0.11: | ||
wget https://downloads.jboss.org/byteman/4.0.11/byteman-download-4.0.11-bin.zip | ||
unzip byteman-download-4.0.11-bin.zip | ||
rm byteman-download-4.0.11-bin.zip | ||
|
||
byte-monkey.jar: | ||
wget https://github.com/mrwilson/byte-monkey/releases/download/1.0.0/byte-monkey.jar | ||
|
||
FizzBuzzEnterpriseEdition/bin/FizzBuzzEnterpriseEdition: src | ||
(cd src && ./gradlew assemble && ./gradlew build) | ||
unzip src/build/distributions/FizzBuzzEnterpriseEdition.zip | ||
|
||
run: | ||
./FizzBuzzEnterpriseEdition/bin/FizzBuzzEnterpriseEdition | ||
|
||
run2: | ||
java -classpath "./FizzBuzzEnterpriseEdition/lib/*" com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
run3: | ||
javap -classpath "./FizzBuzzEnterpriseEdition/lib/*" -c com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
run4: | ||
java -javaagent:./agent1.jar -classpath "./FizzBuzzEnterpriseEdition/lib/*" com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
run5: | ||
java -javaagent:./agent2.jar -classpath "./FizzBuzzEnterpriseEdition/lib/*" com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
run6: | ||
java \ | ||
-javaagent:./byteman-download-4.0.11/lib/byteman.jar=script:throw.btm \ | ||
-classpath "./FizzBuzzEnterpriseEdition/lib/*" \ | ||
com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
run7: | ||
java \ | ||
-javaagent:byte-monkey.jar=mode:fault,rate:0.5,filter:com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/strategies/SystemOutFizzBuzzOutputStrategy/output \ | ||
-classpath "./FizzBuzzEnterpriseEdition/lib/*" \ | ||
com.seriouscompany.business.java.fizzbuzz.packagenamingpackage.impl.Main | ||
|
||
|
||
|
||
example1-compile: | ||
javac ./org/my/example1.java | ||
example1-run: | ||
java org.my.Example1 | ||
example1-bytecode: | ||
javap -c org.my.Example1 | ||
example1-agent: | ||
java -javaagent:./agent1.jar org.my.Example1 | ||
|
||
example2-compile: | ||
javac ./org/my/example2.java | ||
example2-run: | ||
java org.my.Example2 | ||
example2-bytecode: | ||
javap -c org.my.Example2 | ||
|
||
.PHONY: run run2 run3 example1-compile example1-run example1-bytecode example2-compile example2-run example2-bytecode | ||
|
||
agent1.jar: org/agent/Agent.java org/agent/ClassPrinter.java org/agent/manifest.mf | ||
javac org/agent/Agent.java | ||
javac org/agent/ClassPrinter.java | ||
jar vcmf org/agent/manifest.mf agent1.jar org/agent | ||
|
||
agent2.jar: org/agent2/Agent.java org/agent2/ClassInjector.java org/agent2/manifest.mf | ||
javac -XDignore.symbol.file org/agent2/Agent.java | ||
javac -XDignore.symbol.file org/agent2/ClassInjector.java | ||
jar vcmf org/agent2/manifest.mf agent2.jar org/agent2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.agent; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
|
||
class Agent { | ||
public static void premain(String args, Instrumentation instrumentation){ | ||
ClassPrinter transformer = new ClassPrinter(); | ||
instrumentation.addTransformer(transformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package org.agent; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
|
||
|
||
class ClassPrinter implements ClassFileTransformer { | ||
public byte[] transform(ClassLoader loader, | ||
String className, | ||
Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, | ||
byte[] classfileBuffer) throws IllegalClassFormatException { | ||
System.out.println("Found class: " + className + " (" + classfileBuffer.length + " bytes)"); | ||
return classfileBuffer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Manifest-Version: 1.0 | ||
Premain-Class: org.agent.Agent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.agent2; | ||
|
||
import java.lang.instrument.Instrumentation; | ||
|
||
class Agent { | ||
public static void premain(String args, Instrumentation instrumentation){ | ||
ClassInjector transformer = new ClassInjector(); | ||
instrumentation.addTransformer(transformer); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.agent2; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import java.lang.instrument.ClassFileTransformer; | ||
import java.lang.instrument.IllegalClassFormatException; | ||
import java.security.ProtectionDomain; | ||
|
||
import jdk.internal.org.objectweb.asm.ClassReader; | ||
import jdk.internal.org.objectweb.asm.ClassWriter; | ||
import jdk.internal.org.objectweb.asm.tree.*; | ||
import jdk.internal.org.objectweb.asm.Opcodes; | ||
|
||
|
||
public class ClassInjector implements ClassFileTransformer { | ||
|
||
public String targetClassName = "com/seriouscompany/business/java/fizzbuzz/packagenamingpackage/impl/strategies/SystemOutFizzBuzzOutputStrategy"; | ||
|
||
public byte[] transform(ClassLoader loader, | ||
String className, | ||
Class<?> classBeingRedefined, | ||
ProtectionDomain protectionDomain, | ||
byte[] classfileBuffer) throws IllegalClassFormatException { | ||
if (className.equals(this.targetClassName)){ | ||
System.err.println("[CHAOS] TARGET ACQUIRED: " + className + " (" + classfileBuffer.length + " bytes)"); | ||
|
||
ClassNode classNode = new ClassNode(); | ||
new ClassReader(classfileBuffer).accept(classNode, 0); | ||
classNode.methods.stream() | ||
.filter(method -> method.name.equals("output")) | ||
.forEach(method -> { | ||
InsnList instructions = new InsnList(); | ||
instructions.add(new MethodInsnNode( | ||
Opcodes.INVOKESTATIC, | ||
"org/agent2/ClassInjector", | ||
"throwIOException", | ||
"()V", | ||
false // not a method | ||
)); | ||
method.maxStack += 1; | ||
method.instructions.insertBefore(method.instructions.getFirst(), instructions); | ||
System.err.println("[CHAOS] Method " + method.name + " modified"); | ||
}); | ||
final ClassWriter classWriter = new ClassWriter(0); | ||
classNode.accept(classWriter); | ||
byte[] bytes = classWriter.toByteArray(); | ||
System.err.println("[CHAOS] Rewrote: " + className + " (" + bytes.length + " bytes)"); | ||
return bytes; | ||
} | ||
return classfileBuffer; | ||
} | ||
|
||
public static void throwIOException() throws IOException | ||
{ | ||
System.err.println("[CHAOS] BOOM! Throwing"); | ||
throw new IOException("CHAOS"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Manifest-Version: 1.0 | ||
Premain-Class: org.agent2.Agent | ||
Can-Redefine-Classes: true | ||
Can-Retransform-Classes: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package org.my; | ||
|
||
class Example1 | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
System.out.println("Hello chaos!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package org.my; | ||
import java.io.IOException; | ||
|
||
class Example2 | ||
{ | ||
public static void main(String[] args) throws IOException | ||
{ | ||
Example2.throwIOException(); | ||
} | ||
|
||
public static void throwIOException() throws IOException | ||
{ | ||
throw new IOException("Oops"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
RULE throw an exception at output | ||
CLASS SystemOutFizzBuzzOutputStrategy | ||
METHOD output | ||
AT ENTRY | ||
IF true | ||
DO | ||
traceln("entering the method output"); | ||
throw new java.io.IOException("BOOM"); | ||
ENDRULE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters