Skip to content

Commit 9460ce5

Browse files
Merge pull request #5195 from peterschrammel/move-lazy-goto-model
Move lazy goto model to JBMC [TG-9888] [blocks: 4753]
2 parents 2d3b8b8 + a3508a6 commit 9460ce5

File tree

11 files changed

+164
-191
lines changed

11 files changed

+164
-191
lines changed

jbmc/src/java_bytecode/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ SRC = assignments_from_json.cpp \
4343
java_trace_validation.cpp \
4444
java_types.cpp \
4545
java_utils.cpp \
46-
lift_clinit_calls.cpp \
4746
lambda_synthesis.cpp \
47+
lazy_goto_model.cpp \
48+
lift_clinit_calls.cpp \
4849
load_method_by_regex.cpp \
4950
mz_zip_archive.cpp \
5051
remove_exceptions.cpp \

src/goto-programs/lazy_goto_functions_map.h renamed to jbmc/src/java_bytecode/lazy_goto_functions_map.h

Lines changed: 38 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
#include <unordered_set>
1010

11-
#include "goto_functions.h"
12-
#include "goto_convert_functions.h"
11+
#include <goto-programs/goto_convert_functions.h>
12+
#include <goto-programs/goto_functions.h>
1313

1414
#include <langapi/language_file.h>
1515
#include <util/journalling_symbol_table.h>
@@ -49,20 +49,17 @@ class lazy_goto_functions_mapt final
4949
// NOLINTNEXTLINE(readability/identifiers) - name matches those used in STL
5050
typedef std::size_t size_type;
5151

52-
typedef
53-
std::function<void(
54-
const irep_idt &name,
55-
goto_functionst::goto_functiont &function,
56-
journalling_symbol_tablet &function_symbols)>
52+
typedef std::function<void(
53+
const irep_idt &name,
54+
goto_functionst::goto_functiont &function,
55+
journalling_symbol_tablet &function_symbols)>
5756
post_process_functiont;
58-
typedef std::function<bool(const irep_idt &name)>
59-
can_generate_function_bodyt;
60-
typedef std::function<
61-
bool(
62-
const irep_idt &function_name,
63-
symbol_table_baset &symbol_table,
64-
goto_functiont &function,
65-
bool body_available)>
57+
typedef std::function<bool(const irep_idt &name)> can_generate_function_bodyt;
58+
typedef std::function<bool(
59+
const irep_idt &function_name,
60+
symbol_table_baset &symbol_table,
61+
goto_functiont &function,
62+
bool body_available)>
6663
generate_function_bodyt;
6764

6865
private:
@@ -90,15 +87,15 @@ class lazy_goto_functions_mapt final
9087
can_generate_function_bodyt driver_program_can_generate_function_body,
9188
generate_function_bodyt driver_program_generate_function_body,
9289
message_handlert &message_handler)
93-
: goto_functions(goto_functions),
94-
language_files(language_files),
95-
symbol_table(symbol_table),
96-
post_process_function(post_process_function),
97-
driver_program_can_generate_function_body(
98-
driver_program_can_generate_function_body),
99-
driver_program_generate_function_body(
100-
driver_program_generate_function_body),
101-
message_handler(message_handler)
90+
: goto_functions(goto_functions),
91+
language_files(language_files),
92+
symbol_table(symbol_table),
93+
post_process_function(post_process_function),
94+
driver_program_can_generate_function_body(
95+
driver_program_can_generate_function_body),
96+
driver_program_generate_function_body(
97+
driver_program_generate_function_body),
98+
message_handler(message_handler)
10299
{
103100
}
104101

@@ -125,12 +122,14 @@ class lazy_goto_functions_mapt final
125122
/// it a bodyless stub.
126123
bool can_produce_function(const key_type &name) const
127124
{
128-
return
129-
language_files.can_convert_lazy_method(name) ||
130-
driver_program_can_generate_function_body(name);
125+
return language_files.can_convert_lazy_method(name) ||
126+
driver_program_can_generate_function_body(name);
131127
}
132128

133-
void unload(const key_type &name) const { goto_functions.erase(name); }
129+
void unload(const key_type &name) const
130+
{
131+
goto_functions.erase(name);
132+
}
134133

135134
void ensure_function_loaded(const key_type &name) const
136135
{
@@ -148,9 +147,9 @@ class lazy_goto_functions_mapt final
148147

149148
journalling_symbol_tablet journalling_table =
150149
journalling_symbol_tablet::wrap(symbol_table_builder);
151-
reference named_function=ensure_entry_converted(name, journalling_table);
152-
mapped_type function=named_function.second;
153-
if(processed_functions.count(name)==0)
150+
reference named_function = ensure_entry_converted(name, journalling_table);
151+
mapped_type function = named_function.second;
152+
if(processed_functions.count(name) == 0)
154153
{
155154
// Run function-pass conversions
156155
post_process_function(name, function, journalling_table);
@@ -176,19 +175,18 @@ class lazy_goto_functions_mapt final
176175
// Fill in symbol table entry body if not already done
177176
language_files.convert_lazy_method(name, function_symbol_table);
178177

179-
underlying_mapt::iterator it=goto_functions.find(name);
180-
if(it!=goto_functions.end())
178+
underlying_mapt::iterator it = goto_functions.find(name);
179+
if(it != goto_functions.end())
181180
return *it;
182181

183182
goto_functiont function;
184183

185184
// First chance: see if the driver program wants to provide a replacement:
186-
bool body_provided =
187-
driver_program_generate_function_body(
188-
name,
189-
function_symbol_table,
190-
function,
191-
language_files.can_convert_lazy_method(name));
185+
bool body_provided = driver_program_generate_function_body(
186+
name,
187+
function_symbol_table,
188+
function,
189+
language_files.can_convert_lazy_method(name));
192190

193191
// Second chance: see if language_filest can provide a body:
194192
if(!body_provided)
@@ -204,4 +202,4 @@ class lazy_goto_functions_mapt final
204202
}
205203
};
206204

207-
#endif // CPROVER_GOTO_PROGRAMS_LAZY_GOTO_FUNCTIONS_MAP_H
205+
#endif // CPROVER_GOTO_PROGRAMS_LAZY_GOTO_FUNCTIONS_MAP_H

src/goto-programs/lazy_goto_model.cpp renamed to jbmc/src/java_bytecode/lazy_goto_model.cpp

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
/// Model for lazy loading of functions
55

66
#include "lazy_goto_model.h"
7-
#include "read_goto_binary.h"
8-
#include "rebuild_goto_start_function.h"
7+
8+
#include <goto-programs/read_goto_binary.h>
9+
#include <goto-programs/rebuild_goto_start_function.h>
910

1011
#include <langapi/mode.h>
1112

@@ -32,11 +33,10 @@ lazy_goto_modelt::lazy_goto_modelt(
3233
goto_model->goto_functions.function_map,
3334
language_files,
3435
symbol_table,
35-
[this] (
36+
[this](
3637
const irep_idt &function_name,
3738
goto_functionst::goto_functiont &function,
38-
journalling_symbol_tablet &journalling_symbol_table) -> void
39-
{
39+
journalling_symbol_tablet &journalling_symbol_table) -> void {
4040
goto_model_functiont model_function(
4141
journalling_symbol_table,
4242
goto_model->goto_functions,
@@ -65,11 +65,10 @@ lazy_goto_modelt::lazy_goto_modelt(lazy_goto_modelt &&other)
6565
goto_model->goto_functions.function_map,
6666
language_files,
6767
symbol_table,
68-
[this] (
68+
[this](
6969
const irep_idt &function_name,
7070
goto_functionst::goto_functiont &function,
71-
journalling_symbol_tablet &journalling_symbol_table) -> void
72-
{
71+
journalling_symbol_tablet &journalling_symbol_table) -> void {
7372
goto_model_functiont model_function(
7473
journalling_symbol_table,
7574
goto_model->goto_functions,
@@ -150,16 +149,16 @@ void lazy_goto_modelt::initialize(
150149
"failed to open input file '" + filename + '\'');
151150
}
152151

153-
language_filet &lf=add_language_file(filename);
154-
lf.language=get_language_from_filename(filename);
152+
language_filet &lf = add_language_file(filename);
153+
lf.language = get_language_from_filename(filename);
155154

156-
if(lf.language==nullptr)
155+
if(lf.language == nullptr)
157156
{
158157
throw invalid_source_file_exceptiont(
159158
"failed to figure out type of file '" + filename + '\'');
160159
}
161160

162-
languaget &language=*lf.language;
161+
languaget &language = *lf.language;
163162
language.set_message_handler(message_handler);
164163
language.set_language_options(options);
165164

@@ -197,21 +196,35 @@ void lazy_goto_modelt::initialize(
197196
bool binaries_provided_start =
198197
symbol_table.has_symbol(goto_functionst::entry_point());
199198

200-
bool entry_point_generation_failed=false;
199+
bool entry_point_generation_failed = false;
201200

202201
if(binaries_provided_start && options.is_set("function"))
203202
{
204-
// Rebuild the entry-point, using the language annotation of the
205-
// existing __CPROVER_start function:
206-
rebuild_lazy_goto_start_functiont rebuild_existing_start(
207-
options, *this, message_handler);
208-
entry_point_generation_failed=rebuild_existing_start();
203+
// The goto binaries provided already contain a __CPROVER_start
204+
// function that may be tied to a different entry point `function`.
205+
// Hence, we will rebuild the __CPROVER_start function.
206+
207+
// Get the language annotation of the existing __CPROVER_start function.
208+
std::unique_ptr<languaget> language =
209+
get_entry_point_language(symbol_table, options, message_handler);
210+
211+
// To create a new entry point we must first remove the old one
212+
remove_existing_entry_point(symbol_table);
213+
214+
// Create the new entry-point
215+
entry_point_generation_failed =
216+
language->generate_support_functions(symbol_table);
217+
218+
// Remove the function from the goto functions so it is copied back in
219+
// from the symbol table during goto_convert
220+
if(!entry_point_generation_failed)
221+
unload(goto_functionst::entry_point());
209222
}
210223
else if(!binaries_provided_start)
211224
{
212225
// Allow all language front-ends to try to provide the user-specified
213226
// (--function) entry-point, or some language-specific default:
214-
entry_point_generation_failed=
227+
entry_point_generation_failed =
215228
language_files.generate_support_functions(symbol_table);
216229
}
217230

@@ -228,10 +241,11 @@ void lazy_goto_modelt::initialize(
228241
void lazy_goto_modelt::load_all_functions() const
229242
{
230243
symbol_tablet::symbolst::size_type table_size;
231-
symbol_tablet::symbolst::size_type new_table_size=symbol_table.symbols.size();
244+
symbol_tablet::symbolst::size_type new_table_size =
245+
symbol_table.symbols.size();
232246
do
233247
{
234-
table_size=new_table_size;
248+
table_size = new_table_size;
235249

236250
// Find symbols that correspond to functions
237251
std::vector<irep_idt> fn_ids_to_convert;
@@ -247,8 +261,8 @@ void lazy_goto_modelt::load_all_functions() const
247261
// Repeat while new symbols are being added in case any of those are
248262
// stubbed functions. Even stubs can create new stubs while being
249263
// converted if they are special stubs (e.g. string functions)
250-
new_table_size=symbol_table.symbols.size();
251-
} while(new_table_size!=table_size);
264+
new_table_size = symbol_table.symbols.size();
265+
} while(new_table_size != table_size);
252266

253267
goto_model->goto_functions.compute_location_numbers();
254268
}

src/goto-programs/lazy_goto_model.h renamed to jbmc/src/java_bytecode/lazy_goto_model.h

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#include <langapi/language_file.h>
1010

11-
#include "abstract_goto_model.h"
12-
#include "goto_model.h"
11+
#include <goto-programs/abstract_goto_model.h>
12+
#include <goto-programs/goto_convert_functions.h>
13+
#include <goto-programs/goto_model.h>
14+
1315
#include "lazy_goto_functions_map.h"
14-
#include "goto_convert_functions.h"
1516

1617
class optionst;
1718

@@ -148,7 +149,7 @@ class lazy_goto_modelt : public abstract_goto_modelt
148149
/// \param message_handler: The message_handler to use for logging
149150
/// \tparam THandler: a type that defines methods process_goto_function and
150151
/// process_goto_functions
151-
template<typename THandler>
152+
template <typename THandler>
152153
static lazy_goto_modelt from_handler_object(
153154
THandler &handler,
154155
const optionst &options,
@@ -165,15 +166,13 @@ class lazy_goto_modelt : public abstract_goto_modelt
165166
[&handler](const irep_idt &name) -> bool {
166167
return handler.can_generate_function_body(name);
167168
},
168-
[&handler]
169-
(const irep_idt &function_name,
170-
symbol_table_baset &symbol_table,
171-
goto_functiont &function,
172-
bool is_first_chance)
173-
{
174-
return
175-
handler.generate_function_body(
176-
function_name, symbol_table, function, is_first_chance);
169+
[&handler](
170+
const irep_idt &function_name,
171+
symbol_table_baset &symbol_table,
172+
goto_functiont &function,
173+
bool is_first_chance) {
174+
return handler.generate_function_body(
175+
function_name, symbol_table, function, is_first_chance);
177176
},
178177
message_handler);
179178
}
@@ -184,7 +183,10 @@ class lazy_goto_modelt : public abstract_goto_modelt
184183
/// Eagerly loads all functions from the symbol table.
185184
void load_all_functions() const;
186185

187-
void unload(const irep_idt &name) const { goto_functions.unload(name); }
186+
void unload(const irep_idt &name) const
187+
{
188+
goto_functions.unload(name);
189+
}
188190

189191
language_filet &add_language_file(const std::string &filename)
190192
{
@@ -197,8 +199,8 @@ class lazy_goto_modelt : public abstract_goto_modelt
197199
/// Before freezing the functions all module-level passes are run
198200
/// \param model: The lazy_goto_modelt to freeze
199201
/// \return The frozen goto_modelt or an empty optional if freezing fails
200-
static std::unique_ptr<goto_modelt> process_whole_model_and_freeze(
201-
lazy_goto_modelt &&model)
202+
static std::unique_ptr<goto_modelt>
203+
process_whole_model_and_freeze(lazy_goto_modelt &&model)
202204
{
203205
if(model.finalize())
204206
return nullptr;
@@ -237,8 +239,8 @@ class lazy_goto_modelt : public abstract_goto_modelt
237239
/// `languaget::convert_lazy_method` function. If that results in a `codet`
238240
/// representation of the function stored in the symbol table, convert it
239241
/// to GOTO and return it as in step (3).
240-
const goto_functionst::goto_functiont &get_goto_function(const irep_idt &id)
241-
override
242+
const goto_functionst::goto_functiont &
243+
get_goto_function(const irep_idt &id) override
242244
{
243245
return goto_functions.at(id);
244246
}

jbmc/src/jbmc/jbmc_parse_options.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,22 @@ Author: Daniel Kroening, [email protected]
3535
#include <goto-checker/stop_on_fail_verifier_with_fault_localization.h>
3636

3737
#include <goto-programs/adjust_float_expressions.h>
38-
#include <goto-programs/lazy_goto_model.h>
39-
#include <goto-programs/instrument_preconditions.h>
4038
#include <goto-programs/goto_convert_functions.h>
4139
#include <goto-programs/goto_inline.h>
40+
#include <goto-programs/instrument_preconditions.h>
4241
#include <goto-programs/loop_ids.h>
43-
#include <goto-programs/remove_virtual_functions.h>
4442
#include <goto-programs/remove_returns.h>
45-
#include <goto-programs/remove_unused_functions.h>
4643
#include <goto-programs/remove_skip.h>
44+
#include <goto-programs/remove_unused_functions.h>
45+
#include <goto-programs/remove_virtual_functions.h>
4746
#include <goto-programs/set_properties.h>
4847
#include <goto-programs/show_goto_functions.h>
49-
#include <goto-programs/show_symbol_table.h>
5048
#include <goto-programs/show_properties.h>
49+
#include <goto-programs/show_symbol_table.h>
5150

5251
#include <goto-instrument/full_slicer.h>
53-
#include <goto-instrument/reachability_slicer.h>
5452
#include <goto-instrument/nondet_static.h>
53+
#include <goto-instrument/reachability_slicer.h>
5554

5655
#include <goto-symex/path_storage.h>
5756

@@ -68,6 +67,7 @@ Author: Daniel Kroening, [email protected]
6867
#include <java_bytecode/java_multi_path_symex_only_checker.h>
6968
#include <java_bytecode/java_single_path_symex_checker.h>
7069
#include <java_bytecode/java_single_path_symex_only_checker.h>
70+
#include <java_bytecode/lazy_goto_model.h>
7171
#include <java_bytecode/remove_exceptions.h>
7272
#include <java_bytecode/remove_instanceof.h>
7373
#include <java_bytecode/remove_java_new.h>

jbmc/src/jbmc/jbmc_parse_options.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Author: Daniel Kroening, [email protected]
2525

2626
#include <goto-programs/class_hierarchy.h>
2727
#include <goto-programs/goto_trace.h>
28-
#include <goto-programs/lazy_goto_model.h>
2928
#include <goto-programs/show_properties.h>
29+
#include <java_bytecode/lazy_goto_model.h>
3030

3131
#include <goto-symex/path_storage.h>
3232

0 commit comments

Comments
 (0)