summaryrefslogtreecommitdiff
path: root/Test/civl/civl-paper.bpl
blob: 6cac5cea0116e42fc7be1b895ed617e1e4e8a1e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// RUN: %boogie -noinfer -typeEncoding:m -useArrayTheory "%s" > "%t"
// RUN: %diff "%s.expect" "%t"
type X;
const nil: X;

function {:builtin "MapConst"} MapConstBool(bool) : [X]bool;
function {:inline} {:linear "tid"} TidCollector(x: X) : [X]bool
{
  MapConstBool(false)[x := true]
}

type lmap;
function {:linear "mem"} dom(lmap): [int]bool;
function map(lmap): [int]int;
function cons([int]bool, [int]int) : lmap;
axiom (forall x: [int]bool, y: [int]int :: {cons(x,y)} dom(cons(x, y)) == x && map(cons(x,y)) == y);

var {:layer 0,3} {:linear "mem"} g: lmap;
var {:layer 0,3} lock: X;
var {:layer 0,1} b: bool;

const p: int;

procedure {:yields} {:layer 1} Yield1()
requires {:layer 1} InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
    yield;
    assert {:layer 1} InvLock(lock, b);
}

function {:inline} InvLock(lock: X, b: bool) : bool
{
    lock != nil <==> b
}

procedure {:yields} {:layer 2} Yield2()
{
    yield;
}

procedure {:yields} {:layer 3} Yield3()
requires {:layer 3} Inv(g);
ensures {:layer 3} Inv(g);
{
    yield;
    assert {:layer 3} Inv(g);
}

function {:inline} Inv(g: lmap) : bool
{
    dom(g)[p] && dom(g)[p+4] && map(g)[p] == map(g)[p+4]
}

procedure {:yields} {:layer 3} P({:linear "tid"} tid: X)
requires {:layer 1} tid != nil && InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
requires {:layer 3} tid != nil && Inv(g);
ensures {:layer 3} Inv(g);
{
    var t: int;
    var {:linear "mem"} l: lmap;

    par Yield3() | Yield1();
    call AcquireProtected(tid);
    call l := TransferFromGlobalProtected(tid);
    call t := Load(l, p);
    call l := Store(l, p, t+1);
    call t := Load(l, p+4);
    call l := Store(l, p+4, t+1);
    call TransferToGlobalProtected(tid, l);
    call ReleaseProtected(tid);
    par Yield3() | Yield1();
}


procedure {:yields} {:layer 2,3} TransferToGlobalProtected({:linear "tid"} tid: X, {:linear_in "mem"} l: lmap)
ensures {:both} |{ A: assert tid != nil && lock == tid; g := l; return true; }|;
requires {:layer 1} InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
  par Yield1() | Yield2();
  call TransferToGlobal(tid, l);
  par Yield1() | Yield2();
}

procedure {:yields} {:layer 2,3} TransferFromGlobalProtected({:linear "tid"} tid: X) returns ({:linear "mem"} l: lmap)
ensures {:both} |{ A: assert tid != nil && lock == tid; l := g; return true; }|;
requires {:layer 1} InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
  par Yield1() | Yield2();
  call l := TransferFromGlobal(tid);
  par Yield1() | Yield2();
}

procedure {:yields} {:layer 2,3} AcquireProtected({:linear "tid"} tid: X)
ensures {:right} |{ A: assert tid != nil; assume lock == nil; lock := tid; return true; }|;
requires {:layer 1} tid != nil && InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
  par Yield1() | Yield2();
  call Acquire(tid);
  par Yield1() | Yield2();
}

procedure {:yields} {:layer 2,3} ReleaseProtected({:linear "tid"} tid: X)
ensures {:left} |{ A: assert tid != nil && lock == tid; lock := nil; return true; }|;
requires {:layer 1} InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
  par Yield1() | Yield2();
  call Release(tid);
  par Yield1() | Yield2();
}

procedure {:yields} {:layer 1,2} Acquire({:linear "tid"} tid: X)
requires {:layer 1} tid != nil && InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
ensures {:atomic} |{ A: assume lock == nil; lock := tid; return true; }|;
{
    var status: bool;
    var tmp: X;

    par Yield1();
    L: 
	assert {:layer 1} InvLock(lock, b);
        call status := CAS(tid, false, true);
	par Yield1();
        goto A, B;

    A: 
        assume status;
	par Yield1();	
	return;

    B:
        assume !status;
	goto L;
}

procedure {:yields} {:layer 1,2} Release({:linear "tid"} tid: X)
ensures {:atomic} |{ A: lock := nil; return true; }|;
requires {:layer 1} InvLock(lock, b);
ensures {:layer 1} InvLock(lock, b);
{
    par Yield1();
    call CLEAR(tid, false);
    par Yield1();
}

procedure {:yields} {:layer 0,2} TransferToGlobal({:linear "tid"} tid: X, {:linear_in "mem"} l: lmap);
ensures {:atomic} |{ A: g := l; return true; }|;

procedure {:yields} {:layer 0,2} TransferFromGlobal({:linear "tid"} tid: X) returns ({:linear "mem"} l: lmap);
ensures {:atomic} |{ A: l := g; return true; }|;

procedure {:yields} {:layer 0,3} Load({:linear "mem"} l: lmap, a: int) returns (v: int);
ensures {:both} |{ A: v := map(l)[a]; return true; }|;

procedure {:yields} {:layer 0,3} Store({:linear_in "mem"} l_in: lmap, a: int, v: int) returns ({:linear "mem"} l_out: lmap);
ensures {:both} |{ A: assume l_out == cons(dom(l_in), map(l_in)[a := v]); return true; }|;

procedure {:yields} {:layer 0,1} CAS(tid: X, prev: bool, next: bool) returns (status: bool);
ensures {:atomic} |{ 
A: goto B, C; 
B: assume b == prev; b := next; status := true; lock := tid; return true; 
C: status := false; return true; 
}|;

procedure {:yields} {:layer 0,1} CLEAR(tid: X, next: bool);
ensures {:atomic} |{ 
A: b := next; lock := nil; return true; 
}|;