Skip to content

Commit 5f8dcfb

Browse files
Parses the exception attribute
4.7.5. The Exceptions Attribute
1 parent b6f9aa5 commit 5f8dcfb

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

jbmc/src/java_bytecode/java_bytecode_parse_tree.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ struct java_bytecode_parse_treet
114114
typedef std::vector<exceptiont> exception_tablet;
115115
exception_tablet exception_table;
116116

117+
std::vector<irep_idt> throws_exception_table;
118+
117119
struct local_variablet
118120
{
119121
irep_idt name;

jbmc/src/java_bytecode/java_bytecode_parser.cpp

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class java_bytecode_parsert:public parsert
121121
void rmethod(classt &parsed_class);
122122
void
123123
rinner_classes_attribute(classt &parsed_class, const u4 &attribute_length);
124+
std::vector<irep_idt> rexceptions_attribute();
124125
void rclass_attribute(classt &parsed_class);
125126
void rRuntimeAnnotation_attribute(annotationst &);
126127
void rRuntimeAnnotation(annotationt &);
@@ -151,7 +152,7 @@ class java_bytecode_parsert:public parsert
151152
}
152153
}
153154

154-
u8 read_bytes(size_t bytes)
155+
u8 read_bytes(size_t bytes) const
155156
{
156157
u8 result=0;
157158
for(size_t i=0; i<bytes; i++)
@@ -167,22 +168,22 @@ class java_bytecode_parsert:public parsert
167168
return result;
168169
}
169170

170-
u1 read_u1()
171+
u1 read_u1() const
171172
{
172173
return (u1)read_bytes(1);
173174
}
174175

175-
inline u2 read_u2()
176+
inline u2 read_u2() const
176177
{
177178
return (u2)read_bytes(2);
178179
}
179180

180-
u4 read_u4()
181+
u4 read_u4() const
181182
{
182183
return (u4)read_bytes(4);
183184
}
184185

185-
u8 read_u8()
186+
u8 read_u8() const
186187
{
187188
return read_bytes(8);
188189
}
@@ -1242,6 +1243,10 @@ void java_bytecode_parsert::rmethod_attribute(methodt &method)
12421243
{
12431244
rRuntimeAnnotation_attribute(method.annotations);
12441245
}
1246+
else if(attribute_name == "Exceptions")
1247+
{
1248+
method.throws_exception_table = rexceptions_attribute();
1249+
}
12451250
else
12461251
skip_bytes(attribute_length);
12471252
}
@@ -1654,6 +1659,26 @@ void java_bytecode_parsert::rinner_classes_attribute(
16541659
}
16551660
}
16561661

1662+
/// Corresponds to the element_value structure
1663+
/// Described in Java 8 specification 4.7.5
1664+
/// https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.7.5
1665+
/// Parses the Exceptions attribute for the current method,
1666+
/// and returns a vector of exceptions.
1667+
std::vector<irep_idt> java_bytecode_parsert::rexceptions_attribute()
1668+
{
1669+
u2 number_of_exceptions = read_u2();
1670+
1671+
std::vector<irep_idt> exceptions;
1672+
for(size_t i = 0; i < number_of_exceptions; i++)
1673+
{
1674+
u2 exception_index_table = read_u2();
1675+
const irep_idt exception_name =
1676+
constant(exception_index_table).type().get(ID_C_base_name);
1677+
exceptions.push_back(exception_name);
1678+
}
1679+
return exceptions;
1680+
}
1681+
16571682
void java_bytecode_parsert::rclass_attribute(classt &parsed_class)
16581683
{
16591684
u2 attribute_name_index=read_u2();

src/util/irep_ids.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ IREP_ID_TWO(C_temporary_avoided, #temporary_avoided)
692692
IREP_ID_TWO(C_qualifier, #qualifier)
693693
IREP_ID_TWO(C_array_ini, #array_ini)
694694
IREP_ID_ONE(super_class)
695+
IREP_ID_ONE(exceptions_thrown_list)
695696

696697
// Projects depending on this code base that wish to extend the list of
697698
// available ids should provide a file local_irep_ids.h in their source tree and

src/util/std_types.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,16 @@ class code_typet:public typet
912912
return (parameterst &)add(ID_parameters).get_sub();
913913
}
914914

915+
const std::vector<irept> &throws_exceptions() const
916+
{
917+
return find(ID_exceptions_thrown_list).get_sub();
918+
}
919+
920+
std::vector<irept> &throws_exceptions()
921+
{
922+
return add(ID_exceptions_thrown_list).get_sub();
923+
}
924+
915925
bool get_inlined() const
916926
{
917927
return get_bool(ID_C_inlined);

0 commit comments

Comments
 (0)