Verification of Sequential Imperative Programs in Isabelle/HOL
Total Page:16
File Type:pdf, Size:1020Kb
f f f f f f f f f f f f f f f f f f f f Verification of Sequential Imperative Programs in Isabelle/HOL Norbert Schirmer Lehrstuhl fur¨ Software & Systems Engineering Institut fur¨ Informatik Technische Universitat¨ Munchen¨ Lehrstuhl fur¨ Software & Systems Engineering Institut fur¨ Informatik Technische Universitat¨ Munchen¨ Verification of Sequential Imperative Programs in Isabelle/HOL Norbert Schirmer Vollstandiger¨ Abdruck der von der Fakultat¨ fur¨ Informatik der Technischen Universitat¨ Munchen¨ zur Erlangung des akademischen Grades eines Doktors der Naturwissenschaften (Dr. rer. nat.) genehmigten Dissertation. Vorsitzender: Univ.-Prof. Dr. Helmut Seidl Prufer¨ der Dissertation: 1. Univ.-Prof. Tobias Nipkow, Ph.D. 2. Univ.-Prof. Dr. Wolfgang J. Paul Universitat¨ des Saarlandes Die Dissertation wurde am 31.10.2005 bei der Technischen Universitat¨ Munchen¨ eingereicht und durch die Fakultat¨ fur¨ Informatik am 21.04.2006 angenommen. i Kurzfassung Ziel der Dissertation ist es, eine Verifikationsumgebung fur¨ sequentielle imperative Programme zu schaffen. Zunachst¨ wird unabhangig¨ von einer konkreten Program- miersprache ein allgemeines Sprachmodell entwickelt, das ausdrucksstark genug ist um alle gangigen¨ Programmiersprachkonzepte abzudecken: Gegenseitig rekur- sive Prozeduren, abrupte Terminierung und Ausnahmebehandlung, Laufzeitfeh- ler, lokale und globale Variablen, Zeiger und Halde, Ausdrucke¨ mit Seiteneffekten, Zeiger auf Prozeduren, partielle Applikation, dynamischer Methoden Aufruf und unbeschrankter¨ Indeterminismus. Fur¨ dieses Sprachmodell wird eine Hoare Logik sowohl fur¨ partielle alsauch fur¨ totale Korrektheit entwickelt. Darauf aufbauend wird ein Verifikations-Bedingungs- Generator implementiert. Die Hoare Logik erlaubt die Integration von statischer Programmanalyse und Software Model Checkern in die Verifikation. Desweiteren wird eine Teilsprache von C in die Verifikationsumgebung eingebet- tet, um die Durchgangigkeit¨ zu einer realen Programmiersprache zu demonstrieren. Die gesamte Entwicklung wurde im Theorembeweiser Isabelle durchgefuhrt.¨ Dadurch wird zum einen die Korrektheit maschinell sichergestellt und zum anderen steht nun fur¨ die Verifikation von Programmen die reichhaltige Infrastruktur einer vollwertigen und universellen Beweisumgebung zur Verfugung.¨ ii iii Abstract The purpose of this thesis is to create a verification environment for sequential imper- ative programs. First a general language model is proposed, which is independent of a concrete programming language but expressive enough to cover all common language features: mutually recursive procedures, abrupt termination and excep- tions, runtime faults, local and global variables, pointers and heap, expressions with side effects, pointers to procedures, partial application and closures, dynamic method invocation and also unbounded nondeterminism. For this language a Hoare logic for both partial and total correctness is developed and on top of it a verification condition generator is implemented. The Hoare logic is designed to allow the integration of program analysis or software model checking into the verification. To demonstrate the continuity to a real programming language a subset of C is embedded into the verification environment. The whole work is developed in the theorem prover Isabelle. Therefore the correctness is machine-checked and in addition the rich infrastructure of the general purpose theorem prover Isabelle can be employed for the verification of imperative programs. iv v Funk is, what you don’t play. — Maceo Parker Acknowledgements I thank Tobias Nipkow for supervising this thesis and giving me the opportunity to work in his research group. I thank Wolfgang J. Paul for acting as referee and initiating the Verisoft project. I am indebted to Amine Chaieb, Gerwin Klein, Martin Strecker and Martin Wildmoser for reading and commenting on drafts of this thesis. I also thank all the former and current members of the Isabelle team in Munich for the friendly and relaxed working atmosphere, where there is always time for a coffee break and discussions: Clemens Ballarin (for his wise advise), Gertrud Bauer, Stefan Berghofer (for all his support with Isabelle), Amine Chaieb, Florian Haft- mann, Gerwin Klein (for collaboration and friendship even across the continents), Alexander Krauss, Farhad Mehta, Steven Obua (for his power), David von Oheimb, Leonor Prensa-Nieto, Sebastian Skalberg, Martin Strecker (for introducing me to theorem proving and answering all my questions), Tjark Weber, Markus Wenzel (for his ideal quest for proper solutions), and Martin Wildmoser. I thank my parents Anneliese and Jorg,¨ and my girlfriend Sarah Erath for their love and support. vi Contents 1 Introduction 1 1.1 Motivation .................................. 1 1.2 Contributions ................................ 3 1.3 Related Work ................................ 4 1.4 Overview ................................... 6 1.5 Preliminaries on Isabelle .......................... 7 2 The Simpl language 15 2.1 Abstract Syntax ............................... 16 2.2 Semantics .................................. 17 2.3 Termination ................................. 19 2.4 Derived Language Features ........................ 19 2.4.1 State Space Representation .................... 20 2.4.1.1 State as Function ..................... 21 2.4.1.2 State as Tuple ....................... 22 2.4.1.3 State as Record ...................... 23 2.4.2 Concrete Syntax for Simpl ..................... 24 2.4.3 Expressions with Side Effects ................... 25 2.4.4 Abrupt Termination ........................ 25 2.4.5 Blocks and Procedures with Parameters ............ 26 2.4.6 Dynamic Procedure Calls ..................... 28 2.4.7 Closures ............................... 28 2.4.8 Arrays ................................ 30 2.4.9 Heap and Pointers ......................... 30 2.4.9.1 Split Heap Approach .................. 30 2.4.9.2 Memory Management ................. 32 2.5 Conclusion .................................. 33 3 Hoare Logic for Simpl 35 3.1 Partial Correctness ............................. 38 3.1.1 “The” Consequence Rule ..................... 40 3.1.2 Recursive Procedures ....................... 45 3.1.3 Soundness ............................. 46 3.1.4 Completeness ............................ 50 3.2 Total Correctness .............................. 55 3.2.1 Soundness .............................. 57 3.2.2 Completeness ............................ 59 3.3 Conclusion .................................. 70 viii Contents 4 Utilising the Hoare Logic 73 4.1 Loops ..................................... 74 4.2 Expressions with Side Effects ....................... 76 4.3 Blocks ..................................... 76 4.4 Procedures .................................. 77 4.5 Abrupt Termination ............................. 82 4.6 Heap ..................................... 84 4.7 Dynamic Procedure Call .......................... 88 4.8 Closures ................................... 91 4.9 Introducing Annotations ......................... 95 4.10 Conclusion .................................. 96 5 Interfacing Program Analysis 99 5.1 Stripping Guards .............................. 100 5.2 Marking Guards ............................... 103 5.3 Discharging Guards ............................. 104 5.4 Adding Guards ............................... 106 5.5 Conclusion .................................. 107 6 Compositionality 109 6.1 Exploiting Structural Subtyping ...................... 109 6.2 Lifting Hoare Triples ............................ 110 6.3 Example ................................... 115 6.4 Conclusion .................................. 117 7 The C0 Language 119 7.1 Abstract Syntax ............................... 121 7.1.1 Names ................................ 121 7.1.2 Types ................................ 121 7.1.3 Values ................................ 122 7.1.4 Expressions ............................. 123 7.1.5 Statements .............................. 124 7.1.6 Programs .............................. 124 7.2 Semantics .................................. 125 7.2.1 State ................................. 125 7.2.2 Expression Evaluation ....................... 126 7.2.3 Left-Expression Evaluation and Assignment .......... 130 7.2.4 Big-Step Semantics ......................... 132 7.2.5 Termination ............................. 134 7.2.6 Typing ................................ 135 7.2.7 Definite Assignment ........................ 140 7.2.8 Wellformed Programs ....................... 142 7.2.9 Type Safety ............................. 142 7.3 Conclusion .................................. 145 8 Embedding C0 into Simpl 147 8.1 Splitting Types and Values ......................... 152 8.2 Expressions ................................. 156 8.3 Left-Expressions ............................... 168 8.4 Assignments ................................. 171 Contents ix 8.5 Execution ................................... 181 8.6 Termination ................................. 186 8.7 Hoare Triples ................................ 191 8.8 Example ................................... 194 8.9 Conclusion .................................. 214 9 Conclusion and Outlook 215 9.1 Practical Experiences ............................ 215 9.1.1 Normalisation of Binary Decision Diagrams .......... 215 9.1.2 C0 Verification in Verisoft ..................... 217 9.1.3 Memory Management of L4 Kernel, with Refinement ..... 218 9.2 State Space Representation Revisited .................. 219 9.3 Contributions ...............................