Skip to content

Commit 7ff9a8a

Browse files
author
svorenova
committed
Nondet-initialize variables in clinit_wrapper constructor
If nondet-static option is used
1 parent 4119b14 commit 7ff9a8a

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
@@ -179,7 +179,8 @@ gen_clinit_eqexpr(const exprt &expr, const clinit_statest state)
179179

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

@@ -373,7 +413,8 @@ static void create_clinit_wrapper_symbols(
373413
/// // ...
374414
/// java::In::clinit_wrapper();
375415
///
376-
/// java::C::<clinit>();
416+
/// java::C::<clinit>(); // or nondet-initialization of all static
417+
/// // variables of C if nondet-static is true
377418
///
378419
/// // Setting this variable to INIT_COMPLETE will let other threads "cross"
379420
/// // beyond the assume() statement above in this function.
@@ -549,7 +590,8 @@ codet get_thread_safe_clinit_wrapper_body(
549590
// // ...
550591
// java::In::clinit_wrapper();
551592
//
552-
// java::C::<clinit>();
593+
// java::C::<clinit>(); // or nondet-initialization of all static
594+
// // variables of C if nondet-static is true
553595
//
554596
{
555597
code_blockt init_body;
@@ -615,7 +657,8 @@ codet get_clinit_wrapper_body(
615657
// // ...
616658
// java::In::clinit_wrapper();
617659
//
618-
// java::C::<clinit>();
660+
// java::C::<clinit>(); // or nondet-initialization of all static
661+
// // variables of C if nondet-static is true
619662
// }
620663
// }
621664
const symbolt &wrapper_method_symbol = symbol_table.lookup_ref(function_id);

0 commit comments

Comments
 (0)