Skip to content

Commit 19e6e86

Browse files
committed
Warn when --malloc-may-fail has no effect
f7958df documented that malloc-may-fail needs to be used whenever the library is added, but users might not actually have had a chance to read updated documentation. Print a warning at runtime to make it more likely that users become aware.
1 parent e08c77d commit 19e6e86

File tree

6 files changed

+63
-0
lines changed

6 files changed

+63
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <stdlib.h>
2+
3+
void main()
4+
{
5+
int *p = malloc(sizeof(int));
6+
__CPROVER_assert(p != NULL, "malloc-may-fail is not set");
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
--add-library _ --malloc-may-fail
4+
^malloc-may-fail has no effect when an implementation of malloc is already provided$
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test is to confirm that ineffective use of malloc-may-fail yields a
11+
warning.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
void *malloc(__CPROVER_size_t size)
2+
{
3+
return __CPROVER_allocate(size, 0);
4+
}
5+
6+
void main()
7+
{
8+
int *p = malloc(sizeof(int));
9+
__CPROVER_assert(p != NULL, "malloc-may-fail is not set");
10+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
with-malloc.desc
3+
--malloc-may-fail
4+
^malloc-may-fail has no effect when an implementation of malloc is already provided$
5+
^VERIFICATION SUCCESSFUL$
6+
^EXIT=0$
7+
^SIGNAL=0$
8+
--
9+
--
10+
This test is to confirm that ineffective use of malloc-may-fail yields a
11+
warning.

src/cbmc/cbmc_parse_options.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,18 @@ int cbmc_parse_optionst::get_goto_program(
812812
return CPROVER_EXIT_SUCCESS;
813813
}
814814

815+
if(cmdline.isset("malloc-may-fail"))
816+
{
817+
auto malloc_entry = goto_model.goto_functions.function_map.find("malloc");
818+
if(
819+
malloc_entry != goto_model.goto_functions.function_map.end() &&
820+
malloc_entry->second.body_available())
821+
{
822+
log.warning() << "malloc-may-fail has no effect when an implementation "
823+
<< "of malloc is already provided" << messaget::eom;
824+
}
825+
}
826+
815827
if(cbmc_parse_optionst::process_goto_program(goto_model, options, log))
816828
return CPROVER_EXIT_INTERNAL_ERROR;
817829

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,6 +1066,18 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10661066

10671067
// we add the library in some cases, as some analyses benefit
10681068

1069+
if(cmdline.isset("malloc-may-fail"))
1070+
{
1071+
auto malloc_entry = goto_model.goto_functions.function_map.find("malloc");
1072+
if(
1073+
malloc_entry != goto_model.goto_functions.function_map.end() &&
1074+
malloc_entry->second.body_available())
1075+
{
1076+
log.warning() << "malloc-may-fail has no effect when an implementation "
1077+
<< "of malloc is already provided" << messaget::eom;
1078+
}
1079+
}
1080+
10691081
if(cmdline.isset("add-library") ||
10701082
cmdline.isset("mm"))
10711083
{

0 commit comments

Comments
 (0)