-
Notifications
You must be signed in to change notification settings - Fork 273
/
Copy pathsymex_target_equation.cpp
725 lines (610 loc) · 18.8 KB
/
symex_target_equation.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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/*******************************************************************\
Module: Symbolic Execution
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Symbolic Execution
/// Implementation of functions to build SSA equation.
#include "symex_target_equation.h"
#include <util/std_expr.h>
#include "solver_hardness.h"
#include "ssa_step.h"
#include <chrono> // IWYU pragma: keep
static std::function<void(solver_hardnesst &)>
hardness_register_ssa(std::size_t step_index, const SSA_stept &step)
{
return [step_index, &step](solver_hardnesst &hardness) {
hardness.register_ssa(step_index, step.cond_expr, step.source.pc);
};
}
void symex_target_equationt::shared_read(
const exprt &guard,
const ssa_exprt &ssa_object,
unsigned atomic_section_id,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::SHARED_READ);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.ssa_lhs=ssa_object;
SSA_step.atomic_section_id=atomic_section_id;
merge_ireps(SSA_step);
}
void symex_target_equationt::shared_write(
const exprt &guard,
const ssa_exprt &ssa_object,
unsigned atomic_section_id,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::SHARED_WRITE);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.ssa_lhs=ssa_object;
SSA_step.atomic_section_id=atomic_section_id;
merge_ireps(SSA_step);
}
/// spawn a new thread
void symex_target_equationt::spawn(
const exprt &guard,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::SPAWN);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
merge_ireps(SSA_step);
}
void symex_target_equationt::memory_barrier(
const exprt &guard,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::MEMORY_BARRIER);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
merge_ireps(SSA_step);
}
/// start an atomic section
void symex_target_equationt::atomic_begin(
const exprt &guard,
unsigned atomic_section_id,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::ATOMIC_BEGIN);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.atomic_section_id=atomic_section_id;
merge_ireps(SSA_step);
}
/// end an atomic section
void symex_target_equationt::atomic_end(
const exprt &guard,
unsigned atomic_section_id,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::ATOMIC_END);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.atomic_section_id=atomic_section_id;
merge_ireps(SSA_step);
}
void symex_target_equationt::assignment(
const exprt &guard,
const ssa_exprt &ssa_lhs,
const exprt &ssa_full_lhs,
const exprt &original_full_lhs,
const exprt &ssa_rhs,
const sourcet &source,
assignment_typet assignment_type)
{
PRECONDITION(ssa_lhs.is_not_nil());
SSA_steps.emplace_back(SSA_assignment_stept{source,
guard,
ssa_lhs,
ssa_full_lhs,
original_full_lhs,
ssa_rhs,
assignment_type});
merge_ireps(SSA_steps.back());
}
void symex_target_equationt::decl(
const exprt &guard,
const ssa_exprt &ssa_lhs,
const exprt &initializer,
const sourcet &source,
assignment_typet assignment_type)
{
PRECONDITION(ssa_lhs.is_not_nil());
SSA_steps.emplace_back(source, goto_trace_stept::typet::DECL);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.ssa_lhs=ssa_lhs;
SSA_step.ssa_full_lhs = initializer;
SSA_step.original_full_lhs=ssa_lhs.get_original_expr();
SSA_step.hidden=(assignment_type!=assignment_typet::STATE);
// the condition is trivially true, and only
// there so we see the symbols
SSA_step.cond_expr=equal_exprt(SSA_step.ssa_lhs, SSA_step.ssa_lhs);
merge_ireps(SSA_step);
}
/// declare a fresh variable
void symex_target_equationt::dead(
const exprt &,
const ssa_exprt &,
const sourcet &)
{
// we currently don't record these
}
void symex_target_equationt::location(
const exprt &guard,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::LOCATION);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
merge_ireps(SSA_step);
}
void symex_target_equationt::function_call(
const exprt &guard,
const irep_idt &function_id,
const std::vector<exprt> &function_arguments,
const sourcet &source,
const bool hidden)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::FUNCTION_CALL);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard = guard;
SSA_step.called_function = function_id;
SSA_step.ssa_function_arguments = function_arguments;
SSA_step.hidden = hidden;
merge_ireps(SSA_step);
}
void symex_target_equationt::function_return(
const exprt &guard,
const irep_idt &function_id,
const sourcet &source,
const bool hidden)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::FUNCTION_RETURN);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard = guard;
SSA_step.called_function = function_id;
SSA_step.hidden = hidden;
merge_ireps(SSA_step);
}
void symex_target_equationt::output(
const exprt &guard,
const sourcet &source,
const irep_idt &output_id,
const std::list<exprt> &args)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::OUTPUT);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.io_args = args;
SSA_step.io_id=output_id;
merge_ireps(SSA_step);
}
void symex_target_equationt::output_fmt(
const exprt &guard,
const sourcet &source,
const irep_idt &output_id,
const irep_idt &fmt,
const std::list<exprt> &args)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::OUTPUT);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.io_args=args;
SSA_step.io_id=output_id;
SSA_step.formatted=true;
SSA_step.format_string=fmt;
merge_ireps(SSA_step);
}
void symex_target_equationt::input(
const exprt &guard,
const sourcet &source,
const irep_idt &input_id,
const std::list<exprt> &args)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::INPUT);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.io_args=args;
SSA_step.io_id=input_id;
merge_ireps(SSA_step);
}
void symex_target_equationt::assumption(
const exprt &guard,
const exprt &cond,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::ASSUME);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.cond_expr=cond;
merge_ireps(SSA_step);
}
void symex_target_equationt::assertion(
const exprt &guard,
const exprt &cond,
const irep_idt &property_id,
const std::string &msg,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::ASSERT);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.cond_expr=cond;
SSA_step.comment=msg;
SSA_step.property_id = property_id;
merge_ireps(SSA_step);
}
void symex_target_equationt::goto_instruction(
const exprt &guard,
const exprt &cond,
const sourcet &source)
{
SSA_steps.emplace_back(source, goto_trace_stept::typet::GOTO);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=guard;
SSA_step.cond_expr = cond;
merge_ireps(SSA_step);
}
void symex_target_equationt::constraint(
const exprt &cond,
const std::string &msg,
const sourcet &source)
{
// like assumption, but with global effect
SSA_steps.emplace_back(source, goto_trace_stept::typet::CONSTRAINT);
SSA_stept &SSA_step=SSA_steps.back();
SSA_step.guard=true_exprt();
SSA_step.cond_expr=cond;
SSA_step.comment=msg;
merge_ireps(SSA_step);
}
void symex_target_equationt::convert_without_assertions(
decision_proceduret &decision_procedure)
{
with_solver_hardness(decision_procedure, [&](solver_hardnesst &hardness) {
hardness.register_ssa_size(SSA_steps.size());
});
convert_guards(decision_procedure);
convert_assignments(decision_procedure);
convert_decls(decision_procedure);
convert_assumptions(decision_procedure);
convert_goto_instructions(decision_procedure);
convert_function_calls(decision_procedure);
convert_io(decision_procedure);
convert_constraints(decision_procedure);
}
void symex_target_equationt::convert(decision_proceduret &decision_procedure)
{
const auto convert_SSA_start = std::chrono::steady_clock::now();
convert_without_assertions(decision_procedure);
convert_assertions(decision_procedure);
const auto convert_SSA_stop = std::chrono::steady_clock::now();
std::chrono::duration<double> convert_SSA_runtime =
std::chrono::duration<double>(convert_SSA_stop - convert_SSA_start);
log.statistics() << "Runtime Convert SSA: " << convert_SSA_runtime.count()
<< "s" << messaget::eom;
}
void symex_target_equationt::convert_assignments(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.is_assignment() && !step.ignore && !step.converted)
{
log.conditional_output(log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
decision_procedure.set_to_true(step.cond_expr);
step.converted = true;
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
++step_index;
}
}
void symex_target_equationt::convert_decls(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.is_decl() && !step.ignore && !step.converted)
{
// The result is not used, these have no impact on
// the satisfiability of the formula.
decision_procedure.handle(step.cond_expr);
decision_procedure.handle(
equal_exprt{step.ssa_full_lhs, step.ssa_full_lhs});
step.converted = true;
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
++step_index;
}
}
void symex_target_equationt::convert_guards(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.ignore)
step.guard_handle = false_exprt();
else
{
log.conditional_output(log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
step.guard_handle = decision_procedure.handle(step.guard);
with_solver_hardness(
decision_procedure, [step_index, &step](solver_hardnesst &hardness) {
hardness.register_ssa(step_index, step.guard, step.source.pc);
});
}
++step_index;
}
}
void symex_target_equationt::convert_assumptions(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.is_assume())
{
if(step.ignore)
step.cond_handle = true_exprt();
else
{
log.conditional_output(
log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
step.cond_handle = decision_procedure.handle(step.cond_expr);
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
}
++step_index;
}
}
void symex_target_equationt::convert_goto_instructions(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.is_goto())
{
if(step.ignore)
step.cond_handle = true_exprt();
else
{
log.conditional_output(
log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
step.cond_handle = decision_procedure.handle(step.cond_expr);
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
}
++step_index;
}
}
void symex_target_equationt::convert_constraints(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(step.is_constraint() && !step.ignore && !step.converted)
{
log.conditional_output(log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
decision_procedure.set_to_true(step.cond_expr);
step.converted = true;
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
++step_index;
}
}
void symex_target_equationt::convert_assertions(
decision_proceduret &decision_procedure,
bool optimized_for_single_assertions)
{
// we find out if there is only _one_ assertion,
// which allows for a simpler formula
std::size_t number_of_assertions=count_assertions();
if(number_of_assertions==0)
return;
if(number_of_assertions == 1 && optimized_for_single_assertions)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
// hide already converted assertions in the error trace
if(step.is_assert() && step.converted)
step.hidden = true;
if(step.is_assert() && !step.ignore && !step.converted)
{
step.converted = true;
decision_procedure.set_to_false(step.cond_expr);
step.cond_handle = false_exprt();
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
return; // prevent further assumptions!
}
else if(step.is_assume())
{
decision_procedure.set_to_true(step.cond_expr);
with_solver_hardness(
decision_procedure, hardness_register_ssa(step_index, step));
}
++step_index;
}
UNREACHABLE; // unreachable
}
// We do (NOT a1) OR (NOT a2) ...
// where the a's are the assertions
or_exprt::operandst disjuncts;
disjuncts.reserve(number_of_assertions);
exprt assumption=true_exprt();
std::vector<goto_programt::const_targett> involved_steps;
for(auto &step : SSA_steps)
{
// hide already converted assertions in the error trace
if(step.is_assert() && step.converted)
step.hidden = true;
if(step.is_assert() && !step.ignore && !step.converted)
{
step.converted = true;
log.conditional_output(log.debug(), [&step](messaget::mstreamt &mstream) {
step.output(mstream);
mstream << messaget::eom;
});
implies_exprt implication(
assumption,
step.cond_expr);
// do the conversion
step.cond_handle = decision_procedure.handle(implication);
with_solver_hardness(
decision_procedure,
[&involved_steps, &step](solver_hardnesst &hardness) {
involved_steps.push_back(step.source.pc);
});
// store disjunct
disjuncts.push_back(not_exprt(step.cond_handle));
}
else if(step.is_assume())
{
// the assumptions have been converted before
// avoid deep nesting of ID_and expressions
if(assumption.id()==ID_and)
assumption.copy_to_operands(step.cond_handle);
else
assumption = and_exprt(assumption, step.cond_handle);
with_solver_hardness(
decision_procedure,
[&involved_steps, &step](solver_hardnesst &hardness) {
involved_steps.push_back(step.source.pc);
});
}
}
const auto assertion_disjunction = disjunction(disjuncts);
// the below is 'true' if there are no assertions
decision_procedure.set_to_true(assertion_disjunction);
with_solver_hardness(
decision_procedure,
[&assertion_disjunction, &involved_steps](solver_hardnesst &hardness) {
hardness.register_assertion_ssas(assertion_disjunction, involved_steps);
});
}
void symex_target_equationt::convert_function_calls(
decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(!step.ignore)
{
and_exprt::operandst conjuncts;
step.converted_function_arguments.reserve(step.ssa_function_arguments.size());
for(const auto &arg : step.ssa_function_arguments)
{
if(arg.is_constant() ||
arg.id()==ID_string_constant)
step.converted_function_arguments.push_back(arg);
else
{
const irep_idt identifier="symex::args::"+std::to_string(argument_count++);
symbol_exprt symbol(identifier, arg.type());
equal_exprt eq(arg, symbol);
merge_irep(eq);
decision_procedure.set_to(eq, true);
conjuncts.push_back(eq);
step.converted_function_arguments.push_back(symbol);
}
}
with_solver_hardness(
decision_procedure,
[step_index, &conjuncts, &step](solver_hardnesst &hardness) {
hardness.register_ssa(
step_index, conjunction(conjuncts), step.source.pc);
});
}
++step_index;
}
}
void symex_target_equationt::convert_io(decision_proceduret &decision_procedure)
{
std::size_t step_index = 0;
for(auto &step : SSA_steps)
{
if(!step.ignore)
{
and_exprt::operandst conjuncts;
for(const auto &arg : step.io_args)
{
if(arg.is_constant() ||
arg.id()==ID_string_constant)
step.converted_io_args.push_back(arg);
else
{
const irep_idt identifier =
"symex::io::" + std::to_string(io_count++);
symbol_exprt symbol(identifier, arg.type());
equal_exprt eq(arg, symbol);
merge_irep(eq);
decision_procedure.set_to(eq, true);
conjuncts.push_back(eq);
step.converted_io_args.push_back(symbol);
}
}
with_solver_hardness(
decision_procedure,
[step_index, &conjuncts, &step](solver_hardnesst &hardness) {
hardness.register_ssa(
step_index, conjunction(conjuncts), step.source.pc);
});
}
++step_index;
}
}
/// Merging causes identical ireps to be shared.
/// This is only enabled if the definition SHARING is defined.
/// \param SSA_step The step you want to have shared values.
void symex_target_equationt::merge_ireps(SSA_stept &SSA_step)
{
merge_irep(SSA_step.guard);
merge_irep(SSA_step.ssa_lhs);
merge_irep(SSA_step.ssa_full_lhs);
merge_irep(SSA_step.original_full_lhs);
merge_irep(SSA_step.ssa_rhs);
merge_irep(SSA_step.cond_expr);
for(auto &step : SSA_step.io_args)
merge_irep(step);
for(auto &arg : SSA_step.ssa_function_arguments)
merge_irep(arg);
// converted_io_args is merged in convert_io
}
void symex_target_equationt::output(std::ostream &out) const
{
for(const auto &step : SSA_steps)
{
step.output(out);
out << "--------------\n";
}
}