Skip to content

Commit effdd30

Browse files
committed
Add malloc-may-fail option to goto-checker
And append an `assert(false)` property to every `malloc` call.
1 parent be84cd1 commit effdd30

File tree

4 files changed

+41
-2
lines changed

4 files changed

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

src/analyses/goto_check.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class goto_checkt
7272
enable_built_in_assertions=_options.get_bool_option("built-in-assertions");
7373
enable_assumptions=_options.get_bool_option("assumptions");
7474
error_labels=_options.get_list_option("error-label");
75+
enable_malloc_fail = _options.get_bool_option("malloc-may-fail");
7576
}
7677

7778
typedef goto_functionst::goto_functiont goto_functiont;
@@ -238,6 +239,7 @@ class goto_checkt
238239
bool enable_assertions;
239240
bool enable_built_in_assertions;
240241
bool enable_assumptions;
242+
bool enable_malloc_fail;
241243

242244
typedef optionst::value_listt error_labelst;
243245
error_labelst error_labels;
@@ -1941,6 +1943,21 @@ void goto_checkt::goto_check(
19411943
if(rw_ok_cond.has_value())
19421944
rhs = *rw_ok_cond;
19431945
}
1946+
1947+
if(enable_malloc_fail && code_assign.rhs().id() == ID_side_effect)
1948+
{
1949+
const auto &rhs_side_effect = to_side_effect_expr(code_assign.rhs());
1950+
if(rhs_side_effect.get_statement() == ID_allocate)
1951+
{
1952+
add_guarded_property(
1953+
false_exprt{},
1954+
"malloc may fail",
1955+
"malloc may fail",
1956+
i.code.source_location(),
1957+
code_assign.rhs(),
1958+
guardt{true_exprt{}, guard_manager});
1959+
}
1960+
}
19441961
}
19451962
else if(i.is_function_call())
19461963
{

src/analyses/goto_check.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ void goto_check(
3838
"(div-by-zero-check)(enum-range-check)(signed-overflow-check)(unsigned-" \
3939
"overflow-check)" \
4040
"(pointer-overflow-check)(conversion-check)(undefined-shift-check)" \
41-
"(float-overflow-check)(nan-check)(no-built-in-assertions)"
41+
"(float-overflow-check)(nan-check)(no-built-in-assertions)" \
42+
"(malloc-may-fail)"
4243

4344
// clang-format off
4445
#define HELP_GOTO_CHECK \
@@ -55,6 +56,7 @@ void goto_check(
5556
" --nan-check check floating-point for NaN\n" \
5657
" --no-built-in-assertions ignore assertions in built-in library\n" \
5758
" --enum-range-check checks that all enum type expressions have values in the enum range\n" /* NOLINT(whitespace/line_length) */ \
59+
" --malloc-may-fail enable failures for malloc calls" \
5860
// clang-format on
5961

6062
#define PARSE_OPTIONS_GOTO_CHECK(cmdline, options) \
@@ -70,6 +72,7 @@ void goto_check(
7072
options.set_option("undefined-shift-check", cmdline.isset("undefined-shift-check")); /* NOLINT(whitespace/line_length) */ \
7173
options.set_option("float-overflow-check", cmdline.isset("float-overflow-check")); /* NOLINT(whitespace/line_length) */ \
7274
options.set_option("nan-check", cmdline.isset("nan-check")); \
73-
options.set_option("built-in-assertions", !cmdline.isset("no-built-in-assertions")) /* NOLINT(whitespace/line_length) */
75+
options.set_option("built-in-assertions", !cmdline.isset("no-built-in-assertions")); /* NOLINT(whitespace/line_length) */ \
76+
options.set_option("malloc-may-fail", cmdline.isset("malloc-may-fail"))
7477

7578
#endif // CPROVER_ANALYSES_GOTO_CHECK_H

0 commit comments

Comments
 (0)