Static and Dynamic Contract Verifiers For Java Hongming Liu
Agenda Introduction to Design by Contract. Existing Design by Contract solutions. Our Design by Contract toolset.
Design by Contract Design by Contract (DBC) theory suggests associating a specification with every software element. These specifications (or contracts) govern the interaction of the element with the rest of the world. Component of a system communicate with one another on the basis of precisely defined benefits and obligations.
Design by Contract Contract tags are specified as comments: an example from the java.lang.String class: /** index >= 0 index < this.length() */ char charAt(int index){…} Three types of basic contract tags: Method preconditions. Method postconditions. Class invariants.
Design by Contract – Tags Three tag types standard to DBC solutions: Method precondition a boolean expression assumed to be true whenever the method is called. Method postcondition a boolean expression assumed to be true when the method returns. Class invariant a boolean expression that can assumed to be true in all instances when the point of execution is not in a transit state.
Design by Contract Sets forth a system of obligations and benefits. The client has the obligation to satisfy the preconditions of a method before calling that method. The supplier of the method has the obligation to satisfy the postconditions of the method before returning from that method. Both receive the benefit of input or output values that are guaranteed to comply to the contract.
Design by Contract – Tags Three extended tags found in our DBC solution: Assert assertion for a statement, equivalent to the Java assert. Fact fact that is guaranteed to hold for a statement. Loop invariant Invariant that holds for each iteration of a loop.
Design by Contract - Benefits Gives a better understanding of software construction. A systematic approach to building bug-free object- oriented systems. A method for documenting software components. An effective framework for debugging, testing, and quality assurance. Better understanding and control of the inheritance mechanism. A technique for dealing with abnormal cases, leading to an effective language construct for exception handling.
Design by Contract Existing Tools include: JDK 1.4 (assert) iContract JMSAssert Eiffel
Our DBC Toolset Components of our DBC Toolset: DocGen – generates documentation (Javadoc) that includes DBC information. ContractChecker – creates modified versions of Java classes with additional code that is used for runtime checking of the contract. Static Contract Verifier – statically checks Java code for specific problems, such a possible null- pointer exceptions, array out of bounds exceptions, or contract adherence at compile-time (Static Contract Verification).
Our DBC Toolset Advantages of our DBC Toolset: Uses a specified syntax with high expressiveness. DocGen provides documentation that includes contract information. Static Analysis provides a partial alternative to time-consuming runtime checking with Static Analysis and Static Contract Verification.
Static Contract Verification Uses a weakest precondition algorithm. /** * weakest precondition: this.x >=1 this.x >= 2 this’.x >= 0 */ public void decrementX() { this.x = this.x - 1; }
Example – Contract Generation /** * From a hypothetical Stack class. this.size >= 1 this'.size == this.size - 1 */ public Object pop() { Object e = this.stack[this.size - 1]; this.size = this.size - 1; return e; }
Generated Code - Part 1 of 2 public Object pop() { if (!(this.size >= 1)) { Contract contract = Contract.getInstance(); contract.reportViolation(new PreconditionException("PreconditionException.o ccurs:.the.precondition.contract.is.violated:")); } Object e = this.stack[this.size - 1]; this.size = this.size - 1; Object _$$_return = e; /* continued… */
Generated Code - Part 2 of 2 if (!(this.size == _pre$$_this.size - 1)) { Contract contract = Contract.getInstance(); contract.reportViolation(new PostconditionException("PostconditionException. occurs:.the.postcondition.contract.is.violated:")); } return _$$_return; }
Miscellaneous Thank you to Dr. Jia, Frank Qin, and Jing Wang. Project website is at: Questions?