Skip to content

Commit 9692359

Browse files
committed
Create json_clinit symbol if --static-values is given
This function symbol is created in the same way as the one for clinit_wrapper. In this commit, the function has an empty body. It will be given content in the following commits.
1 parent 66f19ed commit 9692359

File tree

4 files changed

+53
-10
lines changed

4 files changed

+53
-10
lines changed

jbmc/src/java_bytecode/java_bytecode_language.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -788,11 +788,11 @@ bool java_bytecode_languaget::typecheck(
788788
symbol_table, symbol_table_journal.get_inserted(), synthetic_methods);
789789
}
790790

791-
// For each class that will require a static initializer wrapper, create a
792-
// function named package.classname::clinit_wrapper, and a corresponding
793-
// global tracking whether it has run or not:
794-
create_static_initializer_wrappers(
795-
symbol_table, synthetic_methods, threading_support);
791+
create_static_initializer_symbols(
792+
symbol_table,
793+
synthetic_methods,
794+
threading_support,
795+
!static_values_file.empty());
796796

797797
// Now incrementally elaborate methods
798798
// that are reachable from this entry point.
@@ -1102,6 +1102,8 @@ bool java_bytecode_languaget::convert_single_method(
11021102
get_pointer_type_selector(),
11031103
get_message_handler());
11041104
break;
1105+
case synthetic_method_typet::JSON_STATIC_INITIALIZER:
1106+
break;
11051107
case synthetic_method_typet::STUB_CLASS_STATIC_INITIALIZER:
11061108
writable_symbol.value =
11071109
stub_global_initializer_factory.get_stub_initializer_body(

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ static typet clinit_states_type()
5252
// Disable linter here to allow a std::string constant, since that holds
5353
// a length, whereas a cstr would require strlen every time.
5454
const std::string clinit_wrapper_suffix = "::clinit_wrapper"; // NOLINT(*)
55+
const std::string json_clinit_suffix = "::json_clinit"; // NOLINT(*)
5556
const std::string clinit_function_suffix = ".<clinit>:()V"; // NOLINT(*)
5657

5758
/// Get the Java static initializer wrapper name for a given class (the wrapper
@@ -65,6 +66,11 @@ irep_idt clinit_wrapper_name(const irep_idt &class_name)
6566
return id2string(class_name) + clinit_wrapper_suffix;
6667
}
6768

69+
irep_idt json_clinit_name(const irep_idt &class_name)
70+
{
71+
return id2string(class_name) + json_clinit_suffix;
72+
}
73+
6874
/// Check if function_id is a clinit wrapper
6975
/// \param function_id: some function identifier
7076
/// \return true if the passed identifier is a clinit wrapper
@@ -335,6 +341,21 @@ static void create_clinit_wrapper_function_symbol(
335341
synthetic_methods);
336342
}
337343

344+
// Create symbol for the "json_clinit"
345+
static void create_json_clinit_function_symbol(
346+
const irep_idt &class_name,
347+
symbol_tablet &symbol_table,
348+
synthetic_methods_mapt &synthetic_methods)
349+
{
350+
create_function_symbol(
351+
class_name,
352+
json_clinit_name(class_name),
353+
"json_clinit",
354+
synthetic_method_typet::JSON_STATIC_INITIALIZER,
355+
symbol_table,
356+
synthetic_methods);
357+
}
358+
338359
/// Creates a static initializer wrapper symbol for the given class, along with
339360
/// a global boolean that tracks if it has been run already.
340361
/// \param class_name: class symbol name
@@ -716,17 +737,27 @@ code_ifthenelset get_clinit_wrapper_body(
716737
return code_ifthenelset(std::move(check_already_run), std::move(init_body));
717738
}
718739

719-
/// Create static initializer wrappers for all classes that need them.
740+
/// Create static initializer wrappers and possibly assignments from a JSON file
741+
/// for all classes that need them.
742+
/// For each class that will require a static initializer wrapper, create a
743+
/// function named package.classname::clinit_wrapper, and a corresponding
744+
/// global tracking whether it has run or not. If a JSON file containing static
745+
/// values is given, also create a function named
746+
/// package.classname::json_clinit.
720747
/// \param symbol_table: global symbol table
721748
/// \param synthetic_methods: synthetic methods map. Will be extended noting
722749
/// that any wrapper belongs to this code, and so `get_clinit_wrapper_body`
723750
/// should be used to produce the method body when required.
724751
/// \param thread_safe: if true state variables required to make the
725752
/// clinit_wrapper thread safe will be created.
726-
void create_static_initializer_wrappers(
753+
/// \param is_json_clinit_needed: determines whether or not a symbol for the
754+
/// synthetic json_clinit function should be created. This is true if a file
755+
/// was given with the --static-values option and false otherwise.
756+
void create_static_initializer_symbols(
727757
symbol_tablet &symbol_table,
728758
synthetic_methods_mapt &synthetic_methods,
729-
const bool thread_safe)
759+
const bool thread_safe,
760+
const bool is_json_clinit_needed)
730761
{
731762
// Top-sort the class hierarchy, such that we visit parents before children,
732763
// and can so identify parents that need static initialisation by whether we
@@ -743,6 +774,11 @@ void create_static_initializer_wrappers(
743774
{
744775
create_clinit_wrapper_symbols(
745776
class_identifier, symbol_table, synthetic_methods, thread_safe);
777+
if(is_json_clinit_needed)
778+
{
779+
create_json_clinit_function_symbol(
780+
class_identifier, symbol_table, synthetic_methods);
781+
}
746782
}
747783
}
748784
}

jbmc/src/java_bytecode/java_static_initializers.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ Author: Chris Smowton, [email protected]
2020
#include <util/symbol_table.h>
2121

2222
irep_idt clinit_wrapper_name(const irep_idt &class_name);
23+
irep_idt json_clinit_name(const irep_idt &class_name);
2324

2425
bool is_clinit_wrapper_function(const irep_idt &function_id);
2526

26-
void create_static_initializer_wrappers(
27+
void create_static_initializer_symbols(
2728
symbol_tablet &symbol_table,
2829
synthetic_methods_mapt &synthetic_methods,
29-
const bool thread_safe);
30+
const bool thread_safe,
31+
const bool is_json_clinit_needed);
3032

3133
code_blockt get_thread_safe_clinit_wrapper_body(
3234
const irep_idt &function_id,

jbmc/src/java_bytecode/synthetic_methods_map.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ enum class synthetic_method_typet
2727
/// These are generated for both user and stub types, to ensure the actual
2828
/// static initializer is only run once on any given path.
2929
STATIC_INITIALIZER_WRAPPER,
30+
/// Only exists if the `--static-values` option was used.
31+
/// TODO Currently has no content, future commits will add it.
32+
JSON_STATIC_INITIALIZER,
3033
/// A generated (synthetic) static initializer function for a stub type.
3134
/// Because we don't have the bytecode for a stub type (by definition), we
3235
/// generate a static initializer function to initialize its static fields.

0 commit comments

Comments
 (0)