Programming Language Semantics Axiomatic Semantics of Parallel Programs.

Slides:



Advertisements
Similar presentations
Operating Systems Semaphores II
Advertisements

Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 12.
PZ03D Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ03D - Program verification Programming Language Design.
Greta YorshEran YahavMartin Vechev IBM Research. { ……………… …… …………………. ……………………. ………………………… } T1() Challenge: Correct and Efficient Synchronization { ……………………………
50.530: Software Engineering Sun Jun SUTD. Week 13: Rely-Guarantee Reasoning.
CIS 720 Lecture 2. Concurrency …shared variable declaration…. …shared variable initialization… co P 1 // P 2 // P 3 // P 4 oc Execution of P 1 … P 4 starts.
Formal Semantics of Programming Languages 虞慧群 Topic 6: Advanced Issues.
1 1 Regression Verification for Multi-Threaded Programs Sagar Chaki, SEI-Pittsburgh Arie Gurfinkel, SEI-Pittsburgh Ofer Strichman, Technion-Haifa Originally.
Hoare’s Correctness Triplets Dijkstra’s Predicate Transformers
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Formal Semantics of Programming Languages 虞慧群 Topic 5: Axiomatic Semantics.
Program Analysis and Verification
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Programming Language Semantics Rely/Guarantee Reasoning Parallel Programs Tal Lev-Ami Viktor Vafeiadis Mooly Sagiv.
Programming Language Semantics Axiomatic Semantics Chapter 6.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Programming Language Semantics Java Threads and Locks Informal Introduction The Java Specification Language Chapter 17.
Local Reasoning Peter O’Hearn John Reynolds Hongseok Yang.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Chapter 11: Distributed Processing Parallel programming Principles of parallel programming languages Concurrent execution –Programming constructs –Guarded.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
PSUCS322 HM 1 Languages and Compiler Design II Formal Semantics Material provided by Prof. Jingke Li Stolen with pride and modified by Herb Mayer PSU Spring.
Semaphores CSCI 444/544 Operating Systems Fall 2008.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
Operational Semantics Semantics with Applications Chapter 2 H. Nielson and F. Nielson
A. Frank - P. Weisberg Operating Systems Introduction to Cooperating Processes.
CS444/CS544 Operating Systems Classic Synchronization Problems 2/26/2007 Prof. Searleman
Proving Program Correctness The Axiomatic Approach.
Software Verification 1 Deductive Verification Prof. Dr. Holger Schlingloff Institut für Informatik der Humboldt Universität und Fraunhofer Institut.
Verifying a Wait Free Register Algorithm Using Assertional Reasoning Xu Qiwen Faculty of Science and Technology University of Macau.
Monitor  Giving credit where it is due:  The lecture notes are borrowed from Dr. I-Ling Yen at University of Texas at Dallas  I have modified them and.
1 Thread Synchronization: Too Much Milk. 2 Implementing Critical Sections in Software Hard The following example will demonstrate the difficulty of providing.
Concurrency, Mutual Exclusion and Synchronization.
Concurrency Verification. Why concurrency verification Concurrent programs show in many systems –Multi-task support in OS kernels –Handling interrupts.
Program Analysis and Verification Spring 2014 Program Analysis and Verification Lecture 4: Axiomatic Semantics I Roman Manevich Ben-Gurion University.
Automated and Modular Refinement Reasoning for Concurrent Programs Shaz Qadeer.
Background Concurrent access to shared data may result in data inconsistency Maintaining data consistency requires mechanisms to ensure the orderly execution.
Verifying Properties of Parallel Programs: An Axiomatic Approach Susan Owicki and David Gries Communications of the ACM, 1976 Presented by Almog Benin.
Specifying Multithreaded Java semantics for Program Verification Abhik Roychoudhury National University of Singapore (Joint work with Tulika Mitra)
Program Analysis and Verification
Formal Semantics of Programming Languages 虞慧群 Topic 2: Operational Semantics.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Software Verification 1 Deductive Verification Prof. Dr. Holger Schlingloff Institut für Informatik der Humboldt Universität und Fraunhofer Institut.
Operational Semantics Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson
Imperative Programming Statements and invariants.
11/22/2016IT 3271 A formal system:Axioms and Rules, for inferring valid specification x := m; y := n; while ¬(x=y) do if x>y then x := x-y else y := y-x.
Spring 2017 Program Analysis and Verification
Chapter 3 of Programming Languages by Ravi Sethi
Formal methods: Lecture
Background on the need for Synchronization
SS 2017 Software Verification Software Model Checking 2 - Parallelism
Formal Methods in Software Engineering 1
Verification of Concurrent Programs
Programming Languages 2nd edition Tucker and Noonan
Denotational Semantics (Denotational Semantics)
Monitor Giving credit where it is due:
Formal Methods in software development
Program correctness Axiomatic semantics
CIS 720 Lecture 3.
Programming Languages and Compilers (CS 421)
CIS 720 Lecture 3.
CIS 720 Lecture 2.
Programming Languages 2nd edition Tucker and Noonan
CIS 720 Lecture 2.
Program Analysis and Verification
Presentation transcript:

Programming Language Semantics Axiomatic Semantics of Parallel Programs

Tentative Schedule 15/6 Parallel Programs 22/6 Rely/Guarantee Reasoning –Assignment 2 29/6 Separation Logic 6, 3/7 Type Inference –Assignment 3

Plan More on Hoare Proof Rules Proof Rules for Concurrent Programs

Hoare Proof Rules for Partial Correctness {A} skip {A} {B[a/X]} X:=a {B} (Assignment Rule) {P} c 0 {C} {C} c 1 {Q} {P} c 0 ;c 1 {Q} {P  b} c 0 {Q} {P  b} c 1 {Q} {P} if b then c 0 else c 1 {Q} {I  b} c {I} {I} while b do c{I  b}  P  P’ {P’} c {Q’}  Q’  Q {P} c {Q} (Composition Rule) (Conditional Rule) (Loop Rule) (Consequence Rule)

Potential Language Extensions Blocks Procedures –Procedures as parameters Abstract data types Non-determinism Parallelism Arrays, Pointers, and Dynamic allocations

More Rules {P 1 } c {Q 1 } {P 2 } c {Q 2 } {P 1  P 2 } c {Q 1  Q 2 } (Conjunction Rule) {P} c {P} where Mod(c)  Var(P) =  (Invariance Rule)

Underlying Principles of Hoare Rules Commands are predicate transformers Different behaviors for different preconditions Commands can be partial –Resource restriction –Implementation restriction In {P} comm {Q} –P and Q are contracts with the clients of comm

An Axiomatic Proof Technique for Parallel Programs Owicky & Gries Verification of Sequential and Concurrent Programs Apt & Oldrog Chapters 4-6

IMP+ Parallel Constructs Abstract syntax com::= X := a | skip | com 1 ; com 2 | if b then com 1 else com 2 | while b do com| cobegin com 1 || com 2 … || com k coend All the interleavings of are executed Example –cobegin X := 1 || (X :=2 ; X := X+2) coend

A First Attempt {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } {P 1  P 2  … P k } cobegin c 1 || c 2 || …|| c n coend {Q 1  Q 2  …  Q k } (Parallel Rule)

Simple Examples {true} X := 1 { X >0 } {true} Y:= 1 {Y>0} {true} cobegin X :=1 || Y := 1 coend{X>0  Y >0} {true} X := 1 { X >0 } {true} X:=2 ; X := X+2 {X>0} {true} cobegin X :=1 || X:=2 ; X := X+2 coend{X>0 }

Unsoundness {Y=1} X := 0 { Y=1 } {true} Y:=0 {true} {Y=1} cobegin X :=0 || Y:=0 coend {Y=1 }

Modified Parallel Rule {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } {P 1  P 2  … P k } cobegin c 1 || c 2 || …|| c k coend {Q 1  Q 2  …  Q k } Mod(c j )  Var(P i ) =  Mod(c j )  Var(Q i ) =  i  j

Unsoundness from sharing {true} X := 1 { X >0 } {true} X:=2 ; X := X+2 {X>3} {true} cobegin X :=1 || X:=2 ; X := X+2 coend {X>3 }

Handling Shared Variables Carefully design the proofs of every thread to make them local Show that the code of other threads do not interfere with the proofs of other threads

Interference A command T with a precondition pre(T) does not interfere with the proof of {P} C {Q} if: –{Q  pre(T) } T {Q} –For any command C’ inside C with a precondition pre(C’) {pre(C’)  pre(T) } T {pre(C’)} {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } are interference free if for every i  j and for every assignment in T in c i does not interfere with {P j } c j {Q j }

Modified Parallel Rule {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } {P 1  P 2  … P k } cobegin c 1 || c 2 || …|| c k coend {Q 1  Q 2  …  Q k } {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } are interference free

No unsoundness from sharing {true} X := 1 { X >0 } {true} X:=2 ; X := X+2 {X>3} {true} cobegin X :=1 || X:=2 ; X := X+2 coend {X>3 }

A Realistic Example Findpos: begin initialize: i :=2 ; j :=1; eventop = M+1 ; oddtop := M+1; search: cobegin Evensearch: while i < min(oddtop, eventop) do if (x(i) > 0) then eventop := i else i := i + 2; || Oddsearch: while j < min(oddtop, eventop) do if (x(j) > 0) then oddtop := j else j := j + 2; coend k := min(eventop, oddtop) end {k  M+1  (  l: 1  l 0)} ES=eventop  M+1  even(i)   l: (even(l)  0<l<i)  x(l)  0  eventop  M  x(eventop)>0 OS=oddtop  M+1  odd(j)   l: (odd(l)  0<l<j)  x(l)  0  oddtop  M  x(oddtop)>0

Incompleteness There exist correct programs which cannot be verified by the parallelization rule –{X = Y} cobegin X := X + 1 || Y := Y+1 coend {X=Y}

An Informal Proof of {X = Y} cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X = Z} X : = X + 1 { X = Z +1} {Y = Z} Y : = Y + 1 { Y = Z +1} {X = Z  Y = Z} cobegin X := X + 1 || Y := Y+1 coend {X=Z+1  Y= Z+1} {X = Z  Y = Z} cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X = Y} Z : = X { X = Y  Y = Z} {X =Y } Z : = X ; cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X =Y } cobegin X := X + 1 || Y := Y+1 coend {X=Y}

Auxiliary Variables Record history information A set of variables AV is auxiliary for a command S if each variable from AV only occurs in assignments of the form X := t where X  AV Auxiliary variable elimination rule {P} S {Q} {P} S’ {Q} Where A is auxiliary for S FV(Q)  A =  FV(P)  A =  S’ results from S by deleting all assignments in to variables in S

A Formal Proof of {X = Y} cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X = Z} X : = X + 1 { X = Z +1} {Y = Z} Y : = Y + 1 { Y = Z +1} {X = Z  Y = Z} cobegin X := X + 1 || Y := Y+1 coend {X=Z+1  Y= Z+1} {X = Z  Y = Z} cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X = Y} Z : = X { X = Y  Y = Z} {X =Y } Z : = X ; cobegin X := X + 1 || Y := Y+1 coend {X=Y} {X =Y } cobegin X := X + 1 || Y := Y+1 coend {X=Y} (Assign-Rule) (Par-Rule) (Cons.-Rule) (Assign-Rule) (Comp.-Rule) (Aux.-Rule)

Synchronization Primitives Support for atomic sections and communication A High Level Construct await B then S –No parallelization in S –Blocked until B holds –If B is true then S is executed atomically Examples –await true then S –Await “some condition” then skip –Semaphore P(sem): await sem >0 then sem := sem – 1 V(sem): await true then sem := sem + 1

Interference (modified) A command T with a precondition pre(T) does not interfere with the proof of {P} C {Q} if: –{Q  pre(T) } T {Q} –For any command C’ inside C but not within await with a precondition pre(C’) {pre(C’)  pre(T) } T {pre(C’)} {P 1 } c 1 {Q 1 } {P 2 } c 2 {Q 2 }.. {P k } c k {Q k } are interference free if for every i  j and for every assignment not inside await or an await T in c i does not interfere with {P j } c j {Q j }

An Inference Rule for Await {P  b} c {Q} {P } await B then c {Q}

Producer-Consumer Two processes communicating via a shared bounded buffer Consumer waits for the buffer to be filled Producer waits for the buffer to be non-full and fills the buffer buffer[0..N-1] in – the number of elements added out – the number of elements deleted buffer[out mod N], … buffer[(out + in – out -1) mod N] elements

in := 0; out := 0 ; cobegin producer: … await in-out < N then skip add: buffer(in mod N) := next value; markin: in := in +1; … || consumer: … await in-out >0 then skip remove: this value := buffer(out mod N); markout: out := out +1; … coend Producer/Consumer Code

in := 0; out := 0 ; i := 1; j :=1; {M  0} cobegin producer: while i  M do begin x:= A[i]; await in-out <N then skip; add: buffer[in mod N] := x ; markin: in := in + 1; i := i + 1 end || consumer: while j  M do begin await in-out >0 then skip; remove: y := buffer[out mod N] ; markout: out := out + 1; B[j] := y; j := j + 1 end coend {  k: 1  k  M  B[k] = A[k]} I=  k: out < k  M  buffer[(k-1} mod N] = A[k]  0  in-out  N  0  i  M+1  0  j  M+1

{I  i = in+1=1  j=out+1=1} cobegin {I  i = in+1=1} producer: while i  M do begin x:= A[i]; await in-out <N then skip; add: buffer[in mod N] := x ; markin: in := in + 1; i := i + 1 end {I  i = in+1=M+1} || {I  j = out+1=1} consumer: while j  M do begin await in-out >0 then skip; remove: y := buffer[out mod N] ; markout: out := out + 1; B[j] := y; j := j + 1 end {I   k: 1  k  M  B[k] = A[k]}} coend {  k: 1  k  M  B[k] = A[k]} I=  k: out < k  M  buffer[(k-1} mod N] = A[k]  0  in-out  N  0  i  M+1  0  j  M+1

Summary Reasoning about concurrent programs is difficult Aweeki-Gries suggest to carefully design the sequential proofs to simplify the proof procedure The use of auxiliary variables can make proofs difficult Can have difficulties with fine-grained concurrency –Benign dataraces Rely/Guarantee style can allow more elegant/general reasoning (next lesson)