Skip to content

Commit 0f68971

Browse files
Merge pull request diffblue#4513 from romainbrenguier/bugfix/get-class
Replace Object.getClass preprocessing by a CProver.classIdentifier method
2 parents 1622bf8 + d841712 commit 0f68971

File tree

6 files changed

+45
-36
lines changed

6 files changed

+45
-36
lines changed
996 Bytes
Binary file not shown.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class Test {
2+
public String check(int assertId) {
3+
Object o;
4+
o = "foo";
5+
String classId = org.cprover.CProver.classIdentifier(o);
6+
if(assertId == 1) {
7+
assert org.cprover.CProverString.equals(classId, "java.lang.String");
8+
} else if(assertId == 2) {
9+
assert org.cprover.CProverString.equals(classId, "java.lang.Integer");
10+
}
11+
o = new Integer(42);
12+
classId = org.cprover.CProver.classIdentifier(o);
13+
if(assertId == 3) {
14+
assert org.cprover.CProverString.equals(classId, "java.lang.String");
15+
} else if(assertId == 4) {
16+
assert org.cprover.CProverString.equals(classId, "java.lang.Integer");
17+
}
18+
return classId;
19+
}
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
Test.class
3+
--function Test.check --cp `../../../../scripts/format_classpath.sh . ../../../lib/java-models-library/target/core-models.jar`
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
assertion at file Test.java line 7 .*: SUCCESS
7+
assertion at file Test.java line 9 .*: FAILURE
8+
assertion at file Test.java line 14 .*: FAILURE
9+
assertion at file Test.java line 16 .*: SUCCESS

jbmc/src/java_bytecode/java_string_library_preprocess.cpp

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1101,20 +1101,18 @@ code_blockt java_string_library_preprocesst::make_assign_function_from_call(
11011101
function_id, type, loc, symbol_table, false);
11021102
}
11031103

1104-
/// Used to provide our own implementation of the java.lang.Object.getClass()
1104+
/// Used to provide our own implementation of the CProver.classIdentifier()
11051105
/// function.
11061106
/// \param type: type of the function called
11071107
/// \param loc: location in the source
11081108
/// \param function_id: function the generated code will be added to
11091109
/// \param symbol_table: the symbol table
11101110
/// \return Code corresponding to
11111111
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1112-
/// Class class1;
1113-
/// string_expr1 = substr(this->@class_identifier, 6)
1114-
/// class1=Class.forName(string_expr1)
1115-
/// return class1
1112+
/// string_expr1 = substr(obj->@class_identifier, 6)
1113+
/// return string_expr1;
11161114
/// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1117-
code_blockt java_string_library_preprocesst::make_object_get_class_code(
1115+
code_blockt java_string_library_preprocesst::make_class_identifier_code(
11181116
const java_method_typet &type,
11191117
const source_locationt &loc,
11201118
const irep_idt &function_id,
@@ -1123,21 +1121,14 @@ code_blockt java_string_library_preprocesst::make_object_get_class_code(
11231121
java_method_typet::parameterst params = type.parameters();
11241122
PRECONDITION(!params.empty());
11251123
PRECONDITION(!params[0].get_identifier().empty());
1126-
const symbol_exprt this_obj(params[0].get_identifier(), params[0].type());
1124+
const symbol_exprt obj(params[0].get_identifier(), params[0].type());
11271125

11281126
// Code to be returned
11291127
code_blockt code;
11301128

1131-
// > Class class1;
1132-
const pointer_typet &class_type = to_pointer_type(type.return_type());
1133-
const symbolt &class1_sym = fresh_java_symbol(
1134-
class_type, "class_symbol", loc, function_id, symbol_table);
1135-
const symbol_exprt class1 = class1_sym.symbol_expr();
1136-
code.add(code_declt(class1), loc);
1137-
1138-
// class_identifier is this->@class_identifier
1139-
const member_exprt class_identifier(
1140-
checked_dereference(this_obj), "@class_identifier", string_typet());
1129+
// class_identifier is obj->@class_identifier
1130+
const member_exprt class_identifier{
1131+
checked_dereference(obj), "@class_identifier", string_typet()};
11411132

11421133
// string_expr = cprover_string_literal(this->@class_identifier)
11431134
const refined_string_exprt string_expr = string_expr_of_function(
@@ -1157,28 +1148,16 @@ code_blockt java_string_library_preprocesst::make_object_get_class_code(
11571148
code);
11581149

11591150
// string1 = (String*) string_expr
1160-
const pointer_typet string_ptr_type =
1161-
java_reference_type(symbol_table.lookup_ref("java::java.lang.String").type);
1151+
const typet &string_ptr_type = type.return_type();
11621152
const exprt string1 = allocate_fresh_string(
11631153
string_ptr_type, loc, function_id, symbol_table, code);
11641154
code.add(
11651155
code_assign_string_expr_to_java_string(
11661156
string1, string_expr1, symbol_table, true),
11671157
loc);
11681158

1169-
// > class1 = Class.forName(string1)
1170-
java_method_typet fun_type(
1171-
{java_method_typet::parametert(string_ptr_type)}, class_type);
1172-
code_function_callt fun_call(
1173-
class1,
1174-
symbol_exprt(
1175-
"java::java.lang.Class.forName:(Ljava/lang/String;)Ljava/lang/Class;",
1176-
std::move(fun_type)),
1177-
{string1});
1178-
code.add(fun_call, loc);
1179-
1180-
// > return class1;
1181-
code.add(code_returnt(class1), loc);
1159+
// > return string1;
1160+
code.add(code_returnt{string1}, loc);
11821161
return code;
11831162
}
11841163

@@ -1900,9 +1879,10 @@ void java_string_library_preprocesst::initialize_conversion_table()
19001879
cprover_equivalent_to_java_string_returning_function
19011880
["java::java.lang.Long.toString:(JI)Ljava/lang/String;"]=
19021881
ID_cprover_string_of_int_func;
1903-
conversion_table["java::java.lang.Object.getClass:()Ljava/lang/Class;"] =
1882+
conversion_table["java::org.cprover.CProver.classIdentifier:("
1883+
"Ljava/lang/Object;)Ljava/lang/String;"] =
19041884
std::bind(
1905-
&java_string_library_preprocesst::make_object_get_class_code,
1885+
&java_string_library_preprocesst::make_class_identifier_code,
19061886
this,
19071887
std::placeholders::_1,
19081888
std::placeholders::_2,

jbmc/src/java_bytecode/java_string_library_preprocess.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ class java_string_library_preprocesst:public messaget
169169
const irep_idt &function_id,
170170
symbol_table_baset &symbol_table);
171171

172-
code_blockt make_object_get_class_code(
172+
code_blockt make_class_identifier_code(
173173
const java_method_typet &type,
174174
const source_locationt &loc,
175175
const irep_idt &function_id,

0 commit comments

Comments
 (0)