Skip to content

Commit d5513db

Browse files
Owen Jonesthk123
Owen Jones
authored and
thk123
committed
Make class derived from object factory
1 parent a904f08 commit d5513db

File tree

1 file changed

+107
-23
lines changed

1 file changed

+107
-23
lines changed

src/java_bytecode/java_object_factory.cpp

+107-23
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static symbolt &new_tmp_symbol(
4949

5050
class java_object_factoryt
5151
{
52+
protected:
5253
std::vector<const symbolt *> &symbols_created;
5354
const source_locationt &loc;
5455
std::unordered_set<irep_idt, irep_id_hash> recursion_set;
@@ -65,13 +66,14 @@ class java_object_factoryt
6566
code_assignt get_null_assignment(
6667
const exprt &expr,
6768
const pointer_typet &ptr_type);
68-
69-
void gen_pointer_target_init(
69+
protected:
70+
virtual void gen_pointer_target_init(
7071
code_blockt &assignments,
7172
const exprt &expr,
7273
const typet &target_type,
7374
bool create_dynamic_objects,
74-
update_in_placet);
75+
update_in_placet update_in_place);
76+
protected:
7577

7678
void allocate_nondet_length_array(
7779
code_blockt &assignments,
@@ -94,6 +96,8 @@ class java_object_factoryt
9496
ns(_symbol_table)
9597
{}
9698

99+
virtual ~java_object_factoryt()=default;
100+
97101
exprt allocate_object(
98102
code_blockt &assignments,
99103
const exprt &,
@@ -105,7 +109,7 @@ class java_object_factoryt
105109
const exprt &expr,
106110
update_in_placet);
107111

108-
void gen_nondet_init(
112+
virtual void gen_nondet_init(
109113
code_blockt &assignments,
110114
const exprt &expr,
111115
bool is_sub,
@@ -136,6 +140,36 @@ class java_object_factoryt
136140
const update_in_placet &update_in_place);
137141
};
138142

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+
139173
/// Generates code for allocating a dynamic object. This is used in
140174
/// allocate_object and for allocating strings in the library preprocessing.
141175
/// \param target_expr: expression to which the necessary memory will be
@@ -660,7 +694,7 @@ void java_object_factoryt::gen_nondet_init(
660694
}
661695
}
662696

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
664698
/// as a separate function for downstream branches.
665699
/// \par parameters: `lhs`, symbol to assign the new array structure
666700
/// `max_length_expr`, maximum length of the new array (minimum is fixed at zero
@@ -857,23 +891,12 @@ exprt object_factory(
857891
irep_idt identifier=id2string(goto_functionst::entry_point())+
858892
"::"+id2string(base_name);
859893

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-
871894
auxiliary_symbolt main_symbol;
872895
main_symbol.mode=ID_java;
873896
main_symbol.is_static_lifetime=false;
874897
main_symbol.name=identifier;
875898
main_symbol.base_name=base_name;
876-
main_symbol.type=real_type;
899+
main_symbol.type=type;
877900
main_symbol.location=loc;
878901

879902
exprt object=main_symbol.symbol_expr();
@@ -884,11 +907,7 @@ exprt object_factory(
884907

885908
std::vector<const symbolt *> symbols_created;
886909
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(
892911
symbols_created,
893912
loc,
894913
!allow_null,
@@ -951,7 +970,7 @@ void gen_nondet_init(
951970
{
952971
std::vector<const symbolt *> symbols_created;
953972

954-
java_object_factoryt state(
973+
java_object_factory_with_randomt state(
955974
symbols_created,
956975
loc,
957976
assume_non_null,
@@ -980,3 +999,68 @@ void gen_nondet_init(
980999

9811000
init_code.append(assignments);
9821001
}
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

Comments
 (0)