An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C

Size: px
Start display at page:

Download "An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C"

Transcription

1 An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C Robbert Krebbers Radboud University Nijmegen January 22, POPL, San Diego, USA 1 / 16

2 What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } 2 / 16

3 What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers Clang prints 4 7, seems just left-right 2 / 16

4 What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers Clang prints 4 7, seems just left-right GCC prints 4 8, does not correspond to any evaluation order 2 / 16

5 What is this program supposed to do? int main() { int x; int y = (x = 3) + (x = 4); printf("%d %d\n", x, y); } Let us try some compilers Clang prints 4 7, seems just left-right GCC prints 4 8, does not correspond to any evaluation order This program violates the sequence point restriction due to two unsequenced writes to x resulting in undefined behavior thus both compilers are right 2 / 16

6 Undefined behavior in C Garbage in, garbage out principle Programs with undefined behavior are not statically excluded Undefined behavior all bets are off Allows compilers to omit (expensive) dynamic checks 3 / 16

7 Undefined behavior in C Garbage in, garbage out principle Programs with undefined behavior are not statically excluded Undefined behavior all bets are off Allows compilers to omit (expensive) dynamic checks A compiler independent C semantics should account for undefined behavior 3 / 16

8 Examples Side-effects are useful in a while, for, return,... while ((x = getchar())!= EOF) /* do something */ 4 / 16

9 Examples Side-effects are useful in a while, for, return,... while ((x = getchar())!= EOF) /* do something */ Non-determinism is subtle in innocent looking examples: *p = g (x, y, z); Here, g may change p, so the evaluation order matters 4 / 16

10 Examples Side-effects are useful in a while, for, return,... while ((x = getchar())!= EOF) /* do something */ Non-determinism is subtle in innocent looking examples: *p = g (x, y, z); Here, g may change p, so the evaluation order matters Interleaving of subexpressions is possible, for example printf("a") + (printf("b") + printf("c")); may print bac 4 / 16

11 Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: expressions with function calls, assignments, conditionals undefined behavior due to integer overflow parametrized by integer types dynamically allocated memory (malloc and free) non-local control (return and goto) local variables (and pointers to those) mutual recursion separation logic soundness proof fully checked by Coq 5 / 16

12 Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: expressions with function calls, assignments, conditionals undefined behavior due to integer overflow parametrized by integer types dynamically allocated memory (malloc and free) non-local control (return and goto) local variables (and pointers to those) mutual recursion separation logic soundness proof fully checked by Coq 5 / 16

13 Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: expressions with function calls, assignments, conditionals undefined behavior due to integer overflow parametrized by integer types dynamically allocated memory (malloc and free) non-local control (return and goto) local variables (and pointers to those) mutual recursion separation logic soundness proof fully checked by Coq 5 / 16

14 Contribution A compiler independent small step operational, and axiomatic, semantics for non-determinism and sequence points, supporting: expressions with function calls, assignments, conditionals undefined behavior due to integer overflow parametrized by integer types dynamically allocated memory (malloc and free) non-local control (return and goto) local variables (and pointers to those) mutual recursion separation logic soundness proof fully checked by Coq 5 / 16

15 Key idea Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } {P 1 P 2 } e 1 e 2 {Q 1 Q 2 } 6 / 16

16 Key idea Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition What does this mean: {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } {P 1 P 2 } e 1 e 2 {Q 1 Q 2 } Split the memory into two disjoint parts Prove that e 1 and e 2 can be executed safely in their part Now e 1 e 2 can be executed safely in the whole memory 6 / 16

17 Key idea Observation: non-determinism corresponds to concurrency Idea: use the separation logic rule for parallel composition What does this mean: {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } {P 1 P 2 } e 1 e 2 {Q 1 Q 2 } Split the memory into two disjoint parts Prove that e 1 and e 2 can be executed safely in their part Now e 1 e 2 can be executed safely in the whole memory Disjointness no sequence point violation 6 / 16

18 Definition of the memory Given a set of permissions P, we define: m mem := index fin (val P) b index := N v val ::= indet int τ n ptr b NULL Integer types: unsigned char, signed int,... 7 / 16

19 Permission systems Actual permissions contains: Locked flag to catch sequence point violations Assignment: lock memory location Sequence point: unlock memory locations A fraction (0, 1] Q to allow sharing Block scope variable or allocated with malloc flag 8 / 16

20 Permission systems Actual permissions contains: Locked flag to catch sequence point violations Assignment: lock memory location Sequence point: unlock memory locations A fraction (0, 1] Q to allow sharing Block scope variable or allocated with malloc flag A permission system P abstracts from these details., \ : P P P, : P P Prop kind : P {Free, Write, Read, Locked} lock, unlock : P P satisfying certain axioms 8 / 16

21 Permission systems Actual permissions contains: Locked flag to catch sequence point violations Assignment: lock memory location Sequence point: unlock memory locations A fraction (0, 1] Q to allow sharing Block scope variable or allocated with malloc flag A permission system P abstracts from these details., \ : P P P, : P P Prop kind : P {Free, Write, Read, Locked} lock, unlock : P P satisfying certain axioms 8 / 16

22 Permission systems Actual permissions contains: Locked flag to catch sequence point violations Assignment: lock memory location Sequence point: unlock memory locations A fraction (0, 1] Q to allow sharing Block scope variable or allocated with malloc flag A permission system P abstracts from these details., \ : P P P, : P P Prop kind : P {Free, Write, Read, Locked} lock, unlock : P P satisfying certain axioms 8 / 16

23 Permission systems Actual permissions contains: Locked flag to catch sequence point violations Assignment: lock memory location Sequence point: unlock memory locations A fraction (0, 1] Q to allow sharing Block scope variable or allocated with malloc flag A permission system P abstracts from these details., \ : P P P, : P P Prop kind : P {Free, Write, Read, Locked} lock, unlock : P P satisfying certain axioms We lift these operations to memories index fin (val P) 8 / 16

24 The language Our language binop ::= == <= + - * / %... e expr ::= x i [v] Ω e 1 := e 2 f ( e) load e alloc free e e 1 e 2 e 1? e 2 : e 3 (τ) e s stmt ::= e skip goto l return e block c s s 1 ; s 2 l : s while(e) s if (e) s 1 else s 2 9 / 16

25 The language Our language binop ::= == <= + - * / %... e expr ::= x i [v] Ω e 1 := e 2 f ( e) load e alloc free e e 1 e 2 e 1? e 2 : e 3 (τ) e s stmt ::= e skip goto l return e block c s s 1 ; s 2 l : s while(e) s if (e) s 1 else s 2 Values [v] Ω carry a set Ω of indexes (memory locations) to be unlocked at the next sequence point 9 / 16

26 Operational semantics Head reduction for expressions (e, m) h (e, m ) (9 rules) 10 / 16

27 Operational semantics Head reduction for expressions (e, m) h (e, m ) (9 rules) On assignments: locked index b added to Ω 1 Ω 2 ([ptr b] Ω1 :=[v] Ω2, m) h ([v] {b} Ω1 Ω 2, lock b (m[b :=v])) On sequence points: Ω unlocked in memory ([v] Ω? e 2 : e 3, m) h (e 2, unlock Ω m) provided / 16

28 Operational semantics Head reduction for expressions (e, m) h (e, m ) (9 rules) On assignments: locked index b added to Ω 1 Ω 2 ([ptr b] Ω1 :=[v] Ω2, m) h ([v] {b} Ω1 Ω 2, lock b (m[b :=v])) On sequence points: Ω unlocked in memory ([v] Ω? e 2 : e 3, m) h (e 2, unlock Ω m) provided... Gives a local treatment of sequence points 10 / 16

29 Operational semantics Head reduction for expressions (e, m) h (e, m ) (9 rules) On assignments: locked index b added to Ω 1 Ω 2 ([ptr b] Ω1 :=[v] Ω2, m) h ([v] {b} Ω1 Ω 2, lock b (m[b :=v])) On sequence points: Ω unlocked in memory ([v] Ω? e 2 : e 3, m) h (e 2, unlock Ω m) provided... Gives a local treatment of sequence points Small step reduction S(k, φ, m) S(k, φ, m ) (k, φ) gives the position in the whole program Uses evaluation contexts to lift head reduction Different φs for expressions, statements, function calls (33 rules) 10 / 16

30 Assertions of separation logic Defined using a shallow embedding: P, Q assert := stack mem Prop Maps variables to indexes in memory 11 / 16

31 Assertions of separation logic Defined using a shallow embedding: P, Q assert := stack mem Prop Maps variables to indexes in memory Some assertions: (P Q) ρ m := m 1 m 2. m = m 1 m 2 m 1 m 2 P ρ m 1 Q ρ m 2 11 / 16

32 Assertions of separation logic Defined using a shallow embedding: Some assertions: P, Q assert := stack mem Prop Maps variables to indexes in memory (P Q) ρ m := m 1 m 2. m = m 1 m 2 m 1 m 2 P ρ m 1 Q ρ m 2 γ (e 1 e2 ) ρ m := b v. [[ e 1 ]] ρ,m = ptr b [[ e 2 ]] ρ,m = v m = {(b, (v, γ))} (with e 1 and e 2 side-effect free) 11 / 16

33 Assertions of separation logic Defined using a shallow embedding: Some assertions: P, Q assert := stack mem Prop Maps variables to indexes in memory (P Q) ρ m := m 1 m 2. m = m 1 m 2 m 1 m 2 P ρ m 1 Q ρ m 2 γ (e 1 e2 ) ρ m := b v. [[ e 1 ]] ρ,m = ptr b [[ e 2 ]] ρ,m = v m = {(b, (v, γ))} (P ) ρ m := P ρ (unlock (locks m)m) (with e 1 and e 2 side-effect free) 11 / 16

34 The Hoare triples Statement judgment ; J; R {P} s {Q} Function conditions Goto/return conditions 12 / 16

35 The Hoare triples Statement judgment ; J; R {P} s {Q} Function conditions Goto/return conditions Q : assert Expression judgment {P} e {Q} Q : val assert In the semantics: if P holds beforehand, then e does not crash Q v holds afterwards when terminating with v with framing memories that can change at each step 12 / 16

36 The axiomatic semantics 24 separation logic proof rules, e.g.: For assignments: Write kind γ {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } av. ((Q 1 a Q 2 v) ((a γ ) R a v)) {P 1 P 2 } e 1 := e 2 {λv. a. (a lock γ v) R a v} 13 / 16

37 The axiomatic semantics 24 separation logic proof rules, e.g.: For assignments: Write kind γ {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } av. ((Q 1 a Q 2 v) ((a γ ) R a v)) For dereferencing: {P 1 P 2 } e 1 := e 2 {λv. a. (a lock γ v) R a v} kind γ Locked {P} e {λa. v. Q a v (a γ v)} {P} load e {λv. a. Q a v (a γ v)} 13 / 16

38 The axiomatic semantics 24 separation logic proof rules, e.g.: For assignments: Write kind γ {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } av. ((Q 1 a Q 2 v) ((a γ ) R a v)) For dereferencing: For the conditional: {P 1 P 2 } e 1 := e 2 {λv. a. (a lock γ v) R a v} kind γ Locked {P} e {λa. v. Q a v (a γ v)} {P} load e {λv. a. Q a v (a γ v)} {P} e 1 {λv. v indet P v } { v. istrue v P v} e 2 {Q}... {P} e 1? e 2 : e 3 {Q} 13 / 16

39 The axiomatic semantics 24 separation logic proof rules, e.g.: For assignments: Write kind γ {P 1 } e 1 {Q 1 } {P 2 } e 2 {Q 2 } av. ((Q 1 a Q 2 v) ((a γ ) R a v)) For dereferencing: For the conditional: {P 1 P 2 } e 1 := e 2 {λv. a. (a lock γ v) R a v} kind γ Locked {P} e {λa. v. Q a v (a γ v)} {P} load e {λv. a. Q a v (a γ v)} {P} e 1 {λv. v indet P v } { v. istrue v P v} e 2 {Q}... {P} e 1? e 2 : e 3 {Q} Common separation logic and more complex rules can be derived 13 / 16

40 Formalization in Coq Based on [Krebbers/Wiedijk, FoSSaCS 13] Extremely useful for debugging Notations close to those on paper Various extensions of the separation logic Uses lots of automation lines of code Lemma ax_load A γ e P Q : perm_kind γ Locked \ A e {{ P }} e {{ λ a, v, Q a v valc a {γ} valc v }} \ A e {{ P }} load e {{ λ v, a, Q a v valc a {γ} valc v }}. 14 / 16

41 Future research Integration with the C type system [Krebbers, CPP 13] Integration with non-aliasing restrictions [Krebbers, CPP 13] Interpreter in Coq Verification condition generator in Coq Automation of separation logic 15 / 16

42 Questions Sources: 16 / 16

An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C

An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C An Operational and Axiomatic Semantics for Non-determinism and Sequence Points in C Robbert Krebbers ICIS, Radboud University Nijmegen, The Netherlands mail@robbertkrebbers.nl Abstract The C11 standard

More information

Aliasing restrictions of C11 formalized in Coq

Aliasing restrictions of C11 formalized in Coq Aliasing restrictions of C11 formalized in Coq Robbert Krebbers Radboud University Nijmegen December 11, 2013 @ CPP, Melbourne, Australia Aliasing Aliasing: multiple pointers referring to the same object

More information

Undefinedness and Non-determinism in C

Undefinedness and Non-determinism in C 1 Undefinedness and Non-determinism in C Nabil M. Al-Rousan Nov. 21, 2018 @ UBC Based on slides from Robbert Krebbers Aarhus University, Denmark 2 What is this program supposed to do? The C quiz, question

More information

Formal C semantics: CompCert and the C standard

Formal C semantics: CompCert and the C standard Formal C semantics: CompCert and the C standard Robbert Krebbers 1, Xavier Leroy 2, and Freek Wiedijk 1 1 ICIS, Radboud University Nijmegen, The Netherlands 2 Inria Paris-Rocquencourt, France Abstract.

More information

arxiv: v1 [cs.lo] 10 Sep 2015

arxiv: v1 [cs.lo] 10 Sep 2015 Journal of Automated Reasoning manuscript No. (will be inserted by the editor) A Formal C Memory Model for Separation Logic Robbert Krebbers arxiv:1509.03339v1 [cs.lo] 10 Sep 2015 Received: n/a Abstract

More information

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic

More information

1 Introduction. 3 Syntax

1 Introduction. 3 Syntax CS 6110 S18 Lecture 19 Typed λ-calculus 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic semantics,

More information

Separation Logic for Non-local Control Flow and Block Scope Variables

Separation Logic for Non-local Control Flow and Block Scope Variables Separation Logic for Non-local Control Flow and Block Scope Variables Robbert Krebbers and Freek Wiedijk ICIS, Radboud University Nijmegen, The Netherlands Abstract. We present an approach for handling

More information

Programming Languages

Programming Languages CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

More information

A concrete memory model for CompCert

A concrete memory model for CompCert A concrete memory model for CompCert Frédéric Besson Sandrine Blazy Pierre Wilke Rennes, France P. Wilke A concrete memory model for CompCert 1 / 28 CompCert real-world C to ASM compiler used in industry

More information

Programming Languages Third Edition

Programming Languages Third Edition Programming Languages Third Edition Chapter 12 Formal Semantics Objectives Become familiar with a sample small language for the purpose of semantic specification Understand operational semantics Understand

More information

Lectures 20, 21: Axiomatic Semantics

Lectures 20, 21: Axiomatic Semantics Lectures 20, 21: Axiomatic Semantics Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Static Analysis Based on slides by George Necula Pratikakis (CSD) Axiomatic Semantics

More information

Hoare logic. A proof system for separation logic. Introduction. Separation logic

Hoare logic. A proof system for separation logic. Introduction. Separation logic Introduction Hoare logic Lecture 6: Examples in separation logic In the previous lecture, we saw how reasoning about pointers in Hoare logic was problematic, which motivated introducing separation logic.

More information

Simply-Typed Lambda Calculus

Simply-Typed Lambda Calculus #1 Simply-Typed Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (small-step) semantics? What is denotational semantics? What is axiomatic semantics? What

More information

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019 Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

More information

Chapter 1. Introduction

Chapter 1. Introduction 1 Chapter 1 Introduction An exciting development of the 21st century is that the 20th-century vision of mechanized program verification is finally becoming practical, thanks to 30 years of advances in

More information

RustBelt: Securing the Foundations of the Rust Programming Language

RustBelt: Securing the Foundations of the Rust Programming Language RustBelt: Securing the Foundations of the Rust Programming Language Ralf Jung, Jacques-Henri Jourdan, Robbert Krebbers, Derek Dreyer POPL 2018 in Los Angeles, USA Max Planck Institute for Software Systems

More information

Pointers (continued), arrays and strings

Pointers (continued), arrays and strings Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011

CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 CS 6110 S11 Lecture 12 Naming and Scope 21 February 2011 In this lecture we introduce the topic of scope in the context of the λ-calculus and define translations from λ-cbv to FL for the two most common

More information

Pointers (continued), arrays and strings

Pointers (continued), arrays and strings Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

More information

CS 314 Principles of Programming Languages. Lecture 9

CS 314 Principles of Programming Languages. Lecture 9 CS 314 Principles of Programming Languages Lecture 9 Zheng Zhang Department of Computer Science Rutgers University Wednesday 5 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Homework

More information

Pierce Ch. 3, 8, 11, 15. Type Systems

Pierce Ch. 3, 8, 11, 15. Type Systems Pierce Ch. 3, 8, 11, 15 Type Systems Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 3a Andrew Tolmach Portland State University 1994-2016 Formal Semantics Goal: rigorous and unambiguous definition in terms of a wellunderstood formalism (e.g.

More information

A CRASH COURSE IN SEMANTICS

A CRASH COURSE IN SEMANTICS LAST TIME Recdef More induction NICTA Advanced Course Well founded orders Slide 1 Theorem Proving Principles, Techniques, Applications Slide 3 Well founded recursion Calculations: also/finally {P}... {Q}

More information

Work-in-progress: "Verifying" the Glasgow Haskell Compiler Core language

Work-in-progress: Verifying the Glasgow Haskell Compiler Core language Work-in-progress: "Verifying" the Glasgow Haskell Compiler Core language Stephanie Weirich Joachim Breitner, Antal Spector-Zabusky, Yao Li, Christine Rizkallah, John Wiegley June 2018 Let's prove GHC correct

More information

C++ Undefined Behavior

C++ Undefined Behavior C++ Undefined Behavior What is it, and why should I care? A presentation originally by Marshal Clow Original: https://www.youtube.com/watch?v=uhclkb1vkay Original Slides: https://github.com/boostcon/cppnow_presentations_2014/blob/master/files/undefined-behavior.pdf

More information

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic

Hoare logic. WHILE p, a language with pointers. Introduction. Syntax of WHILE p. Lecture 5: Introduction to separation logic Introduction Hoare logic Lecture 5: Introduction to separation logic In the previous lectures, we have considered a language, WHILE, where mutability only concerned program variables. Jean Pichon-Pharabod

More information

Combining Static and Dynamic Contract Checking for Curry

Combining Static and Dynamic Contract Checking for Curry Michael Hanus (CAU Kiel) Combining Static and Dynamic Contract Checking for Curry LOPSTR 2017 1 Combining Static and Dynamic Contract Checking for Curry Michael Hanus University of Kiel Programming Languages

More information

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages Formal Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html Benefits of formal

More information

Hoare Logic and Model Checking

Hoare Logic and Model Checking Hoare Logic and Model Checking Kasper Svendsen University of Cambridge CST Part II 2016/17 Acknowledgement: slides heavily based on previous versions by Mike Gordon and Alan Mycroft Pointers Pointers and

More information

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18

Hoare logic. Lecture 5: Introduction to separation logic. Jean Pichon-Pharabod University of Cambridge. CST Part II 2017/18 Hoare logic Lecture 5: Introduction to separation logic Jean Pichon-Pharabod University of Cambridge CST Part II 2017/18 Introduction In the previous lectures, we have considered a language, WHILE, where

More information

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

More information

COS 320. Compiling Techniques

COS 320. Compiling Techniques Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

More information

Introduction to Axiomatic Semantics

Introduction to Axiomatic Semantics Introduction to Axiomatic Semantics Meeting 10, CSCI 5535, Spring 2009 Announcements Homework 3 due tonight Homework 2 is graded 13 (mean), 14 (median), out of 21 total, but Graduate class: final project

More information

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False.

Quiz 0 Answer Key. Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d. True or False. Quiz 0 Answer Key Answers other than the below may be possible. Multiple Choice. 0. a 1. a 2. b 3. c 4. b 5. d True or False. 6. T or F 7. T 8. F 9. T 10. T or F Itching for Week 0? 11. 00011001 + 00011001

More information

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265 Parameter passing Programming in C UVic SEng 265 Daniel M. German Department of Computer Science University of Victoria 1 SEng 265 dmgerman@uvic.ca C implements call-by-value parameter passing int a =

More information

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor CS 261 Fall 2017 Mike Lam, Professor C Introduction Variables, Memory Model, Pointers, and Debugging The C Language Systems language originally developed for Unix Imperative, compiled language with static

More information

Verasco: a Formally Verified C Static Analyzer

Verasco: a Formally Verified C Static Analyzer Verasco: a Formally Verified C Static Analyzer Jacques-Henri Jourdan Joint work with: Vincent Laporte, Sandrine Blazy, Xavier Leroy, David Pichardie,... June 13, 2017, Montpellier GdR GPL thesis prize

More information

Lecture 9 Assertions and Error Handling CS240

Lecture 9 Assertions and Error Handling CS240 Lecture 9 Assertions and Error Handling CS240 The C preprocessor The C compiler performs Macro expansion and directive handling Preprocessing directive lines, including file inclusion and conditional compilation,

More information

C++ Undefined Behavior What is it, and why should I care?

C++ Undefined Behavior What is it, and why should I care? C++ Undefined Behavior What is it, and why should I care? Marshall Clow Qualcomm marshall@idio.com http://cplusplusmusings.wordpress.com (intermittent) Twitter: @mclow ACCU 2014 April 2014 What is Undefined

More information

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Lecture 15: Recursive Types & Subtyping CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

More information

Hiding local state in direct style: a higher-order anti-frame rule

Hiding local state in direct style: a higher-order anti-frame rule 1 / 65 Hiding local state in direct style: a higher-order anti-frame rule François Pottier January 28th, 2008 2 / 65 Contents Introduction Basics of the type system A higher-order anti-frame rule Applications

More information

Calculating Correct Compilers

Calculating Correct Compilers university of copenhagen department of computer science Faculty of Science Calculating Correct Compilers Patrick Bahr1 Graham Hutton2 1 University of Copenhagen, Department of Computer Science paba@diku.dk

More information

Translation Validation for a Verified OS Kernel

Translation Validation for a Verified OS Kernel To appear in PLDI 13 Translation Validation for a Verified OS Kernel Thomas Sewell 1, Magnus Myreen 2, Gerwin Klein 1 1 NICTA, Australia 2 University of Cambridge, UK L4.verified sel4 = a formally verified

More information

Chapter 3 (part 3) Describing Syntax and Semantics

Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 (part 3) Describing Syntax and Semantics Chapter 3 Topics Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute Grammars Describing the Meanings

More information

Formal Semantics of Programming Languages

Formal Semantics of Programming Languages Formal Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html Benefits of formal

More information

Information Flow Analysis and Type Systems for Secure C Language (VITC Project) Jun FURUSE. The University of Tokyo

Information Flow Analysis and Type Systems for Secure C Language (VITC Project) Jun FURUSE. The University of Tokyo Information Flow Analysis and Type Systems for Secure C Language (VITC Project) Jun FURUSE The University of Tokyo furuse@yl.is.s.u-tokyo.ac.jp e-society MEXT project toward secure and reliable software

More information

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS Pages 792 to 800 Anna Rakitianskaia, University of Pretoria INITIALISING POINTER VARIABLES Pointer variables are declared by putting

More information

Hoare triples. Floyd-Hoare Logic, Separation Logic

Hoare triples. Floyd-Hoare Logic, Separation Logic Hoare triples Floyd-Hoare Logic, Separation Logic 1. Floyd-Hoare Logic 1969 Reasoning about control Hoare triples {A} p {B} a Hoare triple partial correctness: if the initial state satisfies assertion

More information

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré Hoare Logic COMP2600 Formal Methods for Software Engineering Rajeev Goré Australian National University Semester 2, 2016 (Slides courtesy of Ranald Clouston) COMP 2600 Hoare Logic 1 Australian Capital

More information

Static Program Analysis Part 1 the TIP language

Static Program Analysis Part 1 the TIP language Static Program Analysis Part 1 the TIP language http://cs.au.dk/~amoeller/spa/ Anders Møller & Michael I. Schwartzbach Computer Science, Aarhus University Questions about programs Does the program terminate

More information

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules. Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017 Section 1: Multiple choice (select any that apply) - 20 points 01. Representing 10 using the 4 byte unsigned integer encoding and using 4 byte two s complements encoding yields the same bit pattern. (a)

More information

A Propagation Engine for GCC

A Propagation Engine for GCC A Propagation Engine for GCC Diego Novillo Red Hat Canada dnovillo@redhat.com May 1, 2005 Abstract Several analyses and transformations work by propagating known values and attributes throughout the program.

More information

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

More information

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL

Overview. Probabilistic Programming. Dijkstra s guarded command language: Syntax. Elementary pgcl ingredients. Lecture #4: Probabilistic GCL Overview Lecture #4: Probabilistic GCL 1 Joost-Pieter Katoen 2 3 Recursion RWTH Lecture Series on 2018 Joost-Pieter Katoen 1/31 Joost-Pieter Katoen 2/31 Dijkstra s guarded command language: Syntax Elementary

More information

Lecture 07 Debugging Programs with GDB

Lecture 07 Debugging Programs with GDB Lecture 07 Debugging Programs with GDB In this lecture What is debugging Most Common Type of errors Process of debugging Examples Further readings Exercises What is Debugging Debugging is the process of

More information

CS 351 Design of Large Programs Programming Abstractions

CS 351 Design of Large Programs Programming Abstractions CS 351 Design of Large Programs Programming Abstractions Brooke Chenoweth University of New Mexico Spring 2019 Searching for the Right Abstraction The language we speak relates to the way we think. The

More information

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

More information

Why. an intermediate language for deductive program verification

Why. an intermediate language for deductive program verification Why an intermediate language for deductive program verification Jean-Christophe Filliâtre CNRS Orsay, France AFM workshop Grenoble, June 27, 2009 Jean-Christophe Filliâtre Why tutorial AFM 09 1 / 56 Motivations

More information

Abstract Interpretation

Abstract Interpretation Abstract Interpretation Ranjit Jhala, UC San Diego April 22, 2013 Fundamental Challenge of Program Analysis How to infer (loop) invariants? Fundamental Challenge of Program Analysis Key issue for any analysis

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 4a Andrew Tolmach Portland State University 1994-2017 Semantics and Erroneous Programs Important part of language specification is distinguishing valid from

More information

Qualifying Exam in Programming Languages and Compilers

Qualifying Exam in Programming Languages and Compilers Qualifying Exam in Programming Languages and Compilers University of Wisconsin Fall 1991 Instructions This exam contains nine questions, divided into two parts. All students taking the exam should answer

More information

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes Chapter 13: Reference Why reference Typing Evaluation Store Typings Safety Notes References Computational Effects Also known as side effects. A function or expression is said to have a side effect if,

More information

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community http://csc.cs.rit.edu History and Evolution of Programming Languages 1. Explain the relationship between machine

More information

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium Ornaments in ML Thomas Williams, Didier Rémy Inria - Gallium April 18, 2017 1 Motivation Two very similar functions let rec add m n = match m with Z n S m S (add m n) let rec append ml nl = match ml with

More information

Compiler Construction

Compiler Construction Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: Static Data Structures Outline of Lecture 18 Recap:

More information

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to. Refresher When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to. i.e. char *ptr1 = malloc(1); ptr1 + 1; // adds 1 to pointer

More information

Iteration. Side effects

Iteration. Side effects Computer programming Iteration. Side effects Marius Minea marius@cs.upt.ro 17 October 2017 Assignment operators We ve used the simple assignment: lvalue = expression lvalue = what can be on the left of

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 19 Tuesday, April 3, 2018 1 Introduction to axiomatic semantics The idea in axiomatic semantics is to give specifications

More information

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions?

Lecture 14. No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Lecture 14 No in-class files today. Homework 7 (due on Wednesday) and Project 3 (due in 10 days) posted. Questions? Friday, February 11 CS 215 Fundamentals of Programming II - Lecture 14 1 Outline Static

More information

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics

Semantics. There is no single widely acceptable notation or formalism for describing semantics Operational Semantics There is no single widely acceptable notation or formalism for describing semantics Operational Describe the meaning of a program by executing its statements on a machine, either simulated or actual. The

More information

CS 161 Computer Security

CS 161 Computer Security Wagner Spring 2014 CS 161 Computer Security 1/27 Reasoning About Code Often functions make certain assumptions about their arguments, and it is the caller s responsibility to make sure those assumptions

More information

The Rule of Constancy(Derived Frame Rule)

The Rule of Constancy(Derived Frame Rule) The Rule of Constancy(Derived Frame Rule) The following derived rule is used on the next slide The rule of constancy {P } C {Q} {P R} C {Q R} where no variable assigned to in C occurs in R Outline of derivation

More information

Matching Logic. Grigore Rosu University of Illinois at Urbana-Champaign

Matching Logic. Grigore Rosu University of Illinois at Urbana-Champaign Matching Logic Grigore Rosu University of Illinois at Urbana-Champaign Joint work with Andrei Stefanescu and Chucky Ellison. Started with Wolfram Schulte at Microsoft Research in 2009 Question could it

More information

Deep C (and C++) by Olve Maudal

Deep C (and C++) by Olve Maudal Deep C (and C++) by Olve Maudal http://www.noaanews.noaa.gov/stories2005/images/rov-hercules-titanic.jpg Programming is hard. Programming correct C and C++ is particularly hard. Indeed, it is uncommon

More information

Coq projects for type theory 2018

Coq projects for type theory 2018 Coq projects for type theory 2018 Herman Geuvers, James McKinna, Freek Wiedijk February 6, 2018 Here are five projects for the type theory course to choose from. Each student has to choose one of these

More information

A Formal C Memory Model Supporting Integer-Pointer Casts

A Formal C Memory Model Supporting Integer-Pointer Casts A Formal C Memory Model Supporting Integer-Pointer Casts Abstract The ISO C standard does not specify the semantics of many valid programs that use non-portable idioms such as integerpointer casts. Recent

More information

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor

COSC252: Programming Languages: Semantic Specification. Jeremy Bolton, PhD Adjunct Professor COSC252: Programming Languages: Semantic Specification Jeremy Bolton, PhD Adjunct Professor Outline I. What happens after syntactic analysis (parsing)? II. Attribute Grammars: bridging the gap III. Semantic

More information

Integrating Arrays into the Heap-Hop program prover

Integrating Arrays into the Heap-Hop program prover Integrating Arrays into the Heap-Hop program prover Ioana Cristescu under the supervision of Jules Villard Queen Mary University of London 7 September 2011 1 / 29 Program verification using Heap-Hop Program

More information

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term

Programming in C. Lecture 9: Tooling. Dr Neel Krishnaswami. Michaelmas Term Programming in C Lecture 9: Tooling Dr Neel Krishnaswami Michaelmas Term 2017-2018 1 / 24 Undefined and Unspecified Behaviour 2 / 24 Undefined and Unspecified Behaviour We have seen that C is an unsafe

More information

Static Program Analysis

Static Program Analysis Static Program Analysis Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ws-1617/spa/ Recap: Taking Conditional Branches into Account Extending

More information

Certified Memory Usage Analysis

Certified Memory Usage Analysis Certified Memory Usage Analysis David Cachera, Thomas Jensen, David Pichardie, Gerardo Schneider IRISA, ENS Cachan Bretagne, France Context Embedded devices (smart cards, mobile phones) memory is limited

More information

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile

Page 1. Today. Last Time. Is the assembly code right? Is the assembly code right? Which compiler is right? Compiler requirements CPP Volatile Last Time Today Compiler requirements CPP Volatile Advanced C What C programs mean int my_loop (int base) { int index, count = 0; for (index = base; index < (base+10); index++) count++; urn count; my_loop:

More information

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a.

b. array s first element address c. base address of an array d. all elements of an array e. both b and c 9. An array elements are always stored in a. UNIT IV 1. Appropriately comment on the following declaration int a[20]; a. Array declaration b. array initialization c. pointer array declaration d. integer array of size 20 2. Appropriately comment on

More information

Programming with C Library Functions Safely

Programming with C Library Functions Safely Programming with C Library Functions Safely p.1/39 Programming with C Library Functions Safely Hongwei Xi Boston University Work partly funded by NSF grant CCR-0229480 Programming with C Library Functions

More information

Reasoning about Memory Layouts

Reasoning about Memory Layouts Formal Methods in System Design manuscript No. (will be inserted by the editor) Reasoning about Memory Layouts Holger Gast Received: 28.2.2010 / Accepted: 10.8.2010 Abstract Verification methods for memory-manipulating

More information

The Spin Model Checker : Part I/II

The Spin Model Checker : Part I/II The Spin Model Checker : Part I/II Moonzoo Kim CS Dept. KAIST Korea Advanced Institute of Science and Technology Motivation: Tragic Accidents Caused by SW Bugs 2 Cost of Software Errors June 2002 Software

More information

Undefined Behaviour in C

Undefined Behaviour in C Undefined Behaviour in C Report Field of work: Scientific Computing Field: Computer Science Faculty for Mathematics, Computer Science and Natural Sciences University of Hamburg Presented by: Dennis Sobczak

More information

C Introduction. Comparison w/ Java, Memory Model, and Pointers

C Introduction. Comparison w/ Java, Memory Model, and Pointers CS 261 Fall 2018 Mike Lam, Professor C Introduction Comparison w/ Java, Memory Model, and Pointers Please go to socrative.com on your phone or laptop, choose student login and join room LAMJMU The C Language

More information

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture

Reminder of the last lecture. Aliasing Issues: Call by reference, Pointer programs. Introducing Aliasing Issues. Home Work from previous lecture Reminder of the last lecture Aliasing Issues: Call by reference, Pointer programs Claude Marché Cours MPRI 2-36-1 Preuve de Programme 18 janvier 2017 Additional features of the specification language Abstract

More information

Model Viva Questions for Programming in C lab

Model Viva Questions for Programming in C lab Model Viva Questions for Programming in C lab Title of the Practical: Assignment to prepare general algorithms and flow chart. Q1: What is a flowchart? A1: A flowchart is a diagram that shows a continuous

More information

Compilation and Program Analysis (#11) : Hoare triples and shape analysis

Compilation and Program Analysis (#11) : Hoare triples and shape analysis Compilation and Program Analysis (#11) : Hoare triples and shape analysis Laure Gonnord http://laure.gonnord.org/pro/teaching/capm1.html Laure.Gonnord@ens-lyon.fr Master 1, ENS de Lyon dec 2017 Inspiration

More information

Separation Logic for Small-step C Minor

Separation Logic for Small-step C Minor Separation Logic for Small-step C Minor Andrew W. Appel 1 and Sandrine Blazy 2 1 Princeton University and INRIA Rocquencourt appel@princeton.edu 2 ENSIIE and INRIA Rocquencourt sandrine.blazy@inria.fr

More information

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics

Relation Overriding. Syntax and Semantics. Simple Semantic Domains. Operational Semantics SE3E03, 2006 1.59 61 Syntax and Semantics Syntax Shape of PL constructs What are the tokens of the language? Lexical syntax, word level How are programs built from tokens? Mostly use Context-Free Grammars

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics

Static semantics. Lecture 3-6: Semantics. Attribute grammars (2) Attribute grammars. Attribute grammars example. Dynamic semantics Lecture 3-6: Semantics Static semantics Attribute grammars Dynamic semantics Denotational semantics: semantic equations Axiomatic semantics: inference rules and correctness proofs Static semantics Semantics

More information

Denotational Semantics of a Simple Imperative Language Using Intensional Logic

Denotational Semantics of a Simple Imperative Language Using Intensional Logic Denotational Semantics of a Simple Imperative Language Using Intensional Logic Marc Bender bendermm@mcmaster.ca CAS 706 - Programming Languages Instructor: Dr. Jacques Carette Dept. of Computing and Software

More information

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers. Semantics The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers. The semantics of a programming language assigns a precise

More information