The K Framework and a Formal Semantics of C Chucky Ellison Traian - - PowerPoint PPT Presentation

the k framework and a formal semantics of c
SMART_READER_LITE
LIVE PREVIEW

The K Framework and a Formal Semantics of C Chucky Ellison Traian - - PowerPoint PPT Presentation

The K Framework Semantics of C The K Framework and a Formal Semantics of C Chucky Ellison Traian Florin S , erb anut , a Grigore Ros , u University of Illinois MVD10 September 17, 2010 MVD10 Chucky Ellison The K Framework and


slide-1
SLIDE 1

The K Framework Semantics of C

The K Framework and a Formal Semantics of C

Chucky Ellison Traian Florin S

, erb˘

anut

a Grigore Ros

,u University of Illinois

MVD’10 September 17, 2010

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 1/13

slide-2
SLIDE 2

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-3
SLIDE 3

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-4
SLIDE 4

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

Languages Functional, Imperative, OO, Declarative; including Verilog, Scheme, Java, and now C

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-5
SLIDE 5

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

Languages Functional, Imperative, OO, Declarative; including Verilog, Scheme, Java, and now C Type Systems Checkers, Inferencers; including W

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-6
SLIDE 6

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

Languages Functional, Imperative, OO, Declarative; including Verilog, Scheme, Java, and now C Type Systems Checkers, Inferencers; including W Algorithms Sorting, Dijkstra’s Algorithm, Sudoku Solving

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-7
SLIDE 7

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

Languages Functional, Imperative, OO, Declarative; including Verilog, Scheme, Java, and now C Type Systems Checkers, Inferencers; including W Algorithms Sorting, Dijkstra’s Algorithm, Sudoku Solving Tools Debugging, Race Detection

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-8
SLIDE 8

The K Framework Semantics of C Overview Example

What is K?

◮ K is an executable semantic framework for defining

programming languages, calculi, type systems, and formal analysis tools

◮ It has been used to define:

Languages Functional, Imperative, OO, Declarative; including Verilog, Scheme, Java, and now C Type Systems Checkers, Inferencers; including W Algorithms Sorting, Dijkstra’s Algorithm, Sudoku Solving Tools Debugging, Race Detection Logics Matching Logic

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 2/13

slide-9
SLIDE 9

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-10
SLIDE 10

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-11
SLIDE 11

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-12
SLIDE 12

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

◮ Next task always at top of the list MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-13
SLIDE 13

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

◮ Next task always at top of the list ◮ Easy to define control-intensive features like halt, call/cc MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-14
SLIDE 14

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

◮ Next task always at top of the list ◮ Easy to define control-intensive features like halt, call/cc

◮ Rewriting modulo ACI (associativity, commutivity, identity) to

improve modularity

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-15
SLIDE 15

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

◮ Next task always at top of the list ◮ Easy to define control-intensive features like halt, call/cc

◮ Rewriting modulo ACI (associativity, commutivity, identity) to

improve modularity

◮ Specify only what is needed from a cell for a semantic rule MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-16
SLIDE 16

The K Framework Semantics of C Overview Example

The K Technique

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result ◮ Flexible, extensible, configurations as multi-sets of nested cells

◮ Cells can contain (multi-)sets, lists, maps, or computations

◮ Computation (K) as a list of -separated tasks

◮ Next task always at top of the list ◮ Easy to define control-intensive features like halt, call/cc

◮ Rewriting modulo ACI (associativity, commutivity, identity) to

improve modularity

◮ Specify only what is needed from a cell for a semantic rule ◮ Abstract the remainder of the cell MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 3/13

slide-17
SLIDE 17

The K Framework Semantics of C Overview Example

IMP Syntax

AExp

::=

Int | VarId

|

AExp + AExp

[strict] |

AExp / AExp

[strict]

BExp

::=

Bool

|

AExp <= AExp

[seqstrict] | not BExp [strict] |

BExp and BExp

[strict(1)]

Stmt

::= skip| Stmt ; Stmt |

VarId := AExp

[strict(2)] | if BExp then Stmt else Stmt [strict(1)] | while BExp do Stmt | print AExp [strict] | var VarId ; Stmt

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 4/13

slide-18
SLIDE 18

The K Framework Semantics of C Overview Example

Assignment Rule

Configuration

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result

Variable Lookup Rule

X

V

· · · k · · · X → L · · · env · · · L → V · · · store

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 5/13

slide-19
SLIDE 19

The K Framework Semantics of C Overview Example

IMP Semantics

K k Map{VarId,Location} env thread* threads Map{Location,KResult} store Nat nextLoc List{KResult}

  • utput

T result

Assignment

X := V · · · · k · · · X → L · · · env · · · L →

V

· · · store

Print (Output)

print V · · · k · · · ·

V

  • utput

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 6/13

slide-20
SLIDE 20

The K Framework Semantics of C Overview Example

The K Framework Overview Example Semantics of C Background Results

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 7/13

slide-21
SLIDE 21

The K Framework Semantics of C Background Results

C Semantics

◮ Plenty of formal semantics for C already: [Gurevich and

Huggins, 1993]; [Cook, Cohen, and Redmond, 1994]; [Cook, and Subramanian, 1994]; [Norrish, 1998]; [Papaspyrou, 1998]; [Blazy and Leroy, 2009]

◮ Hard to deal with:

◮ Unstructured control flow (goto, switch) ◮ Intricate typing rules ◮ Expression evaluation order has few restrictions MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 8/13

slide-22
SLIDE 22

The K Framework Semantics of C Background Results

Duff’s Device

◮ Unstructured control flow (goto, switch)

int n = (count+7)/8; switch(count%8) { case 0: do{ *dest++ = *src++; case 7: *dest++ = *src++; case 6: *dest++ = *src++; case 5: *dest++ = *src++; case 4: *dest++ = *src++; case 3: *dest++ = *src++; case 2: *dest++ = *src++; case 1: *dest++ = *src++; } while(--n>0); }

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 9/13

slide-23
SLIDE 23

The K Framework Semantics of C Background Results

◮ Intricate typing rules

Signed chars: (−128 to 127) Ints: (−32768 to 32767) Unsigned ints: (0 to 65535) Long ints: (−2M to 2M)

int a = 1000, b = 1000; long int c = a * b; unsigned int a = 1000, b = 1000; long int c = a * b; signed char a = 100, b = 100; int c = a * b;

2147483648 0x80000000

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 10/13

slide-24
SLIDE 24

The K Framework Semantics of C Background Results

◮ Expression evaluation order has few restrictions

(A + B++) + C

A, B, C, B++ A, B, B++, C A, C, B, B++ A, C, B, B++

· · ·

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 11/13

slide-25
SLIDE 25

The K Framework Semantics of C Background Results

Our Semantics

The most complete formal semantics for C to date

◮ Parameterizable on implementation-defined parts of the

semantics, but given a default instantiation

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 12/13

slide-26
SLIDE 26

The K Framework Semantics of C Background Results

Our Semantics

The most complete formal semantics for C to date

◮ Parameterizable on implementation-defined parts of the

semantics, but given a default instantiation

◮ Covering every major feature including parts of the standard

library: goto, longjump, malloc, variadic functions, enums, structs, unions, bitfields, typedefs. . .

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 12/13

slide-27
SLIDE 27

The K Framework Semantics of C Background Results

Our Semantics

The most complete formal semantics for C to date

◮ Parameterizable on implementation-defined parts of the

semantics, but given a default instantiation

◮ Covering every major feature including parts of the standard

library: goto, longjump, malloc, variadic functions, enums, structs, unions, bitfields, typedefs. . .

◮ Yielding an interpreter, debugger, and state space search and

model checker “for free”

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 12/13

slide-28
SLIDE 28

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-29
SLIDE 29

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-30
SLIDE 30

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-31
SLIDE 31

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules ◮ 2800 source lines of code (SLOC)

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-32
SLIDE 32

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules ◮ 2800 source lines of code (SLOC) ◮ Only 50 rules for statements

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-33
SLIDE 33

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules ◮ 2800 source lines of code (SLOC) ◮ Only 50 rules for statements ◮ Only 130 for expressions

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-34
SLIDE 34

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules ◮ 2800 source lines of code (SLOC) ◮ Only 50 rules for statements ◮ Only 130 for expressions ◮ Tested against the GCC torture tests:

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13

slide-35
SLIDE 35

The K Framework Semantics of C Background Results

Our Semantics (Results)

◮ 125 syntactic operators ◮ 200 auxiliary semantic operators ◮ 620 different rules ◮ 2800 source lines of code (SLOC) ◮ Only 50 rules for statements ◮ Only 130 for expressions ◮ Tested against the GCC torture tests:

◮ Of 1057 tests, 720 tests appear to be standards compliant. Of

those 720, we pass about 95%.

MVD’10 Chucky Ellison The K Framework and a Formal Semantics of C 13/13