VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation...

27
VCC: A Practical System for Verifying Concurrent C Ernie Cohen 1 , Markus Dahlweid 2 , Mark Hillebrand 3 , Dirk Leinenbach 3 , Michal Moskal 2 , Thomas Santen 2 , Wolfram Schulte 4 and Stephan Tobies 2 1 Microsoft Corporation, Redmond, WA, USA 2 European Microsoft Innovation Center, Aachen, Germany 3 German Research Center for Artificial Intelligence (DFKI), Saarbrucken, Germany 4 Microsoft Research, Redmond, WA, USA May 6, 2013 Pavol Bielik

Transcript of VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation...

Page 1: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

VCC: A Practical System for Verifying Concurrent C

Ernie Cohen1, Markus Dahlweid2, Mark Hillebrand3, Dirk Leinenbach3,Michal Moskal2, Thomas Santen2, Wolfram Schulte4 and Stephan

Tobies2

1Microsoft Corporation, Redmond, WA, USA2European Microsoft Innovation Center, Aachen, Germany

3German Research Center for Artificial Intelligence (DFKI), Saarbrucken, Germany4Microsoft Research, Redmond, WA, USA

May 6, 2013

Pavol Bielik

Page 2: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Introduction

I VCC stands for Verifying C Compiler

I deductive verifier for concurrent C code

I performs static modular analysis and sound verificationof functional properties of low-level concurrent C code

I VCC translates annotated C code into BoogiePLI Boogie translates BoogiePL into verification conditionsI Z3 solves them or gives couterexamples

Pavol Bielik Research Topics in Software Engineering May 6, 2013 2 / 27

Page 3: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Motivation

I driven by the verification of the Microsoft Hyper-V hypervisor

I the hypervisor turns a single real multi-processor x64 machine into anumber of virtual multiprocessor x64 machines

I own concurrency control primitives, complex data structures anddozens of tricky optimizations

I written in no verification in mind (100KLOC of C, 5KLOC ofassembly)

I performance was of a great importance

Pavol Bielik Research Topics in Software Engineering May 6, 2013 3 / 27

Page 4: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Function Contracts

Specification (Contract) consisting of four kinds of clauses

I preconditions: (requires P)

I postconditions: (ensures Q)

I writes clause: (writes S)

I termination: (decreases T )

Modular – only looks at function specification

Pavol Bielik Research Topics in Software Engineering May 6, 2013 4 / 27

Page 5: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Function Contracts

int min(int a, int b)

_(requires \true)

_(ensures \result <= a && \result <= b)

_(ensures \result == a || \result == b)

{

if (a <= b)

return a;

else return b;

}

Pavol Bielik Research Topics in Software Engineering May 6, 2013 5 / 27

Page 6: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Pure functions

_(pure) int min(int a, int b) ...

I no side effects on programs state

I not allowed to allocate memory

I can only write to local variables

I can be called within VCC annotations

However:

I empty writes clause ; pure

Pavol Bielik Research Topics in Software Engineering May 6, 2013 6 / 27

Page 7: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Pure Ghost functions

I used only in specification

I along with other annotations removed before compilation bypreprocessor

_(def \bool sorted(int *arr , unsigned len) {

return \forall unsigned i, j;

i <= j && j < len ==> arr[i] <= arr[j];

})

void sort(int *arr , unsigned len)

_(writes \array_range(arr , len))

_(ensures sorted(arr , len))

Pavol Bielik Research Topics in Software Engineering May 6, 2013 7 / 27

Page 8: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Type Invariants

I object invariants can be associated with compound types (structs andunions)

I support for both single and two-state predicates

#define SSTR_MAXLEN 100

typedef struct SafeString {

unsigned len;

char content[SSTR_MAXLEN + 1];

_(invariant \this ->len <= SSTR_MAXLEN)

_(invariant content[len] == ’\0’)

} SafeString;

Pavol Bielik Research Topics in Software Engineering May 6, 2013 8 / 27

Page 9: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Type Invariants

I object invariants cannot hold at all times, e.g.:I initializationI destructionI updates

I an object can be in two states controlled by \closed ghost field

I closed – invariant holds but non-volatile fields can not be changedI open – invariant can not be assumed, but fields can be changed

I closedness is manipulated using \wrap and \unwrap helper methods

I type invariants are coupled with ownership

Pavol Bielik Research Topics in Software Engineering May 6, 2013 9 / 27

Page 10: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Ownership

I ownership is expressed by adding a ghost field to every object andmaking it point to object owner

I the roots of trees in the ownership forest are objects representingthreads of execution.

I threads are always closed, and own themselves

I the set of objects directly or transitively owned by an object is calledthe ownership domain of that object

Pavol Bielik Research Topics in Software Engineering May 6, 2013 10 / 27

Page 11: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Ownership

I type invariants are coupled with ownership

I if an object is owned by a thread, only that thread can change its(nonvolatile) fields (and then only if the object is open), wrap orunwrap it, or change its owner to another object

mu t ab le

!closed(o)owner(o)==me()

w r ap p ed 0

closed(o)owner(o)==me()

nest ed

closed(o)owner(o)==o'o' !=me()

wrap(o)

unwrap(o)

unwrap(o' ) whereo ∈owns(o' ) or

giveup closed owner(o,o' )

wrap(o' ) whereo ∈owns(o' ) or

set closed owner(o,o' )

Pavol Bielik Research Topics in Software Engineering May 6, 2013 11 / 27

Page 12: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Wrapping & Unwrapping Example

1 void sstr_append(struct SafeString *s, char c)

2 _(maintains \wrapped(s))

3 _(requires s->len < SSTR_MAXLEN)

4 _(ensures s->len == \old(s->len) + 1)

5 _(ensures s->content [\old(s->len)] == c)

6 ...

7 _(writes s)

8 {

9 _(unwrap s)

10 s->content[s->len ++] = c;

11 s->content[s->len] = ’\0’;

12 _(wrap s)

13 }

Pavol Bielik Research Topics in Software Engineering May 6, 2013 12 / 27

Page 13: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Verifying Concurrent Programs

I coarse-grained concurrency - ownership based

I fine-grained concurrency - atomic actions on volatile fields

Pavol Bielik Research Topics in Software Engineering May 6, 2013 13 / 27

Page 14: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Volatile fields

C meaning:

I value can be changed from “outside”

I prevents compiler from storing value in registers

VCC meaning:

I field can be modified while the object is closed, as long as the updateoccurs inside an explicit atomic action that preserves the objectinvariant

I thread forgets the values of these fields when it makes an impurefunction call and just before an atomic action

Pavol Bielik Research Topics in Software Engineering May 6, 2013 14 / 27

Page 15: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock

I ownership used to control access to shared resource

1 _(volatile_owns) struct Lock {

volatile int locked;

_(ghost \object protected_obj ;)

_(invariant locked == 0 ==> \mine(protected_obj ))

5 };

Pavol Bielik Research Topics in Software Engineering May 6, 2013 15 / 27

Page 16: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock - Initialization

1 void InitializeLock(struct Lock *l

_(ghost \object obj))

_(requires \wrapped(obj))

_(ensures \wrapped(l) && l->protected_obj == obj )

5 _(ensures \nested(obj))

_(writes \span(l), obj)

{

l->locked = 0;

_(ghost {

10 l->protected_obj = obj;

l->\owns = {obj};

_(wrap l)

})

}

Pavol Bielik Research Topics in Software Engineering May 6, 2013 16 / 27

Page 17: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock - Acquire

void Acquire(struct Lock *l)

_(maintains \wrapped(l))

_(ensures l->locked == 1)

_(ensures \wrapped(l->protected_obj) && \fresh(l->protected_obj ))

{

int stop = 0;

do {

_(atomic l) {

stop = InterlockedCompareExchange (&l->locked , 1, 0) == 0;

_(ghost if (stop) l->\owns -= l->protected_obj)

}

} while (!stop);

}

Pavol Bielik Research Topics in Software Engineering May 6, 2013 17 / 27

Page 18: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock - Release

void Release(struct Lock *l)

_(maintains \wrapped(l))

_(requires l->locked == 1)

_(requires \wrapped(l->protected_obj ))

_(ensures l->locked == 0

_(ensures \nested(l->protected_obj ))

_(writes l->protected_obj)

{

_(atomic l) {

l->locked = 0;

_(ghost l->\owns += l->protected_obj)

}

}

Pavol Bielik Research Topics in Software Engineering May 6, 2013 18 / 27

Page 19: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock - Problems

I acquire and release require that the lock is closed

void Acquire(struct Lock *l)

_(requires \wrapped(l)) ...

I definition of wrapped:

\bool \wrapped (\ object o)

_(ensures \result <==> o->\owner == \me && o->\closed)

I o → \owner == \me is satisfiable by only single thread

Pavol Bielik Research Topics in Software Engineering May 6, 2013 19 / 27

Page 20: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Claims

1 _(ghost typedef struct {

\ptrset claimed;

_(invariant \forall \object o; o \in claimed ==> o->\closed)

} \claim; )

I Ownership meta-states updated:

mu t ab le

!closed(o)owner(o)==me()ref_cnt(o)==0

w r ap p ed 0

closed(o)owner(o)==me()ref_cnt(o)==0

w r ap p ed

closed(o)owner(o)==me()ref_cnt(o)==1

nest ed

closed(o)owner(o)==o'o' !=me()

ref_cnt(o)==0

nest ed

closed(o)owner(o)==o'o' !=me()

ref_cnt(o)==1

wrap(o)

unwrap(o)

claim(o,)

unclaim( ,o,)

.

.

.

.

.

.

claim(o,)

unclaim( ,o,)

unwrap(o' ) whereo ∈owns(o' ) or

giveup closed owner(o,o' )

wrap(o' ) whereo ∈owns(o' ) or

set closed owner(o,o' )

C on cu r r en tSequ en t i al

Pavol Bielik Research Topics in Software Engineering May 6, 2013 20 / 27

Page 21: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Spin-lock revisited

1 void Release(struct Lock *l _(ghost \claim c))

_(maintains \wrapped(c) && \claims_object(c, l))

_(requires l->locked == 1 )

_(requires \wrapped(l->protected_obj ))

5 _(ensures l->locked == 0 )

_(ensures \nested(l->protected_obj ))

_(writes l->protected_obj)

{

_(atomic c, l) {

10 l->locked = 0;

_(ghost l->\owns += l->protected_obj)

}

}

Pavol Bielik Research Topics in Software Engineering May 6, 2013 21 / 27

Page 22: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Invariant contraints

I what part of state are invariants allowed to mentionI how to make sure that updates dont break out of scope invariants?I VCC allows invariants to mention arbitrary parts of the state, but

requires them to be admissibleI VCC checks that no object invariant can be broken by

invariant-preserving changes to other objects

struct Counter {

int n;

_(invariant n = old(n) ||

n = old(n) + 2)

};

struct Low {

Counter cnt;

int floor;

_(invariant floor <= cnt.n)

};

struct High {

Counter cnt;

int ceiling;

_(invariant cnt.n <= ceiling)

};

Pavol Bielik Research Topics in Software Engineering May 6, 2013 22 / 27

Page 23: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Locally checked invariant

I VCC enforces that all invariants are reflexive.

refl(τ) ≡ ∀p, ho , h.type(p) = τ ∧ invτ (ho , h, p)⇒ invτ (h, h, p)

I an action is legal iff it preserves the invariants of updated objects

legal(ho , h) ≡ safe(ho)⇒ ∀p.ho [p] = h[p] ∨ inv(ho , h, p)

I a stable invariant is one that cannot be broken by legal actions

stable(τ) ≡ ∀p, hoh.type(p) = τ∧safe(ho)∧legal(ho , h)⇒ invτ (ho , h, p)

I An admissible invariant is one that is stable and reflexive

adm(τ) ≡ stable(τ) ∧ refl(τ)

Pavol Bielik Research Topics in Software Engineering May 6, 2013 23 / 27

Page 24: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

VCC Workflow

Annotate C code

Compile with regular C compiler

Verify with VCC

erified Executable Error

Inspect counterexample with Model Viewer

Fix code or specs with VCC VS plugin

Timeout

Inspect Z3 log with Z3 Visualizer

source: http://research.microsoft.com/en-us/projects/vcc/Pavol Bielik Research Topics in Software Engineering May 6, 2013 24 / 27

Page 25: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Microsoft Hyper-V hypervisor Experience

I implementation of the SPT algorithm contains ≈ 700 lines of C code

I ≈ 4000 lines of the annotations

I overall proof time is ≈ 18 hours on one core of 2GHz Intel Core 2Duo machine

I most functions in 0.5 to 500 seconds with an average of ≈ 25 seconds

I estimated person effort is ≈ 1.5 person-years, including VCC learningperiod

Pavol Bielik Research Topics in Software Engineering May 6, 2013 25 / 27

Page 26: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

Conclusion

I VCC follows largely the design of Spec#

I expressive enough for industrial program verification

I lot’s of helper methods

I up to the user to guarantee that access annotated as \atomic isindeed atomic

I assumes sequential consistency

Future work

I incorporate x86 memory model

I annotation overhead

I performance

Pavol Bielik Research Topics in Software Engineering May 6, 2013 26 / 27

Page 27: VCC: A Practical System for Verifying Concurrent C Boogie translates BoogiePL into veri cation conditions ... I object invariants can be associated with compound types ... A practical

References

Ernie Cohen, Markus Dahlweid, Mark Hillebrand, et. al.,“VCC: A practical system for verifying concurrent C,”Artificial Intelligence, vol. 5674, no. 1753, pp. 23–42, 2009.

Ernie Cohen, Micha l Moskal, Wolfram Schulte, and Stephan Tobies,“Local Verification of Global Invariants in Concurrent Programs,”Innovation, vol. 6174, pp. 480–494, 2010.

Ernie Cohen and Mark A Hillebrand,“The VCC Manual & Verifying C Programs : A VCC Tutorial,”Tech. Rep., 2012.

Ernie Cohen, Micha l Moskal, Stephan Tobies, and Wolfram Schulte,“A precise yet efficient memory model for C,”Electron. Notes Theor. Comput. Sci., vol. 254, pp. 85–103, Oct. 2009.

Pavol Bielik Research Topics in Software Engineering May 6, 2013 27 / 27