@@ -49,6 +49,7 @@ static symbolt &new_tmp_symbol(
49
49
50
50
class java_object_factoryt
51
51
{
52
+ protected:
52
53
std::vector<const symbolt *> &symbols_created;
53
54
const source_locationt &loc;
54
55
std::unordered_set<irep_idt, irep_id_hash> recursion_set;
@@ -65,13 +66,14 @@ class java_object_factoryt
65
66
code_assignt get_null_assignment (
66
67
const exprt &expr,
67
68
const pointer_typet &ptr_type);
68
-
69
- void gen_pointer_target_init (
69
+ protected:
70
+ virtual void gen_pointer_target_init (
70
71
code_blockt &assignments,
71
72
const exprt &expr,
72
73
const typet &target_type,
73
74
bool create_dynamic_objects,
74
- update_in_placet);
75
+ update_in_placet update_in_place);
76
+ protected:
75
77
76
78
void allocate_nondet_length_array (
77
79
code_blockt &assignments,
@@ -94,6 +96,8 @@ class java_object_factoryt
94
96
ns (_symbol_table)
95
97
{}
96
98
99
+ virtual ~java_object_factoryt ()=default ;
100
+
97
101
exprt allocate_object (
98
102
code_blockt &assignments,
99
103
const exprt &,
@@ -105,7 +109,7 @@ class java_object_factoryt
105
109
const exprt &expr,
106
110
update_in_placet);
107
111
108
- void gen_nondet_init (
112
+ virtual void gen_nondet_init (
109
113
code_blockt &assignments,
110
114
const exprt &expr,
111
115
bool is_sub,
@@ -136,6 +140,36 @@ class java_object_factoryt
136
140
const update_in_placet &update_in_place);
137
141
};
138
142
143
+
144
+ class java_object_factory_with_randomt :java_object_factoryt
145
+ {
146
+ public:
147
+ java_object_factory_with_randomt (
148
+ std::vector<const symbolt *> &_symbols_created,
149
+ const source_locationt &loc,
150
+ bool _assume_non_null,
151
+ size_t _max_nondet_array_length,
152
+ symbol_tablet &_symbol_table):
153
+ java_object_factoryt (
154
+ _symbols_created,
155
+ loc,
156
+ _assume_non_null,
157
+ _max_nondet_array_length,
158
+ _symbol_table)
159
+ {}
160
+
161
+ void gen_nondet_init (
162
+ code_blockt &assignments,
163
+ const exprt &expr,
164
+ bool is_sub,
165
+ irep_idt class_identifier,
166
+ bool skip_classid,
167
+ bool create_dynamic_objects,
168
+ bool override ,
169
+ const typet &override_type,
170
+ update_in_placet update_in_place) override ;
171
+ };
172
+
139
173
// / Generates code for allocating a dynamic object. This is used in
140
174
// / allocate_object and for allocating strings in the library preprocessing.
141
175
// / \param target_expr: expression to which the necessary memory will be
@@ -660,7 +694,7 @@ void java_object_factoryt::gen_nondet_init(
660
694
}
661
695
}
662
696
663
- // / Allocates a fresh array. Single-use herem at the moment, but useful to keep
697
+ // / Allocates a fresh array. Single-use at the moment, but useful to keep
664
698
// / as a separate function for downstream branches.
665
699
// / \par parameters: `lhs`, symbol to assign the new array structure
666
700
// / `max_length_expr`, maximum length of the new array (minimum is fixed at zero
@@ -857,23 +891,12 @@ exprt object_factory(
857
891
irep_idt identifier=id2string (goto_functionst::entry_point ())+
858
892
" ::" +id2string (base_name);
859
893
860
- typet real_type=type;
861
- if (type.id ()==ID_pointer && type.subtype ().id ()==ID_symbol)
862
- {
863
- const pointer_typet &pointer_type=to_pointer_type (type);
864
- const namespacet ns (symbol_table);
865
- get_concrete_class_at_randomt get_concrete_class_at_random (ns);
866
- const typet &resolved_type=
867
- get_concrete_class_at_random (pointer_type);
868
- real_type.subtype ()=resolved_type;
869
- }
870
-
871
894
auxiliary_symbolt main_symbol;
872
895
main_symbol.mode =ID_java;
873
896
main_symbol.is_static_lifetime =false ;
874
897
main_symbol.name =identifier;
875
898
main_symbol.base_name =base_name;
876
- main_symbol.type =real_type ;
899
+ main_symbol.type =type ;
877
900
main_symbol.location =loc;
878
901
879
902
exprt object=main_symbol.symbol_expr ();
@@ -884,11 +907,7 @@ exprt object_factory(
884
907
885
908
std::vector<const symbolt *> symbols_created;
886
909
symbols_created.push_back (main_symbol_ptr);
887
-
888
- symbolt &aux_symbol=new_tmp_symbol (symbol_table, loc, real_type);
889
- aux_symbol.is_static_lifetime =true ;
890
-
891
- java_object_factoryt state (
910
+ java_object_factory_with_randomt state (
892
911
symbols_created,
893
912
loc,
894
913
!allow_null,
@@ -951,7 +970,7 @@ void gen_nondet_init(
951
970
{
952
971
std::vector<const symbolt *> symbols_created;
953
972
954
- java_object_factoryt state (
973
+ java_object_factory_with_randomt state (
955
974
symbols_created,
956
975
loc,
957
976
assume_non_null,
@@ -980,3 +999,68 @@ void gen_nondet_init(
980
999
981
1000
init_code.append (assignments);
982
1001
}
1002
+
1003
+
1004
+ void java_object_factory_with_randomt::gen_nondet_init (
1005
+ code_blockt &assignments,
1006
+ const exprt &expr,
1007
+ bool is_sub,
1008
+ irep_idt class_identifier,
1009
+ bool skip_classid,
1010
+ bool create_dynamic_objects,
1011
+ bool override ,
1012
+ const typet &override_type,
1013
+ update_in_placet update_in_place)
1014
+ {
1015
+ const typet &type=
1016
+ override ? ns.follow (override_type) : ns.follow (expr.type ());
1017
+ typet real_type=type;
1018
+
1019
+ if (type.id ()==ID_pointer && type.subtype ().id ()==ID_symbol)
1020
+ {
1021
+ const pointer_typet &pointer_type=to_pointer_type (type);
1022
+ const namespacet ns (symbol_table);
1023
+ get_concrete_class_at_randomt get_concrete_class_at_random (ns);
1024
+ const typet &resolved_type=
1025
+ get_concrete_class_at_random (pointer_type);
1026
+
1027
+ if (resolved_type!=real_type.subtype ())
1028
+ {
1029
+ // Generate GOTO code to initalize the selected concrete type
1030
+ // A { ... } tmp_object;
1031
+ // A.x = NONDET ...
1032
+ // // non-det init of all the fields of A
1033
+ // A * p = &tmp_object
1034
+ // expr = (I *)p
1035
+
1036
+ symbolt new_symbol=new_tmp_symbol (symbol_table, loc, pointer_typet (resolved_type));
1037
+
1038
+ // Generate a new object into this new symbol
1039
+ gen_nondet_init (
1040
+ assignments,
1041
+ new_symbol.symbol_expr (),
1042
+ is_sub,
1043
+ class_identifier,
1044
+ skip_classid,
1045
+ create_dynamic_objects,
1046
+ override ,
1047
+ override_type,
1048
+ update_in_placet::NO_UPDATE_IN_PLACE);
1049
+
1050
+ assignments.add (
1051
+ code_assignt (expr, typecast_exprt (new_symbol.symbol_expr (), type)));
1052
+
1053
+ return ;
1054
+ }
1055
+ }
1056
+ java_object_factoryt::gen_nondet_init (
1057
+ assignments,
1058
+ expr,
1059
+ is_sub,
1060
+ class_identifier,
1061
+ skip_classid,
1062
+ create_dynamic_objects,
1063
+ override ,
1064
+ override_type,
1065
+ update_in_place);
1066
+ }
0 commit comments