120 const bool is_thread_local,
121 const bool is_static_lifetime)
123 symbolt new_symbol{name, type, ID_java};
124 new_symbol.pretty_name = name;
125 new_symbol.base_name = name;
126 new_symbol.type.set(ID_C_no_nondet_initialization,
true);
127 new_symbol.value = value;
128 new_symbol.is_lvalue =
true;
129 new_symbol.is_state_var =
true;
130 new_symbol.is_static_lifetime = is_static_lifetime;
131 new_symbol.is_thread_local = is_thread_local;
132 symbol_table.
add(new_symbol);
142 return id2string(class_name) +
"::clinit_already_run";
234 const bool replace_clinit,
242 const auto base_name = base.
type().get_identifier();
244 if(
const auto base_init_func = symbol_table.
lookup(base_init_routine))
248 const irep_idt &clinit_name = replace_clinit
251 if(
const auto clinit_func = symbol_table.
lookup(clinit_name))
262 std::vector<irep_idt> nondet_ids;
266 [&](
const std::pair<irep_idt, symbolt> &symbol) {
268 declaring_class(symbol.second) == class_name &&
269 symbol.second.is_static_lifetime &&
270 !symbol.second.type.get_bool(ID_C_constant))
272 nondet_ids.push_back(symbol.first);
276 for(
const auto &
id : nondet_ids)
281 parameters.min_null_tree_depth =
294 pointer_type_selector,
338 function_symbol.name = function_name;
339 function_symbol.pretty_name = function_symbol.name;
340 function_symbol.base_name = function_base_name;
344 bool failed = symbol_table.
add(function_symbol);
348 synthetic_methods.emplace(function_symbol.name, synthetic_method_type);
350 insert_result.second,
351 "synthetic methods map should not already contain entry for " +
379 "user_specified_clinit",
399 const bool thread_safe)
440 class_name, symbol_table, synthetic_methods);
524 const bool replace_clinit,
531 INVARIANT(class_name,
"Wrapper function should have an owning class.");
533 const symbolt &clinit_state_sym =
535 const symbolt &clinit_thread_local_state_sym =
550 codet atomic_begin(ID_atomic_begin);
551 codet atomic_end(ID_atomic_end);
569 "Automatically generated function. States are:\n"
570 " 0 = class not initialized, init val of clinit_state/clinit_local_state\n"
571 " 1 = class initialization in progress, by this or another thread\n"
572 " 2 = initialization finished with success, by this or another thread\n";
581 function_body.
add(decl);
591 function_body.
add(std::move(conditional));
599 function_body.
add(assign);
604 function_body.
add(atomic_begin);
613 function_body.
add(assume);
639 std::move(init_conditional));
641 function_body.
add(std::move(not_init_conditional));
646 function_body.
add(atomic_end);
653 function_body.
add(std::move(conditional));
680 object_factory_parameters,
681 pointer_type_selector,
683 function_body.
append(init_body);
692 function_body.
add(atomic_begin);
696 function_body.
add(atomic_end);
700 return function_body;
722 const bool replace_clinit,
749 INVARIANT(class_name,
"Wrapper function should have an owning class.");
751 const symbolt &already_run_symbol =
769 object_factory_parameters,
770 pointer_type_selector,
774 return code_ifthenelset(std::move(check_already_run), std::move(init_body));
778 std::unordered_multimap<irep_idt, symbolt>
781 std::unordered_multimap<irep_idt, symbolt> result;
782 for(
const auto &symbol_pair : symbol_table)
784 const symbolt &symbol = symbol_pair.second;
786 result.emplace(*declaring, symbol);
795 std::optional<ci_lazy_methods_neededt> needed_lazy_methods,
796 size_t max_user_array_length,
797 std::unordered_map<std::string, object_creation_referencet> &references,
798 const std::unordered_multimap<irep_idt, symbolt>
799 &class_to_declared_symbols_map)
802 const auto clinit_func = symbol_table.
lookup(real_clinit_name);
803 if(clinit_func ==
nullptr)
810 const auto class_entry =
812 if(class_entry != static_values_json.
end() && class_entry->second.is_object())
814 const auto &class_json_object =
to_json_object(class_entry->second);
815 std::map<symbol_exprt, jsont> static_field_values;
816 for(
const auto &symbol_pair :
817 equal_range(class_to_declared_symbols_map, class_id))
819 const symbolt &symbol = symbol_pair.second;
823 const auto &static_field_entry =
825 if(static_field_entry != class_json_object.end())
827 static_field_values.insert(
828 {static_field_expr, static_field_entry->second});
833 for(
const auto &value_pair : static_field_values)
841 max_user_array_length,
846 auto it = references.find(reference_id);
847 INVARIANT(it != references.end(),
"reference id must be present in map");
851 for(
const auto &code_with_ref : code_with_references.
list)
852 body.
append(code_with_ref->to_code(reference_substitution));
877 const bool thread_safe,
878 const bool is_user_clinit_needed)
885 std::list<class_hierarchy_grapht::node_indext> topsorted_nodes =
888 for(
const auto node : topsorted_nodes)
890 const irep_idt &class_identifier = class_graph[node].class_identifier;
894 class_identifier, symbol_table, synthetic_methods, thread_safe);
895 if(is_user_clinit_needed)
898 class_identifier, symbol_table, synthetic_methods);
907 template<
class itertype>
911 auto initial_key = in->first;
912 while(in != end && in->first == initial_key)
927 const std::unordered_set<irep_idt> &stub_globals_set,
931 for(
const irep_idt &stub_global : stub_globals_set)
946 INVARIANT(class_id,
"Static field should have a defining class.");
961 "only stub classes should be given synthetic static initialisers");
964 "a class cannot be both incomplete, and so have stub static fields, and "
965 "also define a static initializer");
969 static_init_symbol.pretty_name = static_init_name;
970 static_init_symbol.base_name =
"clinit():V";
975 bool failed = symbol_table.
add(static_init_symbol);
978 auto insert_result = synthetic_methods.emplace(
979 static_init_symbol.name,
982 insert_result.second,
983 "synthetic methods map should not already contain entry for "
984 "stub static initializer");
1012 class_id,
"Synthetic static initializer should have an owning class.");
1021 !class_globals.empty(),
1022 "class with synthetic clinit should have at least one global to init");
1027 for(
const auto &pair : class_globals)
1047 pointer_type_selector,
1052 return static_init_body;
@ DYNAMIC
Allocate dynamic objects (using ALLOCATE)
code_with_references_listt assign_from_json(const exprt &expr, const jsont &json, const irep_idt &function_id, symbol_table_baset &symbol_table, std::optional< ci_lazy_methods_neededt > &needed_lazy_methods, size_t max_user_array_length, std::unordered_map< std::string, object_creation_referencet > &references)
Given an expression expr representing a Java object or primitive and a JSON representation json of th...
Context-insensitive lazy methods container.
Class hierarchy, represented using grapht and therefore suitable for use with generic graph algorithm...
void populate(const symbol_table_baset &)
Populate the class hierarchy graph, such that there is a node for every struct type in the symbol tab...
An assumption, which must hold in subsequent code.
A codet representing sequential composition of program statements.
void append(const code_blockt &extra_block)
Add all the codets from extra_block to the current code_blockt.
void add(const codet &code)
A codet representing an assignment in the program.
A codet representing the declaration of a local variable.
codet representation of a "return from a function" statement.
goto_instruction_codet representation of a function call statement.
codet representation of an if-then-else statement.
Wrapper around a list of shared pointer to code_with_referencest objects, which provides a nicer inte...
std::list< std::shared_ptr< code_with_referencest > > list
void append(code_with_references_listt &&other)
std::function< const object_creation_referencet &(const std::string &)> reference_substitutiont
Data structure for representing an arbitrary statement in a program.
A constant literal expression.
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Base class for all expressions.
source_locationt & add_source_location()
The Boolean constant false.
std::list< node_indext > topsort() const
Find a topological order of the nodes if graph is DAG, return empty list for non-DAG or empty graph.
const irept & find(const irep_idt &name) const
iterator find(const std::string &key)
void set_file(const irep_idt &file)
void set_line(const irep_idt &line)
void set_function(const irep_idt &function)
Base class or struct that a class or struct inherits from.
const basest & bases() const
Get the collection of base classes/structs.
void create_stub_global_initializer_symbols(symbol_table_baset &symbol_table, const std::unordered_set< irep_idt > &stub_globals_set, synthetic_methods_mapt &synthetic_methods)
Create static initializer symbols for each distinct class that has stub globals.
code_blockt get_stub_initializer_body(const irep_idt &function_id, symbol_table_baset &symbol_table, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, message_handlert &message_handler)
Create the body of a synthetic static initializer (clinit method), which initialise stub globals in t...
stub_globals_by_classt stub_globals_by_class
Expression to hold a symbol (variable)
The symbol table base class interface.
const symbolt * lookup(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
const symbolst & symbols
Read-only field, used to look up symbols given their names.
const symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
bool has_symbol(const irep_idt &name) const
Check whether a symbol exists in the symbol table.
bool add(const symbolt &symbol)
Add a new symbol to the symbol table.
irep_idt base_name
Base (non-scoped) name.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
typet type
Type of symbol.
exprt value
Initial value of symbol.
The Boolean constant true.
The type of an expression, extends irept.
const std::string & id2string(const irep_idt &d)
void gen_nondet_init(const exprt &expr, code_blockt &init_code, symbol_table_baset &symbol_table, const source_locationt &loc, bool skip_classid, lifetimet lifetime, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, update_in_placet update_in_place, message_handlert &log)
Initializes a primitive-typed or reference-typed object tree rooted at expr, allocating child objects...
This module is responsible for the synthesis of code (in the form of a sequence of codet statements) ...
static code_frontend_assignt gen_clinit_assign(const exprt &expr, const clinit_statest state)
Generates a code_frontend_assignt for clinit_statest /param expr: expression to be used as the LHS of...
clinit_statest
The three states in which a <clinit> method for a class can be before, after, and during static class...
code_blockt get_user_specified_clinit_body(const irep_idt &class_id, const json_objectt &static_values_json, symbol_table_baset &symbol_table, std::optional< ci_lazy_methods_neededt > needed_lazy_methods, size_t max_user_array_length, std::unordered_map< std::string, object_creation_referencet > &references, const std::unordered_multimap< irep_idt, symbolt > &class_to_declared_symbols_map)
Create the body of a user_specified_clinit function for a given class, which includes assignments for...
static void create_function_symbol(const irep_idt &class_name, const irep_idt &function_name, const irep_idt &function_base_name, const synthetic_method_typet &synthetic_method_type, symbol_table_baset &symbol_table, synthetic_methods_mapt &synthetic_methods)
void create_static_initializer_symbols(symbol_table_baset &symbol_table, synthetic_methods_mapt &synthetic_methods, const bool thread_safe, const bool is_user_clinit_needed)
Create static initializer wrappers and possibly user-specified functions for initial static field val...
static typet clinit_states_type()
static irep_idt clinit_state_var_name(const irep_idt &class_name)
Get name of the static-initialization-state global variable for a given class.
const std::string clinit_function_suffix
static void create_user_specified_clinit_function_symbol(const irep_idt &class_name, symbol_table_baset &symbol_table, synthetic_methods_mapt &synthetic_methods)
code_ifthenelset get_clinit_wrapper_body(const irep_idt &function_id, symbol_table_baset &symbol_table, const bool nondet_static, const bool replace_clinit, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, message_handlert &message_handler)
Produces the static initializer wrapper body for the given function.
static void create_clinit_wrapper_function_symbol(const irep_idt &class_name, symbol_table_baset &symbol_table, synthetic_methods_mapt &synthetic_methods)
static equal_exprt gen_clinit_eqexpr(const exprt &expr, const clinit_statest state)
Generates an equal_exprt for clinit_statest /param expr: expression to be used as the LHS of generate...
static void clinit_wrapper_do_recursive_calls(symbol_table_baset &symbol_table, const irep_idt &class_name, code_blockt &init_body, const bool nondet_static, const bool replace_clinit, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, message_handlert &message_handler)
Generates codet that iterates through the base types of the class specified by class_name,...
static void create_clinit_wrapper_symbols(const irep_idt &class_name, symbol_table_baset &symbol_table, synthetic_methods_mapt &synthetic_methods, const bool thread_safe)
Creates a static initializer wrapper symbol for the given class, along with a global boolean that tra...
bool is_user_specified_clinit_function(const irep_idt &function_id)
Check if function_id is a user-specified clinit.
irep_idt user_specified_clinit_name(const irep_idt &class_name)
const std::string user_specified_clinit_suffix
std::unordered_multimap< irep_idt, symbolt > class_to_declared_symbols(const symbol_table_baset &symbol_table)
code_blockt get_thread_safe_clinit_wrapper_body(const irep_idt &function_id, symbol_table_baset &symbol_table, const bool nondet_static, const bool replace_clinit, const java_object_factory_parameterst &object_factory_parameters, const select_pointer_typet &pointer_type_selector, message_handlert &message_handler)
Thread safe version of the static initializer.
static irep_idt clinit_thread_local_state_var_name(const irep_idt &class_name)
Get name of the static-initialization-state local state variable for a given class.
static itertype advance_to_next_key(itertype in, itertype end)
Advance map iterator to next distinct key.
bool is_clinit_wrapper_function(const irep_idt &function_id)
Check if function_id is a clinit wrapper.
irep_idt clinit_wrapper_name(const irep_idt &class_name)
Get the Java static initializer wrapper name for a given class (the wrapper checks if static initiali...
static irep_idt clinit_function_name(const irep_idt &class_name)
Get name of the real static initializer for a given class.
static irep_idt clinit_already_run_variable_name(const irep_idt &class_name)
Get name of the static-initialization-already-done global variable for a given class.
bool is_clinit_function(const irep_idt &function_id)
Check if function_id is a clinit.
static irep_idt clinit_local_init_complete_var_name(const irep_idt &class_name)
Get name of the static-initialization local variable for a given class.
static symbolt add_new_variable_symbol(symbol_table_baset &symbol_table, const irep_idt &name, const typet &type, const exprt &value, const bool is_thread_local, const bool is_static_lifetime)
Add a new symbol to the symbol table.
const std::string clinit_wrapper_suffix
static bool needs_clinit_wrapper(const irep_idt &class_name, const symbol_table_baset &symbol_table)
Checks whether a static initializer wrapper is needed for a given class, i.e.
empty_typet java_void_type()
signedbv_typet java_byte_type()
const java_class_typet & to_java_class_type(const typet &type)
std::optional< irep_idt > declaring_class(const symbolt &symbol)
Gets the identifier of the class which declared a given symbol.
bool is_non_null_library_global(const irep_idt &symbolid)
Check if a symbol is a well-known non-null global.
void set_declaring_class(symbolt &symbol, const irep_idt &declaring_class)
Sets the identifier of the class which declared a given symbol to declaring_class.
irep_idt strip_java_namespace_prefix(const irep_idt &to_strip)
Strip java:: prefix from given identifier.
json_objectt & to_json_object(jsont &json)
static void nondet_static(const namespacet &ns, goto_modelt &goto_model, const irep_idt &fct_name)
Nondeterministically initializes global scope variables in a goto-function.
static std::string comment(const rw_set_baset::entryt &entry, bool write)
ranget< typename multimapt::const_iterator > equal_range(const multimapt &multimap, const typename multimapt::key_type &key)
Utility function to make equal_range method of multimap easier to use by returning a ranget object.
#define PRECONDITION(CONDITION)
#define INVARIANT(CONDITION, REASON)
This macro uses the wrapper function 'invariant_violated_string'.
const class_typet & to_class_type(const typet &type)
Cast a typet to a class_typet.
Information to store when several references point to the same Java object.
irep_idt function_id
Function id, used as a prefix for identifiers of temporaries.
size_t min_null_tree_depth
To force a certain depth of non-null objects.
bool has_suffix(const std::string &s, const std::string &suffix)
static bool failed(bool error_indicator)
std::unordered_map< irep_idt, synthetic_method_typet > synthetic_methods_mapt
Maps method names on to a synthetic method kind.
synthetic_method_typet
Synthetic method kinds.
@ USER_SPECIFIED_STATIC_INITIALIZER
Only exists if the --static-values option was used.
@ STATIC_INITIALIZER_WRAPPER
A static initializer wrapper (code of the form if(!already_run) clinit(); already_run = true;) These ...
@ STUB_CLASS_STATIC_INITIALIZER
A generated (synthetic) static initializer function for a stub type.