Skip to content

Commit 98aaf66

Browse files
committed
Add malloc-may-fail option to cbmc
and hard-code appropriate check inside our `stdlib.c`. Note: minus clang-format.
1 parent 4682d07 commit 98aaf66

File tree

7 files changed

+16
-2
lines changed

7 files changed

+16
-2
lines changed

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: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,14 @@ __CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
112112

113113
inline void *malloc(__CPROVER_size_t malloc_size)
114114
{
115-
// realistically, malloc may return NULL,
116-
// and __CPROVER_allocate doesn't, but no one cares
117115
__CPROVER_HIDE:;
116+
// realistically, malloc may return NULL,
117+
// but we only do so if `--malloc-may-fail` is set
118+
119+
__CPROVER_bool should_malloc_fail = __VERIFIER_nondet___CPROVER_bool();
120+
if(__CPROVER_malloc_may_fail && should_malloc_fail)
121+
return (void *)0;
122+
118123
void *malloc_res;
119124
malloc_res = __CPROVER_allocate(malloc_size, 0);
120125

src/cbmc/cbmc_parse_options.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,6 +1041,8 @@ void cbmc_parse_optionst::help()
10411041
"\n"
10421042
"Backend options:\n"
10431043
" --object-bits n number of bits used for object addresses\n"
1044+
// NOLINTNEXTLINE(whitespace/line_length)
1045+
" --malloc-may-fail allow malloc calls to return a null pointer\n"
10441046
" --dimacs generate CNF in DIMACS format\n"
10451047
" --beautify beautify the counterexample (greedy heuristic)\n" // NOLINT(*)
10461048
" --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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,6 +1093,8 @@ bool configt::set(const cmdlinet &cmdline)
10931093
bv_encoding.is_object_bits_default = false;
10941094
}
10951095

1096+
ansi_c.malloc_may_fail = cmdline.isset("malloc-may-fail");
1097+
10961098
return false;
10971099
}
10981100

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)