Skip to content

Commit

Permalink
disable instrumentation verification while instrumenting old jar files.
Browse files Browse the repository at this point in the history
  • Loading branch information
aoli-al committed May 6, 2024
1 parent 0b10c03 commit 17c6c19
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import java.security.ProtectionDomain
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassVisitor
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.util.CheckClassAdapter

Expand Down Expand Up @@ -41,10 +42,7 @@ class ApplicationCodeTransformer : ClassFileTransformer {
File("/tmp/out/origin/${className.replace("/", ".").removePrefix(".")}.class")
.writeBytes(classfileBuffer)
val classReader = ClassReader(classfileBuffer)
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
val checkClassAdapter = CheckClassAdapter(classWriter)
val cn = ClassNode()

try {
var cv: ClassVisitor = ObjectNotifyInstrumenter(cn)
cv = TargetExitInstrumenter(cv)
Expand All @@ -53,10 +51,16 @@ class ApplicationCodeTransformer : ClassFileTransformer {
cv = MonitorInstrumenter(cv)
cv = ConditionInstrumenter(cv)
cv = SynchronizedMethodInstrumenter(cv)
cv = ClassVersionInstrumenter(cv)
cv = ArrayOperationInstrumenter(cv)
val classVersionInstrumenter = ClassVersionInstrumenter(cv)
cv = ArrayOperationInstrumenter(classVersionInstrumenter)
classReader.accept(cv, ClassReader.EXPAND_FRAMES)
cn.accept(checkClassAdapter)
val classWriter = ClassWriter(classReader, ClassWriter.COMPUTE_MAXS)
if (classVersionInstrumenter.classVersion >= Opcodes.V1_5) {
val checkClassAdapter = CheckClassAdapter(classWriter)
cn.accept(checkClassAdapter)
} else {
cn.accept(classWriter)
}
val out = classWriter.toByteArray()
File("/tmp/out/app/${className.replace("/", ".").removePrefix(".")}.class").writeBytes(out)
return out
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ fun instrumentClass(path: String, inputStream: InputStream): ByteArray {
println(path)
throw e
}
File("/tmp/out/jdk/${path.replace("/", ".").removePrefix(".")}").writeBytes(byteArray)
return byteArray
}

fun instrumentModuleInfo(inputStream: InputStream, packages: List<String>): ByteArray {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import java.io.File
fun main(args: Array<String>) {
var ba = File(args[0]).inputStream()

instrumentClass(args[0], ba)
// val appTransformer = ApplicationCodeTransformer()
// appTransformer.transform(null, "", null, null, ba.readBytes())
// var t = ApplicationCodeTransformer()
// t.transform(null, "", null, null, ba)
// instrumentClass(args[0], ba)
val appTransformer = ApplicationCodeTransformer()
appTransformer.transform(null, "", null, null, ba.readBytes())
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.objectweb.asm.Opcodes
import org.objectweb.asm.Opcodes.ASM9

class ClassVersionInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
var classVersion = Opcodes.V21
override fun visit(
version: Int,
access: Int,
Expand All @@ -13,12 +14,12 @@ class ClassVersionInstrumenter(cv: ClassVisitor) : ClassVisitor(ASM9, cv) {
superName: String?,
interfaces: Array<out String>?
) {
val majorVersion = version and 0xFF
classVersion = version and 0xFF
val patchedVersion =
if (majorVersion < Opcodes.V1_5) {
if (classVersion < Opcodes.V1_5) {
Opcodes.V1_5
} else {
majorVersion
classVersion
} + (version shr 16 shl 16)
super.visit(patchedVersion, access, name, signature, superName, interfaces)
}
Expand Down

0 comments on commit 17c6c19

Please sign in to comment.