OLD | NEW |
1 /* crypto/mem.c */ | 1 /* crypto/mem.c */ |
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
3 * All rights reserved. | 3 * All rights reserved. |
4 * | 4 * |
5 * This package is an SSL implementation written | 5 * This package is an SSL implementation written |
6 * by Eric Young (eay@cryptsoft.com). | 6 * by Eric Young (eay@cryptsoft.com). |
7 * The implementation was written so as to conform with Netscapes SSL. | 7 * The implementation was written so as to conform with Netscapes SSL. |
8 * | 8 * |
9 * This library is free for commercial and non-commercial use as long as | 9 * This library is free for commercial and non-commercial use as long as |
10 * the following conditions are aheared to. The following conditions | 10 * the following conditions are aheared to. The following conditions |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
94 { return malloc_locked_func(num); } | 94 { return malloc_locked_func(num); } |
95 static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) | 95 static void *(*malloc_locked_ex_func)(size_t, const char *file, int line) |
96 = default_malloc_locked_ex; | 96 = default_malloc_locked_ex; |
97 | 97 |
98 static void (*free_locked_func)(void *) = free; | 98 static void (*free_locked_func)(void *) = free; |
99 | 99 |
100 | 100 |
101 | 101 |
102 /* may be changed as long as 'allow_customize_debug' is set */ | 102 /* may be changed as long as 'allow_customize_debug' is set */ |
103 /* XXX use correct function pointer types */ | 103 /* XXX use correct function pointer types */ |
104 #if defined(CRYPTO_MDEBUG) && !defined(OPENSSL_FIPS) | 104 #ifdef CRYPTO_MDEBUG |
105 /* use default functions from mem_dbg.c */ | 105 /* use default functions from mem_dbg.c */ |
106 static void (*malloc_debug_func)(void *,int,const char *,int,int) | 106 static void (*malloc_debug_func)(void *,int,const char *,int,int) |
107 = CRYPTO_dbg_malloc; | 107 = CRYPTO_dbg_malloc; |
108 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) | 108 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) |
109 = CRYPTO_dbg_realloc; | 109 = CRYPTO_dbg_realloc; |
110 static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; | 110 static void (*free_debug_func)(void *,int) = CRYPTO_dbg_free; |
111 static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; | 111 static void (*set_debug_options_func)(long) = CRYPTO_dbg_set_options; |
112 static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; | 112 static long (*get_debug_options_func)(void) = CRYPTO_dbg_get_options; |
113 | |
114 static int (*push_info_func)(const char *info, const char *file, int line) | |
115 = CRYPTO_dbg_push_info; | |
116 static int (*pop_info_func)(void) | |
117 = CRYPTO_dbg_pop_info; | |
118 static int (*remove_all_info_func)(void) | |
119 = CRYPTO_dbg_remove_all_info; | |
120 | |
121 #else | 113 #else |
122 /* applications can use CRYPTO_malloc_debug_init() to select above case | 114 /* applications can use CRYPTO_malloc_debug_init() to select above case |
123 * at run-time */ | 115 * at run-time */ |
124 static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; | 116 static void (*malloc_debug_func)(void *,int,const char *,int,int) = NULL; |
125 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) | 117 static void (*realloc_debug_func)(void *,void *,int,const char *,int,int) |
126 = NULL; | 118 = NULL; |
127 static void (*free_debug_func)(void *,int) = NULL; | 119 static void (*free_debug_func)(void *,int) = NULL; |
128 static void (*set_debug_options_func)(long) = NULL; | 120 static void (*set_debug_options_func)(long) = NULL; |
129 static long (*get_debug_options_func)(void) = NULL; | 121 static long (*get_debug_options_func)(void) = NULL; |
130 | |
131 | |
132 static int (*push_info_func)(const char *info, const char *file, int line) | |
133 = NULL; | |
134 static int (*pop_info_func)(void) = NULL; | |
135 static int (*remove_all_info_func)(void) = NULL; | |
136 | |
137 #endif | 122 #endif |
138 | 123 |
139 | 124 |
140 int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), | 125 int CRYPTO_set_mem_functions(void *(*m)(size_t), void *(*r)(void *, size_t), |
141 void (*f)(void *)) | 126 void (*f)(void *)) |
142 { | 127 { |
143 if (!allow_customize) | 128 if (!allow_customize) |
144 return 0; | 129 return 0; |
145 if ((m == 0) || (r == 0) || (f == 0)) | 130 if ((m == 0) || (r == 0) || (f == 0)) |
146 return 0; | 131 return 0; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 if (!allow_customize_debug) | 187 if (!allow_customize_debug) |
203 return 0; | 188 return 0; |
204 malloc_debug_func=m; | 189 malloc_debug_func=m; |
205 realloc_debug_func=r; | 190 realloc_debug_func=r; |
206 free_debug_func=f; | 191 free_debug_func=f; |
207 set_debug_options_func=so; | 192 set_debug_options_func=so; |
208 get_debug_options_func=go; | 193 get_debug_options_func=go; |
209 return 1; | 194 return 1; |
210 } | 195 } |
211 | 196 |
212 void CRYPTO_set_mem_info_functions( | |
213 int (*push_info_fn)(const char *info, const char *file, int line), | |
214 int (*pop_info_fn)(void), | |
215 int (*remove_all_info_fn)(void)) | |
216 { | |
217 push_info_func = push_info_fn; | |
218 pop_info_func = pop_info_fn; | |
219 remove_all_info_func = remove_all_info_fn; | |
220 } | |
221 | 197 |
222 void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), | 198 void CRYPTO_get_mem_functions(void *(**m)(size_t), void *(**r)(void *, size_t), |
223 void (**f)(void *)) | 199 void (**f)(void *)) |
224 { | 200 { |
225 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? | 201 if (m != NULL) *m = (malloc_ex_func == default_malloc_ex) ? |
226 malloc_func : 0; | 202 malloc_func : 0; |
227 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? | 203 if (r != NULL) *r = (realloc_ex_func == default_realloc_ex) ? |
228 realloc_func : 0; | 204 realloc_func : 0; |
229 if (f != NULL) *f=free_func; | 205 if (f != NULL) *f=free_func; |
230 } | 206 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 if (r != NULL) *r=realloc_debug_func; | 243 if (r != NULL) *r=realloc_debug_func; |
268 if (f != NULL) *f=free_debug_func; | 244 if (f != NULL) *f=free_debug_func; |
269 if (so != NULL) *so=set_debug_options_func; | 245 if (so != NULL) *so=set_debug_options_func; |
270 if (go != NULL) *go=get_debug_options_func; | 246 if (go != NULL) *go=get_debug_options_func; |
271 } | 247 } |
272 | 248 |
273 | 249 |
274 void *CRYPTO_malloc_locked(int num, const char *file, int line) | 250 void *CRYPTO_malloc_locked(int num, const char *file, int line) |
275 { | 251 { |
276 void *ret = NULL; | 252 void *ret = NULL; |
277 extern unsigned char cleanse_ctr; | |
278 | 253 |
279 if (num <= 0) return NULL; | 254 if (num <= 0) return NULL; |
280 | 255 |
281 allow_customize = 0; | 256 allow_customize = 0; |
282 if (malloc_debug_func != NULL) | 257 if (malloc_debug_func != NULL) |
283 { | 258 { |
284 allow_customize_debug = 0; | 259 allow_customize_debug = 0; |
285 malloc_debug_func(NULL, num, file, line, 0); | 260 malloc_debug_func(NULL, num, file, line, 0); |
286 } | 261 } |
287 ret = malloc_locked_ex_func(num,file,line); | 262 ret = malloc_locked_ex_func(num,file,line); |
288 #ifdef LEVITTE_DEBUG_MEM | 263 #ifdef LEVITTE_DEBUG_MEM |
289 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 264 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); |
290 #endif | 265 #endif |
291 if (malloc_debug_func != NULL) | 266 if (malloc_debug_func != NULL) |
292 malloc_debug_func(ret, num, file, line, 1); | 267 malloc_debug_func(ret, num, file, line, 1); |
293 | 268 |
| 269 #ifndef OPENSSL_CPUID_OBJ |
294 /* Create a dependency on the value of 'cleanse_ctr' so our memory | 270 /* Create a dependency on the value of 'cleanse_ctr' so our memory |
295 * sanitisation function can't be optimised out. NB: We only do | 271 * sanitisation function can't be optimised out. NB: We only do |
296 * this for >2Kb so the overhead doesn't bother us. */ | 272 * this for >2Kb so the overhead doesn't bother us. */ |
297 if(ret && (num > 2048)) | 273 if(ret && (num > 2048)) |
| 274 { extern unsigned char cleanse_ctr; |
298 ((unsigned char *)ret)[0] = cleanse_ctr; | 275 ((unsigned char *)ret)[0] = cleanse_ctr; |
| 276 } |
| 277 #endif |
299 | 278 |
300 return ret; | 279 return ret; |
301 } | 280 } |
302 | 281 |
303 void CRYPTO_free_locked(void *str) | 282 void CRYPTO_free_locked(void *str) |
304 { | 283 { |
305 if (free_debug_func != NULL) | 284 if (free_debug_func != NULL) |
306 free_debug_func(str, 0); | 285 free_debug_func(str, 0); |
307 #ifdef LEVITTE_DEBUG_MEM | 286 #ifdef LEVITTE_DEBUG_MEM |
308 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); | 287 fprintf(stderr, "LEVITTE_DEBUG_MEM: < 0x%p\n", str); |
309 #endif | 288 #endif |
310 free_locked_func(str); | 289 free_locked_func(str); |
311 if (free_debug_func != NULL) | 290 if (free_debug_func != NULL) |
312 free_debug_func(NULL, 1); | 291 free_debug_func(NULL, 1); |
313 } | 292 } |
314 | 293 |
315 void *CRYPTO_malloc(int num, const char *file, int line) | 294 void *CRYPTO_malloc(int num, const char *file, int line) |
316 { | 295 { |
317 void *ret = NULL; | 296 void *ret = NULL; |
318 extern unsigned char cleanse_ctr; | |
319 | 297 |
320 if (num <= 0) return NULL; | 298 if (num <= 0) return NULL; |
321 | 299 |
322 allow_customize = 0; | 300 allow_customize = 0; |
323 if (malloc_debug_func != NULL) | 301 if (malloc_debug_func != NULL) |
324 { | 302 { |
325 allow_customize_debug = 0; | 303 allow_customize_debug = 0; |
326 malloc_debug_func(NULL, num, file, line, 0); | 304 malloc_debug_func(NULL, num, file, line, 0); |
327 } | 305 } |
328 ret = malloc_ex_func(num,file,line); | 306 ret = malloc_ex_func(num,file,line); |
329 #ifdef LEVITTE_DEBUG_MEM | 307 #ifdef LEVITTE_DEBUG_MEM |
330 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); | 308 fprintf(stderr, "LEVITTE_DEBUG_MEM: > 0x%p (%d)\n", ret, num); |
331 #endif | 309 #endif |
332 if (malloc_debug_func != NULL) | 310 if (malloc_debug_func != NULL) |
333 malloc_debug_func(ret, num, file, line, 1); | 311 malloc_debug_func(ret, num, file, line, 1); |
334 | 312 |
| 313 #ifndef OPENSSL_CPUID_OBJ |
335 /* Create a dependency on the value of 'cleanse_ctr' so our memory | 314 /* Create a dependency on the value of 'cleanse_ctr' so our memory |
336 * sanitisation function can't be optimised out. NB: We only do | 315 * sanitisation function can't be optimised out. NB: We only do |
337 * this for >2Kb so the overhead doesn't bother us. */ | 316 * this for >2Kb so the overhead doesn't bother us. */ |
338 if(ret && (num > 2048)) | 317 if(ret && (num > 2048)) |
| 318 { extern unsigned char cleanse_ctr; |
339 ((unsigned char *)ret)[0] = cleanse_ctr; | 319 ((unsigned char *)ret)[0] = cleanse_ctr; |
| 320 } |
| 321 #endif |
340 | 322 |
341 return ret; | 323 return ret; |
342 } | 324 } |
| 325 char *CRYPTO_strdup(const char *str, const char *file, int line) |
| 326 { |
| 327 char *ret = CRYPTO_malloc(strlen(str)+1, file, line); |
| 328 |
| 329 strcpy(ret, str); |
| 330 return ret; |
| 331 } |
343 | 332 |
344 void *CRYPTO_realloc(void *str, int num, const char *file, int line) | 333 void *CRYPTO_realloc(void *str, int num, const char *file, int line) |
345 { | 334 { |
346 void *ret = NULL; | 335 void *ret = NULL; |
347 | 336 |
348 if (str == NULL) | 337 if (str == NULL) |
349 return CRYPTO_malloc(num, file, line); | 338 return CRYPTO_malloc(num, file, line); |
350 | 339 |
351 if (num <= 0) return NULL; | 340 if (num <= 0) return NULL; |
352 | 341 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
416 if (set_debug_options_func != NULL) | 405 if (set_debug_options_func != NULL) |
417 set_debug_options_func(bits); | 406 set_debug_options_func(bits); |
418 } | 407 } |
419 | 408 |
420 long CRYPTO_get_mem_debug_options(void) | 409 long CRYPTO_get_mem_debug_options(void) |
421 { | 410 { |
422 if (get_debug_options_func != NULL) | 411 if (get_debug_options_func != NULL) |
423 return get_debug_options_func(); | 412 return get_debug_options_func(); |
424 return 0; | 413 return 0; |
425 } | 414 } |
426 | |
427 int CRYPTO_push_info_(const char *info, const char *file, int line) | |
428 { | |
429 if (push_info_func) | |
430 return push_info_func(info, file, line); | |
431 return 1; | |
432 } | |
433 | |
434 int CRYPTO_pop_info(void) | |
435 { | |
436 if (pop_info_func) | |
437 return pop_info_func(); | |
438 return 1; | |
439 } | |
440 | |
441 int CRYPTO_remove_all_info(void) | |
442 { | |
443 if (remove_all_info_func) | |
444 return remove_all_info_func(); | |
445 return 1; | |
446 } | |
OLD | NEW |