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

14 files changed

+150
-13
lines changed

doc/cprover-manual/properties.md

+16
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.
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+
}
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
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+
}
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
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+
}
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

+9
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

+6
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

+37-13
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 */

src/cbmc/cbmc_parse_options.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,9 @@ void cbmc_parse_optionst::help()
10671067
" --error-label label check that label is unreachable\n"
10681068
" --cover CC create test-suite with coverage criterion CC\n" // NOLINT(*)
10691069
" --mm MM memory consistency model for concurrent programs\n" // NOLINT(*)
1070+
// NOLINTNEXTLINE(whitespace/line_length)
1071+
" --malloc-fail-assert set malloc failure mode to assert-then-assume\n"
1072+
" --malloc-fail-null set malloc failure mode to return null\n"
10701073
HELP_REACHABILITY_SLICER
10711074
HELP_REACHABILITY_SLICER_FB
10721075
" --full-slice run full slicer (experimental)\n" // NOLINT(*)

src/cbmc/cbmc_parse_options.h

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class optionst;
5050
"(object-bits):" \
5151
OPT_GOTO_CHECK \
5252
"(no-assertions)(no-assumptions)" \
53+
"(malloc-fail-assert)(malloc-fail-null)" \
5354
OPT_XML_INTERFACE \
5455
OPT_JSON_INTERFACE \
5556
"(smt1)(smt2)(fpa)(cvc3)(cvc4)(boolector)(yices)(z3)(mathsat)" \

src/util/config.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,16 @@ bool configt::set(const cmdlinet &cmdline)
10931093
bv_encoding.is_object_bits_default = false;
10941094
}
10951095

1096+
if(cmdline.isset("malloc-fail-assert") && cmdline.isset("malloc-fail-null"))
1097+
{
1098+
throw invalid_command_line_argument_exceptiont{
1099+
"at most one malloc failure mode is acceptable", "--malloc-fail-null"};
1100+
}
1101+
if(cmdline.isset("malloc-fail-null"))
1102+
ansi_c.malloc_failure_mode = ansi_c.malloc_failure_mode_return_null;
1103+
if(cmdline.isset("malloc-fail-assert"))
1104+
ansi_c.malloc_failure_mode = ansi_c.malloc_failure_mode_assert_then_assume;
1105+
10961106
return false;
10971107
}
10981108

src/util/config.h

+9
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,15 @@ class configt
130130

131131
bool string_abstraction;
132132

133+
enum malloc_failure_modet
134+
{
135+
malloc_failure_mode_none = 0,
136+
malloc_failure_mode_return_null = 1,
137+
malloc_failure_mode_assert_then_assume = 2
138+
};
139+
140+
malloc_failure_modet malloc_failure_mode = malloc_failure_mode_none;
141+
133142
static const std::size_t default_object_bits=8;
134143
} ansi_c;
135144

0 commit comments

Comments
 (0)