Skip to content

Commit d3d3cdb

Browse files
authored
Merge pull request #3288 from diffblue/remove_exceptions_ptr
simplify the remove_exceptions API
2 parents 7835a19 + 902cc69 commit d3d3cdb

File tree

5 files changed

+116
-62
lines changed

5 files changed

+116
-62
lines changed

jbmc/src/janalyzer/janalyzer_parse_options.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -673,7 +673,7 @@ bool janalyzer_parse_optionst::process_goto_program(const optionst &options)
673673

674674
// remove Java throw and catch
675675
// This introduces instanceof, so order is important:
676-
remove_exceptions(goto_model, nullptr, get_message_handler());
676+
remove_exceptions_using_instanceof(goto_model, get_message_handler());
677677

678678
// Java instanceof -> clsid comparison:
679679
class_hierarchyt class_hierarchy(goto_model.symbol_table);

jbmc/src/java_bytecode/remove_exceptions.cpp

+84-31
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ class remove_exceptionst
8787

8888
explicit remove_exceptionst(
8989
symbol_table_baset &_symbol_table,
90-
const class_hierarchyt *class_hierarchy,
90+
const class_hierarchyt *_class_hierarchy,
9191
function_may_throwt _function_may_throw,
92-
bool remove_added_instanceof,
93-
message_handlert &message_handler)
92+
bool _remove_added_instanceof,
93+
message_handlert &_message_handler)
9494
: symbol_table(_symbol_table),
95-
class_hierarchy(class_hierarchy),
95+
class_hierarchy(_class_hierarchy),
9696
function_may_throw(_function_may_throw),
97-
remove_added_instanceof(remove_added_instanceof),
98-
message_handler(message_handler)
97+
remove_added_instanceof(_remove_added_instanceof),
98+
message_handler(_message_handler)
9999
{
100100
if(remove_added_instanceof)
101101
{
@@ -590,27 +590,88 @@ void remove_exceptionst::operator()(goto_programt &goto_program)
590590
instrument_exceptions(goto_program);
591591
}
592592

593+
/// removes throws/CATCH-POP/CATCH-PUSH
594+
void remove_exceptions_using_instanceof(
595+
symbol_table_baset &symbol_table,
596+
goto_functionst &goto_functions,
597+
message_handlert &message_handler)
598+
{
599+
const namespacet ns(symbol_table);
600+
std::map<irep_idt, std::set<irep_idt>> exceptions_map;
601+
602+
uncaught_exceptions(goto_functions, ns, exceptions_map);
603+
604+
remove_exceptionst::function_may_throwt function_may_throw =
605+
[&exceptions_map](const irep_idt &id) {
606+
return !exceptions_map[id].empty();
607+
};
608+
609+
remove_exceptionst remove_exceptions(
610+
symbol_table, nullptr, function_may_throw, false, message_handler);
611+
612+
remove_exceptions(goto_functions);
613+
}
614+
615+
/// removes throws/CATCH-POP/CATCH-PUSH from a single GOTO program, replacing
616+
/// them with explicit exception propagation.
617+
/// Note this is somewhat less accurate than the whole-goto-model version,
618+
/// because we can't inspect other functions to determine whether they throw
619+
/// or not, and therefore must assume they do and always introduce post-call
620+
/// exception dispatch.
621+
/// \param goto_program: program to remove exceptions from
622+
/// \param symbol_table: global symbol table. The `@inflight_exception` global
623+
/// may be added if not already present. It will not be initialised; that is
624+
/// the caller's responsibility.
625+
/// \param message_handler: logging output
626+
void remove_exceptions_using_instanceof(
627+
goto_programt &goto_program,
628+
symbol_table_baset &symbol_table,
629+
message_handlert &message_handler)
630+
{
631+
remove_exceptionst::function_may_throwt any_function_may_throw =
632+
[](const irep_idt &) { return true; };
633+
634+
remove_exceptionst remove_exceptions(
635+
symbol_table, nullptr, any_function_may_throw, false, message_handler);
636+
637+
remove_exceptions(goto_program);
638+
}
639+
640+
/// removes throws/CATCH-POP/CATCH-PUSH, replacing them with explicit exception
641+
/// propagation.
642+
/// \param goto_model: model to remove exceptions from. The
643+
/// `@inflight_exception` global may be added to its symbol table if not
644+
/// already present. It will not be initialised; that is the caller's
645+
/// responsibility.
646+
/// \param message_handler: logging output
647+
void remove_exceptions_using_instanceof(
648+
goto_modelt &goto_model,
649+
message_handlert &message_handler)
650+
{
651+
remove_exceptions_using_instanceof(
652+
goto_model.symbol_table, goto_model.goto_functions, message_handler);
653+
}
654+
593655
/// removes throws/CATCH-POP/CATCH-PUSH
594656
void remove_exceptions(
595657
symbol_table_baset &symbol_table,
596-
const class_hierarchyt *class_hierarchy,
597658
goto_functionst &goto_functions,
598-
message_handlert &message_handler,
599-
remove_exceptions_typest type)
659+
const class_hierarchyt &class_hierarchy,
660+
message_handlert &message_handler)
600661
{
601662
const namespacet ns(symbol_table);
602663
std::map<irep_idt, std::set<irep_idt>> exceptions_map;
664+
603665
uncaught_exceptions(goto_functions, ns, exceptions_map);
666+
604667
remove_exceptionst::function_may_throwt function_may_throw =
605668
[&exceptions_map](const irep_idt &id) {
606669
return !exceptions_map[id].empty();
607670
};
671+
608672
remove_exceptionst remove_exceptions(
609-
symbol_table,
610-
class_hierarchy,
611-
function_may_throw,
612-
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF,
613-
message_handler);
673+
symbol_table, &class_hierarchy, function_may_throw, true, message_handler);
674+
614675
remove_exceptions(goto_functions);
615676
}
616677

@@ -627,25 +688,22 @@ void remove_exceptions(
627688
/// \param class_hierarchy: class hierarchy analysis of symbol_table.
628689
/// Only needed if type == REMOVE_ADDED_INSTANCEOF; otherwise may be null.
629690
/// \param message_handler: logging output
630-
/// \param type: specifies whether instanceof operations generated by this pass
631-
/// should be lowered to class-identifier comparisons (using
632-
/// remove_instanceof).
633691
void remove_exceptions(
634692
goto_programt &goto_program,
635693
symbol_table_baset &symbol_table,
636-
const class_hierarchyt *class_hierarchy,
637-
message_handlert &message_handler,
638-
remove_exceptions_typest type)
694+
const class_hierarchyt &class_hierarchy,
695+
message_handlert &message_handler)
639696
{
640697
remove_exceptionst::function_may_throwt any_function_may_throw =
641698
[](const irep_idt &) { return true; };
642699

643700
remove_exceptionst remove_exceptions(
644701
symbol_table,
645-
class_hierarchy,
702+
&class_hierarchy,
646703
any_function_may_throw,
647-
type == remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF,
704+
true,
648705
message_handler);
706+
649707
remove_exceptions(goto_program);
650708
}
651709

@@ -658,19 +716,14 @@ void remove_exceptions(
658716
/// \param class_hierarchy: class hierarchy analysis of symbol_table.
659717
/// Only needed if type == REMOVE_ADDED_INSTANCEOF; otherwise may be null.
660718
/// \param message_handler: logging output
661-
/// \param type: specifies whether instanceof operations generated by this pass
662-
/// should be lowered to class-identifier comparisons (using
663-
/// remove_instanceof).
664719
void remove_exceptions(
665720
goto_modelt &goto_model,
666-
const class_hierarchyt *class_hierarchy,
667-
message_handlert &message_handler,
668-
remove_exceptions_typest type)
721+
const class_hierarchyt &class_hierarchy,
722+
message_handlert &message_handler)
669723
{
670724
remove_exceptions(
671725
goto_model.symbol_table,
672-
class_hierarchy,
673726
goto_model.goto_functions,
674-
message_handler,
675-
type);
727+
class_hierarchy,
728+
message_handler);
676729
}

jbmc/src/java_bytecode/remove_exceptions.h

+27-21
Original file line numberDiff line numberDiff line change
@@ -23,28 +23,34 @@ Date: December 2016
2323
#define INFLIGHT_EXCEPTION_VARIABLE_NAME \
2424
"java::" INFLIGHT_EXCEPTION_VARIABLE_BASENAME
2525

26-
// Removes 'throw x' and CATCH-PUSH/CATCH-POP
27-
// and adds the required instrumentation (GOTOs and assignments)
28-
29-
enum class remove_exceptions_typest
30-
{
31-
REMOVE_ADDED_INSTANCEOF,
32-
DONT_REMOVE_INSTANCEOF,
33-
};
34-
26+
/// Removes 'throw x' and CATCH-PUSH/CATCH-POP
27+
/// and adds the required instrumentation (GOTOs and assignments)
28+
/// This introduces instanceof expressions.
29+
void remove_exceptions_using_instanceof(
30+
goto_programt &,
31+
symbol_table_baset &,
32+
message_handlert &);
33+
34+
/// Removes 'throw x' and CATCH-PUSH/CATCH-POP
35+
/// and adds the required instrumentation (GOTOs and assignments)
36+
/// This introduces instanceof expressions.
37+
void remove_exceptions_using_instanceof(goto_modelt &, message_handlert &);
38+
39+
/// Removes 'throw x' and CATCH-PUSH/CATCH-POP
40+
/// and adds the required instrumentation (GOTOs and assignments)
41+
/// This does not introduce instanceof expressions.
3542
void remove_exceptions(
36-
goto_programt &goto_program,
37-
symbol_table_baset &symbol_table,
38-
const class_hierarchyt *class_hierarchy,
39-
message_handlert &message_handler,
40-
remove_exceptions_typest type =
41-
remove_exceptions_typest::DONT_REMOVE_INSTANCEOF);
42-
43+
goto_programt &,
44+
symbol_table_baset &,
45+
const class_hierarchyt &,
46+
message_handlert &);
47+
48+
/// Removes 'throw x' and CATCH-PUSH/CATCH-POP
49+
/// and adds the required instrumentation (GOTOs and assignments)
50+
/// This does not introduce instanceof expressions.
4351
void remove_exceptions(
44-
goto_modelt &goto_model,
45-
const class_hierarchyt *class_hierarchy,
46-
message_handlert &message_handler,
47-
remove_exceptions_typest type =
48-
remove_exceptions_typest::DONT_REMOVE_INSTANCEOF);
52+
goto_modelt &,
53+
const class_hierarchyt &,
54+
message_handlert &);
4955

5056
#endif

jbmc/src/jbmc/jbmc_parse_options.cpp

+3-8
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,8 @@ void jbmc_parse_optionst::process_goto_function(
748748
remove_exceptions(
749749
goto_function.body,
750750
symbol_table,
751-
class_hierarchy.get(),
752-
get_message_handler(),
753-
remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
751+
*class_hierarchy.get(),
752+
get_message_handler());
754753
}
755754

756755
auto function_is_stub = [&symbol_table, &model](const irep_idt &id) {
@@ -891,12 +890,8 @@ bool jbmc_parse_optionst::process_goto_functions(
891890
return false;
892891

893892
// remove catch and throw
894-
// (introduces instanceof but request it is removed)
895893
remove_exceptions(
896-
goto_model,
897-
class_hierarchy.get(),
898-
get_message_handler(),
899-
remove_exceptions_typest::REMOVE_ADDED_INSTANCEOF);
894+
goto_model, *class_hierarchy.get(), get_message_handler());
900895

901896
// instrument library preconditions
902897
instrument_preconditions(goto_model);

jbmc/src/jdiff/jdiff_parse_options.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ bool jdiff_parse_optionst::process_goto_program(
354354

355355
// remove Java throw and catch
356356
// This introduces instanceof, so order is important:
357-
remove_exceptions(goto_model, nullptr, get_message_handler());
357+
remove_exceptions_using_instanceof(goto_model, get_message_handler());
358358

359359
// Java instanceof -> clsid comparison:
360360
class_hierarchyt class_hierarchy(goto_model.symbol_table);

0 commit comments

Comments
 (0)