it - McGill University

Download Report

Transcript it - McGill University

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
Related work
Conclusion
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.
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 !
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.
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.
Attributes in Classfiles
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
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.
Full Circle: Static Self
Reinforcing Feedback Loop

Annotations can flow in both directions
JVM with
Profiling
annotations
Soot
Static Compiler Analysis Info






Array bound checks.
Null pointer checks (Maybe)
Stack allocation of objects (Escape
Analysis).
Runtime Static Method Binding.
Register Allocation.
…
Profile Related Info





Hot methods.
Persistent objects / Various information
relating the GC subsystem and memory
allocation.
Branch Prediction Annotation.
Hot Data
…
Important Issues





Verifiability
Platform Independence
Classfile bloat due to annotations
Vendor support
…
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.
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.
Attribute Support in Soot
The Host Interface
public 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);
}
The Tag Interface
public 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);
}
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)
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
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 , …
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.
The StdTagPrinter


Soot already has a printer for Tags: the
StdTagPrinter.
Sample output:
<FFT:protected static
<FFT:protected static
<FFT:protected static
<FFT:protected static
<FFT:protected static

void
void
void
void
void
transform_internal(double[], int)>+130/ArrayCheckTag AQ==
transform_internal(double[], int)>+144/ArrayCheckTag AQ==
transform_internal(double[], int)>+155/ArrayCheckTag AQ==
transform_internal(double[], int)>+176/ArrayCheckTag AQ==
transform_internal(double[], int)>+313/ArrayCheckTag AQ==
Used by the PrintAttributes tool.
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.
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);
}
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.
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.
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)
Related Work
Restricted to register allocation attributes.
 Joel Jones and Samuel Kamin –
University of Illinois at UrbanaChampaign.
 Hummel, Azevedo and Nicolau –
University of California at Irvine
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.).
Conclusion


Soot can now generate classfile
attributes.
Tools now exist to visualize and printout
Soot attributes.
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 !
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/