Skip to content

Commit 531b856

Browse files
committed
Malloc should fail on too large allocations
1: two new options to choose between fail by assert-then-assume and return null 2: `__CPROVER_max_malloc_size` is introduced 3: some tests for boundary conditions 4: user-level documentation
1 parent 1f2f1fa commit 531b856

File tree

14 files changed

+150
-13
lines changed

14 files changed

+150
-13
lines changed

doc/cprover-manual/properties.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,19 @@ example, replacing functions or setting global variables with the `__CPROVER`
319319
prefix might make analysis impossible. To avoid doing this by accident, negative
320320
lookahead can be used. For example, `(?!__).*` matches all names not starting
321321
with `__`.
322+
323+
### Malloc failure mode
324+
325+
|Flag | Check |
326+
|------------------------|-------------------------------------------------|
327+
| `--malloc-fail-null` | in case malloc fails return NULL |
328+
| `--malloc-fail-assert` | in case malloc fails report as failed property |
329+
330+
Calling `malloc` may fail for a number of reasons and the function may return a
331+
NULL pointer. The users can choose if and how they want the `malloc`-related
332+
failures to occur. The option `--malloc-fail-null` results in `malloc` returning
333+
the NULL pointer when failing. The option `--malloc-fail-assert` places
334+
additional properties inside `malloc` that are checked and if failing the
335+
verification is terminated (by `assume(false)`). One such property is that the
336+
allocated size is not too large, i.e. internally representable. When neither of
337+
those two options are used, CBMC will assume that `malloc` does not fail.
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(__CPROVER_max_malloc_size); // try to allocate exactly the max
8+
9+
return 0;
10+
}
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+
--malloc-fail-assert
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+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
max_size.c
3+
--malloc-fail-assert
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[malloc.assertion.\d+\] line \d+ max allocation size exceeded: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include <assert.h>
2+
#include <stdint.h>
3+
#include <stdlib.h>
4+
5+
int main()
6+
{
7+
// try to allocate the smallest violating amount
8+
int *p = malloc(__CPROVER_max_malloc_size + 1);
9+
assert(p);
10+
11+
return 0;
12+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
one_byte_too_large.c
3+
--malloc-fail-null
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[main.assertion.\d+\] line \d+ assertion p: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,15 @@ void ansi_c_internal_additions(std::string &code)
155155
"void *" CPROVER_PREFIX "allocate("
156156
CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);\n"
157157
"const void *" CPROVER_PREFIX "alloca_object = 0;\n"
158+
"int " CPROVER_PREFIX "malloc_failure_mode="+
159+
std::to_string(config.ansi_c.malloc_failure_mode)+";\n"
160+
"int " CPROVER_PREFIX "malloc_failure_mode_return_null="+
161+
std::to_string(config.ansi_c.malloc_failure_mode_return_null)+";\n"
162+
"int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
163+
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
164+
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
165+
std::to_string(1 << (config.ansi_c.pointer_width -
166+
config.bv_encoding.object_bits - 1))+";\n"
158167

159168
// this is ANSI-C
160169
"extern " CPROVER_PREFIX "thread_local const char __func__["

src/ansi-c/library/cprover.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ extern const void *__CPROVER_malloc_object;
1616
extern __CPROVER_size_t __CPROVER_malloc_size;
1717
extern _Bool __CPROVER_malloc_is_new_array;
1818
extern const void *__CPROVER_memory_leak;
19+
extern int __CPROVER_malloc_failure_mode;
20+
extern __CPROVER_size_t __CPROVER_max_malloc_size;
21+
22+
// malloc failure modes
23+
extern int __CPROVER_malloc_failure_mode_return_null;
24+
extern int __CPROVER_malloc_failure_mode_assert_then_assume;
1925

2026
void __CPROVER_assume(__CPROVER_bool assumption) __attribute__((__noreturn__));
2127
void __CPROVER_assert(__CPROVER_bool assertion, const char *description);

src/ansi-c/library/stdlib.c

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,47 @@ inline void *malloc(__CPROVER_size_t malloc_size)
115115
// realistically, malloc may return NULL,
116116
// and __CPROVER_allocate doesn't, but no one cares
117117
__CPROVER_HIDE:;
118-
void *malloc_res;
119-
malloc_res = __CPROVER_allocate(malloc_size, 0);
120118

121-
// make sure it's not recorded as deallocated
122-
__CPROVER_deallocated=(malloc_res==__CPROVER_deallocated)?0:__CPROVER_deallocated;
119+
if(
120+
__CPROVER_malloc_failure_mode ==
121+
__CPROVER_malloc_failure_mode_return_null)
122+
{
123+
if(malloc_size > __CPROVER_max_malloc_size)
124+
{
125+
return (void *)0;
126+
}
127+
}
128+
else if(
129+
__CPROVER_malloc_failure_mode ==
130+
__CPROVER_malloc_failure_mode_assert_then_assume)
131+
{
132+
__CPROVER_assert(
133+
malloc_size <= __CPROVER_max_malloc_size,
134+
"max allocation size exceeded");
135+
__CPROVER_assume(malloc_size <= __CPROVER_max_malloc_size);
136+
}
123137

124-
// record the object size for non-determistic bounds checking
125-
__CPROVER_bool record_malloc=__VERIFIER_nondet___CPROVER_bool();
126-
__CPROVER_malloc_object=record_malloc?malloc_res:__CPROVER_malloc_object;
127-
__CPROVER_malloc_size=record_malloc?malloc_size:__CPROVER_malloc_size;
128-
__CPROVER_malloc_is_new_array=record_malloc?0:__CPROVER_malloc_is_new_array;
138+
void *malloc_res;
139+
malloc_res = __CPROVER_allocate(malloc_size, 0);
129140

130-
// detect memory leaks
131-
__CPROVER_bool record_may_leak=__VERIFIER_nondet___CPROVER_bool();
132-
__CPROVER_memory_leak=record_may_leak?malloc_res:__CPROVER_memory_leak;
141+
// make sure it's not recorded as deallocated
142+
__CPROVER_deallocated =
143+
(malloc_res == __CPROVER_deallocated) ? 0 : __CPROVER_deallocated;
133144

134-
return malloc_res;
145+
// record the object size for non-determistic bounds checking
146+
__CPROVER_bool record_malloc = __VERIFIER_nondet___CPROVER_bool();
147+
__CPROVER_malloc_object =
148+
record_malloc ? malloc_res : __CPROVER_malloc_object;
149+
__CPROVER_malloc_size = record_malloc ? malloc_size : __CPROVER_malloc_size;
150+
__CPROVER_malloc_is_new_array =
151+
record_malloc ? 0 : __CPROVER_malloc_is_new_array;
152+
153+
// detect memory leaks
154+
__CPROVER_bool record_may_leak = __VERIFIER_nondet___CPROVER_bool();
155+
__CPROVER_memory_leak =
156+
record_may_leak ? malloc_res : __CPROVER_memory_leak;
157+
158+
return malloc_res;
135159
}
136160

137161
/* FUNCTION: __builtin_alloca */

0 commit comments

Comments
 (0)