Skip to content

Commit 40ffd7d

Browse files
Inherit cproverNondetInitialize like a virtual method
We should look for it on the super class, and on that's super class, and so on till we reach the top of the class hierarchy.
1 parent 5321624 commit 40ffd7d

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

jbmc/src/java_bytecode/ci_lazy_methods_needed.cpp

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Author: Chris Smowton, [email protected]
1111

1212
#include "ci_lazy_methods_needed.h"
1313

14+
#include <goto-programs/resolve_inherited_component.h>
15+
1416
#include <util/namespace.h>
1517
#include <util/std_types.h>
1618

@@ -43,6 +45,24 @@ void ci_lazy_methods_neededt::add_clinit_call(const irep_idt &class_id)
4345
add_needed_method(clinit_wrapper);
4446
}
4547

48+
/// For a given class id, if cproverNondetInitialize exists on it or any of its
49+
/// ancestors then note that it is needed.
50+
/// \param class_id: The given class id
51+
void ci_lazy_methods_neededt::add_cprover_nondet_initialize_if_it_exists(
52+
const irep_idt &class_id)
53+
{
54+
resolve_inherited_componentt resolve_inherited_component{symbol_table};
55+
optionalt<resolve_inherited_componentt::inherited_componentt>
56+
cprover_nondet_initialize = resolve_inherited_component(
57+
class_id, "cproverNondetInitialize:()V", true);
58+
59+
if(cprover_nondet_initialize)
60+
{
61+
add_needed_method(
62+
cprover_nondet_initialize->get_full_component_identifier());
63+
}
64+
}
65+
4666
/// Notes class `class_symbol_name` will be instantiated, or a static field
4767
/// belonging to it will be accessed. Also notes that its static initializer is
4868
/// therefore reachable.
@@ -54,11 +74,7 @@ bool ci_lazy_methods_neededt::add_needed_class(
5474
if(!instantiated_classes.insert(class_symbol_name).second)
5575
return false;
5676

57-
const std::string &class_name_string = id2string(class_symbol_name);
58-
const irep_idt cprover_validate(
59-
class_name_string + ".cproverNondetInitialize:()V");
60-
if(symbol_table.symbols.count(cprover_validate))
61-
add_needed_method(cprover_validate);
77+
add_cprover_nondet_initialize_if_it_exists(class_symbol_name);
6278

6379
// Special case for enums. We may want to generalise this, the comment in
6480
// \ref java_object_factoryt::gen_nondet_pointer_init (TG-4689).

jbmc/src/java_bytecode/ci_lazy_methods_needed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class ci_lazy_methods_neededt
5757
const select_pointer_typet &pointer_type_selector;
5858

5959
void add_clinit_call(const irep_idt &class_id);
60+
void add_cprover_nondet_initialize_if_it_exists(const irep_idt &class_id);
6061

6162
void initialize_instantiated_classes_from_pointer(
6263
const pointer_typet &pointer_type,

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -897,17 +897,25 @@ void java_object_factoryt::gen_nondet_struct_init(
897897
}
898898
}
899899

900-
// If <class_identifier>.cproverNondetInitialize() can be found in the
901-
// symbol table, we add a call:
900+
// If cproverNondetInitialize() can be found in the symbol table as a method
901+
// on this class or any parent, we add a call:
902902
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
903903
// expr.cproverNondetInitialize();
904904
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
905905

906-
const irep_idt init_method_name =
907-
"java::" + id2string(struct_tag) + ".cproverNondetInitialize:()V";
908-
if(const auto init_func = symbol_table.lookup(init_method_name))
906+
resolve_inherited_componentt resolve_inherited_component{symbol_table};
907+
optionalt<resolve_inherited_componentt::inherited_componentt>
908+
cprover_nondet_initialize = resolve_inherited_component(
909+
"java::" + id2string(struct_tag), "cproverNondetInitialize:()V", true);
910+
911+
if(cprover_nondet_initialize)
912+
{
913+
const symbolt &cprover_nondet_initialize_symbol =
914+
ns.lookup(cprover_nondet_initialize->get_full_component_identifier());
909915
assignments.add(
910-
code_function_callt{init_func->symbol_expr(), {address_of_exprt{expr}}});
916+
code_function_callt{cprover_nondet_initialize_symbol.symbol_expr(),
917+
{address_of_exprt{expr}}});
918+
}
911919
}
912920

913921
/// Generate code block that verifies that an expression of type float or

0 commit comments

Comments
 (0)