Skip to content

Commit 06ead45

Browse files
committed
Add malloc-may-fail option to cbmc
and hard-code appropriate assertion inside our `stdlib.c`.
1 parent 4682d07 commit 06ead45

File tree

9 files changed

+47
-13
lines changed

9 files changed

+47
-13
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <stdlib.h>
2+
3+
int main()
4+
{
5+
char *p = malloc(100); // may fail, since malloc may fail
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
--malloc-may-fail
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^\[malloc.assertion.\d+\] line \d+ malloc may fail: FAILURE$
7+
^VERIFICATION FAILED$
8+
--
9+
^warning: ignoring

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ 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+
CPROVER_PREFIX "bool " CPROVER_PREFIX "malloc_may_fail = " +
159+
std::to_string(config.ansi_c.malloc_may_fail) + ";\n"
158160

159161
// this is ANSI-C
160162
"extern " CPROVER_PREFIX "thread_local const char __func__["

src/ansi-c/library/cprover.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ 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 _Bool __CPROVER_malloc_may_fail;
1920

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

src/ansi-c/library/stdlib.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,23 +115,31 @@ 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+
__CPROVER_assert(
120+
__CPROVER_malloc_may_fail == (__CPROVER_bool)0, "malloc may fail");
123121

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;
122+
void *malloc_res;
123+
malloc_res = __CPROVER_allocate(malloc_size, 0);
129124

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;
125+
// make sure it's not recorded as deallocated
126+
__CPROVER_deallocated =
127+
(malloc_res == __CPROVER_deallocated) ? 0 : __CPROVER_deallocated;
133128

134-
return malloc_res;
129+
// record the object size for non-determistic bounds checking
130+
__CPROVER_bool record_malloc = __VERIFIER_nondet___CPROVER_bool();
131+
__CPROVER_malloc_object =
132+
record_malloc ? malloc_res : __CPROVER_malloc_object;
133+
__CPROVER_malloc_size = record_malloc ? malloc_size : __CPROVER_malloc_size;
134+
__CPROVER_malloc_is_new_array =
135+
record_malloc ? 0 : __CPROVER_malloc_is_new_array;
136+
137+
// detect memory leaks
138+
__CPROVER_bool record_may_leak = __VERIFIER_nondet___CPROVER_bool();
139+
__CPROVER_memory_leak =
140+
record_may_leak ? malloc_res : __CPROVER_memory_leak;
141+
142+
return malloc_res;
135143
}
136144

137145
/* FUNCTION: __builtin_alloca */

src/cbmc/cbmc_parse_options.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,7 @@ void cbmc_parse_optionst::help()
10411041
"\n"
10421042
"Backend options:\n"
10431043
" --object-bits n number of bits used for object addresses\n"
1044+
" --malloc-may-fail enable failures for malloc calls\n"
10441045
" --dimacs generate CNF in DIMACS format\n"
10451046
" --beautify beautify the counterexample (greedy heuristic)\n" // NOLINT(*)
10461047
" --localize-faults localize faults (experimental)\n"

src/cbmc/cbmc_parse_options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class optionst;
4848
"(document-subgoals)(outfile):(test-preprocessor)" \
4949
"D:I:(c89)(c99)(c11)(cpp98)(cpp03)(cpp11)" \
5050
"(object-bits):" \
51+
"(malloc-may-fail)" \
5152
OPT_GOTO_CHECK \
5253
"(no-assertions)(no-assumptions)" \
5354
OPT_XML_INTERFACE \

src/util/config.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,11 @@ bool configt::set(const cmdlinet &cmdline)
10931093
bv_encoding.is_object_bits_default = false;
10941094
}
10951095

1096+
if(cmdline.isset("malloc-may-fail"))
1097+
{
1098+
ansi_c.malloc_may_fail = true;
1099+
}
1100+
10961101
return false;
10971102
}
10981103

src/util/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ class configt
129129
libt lib;
130130

131131
bool string_abstraction;
132+
bool malloc_may_fail = false;
132133

133134
static const std::size_t default_object_bits=8;
134135
} ansi_c;

0 commit comments

Comments
 (0)