Skip to content

Commit 8d6b393

Browse files
committed
Add message handler to remove_instanceof and _exceptions
This enables reporting errors from remove_instanceof usage of goto_convert.
1 parent b8631b9 commit 8d6b393

File tree

8 files changed

+68
-40
lines changed

8 files changed

+68
-40
lines changed

jbmc/src/janalyzer/janalyzer_parse_options.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,9 @@ bool janalyzer_parse_optionst::process_goto_program(const optionst &options)
670670
remove_virtual_functions(goto_model);
671671
// remove Java throw and catch
672672
// This introduces instanceof, so order is important:
673-
remove_exceptions(goto_model);
673+
remove_exceptions(goto_model, get_message_handler());
674674
// remove rtti
675-
remove_instanceof(goto_model);
675+
remove_instanceof(goto_model, get_message_handler());
676676

677677
// do partial inlining
678678
status() << "Partial Inlining" << eom;

jbmc/src/java_bytecode/remove_exceptions.cpp

+19-7
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ class remove_exceptionst
8888
explicit remove_exceptionst(
8989
symbol_table_baset &_symbol_table,
9090
function_may_throwt _function_may_throw,
91-
bool remove_added_instanceof)
91+
bool remove_added_instanceof,
92+
message_handlert &message_handler)
9293
: symbol_table(_symbol_table),
9394
function_may_throw(_function_may_throw),
94-
remove_added_instanceof(remove_added_instanceof)
95+
remove_added_instanceof(remove_added_instanceof),
96+
message_handler(message_handler)
9597
{
9698
}
9799

@@ -102,6 +104,7 @@ class remove_exceptionst
102104
symbol_table_baset &symbol_table;
103105
function_may_throwt function_may_throw;
104106
bool remove_added_instanceof;
107+
message_handlert &message_handler;
105108

106109
symbol_exprt get_inflight_exception_global();
107110

@@ -342,7 +345,7 @@ void remove_exceptionst::add_exception_dispatch_sequence(
342345
t_exc->guard=check;
343346

344347
if(remove_added_instanceof)
345-
remove_instanceof(t_exc, goto_program, symbol_table);
348+
remove_instanceof(t_exc, goto_program, symbol_table, message_handler);
346349
}
347350
}
348351
}
@@ -574,6 +577,7 @@ void remove_exceptionst::operator()(goto_programt &goto_program)
574577
void remove_exceptions(
575578
symbol_table_baset &symbol_table,
576579
goto_functionst &goto_functions,
580+
message_handlert &message_handler,
577581
remove_exceptions_typest type)
578582
{
579583
const namespacet ns(symbol_table);
@@ -586,7 +590,8 @@ void remove_exceptions(
586590
remove_exceptionst remove_exceptions(
587591
symbol_table,
588592
function_may_throw,
589-
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
593+
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF,
594+
message_handler);
590595
remove_exceptions(goto_functions);
591596
}
592597

@@ -600,12 +605,14 @@ void remove_exceptions(
600605
/// \param symbol_table: global symbol table. The `@inflight_exception` global
601606
/// may be added if not already present. It will not be initialised; that is
602607
/// the caller's responsibility.
608+
/// \param message_handler: logging output
603609
/// \param type: specifies whether instanceof operations generated by this pass
604610
/// should be lowered to class-identifier comparisons (using
605611
/// remove_instanceof).
606612
void remove_exceptions(
607613
goto_programt &goto_program,
608614
symbol_table_baset &symbol_table,
615+
message_handlert &message_handler,
609616
remove_exceptions_typest type)
610617
{
611618
remove_exceptionst::function_may_throwt any_function_may_throw =
@@ -614,12 +621,17 @@ void remove_exceptions(
614621
remove_exceptionst remove_exceptions(
615622
symbol_table,
616623
any_function_may_throw,
617-
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
624+
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF,
625+
message_handler);
618626
remove_exceptions(goto_program);
619627
}
620628

621629
/// removes throws/CATCH-POP/CATCH-PUSH
622-
void remove_exceptions(goto_modelt &goto_model, remove_exceptions_typest type)
630+
void remove_exceptions(
631+
goto_modelt &goto_model,
632+
message_handlert &message_handler,
633+
remove_exceptions_typest type)
623634
{
624-
remove_exceptions(goto_model.symbol_table, goto_model.goto_functions, type);
635+
remove_exceptions(
636+
goto_model.symbol_table, goto_model.goto_functions, message_handler, type);
625637
}

jbmc/src/java_bytecode/remove_exceptions.h

+4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ Date: December 2016
1616

1717
#include <goto-programs/goto_model.h>
1818

19+
#include <util/message.h>
20+
1921
#define INFLIGHT_EXCEPTION_VARIABLE_BASENAME "@inflight_exception"
2022
#define INFLIGHT_EXCEPTION_VARIABLE_NAME \
2123
"java::" INFLIGHT_EXCEPTION_VARIABLE_BASENAME
@@ -32,11 +34,13 @@ enum class remove_exceptions_typest
3234
void remove_exceptions(
3335
goto_programt &goto_program,
3436
symbol_table_baset &symbol_table,
37+
message_handlert &message_handler,
3538
remove_exceptions_typest type =
3639
remove_exceptions_typest::DONT_REMOVE_INSTANCEOF);
3740

3841
void remove_exceptions(
3942
goto_modelt &goto_model,
43+
message_handlert &message_handler,
4044
remove_exceptions_typest type =
4145
remove_exceptions_typest::DONT_REMOVE_INSTANCEOF);
4246

jbmc/src/java_bytecode/remove_instanceof.cpp

+25-20
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ Author: Chris Smowton, [email protected]
2323
class remove_instanceoft
2424
{
2525
public:
26-
explicit remove_instanceoft(symbol_table_baset &symbol_table)
27-
: symbol_table(symbol_table), ns(symbol_table)
26+
remove_instanceoft(
27+
symbol_table_baset &symbol_table, message_handlert &message_handler)
28+
: symbol_table(symbol_table)
29+
, ns(symbol_table)
30+
, message_handler(message_handler)
2831
{
2932
class_hierarchy(symbol_table);
3033
}
@@ -39,6 +42,7 @@ class remove_instanceoft
3942
symbol_table_baset &symbol_table;
4043
namespacet ns;
4144
class_hierarchyt class_hierarchy;
45+
message_handlert &message_handler;
4246

4347
bool lower_instanceof(
4448
exprt &, goto_programt &, goto_programt::targett);
@@ -154,24 +158,15 @@ bool remove_instanceoft::lower_instanceof(
154158
// Replace the instanceof construct with instanceof_result:
155159
expr = instanceof_result_sym.symbol_expr();
156160

157-
std::ostringstream convert_output;
158-
stream_message_handlert convert_message_handler(convert_output);
159-
convert_message_handler.set_verbosity(messaget::message_levelt::M_WARNING);
160-
161161
// Insert the new test block before it:
162162
goto_programt new_check_program;
163163
goto_convert(
164164
is_null_branch,
165165
symbol_table,
166166
new_check_program,
167-
convert_message_handler,
167+
message_handler,
168168
ID_java);
169169

170-
INVARIANT(
171-
convert_output.str().empty(),
172-
"remove_instanceof generated test should be goto-converted without error, "
173-
"but goto_convert reported: " + convert_output.str());
174-
175170
goto_program.destructive_insert(this_inst, new_check_program);
176171

177172
return true;
@@ -245,12 +240,14 @@ bool remove_instanceoft::lower_instanceof(goto_programt &goto_program)
245240
/// \param target: The instruction to work on.
246241
/// \param goto_program: The function body containing the instruction.
247242
/// \param symbol_table: The symbol table to add symbols to.
243+
/// \param message_handler: logging output
248244
void remove_instanceof(
249245
goto_programt::targett target,
250246
goto_programt &goto_program,
251-
symbol_table_baset &symbol_table)
247+
symbol_table_baset &symbol_table,
248+
message_handlert &message_handler)
252249
{
253-
remove_instanceoft rem(symbol_table);
250+
remove_instanceoft rem(symbol_table, message_handler);
254251
rem.lower_instanceof(goto_program, target);
255252
}
256253

@@ -259,11 +256,13 @@ void remove_instanceof(
259256
/// \remarks Extra auxiliary variables may be introduced into symbol_table.
260257
/// \param function: The function to work on.
261258
/// \param symbol_table: The symbol table to add symbols to.
259+
/// \param message_handler: logging output
262260
void remove_instanceof(
263261
goto_functionst::goto_functiont &function,
264-
symbol_table_baset &symbol_table)
262+
symbol_table_baset &symbol_table,
263+
message_handlert &message_handler)
265264
{
266-
remove_instanceoft rem(symbol_table);
265+
remove_instanceoft rem(symbol_table, message_handler);
267266
rem.lower_instanceof(function.body);
268267
}
269268

@@ -272,11 +271,13 @@ void remove_instanceof(
272271
/// \remarks Extra auxiliary variables may be introduced into symbol_table.
273272
/// \param goto_functions: The functions to work on.
274273
/// \param symbol_table: The symbol table to add symbols to.
274+
/// \param message_handler: logging output
275275
void remove_instanceof(
276276
goto_functionst &goto_functions,
277-
symbol_table_baset &symbol_table)
277+
symbol_table_baset &symbol_table,
278+
message_handlert &message_handler)
278279
{
279-
remove_instanceoft rem(symbol_table);
280+
remove_instanceoft rem(symbol_table, message_handler);
280281
bool changed=false;
281282
for(auto &f : goto_functions.function_map)
282283
changed=rem.lower_instanceof(f.second.body) || changed;
@@ -288,8 +289,12 @@ void remove_instanceof(
288289
/// class-identifier test.
289290
/// \remarks Extra auxiliary variables may be introduced into symbol_table.
290291
/// \param goto_model: The functions to work on and the symbol table to add
292+
/// \param message_handler: logging output
291293
/// symbols to.
292-
void remove_instanceof(goto_modelt &goto_model)
294+
void remove_instanceof(
295+
goto_modelt &goto_model,
296+
message_handlert &message_handler)
293297
{
294-
remove_instanceof(goto_model.goto_functions, goto_model.symbol_table);
298+
remove_instanceof(
299+
goto_model.goto_functions, goto_model.symbol_table, message_handler);
295300
}

jbmc/src/java_bytecode/remove_instanceof.h

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,24 +12,28 @@ Author: Chris Smowton, [email protected]
1212
#ifndef CPROVER_JAVA_BYTECODE_REMOVE_INSTANCEOF_H
1313
#define CPROVER_JAVA_BYTECODE_REMOVE_INSTANCEOF_H
1414

15-
#include <util/symbol_table.h>
16-
1715
#include <goto-programs/goto_functions.h>
1816
#include <goto-programs/goto_model.h>
1917

18+
#include <util/message.h>
19+
#include <util/symbol_table.h>
20+
2021
void remove_instanceof(
2122
goto_programt::targett target,
2223
goto_programt &goto_program,
23-
symbol_table_baset &symbol_table);
24+
symbol_table_baset &symbol_table,
25+
message_handlert &);
2426

2527
void remove_instanceof(
2628
goto_functionst::goto_functiont &function,
27-
symbol_table_baset &symbol_table);
29+
symbol_table_baset &symbol_table,
30+
message_handlert &);
2831

2932
void remove_instanceof(
3033
goto_functionst &goto_functions,
31-
symbol_table_baset &symbol_table);
34+
symbol_table_baset &symbol_table,
35+
message_handlert &);
3236

33-
void remove_instanceof(goto_modelt &model);
37+
void remove_instanceof(goto_modelt &model, message_handlert &);
3438

3539
#endif

jbmc/src/jbmc/jbmc_parse_options.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,7 @@ void jbmc_parse_optionst::process_goto_function(
755755
try
756756
{
757757
// Removal of RTTI inspection:
758-
remove_instanceof(goto_function, symbol_table);
758+
remove_instanceof(goto_function, symbol_table, get_message_handler());
759759
// Java virtual functions -> explicit dispatch tables:
760760
remove_virtual_functions(function);
761761

@@ -769,6 +769,7 @@ void jbmc_parse_optionst::process_goto_function(
769769
remove_exceptions(
770770
goto_function.body,
771771
symbol_table,
772+
get_message_handler(),
772773
remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
773774
}
774775

@@ -916,7 +917,9 @@ bool jbmc_parse_optionst::process_goto_functions(
916917
// remove catch and throw
917918
// (introduces instanceof but request it is removed)
918919
remove_exceptions(
919-
goto_model, remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
920+
goto_model,
921+
get_message_handler(),
922+
remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
920923

921924
// instrument library preconditions
922925
instrument_preconditions(goto_model);

jbmc/src/jdiff/jdiff_parse_options.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,9 +346,9 @@ bool jdiff_parse_optionst::process_goto_program(
346346
// Java virtual functions -> explicit dispatch tables:
347347
remove_virtual_functions(goto_model);
348348
// remove catch and throw
349-
remove_exceptions(goto_model);
349+
remove_exceptions(goto_model, get_message_handler());
350350
// Java instanceof -> clsid comparison:
351-
remove_instanceof(goto_model);
351+
remove_instanceof(goto_model, get_message_handler());
352352

353353
mm_io(goto_model);
354354

jbmc/unit/java_bytecode/java_replace_nondet/replace_nondet.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ TEST_CASE(
9595
goto_model_functiont model_function(
9696
symbol_table, functions, function_name, goto_function);
9797

98-
remove_instanceof(goto_function, symbol_table);
98+
remove_instanceof(goto_function, symbol_table, null_message_handler);
9999

100100
remove_virtual_functions(model_function);
101101

0 commit comments

Comments
 (0)