[SOLVED] html Java UML assembly compiler Introduction to Apache BCEL

$25

File Name: html_Java_UML_assembly_compiler_Introduction_to_Apache_BCEL.zip
File Size: 555.78 KB

5/5 - (1 vote)

Introduction to Apache BCEL
UCL
COMP0012 [email protected]

Javas Execution Model
UCL

Programs written in Java are compiled into a portable binary format called byte code.
Every Java class is represented in a single class file containing class related data and byte code instructions. These files are loaded dynamically into Java Virtual Machine (a.k.a. JVM) and executed.
COMP0012 [email protected]

JVM Architecture
UCL
COMP0012 [email protected]

JVM Memory Model
UCL

Operates on primitive values and references (i.e., pointers to objects).
Has a garbage-collected heap for storing objects and arrays.
Creates a stack frame for each method call, and destroys the frame when that method exits.
Each frame provides an operand stack and an array of local variables.
COMP0012 [email protected]

Byte Code Instruction Set
UCL

An instruction consists of a byte-long opcode specifying the operation to be performed, followed by zero or more operands.
Of the 256 possible opcodes, as of 2015, 202 are in use, 51 are reserved for future use, and 3 are permanently reserved for JVM.

COMP0012 [email protected]

Instruction Groups
Load and store (e.g. aload_0, istore)
Arithmetic and logic (e.g. ladd, fcmpl)
Type conversion (e.g. i2b, d2i)
Object creation and manipulation (new, putfield) Operand stack management (e.g. swap, dup2) Control transfer (e.g. ifeq, goto)
Method invocation and return (e.g. invokespecial, areturn) COMP0012 [email protected]
UCL

Byte Code Manipulation Libraries
UCL
Apache Commons BCEL: https://
commons.apache.org/proper/commons-bcel/ ObjectWeb Consortiums ASM: http://asm.ow2.org/ Javassist: http://www.javassist.org/

COMP0012 [email protected]

Byte Code Manipulation Libraries
UCL

Apache Commons BCEL: https://
commons.apache.org/proper/commons-bcel/
ObjectWeb Consortiums ASM: http://asm.ow2.org/
Javassist: http://www.javassist.org/
COMP0012 [email protected]

Byte Code Engineering Library UCL
Represents a given class in an object that contains all the symbolic information: methods, fields and byte code instructions.
Enables various activities without having access to the source code: bug finding, dead code elimination, obfuscation, etc.

COMP0012 [email protected]

The BCEL API
UCL

Abstracts the JVM and the interactions with Java class files Mainly consists of three parts:
A package containing classes that reflect the class file format;
A package to dynamically generate or modify JavaClass or Method objects;
Various code examples and utilities like a class file viewer, a tool to convert class files into HTML, and a converter from class files to the Jasmin assembly language.
COMP0012 [email protected]

The classfile Package
package org.apache.bcel.classfile
UCL
COMP0012 UML diagram for the JavaClass API [email protected]

JavaClass
JavaClass: represents a Java byte code class
Of its various parts, we are mostly interested in:
ConstantPool: represents the collection of constants
Method: represents a method (a list of byte code instructions)
UCL

JavaClass is parsed from .class file by a ClassParser COMP0012 [email protected]

The generic Package
package org.apache.bcel.generic
UCL
COMP0012 UML diagram of the ClassGen API [email protected]

ClassGen
UCL

ClassGen: generates a Java class from parts, which include ConstantPool and Methods
ConstantPoolGen: generates the constant pool
MethodGen: generates Java methods Eventually, ClassGen outputs byte[], which is
our .class file
COMP0012 [email protected]

Example 1: CompilerString
Source code available on Moodle
UCL

Changes all String constants into Compiler
COMP0012 [email protected]

Example 1: CompilerString
Source code available on Moodle
UCL
// load the original class into a class generator
ClassGen cgen = new ClassGen(original);
ConstantPoolGen cpgen = cgen.getConstantPool();
// get the current constant pool
ConstantPool cp = cpgen.getConstantPool();
// get the constants in the pool
Constant[] constants = cp.getConstantPool();

Changes all String constants into Compiler
COMP0012 [email protected]

Example 1: CompilerString
Source code available on Moodle
UCL

Changes all String constants into Compiler
for (int i = 0; i < constants.length; i++){[email protected] // string constants take two entries in the pool // the first one is of ConstantString, which contains // an index to the second entry, which is ConstantUtf8 // (displayed Asciz when disassembled by javap) // // ConstantUtf8 (Asciz) entries are used to store method names, etc // whereas we are only interested in String constants // So we first look for ConstantString entry, // then retrieve the index of ConstantUtf8 entry, which we then replace if (constants[i] instanceof ConstantString) {ConstantString cs = (ConstantString) constants[i];cp.setConstant(cs.getStringIndex(), new ConstantUtf8(“Compiler”)); }} Example 2: FiveSource code available on MoodleUCLChanges any integer constants pushed to the stack to 5// load the original class into a class generatorClassGen cgen = new ClassGen(original);ConstantPoolGen cpgen = cgen.getConstantPool();// Do your optimization hereMethod[] methods = cgen.getMethods();for (Method m : methods){optimizeMethod(cgen, cpgen, m); }[email protected] Example 2: FiveSource code available on MoodleUCLChanges any integer constants pushed to the stack to 5 InstructionList instList = new InstructionList(methodCode.getCode()); // InstructionHandle is a wrapper for actual Instructions for (InstructionHandle handle : instList.getInstructionHandles()) {// if the instruction inside is iconstif (handle.getInstruction() instanceof ICONST){// insert new one with integer 5, and…instList.insert(handle, new ICONST(5));try{ // delete the old one instList.delete(handle);}catch (TargetLostException e){ // TODO Auto-generated catch block e.printStackTrace();}COMP0012 }[email protected]} ResourcesUCLManual: https://commons.apache.org/proper/commons-bcel/manual/manual.htmlAPI doc: http://commons.apache.org/proper/ commons-bcel/apidocs/index.htmlCOMP0012 [email protected]

Reviews

There are no reviews yet.

Only logged in customers who have purchased this product may leave a review.

Shopping Cart
[SOLVED] html Java UML assembly compiler Introduction to Apache BCEL
$25