a typed c11 semantics for interactive theorem proving
play

A Typed C11 Semantics for Interactive Theorem Proving Freek Wiedijk - PowerPoint PPT Presentation

A Typed C11 Semantics for Interactive Theorem Proving Freek Wiedijk Robbert Krebbers ICIS, Radboud University Nijmegen, The Netherlands January 13, 2015 @ CPP, Mumbai, India 1 What is this C program supposed to do? int x = 0, y = 0, *p =


  1. A Typed C11 Semantics for Interactive Theorem Proving Freek Wiedijk Robbert Krebbers ICIS, Radboud University Nijmegen, The Netherlands January 13, 2015 @ CPP, Mumbai, India 1

  2. What is this C program supposed to do? int x = 0, y = 0, *p = &x; Initial state: int f() { p = &y; return 17; } x y p int main() { • 0 0 *p = f(); printf("x=%d,y=%d\n", x, y); } Let us try some compilers ◮ Clang prints x=0,y=17 f is called first, thereafter p is evaluated to &y ◮ GCC prints x=17,y=0 p is evaluated to &x first, then f is called More subtle: *p = (p = &y, 17); has undefined behavior 2

  3. Contribution CH 2 O (Krebbers & Wiedijk) ◮ Compiler independent C11 semantics in Coq ◮ Operational, executable, and axiomatic semantics CPP’15 contribution: a verified interpreter to explore the non-deterministic behaviors of CH 2 O ◮ Type system & weak type safety ◮ Executable semantics & soundness/completeness ◮ Formal translation from AST & type soundness 3

  4. Recent related work CompCert KCC CH 2 O Compiler indep/close to C11 � � � � Size of C fragment � � � � Proof assistant support � � � Type system � � � Principled core language � � � Formal translation from AST n/a � � 4

  5. Overview of the CH 2 O project OCaml part Coq part CIL CH 2 O Stream of CH 2 O core .c file abstract abstract finite sets syntax syntax syntax of states Soundness and completeness Type soundness Subject red. CH 2 O and progress Type operational judgment semantics Soundness [FoSSaCS’13] [POPL’14] [VSTTE’14] Separation logic = translation = proof Executable structured memory model 5

  6. CH 2 O abstract C I ∈ cinit ::= e | { # » # » r := I } k ∈ cintrank ::= char | short | int sto ∈ cstorage ::= static | extern | auto | long | long long | ptr s ∈ cstmt ::= e | skip si ∈ signedness ::= signed | unsigned | goto x | return e ? τ i ∈ cinttype ::= si ? k | break | continue τ ∈ ctype ::= void | def x | τ i | τ ∗ | { s } | τ [ e ] | struct x | union x | # » sto τ x := I ? ; s | enum x | typeof e | typedef x := τ ; s α ∈ assign ::= := | ⊚ := | := ⊚ | s 1 ; s 2 | x : s e ∈ cexpr ::= x | const τ i z | sizeof τ | while( e ) s | τ i min | τ i max | τ i bits | for( e 1 ; e 2 ; e 3 ) s | & e | ∗ e | do s while( e ) | e 1 α e 2 | if ( e ) s 1 else s 2 | x ( � e ) | abort d ∈ decl ::= struct # » τ x | union # » τ x | alloc τ e | free e | typedef τ | ⊚ u e | e 1 ⊚ e 2 | enum # » x := e ? : τ i | e 1 && e 2 | e 1 || e 2 | global I ? : # » sto τ | e 1 ? e 2 : e 3 | ( e 1 , e 2 ) | fun ( # » τ x ? ) s ? : # » sto τ | ( τ ) I | e . x Θ ∈ decls := list (string × decl) r ∈ crefseg ::= [ e ] | . x 6

  7. CH 2 O abstract C Formal translation to core C Conversions include: ◮ Named variables to De Bruijn indices ◮ Sound/complete constant expression evaluation, e.g. in τ [ e ] ◮ Simplification of loops, e.g. while( e ) s ⇒ catch (loop (if ( e ) skip else throw 0 ; catch s )) ◮ Expansion of typedef and enum declarations ◮ Translation of constants like INT_MIN ◮ Translation of compound literals, e.g. (struct S){ .x=1, {4,r}, .y[4+1]=0, q } Theorem (Type soundness) The translator only produces well-typed CH 2 O core programs 7

  8. CH 2 O operational semantics ◮ Zippers are used to describe non-local control flow ◮ Structured memory model (as separation algebra) to accurately describe low- versus high-level subtleties of C11 ◮ Permissions (as separation algebra) are used for: ◮ Ruling out expressions like (x = 1) + (x = 2) ◮ Connection with separation logic ◮ Evaluation contexts for non-deterministic redex selection ◮ Stuck states for undefined behavior 8

  9. CH 2 O operational semantics Example of memory state Consider: struct S { union U { signed char x[2]; int y; } u; void *p; } s = { { .x = {33,34} }, s.u.x + 2 } The object in memory may look like: o s �→ void ∗ : (ptr p ) 0 (ptr p ) 1 . . . (ptr p ) 31 .0 signed char: 10000100 01000100 �������� �������� signed char[2] struct S union U p = ( o s : struct S , → 0 → • 0 → 0 , 16) signed char > void ֒ − − − − ֒ − − − ֒ − − − − − − − 9

  10. Typing of CH 2 O core C Expression judgment Γ , Γ f , ∆ ,� τ ⊢ e : τ lr ◮ Struct/union fields: Γ ∈ tag → fin list type ◮ Functions: Γ f ∈ funname → fin (list type × type) ◮ Memory layout: ∆ ∈ index → fin (type × bool) ◮ De Bruijn variables: � τ ∈ list type For example: τ ( i ) = τ e : τ l Γ f ( f ) = ( � τ, σ ) � e : � τ r � x τ i : τ l & e : ( τ ∗ ) r f ( � e ) : σ r Statement judgment Γ , Γ f , ∆ ,� τ ⊢ s : ( β, τ ? ) e : τ r skip : (false , ⊥ ) return e : (true , τ ) goto l : (true , ⊥ ) State judgment Γ , Γ f , ∆ ⊢ S : g (typically g = main ) 10

  11. Typing of CH 2 O core C Type preservation Lemma (Type preservation) If S 1 : g and S 1 � S 2 , then S 2 : g Theorem (Weak type safety) v ), then if S 1 � ∗ S 2 we have either: If S 1 initial for g ( � 1. Not finished: S 2 � S 3 for some S 3 2. Undefined behavior: S 2 = S ( P , undef φ U , m ) 3. Final state: S 2 = S ( ǫ, return g v , m ) 11

  12. Executable semantics Goal: define exec : state → P fin (state) and extract to OCaml Problems: 1. Decomposition E [ e 1 ] of expressions is non-deterministic: S ( P , E [ e 1 ] , m 1 ) � S ( P , E [ e 2 ] , m 2 ) if ( e 1 , m 1 ) � h ( e 2 , m 2 ) 2. Object identifiers o for newly allocated memory are arbitrary: S ( P , ( ց , local τ s ) , m ) � S ((local o : τ � ) P , ( ց , s ) , alloc Γ o τ false m ) if o / ∈ dom m Solutions: 1. Enumerate all possible decompositions E [ e 1 ] 2. Pick a canonical object identifier fresh m for o (makes completeness difficult!) 12

  13. Executable semantics Soundness and completeness Theorem (Soundness) If S 2 ∈ exec S 1 , then S 1 � S 2 Definition (Permutation) We let S 1 ∼ f S 2 , if S 2 is obtained by renaming S 1 with respect to f : index → option index Theorem (Completeness) If S 1 � ∗ S 2 , then there exists an f and S ′ 2 such that: S ′ ∗ 2 exec ∼ f ∗ S 1 S 2 13

  14. Formalization in Coq Interpreter extracted to OCaml from Coq ◮ Error monad for failure of type checking ◮ Set monad for non-determinism ◮ Verified hash sets for efficiency All essential properties proven in Coq: ◮ Weak type safety ◮ Soundness and completeness of executable semantics ◮ Type soundness of translation from AST Part of ∼ 40.000 LOC constructive and axiom free development 14

  15. Conclusion A programming language semantics should consist of: ◮ Operational semantics Reasoning about program transformations ◮ Axiomatic semantics Correctness proofs of concrete programs ◮ Executable semantics Debugging and testing Extremely challenging to develop matching versions for C11 Future work: still many parts of C11 left to be explored 15

  16. Demo and questions Sources: http://robbertkrebbers.nl/research/ch2o/ 16

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend