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