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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Boogie;
using System.Diagnostics.Contracts;
namespace GPUVerify {
class AdversarialAbstraction {
private GPUVerifier verifier;
internal AdversarialAbstraction(GPUVerifier verifier) {
this.verifier = verifier;
}
internal void Abstract() {
List<Declaration> NewTopLevelDeclarations = new List<Declaration>();
foreach (Declaration d in verifier.Program.TopLevelDeclarations) {
if (d is Variable && verifier.KernelArrayInfo.Contains(d as Variable) &&
verifier.ArrayModelledAdversarially(d as Variable)) {
continue;
}
if (d is Implementation) {
Abstract(d as Implementation);
}
if (d is Procedure) {
Abstract(d as Procedure);
}
NewTopLevelDeclarations.Add(d);
}
verifier.Program.TopLevelDeclarations = NewTopLevelDeclarations;
AbstractRequiresClauses(verifier.KernelProcedure);
}
private void AbstractRequiresClauses(Procedure proc) {
RequiresSeq newRequires = new RequiresSeq();
foreach (Requires r in proc.Requires) {
var visitor = new AccessesAdversarialArrayVisitor(verifier);
visitor.VisitRequires(r);
if (!visitor.found) {
newRequires.Add(r);
}
}
proc.Requires = newRequires;
}
private void Abstract(Procedure proc) {
AbstractModifiesSet(proc);
}
private void AbstractModifiesSet(Procedure proc) {
IdentifierExprSeq NewModifies = new IdentifierExprSeq();
foreach (IdentifierExpr e in proc.Modifies) {
var visitor = new AccessesAdversarialArrayVisitor(verifier);
visitor.VisitIdentifierExpr(e);
if(!visitor.found) {
NewModifies.Add(e);
}
}
proc.Modifies = NewModifies;
}
private void Abstract(Implementation impl) {
VariableSeq NewLocVars = new VariableSeq();
foreach (Variable v in impl.LocVars) {
Debug.Assert(!verifier.KernelArrayInfo.getGroupSharedArrays().Contains(v));
NewLocVars.Add(v);
}
impl.LocVars = NewLocVars;
if (CommandLineOptions.Unstructured)
impl.Blocks = impl.Blocks.Select(Abstract).ToList();
else
impl.StructuredStmts = Abstract(impl.StructuredStmts);
}
private StmtList Abstract(StmtList stmtList) {
Contract.Requires(stmtList != null);
StmtList result = new StmtList(new List<BigBlock>(), stmtList.EndCurly);
foreach (BigBlock bodyBlock in stmtList.BigBlocks) {
result.BigBlocks.Add(Abstract(bodyBlock));
}
return result;
}
private CmdSeq Abstract(CmdSeq cs) {
var result = new CmdSeq();
foreach (Cmd c in cs) {
if (c is AssignCmd) {
AssignCmd assign = c as AssignCmd;
var lhss = new List<AssignLhs>();
var rhss = new List<Expr>();
for (int i = 0; i != assign.Lhss.Count; i++) {
AssignLhs lhs = assign.Lhss[i];
Expr rhs = assign.Rhss[i];
ReadCollector rc = new ReadCollector(verifier.KernelArrayInfo);
rc.Visit(rhs);
bool foundAdversarial = false;
foreach (AccessRecord ar in rc.accesses) {
if (verifier.ArrayModelledAdversarially(ar.v)) {
foundAdversarial = true;
break;
}
}
if (foundAdversarial) {
Debug.Assert(lhs is SimpleAssignLhs);
result.Add(new HavocCmd(c.tok, new IdentifierExprSeq(new IdentifierExpr[] { (lhs as SimpleAssignLhs).AssignedVariable })));
continue;
}
WriteCollector wc = new WriteCollector(verifier.KernelArrayInfo);
wc.Visit(lhs);
if (wc.GetAccess() != null && verifier.ArrayModelledAdversarially(wc.GetAccess().v)) {
continue; // Just remove the write
}
lhss.Add(lhs);
rhss.Add(rhs);
}
if (lhss.Count != 0) {
result.Add(new AssignCmd(assign.tok, lhss, rhss));
}
continue;
}
result.Add(c);
}
return result;
}
private Block Abstract(Block b) {
b.Cmds = Abstract(b.Cmds);
return b;
}
private BigBlock Abstract(BigBlock bb) {
BigBlock result = new BigBlock(bb.tok, bb.LabelName, new CmdSeq(), null, bb.tc);
result.simpleCmds = Abstract(bb.simpleCmds);
if (bb.ec is WhileCmd) {
WhileCmd WhileCommand = bb.ec as WhileCmd;
result.ec =
new WhileCmd(WhileCommand.tok, WhileCommand.Guard, WhileCommand.Invariants, Abstract(WhileCommand.Body));
}
else if (bb.ec is IfCmd) {
IfCmd IfCommand = bb.ec as IfCmd;
Debug.Assert(IfCommand.elseIf == null);
result.ec = new IfCmd(IfCommand.tok, IfCommand.Guard, Abstract(IfCommand.thn), IfCommand.elseIf, IfCommand.elseBlock != null ? Abstract(IfCommand.elseBlock) : null);
}
else {
Debug.Assert(bb.ec == null || bb.ec is BreakCmd);
}
return result;
}
class AccessesAdversarialArrayVisitor : StandardVisitor {
internal bool found;
private GPUVerifier verifier;
internal AccessesAdversarialArrayVisitor(GPUVerifier verifier) {
this.found = false;
this.verifier = verifier;
}
public override Variable VisitVariable(Variable v) {
if (verifier.KernelArrayInfo.Contains(v)) {
if (verifier.ArrayModelledAdversarially(v)) {
found = true;
}
}
return base.VisitVariable(v);
}
}
}
}
|