Static Value-Flow Analysis
Loading...
Searching...
No Matches
ae.cpp
Go to the documentation of this file.
1//===- ae.cpp -- Abstract Execution -------------------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-2017> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===-----------------------------------------------------------------------===//
22
23/*
24 // Abstract Execution
25 //
26 // Author: Jiawei Wang, Xiao Cheng, Jiawei Yang, Jiawei Ren, Yulei Sui
27 */
28#include "SVF-LLVM/PAGBuilder.h"
29#include "WPA/WPAPass.h"
30#include "Util/CommandLine.h"
31#include "Util/Options.h"
32#include "WPA/Andersen.h"
33
34#include "AE/Core/RelExeState.h"
37#include <chrono>
38#include <limits>
39
40using namespace SVF;
41using namespace SVFUtil;
42
43
45 "symabs",
46 "symbolic abstraction test",
47 false
48);
49
51 "aetest",
52 "abstract execution basic function test",
53 false
54);
55
57{
58public:
60
62
63 static z3::context& getContext()
64 {
65 return Z3Expr::getContext();
66 }
67
69 {
70 outs() << "hello print\n";
71 }
72
75 {
76 auto start_time = std::chrono::high_resolution_clock::now();
77 AbstractState resRSY = rs.RSY(inv, phi);
78 auto end_time = std::chrono::high_resolution_clock::now();
79 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
80 end_time - start_time);
81 outs() << "running time of RSY : " << duration.count()
82 << " microseconds\n";
83 return resRSY;
84 }
87 {
88 auto start_time = std::chrono::high_resolution_clock::now();
89 AbstractState resBilateral = rs.bilateral(inv, phi);
90 auto end_time = std::chrono::high_resolution_clock::now();
91 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
92 end_time - start_time);
93 outs() << "running time of Bilateral: " << duration.count()
94 << " microseconds\n";
95 return resBilateral;
96 }
99 {
100 auto start_time = std::chrono::high_resolution_clock::now();
101 AbstractState resBS = rs.BS(inv, phi);
102 auto end_time = std::chrono::high_resolution_clock::now();
103 auto duration = std::chrono::duration_cast<std::chrono::microseconds>(
104 end_time - start_time);
105 outs() << "running time of BS : " << duration.count()
106 << " microseconds\n";
107 return resBS;
108 }
109
111 {
112 outs() << sucMsg("\t SUCCESS :") << "test1_1 start\n";
113 AbstractState itv;
114 RelExeState relation;
115 // var0 := [0, 1];
116 itv[0] = IntervalValue(0, 1);
117 relation[0] = getContext().int_const("0");
118 // var1 := var0 + 1;
119 relation[1] =
120 getContext().int_const("1") == getContext().int_const("0") + 1;
121 itv[1] = itv[0].getInterval() + IntervalValue(1);
122 // Test extract sub vars
123 Set<u32_t> res;
124 relation.extractSubVars(relation[1], res);
125 assert(res == Set<u32_t>({0, 1}) && "inconsistency occurs");
126 AbstractState inv = itv.sliceState(res);
128 const Z3Expr& relExpr = relation[1];
129 const Z3Expr& initExpr = rs.gamma_hat(inv);
130 const Z3Expr& phi = (relExpr && initExpr).simplify();
131 AbstractState resRSY = rs.RSY(inv, phi);
132 AbstractState resBilateral = rs.bilateral(inv, phi);
133 AbstractState resBS = rs.BS(inv, phi);
134 // 0:[0,1] 1:[1,2]
135 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
136 for (auto r : resRSY.getVarToVal())
137 {
138 outs() << r.first << " " << r.second.getInterval() << "\n";
139 }
140 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 1)}, {1, IntervalValue(1, 2)}};
141 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
142 }
143
145 {
146 outs() << "test1_2 start\n";
147 AbstractState itv;
148 RelExeState relation;
149 // var0 := [0, 1];
150 relation[0] = getContext().int_const("0");
151 itv[0] = IntervalValue(0, 1);
152 // var1 := var0 + 1;
153 relation[1] =
154 getContext().int_const("1") == getContext().int_const("0") * 2;
155 itv[1] = itv[0].getInterval() * IntervalValue(2);
156
157 // Test extract sub vars
158 Set<u32_t> res;
159 relation.extractSubVars(relation[1], res);
160 assert(res == Set<u32_t>({0, 1}) && "inconsistency occurs");
161 AbstractState inv = itv.sliceState(res);
163 const Z3Expr& relExpr = relation[1];
164 const Z3Expr& initExpr = rs.gamma_hat(inv);
165 const Z3Expr& phi = (relExpr && initExpr).simplify();
166 AbstractState resRSY = rs.RSY(inv, phi);
167 AbstractState resBilateral = rs.bilateral(inv, phi);
168 AbstractState resBS = rs.BS(inv, phi);
169 // 0:[0,1] 1:[0,2]
170 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
171 for (auto r : resRSY.getVarToVal())
172 {
173 outs() << r.first << " " << r.second.getInterval() << "\n";
174 }
175 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 1)}, {1, IntervalValue(0, 2)}};
176 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
177 }
178
180 {
181 outs() << "test2_1 start\n";
182 AbstractState itv;
183 RelExeState relation;
184 // var0 := [0, 10];
185 relation[0] = getContext().int_const("0");
186 itv[0] = IntervalValue(0, 10);
187 // var1 := var0;
188 relation[1] =
189 getContext().int_const("1") == getContext().int_const("0");
190 itv[1] = itv[0];
191 // var2 := var1 - var0;
192 relation[2] = getContext().int_const("2") ==
193 getContext().int_const("1") - getContext().int_const("0");
194 itv[2] = itv[1].getInterval() - itv[0].getInterval();
195 // Test extract sub vars
196 Set<u32_t> res;
197 relation.extractSubVars(relation[2], res);
198 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
199 AbstractState inv = itv.sliceState(res);
201 const Z3Expr& relExpr = relation[2] && relation[1];
202 const Z3Expr& initExpr = rs.gamma_hat(inv);
203 const Z3Expr& phi = (relExpr && initExpr).simplify();
204 AbstractState resRSY = rs.RSY(inv, phi);
205 AbstractState resBilateral = rs.bilateral(inv, phi);
206 AbstractState resBS = rs.BS(inv, phi);
207 // 0:[0,10] 1:[0,10] 2:[0,0]
208 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
209 for (auto r : resRSY.getVarToVal())
210 {
211 outs() << r.first << " " << r.second.getInterval() << "\n";
212 }
213 // ground truth
214 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 10)},
215 {1, IntervalValue(0, 10)},
216 {2, IntervalValue(0, 0)}
217 };
218 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
219 }
220
222 {
223 outs() << "test2_2 start\n";
224 AbstractState itv;
225 RelExeState relation;
226 // var0 := [0, 100];
227 relation[0] = getContext().int_const("0");
228 itv[0] = IntervalValue(0, 100);
229 // var1 := var0;
230 relation[1] =
231 getContext().int_const("1") == getContext().int_const("0");
232 itv[1] = itv[0];
233 // var2 := var1 - var0;
234 relation[2] = getContext().int_const("2") ==
235 getContext().int_const("1") - getContext().int_const("0");
236 itv[2] = itv[1].getInterval() - itv[0].getInterval();
237
238 // Test extract sub vars
239 Set<u32_t> res;
240 relation.extractSubVars(relation[2], res);
241 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
242 AbstractState inv = itv.sliceState(res);
244 const Z3Expr& relExpr = relation[2] && relation[1];
245 const Z3Expr& initExpr = rs.gamma_hat(inv);
246 const Z3Expr& phi = (relExpr && initExpr).simplify();
247 AbstractState resRSY = rs.RSY(inv, phi);
248 AbstractState resBilateral = rs.bilateral(inv, phi);
249 AbstractState resBS = rs.BS(inv, phi);
250 // 0:[0,100] 1:[0,100] 2:[0,0]
251 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
252 for (auto r : resRSY.getVarToVal())
253 {
254 outs() << r.first << " " << r.second.getInterval() << "\n";
255 }
256 // ground truth
257 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 100)},
258 {1, IntervalValue(0, 100)},
259 {2, IntervalValue(0, 0)}
260 };
261 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
262 }
263
265 {
266 outs() << "test2_3 start\n";
267 AbstractState itv;
268 RelExeState relation;
269 // var0 := [0, 1000];
270 relation[0] = getContext().int_const("0");
271 itv[0] = IntervalValue(0, 1000);
272 // var1 := var0;
273 relation[1] =
274 getContext().int_const("1") == getContext().int_const("0");
275 itv[1] = itv[0];
276 // var2 := var1 - var0;
277 relation[2] = getContext().int_const("2") ==
278 getContext().int_const("1") - getContext().int_const("0");
279 itv[2] = itv[1].getInterval() - itv[0].getInterval();
280
281 // Test extract sub vars
282 Set<u32_t> res;
283 relation.extractSubVars(relation[2], res);
284 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
285 AbstractState inv = itv.sliceState(res);
287 const Z3Expr& relExpr = relation[2] && relation[1];
288 const Z3Expr& initExpr = rs.gamma_hat(inv);
289 const Z3Expr& phi = (relExpr && initExpr).simplify();
290 AbstractState resRSY = rs.RSY(inv, phi);
291 AbstractState resBilateral = rs.bilateral(inv, phi);
292 AbstractState resBS = rs.BS(inv, phi);
293 // 0:[0,1000] 1:[0,1000] 2:[0,0]
294 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
295 for (auto r : resRSY.getVarToVal())
296 {
297 outs() << r.first << " " << r.second.getInterval() << "\n";
298 }
299 // ground truth
300 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 1000)},
301 {1, IntervalValue(0, 1000)},
302 {2, IntervalValue(0, 0)}
303 };
304 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
305 }
306
308 {
309 outs() << "test2_4 start\n";
310 AbstractState itv;
311 RelExeState relation;
312 // var0 := [0, 10000];
313 relation[0] = getContext().int_const("0");
314 itv[0] = IntervalValue(0, 10000);
315 // var1 := var0;
316 relation[1] =
317 getContext().int_const("1") == getContext().int_const("0");
318 itv[1] = itv[0];
319 // var2 := var1 - var0;
320 relation[2] = getContext().int_const("2") ==
321 getContext().int_const("1") - getContext().int_const("0");
322 itv[2] = itv[1].getInterval() - itv[0].getInterval();
323
324 // Test extract sub vars
325 Set<u32_t> res;
326 relation.extractSubVars(relation[2], res);
327 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
328 AbstractState inv = itv.sliceState(res);
330 const Z3Expr& relExpr = relation[2] && relation[1];
331 const Z3Expr& initExpr = rs.gamma_hat(inv);
332 const Z3Expr& phi = (relExpr && initExpr).simplify();
333 AbstractState resRSY = RSY_time(inv, phi, rs);
334 AbstractState resBilateral = Bilateral_time(inv, phi, rs);
335 AbstractState resBS = BS_time(inv, phi, rs);
336 // 0:[0,10000] 1:[0,10000] 2:[0,0]
337 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
338 for (auto r : resRSY.getVarToVal())
339 {
340 outs() << r.first << " " << r.second.getInterval() << "\n";
341 }
342 // ground truth
343 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 10000)},
344 {1, IntervalValue(0, 10000)},
345 {2, IntervalValue(0, 0)}
346 };
347 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
348 }
349
351 {
352 outs() << "test2_5 start\n";
353 AbstractState itv;
354 RelExeState relation;
355 // var0 := [0, 100000];
356 relation[0] = getContext().int_const("0");
357 itv[0] = IntervalValue(0, 100000);
358 // var1 := var0;
359 relation[1] =
360 getContext().int_const("1") == getContext().int_const("0");
361 itv[1] = itv[0];
362 // var2 := var1 - var0;
363 relation[2] = getContext().int_const("2") ==
364 getContext().int_const("1") - getContext().int_const("0");
365 itv[2] = itv[1].getInterval() - itv[0].getInterval();
366
367 // Test extract sub vars
368 Set<u32_t> res;
369 relation.extractSubVars(relation[2], res);
370 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
371 AbstractState inv = itv.sliceState(res);
373 const Z3Expr& relExpr = relation[2] && relation[1];
374 const Z3Expr& initExpr = rs.gamma_hat(inv);
375 const Z3Expr& phi = (relExpr && initExpr).simplify();
376 AbstractState resRSY = RSY_time(inv, phi, rs);
377 AbstractState resBilateral = Bilateral_time(inv, phi, rs);
378 AbstractState resBS = BS_time(inv, phi, rs);
379 // 0:[0,100000] 1:[0,100000] 2:[0,0]
380 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
381 for (auto r : resRSY.getVarToVal())
382 {
383 outs() << r.first << " " << r.second.getInterval() << "\n";
384 }
385 // ground truth
386 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 100000)},
387 {1, IntervalValue(0, 100000)},
388 {2, IntervalValue(0, 0)}
389 };
390 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
391 }
392
394 {
395 outs() << "test3_1 start\n";
396 AbstractState itv;
397 RelExeState relation;
398 // var0 := [1, 10];
399 relation[0] = getContext().int_const("0");
400 itv[0] = IntervalValue(1, 10);
401 // var1 := var0;
402 relation[1] =
403 getContext().int_const("1") == getContext().int_const("0");
404 itv[1] = itv[0];
405 // var2 := var1 / var0;
406 relation[2] = getContext().int_const("2") ==
407 getContext().int_const("1") / getContext().int_const("0");
408 itv[2] = itv[1].getInterval() / itv[0].getInterval();
409 // Test extract sub vars
410 Set<u32_t> res;
411 relation.extractSubVars(relation[2], res);
412 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
413 AbstractState inv = itv.sliceState(res);
415 const Z3Expr& relExpr = relation[2] && relation[1];
416 const Z3Expr& initExpr = rs.gamma_hat(inv);
417 const Z3Expr& phi = (relExpr && initExpr).simplify();
418 AbstractState resRSY = rs.RSY(inv, phi);
419 AbstractState resBilateral = rs.bilateral(inv, phi);
420 AbstractState resBS = rs.BS(inv, phi);
421 // 0:[1,10] 1:[1,10] 2:[1,1]
422 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
423 for (auto r : resRSY.getVarToVal())
424 {
425 outs() << r.first << " " << r.second.getInterval() << "\n";
426 }
427 // ground truth
428 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(1, 10)},
429 {1, IntervalValue(1, 10)},
430 {2, IntervalValue(1, 1)}
431 };
432 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
433 }
434
436 {
437 outs() << "test3_2 start\n";
438 AbstractState itv;
439 RelExeState relation;
440 // var0 := [1, 1000];
441 relation[0] = getContext().int_const("0");
442 itv[0] = IntervalValue(1, 1000);
443 // var1 := var0;
444 relation[1] =
445 getContext().int_const("1") == getContext().int_const("0");
446 itv[1] = itv[0];
447 // var2 := var1 / var0;
448 relation[2] = getContext().int_const("2") ==
449 getContext().int_const("1") / getContext().int_const("0");
450 itv[2] = itv[1].getInterval() / itv[0].getInterval();
451 // Test extract sub vars
452 Set<u32_t> res;
453 relation.extractSubVars(relation[2], res);
454 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
455 AbstractState inv = itv.sliceState(res);
457 const Z3Expr& relExpr = relation[2] && relation[1];
458 const Z3Expr& initExpr = rs.gamma_hat(inv);
459 const Z3Expr& phi = (relExpr && initExpr).simplify();
460 AbstractState resRSY = rs.RSY(inv, phi);
461 AbstractState resBilateral = rs.bilateral(inv, phi);
462 AbstractState resBS = rs.BS(inv, phi);
463 // 0:[1,1000] 1:[1,1000] 2:[1,1]
464 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
465 for (auto r : resRSY.getVarToVal())
466 {
467 outs() << r.first << " " << r.second.getInterval() << "\n";
468 }
469 // ground truth
470 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(1, 1000)},
471 {1, IntervalValue(1, 1000)},
472 {2, IntervalValue(1, 1)}
473 };
474 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
475 }
476
478 {
479 outs() << "test3_3 start\n";
480 AbstractState itv;
481 RelExeState relation;
482 // var0 := [1, 10000];
483 relation[0] = getContext().int_const("0");
484 itv[0] = IntervalValue(1, 10000);
485 // var1 := var0;
486 relation[1] =
487 getContext().int_const("1") == getContext().int_const("0");
488 itv[1] = itv[0];
489 // var2 := var1 / var0;
490 relation[2] = getContext().int_const("2") ==
491 getContext().int_const("1") / getContext().int_const("0");
492 itv[2] = itv[1].getInterval() / itv[0].getInterval();
493 // Test extract sub vars
494 Set<u32_t> res;
495 relation.extractSubVars(relation[2], res);
496 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
497 AbstractState inv = itv.sliceState(res);
499 const Z3Expr& relExpr = relation[2] && relation[1];
500 const Z3Expr& initExpr = rs.gamma_hat(inv);
501 const Z3Expr& phi = (relExpr && initExpr).simplify();
502 AbstractState resRSY = RSY_time(inv, phi, rs);
503 AbstractState resBilateral = Bilateral_time(inv, phi, rs);
504 AbstractState resBS = BS_time(inv, phi, rs);
505 // 0:[1,10000] 1:[1,10000] 2:[1,1]
506 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
507 for (auto r : resRSY.getVarToVal())
508 {
509 outs() << r.first << " " << r.second.getInterval() << "\n";
510 }
511 // ground truth
512 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(1, 10000)},
513 {1, IntervalValue(1, 10000)},
514 {2, IntervalValue(1, 1)}
515 };
516 }
517
519 {
520 outs() << "test3_4 start\n";
521 AbstractState itv;
522 RelExeState relation;
523 // var0 := [1, 100000];
524 relation[0] = getContext().int_const("0");
525 itv[0] = IntervalValue(1, 100000);
526 // var1 := var0;
527 relation[1] =
528 getContext().int_const("1") == getContext().int_const("0");
529 itv[1] = itv[0];
530 // var2 := var1 / var0;
531 relation[2] = getContext().int_const("2") ==
532 getContext().int_const("1") / getContext().int_const("0");
533 itv[2] = itv[1].getInterval() / itv[0].getInterval();
534 // Test extract sub vars
535 Set<u32_t> res;
536 relation.extractSubVars(relation[2], res);
537 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
538 AbstractState inv = itv.sliceState(res);
540 const Z3Expr& relExpr = relation[2] && relation[1];
541 const Z3Expr& initExpr = rs.gamma_hat(inv);
542 const Z3Expr& phi = (relExpr && initExpr).simplify();
543 AbstractState resRSY = RSY_time(inv, phi, rs);
544 AbstractState resBilateral = Bilateral_time(inv, phi, rs);
545 AbstractState resBS = BS_time(inv, phi, rs);
546 // 0:[1,100000] 1:[1,100000] 2:[1,1]
547 assert(resRSY == resBS && resBS == resBilateral && "inconsistency occurs");
548 for (auto r : resRSY.getVarToVal())
549 {
550 outs() << r.first << " " << r.second.getInterval() << "\n";
551 }
552 // ground truth
553 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(1, 100000)},
554 {1, IntervalValue(1, 100000)},
555 {2, IntervalValue(1, 1)}
556 };
557 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
558 }
559
561 {
562 outs() << "test4_1 start\n";
563 AbstractState itv;
564 RelExeState relation;
565 // var0 := [0, 10];
566 relation[0] = getContext().int_const("0");
567 itv[0] = IntervalValue(0, 10);
568 // var1 := var0;
569 relation[1] =
570 getContext().int_const("1") == getContext().int_const("0");
571 itv[1] = itv[0];
572 // var2 := var1 / var0;
573 relation[2] = getContext().int_const("2") ==
574 getContext().int_const("1") / getContext().int_const("0");
575 itv[2] = itv[1].getInterval() / itv[0].getInterval();
576 // Test extract sub vars
577 Set<u32_t> res;
578 relation.extractSubVars(relation[2], res);
579 assert(res == Set<u32_t>({0, 1, 2}) && "inconsistency occurs");
580 AbstractState inv = itv.sliceState(res);
582 const Z3Expr& relExpr = relation[2] && relation[1];
583 const Z3Expr& initExpr = rs.gamma_hat(inv);
584 const Z3Expr& phi = (relExpr && initExpr).simplify();
585 // IntervalExeState resRSY = rs.RSY(inv, phi);
586 outs() << "rsy done\n";
587 // IntervalExeState resBilateral = rs.bilateral(inv, phi);
588 outs() << "bilateral done\n";
589 AbstractState resBS = rs.BS(inv, phi);
590 outs() << "bs done\n";
591 // 0:[0,10] 1:[0,10] 2:[-00,+00]
592 // assert(resRSY == resBS && resBS == resBilateral);
593 for (auto r : resBS.getVarToVal())
594 {
595 outs() << r.first << " " << r.second.getInterval() << "\n";
596 }
597 // ground truth
598 AbstractState::VarToAbsValMap intendedRes = {{0, IntervalValue(0, 10)},
599 {1, IntervalValue(0, 10)},
600 {2, IntervalValue(0, 10)}
601 };
602 assert(resBS.eqVarToValMap(resBS.getVarToVal(), intendedRes) && "inconsistency occurs");
603 }
604
606 {
608 saTest.testRelExeState1_1();
609 saTest.testRelExeState1_2();
610
611 saTest.testRelExeState2_1();
612 saTest.testRelExeState2_2();
613 saTest.testRelExeState2_3();
614 // saTest.testRelExeState2_4(); /// 10000
615 // saTest.testRelExeState2_5(); /// 100000
616
617 saTest.testRelExeState3_1();
618 saTest.testRelExeState3_2();
619 // saTest.testRelExeState3_3(); /// 10000
620 // saTest.testRelExeState3_4(); /// 100000
621
622 outs() << "start top\n";
623 saTest.testRelExeState4_1();
624 }
625};
626
628{
629public:
630 AETest() = default;
631
632 ~AETest() = default;
633
635 {
636 const s64_t intMin = std::numeric_limits<s64_t>::min();
637 const s64_t intMax = std::numeric_limits<s64_t>::max();
638
639 const BoundedInt minValue(intMin);
640 const BoundedInt maxValue(intMax);
641 const BoundedInt zero(0);
642 const BoundedInt one(1);
643 const BoundedInt minusOne(-1);
644
645 // Negation.
646 assert((-minValue).is_plus_infinity());
647 assert(abs(minValue).is_plus_infinity());
648
649 // Addition: preserve exact INT64_MIN and abstract true overflow.
650 assert((minusOne + BoundedInt(-intMax)).equal(minValue));
651 assert((maxValue + one).is_plus_infinity());
652 assert((minValue + minusOne).is_minus_infinity());
653
654 // Subtraction must not negate the right-hand operand first.
655 assert((minValue - minValue).equal(zero));
656 assert((maxValue - maxValue).equal(zero));
657 assert((maxValue - minValue).is_plus_infinity());
658 assert((minValue - maxValue).is_minus_infinity());
659
660 // Multiplication: exercise both operand orders at INT64_MIN.
661 assert((one * minValue).equal(minValue));
662 assert((minValue * one).equal(minValue));
663 assert((minusOne * minValue).is_plus_infinity());
664 assert((BoundedInt::minus_infinity() * minValue).is_plus_infinity());
665
666 // Division and remainder have one overflowing finite input pair.
667 assert((minValue / one).equal(minValue));
668 assert((minValue / minusOne).is_plus_infinity());
669 assert((minValue % minusOne).equal(zero));
670 }
671
673 {
674 const s64_t intMin = std::numeric_limits<s64_t>::min();
675 const s64_t intMax = std::numeric_limits<s64_t>::max();
676 const IntervalValue fullRange(intMin, intMax);
677
678 // Regression for #1961:
679 // 1 - [INT64_MIN, INT64_MAX] = [INT64_MIN + 2, +inf].
680 const IntervalValue issueResult = IntervalValue(1) - fullRange;
681 const IntervalValue issueExpected(BoundedInt(intMin + 2),
683 assert(issueResult.equals(issueExpected));
684
685 // Exercise the opposite subtraction direction and both overflows.
686 const IntervalValue reverseResult = fullRange - IntervalValue(1);
687 const IntervalValue reverseExpected(BoundedInt::minus_infinity(),
688 BoundedInt(intMax - 1));
689 assert(reverseResult.equals(reverseExpected));
690 assert((fullRange - fullRange).equals(IntervalValue::top()));
691
692 // Exact INT64_MIN results must remain finite.
693 assert((IntervalValue(-1) + IntervalValue(-intMax))
694 .equals(IntervalValue(intMin)));
695 assert((IntervalValue(1) * fullRange).equals(fullRange));
696
697 // Propagate division and remainder boundary behavior through intervals.
698 assert((IntervalValue(intMin) / IntervalValue(-1))
700 assert((IntervalValue(intMin) % IntervalValue(-1))
701 .equals(IntervalValue(0)));
702 }
703
705 {
706 // test division /
709 assert((IntervalValue::top() / IntervalValue(0)).equals(IntervalValue::bottom()));
710 assert((IntervalValue(4) / IntervalValue(2)).equals(IntervalValue(2)));
711 assert((IntervalValue(3) / IntervalValue(2)).equals(IntervalValue(1))); //
712 assert((IntervalValue(-3) / IntervalValue(2)).equals(IntervalValue(-1))); //
713 assert((IntervalValue(1, 3) / IntervalValue(2)).equals(IntervalValue(0, 1))); //
714 assert((IntervalValue(2, 7) / IntervalValue(2)).equals(IntervalValue(1, 3))); //
715 assert((IntervalValue(-3, 3) / IntervalValue(2)).equals(IntervalValue(-1, 1)));
718 assert((IntervalValue(1, 3) / IntervalValue(1, 2)).equals(IntervalValue(0, 3)));//
719 assert((IntervalValue(-3, 3) / IntervalValue(1, 2)).equals(IntervalValue(-3, 3)));
720 assert((IntervalValue(2, 7) / IntervalValue(-2, 3)).equals(IntervalValue(-7, 7))); //
721 assert((IntervalValue(-2, 7) / IntervalValue(-2, 3)).equals(IntervalValue(-7, 7))); //
724
725 assert((IntervalValue(-2, 7) / IntervalValue(IntervalValue::minus_infinity(), 3)).equals(IntervalValue(-7, 7)));
726 assert((IntervalValue(-2, 7) / IntervalValue(-2, IntervalValue::plus_infinity())).equals(IntervalValue(-7, 7)));
727 assert((IntervalValue(-6, -3) / IntervalValue(3, 9)).equals(IntervalValue(-2, 0)));
728 assert((IntervalValue(-6, 6) / IntervalValue(3, 9)).equals(IntervalValue(-2, 2)));
729
730 // test remainder %
733 assert((IntervalValue::top() % IntervalValue(0)).equals(IntervalValue::top()));
734 assert((IntervalValue(4) % IntervalValue(2)).equals(IntervalValue(0)));
735 assert((IntervalValue(3) % IntervalValue(2)).equals(IntervalValue(1)));
736 assert((IntervalValue(-3) % IntervalValue(2)).equals(IntervalValue(-1)));
737 assert((IntervalValue(1, 3) % IntervalValue(2)).equals(IntervalValue(0, 1)));
738 assert((IntervalValue(2, 7) % IntervalValue(2)).equals(IntervalValue(0, 1)));
739 assert((IntervalValue(-3, 3) % IntervalValue(2)).equals(IntervalValue(-1, 1)));
740 assert((IntervalValue(-3, IntervalValue::plus_infinity()) % IntervalValue(2)).equals(IntervalValue(-1, 1)));
742 assert((IntervalValue(1, 3) % IntervalValue(1, 2)).equals(IntervalValue(0, 1)));
743 assert((IntervalValue(-3, 3) % IntervalValue(1, 2)).equals(IntervalValue(-1, 1)));
744 assert((IntervalValue(2, 7) % IntervalValue(-2, 3)).equals(IntervalValue::top())); //
745 assert((IntervalValue(-2, 7) % IntervalValue(-2, 3)).equals(IntervalValue::top())); //
750 assert((IntervalValue(-6, -3) % IntervalValue(3, 9)).equals(IntervalValue(-6, 0)));
751 assert((IntervalValue(-6, 6) % IntervalValue(3, 9)).equals(IntervalValue(-6, 6)));
752
753 // shl <<
759 assert((IntervalValue(0, 0) << IntervalValue(IntervalValue::plus_infinity())).equals(IntervalValue(0, 0)));
760 assert((IntervalValue(0) << IntervalValue(31)).equals(IntervalValue(0)));
761 assert((IntervalValue(0) << IntervalValue(31, 32)).equals(IntervalValue(0)));
762 assert((IntervalValue(0) << IntervalValue(-1)).equals(IntervalValue::bottom()));
764 assert((IntervalValue(1) << IntervalValue(31)).equals(IntervalValue::top()));
766 assert((IntervalValue(0, 0) << IntervalValue(2, 2)).equals(IntervalValue(0, 0)));
767 assert((IntervalValue(2, 2) << IntervalValue(3, 3)).equals(IntervalValue(16, 16)));
768 assert((IntervalValue(-2, -2) << IntervalValue(3, 3)).equals(IntervalValue(-16, -16)));
769
772 assert((IntervalValue::top() << IntervalValue(0)).equals(IntervalValue::top()));
773 assert((IntervalValue(4) << IntervalValue(2)).equals(IntervalValue(16)));
774 assert((IntervalValue(3) << IntervalValue(2)).equals(IntervalValue(12)));
775 assert((IntervalValue(-3) << IntervalValue(2)).equals(IntervalValue(-12)));
776 assert((IntervalValue(4) << IntervalValue(-2)).equals(IntervalValue::bottom()));
777 assert((IntervalValue(1, 3) << IntervalValue(2)).equals(IntervalValue(4, 12)));
778 assert((IntervalValue(2, 7) << IntervalValue(2)).equals(IntervalValue(8, 28)));
779 assert((IntervalValue(-3, 3) << IntervalValue(2)).equals(IntervalValue(-12, 12)));
782 assert((IntervalValue(1, 3) << IntervalValue(1, 2)).equals(IntervalValue(2, 12)));
783 assert((IntervalValue(-3, 3) << IntervalValue(1, 2)).equals(IntervalValue(-12, 12)));
784 assert((IntervalValue(2, 7) << IntervalValue(-2, 3)).equals(IntervalValue(2, 56)));
785 assert((IntervalValue(-2, 7) << IntervalValue(-2, 3)).equals(IntervalValue(-16, 56)));
788 assert((IntervalValue(-2, 7) << IntervalValue(IntervalValue::minus_infinity(), 3)).equals(IntervalValue(-16, 56)));
790 assert((IntervalValue(-6, -3) << IntervalValue(3, 9)).equals(IntervalValue(-3072, -24)));
791 assert((IntervalValue(-6, 6) << IntervalValue(3, 9)).equals(IntervalValue(-3072, 3072)));
793 assert((IntervalValue(0) << IntervalValue::top()).equals(IntervalValue(0)));
794
795
796 // shr >>
804 assert((IntervalValue(0) >> IntervalValue(2)).equals(IntervalValue(0)));
805 assert((IntervalValue(15) >> IntervalValue(2)).equals(IntervalValue(3)));
806 assert((IntervalValue(-15) >> IntervalValue(2)).equals(IntervalValue(-4)));
807
810 assert((IntervalValue::top() >> IntervalValue(0)).equals(IntervalValue::top()));
811 assert((IntervalValue(15) >> IntervalValue(2)).equals(IntervalValue(3)));
812 assert((IntervalValue(1) >> IntervalValue(2)).equals(IntervalValue(0)));
813 assert((IntervalValue(-15) >> IntervalValue(2)).equals(IntervalValue(-4)));
814 assert((IntervalValue(4) >> IntervalValue(-2)).equals(IntervalValue::bottom()));
815 assert((IntervalValue(1, 3) >> IntervalValue(2)).equals(IntervalValue(0)));
816 assert((IntervalValue(2, 7) >> IntervalValue(2)).equals(IntervalValue(0, 1)));
817 assert((IntervalValue(-15, 15) >> IntervalValue(2)).equals(IntervalValue(-4, 3)));
820 assert((IntervalValue(0, 15) >> IntervalValue(1, 2)).equals(IntervalValue(0, 7)));
821 assert((IntervalValue(-17, 15) >> IntervalValue(1, 2)).equals(IntervalValue(-9, 7)));
822 assert((IntervalValue(2, 7) >> IntervalValue(-2, 3)).equals(IntervalValue(0, 7)));
823 assert((IntervalValue(-2, 7) >> IntervalValue(-2, 3)).equals(IntervalValue(-2, 7)));
826 assert((IntervalValue(-2, 7) >> IntervalValue(IntervalValue::minus_infinity(), 3)).equals(IntervalValue(-2, 7)));
827 assert((IntervalValue(-2, 7) >> IntervalValue(-2, IntervalValue::plus_infinity())).equals(IntervalValue(-2, 7)));
828 assert((IntervalValue(-6, -3) >> IntervalValue(2, 3)).equals(IntervalValue(-2, -1)));
829 assert((IntervalValue(-6, 6) >> IntervalValue(2, 3)).equals(IntervalValue(-2, 1)));
831 assert((IntervalValue(0) >> IntervalValue::top()).equals(IntervalValue(0)));
832
833 // and &
836 assert((IntervalValue::top() & IntervalValue(0)).equals(IntervalValue(0)));
837 assert((IntervalValue(4) & IntervalValue(2)).equals(IntervalValue(0)));
838 assert((IntervalValue(3) & IntervalValue(2)).equals(IntervalValue(2)));
839 assert((IntervalValue(-3) & IntervalValue(2)).equals(IntervalValue(0)));
840 assert((IntervalValue(1, 3) & IntervalValue(2)).equals(IntervalValue(0, 2)));
841 assert((IntervalValue(2, 7) & IntervalValue(2)).equals(IntervalValue(0, 2)));
842 assert((IntervalValue(-3, 3) & IntervalValue(2)).equals(IntervalValue(0, 2)));
843 assert((IntervalValue(-3, IntervalValue::plus_infinity()) & IntervalValue(2)).equals(IntervalValue(0, 2)));
845 assert((IntervalValue(1, 3) & IntervalValue(1, 2)).equals(IntervalValue(0, 2)));
846 assert((IntervalValue(-3, 3) & IntervalValue(1, 2)).equals(IntervalValue(0, 2)));
847 assert((IntervalValue(2, 7) & IntervalValue(-2, 3)).equals(IntervalValue(0, 7)));
848 assert((IntervalValue(-2, 7) & IntervalValue(-2, 3)).equals(IntervalValue::top()));
853 assert((IntervalValue(-6, -3) & IntervalValue(3, 9)).equals(IntervalValue(0, 9)));
854 assert((IntervalValue(-6, 6) & IntervalValue(3, 9)).equals(IntervalValue(0, 9)));
855
856 // Or |
859 assert((IntervalValue::top() | IntervalValue(-1)).equals(IntervalValue::top()));//
860 assert((IntervalValue(-1) | IntervalValue::top()).equals(IntervalValue::top()));//
861 assert((IntervalValue(4) | IntervalValue(2)).equals(IntervalValue(6)));
862 assert((IntervalValue(3) | IntervalValue(2)).equals(IntervalValue(3)));
863 assert((IntervalValue(-3) | IntervalValue(2)).equals(IntervalValue(-1)));
864 assert((IntervalValue(1, 3) | IntervalValue(2)).equals(IntervalValue(0, 3)));
865 assert((IntervalValue(2, 7) | IntervalValue(2)).equals(IntervalValue(0, 7)));
866 assert((IntervalValue(-3, 3) | IntervalValue(2)).equals(IntervalValue::top()));
869 assert((IntervalValue(1, 3) | IntervalValue(1, 2)).equals(IntervalValue(0, 3)));
870 assert((IntervalValue(-3, 3) | IntervalValue(1, 2)).equals(IntervalValue::top()));
871 assert((IntervalValue(2, 7) | IntervalValue(-2, 3)).equals(IntervalValue::top()));
872 assert((IntervalValue(-2, 7) | IntervalValue(-2, 3)).equals(IntervalValue::top()));
877 assert((IntervalValue(-6, -3) | IntervalValue(3, 9)).equals(IntervalValue::top()));
878 assert((IntervalValue(-6, 6) | IntervalValue(3, 9)).equals(IntervalValue::top()));
879
880 // Xor ^
883 assert((IntervalValue::top() ^ IntervalValue(-1)).equals(IntervalValue::top()));
884 assert((IntervalValue(-1) ^ IntervalValue::top()).equals(IntervalValue::top()));
885 assert((IntervalValue(4) ^ IntervalValue(2)).equals(IntervalValue(6)));
886 assert((IntervalValue(3) ^ IntervalValue(2)).equals(IntervalValue(1)));
887 assert((IntervalValue(-3) ^ IntervalValue(2)).equals(IntervalValue(-1)));
888 assert((IntervalValue(1, 3) ^ IntervalValue(2)).equals(IntervalValue(0, 3)));
889 assert((IntervalValue(2, 7) ^ IntervalValue(2)).equals(IntervalValue(0, 7)));
890 assert((IntervalValue(-3, 3) ^ IntervalValue(2)).equals(IntervalValue::top()));
893 assert((IntervalValue(1, 3) ^ IntervalValue(1, 2)).equals(IntervalValue(0, 3)));
894 assert((IntervalValue(-3, 3) ^ IntervalValue(1, 2)).equals(IntervalValue::top()));
895 assert((IntervalValue(2, 7) ^ IntervalValue(-2, 3)).equals(IntervalValue::top()));
896 assert((IntervalValue(-2, 7) ^ IntervalValue(-2, 3)).equals(IntervalValue::top()));
901 assert((IntervalValue(-6, -3) ^ IntervalValue(3, 9)).equals(IntervalValue::top()));
902 assert((IntervalValue(-6, 6) ^ IntervalValue(3, 9)).equals(IntervalValue::top()));
903 }
904
906 {
907 AbstractState as;
908 as[1] = IntervalValue(1, 3);
909 as[2] = IntervalValue(2, 7);
910 as[3] = AddressValue(0x7f000007);
911 as[4] = AddressValue(0x7f000008);
912 // store: *as[3] = as[1], *as[4] = as[2]
913 for (auto addr : as[3].getAddrs()) as.store(addr, as[1]);
914 for (auto addr : as[4].getAddrs()) as.store(addr, as[2]);
916 // load: verify *as[3] == as[1] && *as[4] == as[2]
917 AbstractValue v3, v4;
918 for (auto addr : as[3].getAddrs()) v3.join_with(as.load(addr));
919 for (auto addr : as[4].getAddrs()) v4.join_with(as.load(addr));
920 assert(v3.equals(as[1]) && v4.equals(as[2]));
921 }
922};
923
924
925int main(int argc, char** argv)
926{
927 int arg_num = 0;
928 int extraArgc = 3;
929 char **arg_value = new char *[argc + extraArgc];
930 for (; arg_num < argc; ++arg_num)
931 {
932 arg_value[arg_num] = argv[arg_num];
933 }
934 // add extra options
935 arg_value[arg_num++] = (char*) "-model-consts=true";
936 arg_value[arg_num++] = (char*) "-model-arrays=true";
937 arg_value[arg_num++] = (char*) "-pre-field-sensitive=false";
938 assert(arg_num == (argc + extraArgc) && "more extra arguments? Change the value of extraArgc");
939
940 std::vector<std::string> moduleNameVec;
941 moduleNameVec = OptionBase::parseOptions(
942 arg_num, arg_value, "Static Symbolic Execution", "[options] <input-bitcode...>"
943 );
944 delete[] arg_value;
945 if (SYMABS())
946 {
948 saTest.testsValidation();
949 return 0;
950 }
951
952 if (AETEST())
953 {
954 AETest aeTest;
957 aeTest.testBinaryOpStmt();
958 aeTest.testAbsState();
959 return 0;
960 }
961
963 PAGBuilder builder;
964 PAG* pag = builder.build();
965 // Run Andersen's to resolve indirect calls, then update PAG with resolved targets.
966 // The Andersen singleton will be reused inside AbstractInterpretation::runOnModule().
968 builder.updateCallGraph(ander->getCallGraph());
971 ae.addDetector(std::make_unique<BufOverflowDetector>());
973 ae.addDetector(std::make_unique<NullptrDerefDetector>());
974 ae.runOnModule();
975
978
979 return 0;
980}
int main(int argc, char **argv)
Definition ae.cpp:925
static Option< bool > SYMABS("symabs", "symbolic abstraction test", false)
static Option< bool > AETEST("aetest", "abstract execution basic function test", false)
Definition ae.cpp:628
~AETest()=default
void testBoundedIntBoundaryArithmetic()
Definition ae.cpp:634
AETest()=default
void testIntervalBoundaryArithmetic()
Definition ae.cpp:672
void testBinaryOpStmt()
Definition ae.cpp:704
void testAbsState()
Definition ae.cpp:905
static std::vector< std::string > parseOptions(int argc, char *argv[], std::string description, std::string callFormat)
Definition CommandLine.h:75
void addDetector(std::unique_ptr< AEDetector > detector)
static AbstractInterpretation & getAEInstance()
const VarToAbsValMap & getVarToVal() const
get var2val map
void store(u32_t addr, const AbstractValue &val)
void printAbstractState() const
bool eqVarToValMap(const VarToAbsValMap &lhs, const VarToAbsValMap &rhs) const
virtual AbstractValue & load(u32_t addr)
Map< u32_t, AbstractValue > VarToAbsValMap
AbstractState sliceState(Set< u32_t > &sl)
Copy some values and return a new IntervalExeState.
void join_with(const AbstractValue &other)
bool equals(const AbstractValue &rhs) const
static AndersenWaveDiff * createAndersenWaveDiff(PAG *_pag)
Create an singleton instance directly instead of invoking llvm pass manager.
Definition Andersen.h:430
static void releaseAndersenWaveDiff()
Definition Andersen.h:440
A class representing a bounded 64-bit integer.
static BoundedInt plus_infinity()
static BoundedInt minus_infinity()
static BoundedInt minus_infinity()
Get minus infinity -inf.
static IntervalValue bottom()
Create the bottom IntervalValue [+inf, -inf].
static BoundedInt plus_infinity()
Get plus infinity +inf.
bool equals(const IntervalValue &other) const
Equality comparison.
static IntervalValue top()
Create the IntervalValue [-inf, +inf].
static LLVMModuleSet * getLLVMModuleSet()
Definition LLVMModule.h:137
static void releaseLLVMModuleSet()
Definition LLVMModule.h:144
static void buildSVFModule(Module &mod)
static const Option< bool > NullDerefCheck
nullptr dereference checker, Default: false
Definition Options.h:250
static const Option< bool > BufferOverflowCheck
buffer overflow checker, Default: false
Definition Options.h:248
virtual PAG * build()
Start building PAG here.
void updateCallGraph(CallGraph *callgraph)
connect PAG edges based on callgraph
Definition PAG.h:46
CallGraph * getCallGraph() const
Return call graph.
void extractSubVars(const Z3Expr &expr, Set< u32_t > &res)
Extract sub SVFVar IDs of a Z3Expr.
Z3Expr gamma_hat(const AbstractState &exeState) const
Return Z3Expr according to valToValMap.
AbstractState RSY(const AbstractState &domain, const Z3Expr &phi)
AbstractState bilateral(const AbstractState &domain, const Z3Expr &phi, u32_t descend_check=0)
AbstractState BS(const AbstractState &domain, const Z3Expr &phi)
static z3::context & getContext()
Get z3 context, singleton design here to make sure we only have one context.
Definition Z3Expr.cpp:66
AbstractState BS_time(AbstractState &inv, const Z3Expr &phi, RelationSolver &rs)
Definition ae.cpp:97
void testRelExeState1_2()
Definition ae.cpp:144
SymblicAbstractionTest()=default
void testRelExeState2_4()
Definition ae.cpp:307
void testRelExeState2_1()
Definition ae.cpp:179
void testRelExeState2_2()
Definition ae.cpp:221
void testRelExeState3_2()
Definition ae.cpp:435
~SymblicAbstractionTest()=default
void testsValidation()
Definition ae.cpp:605
void testRelExeState2_3()
Definition ae.cpp:264
void testRelExeState3_3()
Definition ae.cpp:477
AbstractState RSY_time(AbstractState &inv, const Z3Expr &phi, RelationSolver &rs)
Definition ae.cpp:73
void testRelExeState4_1()
Definition ae.cpp:560
void testRelExeState2_5()
Definition ae.cpp:350
void testRelExeState3_1()
Definition ae.cpp:393
void testRelExeState1_1()
Definition ae.cpp:110
AbstractState Bilateral_time(AbstractState &inv, const Z3Expr &phi, RelationSolver &rs)
Definition ae.cpp:85
static z3::context & getContext()
Definition ae.cpp:63
void testRelExeState3_4()
Definition ae.cpp:518
std::string sucMsg(const std::string &msg)
Returns successful message by converting a string into green string output.
Definition SVFUtil.cpp:75
std::ostream & outs()
Overwrite llvm::outs().
Definition SVFUtil.h:58
for isBitcode
Definition BasicTypes.h:70
std::unordered_set< Key, Hash, KeyEqual, Allocator > Set
Definition GeneralType.h:51
signed long long s64_t
Definition GeneralType.h:70