1 / 33

Annotating Java Bytecode

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

mahdis
Download Presentation

Annotating Java Bytecode

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Annotating Java Bytecode By Patrice Pominville McGill University April 2000

  2. 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

  3. 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.

  4. 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 !

  5. 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.

  6. 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.

  7. Attributes in Classfiles

  8. 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

  9. 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.

  10. Full Circle: Static Self Reinforcing Feedback Loop • Annotations can flow in both directions JVM with Profiling Soot annotations

  11. Static Compiler Analysis Info • Array bound checks. • Null pointer checks (Maybe) • Stack allocation of objects (Escape Analysis). • Runtime Static Method Binding. • Register Allocation. • …

  12. Profile Related Info • Hot methods. • Persistent objects / Various information relating the GC subsystem and memory allocation. • Branch Prediction Annotation. • Hot Data • …

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

  14. 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.

  15. 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.

  16. Attribute Support in Soot

  17. 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); }

  18. 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); }

  19. 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)

  20. 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

  21. 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 , …

  22. 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.

  23. 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.

  24. 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.

  25. 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); }

  26. 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.

  27. 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.

  28. 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)

  29. 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

  30. 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.).

  31. Conclusion • Soot can now generate classfile attributes. • Tools now exist to visualize and printout Soot attributes.

  32. 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 !

  33. 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/

More Related