forked from diffblue/cbmc
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvalue_set.cpp
2066 lines (1802 loc) · 53.1 KB
/
value_set.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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************\
Module: Value Set
Author: Daniel Kroening, [email protected]
\*******************************************************************/
/// \file
/// Value Set
#include "value_set.h"
#include <util/arith_tools.h>
#include <util/bitvector_expr.h>
#include <util/byte_operators.h>
#include <util/c_types.h>
#include <util/expr_util.h>
#include <util/format_expr.h>
#include <util/format_type.h>
#include <util/namespace.h>
#include <util/pointer_expr.h>
#include <util/pointer_offset_size.h>
#include <util/prefix.h>
#include <util/range.h>
#include <util/simplify_expr.h>
#include <util/std_code.h>
#include <util/symbol.h>
#include <util/xml.h>
#include <ostream>
#ifdef DEBUG
#include <iostream>
#include <util/format_expr.h>
#include <util/format_type.h>
#endif
#include "add_failed_symbols.h"
// Due to a large number of functions defined inline, `value_sett` and
// associated types are documented in its header file, `value_set.h`.
const value_sett::object_map_dt value_sett::empty_object_map{};
object_numberingt value_sett::object_numbering;
bool value_sett::field_sensitive(const irep_idt &id, const typet &type)
{
// we always track fields on these
if(
id.starts_with("value_set::dynamic_object") ||
id == "value_set::return_value" || id == "value_set::memory")
return true;
// otherwise it has to be a struct
return type.id() == ID_struct || type.id() == ID_struct_tag;
}
const value_sett::entryt *value_sett::find_entry(const irep_idt &id) const
{
auto found = values.find(id);
return !found.has_value() ? nullptr : &(found->get());
}
void value_sett::update_entry(
const entryt &e,
const typet &type,
const object_mapt &new_values,
bool add_to_sets)
{
irep_idt index;
if(field_sensitive(e.identifier, type))
index=id2string(e.identifier)+e.suffix;
else
index=e.identifier;
auto existing_entry = values.find(index);
if(existing_entry.has_value())
{
if(add_to_sets)
{
if(make_union_would_change(existing_entry->get().object_map, new_values))
{
values.update(index, [&new_values, this](entryt &entry) {
make_union(entry.object_map, new_values);
});
}
}
else
{
values.update(
index, [&new_values](entryt &entry) { entry.object_map = new_values; });
}
}
else
{
entryt new_entry = e;
new_entry.object_map = new_values;
values.insert(index, std::move(new_entry));
}
}
value_sett::insert_actiont value_sett::get_insert_action(
const object_mapt &dest,
object_numberingt::number_type n,
const offsett &offset) const
{
auto entry=dest.read().find(n);
if(entry==dest.read().end())
{
// new
return insert_actiont::INSERT;
}
else if(!entry->second)
return insert_actiont::NONE;
else if(offset && *entry->second == *offset)
return insert_actiont::NONE;
else
return insert_actiont::RESET_OFFSET;
}
bool value_sett::insert(
object_mapt &dest,
object_numberingt::number_type n,
const offsett &offset) const
{
auto insert_action = get_insert_action(dest, n, offset);
if(insert_action == insert_actiont::NONE)
return false;
auto &new_offset = dest.write()[n];
if(insert_action == insert_actiont::INSERT)
new_offset = offset;
else
new_offset.reset();
return true;
}
void value_sett::output(std::ostream &out, const std::string &indent) const
{
values.iterate([&](const irep_idt &, const entryt &e) {
irep_idt identifier, display_name;
if(e.identifier.starts_with("value_set::dynamic_object"))
{
display_name = id2string(e.identifier) + e.suffix;
identifier.clear();
}
else if(e.identifier == "value_set::return_value")
{
display_name = "RETURN_VALUE" + e.suffix;
identifier.clear();
}
else
{
#if 0
const symbolt &symbol=ns.lookup(e.identifier);
display_name=id2string(symbol.display_name())+e.suffix;
identifier=symbol.name;
#else
identifier = id2string(e.identifier);
display_name = id2string(identifier) + e.suffix;
#endif
}
out << indent << display_name << " = { ";
const object_map_dt &object_map = e.object_map.read();
std::size_t width = 0;
for(object_map_dt::const_iterator o_it = object_map.begin();
o_it != object_map.end();
o_it++)
{
const exprt &o = object_numbering[o_it->first];
std::ostringstream stream;
if(o.id() == ID_invalid || o.id() == ID_unknown)
stream << format(o);
else
{
stream << "<" << format(o) << ", ";
if(o_it->second)
stream << *o_it->second;
else
stream << '*';
if(o.type().is_nil())
stream << ", ?";
else
stream << ", " << format(o.type());
stream << '>';
}
const std::string result = stream.str();
out << result;
width += result.size();
object_map_dt::const_iterator next(o_it);
next++;
if(next != object_map.end())
{
out << ", ";
if(width >= 40)
out << "\n" << std::string(indent.size(), ' ') << " ";
}
}
out << " } \n";
});
}
xmlt value_sett::output_xml(void) const
{
xmlt output;
value_sett::valuest::viewt view;
this->values.get_view(view);
for(const auto &values_entry : view)
{
xmlt &var = output.new_element("variable");
var.new_element("identifier").data = id2string(values_entry.first);
#if 0
const value_sett::expr_sett &expr_set=
value_entries.expr_set();
for(value_sett::expr_sett::const_iterator
e_it=expr_set.begin();
e_it!=expr_set.end();
e_it++)
{
std::string value_str=
from_expr(ns, identifier, *e_it);
var.new_element("value").data=
xmlt::escape(value_str);
}
#endif
}
return output;
}
exprt value_sett::to_expr(const object_map_dt::value_type &it) const
{
const exprt &object=object_numbering[it.first];
if(object.id()==ID_invalid ||
object.id()==ID_unknown)
return object;
object_descriptor_exprt od;
od.object()=object;
if(it.second)
od.offset() = from_integer(*it.second, c_index_type());
od.type()=od.object().type();
return std::move(od);
}
bool value_sett::make_union(const value_sett::valuest &new_values)
{
bool result=false;
value_sett::valuest::delta_viewt delta_view;
new_values.get_delta_view(values, delta_view, false);
for(const auto &delta_entry : delta_view)
{
if(delta_entry.is_in_both_maps())
{
if(make_union_would_change(
delta_entry.get_other_map_value().object_map,
delta_entry.m.object_map))
{
values.update(delta_entry.k, [&](entryt &existing_entry) {
make_union(existing_entry.object_map, delta_entry.m.object_map);
});
result = true;
}
}
else
{
values.insert(delta_entry.k, delta_entry.m);
result = true;
}
}
return result;
}
bool value_sett::make_union_would_change(
const object_mapt &dest,
const object_mapt &src) const
{
for(const auto &number_and_offset : src.read())
{
if(
get_insert_action(
dest, number_and_offset.first, number_and_offset.second) !=
insert_actiont::NONE)
{
return true;
}
}
return false;
}
bool value_sett::make_union(object_mapt &dest, const object_mapt &src) const
{
bool result=false;
for(object_map_dt::const_iterator it=src.read().begin();
it!=src.read().end();
it++)
{
if(insert(dest, *it))
result=true;
}
return result;
}
bool value_sett::eval_pointer_offset(
exprt &expr,
const namespacet &ns) const
{
bool mod=false;
if(expr.id()==ID_pointer_offset)
{
const object_mapt reference_set =
get_value_set(to_pointer_offset_expr(expr).pointer(), ns, true);
exprt new_expr;
mp_integer previous_offset=0;
const object_map_dt &object_map=reference_set.read();
for(object_map_dt::const_iterator
it=object_map.begin();
it!=object_map.end();
it++)
if(!it->second)
return false;
else
{
const exprt &object=object_numbering[it->first];
auto ptr_offset = compute_pointer_offset(object, ns);
if(!ptr_offset.has_value())
return false;
*ptr_offset += *it->second;
if(mod && *ptr_offset != previous_offset)
return false;
new_expr = from_integer(*ptr_offset, expr.type());
previous_offset = *ptr_offset;
mod=true;
}
if(mod)
expr.swap(new_expr);
}
else
{
Forall_operands(it, expr)
mod=eval_pointer_offset(*it, ns) || mod;
}
return mod;
}
std::vector<exprt>
value_sett::get_value_set(exprt expr, const namespacet &ns) const
{
const object_mapt object_map = get_value_set(std::move(expr), ns, false);
return make_range(object_map.read())
.map([&](const object_map_dt::value_type &pair) { return to_expr(pair); });
}
value_sett::object_mapt value_sett::get_value_set(
exprt expr,
const namespacet &ns,
bool is_simplified) const
{
if(!is_simplified)
simplify(expr, ns);
object_mapt dest;
bool includes_nondet_pointer = false;
get_value_set_rec(expr, dest, includes_nondet_pointer, "", expr.type(), ns);
if(includes_nondet_pointer && expr.type().id() == ID_pointer)
{
// we'll take the union of all objects we see, with unspecified offsets
values.iterate([this, &dest](const irep_idt &key, const entryt &value) {
for(const auto &object : value.object_map.read())
insert(dest, object.first, offsett());
});
// we'll add null, in case it's not there yet
insert(
dest,
exprt(ID_null_object, to_pointer_type(expr.type()).base_type()),
offsett());
}
return dest;
}
/// Check if 'suffix' starts with 'field'.
/// Suffix is delimited by periods and '[]' (array access) tokens, so we're
/// looking for ".field($|[]|.)"
static bool suffix_starts_with_field(
const std::string &suffix, const std::string &field)
{
return
!suffix.empty() &&
suffix[0] == '.' &&
suffix.compare(1, field.length(), field) == 0 &&
(suffix.length() == field.length() + 1 ||
suffix[field.length() + 1] == '.' ||
suffix[field.length() + 1] == '[');
}
static std::string strip_first_field_from_suffix(
const std::string &suffix, const std::string &field)
{
INVARIANT(
suffix_starts_with_field(suffix, field),
"suffix should start with " + field);
return suffix.substr(field.length() + 1);
}
std::optional<irep_idt> value_sett::get_index_of_symbol(
irep_idt identifier,
const typet &type,
const std::string &suffix,
const namespacet &ns) const
{
if(
type.id() != ID_pointer && type.id() != ID_signedbv &&
type.id() != ID_unsignedbv && type.id() != ID_array &&
type.id() != ID_struct && type.id() != ID_struct_tag &&
type.id() != ID_union && type.id() != ID_union_tag)
{
return {};
}
// look it up
irep_idt index = id2string(identifier) + suffix;
auto *entry = find_entry(index);
if(entry)
return std::move(index);
const typet &followed_type = type.id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(type))
: type.id() == ID_union_tag
? ns.follow_tag(to_union_tag_type(type))
: type;
// try first component name as suffix if not yet found
if(followed_type.id() == ID_struct || followed_type.id() == ID_union)
{
const struct_union_typet &struct_union_type =
to_struct_union_type(followed_type);
if(!struct_union_type.components().empty())
{
const irep_idt &first_component_name =
struct_union_type.components().front().get_name();
index =
id2string(identifier) + "." + id2string(first_component_name) + suffix;
entry = find_entry(index);
if(entry)
return std::move(index);
}
}
// not found? try without suffix
entry = find_entry(identifier);
if(entry)
return std::move(identifier);
return {};
}
void value_sett::get_value_set_rec(
const exprt &expr,
object_mapt &dest,
bool &includes_nondet_pointer,
const std::string &suffix,
const typet &original_type,
const namespacet &ns) const
{
#ifdef DEBUG
std::cout << "GET_VALUE_SET_REC EXPR: " << format(expr) << "\n";
std::cout << "GET_VALUE_SET_REC SUFFIX: " << suffix << '\n';
#endif
if(expr.id()==ID_unknown || expr.id()==ID_invalid)
{
insert(dest, exprt(ID_unknown, original_type));
}
else if(expr.id()==ID_index)
{
const typet &type = to_index_expr(expr).array().type();
DATA_INVARIANT(
type.id() == ID_array, "operand 0 of index expression must be an array");
get_value_set_rec(
to_index_expr(expr).array(),
dest,
includes_nondet_pointer,
"[]" + suffix,
original_type,
ns);
}
else if(expr.id()==ID_member)
{
const exprt &compound = to_member_expr(expr).compound();
DATA_INVARIANT(
compound.type().id() == ID_struct ||
compound.type().id() == ID_struct_tag ||
compound.type().id() == ID_union ||
compound.type().id() == ID_union_tag,
"compound of member expression must be struct or union");
const std::string &component_name=
expr.get_string(ID_component_name);
get_value_set_rec(
compound,
dest,
includes_nondet_pointer,
"." + component_name + suffix,
original_type,
ns);
}
else if(expr.id()==ID_symbol)
{
auto entry_index = get_index_of_symbol(
to_symbol_expr(expr).get_identifier(), expr.type(), suffix, ns);
if(entry_index.has_value())
make_union(dest, find_entry(*entry_index)->object_map);
else
insert(dest, exprt(ID_unknown, original_type));
}
else if(expr.id() == ID_nondet_symbol)
{
if(expr.type().id() == ID_pointer)
includes_nondet_pointer = true;
else
insert(dest, exprt(ID_unknown, original_type));
}
else if(expr.id()==ID_if)
{
get_value_set_rec(
to_if_expr(expr).true_case(),
dest,
includes_nondet_pointer,
suffix,
original_type,
ns);
get_value_set_rec(
to_if_expr(expr).false_case(),
dest,
includes_nondet_pointer,
suffix,
original_type,
ns);
}
else if(expr.id()==ID_address_of)
{
get_reference_set(to_address_of_expr(expr).object(), dest, ns);
}
else if(expr.id()==ID_dereference)
{
object_mapt reference_set;
get_reference_set(expr, reference_set, ns);
const object_map_dt &object_map=reference_set.read();
if(object_map.begin()==object_map.end())
insert(dest, exprt(ID_unknown, original_type));
else
{
for(object_map_dt::const_iterator
it1=object_map.begin();
it1!=object_map.end();
it1++)
{
/// Do not take a reference to object_numbering's storage as we may call
/// object_numbering.number(), which may reallocate it.
const exprt object=object_numbering[it1->first];
get_value_set_rec(
object, dest, includes_nondet_pointer, suffix, original_type, ns);
}
}
}
else if(expr.is_constant())
{
// check if NULL
if(is_null_pointer(to_constant_expr(expr)))
{
insert(
dest,
exprt(ID_null_object, to_pointer_type(expr.type()).base_type()),
mp_integer{0});
}
else if(
expr.type().id() == ID_unsignedbv || expr.type().id() == ID_signedbv)
{
// an integer constant got turned into a pointer
insert(dest, exprt(ID_integer_address, unsigned_char_type()));
}
else
insert(dest, exprt(ID_unknown, original_type));
}
else if(expr.id()==ID_typecast)
{
// let's see what gets converted to what
const auto &op = to_typecast_expr(expr).op();
const typet &op_type = op.type();
if(op_type.id()==ID_pointer)
{
// pointer-to-something -- we just ignore the type cast
get_value_set_rec(
op, dest, includes_nondet_pointer, suffix, original_type, ns);
}
else if(
op_type.id() == ID_unsignedbv || op_type.id() == ID_signedbv ||
op_type.id() == ID_bv)
{
// integer-to-something
if(op.is_zero())
{
insert(dest, exprt(ID_null_object, empty_typet{}), mp_integer{0});
}
else
{
// see if we have something for the integer
object_mapt tmp;
get_value_set_rec(
op, tmp, includes_nondet_pointer, suffix, original_type, ns);
if(tmp.read().empty())
{
// if not, throw in integer
insert(dest, exprt(ID_integer_address, unsigned_char_type()));
}
else if(tmp.read().size()==1 &&
object_numbering[tmp.read().begin()->first].id()==ID_unknown)
{
// if not, throw in integer
insert(dest, exprt(ID_integer_address, unsigned_char_type()));
}
else
{
// use as is
dest.write().insert(tmp.read().begin(), tmp.read().end());
}
}
}
else
insert(dest, exprt(ID_unknown, original_type));
}
else if(
expr.id() == ID_plus || expr.id() == ID_minus || expr.id() == ID_bitor ||
expr.id() == ID_bitand || expr.id() == ID_bitxor ||
expr.id() == ID_bitnand || expr.id() == ID_bitnor ||
expr.id() == ID_bitxnor)
{
if(expr.operands().size()<2)
throw expr.id_string()+" expected to have at least two operands";
object_mapt pointer_expr_set;
std::optional<mp_integer> i;
// special case for plus/minus and exactly one pointer
std::optional<exprt> ptr_operand;
if(
expr.type().id() == ID_pointer &&
(expr.id() == ID_plus || expr.id() == ID_minus))
{
bool non_const_offset = false;
for(const auto &op : expr.operands())
{
if(op.type().id() == ID_pointer)
{
if(ptr_operand.has_value())
{
ptr_operand.reset();
break;
}
ptr_operand = op;
}
else if(!non_const_offset)
{
auto offset = numeric_cast<mp_integer>(op);
if(!offset.has_value())
{
i.reset();
non_const_offset = true;
}
else
{
if(!i.has_value())
i = mp_integer{0};
i = *i + *offset;
}
}
}
if(ptr_operand.has_value() && i.has_value())
{
typet pointer_base_type =
to_pointer_type(ptr_operand->type()).base_type();
if(pointer_base_type.id() == ID_empty)
pointer_base_type = char_type();
auto size = pointer_offset_size(pointer_base_type, ns);
if(!size.has_value() || (*size) == 0)
{
i.reset();
}
else
{
*i *= *size;
if(expr.id()==ID_minus)
{
DATA_INVARIANT(
to_minus_expr(expr).lhs() == *ptr_operand,
"unexpected subtraction of pointer from integer");
i->negate();
}
}
}
}
if(ptr_operand.has_value())
{
get_value_set_rec(
*ptr_operand,
pointer_expr_set,
includes_nondet_pointer,
"",
ptr_operand->type(),
ns);
}
else
{
// we get the points-to for all operands, even integers
for(const auto &op : expr.operands())
{
get_value_set_rec(
op, pointer_expr_set, includes_nondet_pointer, "", op.type(), ns);
}
}
for(object_map_dt::const_iterator
it=pointer_expr_set.read().begin();
it!=pointer_expr_set.read().end();
it++)
{
offsett offset = it->second;
// adjust by offset
if(offset && i.has_value())
*offset += *i;
else
offset.reset();
insert(dest, it->first, offset);
}
}
else if(expr.id()==ID_mult)
{
// this is to do stuff like
// (int*)((sel*(ulong)&a)+((sel^0x1)*(ulong)&b))
if(expr.operands().size()<2)
throw expr.id_string()+" expected to have at least two operands";
object_mapt pointer_expr_set;
// we get the points-to for all operands, even integers
for(const auto &op : expr.operands())
{
get_value_set_rec(
op, pointer_expr_set, includes_nondet_pointer, "", op.type(), ns);
}
for(object_map_dt::const_iterator
it=pointer_expr_set.read().begin();
it!=pointer_expr_set.read().end();
it++)
{
offsett offset = it->second;
// kill any offset
offset.reset();
insert(dest, it->first, offset);
}
}
else if(expr.id() == ID_lshr || expr.id() == ID_ashr || expr.id() == ID_shl)
{
const binary_exprt &binary_expr = to_binary_expr(expr);
object_mapt pointer_expr_set;
get_value_set_rec(
binary_expr.op0(),
pointer_expr_set,
includes_nondet_pointer,
"",
binary_expr.op0().type(),
ns);
for(const auto &object_map_entry : pointer_expr_set.read())
{
offsett offset = object_map_entry.second;
// kill any offset
offset.reset();
insert(dest, object_map_entry.first, offset);
}
}
else if(expr.id()==ID_side_effect)
{
const irep_idt &statement = to_side_effect_expr(expr).get_statement();
if(statement==ID_function_call)
{
// these should be gone
throw "unexpected function_call sideeffect";
}
else if(statement==ID_allocate)
{
PRECONDITION(suffix.empty());
const typet &dynamic_type=
static_cast<const typet &>(expr.find(ID_C_cxx_alloc_type));
dynamic_object_exprt dynamic_object(dynamic_type);
dynamic_object.set_instance(location_number);
dynamic_object.valid()=true_exprt();
insert(dest, dynamic_object, mp_integer{0});
}
else if(statement==ID_cpp_new ||
statement==ID_cpp_new_array)
{
PRECONDITION(suffix.empty());
PRECONDITION(expr.type().id() == ID_pointer);
dynamic_object_exprt dynamic_object(
to_pointer_type(expr.type()).base_type());
dynamic_object.set_instance(location_number);
dynamic_object.valid()=true_exprt();
insert(dest, dynamic_object, mp_integer{0});
}
else
insert(dest, exprt(ID_unknown, original_type));
}
else if(expr.id()==ID_struct)
{
const auto &struct_components =
expr.type().id() == ID_struct_tag
? ns.follow_tag(to_struct_tag_type(expr.type())).components()
: to_struct_type(expr.type()).components();
INVARIANT(
struct_components.size() == expr.operands().size(),
"struct expression should have an operand per component");
auto component_iter = struct_components.begin();
bool found_component = false;
// a struct constructor, which may contain addresses
for(const auto &op : expr.operands())
{
const std::string &component_name =
id2string(component_iter->get_name());
if(suffix_starts_with_field(suffix, component_name))
{
std::string remaining_suffix =
strip_first_field_from_suffix(suffix, component_name);
get_value_set_rec(
op,
dest,
includes_nondet_pointer,
remaining_suffix,
original_type,
ns);
found_component = true;
}
++component_iter;
}
if(!found_component)
{
// Struct field doesn't appear as expected -- this has probably been
// cast from an incompatible type. Conservatively assume all fields may
// be of interest.
for(const auto &op : expr.operands())
{
get_value_set_rec(
op, dest, includes_nondet_pointer, suffix, original_type, ns);
}
}
}
else if(expr.id() == ID_union)
{
get_value_set_rec(
to_union_expr(expr).op(),
dest,
includes_nondet_pointer,
suffix,
original_type,
ns);
}
else if(expr.id()==ID_with)
{
const with_exprt &with_expr = to_with_expr(expr);
// If the suffix is empty we're looking for the whole struct:
// default to combining both options as below.
if(
(expr.type().id() == ID_struct_tag || expr.type().id() == ID_struct) &&
!suffix.empty())
{
irep_idt component_name = with_expr.where().get(ID_component_name);
if(suffix_starts_with_field(suffix, id2string(component_name)))
{
// Looking for the member overwritten by this WITH expression
std::string remaining_suffix =
strip_first_field_from_suffix(suffix, id2string(component_name));
get_value_set_rec(
with_expr.new_value(),
dest,
includes_nondet_pointer,
remaining_suffix,
original_type,
ns);
}
else if(
(expr.type().id() == ID_struct &&
to_struct_type(expr.type()).has_component(component_name)) ||
(expr.type().id() == ID_struct_tag &&
ns.follow_tag(to_struct_tag_type(expr.type()))
.has_component(component_name)))
{
// Looking for a non-overwritten member, look through this expression
get_value_set_rec(
with_expr.old(),
dest,
includes_nondet_pointer,
suffix,
original_type,
ns);
}
else
{
// Member we're looking for is not defined in this struct -- this
// must be a reinterpret cast of some sort. Default to conservatively
// assuming either operand might be involved.
get_value_set_rec(
with_expr.old(),
dest,
includes_nondet_pointer,
suffix,