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