Skip to content

Library for building CFG for Java bytecode

License

Notifications You must be signed in to change notification settings

DespairedController/kfg

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

KFG

Library for building control flow graph from Java bytecode.

Build

mvn clean package

Run integration tests

mvn clean verify

Download

The latest release of the KFG is available through Vorpal GitHub Packages repository. Add the link to the repository to your pom.xml:

<repository>
    <id>github-vorpal-research-kotlin-maven</id>
    <url>https://maven.vorpal-research.science</url>
</repository>

Include:

<dependency>
	<groupId>org.jetbrains.research</groupId>
	<artifactId>kfg</artifactId>
	<version>${kfg.version}</version>
</dependency>

Usage example

Simple example of how to scan Jar file

/**
 * @path -- path to the jar file to analyze
 * @package -- package to scan in the jar
 */
fun example(path: Path, `package`: Package) {
    // create Jar file instance
    val jar = Jar(path, `package`)
    val target = Paths.get("updated")
    // create ClassManager and initialize it with the jar
    val cm = ClassManager(KfgConfig(Flags.readAll, failOnError = true))
    cm.initialize(jar)
    // iterate over all found classes
    for (klass in cm.concreteClasses) {
        for (method in klass.allMethods) {
            // view each method as graph
            method.view("/usr/bin/dot", "/usr/bin/browser")
        }
    }
    // create copy of the original jar with the updated 
    // classes in directory `target`
    jar.update(cm, target)
}

Pipeline example

class MethodPrinter(override val cm: ClassManager) : MethodVisitor {
    /**
     * should override this method and cleanup all the temporary info between visitor invocations
     */
    override fun cleanup() {}

    override fun visit(method: Method) {
        println("$method")
        super.visit(method)
    }

    override fun visitBasicBlock(bb: BasicBlock) {
        println("${bb.name}:")
        super.visitBasicBlock(bb)
    }

    override fun visitInstruction(inst: Instruction) {
        println("  $inst")
        super.visitInstruction(inst)
    }
}

fun pipelineExample(cm: ClassManager, `package`: Package) {
    executePipeline(cm, `package`) {
        +MethodPrinter(cm)
        +LoopAnalysis(cm)
        +LoopSimplifier(cm)
        +MethodPrinter(cm)
    }
}

About

Library for building CFG for Java bytecode

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Languages

  • Kotlin 99.9%
  • Java 0.1%