Skip to content

Commit 7b55399

Browse files
authored
Merge pull request #1152 from thk123/feature/java-array-checker
Added check for a type being a valid formed Java array
2 parents 5c9eb59 + 5274109 commit 7b55399

File tree

4 files changed

+55
-0
lines changed

4 files changed

+55
-0
lines changed

src/java_bytecode/java_bytecode_convert_class.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,11 @@ void java_bytecode_convert_classt::add_array_types(symbol_tablet &symbol_table)
292292
comp2.set_base_name("data");
293293
struct_type.components().push_back(comp2);
294294

295+
INVARIANT(
296+
is_valid_java_array(struct_type),
297+
"Constructed a new type representing a Java Array "
298+
"object that doesn't match expectations");
299+
295300
symbolt symbol;
296301
symbol.name=symbol_type.get_identifier();
297302
symbol.base_name=symbol_type.get(ID_C_base_name);

src/java_bytecode/java_object_factory.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,10 @@ void java_object_factoryt::gen_nondet_array_init(
10591059
// Otherwise we're updating the array in place, and use the
10601060
// existing array allocation and length.
10611061

1062+
INVARIANT(
1063+
is_valid_java_array(struct_type),
1064+
"Java struct array does not conform to expectations");
1065+
10621066
dereference_exprt deref_expr(expr, expr.type().subtype());
10631067
const auto &comps=struct_type.components();
10641068
exprt length_expr=member_exprt(deref_expr, "length", comps[1].type());

src/java_bytecode/java_types.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,47 @@ symbol_typet java_classname(const std::string &id)
314314

315315
return symbol_type;
316316
}
317+
318+
/// Programmatic documentation of the structure of a Java array (of either
319+
/// primitives or references) type.
320+
/// A Java array is represented in GOTO in the following way:
321+
/// A struct component with a tag like java::array[type] where type is either
322+
/// a primitive like int, or reference.
323+
/// The struct has precisely three components:
324+
/// 1. \@java.lang.Object: of type struct {java.lang.Object} containing the base
325+
/// class data
326+
/// 2. length: of type Java integer - the length of the array
327+
/// 3. data: of type pointer to either pointer to a primitive type, or pointer
328+
/// to pointer to empty (i.e. a void**) pointing to the contents of the array
329+
/// \param type: A type that might represent a Java array
330+
/// \return True if it is a Java array type, false otherwise
331+
bool is_valid_java_array(const struct_typet &type)
332+
{
333+
bool correct_num_components=type.components().size()==3;
334+
if(!correct_num_components)
335+
return false;
336+
337+
// First component, the base class (Object) data
338+
const struct_union_typet::componentt base_class_component=
339+
type.components()[0];
340+
341+
bool base_component_valid=true;
342+
base_component_valid&=base_class_component.get_name()=="@java.lang.Object";
343+
344+
bool length_component_valid=true;
345+
const struct_union_typet::componentt length_component=
346+
type.components()[1];
347+
length_component_valid&=length_component.get_name()=="length";
348+
length_component_valid&=length_component.type()==java_int_type();
349+
350+
bool data_component_valid=true;
351+
const struct_union_typet::componentt data_component=
352+
type.components()[2];
353+
data_component_valid&=data_component.get_name()=="data";
354+
data_component_valid&=data_component.type().id()==ID_pointer;
355+
356+
return correct_num_components &&
357+
base_component_valid &&
358+
length_component_valid &&
359+
data_component_valid;
360+
}

src/java_bytecode/java_types.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ exprt java_bytecode_promotion(const exprt &);
7474

7575
bool is_java_array_tag(const irep_idt& tag);
7676

77+
bool is_valid_java_array(const struct_typet &type);
78+
7779
#endif // CPROVER_JAVA_BYTECODE_JAVA_TYPES_H

0 commit comments

Comments
 (0)