Skip to content

Commit e73e6aa

Browse files
committed
Re-implement using defines
to avoid modifying stdlib.c in case the user option is not selected.
1 parent 195da86 commit e73e6aa

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,12 +155,6 @@ 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-
"int " CPROVER_PREFIX "malloc_failure_mode="+
159-
std::to_string(config.ansi_c.malloc_failure_mode)+";\n"
160-
"int " CPROVER_PREFIX "malloc_failure_mode_return_null="+
161-
std::to_string(config.ansi_c.malloc_failure_mode_return_null)+";\n"
162-
"int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
163-
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
164158
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
165159
std::to_string(1 << (config.ansi_c.pointer_width -
166160
config.bv_encoding.object_bits - 1))+";\n"

src/ansi-c/cprover_library.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,21 @@ static std::string get_cprover_library_text(
2727
if(config.ansi_c.string_abstraction)
2828
library_text << "#define " CPROVER_PREFIX "STRING_ABSTRACTION\n";
2929

30+
if(
31+
config.ansi_c.malloc_failure_mode ==
32+
config.ansi_c.malloc_failure_mode_return_null)
33+
{
34+
library_text << "#define " CPROVER_PREFIX
35+
"MALLOC_FAILURE_MODE_RETURN_NULL\n";
36+
}
37+
if(
38+
config.ansi_c.malloc_failure_mode ==
39+
config.ansi_c.malloc_failure_mode_assert_then_assume)
40+
{
41+
library_text << "#define " CPROVER_PREFIX
42+
"MALLOC_FAILURE_MODE_ASSERT_THEN_ASSUME\n";
43+
}
44+
3045
// cprover_library.inc may not have been generated when running Doxygen, thus
3146
// make Doxygen skip this part
3247
/// \cond
@@ -45,7 +60,7 @@ std::string get_cprover_library_text(
4560
const struct cprover_library_entryt cprover_library[],
4661
const std::string &prologue)
4762
{
48-
std::ostringstream library_text(prologue);
63+
std::ostringstream library_text{prologue, std::ios_base::ate};
4964

5065
std::size_t count=0;
5166

src/ansi-c/library/cprover.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,8 @@ 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 int __CPROVER_malloc_failure_mode;
2019
extern __CPROVER_size_t __CPROVER_max_malloc_size;
2120

22-
// malloc failure modes
23-
extern int __CPROVER_malloc_failure_mode_return_null;
24-
extern int __CPROVER_malloc_failure_mode_assert_then_assume;
25-
2621
void __CPROVER_assume(__CPROVER_bool assumption) __attribute__((__noreturn__));
2722
void __CPROVER_assert(__CPROVER_bool assertion, const char *description);
2823
void __CPROVER_precondition(__CPROVER_bool assertion, const char *description);

src/ansi-c/library/stdlib.c

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -116,24 +116,17 @@ inline void *malloc(__CPROVER_size_t malloc_size)
116116
// and __CPROVER_allocate doesn't, but no one cares
117117
__CPROVER_HIDE:;
118118

119-
if(
120-
__CPROVER_malloc_failure_mode ==
121-
__CPROVER_malloc_failure_mode_return_null)
119+
#ifdef __CPROVER_MALLOC_FAILURE_MODE_RETURN_NULL
120+
if(malloc_size > __CPROVER_max_malloc_size)
122121
{
123-
if(malloc_size > __CPROVER_max_malloc_size)
124-
{
125-
return (void *)0;
126-
}
127-
}
128-
else if(
129-
__CPROVER_malloc_failure_mode ==
130-
__CPROVER_malloc_failure_mode_assert_then_assume)
131-
{
132-
__CPROVER_assert(
133-
malloc_size <= __CPROVER_max_malloc_size,
134-
"max allocation size exceeded");
135-
__CPROVER_assume(malloc_size <= __CPROVER_max_malloc_size);
122+
return (void *)0;
136123
}
124+
#endif
125+
#ifdef __CPROVER_MALLOC_FAILURE_MODE_ASSERT_THEN_ASSUME
126+
__CPROVER_assert(
127+
malloc_size <= __CPROVER_max_malloc_size, "max allocation size exceeded");
128+
__CPROVER_assume(malloc_size <= __CPROVER_max_malloc_size);
129+
#endif
137130

138131
void *malloc_res;
139132
malloc_res = __CPROVER_allocate(malloc_size, 0);

src/cpp/cprover_library.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,24 @@ static std::string get_cprover_library_text(
2323
library_text << "#line 1 \"<builtin-library>\"\n"
2424
<< "#undef inline\n";
2525

26+
if(config.ansi_c.string_abstraction)
27+
library_text << "#define " CPROVER_PREFIX "STRING_ABSTRACTION\n";
28+
29+
if(
30+
config.ansi_c.malloc_failure_mode ==
31+
config.ansi_c.malloc_failure_mode_return_null)
32+
{
33+
library_text << "#define " CPROVER_PREFIX
34+
"MALLOC_FAILURE_MODE_RETURN_NULL\n";
35+
}
36+
if(
37+
config.ansi_c.malloc_failure_mode ==
38+
config.ansi_c.malloc_failure_mode_assert_then_assume)
39+
{
40+
library_text << "#define " CPROVER_PREFIX
41+
"MALLOC_FAILURE_MODE_ASSERT_THEN_ASSUME\n";
42+
}
43+
2644
// cprover_library.inc may not have been generated when running Doxygen, thus
2745
// make Doxygen skip this part
2846
/// \cond

0 commit comments

Comments
 (0)