Skip to content

Commit 68adf73

Browse files
author
Thomas Kiley
committed
Add unit test for checking the max malloc size
Ensure that invalid numbers of bits for object id throws, as well as any overflow if the max allocation size is too big.
1 parent 33f4753 commit 68adf73

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ SRC += analyses/ai/ai.cpp \
1414
analyses/does_remove_const/does_expr_lose_const.cpp \
1515
analyses/does_remove_const/does_type_preserve_const_correctness.cpp \
1616
analyses/does_remove_const/is_type_at_least_as_const_as.cpp \
17+
ansi-c/max_malloc_size.cpp \
1718
big-int/big-int.cpp \
1819
compound_block_locations.cpp \
1920
get_goto_model_from_c_test.cpp \

unit/ansi-c/max_malloc_size.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*******************************************************************\
2+
3+
Module: Unit test for max_malloc_size
4+
5+
Author: Thomas Kiley
6+
7+
\*******************************************************************/
8+
9+
#include <ansi-c/ansi_c_internal_additions.cpp>
10+
#include <iostream>
11+
#include <testing-utils/use_catch.h>
12+
13+
TEST_CASE(
14+
"max_malloc_size",
15+
"[core][ansi-c][ansi_c_internal_additions][max_malloc_size]")
16+
{
17+
cbmc_invariants_should_throwt throw_invariants;
18+
19+
SECTION("Too many bits for pointer ID")
20+
{
21+
REQUIRE_THROWS_AS(max_malloc_size(4, 3), invariant_failedt);
22+
REQUIRE_THROWS_AS(max_malloc_size(4, 4), invariant_failedt);
23+
REQUIRE_THROWS_AS(max_malloc_size(4, 5), invariant_failedt);
24+
}
25+
26+
SECTION("Not enough bits for pointer ID")
27+
{
28+
REQUIRE_THROWS_AS(max_malloc_size(4, 0), invariant_failedt);
29+
}
30+
31+
SECTION("Max allocation size overflow")
32+
{
33+
REQUIRE_THROWS_AS(max_malloc_size(128, 63), invariant_failedt);
34+
}
35+
SECTION("Valid allocation size configurations")
36+
{
37+
// Here we use 4 bits for the object ID, leaving 3 for the offset
38+
REQUIRE(max_malloc_size(8, 4) == 8);
39+
REQUIRE(max_malloc_size(128, 64) == 9223372036854775808ull /*2^63*/);
40+
}
41+
}

unit/ansi-c/module_dependencies.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testing-utils
2+
ansi-c

0 commit comments

Comments
 (0)