2k likes | 2.13k Vues
Java Virtual Machine: VM Architecture, Software Architecture, Implementations, and Application Programming Interfaces. Wen-mei Hwu Department of ECE University of Illinois. Objective of the Course. In-depth understanding of unique aspects of Java-based systems
E N D
Java Virtual Machine: VM Architecture, Software Architecture, Implementations, and Application Programming Interfaces Wen-mei Hwu Department of ECE University of Illinois
Objective of the Course • In-depth understanding of unique aspects of Java-based systems • Benefit of Java language features for various applications • Complexities of the Java Bytecode architecture • Functionalities of the Java API and runtime
Outline • Introduction • Basic Bytecode Architecture • Java Objects • Java Runtime • Java Core API • Related Technologies • Java Related Developments
Java Security • Bytecode Level • Disallows pointer manipulation • Provides array bounds checking • Checks for NULL references (pointers) • Eliminates illegal type casting
Java Security • API Level • Prohibits untrusted code from accessing local disk, creating processes, connecting to other hosts, calling native code, etc. • Authentication: verify that bytecode is from a trusted vendor
Java Portability/Mobility • Bytecode Level • Binary compatibility: abstracts machine architecture and compiler • Standard module format and compact representation • Dynamic linking and loading
Java Portability/Mobility • API Level • Operating system abstraction • Consistent support for • Threads • I/O and Network interface • Database Interface • Graphical User interface
Development Environment • Language features • Automatic memory management • Structured exception handling • Object-oriented design
Development Environment • Modular development and 3rd-party software components • Standard, dynamically linked modules • Packages (related class file naming) • Avoid naming conflicts • Control access between modules
Key Application Areas • Network-based devices • Mobility - applet downloading and software agents • Portability - code server has no knowledge of client platform • Security - code cannot be trusted
Key Application Areas • Distributed applications • Common development environment across tiers • Same code can run on all machines • Software can be repartitioned as hardware/requirements change
Database Application Development • Can safely extend server software with Java bytecode • Traditional approach: may crash server or corrupt database • Java worst case: query fails or ties-up resources
Database Application Development • Removes barriers between client and server development (same language/tools on both platforms) • Easy linkage with server code (no binary compatibility issues) • Run server on multiple platforms/easy migration
Incorporating Object Data Types • Objects can be serialized and delivered to client • Same code can manipulate an object on both server and client • Different situations may call for either approach • Bytecode can be loaded directly from the database
Database Example • Each employee record contains a Java address object • Custom logic can be incorporated in methods of the Java class Address • Example: Using just street and zip, the Address initialization method computes city and state to ensure consistent address
Database Example • Database designers can exploit inheritance to handle address idiosyncrasies • Subclasses of type US_Address and CanadianAddress • A different Java method computes short zip code depending on type
Database Example Insert new rows using inheritance: INSERT INTO employees (id, name, address) VALUES (1001, “Bill Clinton”, new US_Address (‘1600 Pennsylvania Ave’, ‘22042-1234’)) INSERT INTO employees (id, name, address) VALUES (1002, “Jim Carrey”, new CanadianAddress(‘75 John Street’, ‘N2L 3X2’))
Database Example • Query cities and short zip codes • Method to calculate short zip code runs on server SELECT name, address.city, address.shortZip() FROM employees
Database Example • Query an address • Short zip method runs on client SELECT name, address FROM employees WHERE ID = ‘1001’ INTO cust_name, cust_address IF cust_address.state = “IL” THEN PRINT “Local Customer:” + cust_name + “ Zip:” + cust_address.shortZip()
Challenges Presented By Java • Translation to native architecture • Instruction set translation • Stack based to register based conversion • Dynamic Linking - delay translation or manage binary dependencies
Challenges Presented By Java • Barriers to optimization • Run-time checks (null ptr/array bounds) and exception handling constrain code motion • Heavily object-oriented code - unable to resolve calls/inline • Dynamic loading inhibits analysis (code base extended at runtime)
Challenges Presented By Java • Security features add overhead • Overhead of run-time checking instructions (null ptr/array bounds) • Additional code and control flow to protect resource access
Bytecode Architecture Overview • Load-store stack architecture • All operands pushed on value stack before use • Source operands are fetched from stack and the result is pushed on • Equivalent to register file in load-store architectures
Bytecode Architecture Overview • Extensive support for Java Language and Dynamic Linking • Object manipulation instructions • Method invocation instructions • Exception handling support
Bytecode Architecture Overview • Opcode space • 202 assigned, 25 quick, 3 reserved • Load-store stack architecture • avoid register file size assumption • smaller code size • 73.3% of the assigned opcodes are a single byte
Stack Computation Model Example: add Translated code push A push B r2 ¬ pop(B) r1¬ pop(A) r3 ¬ r1+r2 push r3 Stack operation push A push B add
Bytecode Modules (Class Files) • Java language compiler produces separate module (class file) for each class defined in program • All references across classes (and most within a class) • compiled as symbolic references • stored in a data structure inside class file ( constant pool)
Bytecode Modules (Class Files) • Bytecode with operands (27.7% of total assigned opcodes) • typically indexes into the constant pool or local variable table
Bytecode operands • As VM executes a method its stack frame holds a pointer to the constant pool of the method’s class • When constant pool entry is first referenced by an instruction, the Java VM resolves the symbolic link
Simple example Invokevirtual 0x0005 Constant Pool ... ... Method ref 5 Constant pool ... ... 8 class ref Stack frame ... Name & type a ... d “add” ... ‘(‘ ‘I’ ‘I’ ‘)’ ‘I’ 11
Bytecode operation types • Stack Manipulations • push, pop, duplicate, swap • Local Variable accessing • load, store • Arrays • create, store, retrieve
Bytecode operation types • Objects • create, access, set • Arithmetic and Type Conversion • add, subtract, multiply, divide, shift, AND, OR, XOR
Bytecode operation types • Control Flow and Exceptions • conditional, unconditional, goto, local returns, tables, throws • Method Calls and Returns • virtual, special, static, interface, returns
Quick Opcodes • Patented by Sun • Interpreter based runtime optimization • Not part of VM spec, never appear in class files • Patched over normal instructions at runtime the first time they are executed
Quick Opcodes • When a constant pool entry is resolved, the symbolic is reference replaced with direct reference • Quick opcode signifies constant pool entry already contains a direct reference to target, and VM does not have to perform certain checks
Java Objects Outline • Classes and Interfaces • Polymorphism and Dynamic Method Resolution • Method Tables • Bytecode Ops for Method Invocation • Bytecode Ops for Object Manipulation
Language Terms • Class: Data structure and associated functions to manipulate the data • Object: An instance of a class (Object creation=Class instantiation) • Method: Behavior associated with a class (function to manipulate object) • Field: Individual elements of data defined by a class (object state)
Language Terms • Polymorphism: Objects of different classes can be passed to the same client code. When the client code invokes a method on the object, the code executed depends on the object’s run-time class (requires dynamic method resolution, or late binding)
Basic Java Types • Primitive Types • byte, short, int, long, char, float, double, boolean • range of values for each type constant across platforms • Reference Types (non-primitive) • Declare pointers to objects on the Java Heap
Reference Types • Java language requires that each reference variable has a declared type (statically typed) • At runtime a reference may be assigned to different types of objects • If a type-cast is potentially unsafe, compiler inserts run-time checks to insure type “compatibility”
Reference Types • Interface: Set of guaranteed method declarations without any implementation • Class: Set of guaranteed methods and fields including method implementation • Only classes can be instantiated (objects always belong to a class)
Class Hierarchy • A class may extend one other class, its superclass. A class inherits the fields and methods of its superclass including their implementation. • A class may implement multiple interfaces, and must provide code for all methods guaranteed by those interfaces.
Extending Classes: Method Overriding • A subclass contains both the methods “inherited” from its superclass and any new methods defined in the class • A subclass may override a method in its superclass by redefining the method with same name, parameter types and return type
Abstract Classes • A class may defer implementing a method by declaring it abstract • A class with abstract methods cannot be instantiated • Any non-abstract subclass that has an abstract class ancestor must provide the implementation for the ancestor’s abstract methods
Inheritance Hierarchy Printer • A class has only one superclass, but may have many ancestors • Implementation of a single class can then be treated as multiple interfaces Class Interface DotMatrix LaserPrinter Copier FaxMachine Extends Implements OfficeMatePlus
Class/Interface Definition Example // Interfaces contain no code interface Copier { void makeCopies(); } interface FaxMachine { void sendFax(); } // Provide code for Copier and // FaxMachine interfaces // Inherit code from LaserPrinter class OfficeMatePlus extends LaserPrinter implements Copier, FaxMachine { void makeCopies() { // code to make copies ...} void sendFax() { // code to send a fax ... } } // Defer Printer implementation abstract class Printer { abstract void print(); } // Override Printer.print() class DotMatrix extends Printer void print() { // dot matrix printer code ... }} // Override Printer.print() class LaserPrinter extends Printer { void print() { // laser printer code ... }}
Dynamic Method Resolution • Dynamic resolution ensures that the correct code will be invoked based on the object’s runtime class • Interface Calls • If reference type is an interface, method resolution locates the method’s implementation based on the object’s runtime class
Dynamic Method Resolution • Virtual Calls • If the reference type is a class, method resolution must determine which implementation to use - the object’s runtime class may override a method defined in a base class
Revisiting Class/Interface Example // More office equipment class QuickCopier implements Copier { public void makeCopies() { … } } class EasyFax implements FaxMachine { public void sendFax() { … } } // Can use three types of machines class TrainedEmployee { void PrintDocument( Printer p) { p.print(); // Virtual call } void replicateDocument( Copier c) { // Interface call c.makeCopies(); } void faxDocument( FaxMachine f) { // Interface call f.sendFax(); } }
Revisiting Class/Interface Example // Upgrade to the OfficeMatePlus OfficeMatePlus o = new OfficeMatePlus(); // Trained employee can do // everything with one machine // by using different interfaces // depending on the situation e.printDocument(o); e.replicateDocument(o); e.faxDocument(o); // Polymorphism means you // don’t have to retrain your // employee // Create a new TrainedEmployee TrainedEmployee e = new TrainedEmployee(); // Create office equipment objects DotMatrix dm = new DotMatrix(); QuickCopier qc = new QuickCopier(); EasyFax ef = new EasyFax(); // TrainedEmployee can use all // three machines e.printDocument(dm); e.replicateDocument(qc); e.faxDocument(ef);