Skip to content

Refactor runtime exception instrumentation #1019

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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
public class ArrayIndexOutOfBoundsExceptionTest {
Copy link
Contributor

Choose a reason for hiding this comment

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

Dodgy indentation, use spaces everywhere

public static void main(String args[]) {
try {
int[] a=new int[4];
a[4]=0;
throw new RuntimeException();
}
catch (ArrayIndexOutOfBoundsException exc) {
assert false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
ArrayIndexOutOfBoundsExceptionTest.class
--java-throw-runtime-exceptions
^EXIT=10$
^SIGNAL=0$
^.*assertion at file ArrayIndexOutOfBoundsExceptionTest.java line 9 function.*: FAILURE$
^VERIFICATION FAILED$
--
^warning: ignoring
Binary file added regression/cbmc-java/ClassCastException1/A.class
Binary file not shown.
Binary file added regression/cbmc-java/ClassCastException1/B.class
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class ClassCastExceptionTest {
public static void main(String args[]) {
try {
Object x = new Integer(0);
String y = (String)x;
throw new RuntimeException();
}
catch (ClassCastException exc) {
assert false;
}

}
}
9 changes: 9 additions & 0 deletions regression/cbmc-java/ClassCastException1/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
ClassCastExceptionTest.class
--java-throw-runtime-exceptions
^EXIT=10$
^SIGNAL=0$
^.*assertion at file ClassCastExceptionTest.java line 9 function.*: FAILURE$
^VERIFICATION FAILED$
--
^warning: ignoring
Binary file added regression/cbmc-java/ClassCastException2/A.class
Binary file not shown.
Binary file added regression/cbmc-java/ClassCastException2/B.class
Binary file not shown.
Binary file added regression/cbmc-java/ClassCastException2/C.class
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class A {}

class B extends A {}

class C extends B {}

public class ClassCastExceptionTest {
public static void main(String args[]) {
try {
A c = new C();
B b = (B)c;
// TODO: an explicit throw is currently needed in order
// for CBMC to convert the exception handler
throw new RuntimeException();
}
catch (ClassCastException exc) {
assert false;
}

}
}
8 changes: 8 additions & 0 deletions regression/cbmc-java/ClassCastException2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
Copy link
Contributor

Choose a reason for hiding this comment

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

Aren't tests CCE1 and CCE2 identical?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, they somehow were, thanks for noticing that. I've now replaced CCE2 by the one you suggest below (with C<:B<:A), if I understood it correctly.

ClassCastExceptionTest.class
--java-throw-runtime-exceptions
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
Binary file added regression/cbmc-java/NullPointerException2/A.class
Binary file not shown.
Binary file added regression/cbmc-java/NullPointerException2/B.class
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions regression/cbmc-java/NullPointerException2/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class B extends RuntimeException {}

class A {
int i;
}

public class Test {
public static void main(String args[]) {
A a=null;
try {
a.i=0;
// TODO: an explicit throw is currently needed in order
// for CBMC to convert the exception handler
throw new B();
}
catch (NullPointerException exc) {
assert false;
}
}
}
9 changes: 9 additions & 0 deletions regression/cbmc-java/NullPointerException2/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
Test.class
--java-throw-runtime-exceptions
^EXIT=10$
^SIGNAL=0$
^.*assertion at file Test.java line 17 function.*: FAILURE$
^VERIFICATION FAILED$
--
^warning: ignoring
Binary file added regression/cbmc-java/NullPointerException3/A.class
Binary file not shown.
Binary file added regression/cbmc-java/NullPointerException3/B.class
Binary file not shown.
Binary file not shown.
20 changes: 20 additions & 0 deletions regression/cbmc-java/NullPointerException3/Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class B extends RuntimeException {}

class A {
Copy link
Contributor

Choose a reason for hiding this comment

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

How does this test differ from NPE2?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

NPE2 uses putfield, whereas NPE3 uses getfield. They ultimately rely on the same code but I still wanted to check they are both good.

int i;
}

public class Test {
public static void main(String args[]) {
A a=null;
try {
int i=a.i;
// TODO: an explicit throw is currently needed in order
// for CBMC to convert the exception handler
throw new B();
}
catch (NullPointerException exc) {
assert false;
}
}
}
9 changes: 9 additions & 0 deletions regression/cbmc-java/NullPointerException3/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
Test.class
--java-throw-runtime-exceptions
^EXIT=10$
^SIGNAL=0$
^.*assertion at file Test.java line 17 function.*: FAILURE$
^VERIFICATION FAILED$
--
^warning: ignoring
1 change: 1 addition & 0 deletions src/cbmc/cbmc_parse_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ void cbmc_parse_optionst::help()
// NOLINTNEXTLINE(whitespace/line_length)
" --java-max-vla-length limit the length of user-code-created arrays\n"
// NOLINTNEXTLINE(whitespace/line_length)
" --java-throw-runtime-exceptions Make implicit runtime exceptions explicit"
" --java-cp-include-files regexp or JSON list of files to load (with '@' prefix)\n"
" --java-unwind-enum-static try to unwind loops in static initialization of enums\n"
"\n"
Expand Down
1 change: 1 addition & 0 deletions src/cbmc/cbmc_parse_options.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class optionst;
"(graphml-witness):" \
"(java-max-vla-length):(java-unwind-enum-static)" \
"(java-cp-include-files):" \
"(java-throw-runtime-exceptions)" \
"(localize-faults)(localize-faults-method):" \
"(lazy-methods)" \
"(test-invariant-failure)" \
Expand Down
1 change: 1 addition & 0 deletions src/java_bytecode/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SRC = bytecode_info.cpp \
jar_file.cpp \
java_bytecode_convert_class.cpp \
java_bytecode_convert_method.cpp \
java_bytecode_instrument.cpp \
java_bytecode_internal_additions.cpp \
java_bytecode_language.cpp \
java_bytecode_parse_tree.cpp \
Expand Down
41 changes: 5 additions & 36 deletions src/java_bytecode/java_bytecode_convert_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
#include "java_types.h"
#include "java_bytecode_convert_method.h"
#include "java_bytecode_language.h"
#include "java_utils.h"

#include <util/arith_tools.h>
#include <util/namespace.h>
Expand Down Expand Up @@ -56,7 +57,10 @@ class java_bytecode_convert_classt:public messaget
string_preprocess.add_string_type(
parse_tree.parsed_class.name, symbol_table);
else if(!loading_success)
generate_class_stub(parse_tree.parsed_class.name);
generate_class_stub(
parse_tree.parsed_class.name,
symbol_table,
get_message_handler());
}

typedef java_bytecode_parse_treet::classt classt;
Expand All @@ -73,7 +77,6 @@ class java_bytecode_convert_classt:public messaget
void convert(const classt &c);
void convert(symbolt &class_symbol, const fieldt &f);

void generate_class_stub(const irep_idt &class_name);
void add_array_types();
};

Expand Down Expand Up @@ -171,40 +174,6 @@ void java_bytecode_convert_classt::convert(const classt &c)
java_root_class(*class_symbol);
}

void java_bytecode_convert_classt::generate_class_stub(
const irep_idt &class_name)
{
class_typet class_type;

class_type.set_tag(class_name);
class_type.set(ID_base_name, class_name);

class_type.set(ID_incomplete_class, true);

// produce class symbol
symbolt new_symbol;
new_symbol.base_name=class_name;
new_symbol.pretty_name=class_name;
new_symbol.name="java::"+id2string(class_name);
class_type.set(ID_name, new_symbol.name);
new_symbol.type=class_type;
new_symbol.mode=ID_java;
new_symbol.is_type=true;

symbolt *class_symbol;

if(symbol_table.move(new_symbol, class_symbol))
{
warning() << "stub class symbol " << new_symbol.name
<< " already exists" << eom;
}
else
{
// create the class identifier etc
java_root_class(*class_symbol);
}
}

void java_bytecode_convert_classt::convert(
symbolt &class_symbol,
const fieldt &f)
Expand Down
Loading