Skip to content

Commit 897b197

Browse files
committed
JBMC: Zero-initialized 'monitorCount' component (fixes diffblue#2307)
The 'monitorCount' field is a counter in the 'java.lang.Object' model (part of the java core models library). This field is used to implement a critical section and is thus necessary to support concurrency. This commit makes sure that this field (if present) is always zero initialized as it is not meant to be non-deterministic. This field is present only if the java core models library is loaded.
1 parent e54bba2 commit 897b197

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,18 @@ void java_object_factoryt::gen_nondet_struct_init(
10771077
{
10781078
continue;
10791079
}
1080+
else if(name == "monitorCount")
1081+
{
1082+
// Zero-initialize 'monitorCount' field as it is not meant to be nondet.
1083+
// This field is only present when the java core models are embedded. It
1084+
// is used to implement a critical section, which is necessary to support
1085+
// concurrency.
1086+
if(update_in_place == update_in_placet::MUST_UPDATE_IN_PLACE)
1087+
continue;
1088+
code_assignt code(me, from_integer(0, me.type()));
1089+
code.add_source_location() = loc;
1090+
assignments.copy_to_operands(code);
1091+
}
10801092
else
10811093
{
10821094
INVARIANT(!name.empty(), "Each component of a struct must have a name");

jbmc/src/java_bytecode/java_root_class.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,15 @@ void java_root_class_init(
7575
const std::size_t lock_nb=root_type.component_number("@lock");
7676
const typet &lock_type=root_type.components()[lock_nb].type();
7777
jlo.operands()[lock_nb]=from_integer(lock, lock_type);
78+
79+
// Check if the 'monitorCount' component exists and initialize it.
80+
// This field is only present when the java core models are embedded. It is
81+
// used to implement a critical section, which is necessary to support
82+
// concurrency.
83+
if(root_type.has_component("monitorCount"))
84+
{
85+
const std::size_t count_nb = root_type.component_number("monitorCount");
86+
const typet &count_type = root_type.components()[count_nb].type();
87+
jlo.operands()[count_nb] = from_integer(0, count_type);
88+
}
7889
}

0 commit comments

Comments
 (0)