source: trunk/dll/fortify.h@ 1076

Last change on this file since 1076 was 1072, checked in by Steven Levine, 17 years ago

Add Fortify_SetOwner Fortify_ChangeOwner to support cross thread allocations

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