Skip to content

Commit cce4e8e

Browse files
committed
Malloc should fail on too large assertions
1: introduce `pointer-width` and `object-width` as new `cprover-start` variables 2: assert-then-assume about the size limit in our `stdlib.c` implementation of `malloc` 3: add some tests, and assume about the alloc size in others Note: this PR removes the C90-style structhack in test `cbmc/Pointer_byte_extract5`. This may need to be amended if SV-Comp decides they want to keep it.
1 parent be84cd1 commit cce4e8e

File tree

19 files changed

+133
-24
lines changed

19 files changed

+133
-24
lines changed

regression/cbmc-library/memcpy-04/main.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ void publish(info_t *info)
1111
{
1212
size_t size;
1313
__CPROVER_assume(size >= info->len);
14+
// alloc size is arbitrary but no larger than what CBMC can represent
15+
size_t pointer_width = sizeof(void *) * 8;
16+
size_t object_bits = 8; // by default
17+
size_t cbmc_max_alloc_size = ((size_t)1 << (pointer_width - object_bits)) - 1;
18+
__CPROVER_assume(size <= cbmc_max_alloc_size);
1419
unsigned char *buffer = malloc(size);
1520
memcpy(buffer, info->name, info->len);
1621
if(info->len > 42)
@@ -24,6 +29,13 @@ void main()
2429
{
2530
info_t info;
2631
size_t name_len;
32+
33+
// alloc size is arbitrary but no larger than what CBMC can represent
34+
size_t pointer_width = sizeof(void *) * 8;
35+
size_t object_bits = 8; // by default
36+
size_t cbmc_max_alloc_size = ((size_t)1 << (pointer_width - object_bits)) - 1;
37+
__CPROVER_assume(name_len <= cbmc_max_alloc_size);
38+
2739
info.name = malloc(name_len);
2840
info.len = name_len;
2941
if(name_len > 42)

regression/cbmc-library/memcpy-04/test.desc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
CORE
22
main.c
33

4-
^\[publish.assertion.1\] line 18 should pass: SUCCESS$
5-
^\[publish.assertion.2\] line 19 should fail: FAILURE$
4+
^\[publish.assertion.\d+\] line \d+ should pass: SUCCESS$
5+
^\[publish.assertion.\d+\] line \d+ should fail: FAILURE$
66
^\*\* 1 of \d+ failed
77
^VERIFICATION FAILED$
88
^EXIT=10$

regression/cbmc/Malloc25/main.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
int main(int argc, char *argv[])
44
{
5-
int *p = malloc((size_t)argc * (size_t)argc * sizeof(int));
5+
size_t alloc_size = (size_t)argc * (size_t)argc * sizeof(int);
6+
// alloc size is arbitrary but no larger than what CBMC can represent
7+
size_t pointer_width = sizeof(void *) * 8;
8+
size_t object_bits = 8; // by default
9+
size_t cbmc_max_alloc_size = ((size_t)1 << (pointer_width - object_bits)) - 1;
10+
__CPROVER_assume(alloc_size <= cbmc_max_alloc_size);
11+
12+
int *p = malloc(alloc_size);
613
return 0;
714
}

regression/cbmc/Pointer_byte_extract5/main.i

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ typedef union
1111
typedef struct
1212
{
1313
int Count;
14-
Union List[1];
14+
Union List[0];
1515
} Struct3;
1616
#pragma pack(pop)
1717

1818
int main()
1919
{
20-
Struct3 *p = malloc(sizeof(Struct3) + sizeof(Union));
20+
Struct3 *p = malloc(sizeof(Struct3) + 2 * sizeof(Union));
2121
p->Count = 3;
2222
int po=0;
2323

regression/cbmc/Pointer_byte_extract5/no-simplify.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ main.i
44
^EXIT=10$
55
^SIGNAL=0$
66
array\.List dynamic object upper bound in p->List\[2\]: FAILURE
7-
\*\* 1 of 14 failed
7+
\*\* 1 of 15 failed
88
--
99
^warning: ignoring
1010
--

regression/cbmc/Pointer_byte_extract5/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ main.i
44
^EXIT=10$
55
^SIGNAL=0$
66
array\.List dynamic object upper bound in p->List\[2\]: FAILURE
7-
\*\* 1 of 11 failed
7+
\*\* 1 of 13 failed
88
--
99
^warning: ignoring
1010
--

regression/cbmc/array_constraints1/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ main.c
44
^EXIT=10$
55
^SIGNAL=0$
66
^VERIFICATION FAILED$
7-
^\*\* 2 of 14
7+
^\*\* 2 of 15
88
--
99
^warning: ignoring
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
5+
int main()
6+
{
7+
size_t pointer_width = sizeof(void *) * 8;
8+
size_t object_bits = 8; // by default
9+
size_t cbmc_max_alloc_size = ((size_t)1 << (pointer_width - object_bits)) - 1;
10+
int *p = malloc(cbmc_max_alloc_size); // try to allocate exactly the max
11+
12+
return 0;
13+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
largest_representable.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[malloc.assertion.\d+\] line \d+ max allocation size exceeded: SUCCESS$
7+
^VERIFICATION SUCCESSFUL$
8+
--
9+
^warning: ignoring
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
5+
int main()
6+
{
7+
int *p = malloc(SIZE_MAX);
8+
9+
return 0;
10+
}

0 commit comments

Comments
 (0)