Skip to content

Commit 5d74a03

Browse files
committed
Hide __CPROVER_deallocated updates behind a function
This avoids repeating similar code in several places.
1 parent 970cc26 commit 5d74a03

File tree

5 files changed

+17
-13
lines changed

5 files changed

+17
-13
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ void ansi_c_internal_additions(std::string &code)
179179
"void *" CPROVER_PREFIX "allocate("
180180
CPROVER_PREFIX "size_t size, " CPROVER_PREFIX "bool zero);\n"
181181
"const void *" CPROVER_PREFIX "alloca_object = 0;\n"
182+
"void " CPROVER_PREFIX "deallocate(void *);\n"
182183

183184
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
184185
integer2string(max_malloc_size(config.ansi_c.pointer_width, config

src/ansi-c/library/cprover.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef __typeof__(sizeof(int)) __CPROVER_size_t;
1919
typedef signed long long __CPROVER_ssize_t;
2020

2121
void *__CPROVER_allocate(__CPROVER_size_t size, __CPROVER_bool zero);
22+
void __CPROVER_deallocate(void *);
2223
extern const void *__CPROVER_deallocated;
2324
extern const void *__CPROVER_new_object;
2425
extern __CPROVER_bool __CPROVER_malloc_is_new_array;

src/ansi-c/library/mman.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,7 @@ int munmap(void *addr, __CPROVER_size_t length)
112112
{
113113
(void)length;
114114

115-
if(__VERIFIER_nondet___CPROVER_bool())
116-
__CPROVER_deallocated = addr;
115+
__CPROVER_deallocate(addr);
117116

118117
return 0;
119118
}
@@ -126,8 +125,7 @@ int _munmap(void *addr, __CPROVER_size_t length)
126125
{
127126
(void)length;
128127

129-
if(__VERIFIER_nondet___CPROVER_bool())
130-
__CPROVER_deallocated = addr;
128+
__CPROVER_deallocate(addr);
131129

132130
return 0;
133131
}

src/ansi-c/library/stdlib.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,9 +271,7 @@ inline void free(void *ptr)
271271

272272
if(ptr!=0)
273273
{
274-
// non-deterministically record as deallocated
275-
__CPROVER_bool record=__VERIFIER_nondet___CPROVER_bool();
276-
if(record) __CPROVER_deallocated=ptr;
274+
__CPROVER_deallocate(ptr);
277275

278276
// detect memory leaks
279277
if(__CPROVER_memory_leak==ptr)
@@ -590,3 +588,13 @@ __CPROVER_HIDE:;
590588
__CPROVER_assume(result >= 0);
591589
return result;
592590
}
591+
592+
/* FUNCTION: __CPROVER_deallocate */
593+
594+
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
595+
596+
void __CPROVER_deallocate(void *ptr)
597+
{
598+
if(__VERIFIER_nondet___CPROVER_bool())
599+
__CPROVER_deallocated = ptr;
600+
}

src/cpp/library/new.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ inline void __delete(void *ptr)
8888
// This is a requirement by the standard, not generosity!
8989
if(ptr!=0)
9090
{
91-
// non-deterministically record as deallocated
92-
__CPROVER_bool record=__VERIFIER_nondet___CPROVER_bool();
93-
__CPROVER_deallocated=record?ptr:__CPROVER_deallocated;
91+
__CPROVER_deallocate(ptr);
9492

9593
// detect memory leaks
9694
if(__CPROVER_memory_leak==ptr)
@@ -125,9 +123,7 @@ inline void __delete_array(void *ptr)
125123

126124
if(ptr!=0)
127125
{
128-
// non-deterministically record as deallocated
129-
__CPROVER_bool record=__VERIFIER_nondet___CPROVER_bool();
130-
__CPROVER_deallocated=record?ptr:__CPROVER_deallocated;
126+
__CPROVER_deallocate(ptr);
131127

132128
// detect memory leaks
133129
if(__CPROVER_memory_leak==ptr) __CPROVER_memory_leak=0;

0 commit comments

Comments
 (0)