| 1 |
|
|---|
| 2 | /* $Id: fortify.h 1078 2008-07-19 04:08:02Z 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_PresetOwner Fortify_BecomeOwner Fortify_ChangeScope
|
|---|
| 45 | 18 Jul 08 SHL Rename Fortify_ChangeOwner to Fortify_BecomeOwner
|
|---|
| 46 | 18 Jul 08 SHL Add reworked Fortify_SetOwner
|
|---|
| 47 | 18 Jul 08 SHL Drop Fortify_PresetOwner
|
|---|
| 48 | */
|
|---|
| 49 |
|
|---|
| 50 | #ifndef __FORTIFY_H__
|
|---|
| 51 | #define __FORTIFY_H__
|
|---|
| 52 |
|
|---|
| 53 | #include <stdlib.h> // Must include before fortify defintions
|
|---|
| 54 | // 16 Jan 08 SHL Ensure
|
|---|
| 55 | #ifdef __BORLANDC__
|
|---|
| 56 | #ifdef __OS2__
|
|---|
| 57 | #include <alloc.h> // Must include before fortify defintions
|
|---|
| 58 | #endif
|
|---|
| 59 | #endif
|
|---|
| 60 | #include <string.h>
|
|---|
| 61 |
|
|---|
| 62 | /* the user's options */
|
|---|
| 63 | #include "ufortify.h"
|
|---|
| 64 |
|
|---|
| 65 | #if defined(__WATCOMC__) && defined(_MT)
|
|---|
| 66 | #define MT_SCOPES 1
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | /* Ensure the configuration parameters have sensible defaults */
|
|---|
| 70 | #ifndef FORTIFY_STORAGE
|
|---|
| 71 | #define FORTIFY_STORAGE
|
|---|
| 72 | #endif
|
|---|
| 73 |
|
|---|
| 74 | #ifndef FORTIFY_ALIGNMENT
|
|---|
| 75 | #define FORTIFY_ALIGNMENT sizeof(double)
|
|---|
| 76 | #endif
|
|---|
| 77 |
|
|---|
| 78 | #ifndef FORTIFY_BEFORE_SIZE
|
|---|
| 79 | #define FORTIFY_BEFORE_SIZE 32
|
|---|
| 80 | #endif
|
|---|
| 81 | #ifndef FORTIFY_BEFORE_VALUE
|
|---|
| 82 | #define FORTIFY_BEFORE_VALUE 0xA3
|
|---|
| 83 | #endif
|
|---|
| 84 |
|
|---|
| 85 | #ifndef FORTIFY_AFTER_SIZE
|
|---|
| 86 | #define FORTIFY_AFTER_SIZE 32
|
|---|
| 87 | #endif
|
|---|
| 88 |
|
|---|
| 89 | #ifndef FORTIFY_AFTER_VALUE
|
|---|
| 90 | #define FORTIFY_AFTER_VALUE 0xA5
|
|---|
| 91 | #endif
|
|---|
| 92 |
|
|---|
| 93 | #ifndef FORTIFY_FILL_ON_ALLOCATE_VALUE
|
|---|
| 94 | #define FORTIFY_FILL_ON_ALLOCATE_VALUE 0xA7
|
|---|
| 95 | #endif
|
|---|
| 96 |
|
|---|
| 97 | #ifndef FORTIFY_FILL_ON_DEALLOCATE_VALUE
|
|---|
| 98 | #define FORTIFY_FILL_ON_DEALLOCATE_VALUE 0xA9
|
|---|
| 99 | #endif
|
|---|
| 100 |
|
|---|
| 101 | #ifndef FORTIFY_LOCK
|
|---|
| 102 | #define FORTIFY_LOCK()
|
|---|
| 103 | #endif
|
|---|
| 104 |
|
|---|
| 105 | #ifndef FORTIFY_UNLOCK
|
|---|
| 106 | #define FORTIFY_UNLOCK()
|
|---|
| 107 | #endif
|
|---|
| 108 |
|
|---|
| 109 | #ifndef FORTIFY_CHECKSUM_VALUE
|
|---|
| 110 | #define FORTIFY_CHECKSUM_VALUE 0x0AD0
|
|---|
| 111 | #endif
|
|---|
| 112 |
|
|---|
| 113 | #ifndef FORTIFY_DELETE_STACK_SIZE
|
|---|
| 114 | #define FORTIFY_DELETE_STACK_SIZE 256
|
|---|
| 115 | #endif
|
|---|
| 116 |
|
|---|
| 117 | #ifndef FORTIFY_NEW_HANDLER_FUNC
|
|---|
| 118 | typedef void (*Fortify_NewHandlerFunc)(void);
|
|---|
| 119 | #define FORTIFY_NEW_HANDLER_FUNC Fortify_NewHandlerFunc
|
|---|
| 120 | #endif
|
|---|
| 121 |
|
|---|
| 122 | /*
|
|---|
| 123 | * Code to detect and configure for various compilers lives here.
|
|---|
| 124 | */
|
|---|
| 125 |
|
|---|
| 126 | #ifdef __GNUG__
|
|---|
| 127 | /* GCC configuration */
|
|---|
| 128 | #define FORTIFY_PROVIDE_ARRAY_NEW
|
|---|
| 129 | #define FORTIFY_PROVIDE_ARRAY_DELETE
|
|---|
| 130 | #endif
|
|---|
| 131 |
|
|---|
| 132 | #ifdef __BC45__
|
|---|
| 133 | /* Borland C++ 4.5 configuration */
|
|---|
| 134 | #define FORTIFY_PROVIDE_ARRAY_NEW
|
|---|
| 135 | #define FORTIFY_PROVIDE_ARRAY_DELETE
|
|---|
| 136 | #define FORTIFY_FAIL_ON_ZERO_MALLOC
|
|---|
| 137 | #endif
|
|---|
| 138 |
|
|---|
| 139 | // 16 Jan 08 SHL
|
|---|
| 140 | #ifdef __BORLANDC__
|
|---|
| 141 | #ifdef __OS2__
|
|---|
| 142 | /* Borland C++ 2.0 OS/2 configuration */
|
|---|
| 143 | #define FORTIFY_PROVIDE_ARRAY_NEW
|
|---|
| 144 | #define FORTIFY_PROVIDE_ARRAY_DELETE
|
|---|
| 145 | #define FORTIFY_FAIL_ON_ZERO_MALLOC
|
|---|
| 146 | #define FORTIFY_STRDUP // have non-ANSI strdup()
|
|---|
| 147 | #endif
|
|---|
| 148 | #endif
|
|---|
| 149 |
|
|---|
| 150 | #ifdef __SASC
|
|---|
| 151 | /* SAS configuration */
|
|---|
| 152 | #define FORTIFY_FAIL_ON_ZERO_MALLOC
|
|---|
| 153 | #endif
|
|---|
| 154 |
|
|---|
| 155 | /* Allocators */
|
|---|
| 156 | #define Fortify_Allocator_malloc 0 /* ANSI C */
|
|---|
| 157 | #define Fortify_Allocator_calloc 1 /* ANSI C */
|
|---|
| 158 | #define Fortify_Allocator_realloc 2 /* ANSI C */
|
|---|
| 159 | #define Fortify_Allocator_strdup 3 /* C */
|
|---|
| 160 | #define Fortify_Allocator_new 4 /* ANSI C++ */
|
|---|
| 161 | #define Fortify_Allocator_array_new 5 /* Some C++ */
|
|---|
| 162 |
|
|---|
| 163 | /* Deallocators */
|
|---|
| 164 | #define Fortify_Deallocator_nobody 0
|
|---|
| 165 | #define Fortify_Deallocator_free 1 /* ANSI C */
|
|---|
| 166 | #define Fortify_Deallocator_realloc 2 /* ANSI C */
|
|---|
| 167 | #define Fortify_Deallocator_delete 3 /* ANSI C++ */
|
|---|
| 168 | #define Fortify_Deallocator_array_delete 4 /* Some C++ */
|
|---|
| 169 |
|
|---|
| 170 | /* Public Fortify Types */
|
|---|
| 171 | typedef void (*Fortify_OutputFuncPtr)(const char *);
|
|---|
| 172 |
|
|---|
| 173 | #ifdef __cplusplus
|
|---|
| 174 | extern "C" {
|
|---|
| 175 | #endif
|
|---|
| 176 |
|
|---|
| 177 | /* Core Fortify Functions */
|
|---|
| 178 | void *Fortify_Allocate (size_t size, unsigned char allocator, const char *file, unsigned long line);
|
|---|
| 179 | void Fortify_Deallocate(void *uptr, unsigned char deallocator, const char *file, unsigned long line);
|
|---|
| 180 | unsigned long Fortify_CheckAllMemory(const char *file, unsigned long line);
|
|---|
| 181 | unsigned long Fortify_ListAllMemory (const char *file, unsigned long line);
|
|---|
| 182 | unsigned long Fortify_DumpAllMemory (const char *file, unsigned long line);
|
|---|
| 183 | int Fortify_CheckPointer(void *uptr, const char *file, unsigned long line);
|
|---|
| 184 | void Fortify_LabelPointer(void *uptr, const char *label, const char *file, unsigned long line);
|
|---|
| 185 | unsigned char Fortify_EnterScope(const char *file, unsigned long line);
|
|---|
| 186 | unsigned char Fortify_LeaveScope(const char *file, unsigned long line);
|
|---|
| 187 | void Fortify_OutputStatistics(const char *file, unsigned long line);
|
|---|
| 188 | unsigned long Fortify_GetCurrentAllocation(const char *file, unsigned long line);
|
|---|
| 189 | void Fortify_SetAllocationLimit(unsigned long Limit, const char *file, unsigned long line);
|
|---|
| 190 | int Fortify_SetFailRate(int Percent);
|
|---|
| 191 | Fortify_OutputFuncPtr Fortify_SetOutputFunc(Fortify_OutputFuncPtr Output);
|
|---|
| 192 | void Fortify_Disable(const char *file, unsigned long line);
|
|---|
| 193 |
|
|---|
| 194 | #ifdef MT_SCOPES
|
|---|
| 195 | #if 0 // 18 Jul 08 SHL fixme to be gone
|
|---|
| 196 | void Fortify_PresetOwner(long lOwnerTID);
|
|---|
| 197 | #endif
|
|---|
| 198 | void Fortify_BecomeOwner(void *pBlock);
|
|---|
| 199 | void Fortify_SetOwner(void *pBlock, unsigned ordinal);
|
|---|
| 200 | void Fortify_ChangeScope(void *pBlock, int delta);
|
|---|
| 201 | void Fortify_SetScope(void *pBlock, unsigned char scope);
|
|---|
| 202 | #endif
|
|---|
| 203 |
|
|---|
| 204 | /* Fortify versions of the ANSI C memory allocation functions */
|
|---|
| 205 | void *Fortify_malloc(size_t size, const char *file, unsigned long line);
|
|---|
| 206 | void *Fortify_realloc(void *ptr, size_t new_size, const char *file, unsigned long line);
|
|---|
| 207 | void *Fortify_calloc(size_t num, size_t size, const char *file, unsigned long line);
|
|---|
| 208 | void Fortify_free(void *uptr, const char *file, unsigned long line);
|
|---|
| 209 |
|
|---|
| 210 | /* Fortify versions of some non-ANSI C memory allocation functions */
|
|---|
| 211 | #ifdef FORTIFY_STRDUP
|
|---|
| 212 | char *Fortify_strdup(const char *oldStr, const char *file, unsigned long line);
|
|---|
| 213 | #endif
|
|---|
| 214 |
|
|---|
| 215 | #ifdef __cplusplus
|
|---|
| 216 | /* Magic global variable */
|
|---|
| 217 | extern int gbl_FortifyMagic;
|
|---|
| 218 | #endif
|
|---|
| 219 |
|
|---|
| 220 | #ifdef __cplusplus
|
|---|
| 221 | }
|
|---|
| 222 | #endif
|
|---|
| 223 |
|
|---|
| 224 | #ifdef __cplusplus
|
|---|
| 225 | #include <new.h>
|
|---|
| 226 |
|
|---|
| 227 | /* Fortify versions of new and delete */
|
|---|
| 228 | void *operator new(size_t size);
|
|---|
| 229 | void *operator new(size_t size, const char *file, int line); // 16 Jan 08 SHL
|
|---|
| 230 | void operator delete(void *pointer);
|
|---|
| 231 | void Fortify_PreDelete(const char *file, int line); // 16 Jan 08 SHL
|
|---|
| 232 | void Fortify_PostDelete();
|
|---|
| 233 |
|
|---|
| 234 | /* Some compilers use a different new operator for newing arrays.
|
|---|
| 235 | * This includes GNU G++ (2.6.0) and Borland C++ (4.02)
|
|---|
| 236 | */
|
|---|
| 237 | #ifdef FORTIFY_PROVIDE_ARRAY_NEW
|
|---|
| 238 | void *operator new[](size_t size);
|
|---|
| 239 | void *operator new[](size_t size, const char *file, unsigned long line); // 16 Jan 08 SHL
|
|---|
| 240 | #endif
|
|---|
| 241 |
|
|---|
| 242 | /* Some compilers provide a different delete operator for deleting arrays.
|
|---|
| 243 | * This incldues GNU G++ (2.6.0)
|
|---|
| 244 | */
|
|---|
| 245 | #ifdef FORTIFY_PROVIDE_ARRAY_DELETE
|
|---|
| 246 | void operator delete[](void *pointer);
|
|---|
| 247 | #endif
|
|---|
| 248 |
|
|---|
| 249 | #endif /* __cplusplus */
|
|---|
| 250 |
|
|---|
| 251 | #ifndef __FORTIFY_C__ /* Only define the macros if we're NOT in fortify.c */
|
|---|
| 252 |
|
|---|
| 253 | /* Add file and line information to the fortify calls */
|
|---|
| 254 | #ifdef FORTIFY
|
|---|
| 255 | /* Core Fortify Functions */
|
|---|
| 256 | #define Fortify_CheckAllMemory() Fortify_CheckAllMemory(__FILE__, __LINE__)
|
|---|
| 257 | #define Fortify_ListAllMemory() Fortify_ListAllMemory (__FILE__, __LINE__)
|
|---|
| 258 | #define Fortify_DumpAllMemory() Fortify_DumpAllMemory (__FILE__, __LINE__)
|
|---|
| 259 | #define Fortify_CheckPointer(ptr) Fortify_CheckPointer(ptr, __FILE__, __LINE__)
|
|---|
| 260 | #define Fortify_LabelPointer(ptr,str) Fortify_LabelPointer(ptr, str, __FILE__, __LINE__)
|
|---|
| 261 | #define Fortify_EnterScope() Fortify_EnterScope(__FILE__, __LINE__)
|
|---|
| 262 | #define Fortify_LeaveScope() Fortify_LeaveScope(__FILE__, __LINE__)
|
|---|
| 263 | #define Fortify_OutputStatistics() Fortify_OutputStatistics(__FILE__, __LINE__)
|
|---|
| 264 | #define Fortify_GetCurrentAllocation() Fortify_GetCurrentAllocation(__FILE__, __LINE__)
|
|---|
| 265 | #define Fortify_SetAllocationLimit(x) Fortify_SetAllocationLimit(x, __FILE__, __LINE__)
|
|---|
| 266 | #define Fortify_Disable() Fortify_Disable(__FILE__, __LINE__)
|
|---|
| 267 |
|
|---|
| 268 | /* Fortify versions of the ANSI C memory allocation functions */
|
|---|
| 269 | #define malloc(size) Fortify_malloc(size, __FILE__, __LINE__)
|
|---|
| 270 | #define realloc(ptr,new_size) Fortify_realloc(ptr, new_size, __FILE__, __LINE__)
|
|---|
| 271 | #define calloc(num,size) Fortify_calloc(num, size, __FILE__, __LINE__)
|
|---|
| 272 | #define free(ptr) Fortify_free(ptr, __FILE__, __LINE__)
|
|---|
| 273 |
|
|---|
| 274 | /* Fortify versions of some non-ANSI C memory allocation functions */
|
|---|
| 275 | #ifdef FORTIFY_STRDUP
|
|---|
| 276 | #define strdup(ptr) Fortify_strdup(ptr, __FILE__, __LINE__)
|
|---|
| 277 | #endif
|
|---|
| 278 |
|
|---|
| 279 | /* Fortify versions of new and delete */
|
|---|
| 280 | #ifdef __cplusplus
|
|---|
| 281 | #define Fortify_New new(__FILE__, __LINE__)
|
|---|
| 282 | #define Fortify_Delete for(gbl_FortifyMagic = 1, \
|
|---|
| 283 | Fortify_PreDelete(__FILE__, __LINE__); \
|
|---|
| 284 | gbl_FortifyMagic; Fortify_PostDelete()) \
|
|---|
| 285 | gbl_FortifyMagic = 0, delete
|
|---|
| 286 | #define new Fortify_New
|
|---|
| 287 | #define delete Fortify_Delete
|
|---|
| 288 | #endif /* __cplusplus */
|
|---|
| 289 |
|
|---|
| 290 | #else /* Define the special fortify functions away to nothing */
|
|---|
| 291 |
|
|---|
| 292 | // 17 Jul 08 SHL fixme to avoid spurious OpenWatcom warnings
|
|---|
| 293 | #define Fortify_CheckAllMemory() 0
|
|---|
| 294 | #define Fortify_ListAllMemory() 0
|
|---|
| 295 | #define Fortify_DumpAllMemory() 0
|
|---|
| 296 | #define Fortify_CheckPointer(ptr) 1
|
|---|
| 297 | #define Fortify_LabelPointer(ptr,str)
|
|---|
| 298 | #define Fortify_SetOutputFunc() 0
|
|---|
| 299 | #define Fortify_SetMallocFailRate(p) 0
|
|---|
| 300 | #define Fortify_EnterScope() 0
|
|---|
| 301 | #define Fortify_LeaveScope() 0
|
|---|
| 302 | #define Fortify_OutputStatistics() 0
|
|---|
| 303 | #define Fortify_GetCurrentAllocation() 0
|
|---|
| 304 | #define Fortify_SetAllocationLimit(x) 0
|
|---|
| 305 | #define Fortify_Disable() 0
|
|---|
| 306 | #if 0 // 18 Jul 08 SHL fixme to be gone
|
|---|
| 307 | #define Fortify_PresetOwner() 0
|
|---|
| 308 | #endif
|
|---|
| 309 | #define Fortify_BecomeOwner 0
|
|---|
| 310 | #define Fortify_SetOwner 0
|
|---|
| 311 | #define Fortify_ChangeScope 0
|
|---|
| 312 | #define Fortify_SetScope 0
|
|---|
| 313 |
|
|---|
| 314 | #ifdef __cplusplus
|
|---|
| 315 | #define Fortify_New new
|
|---|
| 316 | #define Fortify_Delete delete
|
|---|
| 317 | #endif /* __cplusplus */
|
|---|
| 318 |
|
|---|
| 319 | #endif /* FORTIFY */
|
|---|
| 320 | #endif /* __FORTIFY_C__ */
|
|---|
| 321 | #endif /* __FORTIFY_H__ */
|
|---|