-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathssa.cpp
450 lines (375 loc) · 6.86 KB
/
ssa.cpp
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// semmle-extractor-options: -std=c++17
struct Point {
int x;
int y;
};
struct Rect {
Point topLeft;
Point bottomRight;
};
int ChiPhiNode(Point* p, bool which1, bool which2) {
if (which1) {
p->x++;
}
else {
p->y++;
}
if (which2) {
p->x++;
}
else {
p->y++;
}
return p->x + p->y;
}
int UnreachableViaGoto() {
goto skip;
return 1;
skip:
return 0;
}
int UnreachableIf(bool b) {
int x = 5;
int y = 10;
if (b) {
if (x == y) {
return 1;
}
else {
return 0;
}
}
else {
if (x < y) {
return 0;
}
else {
return 1;
}
}
}
int DoWhileFalse() {
int i = 0;
do {
i++;
} while (false);
return i;
}
void chiNodeAtEndOfLoop(int n, char* p) {
while (n-- > 0)
* p++ = 0;
}
void Escape(void* p);
void ScalarPhi(bool b) {
int x = 0;
int y = 1;
int z = 2;
if (b) {
x = 3;
y = 4;
}
else {
x = 5;
}
int x_merge = x;
int y_merge = y;
int z_merge = z;
}
void MustExactlyOverlap(Point a) {
Point b = a;
}
void MustExactlyOverlapEscaped(Point a) {
Point b = a;
Escape(&a);
}
void MustTotallyOverlap(Point a) {
int x = a.x;
int y = a.y;
}
void MustTotallyOverlapEscaped(Point a) {
int x = a.x;
int y = a.y;
Escape(&a);
}
void MayPartiallyOverlap(int x, int y) {
Point a = { x, y };
Point b = a;
}
void MayPartiallyOverlapEscaped(int x, int y) {
Point a = { x, y };
Point b = a;
Escape(&a);
}
void MergeMustExactlyOverlap(bool c, int x1, int x2) {
Point a = {};
if (c) {
a.x = x1;
}
else {
a.x = x2;
}
int x = a.x; // Both reaching defs must exactly overlap.
Point b = a;
}
void MergeMustExactlyWithMustTotallyOverlap(bool c, Point p, int x1) {
Point a = {};
if (c) {
a.x = x1;
}
else {
a = p;
}
int x = a.x; // Only one (non-Chi) reaching def must exactly overlap, but we should still get a Phi for it.
}
void MergeMustExactlyWithMayPartiallyOverlap(bool c, Point p, int x1) {
Point a = {};
if (c) {
a.x = x1;
}
else {
a = p;
}
Point b = a; // Only one reaching def must exactly overlap, but we should still get a Phi for it.
}
void MergeMustTotallyOverlapWithMayPartiallyOverlap(bool c, Rect r, int x1) {
Rect a = {};
if (c) {
a.topLeft.x = x1;
}
else {
a = r;
}
Point b = a.topLeft; // Neither reaching def must exactly overlap, so we'll just get a Phi of the virtual variable.
}
struct Wrapper {
int f;
};
void WrapperStruct(Wrapper w) {
Wrapper x = w; // MustExactlyOverlap
int a = w.f; // MustTotallyOverlap, because the types don't match
w.f = 5;
a = w.f; // MustExactlyOverlap
x = w; // MustTotallyOverlap
}
int AsmStmt(int *p) {
__asm__("");
return *p;
}
static void AsmStmtWithOutputs(unsigned int& a, unsigned int& b, unsigned int& c, unsigned int& d)
{
__asm__ __volatile__
(
"cpuid\n\t"
: "+a" (a), "+b" (b)
: "c" (c), "d" (d)
);
}
int strcmp(const char *, const char *);
int strlen(const char *);
int abs(int);
int PureFunctions(char *str1, char *str2, int x) {
int ret = strcmp(str1, str2);
ret += strlen(str1);
ret += abs(x);
return ret;
}
void *memcpy(void *dst, void *src, int size);
int ModeledCallTarget(int x) {
int y;
memcpy(&y, &x, sizeof(int));
return y;
}
void InitArray() {
char a_pad[32] = "";
char a_nopad[4] = "foo";
char a_infer[] = "blah";
char b[2];
char c[2] = {};
char d[2] = { 0 };
char e[2] = { 0, 1 };
char f[3] = { 0 };
}
extern void ExternalFunc();
char StringLiteralAliasing() {
ExternalFunc();
const char* s = "Literal";
return s[2]; // Should be defined by `AliasedDefinition`, not `Chi` or `CallSideEffect`.
}
class Constructible {
public:
Constructible(int x) {};
void g() {}
};
void ExplicitConstructorCalls() {
Constructible c(1);
c.g();
c.g();
Constructible c2 = Constructible(2);
c2.g();
}
char *VoidStarIndirectParameters(char *src, int size) {
char *dst = new char[size];
*src = 'a';
memcpy(dst, src, size);
return dst;
}
char StringLiteralAliasing2(bool b) {
if (b) {
ExternalFunc();
}
else {
ExternalFunc();
}
const char* s = "Literal";
return s[2];
}
void *malloc(int size);
void *MallocAliasing(void *s, int size) {
void *buf = malloc(size);
memcpy(buf, s, size);
return buf;
}
Point *pp;
void EscapedButNotConflated(bool c, Point p, int x1) {
Point a = {};
pp = &a; // `a` escapes here and therefore belongs to the aliased vvar
if (c) {
a.x = x1;
}
int x = a.x; // The phi node here is not conflated
}
struct A {
int i;
A(int x) {}
A(A*) {}
A() {}
};
Point *NewAliasing(int x) {
Point* p = new Point;
Point* q = new Point;
int j = (new A(new A(x)))->i;
A* a = new A;
return p;
}
void unknownFunction(int argc, char **argv);
int main(int argc, char **argv) {
unknownFunction(argc, argv);
unknownFunction(argc, argv);
return **argv; // Chi chain goes through side effects from unknownFunction
}
class ThisAliasTest {
int x, y;
void setX(int arg) {
this->x = arg;
}
};
void sink(char **);
void sink(char *);
// This test case comes from DefaultTaintTracking.
void DoubleIndirectionEscapes(char *s)
{
char buffer[1024];
char *ptr1, **ptr2;
char *ptr3, **ptr4;
ptr1 = buffer;
ptr2 = &ptr1;
memcpy(*ptr2, s, 1024);
sink(buffer); // $ MISSING: ast,ir
sink(ptr1); // $ ast MISSING: ir
sink(ptr2); // $ SPURIOUS: ast
sink(*ptr2); // $ ast MISSING: ir
}
int UnreachablePhiOperand(int x, int y) {
bool b = true;
int ret;
if(b) {
ret = x;
} else {
ret = y;
}
return ret;
}
int UnreachablePhiOperand2(int x, int y, int z, bool b1) {
bool b2 = true;
int ret;
if(b1) {
ret = x;
} else {
if(b2) {
ret = y;
} else {
ret = z;
}
}
return ret;
}
int DegeneratePhi(int x, int y, bool b1) {
bool b2 = true;
int ret1;
int ret2 = x;
if(b1) {
ret1 = x;
} else {
if(b2) {
ret1 = x;
} else {
ret2 = y;
}
}
return ret1 + ret2;
}
int FusedBlockPhiOperand(int x, int y, int z, bool b1) {
bool b2 = true;
int ret;
if(b1) {
ret = x;
} else {
if(b2) {
ret = y;
} else {
ret = z;
}
; // creates a NoOp instruction with its own basic block
}
return ret;
}
void vla(int n1, int n2, int n3, bool b1) {
int b[n1];
int c[n1][n2];
*b = 0;
b[0] = 1;
**(c + 1) = 0;
if(b1) {
int b[n1];
} else {
int b[n2];
}
}
void nested_array_designators() {
int x[1][2] = {[0][0] = 1234, [0][1] = 5678};
}
[[noreturn]] void noreturnFunc();
int noreturnTest(int x) {
if (x < 10) {
return x;
} else {
noreturnFunc();
}
}
int noreturnTest2(int x) {
if (x < 10) {
noreturnFunc();
}
return x;
}
void Conditional(bool a, int x, int y) {
int z = a ? x : y;
}
static void NonEscapingParams(void *a, void *b)
{
}
static void EscapingParams(void *a, void *b)
{
Escape(a);
Escape(b);
}