An Introduction to Z3 Huixing Fang National Trusted Embedded - - PowerPoint PPT Presentation

an introduction to z3
SMART_READER_LITE
LIVE PREVIEW

An Introduction to Z3 Huixing Fang National Trusted Embedded - - PowerPoint PPT Presentation

An Introduction to Z3 Huixing Fang National Trusted Embedded Software Engineering Technology Research Center April 12, 2017 . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . .. . . . . . Outline SMT 1 Z3 2 . .


slide-1
SLIDE 1

An Introduction to Z3

Huixing Fang

National Trusted Embedded Software Engineering Technology Research Center

April 12, 2017 . . . . . .. . . . . . . .. . . . . . . .. . . . . . . .. . . .. . . . . .

slide-2
SLIDE 2

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Outline

1

SMT

2

Z3

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 2 / 24

slide-3
SLIDE 3

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Outline

1

SMT

2

Z3

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 3 / 24

slide-4
SLIDE 4

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

SMT: Satisfiability Modulo Theory

1 Satisfiability is the problem of determining whether a formula φ has a

model

1

If φ is propositional, a model is a truth assignemt to Boolean variables

2

If φ is a first-order formula, a model assigns values to variables and interpretations to the function and predicate symbols

2 SAT Solvers: check satisfiability of propositional formulas 3 SMT Solvers: check satisfiability of formulas in a decidable first-order

theory (e.g., linear arithmetic, array theory, bitvectors)

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 4 / 24

slide-5
SLIDE 5

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Outline

1

SMT

2

Z3

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 5 / 24

slide-6
SLIDE 6

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Z3 Context

Figure: Context View

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 6 / 24

slide-7
SLIDE 7

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Z3 Architecture

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 7 / 24

slide-8
SLIDE 8

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Z3 Architecture

1 Simplifier : Input formulas are first processed using an incomplete, but

efficient simplification

p ∧ true − → p x = 4 ∧ q(x) − → x = 4 ∧ q(4)

2 Compiler: The simplified abstract syntax tree representation of the

formula is converted into a different data-structure comprising of a set

  • f clauses and congruence-closure nodes

3 Congruence Closure Core: Handles equalities and uninterpreted

  • functions. Receives the assignment from the SAT solver, and

processed using E-matching

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 8 / 24

slide-9
SLIDE 9

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-10
SLIDE 10

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-11
SLIDE 11

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-12
SLIDE 12

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) 4 asserts a formula : (assert (> a 10)) Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-13
SLIDE 13

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) 4 asserts a formula : (assert (> a 10)) 5 defines a function: (define-fun a () Int 100) Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-14
SLIDE 14

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) 4 asserts a formula : (assert (> a 10)) 5 defines a function: (define-fun a () Int 100) 6 defines a function:

(define-fun f ((x Int) (y Bool)) Int (ite (and (= x 11) (= y true)) 0 1))

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-15
SLIDE 15

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) 4 asserts a formula : (assert (> a 10)) 5 defines a function: (define-fun a () Int 100) 6 defines a function:

(define-fun f ((x Int) (y Bool)) Int (ite (and (= x 11) (= y true)) 0 1))

7 check: (check-sat) determines whether the current formulas on the Z3

stack are satisfiable or not

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-16
SLIDE 16

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

1 displays a message: (echo "starting Z3...") 2 declares a constant of a given type: (declare-const a Int) 3 declares a function : (declare-fun f (Int Bool) Int) 4 asserts a formula : (assert (> a 10)) 5 defines a function: (define-fun a () Int 100) 6 defines a function:

(define-fun f ((x Int) (y Bool)) Int (ite (and (= x 11) (= y true)) 0 1))

7 check: (check-sat) determines whether the current formulas on the Z3

stack are satisfiable or not

8 model: (get-model) is used to retrieve an interpretation that makes all

formulas on the Z3 internal stack true

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 9 / 24

slide-17
SLIDE 17

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

Basic Building Blocks

1 The basic building blocks of SMT formulas are constants and

  • functions. Constants are just functions that take no arguments. So

everything is really just a function.

2 An uninterpreted function or function symbol is one that has no other

property than its name and n-ary form.

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 10 / 24

slide-18
SLIDE 18

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Commands

Basic Building Blocks

1 The basic building blocks of SMT formulas are constants and

  • functions. Constants are just functions that take no arguments. So

everything is really just a function.

2 An uninterpreted function or function symbol is one that has no other

property than its name and n-ary form.

Valid and Satisfiable

1 A formula F is valid if F always evaluates to true for any assignment of

appropriate values to its uninterpreted function and constant symbols.

2 A formula F is satisfiable if there is some assignment of appropriate

values to its uninterpreted function and constant symbols under which F evaluates to true.

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 10 / 24

slide-19
SLIDE 19

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Propositional Logic

The pre-defined sort Bool is the sort (type) of all Boolean propositional

  • expressions. Z3 supports the usual Boolean operators and, or, xor, not, =>

(implication), ite (if-then-else).

Example

Formulas: ( declare −const p Bool ) ( declare −const q Bool ) ( declare −const r Bool ) ( define −fun c o n j e c t u r e () Bool (=> ( and (=> p q) (=> q r )) (=> p r ) ) ) ( a s s e r t ( not c o n j e c t u r e )) ( check−sat ) Result: unsat

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 11 / 24

slide-20
SLIDE 20

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Uninterpreted functions and constants

The basic building blocks of SMT formulas are constants and functions. Constants are just functions that take no arguments. So everything is really just a function.

Example

Formulas: ( declare −fun f ( I n t ) I n t ) ( declare −fun a () I n t ) ; a i s a constant ( declare −const b I n t ) ; syntax sugar f o r ( declare −fun b () I n t ) ( a s s e r t (> a 20)) ( a s s e r t (> b a )) ( a s s e r t (= ( f 10) 1)) ( check−sat ) ( get−model )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 12 / 24

slide-21
SLIDE 21

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Arithmetic

Z3 has builtin support for integer and real constants. These two types (sorts) represent the mathematical integers and reals. ( declare −const a I n t ) ( declare −const b I n t ) ( declare −const d Real ) ( declare −const e Real ) ( a s s e r t (> a (+ b 2))) ( a s s e r t (>= d e )) ( check−sat ) ( get−model ) sat ( model ( define −fun e () Real 0.0) ( define −fun d () Real 0.0) ( define −fun a () I n t 1) ( define −fun b () I n t (− 2)) )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 13 / 24

slide-22
SLIDE 22

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Nonlinear arithmetic

1 A formula is nonlinear if it contains expressions of the form (* t s)

where t and s are not numbers.

2 Nonlinear real arithmetic is very expensive, and Z3 is not complete for

this kind of formula. ( declare −const a I n t ) ( a s s e r t (> (∗ a a ) 3)) ( check−sat ) ( get−model ) ( declare −const b Real ) ( declare −const c Real ) ( a s s e r t (= (+ (∗ b b b) (∗ b c )) 3 . 0 ) ) ( check−sat ) sat ( model ( define −fun a () I n t (− 8) ) ) unknown

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 14 / 24

slide-23
SLIDE 23

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Bitvectors

Bitvector Literals

Bitvector literals may be defined using binary, decimal and hexadecimal notation.

1 #b010 in binary format is a bitvector of size 3 2 bitvector literal #x0a0 in hexadecimal format is a bitvector of size 12

The size must be specified for bitvector literals in decimal format. (_ bv10 32) is a bitvector of size 32 that representes the numeral 10 Z3 supports Bitvectors of arbitrary size. (_ BitVec n) is the sort of bitvectors whose length is n. Declare 64-bit bitvector constant x as follow: ( declare −const x (_ BitVec 64))

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 15 / 24

slide-24
SLIDE 24

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Basic Bitvector Arithmetic

1 bvadd

: a d d i t i o n

2 bvsub

: s u b t r a c t i o n

3 bvneg

: unary minus

4 bvmul

: m u l t i p l i c a t i o n

5 bvurem

: unsigned remainder

6 bvsrem

: signed remainder

7 bvsmod

: signed modulo

8 bvshl

: s h i f t l e f t

9 b v l s h r

: unsigned ( l o g i c a l ) s h i f t r i g h t

10 bvashr

: signed ( a r i t h m e t i c a l ) s h i f t r i g h t

11 bvor

: b i t w i s e

  • r

12 bvand

: b i t w i s e and

13 bvnot

: b i t w i s e not

14 bvnand

: b i t w i s e nand

15 bvnor

: b i t w i s e nor

16 bvxnor

: b i t w i s e xnor

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 16 / 24

slide-25
SLIDE 25

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Arrays

1 The expression (select a i) returns the value stored at position i of the

array a;

2 and (store a i v) returns a new array identical to a, but on position i it

contains the value v.

( declare −const x I n t ) ( declare −const y I n t ) ( declare −const a1 ( Array I n t I n t )) ( a s s e r t (= ( s e l e c t a1 x ) x )) ( a s s e r t (= ( s t o r e a1 x y ) a1 )) ( check−sat ) ( get−model ) sat ( model ( define −fun y () I n t 1) ( define −fun a1 () ( Array I n t I n t ) (_ as−a r r a y k ! 0 ) ) ( define −fun x () I n t 1) ( define −fun k !0 (( x !1 I n t )) I n t ( i t e (= x !1 1) 1 0)) )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 17 / 24

slide-26
SLIDE 26

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Records

A record is specified as a datatype with a single constructor and as many arguments as record elements.

Example

Parametric type Pair, with constructor mk-pair and two arguments that can be accessed using the selector functions first and second.

( declare −datatypes (T1 T2) (( Pair (mk−p a i r ( f i r s t T1) ( second T2 ) ) ) ) ) ( declare −const p1 ( Pair I n t I n t )) ( declare −const p2 ( Pair I n t I n t )) ( a s s e r t (= p1 p2 )) ( a s s e r t (> ( second p1 ) 20)) ( check−sat ) ( get−model ) ( a s s e r t ( not (= ( f i r s t p1 ) ( f i r s t p2 ) ) ) ) ( check−sat )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 18 / 24

slide-27
SLIDE 27

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Model-based Quantifier Instantiation

The model-based quantifier instantiation (MBQI) is essentially a counter-example based refinement loop, where candidate models are built and checked.

( set −option : smt . mbqi t r u e ) ( declare −fun f ( I n t I n t ) I n t ) ( declare −const a I n t ) ( declare −const b I n t ) ( a s s e r t ( f o r a l l (( x I n t )) (>= ( f x x ) (+ x a ) ) ) ) ( a s s e r t (< ( f a b ) a )) ( a s s e r t (> a 0)) ( check−sat ) ( get−model ) ( e v a l ( f (+ a 10) 20)) sat ( model ( define −fun b () I n t 2) ( define −fun a () I n t 1) ( define −fun f (( x !1 I n t ) ( x !2 I n t )) I n t ( i t e ( and (= x !1 1) (= x !2 2)) (+ 1 x ! 1 ) ) ) ) 12

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 19 / 24

slide-28
SLIDE 28

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Relations, rules and queries

The default fixed-point engine is a bottom-up Datalog engine. It works with finite relations and uses finite table representations as hash tables as the default way to represent finite relations.

( declare −r e l a ( ) ) ( declare −r e l b ( ) ) ( declare −r e l c ( ) ) ( r u l e (=> b a )) ( r u l e (=> c b )) ( set −option : f i x e d p o i n t . engine datalog ) ( query a ) ( r u l e c ) ( query a : print −answer true ) unsat sat true

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 20 / 24

slide-29
SLIDE 29

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Engine for Property Directed Reachability

The PDR engine is targeted at applications from symbolic model checking

  • f software. The systems may be infinite state.

Example

McCarthy’s 91 function illustrates a procedure that calls itself recursively twice

mc( x ) = i f x > 100 then x − 10 e l s e mc(mc( x+11)) ( declare −r e l mc ( I n t I n t )) ( declare −var n I n t ) ( declare −var m I n t ) ( declare −var p I n t ) ( r u l e (=> (> m 100) (mc m (− m 1 0)))) ( r u l e (=> ( and (<= m 100) (mc (+ m 11) p) (mc p n )) (mc m n ) ) )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 21 / 24

slide-30
SLIDE 30

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Engine for Property Directed Reachability

Example

McCarthy’s 91 function illustrates a procedure that calls itself recursively twice

mc( x ) = i f x > 100 then x − 10 e l s e mc(mc( x+11)) ( declare −r e l mc ( I n t I n t )) ( declare −var n I n t ) ( declare −var m I n t ) ( declare −var p I n t ) ( r u l e (=> (> m 100) (mc m (− m 1 0)))) ( r u l e (=> ( and (<= m 100) (mc (+ m 11) p) (mc p n )) (mc m n ) ) ) ( declare −r e l q ( I n t I n t )) ( r u l e (=> ( and (mc m n) (< n 92)) (q m n ) ) ) ( query q : print −c e r t i f i c a t e true )

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 22 / 24

slide-31
SLIDE 31

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

Engine for Property Directed Reachability

Example

McCarthy’s 91 function illustrates a procedure that calls itself recursively twice

mc( x ) = i f x > 100 then x − 10 e l s e mc(mc( x+11)) sat ( and ( not (<= 92 ( : var 5))) (mc ( : var 4) ( : var 5)) (= ( : var 5) (+ (− 10) ( : var 4))) ( not (<= ( : var 4) 100)) ) v5 < 92 mc( v4 ) = v5 v5 = v4 − 10 v4 > 100

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 23 / 24

slide-32
SLIDE 32

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

End

Thanks for your attention.

Huixing Fang (ECNU) An Introduction to Z3 April 12, 2017 24 / 24