Skip to content

Commit e1e7a74

Browse files
Merge pull request #5156 from thomasspriggs/tas/lazy_initialize
Support for an external lazy methods driver to control when the `INITIALIZE_FUNCTION` is generated
2 parents db4c76b + 082b368 commit e1e7a74

File tree

5 files changed

+105
-15
lines changed

5 files changed

+105
-15
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <string>
1212

13+
#include <linking/static_lifetime_init.h>
14+
1315
#include <util/cmdline.h>
1416
#include <util/config.h>
1517
#include <util/expr_iterator.h>
@@ -757,6 +759,7 @@ bool java_bytecode_languaget::typecheck(
757759
"the Java front-end should only be used with an empty symbol table");
758760

759761
java_internal_additions(symbol_table);
762+
create_java_initialize(symbol_table);
760763

761764
if(language_options->string_refinement_enabled)
762765
string_preprocess.initialize_conversion_table();
@@ -942,6 +945,8 @@ bool java_bytecode_languaget::typecheck(
942945
convert_single_method(
943946
method_sig.first, journalling_symbol_table, class_to_declared_symbols);
944947
}
948+
convert_single_method(
949+
INITIALIZE_FUNCTION, journalling_symbol_table, class_to_declared_symbols);
945950
// Now convert all newly added string methods
946951
for(const auto &fn_name : journalling_symbol_table.get_inserted())
947952
{
@@ -1095,6 +1100,7 @@ void java_bytecode_languaget::methods_provided(
10951100
// Add all synthetic methods to map
10961101
for(const auto &kv : synthetic_methods)
10971102
methods.insert(kv.first);
1103+
methods.insert(INITIALIZE_FUNCTION);
10981104
}
10991105

11001106
/// \brief Promote a lazy-converted method (one whose type is known but whose
@@ -1119,8 +1125,9 @@ void java_bytecode_languaget::convert_lazy_method(
11191125
lazy_class_to_declared_symbols_mapt class_to_declared_symbols;
11201126
convert_single_method(function_id, symbol_table, class_to_declared_symbols);
11211127

1122-
// Instrument runtime exceptions (unless symbol is a stub)
1123-
if(symbol.value.is_not_nil())
1128+
// Instrument runtime exceptions (unless symbol is a stub or is the
1129+
// INITIALISE_FUNCTION).
1130+
if(symbol.value.is_not_nil() && function_id != INITIALIZE_FUNCTION)
11241131
{
11251132
java_bytecode_instrument_symbol(
11261133
symbol_table,
@@ -1201,6 +1208,20 @@ bool java_bytecode_languaget::convert_single_method(
12011208
// Nothing to do if body is already loaded
12021209
if(symbol.value.is_not_nil())
12031210
return false;
1211+
1212+
if(function_id == INITIALIZE_FUNCTION)
1213+
{
1214+
java_static_lifetime_init(
1215+
symbol_table,
1216+
symbol.location,
1217+
language_options->assume_inputs_non_null,
1218+
object_factory_parameters,
1219+
*pointer_type_selector,
1220+
language_options->string_refinement_enabled,
1221+
get_message_handler());
1222+
return false;
1223+
}
1224+
12041225
INVARIANT(declaring_class(symbol), "Method must have a declaring class.");
12051226

12061227
bool ret = convert_single_method_code(

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ void create_java_initialize(symbol_table_baset &symbol_table)
5353
initialize.mode=ID_java;
5454

5555
initialize.type = java_method_typet({}, java_void_type());
56-
5756
symbol_table.add(initialize);
5857
}
5958

@@ -104,7 +103,7 @@ static constant_exprt constant_bool(bool val)
104103
return from_integer(val ? 1 : 0, java_boolean_type());
105104
}
106105

107-
static void java_static_lifetime_init(
106+
void java_static_lifetime_init(
108107
symbol_table_baset &symbol_table,
109108
const source_locationt &source_location,
110109
bool assume_init_pointers_not_null,
@@ -115,6 +114,7 @@ static void java_static_lifetime_init(
115114
{
116115
symbolt &initialize_symbol =
117116
symbol_table.get_writeable_ref(INITIALIZE_FUNCTION);
117+
PRECONDITION(initialize_symbol.value.is_nil());
118118
code_blockt code_block;
119119

120120
const symbol_exprt rounding_mode =
@@ -590,17 +590,6 @@ bool java_entry_point(
590590

591591
assert(symbol.type.id()==ID_code);
592592

593-
create_java_initialize(symbol_table);
594-
595-
java_static_lifetime_init(
596-
symbol_table,
597-
symbol.location,
598-
assume_init_pointers_not_null,
599-
object_factory_parameters,
600-
pointer_type_selector,
601-
string_refinement_enabled,
602-
message_handler);
603-
604593
return generate_java_start_function(
605594
symbol,
606595
symbol_table,

jbmc/src/java_bytecode/java_entry_point.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,14 @@ std::pair<code_blockt, std::vector<exprt>> java_build_arguments(
190190
/// code for it yet.
191191
void create_java_initialize(symbol_table_baset &symbol_table);
192192

193+
/// Adds the body to __CPROVER_initialize
194+
void java_static_lifetime_init(
195+
symbol_table_baset &symbol_table,
196+
const source_locationt &source_location,
197+
bool assume_init_pointers_not_null,
198+
java_object_factory_parameterst object_factory_parameters,
199+
const select_pointer_typet &pointer_type_selector,
200+
bool string_refinement_enabled,
201+
message_handlert &message_handler);
202+
193203
#endif // CPROVER_JAVA_BYTECODE_JAVA_ENTRY_POINT_H

jbmc/unit/java_bytecode/java_bytecode_language/language.cpp

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Author: Diffblue Limited.
1212

1313
#include <java-testing-utils/load_java_class.h>
1414
#include <java-testing-utils/require_type.h>
15+
#include <java_bytecode/java_bytecode_language.h>
16+
#include <linking/static_lifetime_init.h>
17+
#include <util/options.h>
1518

1619
SCENARIO(
1720
"java_bytecode_language_opaque_field",
@@ -45,3 +48,68 @@ SCENARIO(
4548
}
4649
}
4750
}
51+
52+
static void use_external_driver(java_bytecode_languaget &language)
53+
{
54+
optionst options;
55+
options.set_option("symex-driven-lazy-loading", true);
56+
language.set_language_options(options);
57+
}
58+
59+
SCENARIO(
60+
"LAZY_METHODS_MODE_EXTERNAL_DRIVER based generation of cprover_initialise",
61+
"[core][java_bytecode_language]")
62+
{
63+
java_bytecode_languaget language;
64+
null_message_handlert null_message_handler;
65+
language.set_message_handler(null_message_handler);
66+
use_external_driver(language);
67+
symbol_tablet symbol_table;
68+
GIVEN("java_bytecode_languaget::typecheck is run.")
69+
{
70+
language.typecheck(symbol_table, "");
71+
THEN("The " INITIALIZE_FUNCTION " is in the symbol table without code.")
72+
{
73+
const symbolt *const initialise =
74+
symbol_table.lookup(INITIALIZE_FUNCTION);
75+
REQUIRE(initialise);
76+
REQUIRE(initialise->value.is_nil());
77+
}
78+
GIVEN(
79+
"java_bytecode_languaget::convert_lazy_method is used to "
80+
"generate " INITIALIZE_FUNCTION)
81+
{
82+
language.convert_lazy_method(INITIALIZE_FUNCTION, symbol_table);
83+
THEN("The " INITIALIZE_FUNCTION " is in the symbol table with code.")
84+
{
85+
const symbolt *const initialise =
86+
symbol_table.lookup(INITIALIZE_FUNCTION);
87+
REQUIRE(initialise);
88+
REQUIRE(can_cast_expr<codet>(initialise->value));
89+
}
90+
}
91+
}
92+
}
93+
94+
TEST_CASE(
95+
"Standard generation of cprover_initialise",
96+
"[core][java_bytecode_language]")
97+
{
98+
java_bytecode_languaget language;
99+
null_message_handlert null_message_handler;
100+
language.set_message_handler(null_message_handler);
101+
language.set_language_options(optionst{});
102+
symbol_tablet symbol_table;
103+
GIVEN("java_bytecode_languaget::typecheck is run.")
104+
{
105+
language.typecheck(symbol_table, "");
106+
THEN("The " INITIALIZE_FUNCTION
107+
" function is in the symbol table with code.")
108+
{
109+
const symbolt *const initialise =
110+
symbol_table.lookup(INITIALIZE_FUNCTION);
111+
REQUIRE(initialise);
112+
REQUIRE(can_cast_expr<codet>(initialise->value));
113+
}
114+
}
115+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
java_bytecode
12
java-testing-utils
3+
linking
24
testing-utils
35
util

0 commit comments

Comments
 (0)