Skip to content

Commit 8226c5b

Browse files
authored
Merge pull request #1019 from cristina-david/feature/refactor-exception-instrumentation
Refactor runtime exception instrumentation
2 parents 3d41879 + 236e442 commit 8226c5b

37 files changed

+756
-129
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class ArrayIndexOutOfBoundsExceptionTest {
2+
public static void main(String args[]) {
3+
try {
4+
int[] a=new int[4];
5+
a[4]=0;
6+
throw new RuntimeException();
7+
}
8+
catch (ArrayIndexOutOfBoundsException exc) {
9+
assert false;
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
ArrayIndexOutOfBoundsExceptionTest.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^.*assertion at file ArrayIndexOutOfBoundsExceptionTest.java line 9 function.*: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring
265 Bytes
Binary file not shown.
265 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class ClassCastExceptionTest {
2+
public static void main(String args[]) {
3+
try {
4+
Object x = new Integer(0);
5+
String y = (String)x;
6+
throw new RuntimeException();
7+
}
8+
catch (ClassCastException exc) {
9+
assert false;
10+
}
11+
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
ClassCastExceptionTest.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^.*assertion at file ClassCastExceptionTest.java line 9 function.*: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring
249 Bytes
Binary file not shown.
234 Bytes
Binary file not shown.
234 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class A {}
2+
3+
class B extends A {}
4+
5+
class C extends B {}
6+
7+
public class ClassCastExceptionTest {
8+
public static void main(String args[]) {
9+
try {
10+
A c = new C();
11+
B b = (B)c;
12+
// TODO: an explicit throw is currently needed in order
13+
// for CBMC to convert the exception handler
14+
throw new RuntimeException();
15+
}
16+
catch (ClassCastException exc) {
17+
assert false;
18+
}
19+
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
ClassCastExceptionTest.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
247 Bytes
Binary file not shown.
241 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class B extends RuntimeException {}
2+
3+
class A {
4+
int i;
5+
}
6+
7+
public class Test {
8+
public static void main(String args[]) {
9+
A a=null;
10+
try {
11+
a.i=0;
12+
// TODO: an explicit throw is currently needed in order
13+
// for CBMC to convert the exception handler
14+
throw new B();
15+
}
16+
catch (NullPointerException exc) {
17+
assert false;
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
Test.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^.*assertion at file Test.java line 17 function.*: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring
247 Bytes
Binary file not shown.
241 Bytes
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class B extends RuntimeException {}
2+
3+
class A {
4+
int i;
5+
}
6+
7+
public class Test {
8+
public static void main(String args[]) {
9+
A a=null;
10+
try {
11+
int i=a.i;
12+
// TODO: an explicit throw is currently needed in order
13+
// for CBMC to convert the exception handler
14+
throw new B();
15+
}
16+
catch (NullPointerException exc) {
17+
assert false;
18+
}
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
Test.class
3+
--java-throw-runtime-exceptions
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^.*assertion at file Test.java line 17 function.*: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring

src/cbmc/cbmc_parse_options.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,7 @@ void cbmc_parse_optionst::help()
10701070
// NOLINTNEXTLINE(whitespace/line_length)
10711071
" --java-max-vla-length limit the length of user-code-created arrays\n"
10721072
// NOLINTNEXTLINE(whitespace/line_length)
1073+
" --java-throw-runtime-exceptions Make implicit runtime exceptions explicit"
10731074
" --java-cp-include-files regexp or JSON list of files to load (with '@' prefix)\n"
10741075
" --java-unwind-enum-static try to unwind loops in static initialization of enums\n"
10751076
"\n"

src/cbmc/cbmc_parse_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class optionst;
6565
"(graphml-witness):" \
6666
"(java-max-vla-length):(java-unwind-enum-static)" \
6767
"(java-cp-include-files):" \
68+
"(java-throw-runtime-exceptions)" \
6869
"(localize-faults)(localize-faults-method):" \
6970
"(lazy-methods)" \
7071
"(test-invariant-failure)" \

src/java_bytecode/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ SRC = bytecode_info.cpp \
55
jar_file.cpp \
66
java_bytecode_convert_class.cpp \
77
java_bytecode_convert_method.cpp \
8+
java_bytecode_instrument.cpp \
89
java_bytecode_internal_additions.cpp \
910
java_bytecode_language.cpp \
1011
java_bytecode_parse_tree.cpp \

src/java_bytecode/java_bytecode_convert_class.cpp

+5-36
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Author: Daniel Kroening, [email protected]
1818
#include "java_types.h"
1919
#include "java_bytecode_convert_method.h"
2020
#include "java_bytecode_language.h"
21+
#include "java_utils.h"
2122

2223
#include <util/arith_tools.h>
2324
#include <util/namespace.h>
@@ -56,7 +57,10 @@ class java_bytecode_convert_classt:public messaget
5657
string_preprocess.add_string_type(
5758
parse_tree.parsed_class.name, symbol_table);
5859
else if(!loading_success)
59-
generate_class_stub(parse_tree.parsed_class.name);
60+
generate_class_stub(
61+
parse_tree.parsed_class.name,
62+
symbol_table,
63+
get_message_handler());
6064
}
6165

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

76-
void generate_class_stub(const irep_idt &class_name);
7780
void add_array_types();
7881
};
7982

@@ -171,40 +174,6 @@ void java_bytecode_convert_classt::convert(const classt &c)
171174
java_root_class(*class_symbol);
172175
}
173176

174-
void java_bytecode_convert_classt::generate_class_stub(
175-
const irep_idt &class_name)
176-
{
177-
class_typet class_type;
178-
179-
class_type.set_tag(class_name);
180-
class_type.set(ID_base_name, class_name);
181-
182-
class_type.set(ID_incomplete_class, true);
183-
184-
// produce class symbol
185-
symbolt new_symbol;
186-
new_symbol.base_name=class_name;
187-
new_symbol.pretty_name=class_name;
188-
new_symbol.name="java::"+id2string(class_name);
189-
class_type.set(ID_name, new_symbol.name);
190-
new_symbol.type=class_type;
191-
new_symbol.mode=ID_java;
192-
new_symbol.is_type=true;
193-
194-
symbolt *class_symbol;
195-
196-
if(symbol_table.move(new_symbol, class_symbol))
197-
{
198-
warning() << "stub class symbol " << new_symbol.name
199-
<< " already exists" << eom;
200-
}
201-
else
202-
{
203-
// create the class identifier etc
204-
java_root_class(*class_symbol);
205-
}
206-
}
207-
208177
void java_bytecode_convert_classt::convert(
209178
symbolt &class_symbol,
210179
const fieldt &f)

0 commit comments

Comments
 (0)