Skip to content

Remove assumptions about exception handlers #996

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
25 changes: 25 additions & 0 deletions regression/cbmc-java/exceptions19/exception_handler.j
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.class exception_handler
.super java/lang/Object

.method public <init>()V
aload_0
invokevirtual java/lang/Object/<init>()V
return
.end method

.method public f()V
.limit stack 4
.limit locals 4

begin:
new java/lang/Exception
dup
invokespecial java/lang/Exception.<init>()V
athrow
start:
dup
end:
astore_1
return
.catch all from begin to end using start
.end method
8 changes: 8 additions & 0 deletions regression/cbmc-java/exceptions19/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
exception_handler.class
--function exception_handler.f
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
4 changes: 2 additions & 2 deletions src/goto-programs/remove_exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ void remove_exceptionst::add_exceptional_returns(
if(goto_program.empty())
return;

// the entry function has already been added to the symbol table
// if you find it, initialise it
// some methods (e.g. the entry method) have already been added to
// the symbol table; if you find it, initialise it
if(symbol_table.has_symbol(id2string(function_id)+EXC_SUFFIX))
{
const symbolt &symbol=
Expand Down
61 changes: 21 additions & 40 deletions src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
#include <linking/zero_initializer.h>

#include <goto-programs/cfg.h>
#include <goto-programs/remove_exceptions.h>
#include <analyses/cfg_dominators.h>

#include "java_bytecode_convert_method.h"
Expand Down Expand Up @@ -472,7 +473,8 @@ void java_bytecode_convert_methodt::convert(

tmp_vars.clear();
if((!m.is_abstract) && (!m.is_native))
method_symbol.value=convert_instructions(m, code_type);
method_symbol.value=convert_instructions(
m, code_type, method_symbol.name);

// Replace the existing stub symbol with the real deal:
const auto s_it=symbol_table.symbols.find(method.get_name());
Expand Down Expand Up @@ -1119,7 +1121,8 @@ Function: java_bytecode_convert_methodt::convert_instructions

codet java_bytecode_convert_methodt::convert_instructions(
const methodt &method,
const code_typet &method_type)
const code_typet &method_type,
const irep_idt &method_name)
{
const instructionst &instructions=method.instructions;

Expand Down Expand Up @@ -1305,48 +1308,29 @@ codet java_bytecode_convert_methodt::convert_instructions(
statement=std::string(id2string(statement), 0, statement.size()-2);
}

// we throw away the first statement in an exception handler
// as we don't know if a function call had a normal or exceptional return
auto it=method.exception_table.begin();
for(; it!=method.exception_table.end(); ++it)
{
if(cur_pc==it->handler_pc)
{
exprt exc_var=variable(
arg0, statement[0],
i_it->address,
NO_CAST);

// throw away the operands
pop_residue(bytecode_info.pop);

// add a CATCH-PUSH signaling a handler
side_effect_expr_catcht catch_handler_expr;
// pack the exception variable so that it can be used
// later for instrumentation
catch_handler_expr.get_sub().resize(1);
catch_handler_expr.get_sub()[0]=exc_var;

code_expressiont catch_handler(catch_handler_expr);
code_labelt newlabel(label(std::to_string(cur_pc)),
code_blockt());

code_blockt label_block=to_code_block(newlabel.code());
code_blockt handler_block;
handler_block.move_to_operands(c);
handler_block.move_to_operands(catch_handler);
handler_block.move_to_operands(label_block);
c=handler_block;
break;
// at the beginning of a handler, clear the stack and
// push the corresponding exceptional return variable
stack.clear();
auxiliary_symbolt new_symbol;
new_symbol.is_static_lifetime=true;
// generate the name of the exceptional return variable
const std::string &exceptional_var_name=
id2string(method_name)+
EXC_SUFFIX;
new_symbol.base_name=exceptional_var_name;
new_symbol.name=exceptional_var_name;
new_symbol.type=typet(ID_pointer, empty_typet());
new_symbol.mode=ID_java;
symbol_table.add(new_symbol);
stack.push_back(new_symbol.symbol_expr());
}
}

if(it!=method.exception_table.end())
{
// go straight to the next statement
continue;
}

exprt::operandst op=pop(bytecode_info.pop);
exprt::operandst results;
results.resize(bytecode_info.push, nil_exprt());
Expand Down Expand Up @@ -2533,9 +2517,7 @@ codet java_bytecode_convert_methodt::convert_instructions(
address_mapt::iterator a_it2=address_map.find(address);
assert(a_it2!=address_map.end());

// we don't worry about exception handlers as we don't load the
// operands from the stack anyway -- we keep explicit global
// exception variables
// clear the stack if this is an exception handler
for(const auto &exception_row : method.exception_table)
{
if(address==exception_row.handler_pc)
Expand Down Expand Up @@ -2604,7 +2586,6 @@ codet java_bytecode_convert_methodt::convert_instructions(
c.copy_to_operands(*o_it);
}
}

a_it2->second.stack=stack;
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/java_bytecode/java_bytecode_convert_method_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ class java_bytecode_convert_methodt:public messaget

codet convert_instructions(
const methodt &,
const code_typet &);
const code_typet &,
const irep_idt &);

const bytecode_infot &get_bytecode_info(const irep_idt &statement);

Expand Down
19 changes: 11 additions & 8 deletions src/java_bytecode/java_entry_point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -589,14 +589,17 @@ bool java_entry_point(
call_main.lhs()=return_symbol.symbol_expr();
}

// add the exceptional return value
auxiliary_symbolt exc_symbol;
exc_symbol.mode=ID_C;
exc_symbol.is_static_lifetime=false;
exc_symbol.name=id2string(symbol.name)+EXC_SUFFIX;
exc_symbol.base_name=id2string(symbol.name)+EXC_SUFFIX;
exc_symbol.type=typet(ID_pointer, empty_typet());
symbol_table.add(exc_symbol);
if(!symbol_table.has_symbol(id2string(symbol.name)+EXC_SUFFIX))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This generates a different symbol name than the catch handler in java_bytecode_convert_method.cpp if the main class does not have the same name as the file, e.g.:

class Ex extends Throwable {}

class test2 {
  public static void main() {
   try {
     throw new Ex();
   }
   catch(Ex ex) { }
  }
}

public class test {
 public static void main (String arg[]) {
 }
}

When calling CBMC with --function test2.main, this will produce "java::test2.main:()V#exception_value" and "java::test.main:()V#exception_value" in java_bytecode_convert_method.cpp. This could probably be solved by using the class name in java_bytecode_convert_method.cpp.

{
// add the exceptional return value
auxiliary_symbolt exc_symbol;
exc_symbol.mode=ID_C;
exc_symbol.is_static_lifetime=true;
exc_symbol.name=id2string(symbol.name)+EXC_SUFFIX;
exc_symbol.base_name=id2string(symbol.name)+EXC_SUFFIX;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

  exc_symbol.base_name=exc_symbol.name;

exc_symbol.type=typet(ID_pointer, empty_typet());
symbol_table.add(exc_symbol);
}

exprt::operandst main_arguments=
java_build_arguments(
Expand Down