A CIL Tutorial - Department of Computer Science - ETH Zürich

A CIL Tutorial - Department of Computer Science - ETH Zürich A CIL Tutorial - Department of Computer Science - ETH Zürich

29.01.2014 Views

CHAPTER 11. PROGRAM VERIFICATION 92 11.1 tut11.ml This module is organized as follows. First, we dene types and functions for initializing and maintaining the state needed for translation of the VC into the Term language of Why3. Then, we dene functions for calculating the VC for the elements of the CIL AST and translating them into the Why3 Term language. We begin with attribute parameters, needed for the precondition, postcondition, and loop invariant syntax that we add to C. This is followed by the translation of expressions into Why Terms, which is similar. Next, using the Why3 Terms, we dene functions to generate the VC for instructions, statements, blocks, and functions. Finally, we dene a function that wraps up the generated VC into a proof goal that we pass on to one of Why3's provers. module W = Why3 (∗ We introduce aliases for the Why3 module and some of its sub-modules. ∗) module T = W.Term (∗ The Why3 library for constructing terms. ∗) module Th = W.Theory (∗ The Why3 library of theories. ∗) We use the ops record type to store function symbols of the Why3 theories that we'll employ. In particular we'll use its theories of integer arithmetic, computer division, and maps. type ops = { iplus op : T.lsymbol; (∗ Integer addition ∗) iminus op : T.lsymbol; (∗ Integer subtraction ∗) itimes op : T.lsymbol; (∗ Integer multiplication ∗) idiv op : T.lsymbol; (∗ Computer integer division ∗) imod op : T.lsymbol; (∗ Computer integer modulus ∗) lt op : T.lsymbol; (∗ Integer comparisons ∗) (∗ ... ∗) get op : T.lsymbol; (∗ get and set for maps ∗) set op : T.lsymbol; } We use the type wctxt to store context for Why3. The rst four elds, which we omit here, store the internal conguration of Why3, the theories it will use, and information about the back-end prover it will use. The ops eld stores the symbols described above. The memory eld stores a variable of Why3 type (map int int) that we'll use to model memory accesses. The vars eld is a map from strings to Why3 symbols for the variables that are mentioned in the Why3 Term being constructed. type wctxt = { (∗...∗) mutable ops : ops; mutable memory : T.vsymbol; mutable vars : T.vsymbol SM.t; } The initOps function extracts the Why3 symbols for the operations from the Why3 theories that we are using.

CHAPTER 11. PROGRAM VERIFICATION 93 let initOps (it : Th.theory) (dt : Th.theory) (mt : Th.theory) : ops = {iplus op = Th.ns find ls it.Th.th export ["infix +"]; iminus op = Th.ns find ls it.Th.th export ["infix -"]; (∗...∗) } The function initWhyCtxt initializes the Why3 context and directs it to use the prover specied by the string p. It uses the Why3 API to read its conguration, load its plug-ins, nd the specied prover, and load the theories we'll need into the wctxt. let initWhyCtxt (p : string) (pv : string) : wctxt = (∗ ... ∗) As with the other attributes in previous chapters, we introduce global variables for string constants for the attribute syntax. let invariantAttrStr = "invariant" let postAttrStr = "post" let preAttrStr = "pre" let tut11 attrs = [invariantAttrStr; postAttrStr; preAttrStr; ] The functions term of int and term of int64 convert OCaml integers (which we'll extract from the CIL AST) into Why3 terms. let term of int (i : int) : T.term = i | > string of int | > T.t int const let term of i64 (i : int64) : T.term = i | > Int64.to string | > T.t int const We'll use the functions make symbol and freshvar of ap to make fresh symbols for the variables bound by quantiers that appear in attribute parameters. let make symbol (s : string) : T.vsymbol = T.create vsymbol (W.Ident.id fresh s) W.Ty.ty int let freshvar of ap (ap : attrparam) : string × T.vsymbol = match ap with | ACons(n, [ ]) → n, make symbol n | → Em.s(Em.error "Names only") The function term of attrparam converts an attribute parameter into a Why3 term. For variable references, it looks up symbols in the vars eld of the context. For binary and unary operations, it uses the operations dened in the ops eld. For memory references it uses the get and set operations and the memory eld of the context to generate the appropriate Why3 terms. In particular, memory is modeled as one large array indexed

CHAPTER 11. PROGRAM VERIFICATION 92<br />

11.1 tut11.ml<br />

This module is organized as follows. First, we dene types and functions for initializing and maintaining<br />

the state needed for translation <strong>of</strong> the VC into the Term language <strong>of</strong> Why3. Then, we dene<br />

functions for calculating the VC for the elements <strong>of</strong> the <strong>CIL</strong> AST and translating them into the<br />

Why3 Term language. We begin with attribute parameters, needed for the precondition, postcondition,<br />

and loop invariant syntax that we add to C. This is followed by the translation <strong>of</strong> expressions<br />

into Why Terms, which is similar. Next, using the Why3 Terms, we dene functions to generate the<br />

VC for instructions, statements, blocks, and functions. Finally, we dene a function that wraps up<br />

the generated VC into a pro<strong>of</strong> goal that we pass on to one <strong>of</strong> Why3's provers.<br />

module W = Why3 (∗ We introduce aliases for the Why3 module and some <strong>of</strong> its sub-modules. ∗)<br />

module T = W.Term (∗ The Why3 library for constructing terms. ∗)<br />

module Th = W.Theory (∗ The Why3 library <strong>of</strong> theories. ∗)<br />

We use the ops record type to store function symbols <strong>of</strong> the Why3 theories that we'll employ. In<br />

particular we'll use its theories <strong>of</strong> integer arithmetic, computer division, and maps.<br />

type ops = {<br />

iplus op : T.lsymbol; (∗ Integer addition ∗)<br />

iminus op : T.lsymbol; (∗ Integer subtraction ∗)<br />

itimes op : T.lsymbol; (∗ Integer multiplication ∗)<br />

idiv op : T.lsymbol; (∗ <strong>Computer</strong> integer division ∗)<br />

imod op : T.lsymbol; (∗ <strong>Computer</strong> integer modulus ∗)<br />

lt op : T.lsymbol; (∗ Integer comparisons ∗)<br />

(∗ ... ∗)<br />

get op : T.lsymbol; (∗ get and set for maps ∗)<br />

set op : T.lsymbol;<br />

}<br />

We use the type wctxt to store context for Why3. The rst four elds, which we omit here, store the<br />

internal conguration <strong>of</strong> Why3, the theories it will use, and information about the back-end prover<br />

it will use. The ops eld stores the symbols described above. The memory eld stores a variable <strong>of</strong><br />

Why3 type (map int int) that we'll use to model memory accesses. The vars eld is a map from<br />

strings to Why3 symbols for the variables that are mentioned in the Why3 Term being constructed.<br />

type wctxt = {<br />

(∗...∗)<br />

mutable ops : ops;<br />

mutable memory : T.vsymbol;<br />

mutable vars : T.vsymbol SM.t;<br />

}<br />

The initOps function extracts the Why3 symbols for the operations from the Why3 theories that<br />

we are using.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!