File tree Expand file tree Collapse file tree 7 files changed +16
-2
lines changed Expand file tree Collapse file tree 7 files changed +16
-2
lines changed Original file line number Diff line number Diff line change @@ -155,6 +155,8 @@ void ansi_c_internal_additions(std::string &code)
155
155
" void *" CPROVER_PREFIX " allocate("
156
156
CPROVER_PREFIX " size_t size, " CPROVER_PREFIX " bool zero);\n "
157
157
" 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 "
158
160
159
161
// this is ANSI-C
160
162
" extern " CPROVER_PREFIX " thread_local const char __func__["
Original file line number Diff line number Diff line change @@ -16,6 +16,7 @@ extern const void *__CPROVER_malloc_object;
16
16
extern __CPROVER_size_t __CPROVER_malloc_size ;
17
17
extern _Bool __CPROVER_malloc_is_new_array ;
18
18
extern const void * __CPROVER_memory_leak ;
19
+ extern _Bool __CPROVER_malloc_may_fail ;
19
20
20
21
void __CPROVER_assume (__CPROVER_bool assumption ) __attribute__((__noreturn__ ));
21
22
void __CPROVER_assert (__CPROVER_bool assertion , const char * description );
Original file line number Diff line number Diff line change @@ -112,9 +112,14 @@ __CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
112
112
113
113
inline void * malloc (__CPROVER_size_t malloc_size )
114
114
{
115
- // realistically, malloc may return NULL,
116
- // and __CPROVER_allocate doesn't, but no one cares
117
115
__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
+
118
123
void * malloc_res ;
119
124
malloc_res = __CPROVER_allocate (malloc_size , 0 );
120
125
Original file line number Diff line number Diff line change @@ -1041,6 +1041,8 @@ void cbmc_parse_optionst::help()
1041
1041
" \n "
1042
1042
" Backend options:\n "
1043
1043
" --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 "
1044
1046
" --dimacs generate CNF in DIMACS format\n "
1045
1047
" --beautify beautify the counterexample (greedy heuristic)\n " // NOLINT(*)
1046
1048
" --localize-faults localize faults (experimental)\n "
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ class optionst;
48
48
" (document-subgoals)(outfile):(test-preprocessor)" \
49
49
" D:I:(c89)(c99)(c11)(cpp98)(cpp03)(cpp11)" \
50
50
" (object-bits):" \
51
+ " (malloc-may-fail)" \
51
52
OPT_GOTO_CHECK \
52
53
" (no-assertions)(no-assumptions)" \
53
54
OPT_XML_INTERFACE \
Original file line number Diff line number Diff line change @@ -1093,6 +1093,8 @@ bool configt::set(const cmdlinet &cmdline)
1093
1093
bv_encoding.is_object_bits_default = false ;
1094
1094
}
1095
1095
1096
+ ansi_c.malloc_may_fail = cmdline.isset (" malloc-may-fail" );
1097
+
1096
1098
return false ;
1097
1099
}
1098
1100
Original file line number Diff line number Diff line change @@ -129,6 +129,7 @@ class configt
129
129
libt lib;
130
130
131
131
bool string_abstraction;
132
+ bool malloc_may_fail = false ;
132
133
133
134
static const std::size_t default_object_bits=8 ;
134
135
} ansi_c;
You can’t perform that action at this time.
0 commit comments