Java

Warning

This module is a work in progress.

Java is perhaps the most used programming language in the world.

PPCI offers some functions to deal with compiled Java bytecode (known as .class files) and archives of multiple .class files (.jar files).

Compile Java ahead of time

It’s possible to compile a subset of Java into machine code. Say, we have some Java code:

class Test14 {
    static int my_add(int a, int b) {
        return a + b + 1;
    }
}

We can compile this with javac, and next up, compile it with PPCI into msp430 code:

$ javac Test14.java
$ python -m ppci.cli.java compile Test14.class -m msp430

Load a class file dynamically

Given that you created a class file with a static function my_add in it (using javac), you could do the following:

>>> from ppci.arch.jvm import load_class
>>> klass = load_class('add.class')
>>> klass.my_add(1, 5)
7

This example is located in the file examples/java/load.py

Module reference

Java virtual machine (JVM).

This module supports loading and saving of java bytecode.

See also:

https://en.wikipedia.org/wiki/Java_bytecode_instruction_listings

ppci.arch.jvm.class_to_ir(class_file)

Translate java class file into IR-code.

ppci.arch.jvm.read_class_file(f, verbose=False)

Read a class file.

ppci.arch.jvm.read_jar(filename)

Take a stroll through a java jar file.

ppci.arch.jvm.load_class(filename)

Load a compiled class into memory.

ppci.arch.jvm.print_class_file(class_file)

Dump a class file.

Module to load class/jar files.

Another really good python java package: https://github.com/TkTech/Jawa http://jawa.tkte.ch/

class ppci.arch.jvm.io.DescriptorParser(text)

Descriptor string parser.

parse_method_descriptor()

Parse a method descriptor.

class ppci.arch.jvm.io.JavaFileReader(f, verbose=False)

Java class file reader.

read_attribute_info()

Read a single attribute.

read_attributes()

Read a series of attributes.

read_class_file()

Read a class file.

read_constant_pool()

Read the constant pool.

read_constant_pool_info()

Read a single tag from the constant pool.

read_field_info()

Read field info structure.

read_fields()

Read the fields of a class file.

read_flags()

Process flag field.

read_interfaces()

Read all interfaces from a class file.

read_method_info()

Read method info structure

read_methods()

Read the methods from a classfile.

class ppci.arch.jvm.io.JavaFileWriter

Enables writing of java class files.

ppci.arch.jvm.io.disassemble(bytecode)

Process a bytecode slab into instructions.

ppci.arch.jvm.io.read_class_file(f, verbose=False)

Read a class file.

ppci.arch.jvm.io.read_jar(filename)

Take a stroll through a java jar file.

ppci.arch.jvm.io.read_manifest(f)

Read a jarfile manifest.