Skip to content

[TG-1793] Complete instanceof for Java. #1716

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
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 added regression/cbmc-java/instanceof8/InstanceOf8.class
Binary file not shown.
14 changes: 14 additions & 0 deletions regression/cbmc-java/instanceof8/InstanceOf8.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class InstanceOf8 {
public static boolean test(Integer i) {
if (i instanceof Integer) {
return true;
} else {
return false;
}
}
public static void main(String[] args)
{
assert(!test(null));
assert(test(new Integer(1)));
}
}
7 changes: 7 additions & 0 deletions regression/cbmc-java/instanceof8/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CORE
InstanceOf8.class

^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
9 changes: 7 additions & 2 deletions src/goto-programs/remove_instanceof.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,20 @@ std::size_t remove_instanceoft::lower_instanceof(
newinst->source_location=this_inst->source_location;
newinst->function=this_inst->function;

// Replace the instanceof construct with a big-or.
// Replace the instanceof construct with a conjunction of non-null and the
// disjunction of all possible object types. According to the Java
// specification, null instanceof T is false for all possible values of T.
// (http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.20.2)
notequal_exprt non_null_expr(
check_ptr, null_pointer_exprt(to_pointer_type(check_ptr.type())));
exprt::operandst or_ops;
for(const auto &clsname : children)
{
constant_exprt clsexpr(clsname, string_typet());
equal_exprt test(newsym.symbol_expr(), clsexpr);
or_ops.push_back(test);
}
expr=disjunction(or_ops);
expr = and_exprt(non_null_expr, disjunction(or_ops));

return 1;
}
Expand Down