annotating java bytecode

33
Annotating Java Bytecode By Patrice Pominville McGill University April 2000

Upload: mahdis

Post on 09-Jan-2016

48 views

Category:

Documents


4 download

DESCRIPTION

Annotating Java Bytecode. By Patrice Pominville McGill University April 2000. Overview of Presentation. Introduction Overview of what are classfile attributes Uses for custom classfile attributes and important issues Attribute support in Soot Tools developed to view Soot attributes - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Annotating Java Bytecode

Annotating Java Bytecode

By Patrice PominvilleMcGill University

April 2000

Page 2: Annotating Java Bytecode

Overview of Presentation Introduction Overview of what are classfile attributes Uses for custom classfile attributes and

important issues Attribute support in Soot Tools developed to view Soot attributes Related work Conclusion

Page 3: Annotating Java Bytecode

Introduction Bytecode is a much higher level code

representation than assembly. Many traditional compiler

optimizations cannot be expressed at the bytecode level.

Bytecodes are executed by JVMs. The information the JVMs can extract from bytecodes is inherently limited.

Page 4: Annotating Java Bytecode

We want a Mechanism… To bridge the information gap between

the compiler and the runtime. To take advantage of the interpreted,

dynamic nature of Java and it’s runtime.

To preserve Java portability, platform independence and safety.

To improve performance !

Page 5: Annotating Java Bytecode

A Solution: Classfile Attributes Classfiles can contain attributes. Attributes can be user-defined;

JVMs ignore unknown attributes. Attributes can be associated with a

class, a method, a field or bytecode.

Attributes can be generated by a compiler/profiler.

Page 6: Annotating Java Bytecode

Classfile Attributes Practically all classfiles contain

attributes. Certain attributes are predefined as

part of the classfile specification, others are user defined.

A classfile can contain an unlimited number of attributes.

Attributes can be of various length, size and shape.

Page 7: Annotating Java Bytecode

Attributes in Classfiles

Page 8: Annotating Java Bytecode

Attribute Format attribute_info {    

  u2 attribute_name_index;      u4 attribute_length;      u1 info[attribute_length];

}

2 byte index into the class’s Constant Pool

4 bytes to specify it’s length Attribute length bytes of actual data

Page 9: Annotating Java Bytecode

Possible Uses for Attributes Convey static compiler analysis

information to the Java runtime. Convey profile related data to the

Java runtime. Provide profile data to guide Soot

optimizations and annotation generation.

Page 10: Annotating Java Bytecode

Full Circle: Static Self Reinforcing Feedback Loop

Annotations can flow in both directions

JVM with Profiling

Sootannotations

Page 11: Annotating Java Bytecode

Static Compiler Analysis Info Array bound checks. Null pointer checks (Maybe) Stack allocation of objects (Escape

Analysis). Runtime Static Method Binding. Register Allocation. …

Page 12: Annotating Java Bytecode

Profile Related Info Hot methods.

Persistent objects / Various information relating the GC subsystem and memory allocation.

Branch Prediction Annotation.

Hot Data

Page 13: Annotating Java Bytecode

Important Issues Verifiability Platform Independence Classfile bloat due to annotations Vendor support …

Page 14: Annotating Java Bytecode

Verifiability and Attributes Many attributes are inherently “safe” (I.e

HotMethods). Often the information conveyed by

attributes can be verified fast (linear time). Verifiability could be linked to the Java

security model’s with various permission levels, attributes could be signed/encrypted.

Many Java environments do not enforce verifiability. Attributes could be trusted as a last resort.

Page 15: Annotating Java Bytecode

Attribute Support in Soot Introduced 2 new Interfaces: Host and

Tag. Hosts are objects that can hold Tags;

Tags are objects that can be attached to Hosts.

SootClass, SootField, SootMethod and Unit all implement Host. This is natural counterpart to the 4 locales of attributes in classfiles.

Page 16: Annotating Java Bytecode

Attribute Support in Soot

Page 17: Annotating Java Bytecode

The Host Interfacepublic interface Host

{

/** Get a list of tags associated with the current object. */

public List getTags();

/** Returns the tag with the given name. */

public Tag getTag(String aName);

public void addTag(Tag t);

/** Remove the tag with the given name. */

public void removeTag(String name);

/** Returns true if this host has a tag with the given name. */

public boolean hasTag(String aName);

}

Page 18: Annotating Java Bytecode

The Tag Interfacepublic interface Tag {

/* get the name of the tag as will appear in CP */

public String getName();

/* get the actual data of the tag as byte[] */

public byte[] getEncoding();

/* provide a Human friendly printout of the Tag for tools */

public String toString();

/* explicitly set the value of the tag from raw bytes */

public void setValue(byte[] value);

}

Page 19: Annotating Java Bytecode

From Soot to classfiles Currently Soot produces .jasmin

code that is then processed by Jasmin into classfiles

Attribute support has been added to Jasmin

Attribute support has been added to Jas (used by Jasmin)

Page 20: Annotating Java Bytecode

Jasmin with Attributes Introduced 4 new

directives: .class_attribute, .field_attribute, .method_attribute, .code_attribute

Attributes are encoded in Base64 Format: directive name value Example: dload 8 dastore .code_attribute ArrayCheckTag AAAA goto label1

Page 21: Annotating Java Bytecode

Printing out Attributes The TagPrinter interface:

public interface TagPrinter {

public String print(String aClassName,

String aFieldOrMtdSignature,

Tag aTag);

}

You register a TagPrinter with the TagManager. You can easily implement a custom PreatyPrinter, XML Printer , …

Page 22: Annotating Java Bytecode

The TagManager class Provides support functionality for

Tags. Used for printing out Tags, you can

register a TagPrinter. Used to lookup the class for an

attribute given it’s name.

Page 23: Annotating Java Bytecode

The StdTagPrinter Soot already has a printer for Tags: the

StdTagPrinter. Sample output: <FFT:protected static void transform_internal(double[], int)>+130/ArrayCheckTag AQ== <FFT:protected static void transform_internal(double[], int)>+144/ArrayCheckTag AQ== <FFT:protected static void transform_internal(double[], int)>+155/ArrayCheckTag AQ== <FFT:protected static void transform_internal(double[], int)>+176/ArrayCheckTag AQ== <FFT:protected static void transform_internal(double[], int)>+313/ArrayCheckTag AQ==

Used by the PrintAttributes tool.

Page 24: Annotating Java Bytecode

The first Soot Attribute:soot.ArrayCheckTag Supported in Soot by the class

ArrayCheckTag which implements Tag Using Feng’s Analysis ArrayCheckTags

are attached to Jimple Statements Soot automatically transfers Jimple

level tags to the appropriate bytecodes array access bytecodes.

Page 25: Annotating Java Bytecode

Analysis Extract if (maxValueMap.containsKey(index)) {

AbstractValue indexV = (AbstractValue)maxValueMap.get(index);

if (indexV.lessThan(arrayLength))

upCheck = false;

}

else if (index instanceof IntConstant) {

AbstractValue tmpAv = AbstractValue.newConstantValue(index);

if (tmpAv.lessThan(arrayLength))

upCheck = false;

}

Tag checkTag = new ArrayCheckTag(lowCheck, upCheck);

if (!lowCheck || !upCheck) {

s.addTag(checkTag);

}

Page 26: Annotating Java Bytecode

The soot.ArrayCheckTag Attribute Current Format of it’s data: 2 bytes pc 1 byte array check value (2 bits

are used) Total size of one ArrayCheckTag

Attribute: 7 bytes + a one time cost for the Constant Pool entry.

Page 27: Annotating Java Bytecode

Tools to View Attribute information in classfiles The PrintAttributes class uses the

StdTagPrinter to printout all Attributes in a classfile.

Tag t = TagManager.getTagFor(u.getName()); if(t != null) { t.setValue(u.getBytes()); System.out.println(TagManager.print(currentClass,currentMethod,t)); }

Extended JavaClass’ Class2HTML tool to support arbitrary attributes.

Page 28: Annotating Java Bytecode

What’s Missing for Complete Attribute Support in Soot Reading into Soot attributed .class

and .jimple file. Reading into Soot attribute

summury files. Provide a mechanism in Soot to

abstract the Pc (I.e. for code attributes)

Page 29: Annotating Java Bytecode

Related Work

Restricted to register allocation attributes.

Joel Jones and Samuel Kamin – University of Illinois at Urbana-Champaign.

Hummel, Azevedo and Nicolau – University of California at Irvine

Page 30: Annotating Java Bytecode

Overview of Work on Register Allocation Attributes Perform Virtual Register Allocation Assume an infinite number of

registers and minimize the number actually used.

Deal with verifiability: monotype each virtual register (Jones & al.).

Deal with spills: Virtual register priorities and Swap annotations (Jones &al.).

Page 31: Annotating Java Bytecode

Conclusion

Soot can now generate classfile attributes.

Tools now exist to visualize and printout Soot attributes.

Page 32: Annotating Java Bytecode

Future Work Development of analyses that

exploit annotations IBM HPCJ support for Soot

Annotations Kaffe OpenVM™ support for Soot

Annotations Write Papers, go to conferences,

become famous !

Page 33: Annotating Java Bytecode

References A. Azevedo, A. Nicolau and J. Hummel. Java Annotation-Aware Just-In-Time Compilation System. Proc. Of the ACM 1999 Java Grande Conference. J. Hummel, A. Azevedo, D. Kolson and A. Nicolau. Annotation Java Bytecodes in support for optimization.

Concurrency: Practice and Experience, November 1997. J. Jones, S. Kamin. Annotating Java Class Files with Virtual Registers for Performance.

[To be published] JVM Spec

http://java.sun.com/docs/books/vmspec/2ndedition/html/VMSpecTOC.doc.html

JavaClass http://www.inf.fu-berlin.de/~dahm/JavaClass/ Jasmin

http://www.cat.nyu.edu/meyer/jasmin/