Skip to content

Commit e52f05d

Browse files
committed
Add unit test of object size tracking
Because we were previously missing unit tests for the tracking of object sizes. Note that this test includes tracking for when object sizes are based on symbols rather than statically defined. This is used for dynamically sized objects such as those pointed to by the return value of `malloc`.
1 parent 70b7cf7 commit e52f05d

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

unit/solvers/smt2_incremental/object_tracking.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include <util/arith_tools.h>
44
#include <util/c_types.h>
5+
#include <util/config.h>
56
#include <util/namespace.h>
67
#include <util/std_expr.h>
78
#include <util/symbol_table.h>
@@ -157,3 +158,44 @@ TEST_CASE("Tracking object base expressions", "[core][smt2_incremental]")
157158
CHECK(objects_are_already_tracked(compound_expression, object_map));
158159
}
159160
}
161+
162+
static symbolt make_size_variable()
163+
{
164+
symbolt size_variable;
165+
size_variable.name = "size_variable";
166+
size_variable.base_name = "size_variable";
167+
size_variable.type = size_type();
168+
return size_variable;
169+
}
170+
171+
TEST_CASE("Tracking object sizes.", "[core][smt2_incremental]")
172+
{
173+
// Configuration needs to be performed before tracking because the size_type()
174+
// width depends on the global configuration.
175+
config.ansi_c.mode = configt::ansi_ct::flavourt::GCC;
176+
config.ansi_c.set_arch_spec_x86_64();
177+
smt_object_mapt object_map = initial_smt_object_map();
178+
symbol_tablet symbol_table;
179+
const symbolt size_variable = make_size_variable();
180+
symbol_table.insert(size_variable);
181+
const symbol_exprt size_expr = size_variable.symbol_expr();
182+
namespacet ns{symbol_table};
183+
exprt base_object;
184+
exprt expected_size;
185+
using rowt = std::pair<decltype(base_object), decltype(expected_size)>;
186+
std::tie(base_object, expected_size) = GENERATE_REF(
187+
rowt{from_integer(0, unsignedbv_typet{(8)}), from_integer(1, size_type())},
188+
rowt{from_integer(42, signedbv_typet{16}), from_integer(2, size_type())},
189+
rowt{from_integer(-24, signedbv_typet{32}), from_integer(4, size_type())},
190+
rowt{from_integer(2, unsignedbv_typet{64}), from_integer(8, size_type())},
191+
rowt{
192+
symbol_exprt{"array", array_typet{unsignedbv_typet{8}, size_expr}},
193+
size_expr},
194+
rowt{
195+
symbol_exprt{"array", array_typet{signedbv_typet{32}, size_expr}},
196+
mult_exprt{size_expr, from_integer(4, size_type())}});
197+
track_expression_objects(address_of_exprt{base_object}, ns, object_map);
198+
const auto object = object_map.find(base_object);
199+
REQUIRE(object != object_map.end());
200+
REQUIRE(object->second.size == expected_size);
201+
}

0 commit comments

Comments
 (0)