Skip to content

Commit 511be72

Browse files
committed
quick fix #8428 jbmc array unsoundness
1 parent 97a7c25 commit 511be72

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

jbmc/src/java_bytecode/java_object_factory.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,17 +1143,16 @@ static void allocate_nondet_length_array(
11431143
const allocate_local_symbolt &allocate_local_symbol,
11441144
const source_locationt &location)
11451145
{
1146-
const auto &length_sym_expr = generate_nondet_int(
1147-
from_integer(0, java_int_type()),
1148-
max_length_expr,
1146+
const auto &length_sym_expr = gen_positive_nondet_int(
11491147
"nondet_array_length",
1148+
java_int_type(),
11501149
location,
11511150
allocate_local_symbol,
11521151
assignments);
11531152

11541153
side_effect_exprt java_new_array(ID_java_new_array, lhs.type(), location);
11551154
java_new_array.copy_to_operands(length_sym_expr);
1156-
java_new_array.set(ID_length_upper_bound, max_length_expr);
1155+
//java_new_array.set(ID_length_upper_bound, max_length_expr);
11571156
to_type_with_subtype(java_new_array.type())
11581157
.subtype()
11591158
.set(ID_element_type, element_type);

jbmc/src/java_bytecode/nondet.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,34 @@ symbol_exprt generate_nondet_int(
8888
instructions);
8989
}
9090

91+
symbol_exprt gen_positive_nondet_int(
92+
const std::string &basename_prefix,
93+
const typet &int_type,
94+
const source_locationt &source_location,
95+
const allocate_local_symbolt &alocate_local_symbol,
96+
code_blockt &instructions)
97+
{
98+
const exprt min_value_expr = from_integer(0, int_type);
99+
100+
// Declare a symbol for the non deterministic integer.
101+
const symbol_exprt &nondet_symbol =
102+
alocate_local_symbol(int_type, basename_prefix);
103+
instructions.add(code_frontend_declt(nondet_symbol));
104+
105+
// Assign the symbol any non deterministic integer value.
106+
// int_type name_prefix::nondet_int = NONDET(int_type)
107+
instructions.add(code_frontend_assignt(
108+
nondet_symbol, side_effect_expr_nondett(int_type, source_location)));
109+
110+
// Constrain the non deterministic integer with a lower bound of `min_value`.
111+
// ASSUME(name_prefix::nondet_int >= min_value)
112+
instructions.add(
113+
code_assumet(binary_predicate_exprt(nondet_symbol, ID_ge, min_value_expr)));
114+
115+
return nondet_symbol;
116+
}
117+
118+
91119
code_blockt generate_nondet_switch(
92120
const irep_idt &name_prefix,
93121
const alternate_casest &switch_cases,

jbmc/src/java_bytecode/nondet.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,28 @@ symbol_exprt generate_nondet_int(
7979
allocate_objectst &allocate_objects,
8080
code_blockt &instructions);
8181

82+
/// Gets a fresh nondet choice in range >= 0. GOTO generated
83+
/// resembles:
84+
/// ```
85+
/// int_type name_prefix::nondet_int = NONDET(int_type)
86+
/// ASSUME(name_prefix::nondet_int >= 0)
87+
/// ```
88+
/// \param basename_prefix: Basename prefix for the fresh symbol name generated.
89+
/// \param int_type: The type of the int used to non-deterministically choose
90+
/// one of the switch cases.
91+
/// \param source_location: The location to mark the generated int with.
92+
/// \param allocate_local_symbol: Handles allocation of new objects.
93+
/// \param [out] instructions: Output instructions are written to
94+
/// 'instructions'. These declare, nondet-initialise and range-constrain (with
95+
/// assume statements) a fresh integer.
96+
/// \return Returns a symbol expression for the resulting integer.
97+
symbol_exprt gen_positive_nondet_int(
98+
const std::string &basename_prefix,
99+
const typet &int_type,
100+
const source_locationt &source_location,
101+
const allocate_local_symbolt &alocate_local_symbol,
102+
code_blockt &instructions);
103+
82104
typedef std::vector<codet> alternate_casest;
83105

84106
/// Pick nondeterministically between imperative actions 'switch_cases'.

0 commit comments

Comments
 (0)