Skip to content

Commit 9a1402b

Browse files
author
svorenova
committed
Nondet-initialize variables in clinit_wrapper constructor
If nondet-static option is used
1 parent 47863d0 commit 9a1402b

File tree

1 file changed

+51
-8
lines changed

1 file changed

+51
-8
lines changed

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ gen_clinit_eqexpr(const exprt &expr, const clinit_statest state)
180180

181181
/// Generates codet that iterates through the base types of the class specified
182182
/// by class_name, C, and recursively adds calls to their clinit wrapper.
183-
/// Finally a call to the clinint wrapper of C is made.
183+
/// Finally a call to the clinit of C is made or if nondet-static option was
184+
/// given then all static variables of the class are nondet-initialized instead.
184185
/// \param symbol_table: symbol table
185186
/// \param class_name: name of the class to generate clinit wrapper calls for
186187
/// \param [out] init_body: appended with calls to clinit wrapper
@@ -191,7 +192,7 @@ gen_clinit_eqexpr(const exprt &expr, const clinit_statest state)
191192
/// \param pointer_type_selector: used to choose concrete types for abstract-
192193
/// typed globals and fields (only needed if nondet-static is true)
193194
static void clinit_wrapper_do_recursive_calls(
194-
const symbol_tablet &symbol_table,
195+
symbol_table_baset &symbol_table,
195196
const irep_idt &class_name,
196197
code_blockt &init_body,
197198
const bool nondet_static,
@@ -215,9 +216,48 @@ static void clinit_wrapper_do_recursive_calls(
215216
auto find_sym_it = symbol_table.symbols.find(real_clinit_name);
216217
if(find_sym_it != symbol_table.symbols.end())
217218
{
218-
code_function_callt call_real_init;
219-
call_real_init.function() = find_sym_it->second.symbol_expr();
220-
init_body.move_to_operands(call_real_init);
219+
if(!nondet_static)
220+
{
221+
code_function_callt call_real_init;
222+
call_real_init.function() = find_sym_it->second.symbol_expr();
223+
init_body.move_to_operands(call_real_init);
224+
}
225+
else
226+
{
227+
// Add a standard nondet initialisation for each global declared on this
228+
// class. Note this is the same invocation used in
229+
// get_stub_initializer_body and java_static_lifetime_init.
230+
231+
object_factory_parameterst parameters = object_factory_parameters;
232+
parameters.function_id = clinit_wrapper_name(class_name);
233+
234+
for(const auto &symbol : symbol_table.symbols)
235+
{
236+
if(
237+
symbol.second.is_static_lifetime &&
238+
symbol.second.type.get(ID_C_class) == class_name)
239+
{
240+
const symbol_exprt new_global_symbol = symbol.second.symbol_expr();
241+
242+
parameters.max_nonnull_tree_depth =
243+
is_non_null_library_global(symbol.first)
244+
? std::max(
245+
size_t(1), object_factory_parameters.max_nonnull_tree_depth)
246+
: object_factory_parameters.max_nonnull_tree_depth;
247+
248+
gen_nondet_init(
249+
new_global_symbol,
250+
init_body,
251+
symbol_table,
252+
source_locationt(),
253+
false,
254+
allocation_typet::DYNAMIC,
255+
parameters,
256+
pointer_type_selector,
257+
update_in_placet::NO_UPDATE_IN_PLACE);
258+
}
259+
}
260+
}
221261
}
222262
}
223263

@@ -374,7 +414,8 @@ static void create_clinit_wrapper_symbols(
374414
/// // ...
375415
/// java::In::clinit_wrapper();
376416
///
377-
/// java::C::<clinit>();
417+
/// java::C::<clinit>(); // or nondet-initialization of all static
418+
/// // variables of C if nondet-static is true
378419
///
379420
/// // Setting this variable to INIT_COMPLETE will let other threads "cross"
380421
/// // beyond the assume() statement above in this function.
@@ -550,7 +591,8 @@ codet get_thread_safe_clinit_wrapper_body(
550591
// // ...
551592
// java::In::clinit_wrapper();
552593
//
553-
// java::C::<clinit>();
594+
// java::C::<clinit>(); // or nondet-initialization of all static
595+
// // variables of C if nondet-static is true
554596
//
555597
{
556598
code_blockt init_body;
@@ -616,7 +658,8 @@ codet get_clinit_wrapper_body(
616658
// // ...
617659
// java::In::clinit_wrapper();
618660
//
619-
// java::C::<clinit>();
661+
// java::C::<clinit>(); // or nondet-initialization of all static
662+
// // variables of C if nondet-static is true
620663
// }
621664
// }
622665
const symbolt &wrapper_method_symbol = symbol_table.lookup_ref(function_id);

0 commit comments

Comments
 (0)