83 typedef std::vector<std::pair<
94 bool _remove_added_instanceof,
106 "remove_exceptions needs a class hierarchy to remove instanceof "
107 "statements (either supply one, or don't use REMOVE_ADDED_INSTANCEOF)");
141 std::size_t &universal_try,
142 std::size_t &universal_catch);
145 const irep_idt &function_identifier,
149 const std::vector<symbol_exprt> &locals);
152 const irep_idt &function_identifier,
156 const std::vector<symbol_exprt> &);
159 const irep_idt &function_identifier,
163 const std::vector<symbol_exprt> &);
166 const irep_idt &function_identifier,
174 const symbolt *existing_symbol =
177 existing_symbol !=
nullptr,
178 "Java frontend should have created @inflight_exception variable");
190 for(
const auto &instruction : goto_program.
instructions)
192 if(instruction.is_throw())
197 if(instruction.is_function_call())
199 const exprt &function_expr = instruction.call_function();
201 function_expr.
id()==ID_symbol,
202 "identifier expected to be a symbol");
233 const exprt &thrown_exception_local =
253 thrown_exception_local,
258 instr_it->turn_into_skip();
285 std::size_t &universal_try,
286 std::size_t &universal_catch)
288 for(std::size_t i=stack_catch.size(); i>0;)
291 for(std::size_t j=0; j<stack_catch[i].size(); ++j)
293 if(stack_catch[i][j].first.empty())
302 return stack_catch[i][j].second;
321 const irep_idt &function_identifier,
325 const std::vector<symbol_exprt> &locals)
335 std::size_t default_try=0;
336 std::size_t default_catch=(!stack_catch.empty()) ? stack_catch[0].size() : 0;
340 default_try, default_catch);
348 for(std::size_t i=default_try; i<stack_catch.size(); i++)
350 for(std::size_t j=(i==default_try) ? default_catch : stack_catch[i].size();
355 stack_catch[i][j].second;
356 if(!stack_catch[i][j].first.empty())
387 for(
const auto &local : locals)
397 const irep_idt &function_identifier,
401 const std::vector<symbol_exprt> &locals)
405 const exprt &exc_expr =
409 function_identifier, goto_program, instr_it, stack_catch, locals);
428 const irep_idt &function_identifier,
432 const std::vector<symbol_exprt> &locals)
440 const auto &
function = instr_it->call_function();
443 function.
id() == ID_symbol,
"function call expected to be a symbol");
465 function_identifier, goto_program, instr_it, stack_catch, locals);
472 no_exception_currently_in_flight,
486 const irep_idt &function_identifier,
490 std::vector<std::vector<symbol_exprt>> stack_locals;
491 std::vector<symbol_exprt> locals;
493 if(goto_program.
empty())
496 bool program_or_callees_may_throw =
499 bool did_something =
false;
500 bool added_goto_instruction =
false;
504 if(instr_it->is_decl())
506 locals.push_back(instr_it->decl_symbol());
509 else if(instr_it->type() ==
CATCH)
511 const irep_idt &statement = instr_it->code().get_statement();
513 if(statement==ID_exception_landingpad)
516 goto_program, instr_it, program_or_callees_may_throw);
519 else if(statement==ID_pop_catch)
522 if(!stack_locals.empty())
524 locals=stack_locals.back();
525 stack_locals.pop_back();
528 if(!stack_catch.empty())
530 stack_catch.pop_back();
535 std::cout <<
"Remove exceptions: empty stack\n";
540 else if(statement==ID_push_catch)
542 stack_locals.push_back(locals);
546 stack_catch.push_back(exception);
558 instr_it->targets.empty() ||
559 exception_list.size()==instr_it->targets.size(),
560 "`exception_list` should contain current instruction's targets");
564 for(
auto target : instr_it->targets)
566 last_exception.push_back(
567 std::make_pair(exception_list[i].
get_tag(), target));
575 "CATCH opcode should be one of push-catch, pop-catch, landingpad");
578 instr_it->turn_into_skip();
579 did_something =
true;
581 else if(instr_it->type() ==
THROW)
584 function_identifier, goto_program, instr_it, stack_catch, locals);
590 function_identifier, goto_program, instr_it, stack_catch, locals);
592 added_goto_instruction =
629 std::map<irep_idt, std::set<irep_idt>> exceptions_map;
634 [&exceptions_map](
const irep_idt &id) {
635 return !exceptions_map[id].empty();
639 symbol_table,
nullptr, function_may_throw,
false, message_handler);
657 const irep_idt &function_identifier,
663 [](
const irep_idt &) {
return true; };
666 symbol_table,
nullptr, any_function_may_throw,
false, message_handler);
694 std::map<irep_idt, std::set<irep_idt>> exceptions_map;
699 [&exceptions_map](
const irep_idt &id) {
700 return !exceptions_map[id].empty();
704 symbol_table, &class_hierarchy, function_may_throw,
true, message_handler);
724 const irep_idt &function_identifier,
731 [](
const irep_idt &) {
return true; };
736 any_function_may_throw,
pointer_typet pointer_type(const typet &subtype)
Non-graph-based representation of the class hierarchy.
A goto_instruction_codet representing an assignment in the program.
const exprt & catch_expr() const
std::vector< exception_list_entryt > exception_listt
exception_listt & exception_list()
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Base class for all expressions.
const source_locationt & source_location() const
typet & type()
Return the type of the expression.
A collection of goto functions.
function_mapt function_map
symbol_tablet symbol_table
Symbol table.
goto_functionst goto_functions
GOTO functions.
A generic container class for the GOTO intermediate representation of one function.
static instructiont make_assumption(const exprt &g, const source_locationt &l=source_locationt::nil())
instructionst instructions
The list of instructions in the goto program.
static instructiont make_dead(const symbol_exprt &symbol, const source_locationt &l=source_locationt::nil())
instructionst::iterator targett
static instructiont make_assignment(const code_assignt &_code, const source_locationt &l=source_locationt::nil())
Create an assignment instruction.
targett get_end_function()
Get an instruction iterator pointing to the END_FUNCTION instruction of the goto program.
targett insert_after(const_targett target)
Insertion after the instruction pointed-to by the given instruction iterator target.
static instructiont make_goto(targett _target, const source_locationt &l=source_locationt::nil())
bool empty() const
Is the program empty?
bool get_bool(const irep_idt &name) const
const irep_idt & id() const
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
The null pointer constant.
Lowers high-level exception descriptions into low-level operations suitable for symex and other analy...
const class_hierarchyt * class_hierarchy
std::vector< catch_handlerst > stack_catcht
function_may_throwt function_may_throw
symbol_exprt get_inflight_exception_global()
Create a global named java::@inflight_exception that holds any exception that has been thrown but not...
void add_exception_dispatch_sequence(const irep_idt &function_identifier, goto_programt &goto_program, const goto_programt::targett &instr_it, const stack_catcht &stack_catch, const std::vector< symbol_exprt > &locals)
Emit the code: if (exception instanceof ExnA) then goto handlerA else if (exception instanceof ExnB) ...
message_handlert & message_handler
void instrument_exception_handler(goto_programt &goto_program, const goto_programt::targett &, bool may_catch)
Translates an exception landing-pad into instructions that copy the in-flight exception pointer to a ...
@ ADDED_CODE_WITHOUT_MAY_THROW
@ ADDED_CODE_WITH_MAY_THROW
std::vector< std::pair< irep_idt, goto_programt::targett > > catch_handlerst
bool function_or_callees_may_throw(const goto_programt &) const
Checks whether a function may ever experience an exception (whether or not it catches),...
goto_programt::targett find_universal_exception(const remove_exceptionst::stack_catcht &stack_catch, goto_programt &goto_program, std::size_t &universal_try, std::size_t &universal_catch)
Find the innermost universal exception handler for the current program location which may throw (i....
remove_exceptionst(symbol_table_baset &_symbol_table, const class_hierarchyt *_class_hierarchy, function_may_throwt _function_may_throw, bool _remove_added_instanceof, message_handlert &_message_handler)
std::function< bool(const irep_idt &)> function_may_throwt
instrumentation_resultt instrument_function_call(const irep_idt &function_identifier, goto_programt &goto_program, const goto_programt::targett &, const stack_catcht &, const std::vector< symbol_exprt > &)
instruments each function call that may escape exceptions with conditional GOTOS to the corresponding...
bool instrument_throw(const irep_idt &function_identifier, goto_programt &goto_program, const goto_programt::targett &, const stack_catcht &, const std::vector< symbol_exprt > &)
instruments each throw with conditional GOTOS to the corresponding exception handlers
void instrument_exceptions(const irep_idt &function_identifier, goto_programt &goto_program)
instruments throws, function calls that may escape exceptions and exception handlers.
bool remove_added_instanceof
symbol_table_baset & symbol_table
void operator()(goto_functionst &goto_functions)
A struct tag type, i.e., struct_typet with an identifier.
Expression to hold a symbol (variable)
const irep_idt & get_identifier() const
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 symbolt & lookup_ref(const irep_idt &name) const
Find a symbol in the symbol table for read-only access.
class symbol_exprt symbol_expr() const
Produces a symbol_exprt for a symbol.
typet type
Type of symbol.
The Boolean constant true.
Semantic type conversion.
static exprt get_exception_symbol(const exprt &exor)
Returns the symbol corresponding to an exception.
#define Forall_goto_program_instructions(it, program)
Java-specific exprt subclasses.
static irep_idt get_tag(const typet &type)
empty_typet java_void_type()
API to expression classes for Pointers.
void remove_exceptions_using_instanceof(symbol_table_baset &symbol_table, goto_functionst &goto_functions, message_handlert &message_handler)
removes throws/CATCH-POP/CATCH-PUSH
void remove_exceptions(symbol_table_baset &symbol_table, goto_functionst &goto_functions, const class_hierarchyt &class_hierarchy, message_handlert &message_handler)
removes throws/CATCH-POP/CATCH-PUSH
Remove function exceptional returns.
#define INFLIGHT_EXCEPTION_VARIABLE_NAME
void remove_instanceof(const irep_idt &function_identifier, goto_programt::targett target, goto_programt &goto_program, symbol_table_baset &symbol_table, const class_hierarchyt &class_hierarchy, message_handlert &message_handler)
Replace an instanceof in the expression or guard of the passed instruction of the given function body...
Remove Instance-of Operators.
void remove_skip(goto_programt &goto_program, goto_programt::targett begin, goto_programt::targett end)
remove unnecessary skip statements
#define DATA_INVARIANT(CONDITION, REASON)
This condition should be used to document that assumptions that are made on goto_functions,...
#define PRECONDITION(CONDITION)
#define INITIALIZE_FUNCTION
static code_landingpadt & to_code_landingpad(codet &code)
static code_push_catcht & to_code_push_catch(codet &code)
const symbol_exprt & to_symbol_expr(const exprt &expr)
Cast an exprt to a symbol_exprt.
void uncaught_exceptions(const goto_functionst &goto_functions, const namespacet &ns, std::map< irep_idt, std::set< irep_idt >> &exceptions_map)
Applies the uncaught exceptions analysis and outputs the result.
Over-approximative uncaught exceptions analysis.