Skip to content

Commit

Permalink
Throw ArithmeticException whenever a division-by-zero is encountered
Browse files Browse the repository at this point in the history
  • Loading branch information
cristina-david authored and smowton committed Aug 7, 2017
1 parent 3bf46bf commit 6ed24e5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/java_bytecode/java_bytecode_convert_method.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,10 @@ codet java_bytecode_convert_methodt::convert_instructions(
i_it->statement=="checkcast" ||
i_it->statement=="newarray" ||
i_it->statement=="anewarray" ||
i_it->statement=="idiv" ||
i_it->statement=="ldiv" ||
i_it->statement=="irem" ||
i_it->statement=="lrem" ||
i_it->statement==patternt("?astore") ||
i_it->statement==patternt("?aload") ||
i_it->statement=="invokestatic" ||
Expand Down
36 changes: 36 additions & 0 deletions src/java_bytecode/java_bytecode_instrument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class java_bytecode_instrumentt:public messaget
const exprt &idx,
const source_locationt &original_loc);

codet check_arithmetic_exception(
const exprt &denominator,
const source_locationt &original_loc);

codet check_null_dereference(
const exprt &expr,
const source_locationt &original_loc,
Expand Down Expand Up @@ -133,6 +137,30 @@ codet java_bytecode_instrumentt::throw_exception(
return init_code;
}


/// Checks whether there is a division by zero
/// and throws ArithmeticException if necessary.
/// Exceptions are thrown when the `throw_runtime_exceptions`
/// flag is set.
/// \return Based on the value of the flag `throw_runtime_exceptions`,
/// it returns code that either throws an ArithmeticException
/// or is a skip
codet java_bytecode_instrumentt::check_arithmetic_exception(
const exprt &denominator,
const source_locationt &original_loc)
{
const constant_exprt &zero=from_integer(0, denominator.type());
const binary_relation_exprt equal_zero(denominator, ID_equal, zero);

if(throw_runtime_exceptions)
return throw_exception(
equal_zero,
original_loc,
"java.lang.ArithmeticException");

return code_skipt();
}

/// Checks whether the array access array_struct[idx] is out-of-bounds,
/// and throws ArrayIndexOutofBoundsException/generates an assertion
/// if necessary; Exceptions are thrown when the `throw_runtime_exceptions`
Expand Down Expand Up @@ -464,6 +492,14 @@ codet java_bytecode_instrumentt::instrument_expr(
expr.op0(),
expr.source_location());
}
else if((expr.id()==ID_div || expr.id()==ID_mod) &&
expr.type().id()==ID_signedbv)
{
// check division by zero (for integer types only)
return check_arithmetic_exception(
expr.op1(),
expr.source_location());
}
else if(expr.id()==ID_member &&
expr.get_bool(ID_java_member_access))
{
Expand Down

0 comments on commit 6ed24e5

Please sign in to comment.