Skip to content

Support for an external lazy methods driver to control when the INITIALIZE_FUNCTION is generated #5156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion jbmc/src/java_bytecode/java_bytecode_language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Author: Daniel Kroening, [email protected]

#include <string>

#include <linking/static_lifetime_init.h>

#include <util/cmdline.h>
#include <util/config.h>
#include <util/expr_iterator.h>
Expand Down Expand Up @@ -757,6 +759,7 @@ bool java_bytecode_languaget::typecheck(
"the Java front-end should only be used with an empty symbol table");

java_internal_additions(symbol_table);
create_java_initialize(symbol_table);

if(language_options->string_refinement_enabled)
string_preprocess.initialize_conversion_table();
Expand Down Expand Up @@ -942,6 +945,8 @@ bool java_bytecode_languaget::typecheck(
convert_single_method(
method_sig.first, journalling_symbol_table, class_to_declared_symbols);
}
convert_single_method(
INITIALIZE_FUNCTION, journalling_symbol_table, class_to_declared_symbols);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about old ci-lazy-methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is already indirectly tested via the regression tests. I chose to add a unit test of the new functionality in this PR, because it would otherwise be untested within this repository.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I could add a unit test of that...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

// Now convert all newly added string methods
for(const auto &fn_name : journalling_symbol_table.get_inserted())
{
Expand Down Expand Up @@ -1095,6 +1100,7 @@ void java_bytecode_languaget::methods_provided(
// Add all synthetic methods to map
for(const auto &kv : synthetic_methods)
methods.insert(kv.first);
methods.insert(INITIALIZE_FUNCTION);
}

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

// Instrument runtime exceptions (unless symbol is a stub)
if(symbol.value.is_not_nil())
if(symbol.value.is_not_nil() && function_id != INITIALIZE_FUNCTION)
{
java_bytecode_instrument_symbol(
symbol_table,
Expand Down Expand Up @@ -1201,6 +1207,20 @@ bool java_bytecode_languaget::convert_single_method(
// Nothing to do if body is already loaded
if(symbol.value.is_not_nil())
return false;

if(function_id == INITIALIZE_FUNCTION)
{
java_static_lifetime_init(
symbol_table,
symbol.location,
language_options->assume_inputs_non_null,
object_factory_parameters,
*pointer_type_selector,
language_options->string_refinement_enabled,
get_message_handler());
return false;
}

INVARIANT(declaring_class(symbol), "Method must have a declaring class.");

bool ret = convert_single_method_code(
Expand Down
15 changes: 2 additions & 13 deletions jbmc/src/java_bytecode/java_entry_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ void create_java_initialize(symbol_table_baset &symbol_table)
initialize.mode=ID_java;

initialize.type = java_method_typet({}, java_void_type());

symbol_table.add(initialize);
}

Expand Down Expand Up @@ -104,7 +103,7 @@ static constant_exprt constant_bool(bool val)
return from_integer(val ? 1 : 0, java_boolean_type());
}

static void java_static_lifetime_init(
void java_static_lifetime_init(
symbol_table_baset &symbol_table,
const source_locationt &source_location,
bool assume_init_pointers_not_null,
Expand All @@ -115,6 +114,7 @@ static void java_static_lifetime_init(
{
symbolt &initialize_symbol =
symbol_table.get_writeable_ref(INITIALIZE_FUNCTION);
PRECONDITION(initialize_symbol.value.is_nil());
code_blockt code_block;

const symbol_exprt rounding_mode =
Expand Down Expand Up @@ -590,17 +590,6 @@ bool java_entry_point(

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

create_java_initialize(symbol_table);

java_static_lifetime_init(
symbol_table,
symbol.location,
assume_init_pointers_not_null,
object_factory_parameters,
pointer_type_selector,
string_refinement_enabled,
message_handler);

return generate_java_start_function(
symbol,
symbol_table,
Expand Down
10 changes: 10 additions & 0 deletions jbmc/src/java_bytecode/java_entry_point.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,14 @@ std::pair<code_blockt, std::vector<exprt>> java_build_arguments(
/// code for it yet.
void create_java_initialize(symbol_table_baset &symbol_table);

/// Adds the body to __CPROVER_initialize
void java_static_lifetime_init(
symbol_table_baset &symbol_table,
const source_locationt &source_location,
bool assume_init_pointers_not_null,
java_object_factory_parameterst object_factory_parameters,
const select_pointer_typet &pointer_type_selector,
bool string_refinement_enabled,
message_handlert &message_handler);

#endif // CPROVER_JAVA_BYTECODE_JAVA_ENTRY_POINT_H
48 changes: 48 additions & 0 deletions jbmc/unit/java_bytecode/java_bytecode_language/language.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Author: Diffblue Limited.

#include <java-testing-utils/load_java_class.h>
#include <java-testing-utils/require_type.h>
#include <java_bytecode/java_bytecode_language.h>
#include <linking/static_lifetime_init.h>
#include <util/options.h>

SCENARIO(
"java_bytecode_language_opaque_field",
Expand Down Expand Up @@ -45,3 +48,48 @@ SCENARIO(
}
}
}

static void use_external_driver(java_bytecode_languaget &language)
{
optionst options;
options.set_option("symex-driven-lazy-loading", true);
language.set_language_options(options);
}

TEST_CASE(
"LAZY_METHODS_MODE_EXTERNAL_DRIVER based generation of cprover_initialise",
"[core][java_bytecode_language]")
{
java_bytecode_languaget language;
null_message_handlert null_message_handler;
language.set_message_handler(null_message_handler);
use_external_driver(language);
symbol_tablet symbol_table;
language.typecheck(symbol_table, "");
{
const symbolt *const initialise = symbol_table.lookup(INITIALIZE_FUNCTION);
REQUIRE(initialise);
REQUIRE(initialise->value.is_nil());
}
language.convert_lazy_method(INITIALIZE_FUNCTION, symbol_table);
{
const symbolt *const initialise = symbol_table.lookup(INITIALIZE_FUNCTION);
REQUIRE(initialise);
REQUIRE(can_cast_expr<codet>(initialise->value));
}
}

TEST_CASE(
"Standard generation of cprover_initialise",
"[core][java_bytecode_language]")
{
java_bytecode_languaget language;
null_message_handlert null_message_handler;
language.set_message_handler(null_message_handler);
language.set_language_options(optionst{});
symbol_tablet symbol_table;
language.typecheck(symbol_table, "");
const symbolt *const initialise = symbol_table.lookup(INITIALIZE_FUNCTION);
REQUIRE(initialise);
REQUIRE(can_cast_expr<codet>(initialise->value));
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
java_bytecode
java-testing-utils
linking
testing-utils
util