CBMC
new.c
Go to the documentation of this file.
1 /* FUNCTION: __new */
2 
3 __CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
4 const void *__CPROVER_new_object = 0;
5 __CPROVER_bool __CPROVER_malloc_is_new_array = 0;
6 
7 inline void *__new(__typeof__(sizeof(int)) malloc_size)
8 {
9  // The constructor call is done by the front-end.
10  // This just does memory allocation.
11  __CPROVER_HIDE:;
12  void *res;
13  res = __CPROVER_allocate(malloc_size, 0);
14 
15  // non-deterministically record the object for delete/delete[] checking
16  __CPROVER_bool record_malloc=__VERIFIER_nondet___CPROVER_bool();
17  __CPROVER_new_object = record_malloc ? res : __CPROVER_new_object;
19 
20  // detect memory leaks
21  __CPROVER_bool record_may_leak=__VERIFIER_nondet___CPROVER_bool();
22  __CPROVER_memory_leak=record_may_leak?res:__CPROVER_memory_leak;
23 
24  return res;
25 }
26 
27 /* FUNCTION: __new_array */
28 
29 __CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
30 #ifndef LIBRARY_CHECK
31 const void *__CPROVER_new_object = 0;
32 __CPROVER_bool __CPROVER_malloc_is_new_array = 0;
33 #endif
34 
35 inline void *__new_array(__CPROVER_size_t count, __CPROVER_size_t size)
36 {
37  // The constructor call is done by the front-end.
38  // This just does memory allocation.
39  __CPROVER_HIDE:;
40  void *res;
41  res = __CPROVER_allocate(size*count, 0);
42 
43  // non-deterministically record the object for delete/delete[] checking
44  __CPROVER_bool record_malloc=__VERIFIER_nondet___CPROVER_bool();
45  __CPROVER_new_object = record_malloc ? res : __CPROVER_new_object;
47 
48  // detect memory leaks
49  __CPROVER_bool record_may_leak=__VERIFIER_nondet___CPROVER_bool();
50  __CPROVER_memory_leak=record_may_leak?res:__CPROVER_memory_leak;
51 
52  return res;
53 }
54 
55 /* FUNCTION: __placement_new */
56 
57 inline void *__placement_new(__typeof__(sizeof(int)) malloc_size, void *p)
58 {
59  // The constructor call is done by the front-end.
60  // The allocation is done by the user. So this does nothing.
61  __CPROVER_HIDE:;
62  (void)malloc_size;
63  return p;
64 }
65 
66 /* FUNCTION: __delete */
67 
68 void __CPROVER_deallocate(void *);
69 __CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
70 #ifndef LIBRARY_CHECK
71 const void *__CPROVER_new_object = 0;
72 __CPROVER_bool __CPROVER_malloc_is_new_array = 0;
73 #endif
74 
75 inline void __delete(void *ptr)
76 {
77  __CPROVER_HIDE:;
78  // is it dynamic?
80  "delete argument must be dynamic object");
82  "delete argument must have offset zero");
83 
84  // catch double delete
85  __CPROVER_precondition(ptr==0 || __CPROVER_deallocated!=ptr, "double delete");
86 
87  // catch people who call delete for objects allocated with new[]
90  "delete of array object");
91 
92  // If ptr is NULL, no operation is performed.
93  // This is a requirement by the standard, not generosity!
94  if(ptr!=0)
95  {
97 
98  // detect memory leaks
99  if(__CPROVER_memory_leak==ptr)
101  }
102 }
103 
104 /* FUNCTION: __delete_array */
105 
106 void __CPROVER_deallocate(void *);
107 __CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
108 #ifndef LIBRARY_CHECK
109 const void *__CPROVER_new_object = 0;
110 __CPROVER_bool __CPROVER_malloc_is_new_array = 0;
111 #endif
112 
113 inline void __delete_array(void *ptr)
114 {
115  __CPROVER_HIDE:;
116  // If ptr is NULL, no operation is performed.
117  // This is a requirement by the standard, not generosity!
118 
119  // is it dynamic?
121  "delete argument must be dynamic object");
123  "delete argument must have offset zero");
124 
125  // catch double delete
127  "double delete");
128 
129  // catch people who call delete[] for objects allocated with new
132  "delete[] of non-array object");
133 
134  if(ptr!=0)
135  {
137 
138  // detect memory leaks
140  }
141 }
void * __CPROVER_allocate(__CPROVER_size_t size, __CPROVER_bool zero)
typedef __typeof__(sizeof(int)) __CPROVER_size_t
const void * __CPROVER_deallocated
const void * __CPROVER_memory_leak
__CPROVER_size_t __CPROVER_POINTER_OFFSET(const void *)
void __CPROVER_precondition(__CPROVER_bool precondition, const char *description)
__CPROVER_bool __CPROVER_DYNAMIC_OBJECT(const void *)
void __delete(void *ptr)
Definition: new.c:75
void * __new(__typeof__(sizeof(int)) malloc_size)
Definition: new.c:7
void * __placement_new(__typeof__(sizeof(int)) malloc_size, void *p)
Definition: new.c:57
void __delete_array(void *ptr)
Definition: new.c:113
void __CPROVER_deallocate(void *)
Definition: stdlib.c:670
const void * __CPROVER_new_object
Definition: new.c:4
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void)
void * __new_array(__CPROVER_size_t count, __CPROVER_size_t size)
Definition: new.c:35
__CPROVER_bool __CPROVER_malloc_is_new_array
Definition: new.c:5