source: trunk/dll/fortify.h@ 1077

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

Enhance Fortify infrastructure
Add Fortify_SetOwner Fortify_ChangeOwner Fortify_ChangeScope
Add FORTIFY_VERBOSE_SCOPE_ENTER_EXIT support
Add more fm/2 Fortify tooling and rework existing tooling for correct nesting
Still lots to do for cross-thread allocations
Add misc.h
Add walkem.h

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.9 KB
Line 
1
2/* $Id: fortify.h 1077 2008-07-18 18:11:54Z 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 Fortify_ChangeScope
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);
194void Fortify_ChangeScope(void *pBlock, int delta);
195#endif
196
197/* Fortify versions of the ANSI C memory allocation functions */
198void *Fortify_malloc(size_t size, const char *file, unsigned long line);
199void *Fortify_realloc(void *ptr, size_t new_size, const char *file, unsigned long line);
200void *Fortify_calloc(size_t num, size_t size, const char *file, unsigned long line);
201void Fortify_free(void *uptr, const char *file, unsigned long line);
202
203/* Fortify versions of some non-ANSI C memory allocation functions */
204#ifdef FORTIFY_STRDUP
205 char *Fortify_strdup(const char *oldStr, const char *file, unsigned long line);
206#endif
207
208#ifdef __cplusplus
209/* Magic global variable */
210extern int gbl_FortifyMagic;
211#endif
212
213#ifdef __cplusplus
214}
215#endif
216
217#ifdef __cplusplus
218#include <new.h>
219
220 /* Fortify versions of new and delete */
221 void *operator new(size_t size);
222 void *operator new(size_t size, const char *file, int line); // 16 Jan 08 SHL
223 void operator delete(void *pointer);
224 void Fortify_PreDelete(const char *file, int line); // 16 Jan 08 SHL
225 void Fortify_PostDelete();
226
227 /* Some compilers use a different new operator for newing arrays.
228 * This includes GNU G++ (2.6.0) and Borland C++ (4.02)
229 */
230 #ifdef FORTIFY_PROVIDE_ARRAY_NEW
231 void *operator new[](size_t size);
232 void *operator new[](size_t size, const char *file, unsigned long line); // 16 Jan 08 SHL
233 #endif
234
235 /* Some compilers provide a different delete operator for deleting arrays.
236 * This incldues GNU G++ (2.6.0)
237 */
238 #ifdef FORTIFY_PROVIDE_ARRAY_DELETE
239 void operator delete[](void *pointer);
240 #endif
241
242#endif /* __cplusplus */
243
244#ifndef __FORTIFY_C__ /* Only define the macros if we're NOT in fortify.c */
245
246/* Add file and line information to the fortify calls */
247#ifdef FORTIFY
248 /* Core Fortify Functions */
249 #define Fortify_CheckAllMemory() Fortify_CheckAllMemory(__FILE__, __LINE__)
250 #define Fortify_ListAllMemory() Fortify_ListAllMemory (__FILE__, __LINE__)
251 #define Fortify_DumpAllMemory() Fortify_DumpAllMemory (__FILE__, __LINE__)
252 #define Fortify_CheckPointer(ptr) Fortify_CheckPointer(ptr, __FILE__, __LINE__)
253 #define Fortify_LabelPointer(ptr,str) Fortify_LabelPointer(ptr, str, __FILE__, __LINE__)
254 #define Fortify_EnterScope() Fortify_EnterScope(__FILE__, __LINE__)
255 #define Fortify_LeaveScope() Fortify_LeaveScope(__FILE__, __LINE__)
256 #define Fortify_OutputStatistics() Fortify_OutputStatistics(__FILE__, __LINE__)
257 #define Fortify_GetCurrentAllocation() Fortify_GetCurrentAllocation(__FILE__, __LINE__)
258 #define Fortify_SetAllocationLimit(x) Fortify_SetAllocationLimit(x, __FILE__, __LINE__)
259 #define Fortify_Disable() Fortify_Disable(__FILE__, __LINE__)
260
261 /* Fortify versions of the ANSI C memory allocation functions */
262 #define malloc(size) Fortify_malloc(size, __FILE__, __LINE__)
263 #define realloc(ptr,new_size) Fortify_realloc(ptr, new_size, __FILE__, __LINE__)
264 #define calloc(num,size) Fortify_calloc(num, size, __FILE__, __LINE__)
265 #define free(ptr) Fortify_free(ptr, __FILE__, __LINE__)
266
267 /* Fortify versions of some non-ANSI C memory allocation functions */
268 #ifdef FORTIFY_STRDUP
269 #define strdup(ptr) Fortify_strdup(ptr, __FILE__, __LINE__)
270 #endif
271
272 /* Fortify versions of new and delete */
273 #ifdef __cplusplus
274 #define Fortify_New new(__FILE__, __LINE__)
275 #define Fortify_Delete for(gbl_FortifyMagic = 1, \
276 Fortify_PreDelete(__FILE__, __LINE__); \
277 gbl_FortifyMagic; Fortify_PostDelete()) \
278 gbl_FortifyMagic = 0, delete
279 #define new Fortify_New
280 #define delete Fortify_Delete
281 #endif /* __cplusplus */
282
283#else /* Define the special fortify functions away to nothing */
284
285 // 17 Jul 08 SHL fixme to avoid spurious OpenWatcom warnings
286 #define Fortify_CheckAllMemory() 0
287 #define Fortify_ListAllMemory() 0
288 #define Fortify_DumpAllMemory() 0
289 #define Fortify_CheckPointer(ptr) 1
290 #define Fortify_LabelPointer(ptr,str)
291 #define Fortify_SetOutputFunc() 0
292 #define Fortify_SetMallocFailRate(p) 0
293 #define Fortify_EnterScope() 0
294 #define Fortify_LeaveScope() 0
295 #define Fortify_OutputStatistics() 0
296 #define Fortify_GetCurrentAllocation() 0
297 #define Fortify_SetAllocationLimit(x) 0
298 #define Fortify_Disable() 0
299 #define Fortify_SetOwner() 0
300 #define Fortify_ChangeOwner 0
301
302 #ifdef __cplusplus
303 #define Fortify_New new
304 #define Fortify_Delete delete
305 #endif /* __cplusplus */
306
307#endif /* FORTIFY */
308#endif /* __FORTIFY_C__ */
309#endif /* __FORTIFY_H__ */
Note: See TracBrowser for help on using the repository browser.