-
Notifications
You must be signed in to change notification settings - Fork 273
Throw ArithmeticException whenever a division-by-zero is encountered #950
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
Throw ArithmeticException whenever a division-by-zero is encountered #950
Conversation
f6f4064
to
f74e4ba
Compare
exc_symbol.name="ArithmeticException"; | ||
exc_symbol.mode=ID_java; | ||
exc_symbol.type=typet(ID_pointer, empty_typet()); | ||
symbol_table.add(exc_symbol); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure this shouldn't have some class type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm now using a class stub. Soon, we should have a proper model so that we can capture the class hierarchy (for now, this wouldn't be caught by a handler for, say, RuntimeException).
@@ -1933,6 +1979,7 @@ codet java_bytecode_convert_methodt::convert_instructions( | |||
{ | |||
assert(op.size()==2 && results.size()==1); | |||
results[0]=div_exprt(op[0], op[1]); | |||
c=throw_arithmetic_exception(op[1], i_it->source_location); | |||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't seem to be the right place for this -- this should happen during conversion from program into goto-program.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would think that this is a better place than the conversion to goto-program as the exception being thrown is language dependent. I assume we don't want to throw an exception for all input languages?
3ef77a1
to
85dd6b5
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suspect object-factory needs replacing with a real constructor
{ | ||
symbolt exc_symbol; | ||
|
||
if(!symbol_table.has_symbol("java.lang.ArithmeticException")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Factor with java_bytecode_convert_classt::generate_class_stub
?
|
||
code_blockt init_code; | ||
// create the exception object | ||
auto exc=object_factory( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we really nondet init it rather than calling a particular constructor?
|
||
\*******************************************************************/ | ||
|
||
codet java_bytecode_convert_methodt::throw_arithmetic_exception( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider adding an annotation here and then moving this to your existing instrumentation pass
Once #1019 is merged, this PR will need some major changes as the instrumentation added here also needs to be refactored out of bytecode convert. |
85dd6b5
to
dd811f1
Compare
dd811f1
to
6131121
Compare
Tested this with test case:
I found that the way of initialising the exception object meant it could get thrown without being initialised. Made a couple of improvements here: https://github.com/smowton/cbmc/commits/smowton/feature/exceptions_improved |
Corresponding test-gen PR: https://github.com/diffblue/test-gen/pull/750 |
@@ -1051,6 +1051,7 @@ codet java_bytecode_convert_methodt::convert_instructions( | |||
i_it->statement=="checkcast" || | |||
i_it->statement=="newarray" || | |||
i_it->statement=="anewarray" || | |||
i_it->statement==patternt("?div") || |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
only idiv
and ldiv
throw ArithmeticException
, fdiv
and ddiv
do not. In addition irem
and lrem
throw, too.
const source_locationt &original_loc) | ||
{ | ||
symbolt exc_symbol; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as only integral div-by-zero throws, maybe add invariant for java_int_type
or java_long_type
@@ -463,6 +493,13 @@ codet java_bytecode_instrumentt::instrument_expr( | |||
expr.op0(), | |||
expr.source_location()); | |||
} | |||
else if(expr.id()==ID_div) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
needs ID_mod
, too, ID_rem
is for float/double, if I understood correctly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@smowton, there are comments to address here.
I know, I'm currently working on resolving the remaining exception issues, which might subsume this in any event |
This is best merged after PR #912 which introduces the initial instrumentation for throwing runtime exceptions.