Inferring Invariants in Separation Logic for Imperative List-Processing Programs ∗

Inferring Invariants in Separation Logic for Imperative List-Processing Programs ∗

Inferring Invariants in Separation Logic for Imperative List-processing Programs ∗ Stephen Magill Aleksandar Nanevski Edmund Clarke, Carnegie Mellon University Harvard University Peter Lee [email protected] [email protected] Carnegie Mellon University [email protected], [email protected] ABSTRACT tions and strongest postconditions becomes exponential in An algorithm is presented for automatically inferring loop the number of program variables. This can be ameliorated invariants in separation logic for imperative list-processing somewhat by utilizing a pointer analysis to rule out certain programs. A prototype implementation for a C-like lan- cases, but the results are often unsatisfactorily weak, partic- guage is shown to be successful in generating loop invariants ularly when allocation and deallocation are involved. Any for a variety of sample programs. The programs, while rela- program analysis must also take into account the global con- tively small, iteratively perform destructive heap operations text, since any two pointer variables, regardless of scope, are and hence pose problems more than challenging enough to potential aliases. demonstrate the utility of the approach. The invariants Contrast this with separation logic [14, 19], a program express information not only about the shape of the heap logic with connectives for expressing aliasing patterns and but also conventional properties of the program data. This which provides concise weakest preconditions even for pointer combination makes it possible, in principle, to solve a wider operations. Separation logic also supports compositional range of verification problems and makes it easier to incorpo- reasoning. As such, it seems a promising foundation upon rate separation logic reasoning into static analysis systems, which to build all sorts of static analysis methods and tools. such as software model checkers. It also can provide a com- In this paper we consider the problem of automatically ponent of a separation-logic-based code certification system inferring loop invariants in separation logic for imperative a la proof-carrying code. list-processing programs. Our primary motivation for do- ing this is to provide a key component of a general veri- fication system for imperative programs that make use of 1. INTRODUCTION pointers. Until recently, automated reasoning in separation Automated program verification is a large and active field, logic has been largely unexplored. However, Berdine, et. with substantial research devoted to static analysis tools. al. [2] have presented a decision procedure for a fragment of However, these tools are based mostly on classical logic. separation logic that includes a list predicate. Weber [22] This places a large burden on the verification procedure, par- has developed an implementation of separation logic in Is- ticularly in the practical case of programs involving pointers. abelle/HOL with support for tactics-based reasoning. These Because classical logic contains no primitives for expressing approaches allow for the automatic or semi-automatic verifi- non-aliasing, all aliasing patterns must be considered when cation of programs annotated with loop invariants and pre- doing the program analysis. Computing weakest precondi- /post-conditions. But what is missing is the generation of the loop invariants. Sims [21] has proposed an extension to ∗ This research was sponsored by the National Science separation logic that allows for the representation of fixed Foundation under grant nos. ITR/SY+SI 0121633, CNS- points in the logic, and thus the computation of strongest 0411152, CCF-0429120, CCR-0121547, and CCR-0098072, postconditions for while loops. But the issue still remains the US Army Research Office under grant no. DAAD19-01- 1-0485, and the Office of Naval Research under grant no. of how to compute these fixed points. N00014-01-1-0796. The views and conclusions contained in We present a heuristic method for inferring loop invari- this document are those of the author and should not be ants in separation logic for iterative programs operating on interpreted as representing the official policies, either ex- integers and linked lists. The invariants are obtained by ap- pressed or implied, of any sponsoring institution, the U.S. plying symbolic evaluation [6] to the loop body and then government or any other entity. applying a fold operation, which weakens the result such that when this process is repeated, it eventually converges. Our symbolic execution algorithm is similar to that in [3] and our invariant inference technique has much in common with ongoing and independent work by Distefano, O’Hearn, Permission to make digital or hard copies of all or part of this work for personal or classroom use is granted without fee provided that copies are and Yang. However, our invariants are capable of express- not made or distributed for profit or commercial advantage and that copies ing not only information about the structure of the graph of bear this notice and the full citation on the first page. To copy otherwise, to pointers (shape information), but also facts about program republish, to post on servers or to redistribute to lists, requires prior specific data, both on the stack and in the heap. This ability to permission and/or a fee. reason about data differentiates our work from these other . approaches and, as we will show, is quite desirable for cer- Locations L tain programs. Addressable Locns A = L − {null} Once we have loop invariants, the way is paved for the Integers adaptation of existing program analysis techniques (such as Z Values V = L ∪ Z software model checking [1, 5, 13]) to the separation logic Variables Ω framework. Applications to code certification, for example Stacks S = Ω → V via proof-carrying code [15, 16], would also be enabled by S Heaps H = fin (D → V) this. D ⊆A We have implemented a prototype of our algorithm. Us- ing it, we are able to extract loop invariants automatically Figure 1: Semantic domains for a number of pointer programs. These examples, while rather small, are iterative and perform destructive heap op- erations. Thus, they are more than challenging enough to Variables x, y ∈ Ω demonstrate the utility of our algorithm. Integers n ∈ Z There are many existing approaches to the treatment of Integer Expns i ::= x | n | i1 + i2 | i1 − i2 | i1 × i2 shape information in iterative programs [10, 20]. It is not Pointer Expns k ::= x | x.n | null our intent here to try to beat these techniques. We do not Boolean Expns b ::= i1 = i2 | k1 = k2 | attempt to quantify the relative strength of our inference i1 < i2 | ¬P | P1 ∧ P2 | procedure versus others. We wish merely to present an al- P1 ∨ P2 | > | ⊥ ternate approach, as we believe that the development of a Expressions e ::= i | b | k procedure based on separation logic has merit on its own. In Heap Predicates h ::= k 7→ e | emp particular, the compositionality of separation logic proofs is Pure Formulas p, q ::= b | p ∧ q | p ∨ q | p ⇒ q | ¬p | vital if such proofs are to be used in a proof-carrying code p ∗ q | p −∗ q | ∀x. p | ∃x. p system. In such systems, the proof is written by the code Formulas r, s ::= b | h | r ∧ s | r ∨ s | producer, but the code runs on the client machine in a differ- r ⇒ s | ¬r | r ∗ s | r −∗ s | ent and unpredictable context. As such, the proofs cannot ∀x. r | ∃x. r depend on the global variables and aliasing patterns present on the client. Separation logic allows us to construct such Figure 2: Separation logic syntax modular proofs. In section 2 we present a brief introduction to separation logic. In section 3, we describe the algorithm, which consists predicates. Recall that the locations are ordered. x.n stands of a symbolic evaluation routine and heuristics for finding for the nth location after x in this ordering. null is the non- fixed points. In section 4 we show the soundness of our addressable element of L. The heap predicate emp states approach, and in section 5, we discuss incompleteness and that the heap is empty (its domain is the empty set). The give an example on which our algorithm fails. Finally, in predicate e1 7→ e2 indicates that the heap contains a single section 6 we give the results of our tests involving an SML cell at location e1, which contains the value given by e2. implementation of the algorithm. Heaps with more than one cell are represented by combining these predicates with ∗. The formula p ∗ q holds if the heap can be split into two disjoint parts, one of which satisfies p 2. SEPARATION LOGIC while the other satisfies q. So for example, a heap containing Here we present a brief overview of separation logic. For two cells, one at location x and one at location y could be a full treatment, see [19]. The logic consists of all the con- represented by the formula (x 7→ 3) ∗ (y 7→ 4). There is nectives and quantifiers from classical logic, plus two new a special syntactic class of pure formulas, which are those connectives: a separating conjunction (written p ∗ q) and a formulas that do not contain heap predicates. Pure formulas separating implication (p −∗ q). Only separating conjunction are independent of the heap. We use the abbreviation e1 7→ will be used for the material in this paper. Figure 1 gives (e2, e3) to stand for (e1 7→ e2) ∗ (e1.1 7→ e3). Thus, e1 7→ the domains involved in the semantics of the logic. There is (e2, e3) can be read as saying that there is a pair of values a set of locations, L, which is ordered and disjoint from the (e2, e3) in the heap starting at location e1. set of integers, Z. The set of values consists of the union We use a fragment of separation logic for our invariants of L and Z.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    14 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