Watt-32 tcp/ip  2.2 dev-rel.10
fortify.h
Go to the documentation of this file.
1 
6 /*
7  * This software is not public domain. All material in
8  * this archive is (C) Copyright 1995 Simon P. Bullen. The
9  * software is freely distributable, with the condition that
10  * no more than a nominal fee is charged for media.
11  * Everything in this distribution must be kept together, in
12  * original, unmodified form.
13  * The software may be modified for your own personal use,
14  * but modified files may not be distributed.
15  * The material is provided "as is" without warranty of
16  * any kind. The author accepts no responsibility for damage
17  * caused by this software.
18  * This software may not be used in any way by Microsoft
19  * Corporation or its subsidiaries, or current employees of
20  * Microsoft Corporation or its subsidiaries.
21  * This software may not be used for the construction,
22  * development, production, or testing of weapon systems of
23  * any kind.
24  * This software may not be used for the construction,
25  * development, production, or use of plants/installations
26  * which include the processing of radioactive/fissionable
27  * material.
28  */
29 
30 /*
31  * If you use this software at all, I'd love to hear from
32  * you. All questions, criticisms, suggestions, praise and
33  * postcards are most welcome.
34  *
35  * email: sbullen@cybergraphic.com.au
36  *
37  * snail: Simon P. Bullen
38  * PO BOX 12138
39  * A'Beckett St.
40  * Melbourne 3000
41  * Australia
42  */
43 
44 #ifndef __FORTIFY_H__
45 #define __FORTIFY_H__
46 
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #undef malloc
51 #undef realloc
52 #undef calloc
53 #undef free
54 #undef strdup
55 
56 #if (defined(_MSC_VER) && (_MSC_VER <= 600)) || defined(__CCDL__)
57  /*
58  * MSC <= 6.0 has a identifier length of 32. Don't bother
59  * rewriting to supress warnings. LadSoft doesn't handle this either.
60  */
61  #undef USE_FORTIFY
62 #else /* rest of file */
63 
64 /* the user's options */
65 #include "ufortify.h"
66 
67 /* Ensure the configuration parameters have sensible defaults */
68 #ifndef FORTIFY_STORAGE
69  #define FORTIFY_STORAGE
70 #endif
71 
72 #ifndef FORTIFY_ALIGNMENT
73  #define FORTIFY_ALIGNMENT sizeof(double)
74 #endif
75 
76 #ifndef FORTIFY_BEFORE_SIZE
77  #define FORTIFY_BEFORE_SIZE 32
78 #endif
79 
80 #ifndef FORTIFY_BEFORE_VALUE
81  #define FORTIFY_BEFORE_VALUE 0xA3
82 #endif
83 
84 #ifndef FORTIFY_AFTER_SIZE
85  #define FORTIFY_AFTER_SIZE 32
86 #endif
87 
88 #ifndef FORTIFY_AFTER_VALUE
89  #define FORTIFY_AFTER_VALUE 0xA5
90 #endif
91 
92 #ifndef FORTIFY_FILL_ON_ALLOCATE_VALUE
93  #define FORTIFY_FILL_ON_ALLOCATE_VALUE 0xA7
94 #endif
95 
96 #ifndef FORTIFY_FILL_ON_DEALLOCATE_VALUE
97  #define FORTIFY_FILL_ON_DEALLOCATE_VALUE 0xA9
98 #endif
99 
100 #if 0
101  #ifndef FORTIFY_LOCK
102  #define FORTIFY_LOCK()
103  #endif
104 
105  #ifndef FORTIFY_UNLOCK
106  #define FORTIFY_UNLOCK()
107  #endif
108 #endif
109 
110 #ifndef FORTIFY_CHECKSUM_VALUE
111  #define FORTIFY_CHECKSUM_VALUE 0x0AD0
112 #endif
113 
114 #ifndef FORTIFY_DELETE_STACK_SIZE
115  #define FORTIFY_DELETE_STACK_SIZE 256
116 #endif
117 
118 #ifndef FORTIFY_NEW_HANDLER_FUNC
119  typedef void (*Fortify_NewHandlerFunc)(void);
120  #define FORTIFY_NEW_HANDLER_FUNC Fortify_NewHandlerFunc
121 #endif
122 
123 /*
124  * Code to detect and configure for various compilers lives here.
125  */
126 
127 #ifdef __GNUG__
128  /* GCC configuration */
129  #define FORTIFY_PROVIDE_ARRAY_NEW
130  #define FORTIFY_PROVIDE_ARRAY_DELETE
131 #endif
132 
133 #ifdef __HIGHC__
134  /* Metaware HighC configuration */
135  #define FORTIFY_PROVIDE_ARRAY_NEW
136  #define FORTIFY_PROVIDE_ARRAY_DELETE
137 #endif
138 
139 #ifdef __BC45__
140  /* Borland C++ 4.5 configuration */
141  #define FORTIFY_PROVIDE_ARRAY_NEW
142  #define FORTIFY_PROVIDE_ARRAY_DELETE
143  #define FORTIFY_FAIL_ON_ZERO_MALLOC
144 #endif
145 
146 #ifdef __SASC
147  /* SAS configuration */
148  #define FORTIFY_FAIL_ON_ZERO_MALLOC
149 #endif
150 
151 /* Allocators */
152 #define Fortify_Allocator_malloc 0 /* ANSI C */
153 #define Fortify_Allocator_calloc 1 /* ANSI C */
154 #define Fortify_Allocator_realloc 2 /* ANSI C */
155 #define Fortify_Allocator_strdup 3 /* C */
156 #define Fortify_Allocator_new 4 /* ANSI C++ */
157 #define Fortify_Allocator_array_new 5 /* Some C++ */
158 #define Fortify_Allocator_GlobalAlloc 6 /* Win32 */
159 
160 /* Deallocators */
161 #define Fortify_Deallocator_nobody 0
162 #define Fortify_Deallocator_free 1 /* ANSI C */
163 #define Fortify_Deallocator_realloc 2 /* ANSI C */
164 #define Fortify_Deallocator_delete 3 /* ANSI C++ */
165 #define Fortify_Deallocator_array_delete 4 /* Some C++ */
166 #define Fortify_Allocator_GlobalFree 5 /* Win32 */
167 
168 /* Public Fortify Types */
169 typedef void (*Fortify_OutputFuncPtr)(const char *);
170 
171 #ifdef __cplusplus
172 extern "C" {
173 #endif
174 
175 /* Core Fortify Functions */
176 void *Fortify_Allocate (size_t size, unsigned char allocator, unsigned long flags, const char *file, unsigned long line);
177 void Fortify_Deallocate(void *uptr, unsigned char deallocator, const char *file, unsigned long line);
178 unsigned long Fortify_CheckAllMemory(const char *file, unsigned long line);
179 unsigned long Fortify_ListAllMemory (const char *file, unsigned long line);
180 unsigned long Fortify_DumpAllMemory (const char *file, unsigned long line);
181 int Fortify_CheckPointer(void *uptr, const char *file, unsigned long line);
182 void Fortify_LabelPointer(void *uptr, const char *label, const char *file, unsigned long line);
183 unsigned char Fortify_EnterScope(const char *file, unsigned long line);
184 unsigned char Fortify_LeaveScope(const char *file, unsigned long line);
185 void Fortify_OutputStatistics(const char *file, unsigned long line);
186 unsigned long Fortify_GetCurrentAllocation(const char *file, unsigned long line);
187 void Fortify_SetAllocationLimit(unsigned long Limit, const char *file, unsigned long line);
188 int Fortify_SetFailRate(int Percent);
189 void Fortify_Disable(const char *file, unsigned long line);
190 void Fortify_Enable (const char *file, unsigned long line);
191 int Fortify_SetAllocateFailRate (int Percent);
192 
193 Fortify_OutputFuncPtr Fortify_SetOutputFunc(Fortify_OutputFuncPtr Output);
194 
195 /* Fortify versions of the ANSI C memory allocation functions */
196 void *Fortify_malloc(size_t size, const char *file, unsigned long line);
197 void *Fortify_realloc(void *ptr, size_t new_size, const char *file, unsigned long line);
198 void *Fortify_calloc(size_t num, size_t size, const char *file, unsigned long line);
199 void Fortify_free(void *uptr, const char *file, unsigned long line);
200 
201 /* Fortify versions of some non-ANSI C memory allocation functions */
202 #ifdef FORTIFY_STRDUP
203  char *Fortify_strdup(const char *oldStr, const char *file, unsigned long line);
204 #endif
205 
206 
207 #ifdef __cplusplus
208  /* Magic global variable */
209  extern int gbl_FortifyMagic;
210  };
211 
212  #include <new>
213 
214  /* Fortify versions of new and delete */
215  void *operator new(size_t size);
216  void *operator new(size_t size, const char *file, unsigned long line);
217  void operator delete(void *pointer);
218  void Fortify_PreDelete(const char *file, unsigned long line);
219  void Fortify_PostDelete();
220 
221  /* Some compilers use a different new operator for newing arrays.
222  * This includes GNU G++ (2.6.0) and Borland C++ (4.02)
223  */
224  #ifdef FORTIFY_PROVIDE_ARRAY_NEW
225  void *operator new[](size_t size);
226  void *operator new[](size_t size, const char *file, unsigned long line);
227  #endif
228 
229  /* Some compilers provide a different delete operator for deleting arrays.
230  * This incldues GNU G++ (2.6.0)
231  */
232  #ifdef FORTIFY_PROVIDE_ARRAY_DELETE
233  void operator delete[](void *pointer);
234  #endif
235 #endif /* __cplusplus */
236 
237 #ifndef __FORTIFY_C__ /* Only define the macros if we're NOT in fortify.c */
238 
239 /* Add file and line information to the fortify calls */
240 #if defined(USE_FORTIFY)
241  /* Core Fortify Functions */
242  #define Fortify_CheckAllMemory() Fortify_CheckAllMemory(__FILE__, __LINE__)
243  #define Fortify_ListAllMemory() Fortify_ListAllMemory (__FILE__, __LINE__)
244  #define Fortify_DumpAllMemory() Fortify_DumpAllMemory (__FILE__, __LINE__)
245  #define Fortify_CheckPointer(ptr) Fortify_CheckPointer(ptr, __FILE__, __LINE__)
246  #define Fortify_LabelPointer(ptr,str) Fortify_LabelPointer(ptr, str, __FILE__, __LINE__)
247  #define Fortify_EnterScope() Fortify_EnterScope(__FILE__, __LINE__)
248  #define Fortify_LeaveScope() Fortify_LeaveScope(__FILE__, __LINE__)
249  #define Fortify_OutputStatistics() Fortify_OutputStatistics(__FILE__, __LINE__)
250  #define Fortify_GetCurrentAllocation() Fortify_GetCurrentAllocation(__FILE__, __LINE__)
251  #define Fortify_SetAllocationLimit(x) Fortify_SetAllocationLimit(x, __FILE__, __LINE__)
252  #define Fortify_Disable() Fortify_Disable(__FILE__, __LINE__)
253 
254  /* Fortify versions of the ANSI C memory allocation functions */
255  #define malloc(size) Fortify_malloc(size, __FILE__, __LINE__)
256  #define realloc(ptr,new_size) Fortify_realloc(ptr, new_size, __FILE__, __LINE__)
257  #define calloc(num,size) Fortify_calloc(num, size, __FILE__, __LINE__)
258  #define free(ptr) Fortify_free(ptr, __FILE__, __LINE__)
259 
260  /* Fortify versions of some non-ANSI C memory allocation functions */
261  #ifdef FORTIFY_STRDUP
262  #define strdup(ptr) Fortify_strdup(ptr, __FILE__, __LINE__)
263  #endif
264 
265  #if defined(WIN32) && defined(FORTIFY_GLOBAL_REPLACE)
266 
267  void *Fortify_GlobalAlloc (UINT flags, DWORD size, const char *file, DWORD line);
268  BOOL Fortify_GlobalFree (void *ptr, const char *file, DWORD line);
269 
270  #undef GlobalAllocPtr
271  #define GlobalAllocPtr(flags, size) \
272  Fortify_GlobalAlloc (flags, size, __FILE__, __LINE__)
273  #undef GlobalFreePtr
274  #define GlobalFreePtr(ptr) \
275  Fortify_GlobalFree (ptr, __FILE__, __LINE__)
276  #endif
277 
278  /* Fortify versions of new and delete */
279  #ifdef __cplusplus
280  #define Fortify_New new (__FILE__, __LINE__)
281  #define Fortify_Delete for (gbl_FortifyMagic = 1, \
282  Fortify_PreDelete(__FILE__, __LINE__); \
283  gbl_FortifyMagic; Fortify_PostDelete()) \
284  gbl_FortifyMagic = 0, delete
285  #define new Fortify_New
286  #define delete Fortify_Delete
287  #endif
288 
289 #else /* Define the special fortify functions away to nothing */
290 
291  #define Fortify_CheckAllMemory() ((void)0)
292  #define Fortify_ListAllMemory() ((void)0)
293  #define Fortify_DumpAllMemory() ((void)0)
294  #define Fortify_CheckPointer(ptr) 1
295  #define Fortify_LabelPointer(ptr,str) ((void)0)
296  #define Fortify_SetOutputFunc(func) ((void)0)
297  #define Fortify_SetMallocFailRate(p) ((void)0)
298  #define Fortify_EnterScope() ((void)0)
299  #define Fortify_LeaveScope() ((void)0)
300  #define Fortify_OutputStatistics() ((void)0)
301  #define Fortify_GetCurrentAllocation() ((void)0)
302  #define Fortify_SetAllocationLimit(x) ((void)0)
303  #define Fortify_Disable() ((void)0)
304 
305  #ifdef __cplusplus
306  #define Fortify_New new
307  #define Fortify_Delete delete
308  #endif
309 
310 #endif /* USE_FORTIFY */
311 #endif /* __FORTIFY_C__ */
312 #endif /* __FORTIFY_H__ */
313 #endif /* _MSC_VER <= 6.0 || __CCDL__ */