Annotating Java Bytecode By Patrice Pominville Mc Gill
Annotating Java Bytecode By Patrice Pominville Mc. Gill University April 2000
Overview of Presentation n n n 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 n 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… n n 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 n n 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 n Practically all classfiles contain attributes. n n n 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 { u 2 attribute_name_index; u 4 attribute_length; u 1 info[attribute_length]; } n n n 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 n n n 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 n Annotations can flow in both directions JVM with Profiling annotations Soot
Static Compiler Analysis Info n n n Array bound checks. Null pointer checks (Maybe) Stack allocation of objects (Escape Analysis). Runtime Static Method Binding. Register Allocation. …
Profile Related Info n n n Hot methods. Persistent objects / Various information relating the GC subsystem and memory allocation. Branch Prediction Annotation. Hot Data …
Important Issues n n n Verifiability Platform Independence Classfile bloat due to annotations Vendor support …
Verifiability and Attributes n n Many attributes are inherently “safe” (I. e Hot. Methods). 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 n n n Introduced 2 new Interfaces: Host and Tag. Hosts are objects that can hold Tags; Tags are objects that can be attached to Hosts. Soot. Class, Soot. Field, Soot. Method 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 get. Tags(); /** Returns the tag with the given name. */ public Tag get. Tag(String a. Name); public void add. Tag(Tag t); /** Remove the tag with the given name. */ public void remove. Tag(String name); /** Returns true if this host has a tag with the given name. */ public boolean has. Tag(String a. Name); }
The Tag Interface public interface Tag { /* get the name of the tag as will appear in CP */ public String get. Name(); /* get the actual data of the tag as byte[] */ public byte[] get. Encoding(); /* provide a Human friendly printout of the Tag for tools */ public String to. String(); /* explicitly set the value of the tag from raw bytes */ public void set. Value(byte[] value); }
From Soot to classfiles n n n 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 n Introduced 4 new directives: . class_attribute, . field_attribute, . method_attribute, . code_attribute n Attributes are encoded in Base 64 Format: directive name value n Example: n dload 8 dastore . code_attribute Array. Check. Tag AAAA goto label 1
Printing out Attributes n The Tag. Printerface: public interface Tag. Printer { public String print(String a. Class. Name, String a. Field. Or. Mtd. Signature, Tag a. Tag); } n You register a Tag. Printer with the Tag. Manager. You can easily implement a custom Preaty. Printer, XML Printer , …
The Tag. Manager class n n n Provides support functionality for Tags. Used for printing out Tags, you can register a Tag. Printer. Used to lookup the class for an attribute given it’s name.
The Std. Tag. Printer n n Soot already has a printer for Tags: the Std. Tag. Printer. Sample output: <FFT: protected <FFT: protected n static static void void transform_internal(double[], transform_internal(double[], int)>+130/Array. Check. Tag int)>+144/Array. Check. Tag int)>+155/Array. Check. Tag int)>+176/Array. Check. Tag int)>+313/Array. Check. Tag Used by the Print. Attributes tool. AQ== AQ==
The first Soot Attribute: soot. Array. Check. Tag n n n Supported in Soot by the class Array. Check. Tag which implements Tag Using Feng’s Analysis Array. Check. Tags are attached to Jimple Statements Soot automatically transfers Jimple level tags to the appropriate bytecodes array access bytecodes.
Analysis Extract if (max. Value. Map. contains. Key(index)) { Abstract. Value index. V = (Abstract. Value)max. Value. Map. get(index); if (index. V. less. Than(array. Length)) up. Check = false; } else if (index instanceof Int. Constant) { Abstract. Value tmp. Av = Abstract. Value. new. Constant. Value(index); if (tmp. Av. less. Than(array. Length)) up. Check = false; } Tag check. Tag = new Array. Check. Tag(low. Check, up. Check); if (!low. Check || !up. Check) { s. add. Tag(check. Tag); }
The soot. Array. Check. Tag Attribute n Ø Ø n Current Format of it’s data: 2 bytes pc 1 byte array check value (2 bits are used) Total size of one Array. Check. Tag Attribute: 7 bytes + a one time cost for the Constant Pool entry.
Tools to View Attribute information in classfiles n The Print. Attributes class uses the Std. Tag. Printer to printout all Attributes in a classfile. Tag t = Tag. Manager. get. Tag. For(u. get. Name()); if(t != null) { t. set. Value(u. get. Bytes()); System. out. println(Tag. Manager. print(current. Class, current. Method, t)); } n Extended Java. Class’ Class 2 HTML tool to support arbitrary attributes.
What’s Missing for Complete Attribute Support in Soot n n n 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. n Joel Jones and Samuel Kamin – University of Illinois at Urbana. Champaign. n Hummel, Azevedo and Nicolau – University of California at Irvine
Overview of Work on Register Allocation Attributes n n 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 n n Soot can now generate classfile attributes. Tools now exist to visualize and printout Soot attributes.
Future Work n n Development of analyses that exploit annotations IBM HPCJ support for Soot Annotations Kaffe Open. VM™ support for Soot Annotations Write Papers, go to conferences, become famous !
References n A. Azevedo, A. Nicolau and J. Hummel. Java Annotation-Aware Just-In-Time Compilation System. Proc. Of the ACM 1999 Java Grande Conference. n J. Hummel, A. Azevedo, D. Kolson and A. Nicolau. Annotation Java Bytecodes in support for optimization. Concurrency: Practice and Experience, November 1997. n J. Jones, S. Kamin. Annotating Java Class Files with Virtual Registers for Performance. [To be published] n n n JVM Spec http: //java. sun. com/docs/books/vmspec/2 ndedition/html/VMSpec. TOC. doc. html Java. Class http: //www. inf. fu-berlin. de/~dahm/Java. Class/ Jasmin http: //www. cat. nyu. edu/meyer/jasmin/
- Slides: 33