RSL Spinlock Example

(RSLSpinlock) Encoding of Figure 7 Spinlock example from RSL paper

//:: IgnoreFile(/carbon/issue/280/) // This example is the hand-encoded version of Figure 7 from the original RSL paper // "Relaxed separation logic: a program logic for C11 concurrency", by Vafeiadis and Narayan // Note that verification times via the web frontend are considerably longer than with the desktop application. // PART 1: translation of code // J(z) represents the assertion guarded by the lock // Q0(v) := (v == 0 ? true : v == 1 ? J(z) && realRef(z) : false) // Lock(x) := Rel(x,Q0) && RMWAcq(x,Q0) && Init(x) && realRef(x) method new_lock(z:Ref) returns (x: Ref) requires realRef(z) && J(z) ensures Lock(x) { // x := alloc_RMW(Q0) // for CAS reads allocAtomic(x,0,true) // [x]_rel := 1 releaseWrite(x,1) } method unlock(x: Ref, z:Ref) requires Lock(x) && realRef(z) && J(z) ensures Lock(x) { // [x]_rel := 1 releaseWrite(x,1) } method lock(x: Ref, z:Ref) requires realRef(z) && Lock(x) ensures realRef(z) && J(z) && Lock(x) { var t : Int // t := CAS(x,1,0)_acq CAS(x,1,0,t,true,false) while(t != 1) invariant Lock(x) && (t == 1 ==> J(z)) { // spin with relaxed reads (no ownership transfer) // while ([x]_rlx == 0) { skip } waitOnRelaxedRead(x,0) // t := CAS(x,1,0)_acq CAS(x,1,0,t,true,false) } } // PART 2: invariant definitions + example-specific background definitions // example specifics predicate J(z:Ref) // placeholder for assertion guarded by the lock // Lock(x) := Rel(x,Q0) && RMWAcq(x,Q0) && Init(x) && realRef(x) define Lock(x) realRef(x) && Rel(x,0) && RMWAcq(x,0) && Init(x) // Q0(v) := (v == 0 ? true : v == 1 ? J(z) && realRef(z) : false) define INV0MaybeUp(v,sync) (v == 0 ? true : v == 1 ? realRef(z) && J(maybeUp(z,!sync)) : false) define INV0MaybeDown(v,sync) (v == 0 ? true : v == 1 ? realRef(z) && J(maybeDown(z,!sync)) : false) define INV0TempOrMaybeUp(v,sync) (v == 0 ? true : v == 1 ? realRef(z) && acc(J(maybeUp(z,!sync)),write - min(write,perm(J(temp(z))))) && acc(J(temp(z)),min(write,perm(J(temp(z))))) : false) define INV0Temp(v) (v == 0 ? true : v == 1 ? realRef(z) && J(temp(z)) : false) define INV1Temp(v) true // not used for this example define INV1TempOrMaybeUp(v,sync) true // not used for this example define INV1MaybeUp(v,sync) true // not used for this example define INV1MaybeDown(v,sync) true // not used for this example define INV2Temp(v) true // not used for this example define INV2TempOrMaybeUp(v,sync) true // not used for this example define INV2MaybeUp(v,sync) true // not used for this example define INV2MaybeDown(v,sync) true // not used for this example // Part 3: standard background definitions (common for all examples) define min(a,b) (a < b ? a : b) method havocedInt() returns (x:Int) method havocedBool() returns (x:Bool) method havocedRefSet() returns (x:Set[Ref]) field val : Int field init: Bool // value is used for nonatomics, only permissions are used for atomics field rel: Int field acq: Bool // use true to indicate RMWAcq, false to indicate normal Acq define realRef(x) !is_ghost(x) && heap(x) == 0 define ghostRef(x) is_ghost(x) && heap(x) == 0 define SomeRel(x) acc(x.rel, wildcard) define SomeAcq(x) acc(x.acq, wildcard) && x.acq == true define SomeRMWAcq(x) acc(x.acq, wildcard) && x.acq == false define SomeAcqOrRMWAcq(x) acc(x.acq, wildcard) // these shorthands work if the parameter to the Acq is representable by a single indexed assertion // (otherwise we have to expand by hand to e.g. SomeAcq(x) && AcqConjunct(x,i1) && AcqConjunct(x,i2) ...) define Acq(x, idx) SomeAcq(x) && AcqConjunct(x, idx) && // here, we take care to (re-)initialise the value of valsRead *only* if we didn't already hold a copy of this conjunct (i.e., we hold exactly one (1/1) now) // A Viper inhale-exhale assertion [A1,A2] has the meaning that A1 is used when inhaling (where the care here must be taken), and A2 when exhaling [perm(AcqConjunct(x, idx)) == 1/1 ==> valsRead(x,idx) == Set[Int](), valsRead(x,idx) == Set[Int]()] define Rel(x, idx) SomeRel(x) && x.rel == idx define RMWAcq(x, idx) SomeRMWAcq(x) && acc(AcqConjunct(x, idx), wildcard) predicate AcqConjunct(x: Ref, idx: Int) function AcqConjunctTrigger(x: Ref, idx: Int) : Bool { true } // used for triggering quantifiers regarding AcqConjunct predicates (since Viper doesn't support predicate instances as triggers, directly) function valsRead(x: Ref, index:Int) : Set[Int] requires AcqConjunct(x,index) define hasAcqConjunct(x,idx,isRMW) (isRMW ? perm(AcqConjunct(x,idx)) > none : perm(AcqConjunct(x,idx)) >= write) && AcqConjunctTrigger(x,idx) define Init(x) acc(x.init, wildcard) define Uninit(x) acc(x.init) && acc(x.val) && !x.init define PointsTo(x,v) acc(x.init) && acc(x.val) && x.init && x.val == v domain parallelHeaps { function up(x: Ref) : Ref function down(x: Ref) : Ref function up_inv(x: Ref) : Ref function down_inv(x: Ref) : Ref function temp(x: Ref) : Ref function temp_inv(x: Ref) : Ref function heap(x: Ref) : Int function is_ghost(x:Ref) : Bool axiom inv_up { forall r:Ref :: {up(r)} up_inv(up(r)) == r && (is_ghost(r) ? up(r) == r : heap(r)==0 ==> heap(up(r)) == 1) } axiom inv_up_inv { forall r:Ref :: {up_inv(r)} up(up_inv(r)) == r && (is_ghost(r) ? up_inv(r) == r : heap(r)==1 ==> heap(up_inv(r)) == 0) } axiom inv_down { forall r:Ref :: {down(r)} down_inv(down(r)) == r && (is_ghost(r) ? down(r) == r : heap(r)==0 ==> heap(down(r)) == -1) } axiom inv_down_inv { forall r:Ref :: {down_inv(r)} down(down_inv(r)) == r && (is_ghost(r) ? down_inv(r) == r : heap(r)==-1 ==> heap(down_inv(r)) == 0) } axiom inv_temp { forall r:Ref :: {temp(r)} temp_inv(temp(r)) == r && (is_ghost(r) ? temp(r) == r : heap(r)==0 ==> heap(temp(r)) == -3) } axiom inv_temp_inv { forall r:Ref :: {temp_inv(r)} temp(temp_inv(r)) == r && (is_ghost(r) ? temp_inv(r) == r : heap(r)==-3 ==> heap(temp_inv(r)) == 0) } } define allocAtomic(x, idx, isRMW) { x := new() assume realRef(x) inhale SomeRel(x) && x.rel == idx && (isRMW ? SomeRMWAcq(x) && acc(AcqConjunct(x,idx),wildcard) : SomeAcq(x) && AcqConjunct(x,idx) && valsRead(x,idx) == Set[Int]()) } define relaxedWrite(x,v) { atomicWrite(x,v,false) } define releaseWrite(x,v) { atomicWrite(x,v,true) } define atomicWrite(x, v, sync) { assert SomeRel(x) atomicWriteExhale(x, v, sync) inhale Init(x) } define waitOnRelaxedRead(x,v) { waitOnAtomicRead(x,v,false) } define waitOnAcquireRead(x,v) { waitOnAtomicRead(x,v,true) } define waitOnAtomicRead(x,v,sync) { var tmp: Int // used as temporary variable for atomic reads var intSet : Set[Int] waitOnAtomicReadWithVars(x,v,sync,tmp,intSet) } define waitOnAtomicReadWithVars(x,v,sync,tmpInt,tmpIntSet) { atomicReadWithVar(x,tmpInt,sync,tmpIntSet) if(tmpInt == v) { // all *subsequent* reads of the same value v will not change anything - only the *last* read (which breaks out of the loop) makes a change to what we hold (and the invariant) atomicReadWithVar(x,tmpInt,sync,tmpIntSet) // assign the *last* value read assume tmpInt != v } } define waitOnCAS(x,v1,v2, readSync, writeSync) { var tmpInt : Int waitOnCASWithVars(x,v1,v2, readSync, writeSync, tmpInt) } define waitOnCASWithVars(x,v1,v2, readSync, writeSync, tmpInt) { // all failing CAS operations will not affect what we hold; they can be skipped, here! CAS(x, v1, v2, tmpInt, readSync, writeSync) // model the *last* CAS, which succeeds, breaking out of the loop assume tmpInt == v1 // the last CAS succeeds } define atomicRead(x, tmp, sync) { var tmpSetAtomicRead : Set[Int] atomicReadWithVar(x, tmp, sync, tmpSetAtomicRead) } define atomicReadWithVar(x, tmp, sync, tmpSet) { assert Init(x) && SomeAcqOrRMWAcq(x) tmp := havocedInt() if(x.acq) { // we have the usual Acq permission atomicReadInhaleWithVar(x, tmp, sync, false, tmpSet) } //else { // attempting atomic read with only RMWAcq permission // CASFailedRead(x,tmp) //} } define relaxedRead(x, tmp) { var tmpSet : Set[Int] relaxedReadWithVar(x, tmp, tmpSet) } define relaxedReadWithVar(x, tmp, tmpSet) { atomicReadWithVar(x, tmp, false, tmpSet) } // Could add macro for general loops define allocNonAtomic(x) { x := new() assume realRef(x) inhale Uninit(x) } define allocGhost(x) { x := new() assume ghostRef(x) inhale Uninit(x) } define CAS(x, v1, v2, tmp, readSync, writeSync) { var tmpIntSet : Set[Int] var tmpRefSet : Set[Ref] CASWithVar(x, v1, v2, tmp, readSync, writeSync, tmpIntSet, tmpRefSet) } define CASWithVar(x, v1, v2, tmp, readSync, writeSync, tmpIntSet, tmpRefSet) { CASGeneralised(x, tmp, tmp==v1, v2, readSync, writeSync, tmpIntSet, tmpRefSet) } define CASGeneralised(x, tmp, condition, newVal, readSync, writeSync, tmpIntSet, tmpRefSet) { assert Init(x) && SomeRMWAcq(x) tmp := havocedInt() if(condition) { if(readSync && writeSync) { // can use RSL rule atomicReadInhaleWithVar(x, tmp, readSync, true, tmpIntSet) atomicWrite(x, newVal, writeSync) } else { CASReadInhaleToTempHeapWithVar(x,tmp,tmpIntSet) atomicWriteExhaleGeneralised(x, newVal, writeSync, true) moveTempHeap(tmpRefSet,readSync) } } //else { //CASFailedRead(x,tmp) //} } define fetchUpdate(x, tmp, newVal, readSync, writeSync) { var tmpIntSet : Set[Int] var tmpRefSet : Set[Ref] fetchUpdateWithVar(x, tmp, newVal, readSync, writeSync, tmpIntSet, tmpRefSet) } // we model a fetchUpdate as a CAS which can never fail (there is no condition on the value read) define fetchUpdateWithVar(x, tmp, newVal, readSync, writeSync, tmpIntSet, tmpRefSet) { CASGeneralised(x, tmp, true, newVal, readSync, writeSync, tmpIntSet, tmpRefSet) } // note: this macro always picks the same variable names, so doesn't work multiple times in the same scope (since we don't have local variable scopes). Use the macro below if this is problematic (and declare the variables outside) define FENCEAcq() { var refSet : Set[Ref] FENCEAcqWithVars(refSet) } define maybeDown(x, goDown) (goDown ? down(x) : x) define maybeUp(x, goUp) (goUp ? up(x) : x) define atomicWriteExhale(x, v, sync) { atomicWriteExhaleGeneralised(x,v,sync,false) } define rewriteAcqConjuncts(x,isRMW,oldAcqSet,newAcqSet) { var tmpBool : Bool var tmpInt : Int rewriteAcqConjunctsWithVar(x,isRMW,oldAcqSet,newAcqSet,tmpBool,tmpInt) } // The following definitions need to be expanded to cover at least enough indices for the invariant numbering (but are otherwise independent of the example): define FENCEAcqWithVars(refSet) { // move all locations r to which permission to "init" is held from "down" heap to normal heap refSet := havocedRefSet() assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(down(r).init) > none assume forall r: Ref :: {down(r).init} perm(down(r).init) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(r.init, perm(down(r).init)) assume forall r: Ref :: {r in refSet}{down(r)} r in refSet ==> r.init == down(r).init exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(down(r).init, perm(down(r).init)) refSet := havocedRefSet() // move all locations r to which permission to "vals" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(down(r).val) > none assume forall r: Ref :: {down(r).val} perm(down(r).val) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(r.val, perm(down(r).val)) assume forall r: Ref :: {r in refSet}{down(r)} r in refSet ==> r.val == down(r).val exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(down(r).val, perm(down(r).val)) refSet := havocedRefSet() // move all locations r to which permission to "rel" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(down(r).rel) > none assume forall r: Ref :: {down(r).rel} perm(down(r).rel) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(r.rel, perm(down(r).rel)) assume forall r: Ref :: {r in refSet}{down(r)} r in refSet ==> r.rel == down(r).rel exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(down(r).rel, perm(down(r).rel)) refSet := havocedRefSet() // move all locations r to which permission to "acq" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(down(r).acq) > none assume forall r: Ref :: {down(r).acq} perm(down(r).acq) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(r.acq, perm(down(r).acq)) assume forall r: Ref :: {r in refSet}{down(r)} r in refSet ==> r.acq == down(r).acq exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(down(r).acq, perm(down(r).acq)) // UNROLL per indexed invariant (0,1, ...) and for both RelDisjunct and AcqConjunct... refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,0)" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(down(r),0)) > none assume forall r: Ref :: {down(r)} perm(AcqConjunct(down(r),0)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> acc(AcqConjunct(r,0), perm(AcqConjunct(down(r),0))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(r,0) == valsRead(down(r),0) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(down(r),0), perm(AcqConjunct(down(r),0))) refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,1)" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(down(r),1)) > none assume forall r: Ref :: {down(r)} perm(AcqConjunct(down(r),1)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> acc(AcqConjunct(r,1), perm(AcqConjunct(down(r),1))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(r,1) == valsRead(down(r),1) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(down(r),1), perm(AcqConjunct(down(r),1))) refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,2)" is held from "down" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(down(r),2)) > none assume forall r: Ref :: {down(r)} perm(AcqConjunct(down(r),2)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> acc(AcqConjunct(r,2), perm(AcqConjunct(down(r),2))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(r,2) == valsRead(down(r),2) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(down(r),2), perm(AcqConjunct(down(r),2))) refSet := havocedRefSet() // move all locations r to which permission to "J(r)" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(J(down(r))) > none assume forall r: Ref :: {down(r)} perm(J(down(r))) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> acc(J(r), perm(J(down(r)))) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(J(down(r)), perm(J(down(r)))) } define moveTempHeap(refSet, readSync) { // move all locations r to which permission to "init" is held from "temp" heap to normal heap refSet := havocedRefSet() assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(temp(r).init) > none assume forall r: Ref :: {temp(r).init} perm(temp(r).init) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(maybeDown(r,!readSync).init, perm(temp(r).init)) assume forall r: Ref :: {r in refSet}{temp(r)} r in refSet ==> maybeDown(r,!readSync).init == temp(r).init exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(temp(r).init, perm(temp(r).init)) refSet := havocedRefSet() // move all locations r to which permission to "vals" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(temp(r).val) > none assume forall r: Ref :: {temp(r)} perm(temp(r).val) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(maybeDown(r,!readSync).val, perm(temp(r).val)) assume forall r: Ref :: {r in refSet}{temp(r)} r in refSet ==> maybeDown(r,!readSync).val == temp(r).val exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(temp(r).val, perm(temp(r).val)) refSet := havocedRefSet() // move all locations r to which permission to "rel" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(temp(r).rel) > none assume forall r: Ref :: {temp(r).rel} perm(temp(r).rel) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(maybeDown(r,!readSync).rel, perm(temp(r).rel)) assume forall r: Ref :: {r in refSet}{temp(r)} r in refSet ==> maybeDown(r,!readSync).rel == temp(r).rel exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(temp(r).rel, perm(temp(r).rel)) refSet := havocedRefSet() // move all locations r to which permission to "acq" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(temp(r).acq) > none assume forall r: Ref :: {temp(r).acq} perm(temp(r).acq) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(maybeDown(r,!readSync).acq, perm(temp(r).acq)) assume forall r: Ref :: {r in refSet}{temp(r)} r in refSet ==> maybeDown(r,!readSync).acq == temp(r).acq exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(temp(r).acq, perm(temp(r).acq)) // UNROLL per indexed invariant (0,1, ...) for AcqConjunct... refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,0)" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(temp(r),0)) > none assume forall r: Ref :: {temp(r)} perm(AcqConjunct(temp(r),0)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(maybeDown(r,!readSync),0), perm(AcqConjunct(temp(r),0))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(maybeDown(r,!readSync),0) == valsRead(temp(r),0) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(temp(r),0), perm(AcqConjunct(temp(r),0))) refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,1)" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(temp(r),1)) > none assume forall r: Ref :: {temp(r)} perm(AcqConjunct(temp(r),1)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(maybeDown(r,!readSync),1), perm(AcqConjunct(temp(r),1))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(maybeDown(r,!readSync),1) == valsRead(temp(r),1) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(temp(r),1), perm(AcqConjunct(temp(r),1))) refSet := havocedRefSet() // move all locations r to which permission to "AcqConjunct(r,2)" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(AcqConjunct(temp(r),2)) > none assume forall r: Ref :: {temp(r)} perm(AcqConjunct(temp(r),2)) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(maybeDown(r,!readSync),2), perm(AcqConjunct(temp(r),2))) assume forall r: Ref :: {r in refSet} {down(r)} r in refSet ==> valsRead(maybeDown(r,!readSync),2) == valsRead(temp(r),2) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(AcqConjunct(temp(r),2), perm(AcqConjunct(temp(r),2))) refSet := havocedRefSet() // move all locations r to which permission to "J(r)" is held from "temp" heap to normal heap assume forall r: Ref :: {r in refSet} r in refSet ==> heap(r) == 0 && !is_ghost(r) && perm(J(temp(r))) > none assume forall r: Ref :: {temp(r)} perm(J(temp(r))) > none && !is_ghost(r) ==> r in refSet inhale forall r: Ref :: {r in refSet} r in refSet ==> acc(J(maybeDown(r,!readSync)), perm(J(temp(r)))) exhale forall r: Ref :: {r in refSet} r in refSet ==> acc(J(temp(r)), perm(J(temp(r)))) } define rewriteAcqConjunctsWithVar(x,isRMW,oldAcqSet,newAcqSet,tmpBool,tmpInt) { assert SomeAcq(x) tmpBool := havocedBool() if(tmpBool) { // check rewriting is justified // remove all permissions from current state exhale forall r: Ref :: r != null ==> acc(r.init, perm(r.init)) exhale forall r: Ref :: r != null ==> acc(r.val, perm(r.val)) exhale forall r: Ref :: r != null ==> acc(r.rel, perm(r.rel)) exhale forall r: Ref :: r != null ==> acc(r.acq, perm(r.acq)) //exhale forall r: Ref :: r != null ==> acc(AcqConjunct(r,0), (perm(AcqConjunct(r,0)) > none ? perm(AcqConjunct(r,0)) : none)) //exhale forall r: Ref :: r != null ==> acc(AcqConjunct(r,1), perm(AcqConjunct(r,1))) //exhale forall r: Ref :: r != null ==> acc(AcqConjunct(r,2), perm(AcqConjunct(r,2))) tmpInt := havocedInt() // UNROLL per numbered invariant if(0 in oldAcqSet) { inhale INV0MaybeDown(tmpInt,true) } if(1 in oldAcqSet) { inhale INV1MaybeDown(tmpInt,true) } if(2 in oldAcqSet) { inhale INV2MaybeDown(tmpInt,true) } // UNROLL per numbered invariant if(0 in newAcqSet) { exhale INV0MaybeDown(tmpInt,true) } if(1 in newAcqSet) { exhale INV1MaybeDown(tmpInt,true) } if(2 in newAcqSet) { exhale INV2MaybeDown(tmpInt,true) } assume false // kill this branch - we've generated sufficient checks to ensure that the rewriting is OK } if(isRMW) { exhale forall i:Int :: i in oldAcqSet ==> acc(AcqConjunct(x,i),perm(AcqConjunct(x,i))) inhale forall i:Int :: i in newAcqSet ==> acc(AcqConjunct(x,i),wildcard) } else { exhale forall i:Int :: i in oldAcqSet ==> AcqConjunct(x,i) && valsRead(x,i) == Set[Int]() inhale forall i:Int :: i in newAcqSet ==> AcqConjunct(x,i) && valsRead(x,i) == Set[Int]() } } // These definitions need to be expanded to cover at least enough indices for the invariant numbering (but are otherwise independent of the example): define allocAtomicIndexSet(x, RelInv, AcqInvSet, isRMW) { x := new() assume realRef(x) inhale Rel(x, RelInv) && (isRMW ? SomeRMWAcq(x) : SomeAcq(x)) // UNROLL per numbered invariant if (0 in AcqInvSet) { inhale isRMW ? acc(AcqConjunct(x,0),wildcard) : AcqConjunct(x,0) && valsRead(x,0) == Set[Int]() } if (1 in AcqInvSet) { inhale isRMW ? acc(AcqConjunct(x,1),wildcard) : AcqConjunct(x,1) && valsRead(x,1) == Set[Int]() } if (2 in AcqInvSet) { inhale isRMW ? acc(AcqConjunct(x,2),wildcard) : AcqConjunct(x,2) && valsRead(x,2) == Set[Int]() } } define atomicWriteExhaleGeneralised(x, v, sync, useTempHeap) { // UNROLL per numbered invariant if(x.rel == 0) { if(useTempHeap) { exhale INV0TempOrMaybeUp(v,sync) } else { exhale INV0MaybeUp(v,sync) } } elseif(x.rel == 1) { if(useTempHeap) { exhale INV1TempOrMaybeUp(v,sync) } else { exhale INV1MaybeUp(v,sync) } } elseif(x.rel == 2) { if(useTempHeap) { exhale INV2TempOrMaybeUp(v,sync) } else { exhale INV2MaybeUp(v,sync) } } else { assert false // this cannot happen - no Rel invariant was held! } } define atomicReadInhaleWithVar(x, v, sync, isRMW, tmpSet) { // UNROLL per numbered invariant if(hasAcqConjunct(x,0,isRMW) && (isRMW || !(v in valsRead(x,0)))) { if(!isRMW) { tmpSet := valsRead(x,0) exhale AcqConjunct(x,0) assert perm(AcqConjunct(x,0)) == none // we don't support multiple copies of same AcqConjunct, yet inhale AcqConjunct(x,0) && valsRead(x,0) == tmpSet union Set(v) } inhale INV0MaybeDown(v,sync) } if(hasAcqConjunct(x,1,isRMW) && (isRMW || !(v in valsRead(x,1)))) { if(!isRMW) { tmpSet := valsRead(x,1) exhale AcqConjunct(x,1) assert perm(AcqConjunct(x,1)) == none // we don't support multiple copies of same AcqConjunct, yet inhale AcqConjunct(x,1) && valsRead(x,1) == tmpSet union Set(v) } inhale INV1MaybeDown(v,sync) } if(hasAcqConjunct(x,2,isRMW) && (isRMW || !(v in valsRead(x,2)))) { if(!isRMW) { tmpSet := valsRead(x,2) exhale AcqConjunct(x,2) assert perm(AcqConjunct(x,2)) == none // we don't support multiple copies of same AcqConjunct, yet inhale AcqConjunct(x,2) && valsRead(x,2) == tmpSet union Set(v) } inhale INV2MaybeDown(v,sync) } } define CASReadInhaleToTempHeapWithVar(x,v,tmpSet) { // UNROLL per numbered invariant if(hasAcqConjunct(x,0,true)) { inhale INV0Temp(v) } if(hasAcqConjunct(x,1,true)) { inhale INV1Temp(v) } if(hasAcqConjunct(x,2,true)) { inhale INV2Temp(v) } }