Top Banner
26.8.2004 Model Checking C++ Daniel Kroening
26

26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

Dec 18, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004

Model Checking C++

Daniel Kroening

Page 2: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 2

Warning!

No new research in this talk

Talk is about doing existing stufffor different language only

You might consider this trivial and nod off

!

Page 3: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 3

Example from NASA JPLclass RCPNAM{

public:

RCPNAM(); //!< DND for STRICT protocol.

~RCPNAM(); //!< Decr cntr, delete if zero.

RCPNAM(RCPNOD* p); //!< New ptr at given object.

RCPNOD* operator->();

RCPNOD& operator*();

RCPNAM(const RCPNAM& rhs); //!< Copy of existing RCPNAM.

[…]

RCPNAM& operator=(const RCPNAM& rhs); //!< Assign to existing RCPNAM.

int nullp(){return ref_ == 0;}; //!< DND for STRICT.

[…]

public:

RCPNOD* ref_;

static RCPNOD* copyAndInc(const RCPNOD*const* p,int RCPNOD::*cntr);

};

Page 4: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 4

Example from NASA JPL

Want to verify the code that goes into space

Container class with reference counting

Concurrent

Mostly low-level, performance-orientedC and C++

Uses assembly-language constructs foratomic accesses to pointers and counters

Page 5: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 5

Example from NASA JPL

Verification: Extensive testing SPIN failed

Main challenge: pointers and references

How many threads are enough?

Page 6: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 6

Outline

1. Frontend• What’s so hard about C++?• Parsing, Type Checking• STL

2. Backend• Verification Backends• Dynamic Objects

Page 7: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 7

What’s so hard about C++?

Existing model checkers:compile, and work on binary

Infrastructure is difficult Parsing is complicated (LRk) Complex name resolution rules

(namespaces, templates, class hierarchy, overloading)

But: there are tools that flatten C++ to C

Page 8: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 8

Why C++?

Main advantage of C++:

Encapsulation of complex data structuresin template libraries

Also main challenge whenmodel checking C++

Page 9: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 9

What’s so hard about C++?

What kind of C++ are we going to see? Heavy use of references and pointers Class/Module hierarchy Overloading Templates new/delete

Page 10: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 10

Model Extraction for C++

ParserParser

Type CheckerType Checker

CFG-GeneratorCFG-Generator

Backend

. . .

Frontend

Page 11: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 11

Type Checker

Parse tree to symbol table Both represented as DAGs Algorithm:

1. Expand templates2. Resolve overloading3. Class hierarchy4. Annotate each sub-expression with type

Page 12: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 12

Type Checker

cpp::f(9_reference(7_subtype=8_signedbv(5_width=2_32))) type: code * arguments: 0: argument * type: reference * subtype: signedbv * width: 32 * return_type: empty value: code * statement: block * type: code * arguments: 0: argument * type: reference * subtype: signedbv * width: 32 * return_type: empty

void f(int &r) { }

Page 13: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 13

Control Flow Graph

Symbol table to Control Flow Graph (CFG) Essentially a guarded GOTO program,

but with direct function calls virtual methods and virtual classes and

function pointers require alias analysis

Page 14: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 14

Alias Analysis

For References Pointers virtual tables

Fixed-point iteration on the CFG Interleaved with the computation of the CFG

Control flow sensitive (concurrency!)

Field sensitive

Page 15: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 15

Alias Analysis

int var;

void f() { var=1;}

void g() { var=2;}

int main() { bool c; void (*p)()=c?f:g; (*p)();}

MAIN: INIT var = 0; main()

cpp::main(): p = c ? f() : g(); (*p)();

cpp::main()::1::p = { &f, &g }

Page 16: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 16

Alias Analysis

int var;

void f() { var=1;}

void g() { var=2;}

int main() { bool c; void (*p)()=c?f:g; (*p)();}

MAIN: INIT var = 0; main();

cpp::f(): var = 1;

cpp::g(): var = 2;

cpp::main(): p = c ? f : g; IF p != &g THEN GOTO 1 g(); GOTO 2 1: f(); 2: SKIP

Page 17: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 17

STL

Standard Template Library

Encapsulates complex data structures and algorithms

typedef std::hash_map <std::string, symbolt, string_hash> symbolst;

. . .

typedef std::vector<nodet> nodest;

Page 18: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 18

STL

“Interesting” programs using STLhave > 1000 data structures

Flatten to C? STL implementation highly complex and

optimized Don’t want to verify STL together with

program

Let’s assume STL is correct

Page 19: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 19

Model Extraction for C++

ParserParser

Type CheckerType Checker

CFG-GeneratorCFG-Generator

Backend

. . .

Frontend

Page 20: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 20

Model Extraction with the STL

ParserParser

Type CheckerType Checker

CFG-GeneratorCFG-Generator

Backend

. . .

Frontend

Inject modifications of class definitions

Page 21: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 21

Abstract STL

Manually written abstractions of common STL data types std::vector std::list std::set std::map

Catch errors when using STL

Catch errors in program thatdepend on data in containers

Page 22: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 22

STL

typedef std::vector<…> T; T v;

v.push_back(…); v.reserve(2);

T::const_iterator it=v.begin();

x=*it;

v.push_back(…);

x=*it;

Page 23: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 23

Predicate Abstraction

Predicate Abstraction is a successful method to verify programs

C/C++Program

with threads

C/C++Program

with threads

ConcurrentBooleanProgram

ConcurrentBooleanProgram

ModelChecker

VerificationInitialAbstraction

No erroror bug found

Simulator

Propertyholds

Simulationsuccessful

Bug found

Refinement

Page 24: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 24

Dynamic Objects

C++ code tends to make excessive use of dynamic objects

Algorithm: Allow * and & in predicates,

including pointer arithmetic New: also have quantifiers 8, 9 Maintain active bit (o)

and object size state variables Flow control-sensitive points-to analysis

Page 25: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 25

Dynamic Objects

struct s { s *n; int i; } *p; ... p=new s; p->n=new s; p->n->i=p->i+1;

(*p)(*p), (*(p->n))p->n->i=p->i+1

Postconditions

(*p)(*p),(*(p->n))

Preconditions

Page 26: 26.8.2004 Model Checking C++ Daniel Kroening. 26.8.2004 Daniel Kroening 2 Warning! No new research in this talk Talk is about doing existing stuff for.

26.8.2004 Daniel Kroening 26

Questions?