Skip to content

Commit da76500

Browse files
committed
JBMC: Fixed asymmetry between synchronized blocks and methods.
Previously, the conversation of synchronized methods only took place if the 'java-threading' flag is specified, while in the case of synchronized blocks the conversation is undertaken with and without the aforementioned flag. This commit fixes this asymmetry by replacing the instructions 'monitorenter' and 'moinitorexit' with 'code_skipt' if the 'java-threading' option is not specified (instead of instrumenting calls to 'java::java.lang.Object.monitorexit/enter'). The 'monitorenter' and monitiorexit instructions are also replaced with a 'code_skipt' if the Java-core-models library is not loaded. This prevents JBMC from outputting missing function-body warnings. Commit also modifies relevant regression tests.
1 parent 2111f7c commit da76500

File tree

14 files changed

+54
-14
lines changed

14 files changed

+54
-14
lines changed

jbmc/regression/jbmc-concurrency/synchronized-blocks-illegal-state/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
KNOWNBUG
22
Sync.class
3-
--lazy-methods --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar`
3+
--lazy-methods --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --java-threading
44
^VERIFICATION SUCCESSFUL$
55
^EXIT=0$
66
^SIGNAL=0$

jbmc/regression/jbmc-concurrency/synchronized-blocks-null-throw/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
KNOWNBUG
22
Sync.class
3-
--cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar`
3+
--cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --java-threading
44
^VERIFICATION SUCCESSFUL$
55
--
66
^warning: ignoring

jbmc/regression/jbmc-concurrency/synchronized-blocks-throw/test.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
KNOWNBUG
22
Sync.class
3-
--cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --lazy-methods
3+
--cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --lazy-methods --java-threading
44
^VERIFICATION SUCCESSFUL$
55
--
66
^warning: ignoring
Binary file not shown.

jbmc/regression/jbmc-concurrency/synchronized-blocks/A.java

+19
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,25 @@ public void me0()
1414
}
1515
}
1616

17+
// expected verification success
18+
public static void aStatic() throws java.io.IOException
19+
{
20+
Object _lock = new Object();
21+
try
22+
{
23+
synchronized (_lock)
24+
{
25+
if(true)
26+
throw new java.io.IOException();
27+
}
28+
}
29+
catch (java.io.IOException e)
30+
{
31+
return;
32+
}
33+
assert false; // unreachable
34+
}
35+
1736
// expected verification success
1837
// --
1938
// base-case, no synchronization
Binary file not shown.

jbmc/regression/jbmc-concurrency/synchronized-blocks/test1.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
A.class
3-
--function 'A.me0:()V' --lazy-methods --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --show-goto-functions
3+
--function 'A.me0:()V' --lazy-methods --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --show-goto-functions --java-threading
44
ATOMIC_BEGIN
55
ATOMIC_END
66
--

jbmc/regression/jbmc-concurrency/synchronized-blocks/test2.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
A.class
3-
--function 'A.me0:()V' --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar`
3+
--function 'A.me0:()V' --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --java-threading
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$

jbmc/regression/jbmc-concurrency/synchronized-blocks/test3.desc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
CORE
22
A.class
3-
--function 'A.me0:()V' --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --lazy-methods
3+
--function 'A.me0:()V' --cp `../../../../scripts/format_classpath.sh . ../../../src/java_bytecode/library/core-models.jar` --lazy-methods --java-threading
44
^EXIT=0$
55
^SIGNAL=0$
66
^VERIFICATION SUCCESSFUL$
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE
2+
A.class
3+
--function 'A.aStatic' --lazy-methods
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
--
9+
Tests that throwing an exception from a synchronization blocks does not cause reachability issues when the java-threading flag is not specified.
10+

jbmc/src/java_bytecode/java_bytecode_convert_method.cpp

+11-5
Original file line numberDiff line numberDiff line change
@@ -1026,8 +1026,6 @@ codet java_bytecode_convert_methodt::convert_instructions(
10261026
}
10271027

10281028
if(i_it->statement=="athrow" ||
1029-
i_it->statement=="monitorenter" ||
1030-
i_it->statement=="monitorexit" ||
10311029
i_it->statement=="putfield" ||
10321030
i_it->statement=="getfield" ||
10331031
i_it->statement=="checkcast" ||
@@ -1042,7 +1040,9 @@ codet java_bytecode_convert_methodt::convert_instructions(
10421040
i_it->statement=="invokestatic" ||
10431041
i_it->statement=="invokevirtual" ||
10441042
i_it->statement=="invokespecial" ||
1045-
i_it->statement=="invokeinterface")
1043+
i_it->statement=="invokeinterface" ||
1044+
(threading_support && (i_it->statement=="monitorenter" ||
1045+
i_it->statement=="monitorexit")))
10461046
{
10471047
const std::vector<unsigned int> handler =
10481048
try_catch_handler(i_it->address, method.exception_table);
@@ -1986,6 +1986,9 @@ codet java_bytecode_convert_methodt::convert_monitorenterexit(
19861986
"java::java.lang.Object.monitorenter:(Ljava/lang/Object;)V" :
19871987
"java::java.lang.Object.monitorexit:(Ljava/lang/Object;)V";
19881988

1989+
if(!threading_support || !symbol_table.has_symbol(descriptor))
1990+
return code_skipt();
1991+
19891992
// becomes a function call
19901993
code_typet type(
19911994
{code_typet::parametert(java_reference_type(void_typet()))},
@@ -3099,7 +3102,9 @@ void java_bytecode_convert_method(
30993102
bool throw_assertion_error,
31003103
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
31013104
java_string_library_preprocesst &string_preprocess,
3102-
const class_hierarchyt &class_hierarchy)
3105+
const class_hierarchyt &class_hierarchy,
3106+
bool threading_support)
3107+
31033108
{
31043109
if(std::regex_match(
31053110
id2string(class_symbol.name),
@@ -3118,7 +3123,8 @@ void java_bytecode_convert_method(
31183123
throw_assertion_error,
31193124
needed_lazy_methods,
31203125
string_preprocess,
3121-
class_hierarchy);
3126+
class_hierarchy,
3127+
threading_support);
31223128

31233129
java_bytecode_convert_method(class_symbol, method);
31243130
}

jbmc/src/java_bytecode/java_bytecode_convert_method.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ void java_bytecode_convert_method(
3636
bool throw_assertion_error,
3737
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
3838
java_string_library_preprocesst &string_preprocess,
39-
const class_hierarchyt &class_hierarchy);
39+
const class_hierarchyt &class_hierarchy,
40+
bool threading_support);
4041

4142
void java_bytecode_convert_method_lazy(
4243
const symbolt &class_symbol,

jbmc/src/java_bytecode/java_bytecode_convert_method_class.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ class java_bytecode_convert_methodt:public messaget
3939
bool throw_assertion_error,
4040
optionalt<ci_lazy_methods_neededt> needed_lazy_methods,
4141
java_string_library_preprocesst &_string_preprocess,
42-
const class_hierarchyt &class_hierarchy)
42+
const class_hierarchyt &class_hierarchy,
43+
bool threading_support)
4344
: messaget(_message_handler),
4445
symbol_table(symbol_table),
4546
max_array_length(_max_array_length),
4647
throw_assertion_error(throw_assertion_error),
48+
threading_support(threading_support),
4749
needed_lazy_methods(std::move(needed_lazy_methods)),
4850
string_preprocess(_string_preprocess),
4951
slots_for_parameters(0),
@@ -67,6 +69,7 @@ class java_bytecode_convert_methodt:public messaget
6769
symbol_table_baset &symbol_table;
6870
const size_t max_array_length;
6971
const bool throw_assertion_error;
72+
const bool threading_support;
7073
optionalt<ci_lazy_methods_neededt> needed_lazy_methods;
7174

7275
/// Fully qualified name of the method under translation.

jbmc/src/java_bytecode/java_bytecode_language.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,8 @@ bool java_bytecode_languaget::convert_single_method(
10401040
throw_assertion_error,
10411041
std::move(needed_lazy_methods),
10421042
string_preprocess,
1043-
class_hierarchy);
1043+
class_hierarchy,
1044+
threading_support);
10441045
return false;
10451046
}
10461047

0 commit comments

Comments
 (0)