Stata & Abadi: a Type System for Java Bytecode Subroutines

Stata & Abadi: a Type System for Java Bytecode Subroutines

A Type System for Java Bytecode Subroutines Raymie Stata and Mart´ın Abadi Digital Equipment Corporation Systems Research Center Abstract object initialization failures (such as accessing a newly al- located object before its constructor has been called), and Java is typically compiled into an intermediate language, other dynamic errors. JVML, that is interpreted by the Java Virtual Machine. Be- Figure 1 illustrates how bytecode verification fits into cause mobile JVML code is not always trusted, a bytecode the larger picture of Java security. The figure represents verifier enforces static constraints that prevent various dy- trusted and untrusted code. At the base of the trusted code namic errors. Given the importance of the bytecode verifier is the Java VM itself|including the bytecode verifier—plus for security, its current descriptions are inadequate. This the operating system, which provides access to privileged paper proposes using typing rules to describe the bytecode resources. On top of this base layer is the Java library, which verifier because they are more precise than prose, clearer provides controlled access to those privileged resources. Java than code, and easier to reason about than either. security depends on the VM correctly interpreting JVML JVML has a subroutine construct used for the compi- code, which in turn depends on the verifier rejecting illegal lation of Java's try-finally statement. Subroutines are a JVML code. If the verifier were broken but the rest of the major source of complexity for the bytecode verifier because VM assumed it was correct, then JVML code could behave they are not obviously last-in/first-out and because they re- in ways not anticipated by the Java library, circumventing quire a kind of polymorphism. Focusing on subroutines, we the library's access controls. isolate an interesting, small subset of JVML. We give typ- Given its importance for security, current descriptions of ing rules for this subset and prove their correctness. Our the verifier are deficient. The official specification of the ver- type system constitutes a sound basis for bytecode verifica- ifier is a 20-page, prose description. Although good by the tion and a rational reconstruction of a delicate part of Sun's standards of prose, this description is ambiguous, imprecise, bytecode verifier. and hard to reason about. In addition to this specifica- tion, Sun distributes what could be considered a reference implementation of the verifier. As a description, this imple- 1 Bytecode verification and typing rules mentation is precise, but it is hard to understand and, like the prose description, is hard to reason about. Furthermore, The Java language is typically compiled into an intermedi- the implementation disagrees with the prose description. ate language that is interpreted by the Java Virtual Machine This paper proposes using typing rules to describe the (VM) [LY96]. This intermediate language, which we call verifier. Typing rules are more precise than prose, easier JVML, is an object-oriented language similar to Java. Its to understand than code, and they can be manipulated for- features include packages, classes with single inheritance, mally. Such rules would give implementors of the verifier a and interfaces with multiple inheritance. However, unlike systematic framework on which to base their code, increas- method bodies in Java, method bodies in JVML are se- ing confidence in its correctness. Such rules would also give quences of bytecode instructions. These instructions are implementors of the rest of the VM an unambiguous state- fairly high-level but, compared to the structured statements ment of what they can and cannot assume about legal JVML used in Java, they are more compact and easier to interpret. code. JVML code is often shipped across networks to Java VMs From a typing perspective, JVML is interesting in at embedded in web browsers and other applications. Mobile least two respects: JVML code is not always trusted by the VM that receives it. Therefore, a bytecode verifier enforces static constraints on In JVML, a location can hold different types of val- mobile JVML code. These constraints rule out type errors • ues at different program points. This flexibility al- (such as dereferencing an integer), access control violations lows locations to be reused aggressively, allowing in- (such as accessing a private method from outside its class), terpreters to save space. Thus, JVML contrasts with most typed languages, in which a location has only one type throughout its scope. To appear in the Proceedings of the 25th Annual SIGPLAN-SIGACT Symposium on Principles of Pro- JVML has subroutines to help compilers generate com- gramming Languages, 1998. • pact code for Java try-finally statements. JVML sub- routines are subsequences of the larger sequence of bytecode instructions that make up a method's body. Hostile JVML code Untrustred code Trusted code Java library (including Library reference monitors) level C libraries/syscalls for Java VM Base protected resources (including bytecode verifier) level Figure 1: The Java VM and security The JVML instruction jsr jumps to the start of one Section 7 states the main soundness theorem for our type of these subsequences, and the JVML instruction ret system. Sections 8 and 9 discuss related work, including returns from one. Subroutines introduce two signifi- Sun's bytecode verifier. Section 8 also considers how our cant challenges to the design of the bytecode verifier: type system could be extended to the full JVML. Section 10 ensuring that ret is used in a well-structured manner, concludes. Most formal claims and all proofs are omitted in and supporting a certain kind of polymorphism. We this summary. describe these challenges in more detail in Section 2. This paper addresses these typing problems. It defines a 2 Overview of JVML subroutines and our small subset of JVML, called JVML0, presents a type sys- type system tem and a dynamic semantics for JVML0, and proves the soundness of the type system with respect to the dynamic JVML subroutines are subsequences of a method's larger semantics. JVML0 includes only 9 instructions, and ignores sequence of instructions; they behave like miniature pro- objects and several other features of JVML. This restricted cedures within a method body. Subroutines are used for scope allows us to focus on the challenges introduced by sub- compiling Java's try-finally statement. routines. Thus, our type system provides a precise, sound Consider, for example, the Java method named bar at approach to bytecode verification, and a rational reconstruc- the top of Figure 2. The try body can terminate in three tion of a delicate part of Sun's bytecode verifier. ways: immediately when i does not equal 3, with an excep- We present the type system in three stages: tion raised by the call to foo, or with an execution of the 1. The first stage is a simplified version for a subset of return statement. In all cases, the compiler must guarantee JVML0 that excludes jsr and ret. This simplified that the code in the finally block is executed when the try version provides an introduction to our notation and body terminates. Instead of replicating the finally code at approach, and illustrates how we give different types each escape point, the compiler can put the finally code in to locations at different program points. a JVML subroutine. Compiled code for an escape from a try body executes a jsr to the subroutine containing the 2. The next stage considers all of JVML0 but uses a struc- finally code. tured semantics for jsr and ret. This structured se- Figure 2 illustrates the use of subroutines for compiling mantics has an explicit subroutine call stack for ensur- try-finally statements. It contains a possible result of com- ing that subroutines are called on a last-in, first-out piling the method bar into JVML, putting the finally code basis. In the context of this structured semantics, we in a subroutine in lines 13{16. show how to achieve the polymorphism desired for sub- Figure 2 also introduces some of JVML's runtime struc- routines. tures. JVML bytecode instructions read and write three memory regions. The first region is an object heap shared by 3. The last stage uses a stackless semantics for jsr and all method activations; the heap does not play a part in the ret in which return addresses are stored in random- example code of Figure 2. The other two regions are private access memory. The stackless semantics is closer to to each activation of a method. The first of these regions is Sun's. It admits more efficient implementations, but the operand stack, which is intended to be used on a short- it does not dynamically enforce a last-in, first-out dis- term basis in the evaluation of expressions. For example, cipline on calls to subroutines. Because such a disci- the instruction iconst 3 pushes the integer constant 3 onto pline is needed for type safety, we show how to enforce this stack, while ireturn terminates the current method it statically. returning the integer at the top of the stack. The second The next section describes JVML subroutines in more region is a set of locations known as local variables, which detail and summarizes our type system. Section 3 gives the are intended to be used on a longer-term basis to hold values syntax and an informal semantics for JVML0. Sections 4{6 across expressions and statements (but not across method present our type system in the three stages outlined above. activations). Local variables are not operated on directly. int bar(int i) try f fif (i ==

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us