Skip to content

Commit afa7d52

Browse files
committed
Generate INITIALIZE_FUNCTION from convert_single_method
This allows for an external lazy methods driver to control when the `INITIALIZE_FUNCTION` is generated.
1 parent aca06bd commit afa7d52

6 files changed

+54
-33
lines changed

jbmc/src/java_bytecode/java_bytecode_internal_additions.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,26 @@ Author: Daniel Kroening, [email protected]
1212
#include "java_types.h"
1313
#include "remove_exceptions.h"
1414

15+
#include <linking/static_lifetime_init.h>
1516
#include <util/std_types.h>
1617
#include <util/cprover_prefix.h>
1718
#include <util/c_types.h>
1819

20+
void create_java_initialize(symbol_table_baset &symbol_table)
21+
{
22+
// If __CPROVER_initialize already exists, replace it. It may already exist
23+
// if a GOTO binary provided it. This behaviour mirrors the ANSI-C frontend.
24+
symbol_table.remove(INITIALIZE_FUNCTION);
25+
26+
symbolt initialize;
27+
initialize.name=INITIALIZE_FUNCTION;
28+
initialize.base_name=INITIALIZE_FUNCTION;
29+
initialize.mode=ID_java;
30+
31+
initialize.type = java_method_typet({}, java_void_type());
32+
symbol_table.add(initialize);
33+
}
34+
1935
void java_internal_additions(symbol_table_baset &dest)
2036
{
2137
// add __CPROVER_rounding_mode
@@ -58,4 +74,6 @@ void java_internal_additions(symbol_table_baset &dest)
5874
symbol.is_static_lifetime = true;
5975
dest.add(symbol);
6076
}
77+
78+
create_java_initialize(dest);
6179
}

jbmc/src/java_bytecode/java_bytecode_internal_additions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include <util/symbol_table_base.h>
1414

15+
/// Adds `__cprover_initialize` to the \p symbol_table but does not generate
16+
/// code for it yet.
17+
void create_java_initialize(symbol_table_baset &symbol_table);
18+
1519
void java_internal_additions(symbol_table_baset &dest);
1620

1721
#endif // CPROVER_JAVA_BYTECODE_JAVA_BYTECODE_INTERNAL_ADDITIONS_H

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 20 additions & 1 deletion
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>
@@ -942,6 +944,8 @@ bool java_bytecode_languaget::typecheck(
942944
convert_single_method(
943945
method_sig.first, journalling_symbol_table, class_to_declared_symbols);
944946
}
947+
convert_single_method(
948+
INITIALIZE_FUNCTION, journalling_symbol_table, class_to_declared_symbols);
945949
// Now convert all newly added string methods
946950
for(const auto &fn_name : journalling_symbol_table.get_inserted())
947951
{
@@ -1095,6 +1099,7 @@ void java_bytecode_languaget::methods_provided(
10951099
// Add all synthetic methods to map
10961100
for(const auto &kv : synthetic_methods)
10971101
methods.insert(kv.first);
1102+
methods.insert(INITIALIZE_FUNCTION);
10981103
}
10991104

11001105
/// \brief Promote a lazy-converted method (one whose type is known but whose
@@ -1120,7 +1125,7 @@ void java_bytecode_languaget::convert_lazy_method(
11201125
convert_single_method(function_id, symbol_table, class_to_declared_symbols);
11211126

11221127
// Instrument runtime exceptions (unless symbol is a stub)
1123-
if(symbol.value.is_not_nil())
1128+
if(symbol.value.is_not_nil() && function_id != INITIALIZE_FUNCTION)
11241129
{
11251130
java_bytecode_instrument_symbol(
11261131
symbol_table,
@@ -1201,6 +1206,20 @@ bool java_bytecode_languaget::convert_single_method(
12011206
// Nothing to do if body is already loaded
12021207
if(symbol.value.is_not_nil())
12031208
return false;
1209+
1210+
if(function_id == INITIALIZE_FUNCTION)
1211+
{
1212+
java_static_lifetime_init(
1213+
symbol_table,
1214+
symbol.location,
1215+
language_options->assume_inputs_non_null,
1216+
object_factory_parameters,
1217+
*pointer_type_selector,
1218+
language_options->string_refinement_enabled,
1219+
get_message_handler());
1220+
return false;
1221+
}
1222+
12041223
INVARIANT(declaring_class(symbol), "Method must have a declaring class.");
12051224

12061225
bool ret = convert_single_method_code(

jbmc/src/java_bytecode/java_entry_point.cpp

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,6 @@ static codet record_exception(
4141
const symbolt &function,
4242
const symbol_table_baset &symbol_table);
4343

44-
void create_java_initialize(symbol_table_baset &symbol_table)
45-
{
46-
// If __CPROVER_initialize already exists, replace it. It may already exist
47-
// if a GOTO binary provided it. This behaviour mirrors the ANSI-C frontend.
48-
symbol_table.remove(INITIALIZE_FUNCTION);
49-
50-
symbolt initialize;
51-
initialize.name=INITIALIZE_FUNCTION;
52-
initialize.base_name=INITIALIZE_FUNCTION;
53-
initialize.mode=ID_java;
54-
55-
initialize.type = java_method_typet({}, java_void_type());
56-
57-
symbol_table.add(initialize);
58-
}
59-
6044
static bool should_init_symbol(const symbolt &sym)
6145
{
6246
if(sym.type.id()!=ID_code &&
@@ -104,7 +88,7 @@ static constant_exprt constant_bool(bool val)
10488
return from_integer(val ? 1 : 0, java_boolean_type());
10589
}
10690

107-
static void java_static_lifetime_init(
91+
void java_static_lifetime_init(
10892
symbol_table_baset &symbol_table,
10993
const source_locationt &source_location,
11094
bool assume_init_pointers_not_null,
@@ -115,6 +99,7 @@ static void java_static_lifetime_init(
11599
{
116100
symbolt &initialize_symbol =
117101
symbol_table.get_writeable_ref(INITIALIZE_FUNCTION);
102+
PRECONDITION(initialize_symbol.value.is_nil());
118103
code_blockt code_block;
119104

120105
const symbol_exprt rounding_mode =
@@ -590,17 +575,6 @@ bool java_entry_point(
590575

591576
assert(symbol.type.id()==ID_code);
592577

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-
604578
return generate_java_start_function(
605579
symbol,
606580
symbol_table,

jbmc/src/java_bytecode/java_entry_point.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,14 @@ std::pair<code_blockt, std::vector<exprt>> java_build_arguments(
186186
const select_pointer_typet &pointer_type_selector,
187187
message_handlert &message_handler);
188188

189-
/// Adds `__cprover_initialize` to the \p symbol_table but does not generate
190-
/// code for it yet.
191-
void create_java_initialize(symbol_table_baset &symbol_table);
189+
/// Adds the body to __CPROVER_initialize
190+
void java_static_lifetime_init(
191+
symbol_table_baset &symbol_table,
192+
const source_locationt &source_location,
193+
bool assume_init_pointers_not_null,
194+
java_object_factory_parameterst object_factory_parameters,
195+
const select_pointer_typet &pointer_type_selector,
196+
bool string_refinement_enabled,
197+
message_handlert &message_handler);
192198

193199
#endif // CPROVER_JAVA_BYTECODE_JAVA_ENTRY_POINT_H

jbmc/unit/java_bytecode/java_string_literals.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Author: Diffblue Limited
66
77
\*******************************************************************/
88

9-
#include <java_bytecode/java_entry_point.h>
9+
#include <java_bytecode/java_bytecode_internal_additions.h>
1010
#include <java_bytecode/java_string_literals.h>
1111
#include <java_bytecode/java_types.h>
1212
#include <java_bytecode/java_utils.h>

0 commit comments

Comments
 (0)