Formal Verificat Formal Verification with Dafny
Total Page:16
File Type:pdf, Size:1020Kb
1 Formal Verification with Dafny Cedric Abano Gordon Chu Gabriel Eiseman Justin Fu Tiffany Yu [email protected] [email protected] [email protected] [email protected] [email protected] Abstract North American electric grid led to a blackout Computer programming and software that lasted 26 hours and affected 50 million engineering have found their way into many persons in Ohio, Michigan, Pennsylvania, New facets of the current world. In certain industries, York, Vermont, Massachusetts, Connecticut, correct computer programs are needed for basic New Jersey, and Ontario.1, 2 In 2007, Qantas operation. This absolute correctness could be flight 72 from Singapore to Perth unexpectedly proven by brute force, which involves passing in nose-dived twice due to a faulty stabilization all possible test cases through a program. This algorithm that failed to consider two consecutive technique is largely impractical, especially for sensor errors, injuring 119 passengers and crew complex programs that do not have a bounded members.3 These incidents are just a few input range. Alternatively, a more theoretical instances from a long list of occurrences where and feasible approach can be taken, using incorrect code crippled the integrity of a large mathematics and logical reasoning to prove the scale system, demonstrating the importance of correctness of a program. Formal verification is writing code that is correct in all cases at all a field of computer science dealing with the times. construction of mathematical proofs of programs’ correctness. This paper explores the 1.2 The Software Development Cycle current state of formal verification through While there are several different models Dafny, a static program verifier created by of the software development cycle, the steps in Microsoft Research. Dafny was used to each can be generalized to four main categories.4 implement five common algorithms and compared with lower-level methods of formal verification. Although there were qualities about Dafny that were difficult to work with, ultimately self-verification of programs has positive prospects for future developments. 1. Introduction Figure 1. The Software Development Cycle 1.1 Why is it important to write correct programs? During the specification phase of With the growing reliance on software software engineering, data is gathered about the for functionality in various industries, computer problem to be solved. Developers describe all programs have become increasingly involved in aspects of the issue formally and gain an operations that are responsible for the wellbeing understanding of the subtle intricacies of individuals. This enlarged scope of influence accompanying it. A written specification is often entails a moral obligation for programmers to created at this stage, describing how the write code that works correctly under all program is to behave under various conditions. conditions. Programs must be correct for all After the specification is completed, inputs, not just expected ones, as even the developers proceed to the design phase where smallest error can have disastrous they consider various algorithms to solve the consequences. In 2003, a flawed assumption in problem efficiently. This can involve using pre- FirstEnergy’s estimator program modeling the existing algorithms or creating entirely new algorithms to approach to the problem. The majority of coding is done during this stage. 2 Following the software design phase is 2. Background the validation phase. During validation, Up to this point, the word “correct” has developers write test code to ensure their been used informally to describe programs. The program works. Such code usually consists of following sections will develop a precise testing standard input and edge cases. definition of correctness, building up the Once a program passes its validation definition through the mathematical notation of tests, the development cycle is repeated in order propositional logic and employing it in Dafny. to refine the program. The code is improved by repeatedly reevaluating its specification, design, 2.1 A Brief Introduction to Propositional and validation. Logic A final note on the software It is often difficult to know for certain, development cycle is that the model shown that a given algorithm is correct, especially if above is not discreet. These phases are loosely that algorithm is particularly lengthy. In this defined and occur on a continuum, commonly situation, mathematics provides some certainty overlapping in the real world. in determining an algorithm’s correctness, specifically with a branch of mathematics known 1.3 Limitations of Validation as propositional logic. The primary component of the software Like all forms of logic, propositional development cycle this paper will scrutinize is logic concerns mathematical proofs. validation. When writing tests, programmers Propositional variables, or atoms, denoted by often use ad hoc logic to convince themselves capital letters (A, B, X) represent true or false that their code is correct. It is not feasible to test conditions and make up statements, or all inputs for nontrivial programs, and thus formulae, in propositional logic. More complex validation tests usually check a limited number conditions are derived from these simple of cases meant to represent a sample of probable statements using relationships, or connectors, inputs. If a programmer’s code passes these such as a disjunction, conjunction, negation, etc. tests, they believe that their implementation is (Figure 2). correct, even though there is insufficient evidence to make such a claim. Conjunction: A B denotes A AND B Furthermore, debugging, which is the Disjunction: A B denotes A OR B process of fixing erroneous programs in order to Negation: ¬A denotes∧ NOT A satisfy its tests, is a time-consuming and costly Implication: A∨ B denotes A IMPLIES B, equivalent process. Programmers spend as much as 49.9% to ¬A B ⇒ of their time debugging their code, accounting x denotes FOR ALL “X” ∨ for a total of $156 billion worldwide.5 x denotes There EXISTS a variable X ∀ Figure 2. Notation of Propositional Logic ∃ 1.4 Formal Verification The formulae created by these atoms Various resources are funneled into and connectives can be evaluated, and the truth writing correct programs, yet the standard values of these formulae, can be found by method used in software development lacks trivially following the relationships that the mathematical rigor. An effort introduced to connectives state. In some cases, a truth table remedy this discrepancy is formal verification. may be used to determine all possible truth Formal verification is a systematic approach to values of a formula, given different proving that a program is correct with regards to combinations of truth states of each atom (Table its specification.6 This paper will explore the 2). methodology of formal verification and demonstrate its current capabilities in determining a program’s correctness through a cutting-edge verification tool called Dafny. 3 2.3 Hoare Logic and Defining Correctness True is denoted with a 1 False is denoted with a 0 In the late 1960s, Sir Charles Antony Richard Hoare formalized a model for reasoning about the correctness of programs. The model, A B ¬A A ∧ B A B ⇒ which came to be known as Hoare logic, outlined 0 0 1 0 1 a schema for proving that a program implementation adheres to its specifications. 0 1 1 0 1 The general methodology behind a Hoare logic 1 0 0 0 0 proof of a program is to: 1. Start with a set of axioms that are assumed 1 1 0 1 1 to be true 2. Using propositional logic, develop a series of Figure 3. A truth table a few formula claims from these axioms and the computer code of the program Mathematical principles of substitution, 3. Demonstrate that the program’s double-negation, commutativity, associativity, postconditions are true at the end of the and distributivity can aid in the construction or program deconstruction of propositional formulae. Through this application of propositional logic Moreover, with the use of these principles, to computer programs, Hoare helped to define complex formulae, such as ¬(A ∧ B) ∨ C ⇒D, correctness by connecting a program’s can be broken down into more fundamental preconditions (P), code (Q), and postconditions formulae, A ∨ B ∨ ¬C ∨ D, and vice versa.7 (R). The principles of propositional logic, while having applications in mathematics, also “If the assertion P is true before the initiation of a have a relevant application in computer science. program Q, then the assertion R will be true on its Propositional logic forms the basis of automatic completion.”8 This relationship is called a Hoare theorem provers and other code verification Triple, and is denoted as: P {Q} R tools. Figure 4. Hoare Triples 2.2 Specifications, Preconditions and A rigorous definition of correctness can be Postconditions obtained from the definition of a Hoare triplet. Concerning correctness from a programmer’s perspective, specifications are used to define a program’s behavior with A program that executes code Q, with a specification particular sets of preconditions and consisting of a set of preconditions P and set of * postconditions. Preconditions are assumptions postconditions R, and terminates is correct with respect to its specification {P, R} if: that must be true when a program is invoked and postconditions are claims that must hold P ∧ Q ⇒ R true after a program terminates. For the Figure 5. Defining Correctness purposes of verification, it is important to write meaningful specifications that characterize the behavior of a program. Postconditions should reflect a defining property or relationship of a * Hoare logic can only prove the partial correctness of a program’s manipulated data after it is executed program, as opposed to the total correctness of a program. A and preconditions should ensure that these Hoare triplet makes no claim that the code Q terminates. If postconditions could be reached. Specifications the program never terminates, then it is impossible to assert the postconditions R.