Debian Codegraph @

src/str.c

raw
1 /*
2 * stunnel TLS offloading and load-balancing proxy
3 * Copyright (C) 1998-2026 Michal Trojnara <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, see <http://www.gnu.org/licenses>.
17 *
18 * Linking stunnel statically or dynamically with other modules is making
19 * a combined work based on stunnel. Thus, the terms and conditions of
20 * the GNU General Public License cover the whole combination.
21 *
22 * In addition, as a special exception, the copyright holder of stunnel
23 * gives you permission to combine stunnel with free software programs or
24 * libraries that are released under the GNU LGPL and with code included
25 * in the standard release of OpenSSL under the OpenSSL License (or
26 * modified versions of such code, with unchanged license). You may copy
27 * and distribute such a system following the terms of the GNU GPL for
28 * stunnel and the licenses of the other code concerned.
29 *
30 * Note that people who make modified versions of stunnel are not obligated
31 * to grant this special exception for their modified versions; it is their
32 * choice whether to do so. The GNU General Public License gives permission
33 * to release a modified version without this exception; this exception
34 * also makes it possible to release a modified version which carries
35 * forward this exception.
36 */
37
38 #include "prototypes.h"
39
40 /* Uncomment to see allocation sources in core dumps */
41 /* #define DEBUG_PADDING 64 */
42
43 #ifdef HAVE_MIMALLOC_H
44 #include <mimalloc.h>
45 #define system_malloc(n) mi_malloc(n)
46 #define system_realloc(p,n) mi_realloc((p),(n))
47 #define system_free(p) mi_free(p)
48 #elif defined(USE_WIN32)
49 /* reportedly, malloc does not always return 16-byte aligned addresses
50 * for 64-bit targets as specified by
51 * https://msdn.microsoft.com/en-us/library/6ewkz86d.aspx */
52 #define system_malloc(n) _aligned_malloc((n),16)
53 #define system_realloc(p,n) _aligned_realloc((p),(n),16)
54 #define system_free(p) _aligned_free(p)
55 #else
56 #define system_malloc(n) malloc(n)
57 #define system_realloc(p,n) realloc((p),(n))
58 #define system_free(p) free(p)
59 #endif
60
61 #define CANARY_INITIALIZED 0x0000c0ded0000000LL
62 #define CANARY_UNINTIALIZED 0x0000abadbabe0000LL
63 #define MAGIC_ALLOCATED 0x0000a110c8ed0000LL
64 #define MAGIC_DEALLOCATED 0x0000defec8ed0000LL
65
66 /* most platforms require allocations to be aligned */
67 #ifdef _MSC_VER
68 #pragma warning(disable: 4324)
69 __declspec(align(16))
70 #endif
71 struct alloc_list_struct {
72 ALLOC_LIST *prev, *next;
73 TLS_DATA *tls;
74 size_t size;
75 const char *alloc_file, *free_file;
76 int alloc_line, free_line;
77 #ifdef DEBUG_PADDING
78 char debug[DEBUG_PADDING];
79 #endif
80 uint64_t valid_canary, magic;
81 #ifdef __GNUC__
82 } __attribute__((aligned(16)));
83 #else
84 #ifndef MSC_VER
85 uint64_t :0; /* align the structure */
86 #endif
87 };
88 #endif
89
90 #define LEAK_TABLE_SIZE 997
91 typedef struct {
92 int num, max; /* current and highest number of allocations */
93 int64_t total; /* approximate total number of heap operations */
94 const char *alloc_file;
95 int alloc_line;
96 } LEAK_ENTRY;
97 NOEXPORT LEAK_ENTRY leak_hash_table[LEAK_TABLE_SIZE],
98 *leak_results[LEAK_TABLE_SIZE];
99 NOEXPORT int leak_result_num=0;
100
101 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
102 DEFINE_STACK_OF(LEAK_ENTRY)
103 #endif /* OpenSSL version >= 1.1.1 */
104
105 #ifdef USE_WIN32
106 NOEXPORT LPTSTR str_vtprintf(LPCTSTR, va_list);
107 #endif /* USE_WIN32 */
108
109 NOEXPORT void *str_realloc_internal_debug(void *, size_t, const char *, int);
110
111 NOEXPORT ALLOC_LIST *get_alloc_list_ptr(void *, const char *, int);
112 NOEXPORT void str_leak_debug(const ALLOC_LIST *, int);
113
114 NOEXPORT LEAK_ENTRY *leak_search(const ALLOC_LIST *);
115 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
116 NOEXPORT int leak_cmp(const LEAK_ENTRY *const *, const LEAK_ENTRY *const *);
117 #endif /* OpenSSL version >= 1.1.1 */
118 NOEXPORT void leak_report(void);
119 NOEXPORT long leak_threshold(void);
120
121 #if OPENSSL_VERSION_NUMBER<0x10100000L
122 NOEXPORT void free_function(void *);
123 #endif
124
125 TLS_DATA *ui_tls;
126 NOEXPORT uint8_t canary[10]; /* 80-bit canary value */
127 NOEXPORT volatile uint64_t canary_initialized=CANARY_UNINTIALIZED;
128
129 /**************************************** string manipulation functions */
130
131 char *str_dup_debug(const char *str, const char *file, int line) {
132 char *retval;
133
134 if(!str)
135 return NULL;
136 retval=str_alloc_debug(strlen(str)+1, file, line);
137 strcpy(retval, str);
138 return retval;
139 }
140
141 char *str_dup_detached_debug(const char *str, const char *file, int line) {
142 char *retval;
143
144 if(!str)
145 return NULL;
146 retval=str_alloc_detached_debug(strlen(str)+1, file, line);
147 strcpy(retval, str);
148 return retval;
149 }
150
151 char *str_printf(const char *format, ...) {
152 char *txt;
153 va_list arglist;
154
155 va_start(arglist, format);
156 txt=str_vprintf(format, arglist);
157 va_end(arglist);
158 return txt;
159 }
160
161 #ifdef __GNUC__
162 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
163 #pragma GCC diagnostic push
164 #endif /* __GNUC__>=4.6 */
165 #pragma GCC diagnostic ignored "-Wformat-nonliteral"
166 #endif /* __GNUC__ */
167 char *str_vprintf(const char *format, va_list ap) {
168 size_t size=96;
169 char *p;
170
171 p=str_alloc(size);
172 for(;;) {
173 int n;
174 va_list aq;
175
176 /* cppcheck-suppress-begin va_list_usedBeforeStarted */
177 va_copy(aq, ap);
178 n=vsnprintf(p, size, format, aq);
179 va_end(aq);
180 /* cppcheck-suppress-end va_list_usedBeforeStarted */
181 if(n>-1 && n<(int)size)
182 return p;
183 if(n>-1) /* glibc 2.1 */
184 size=(size_t)n+1; /* precisely what is needed */
185 else /* glibc 2.0, WIN32, etc. */
186 size*=2; /* twice the old size */
187 p=str_realloc(p, size);
188 }
189 }
190 #ifdef __GNUC__
191 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
192 #pragma GCC diagnostic pop
193 #endif /* __GNUC__>=4.6 */
194 #endif /* __GNUC__ */
195
196 #ifdef USE_WIN32
197
198 LPTSTR str_tprintf(LPCTSTR format, ...) {
199 LPTSTR txt;
200 va_list arglist;
201
202 va_start(arglist, format);
203 txt=str_vtprintf(format, arglist);
204 va_end(arglist);
205 return txt;
206 }
207
208 NOEXPORT LPTSTR str_vtprintf(LPCTSTR format, va_list ap) {
209 size_t size=32;
210 LPTSTR p;
211
212 p=str_alloc(size*sizeof(TCHAR));
213 for(;;) {
214 int n;
215 va_list aq;
216
217 /* cppcheck-suppress-begin va_list_usedBeforeStarted */
218 va_copy(aq, ap);
219 n=_vsntprintf(p, size, format, aq);
220 va_end(aq);
221 /* cppcheck-suppress-end va_list_usedBeforeStarted */
222 if(n>-1 && n<(int)size)
223 return p;
224 size*=2;
225 p=str_realloc(p, size*sizeof(TCHAR));
226 }
227 }
228
229 #endif
230
231 /**************************************** memory allocation wrappers */
232
233 void str_init(void) {
234 memset(leak_hash_table, 0, sizeof leak_hash_table);
235 #if OPENSSL_VERSION_NUMBER>=0x10100000L
236 CRYPTO_set_mem_functions(str_alloc_detached_debug,
237 str_realloc_detached_debug, str_free_debug);
238 #else
239 CRYPTO_set_mem_ex_functions(str_alloc_detached_debug,
240 str_realloc_detached_debug, free_function);
241 #endif
242 }
243
244 void str_thread_init(TLS_DATA *tls_data) {
245 tls_data->alloc_head=NULL;
246 tls_data->alloc_bytes=tls_data->alloc_blocks=0;
247 }
248
249 void str_thread_cleanup(TLS_DATA *tls_data) {
250 /* free all attached allocations */
251 while(tls_data->alloc_head) /* str_free macro requires an lvalue */
252 str_free_expression(tls_data->alloc_head+1);
253 }
254
255 void str_canary_init(void) {
256 if(canary_initialized!=CANARY_UNINTIALIZED)
257 return; /* prevent double initialization on config reload */
258 RAND_bytes(canary, (int)sizeof canary);
259 /* an error would reduce the effectiveness of canaries */
260 /* this is nothing critical, so the return value is ignored here */
261 canary_initialized=CANARY_INITIALIZED; /* after RAND_bytes */
262 }
263
264 void str_stats(void) {
265 TLS_DATA *tls_data;
266 ALLOC_LIST *alloc_list;
267 int i=0;
268
269 if(!tls_initialized)
270 fatal("str not initialized");
271 leak_report();
272 tls_data=tls_get();
273 if(!tls_data || (!tls_data->alloc_blocks && !tls_data->alloc_bytes))
274 return; /* skip if no data is allocated */
275 s_log(LOG_DEBUG, "str_stats: %lu block(s), "
276 "%lu data byte(s), %lu control byte(s)",
277 (unsigned long)tls_data->alloc_blocks,
278 (unsigned long)tls_data->alloc_bytes,
279 (unsigned long)(tls_data->alloc_blocks*
280 (sizeof(ALLOC_LIST)+sizeof canary)));
281 for(alloc_list=tls_data->alloc_head; alloc_list; alloc_list=alloc_list->next) {
282 if(++i>10) /* limit the number of results */
283 break;
284 s_log(LOG_DEBUG, "str_stats: %lu byte(s) at %s:%d",
285 (unsigned long)alloc_list->size,
286 alloc_list->alloc_file, alloc_list->alloc_line);
287 }
288 }
289
290 void *str_alloc_debug(size_t size, const char *file, int line) {
291 TLS_DATA *tls_data;
292 ALLOC_LIST *alloc_list;
293
294 if(!tls_initialized)
295 fatal_debug("str not initialized", file, line);
296 tls_data=tls_get();
297 if(!tls_data) {
298 tls_data=tls_alloc(NULL, NULL, "alloc");
299 s_log(LOG_CRIT, "INTERNAL ERROR: Uninitialized TLS at %s, line %d",
300 file, line);
301 }
302
303 alloc_list=(ALLOC_LIST *)str_alloc_detached_debug(size, file, line)-1;
304 alloc_list->prev=NULL;
305 alloc_list->next=tls_data->alloc_head;
306 alloc_list->tls=tls_data;
307 if(tls_data->alloc_head)
308 tls_data->alloc_head->prev=alloc_list;
309 tls_data->alloc_head=alloc_list;
310 tls_data->alloc_bytes+=size;
311 tls_data->alloc_blocks++;
312
313 return alloc_list+1;
314 }
315
316 void *str_alloc_detached_debug(size_t size, const char *file, int line) {
317 ALLOC_LIST *alloc_list;
318
319 #if 0
320 printf("allocating %lu bytes at %s:%d\n", (unsigned long)size, file, line);
321 #endif
322 alloc_list=system_malloc(sizeof(ALLOC_LIST)+size+sizeof canary);
323 if(!alloc_list)
324 fatal_debug("Out of memory", file, line);
325 memset(alloc_list, 0, sizeof(ALLOC_LIST)+size+sizeof canary);
326 alloc_list->prev=NULL; /* for debugging */
327 alloc_list->next=NULL; /* for debugging */
328 alloc_list->tls=NULL;
329 alloc_list->size=size;
330 alloc_list->alloc_file=file;
331 alloc_list->alloc_line=line;
332 alloc_list->free_file="none";
333 alloc_list->free_line=0;
334 #ifdef DEBUG_PADDING
335 snprintf(alloc_list->debug+1, DEBUG_PADDING-1, "ALLOC_%lu@%s:%d",
336 (unsigned long)size, file, line);
337 #endif
338 alloc_list->valid_canary=canary_initialized; /* before memcpy */
339 memcpy((uint8_t *)(alloc_list+1)+size, canary, sizeof canary);
340 alloc_list->magic=MAGIC_ALLOCATED;
341 str_leak_debug(alloc_list, 1);
342
343 return alloc_list+1;
344 }
345
346 void *str_realloc_debug(void *ptr, size_t size, const char *file, int line) {
347 if(ptr)
348 return str_realloc_internal_debug(ptr, size, file, line);
349 else
350 return str_alloc_debug(size, file, line);
351 }
352
353 void *str_realloc_detached_debug(void *ptr, size_t size, const char *file, int line) {
354 if(ptr)
355 return str_realloc_internal_debug(ptr, size, file, line);
356 else
357 return str_alloc_detached_debug(size, file, line);
358 }
359
360 NOEXPORT void *str_realloc_internal_debug(void *ptr, size_t size, const char *file, int line) {
361 ALLOC_LIST *prev_alloc_list, *alloc_list;
362
363 prev_alloc_list=get_alloc_list_ptr(ptr, file, line);
364 str_leak_debug(prev_alloc_list, -1);
365 if(prev_alloc_list->size>size) /* shrinking the allocation */
366 memset((uint8_t *)ptr+size, 0, prev_alloc_list->size-size); /* paranoia */
367 alloc_list=system_realloc(prev_alloc_list, sizeof(ALLOC_LIST)+size+sizeof canary);
368 if(!alloc_list)
369 fatal_debug("Out of memory", file, line);
370 ptr=alloc_list+1;
371 if(size>alloc_list->size) /* growing the allocation */
372 memset((uint8_t *)ptr+alloc_list->size, 0, size-alloc_list->size);
373 if(alloc_list->tls) { /* not detached */
374 /* refresh possibly invalidated linked list pointers */
375 if(alloc_list->tls->alloc_head==prev_alloc_list)
376 alloc_list->tls->alloc_head=alloc_list;
377 if(alloc_list->next)
378 alloc_list->next->prev=alloc_list;
379 if(alloc_list->prev)
380 alloc_list->prev->next=alloc_list;
381 /* update statistics while the old size is still available */
382 alloc_list->tls->alloc_bytes+=size-alloc_list->size;
383 }
384 alloc_list->size=size;
385 alloc_list->alloc_file=file;
386 alloc_list->alloc_line=line;
387 alloc_list->free_file="none";
388 alloc_list->free_line=0;
389 #ifdef DEBUG_PADDING
390 snprintf(alloc_list->debug+1, DEBUG_PADDING-1, "ALLOC_%lu@%s:%d",
391 (unsigned long)size, file, line);
392 #endif
393 alloc_list->valid_canary=canary_initialized; /* before memcpy */
394 memcpy((uint8_t *)ptr+size, canary, sizeof canary);
395 str_leak_debug(alloc_list, 1);
396 return ptr;
397 }
398
399 /* detach from thread automatic deallocation list */
400 /* it has no effect if the allocation is already detached */
401 void str_detach_debug(void *ptr, const char *file, int line) {
402 ALLOC_LIST *alloc_list;
403
404 if(!ptr) /* do not attempt to free null pointers */
405 return;
406 alloc_list=get_alloc_list_ptr(ptr, file, line);
407 if(alloc_list->tls) { /* not detached */
408 /* remove from linked list */
409 if(alloc_list->tls->alloc_head==alloc_list)
410 alloc_list->tls->alloc_head=alloc_list->next;
411 if(alloc_list->next)
412 alloc_list->next->prev=alloc_list->prev;
413 if(alloc_list->prev)
414 alloc_list->prev->next=alloc_list->next;
415 /* update statistics */
416 alloc_list->tls->alloc_bytes-=alloc_list->size;
417 alloc_list->tls->alloc_blocks--;
418 /* clear pointers */
419 alloc_list->next=NULL;
420 alloc_list->prev=NULL;
421 alloc_list->tls=NULL;
422 }
423 }
424
425 /* Here be dragons:
426 * this is an ugly hack to work around OpenSSL API that requires const
427 * function parameters -> use it with extreme caution */
428 void str_detach_const_debug(const void *ptr, const char *file, int line) {
429 union {
430 const void *c;
431 void *v;
432 } u;
433
434 u.c=ptr;
435 str_detach_debug(u.v, file, line);
436 }
437
438 void str_free_debug(void *ptr, const char *file, int line) {
439 ALLOC_LIST *alloc_list;
440
441 if(!ptr) /* do not attempt to free null pointers */
442 return;
443 alloc_list=(ALLOC_LIST *)ptr-1;
444 if(alloc_list->magic==MAGIC_DEALLOCATED) { /* double free */
445 /* this may (unlikely) log garbage instead of file names */
446 s_log(LOG_CRIT, "INTERNAL ERROR: Double free attempt: "
447 "ptr=%p alloc=%s:%d free#1=%s:%d free#2=%s:%d",
448 ptr,
449 alloc_list->alloc_file, alloc_list->alloc_line,
450 alloc_list->free_file, alloc_list->free_line,
451 file, line);
452 return;
453 }
454 str_detach_debug(ptr, file, line);
455 str_leak_debug(alloc_list, -1);
456 alloc_list->free_file=file;
457 alloc_list->free_line=line;
458 alloc_list->magic=MAGIC_DEALLOCATED; /* detect double free attempts */
459 memset(ptr, 0, alloc_list->size+sizeof canary); /* paranoia */
460 system_free(alloc_list);
461 }
462
463 /* Here be dragons:
464 * this is an ugly hack to work around OpenSSL API that requires const
465 * function parameters -> use it with extreme caution */
466 void str_free_const_debug(const void *ptr, const char *file, int line) {
467 union {
468 const void *c;
469 void *v;
470 } u;
471
472 u.c=ptr;
473 str_free_debug(u.v, file, line);
474 }
475
476 NOEXPORT ALLOC_LIST *get_alloc_list_ptr(void *ptr, const char *file, int line) {
477 ALLOC_LIST *alloc_list;
478
479 if(!tls_initialized)
480 fatal_debug("str not initialized", file, line);
481 alloc_list=(ALLOC_LIST *)ptr-1;
482 if(alloc_list->magic!=MAGIC_ALLOCATED) /* not allocated by str_alloc() */
483 fatal_debug("Bad magic", file, line); /* LOL */
484 if(alloc_list->tls /* not detached */ && alloc_list->tls!=tls_get())
485 fatal_debug("Memory allocated in a different thread", file, line);
486 if(alloc_list->valid_canary!=CANARY_UNINTIALIZED &&
487 safe_memcmp((uint8_t *)ptr+alloc_list->size, canary, sizeof canary))
488 fatal_debug("Dead canary", file, line); /* LOL */
489 return alloc_list;
490 }
491
492 /**************************************** memory leak detection */
493
494 NOEXPORT void str_leak_debug(const ALLOC_LIST *alloc_list, int change) {
495 static size_t entries=0;
496 LEAK_ENTRY *entry;
497 int new_entry;
498 int allocations;
499
500 if(service_options.log_level<LOG_DEBUG) /* performance optimization */
501 return;
502 #ifdef USE_OS_THREADS
503 if(!stunnel_locks[STUNNEL_LOCKS-1]) /* threads not initialized */
504 return;
505 #endif /* USE_OS_THREADS */
506 if(!number_of_sections) /* configuration file not initialized */
507 return;
508
509 entry=leak_search(alloc_list);
510 /* the race condition may lead to false positives, which is handled later */
511 new_entry=entry->alloc_line!=alloc_list->alloc_line ||
512 entry->alloc_file!=alloc_list->alloc_file;
513
514 if(new_entry) { /* the file:line pair was encountered for the first time */
515 CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_LEAK_HASH]);
516 entry=leak_search(alloc_list); /* the list may have changed */
517 if(entry->alloc_line==0) {
518 if(entries>LEAK_TABLE_SIZE-100) { /* this should never happen */
519 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LEAK_HASH]);
520 return;
521 }
522 entries++;
523 entry->alloc_line=alloc_list->alloc_line;
524 entry->alloc_file=alloc_list->alloc_file;
525 }
526 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LEAK_HASH]);
527 }
528
529 /* for performance reasons, we ignore the race condition, as an approximate
530 * number of allocations is good enough to identify the most used entries */
531 entry->total++;
532
533 /* for performance reasons, we try to avoid calling CRYPTO_atomic_add() */
534 #ifdef USE_OS_THREADS
535 #ifdef _MSC_VER
536 /* casting is safe, because sizeof(long)==sizeof(int) on Windows */
537 allocations=InterlockedExchangeAdd((long *)&entry->num, change)+change;
538 #else /* defined(_MSC_VER) */
539 #ifdef __ATOMIC_ACQ_REL
540 if(__atomic_is_lock_free(sizeof entry->num, &entry->num))
541 allocations=__atomic_add_fetch(&entry->num, change, __ATOMIC_ACQ_REL);
542 else /* atomic add not directly supported by the compiler */
543 #endif /* defined(__ATOMIC_ACQ_REL) */
544 CRYPTO_atomic_add(&entry->num, change, &allocations,
545 stunnel_locks[LOCK_LEAK_HASH]);
546 #endif /* defined(_MSC_VER) */
547 #else /* USE_OS_THREADS */
548 allocations=(entry->num+=change);
549 #endif /* USE_OS_THREADS */
550
551 if(allocations<=leak_threshold()) /* leak not detected */
552 return;
553 if(allocations<=entry->max) /* not the biggest leak for this entry */
554 return;
555 if(entry->max) { /* not the first time we found a leak for this entry */
556 entry->max=allocations; /* just update the value */
557 return;
558 }
559 /* we *may* need to allocate a new leak_results entry */
560 /* locking is slow, so we try to avoid it if possible */
561 CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_LEAK_RESULTS]);
562 if(entry->max==0) /* the table may have changed */
563 leak_results[leak_result_num++]=entry;
564 entry->max=allocations;
565 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LEAK_RESULTS]);
566 }
567
568 /* O(1) hash table lookup */
569 NOEXPORT LEAK_ENTRY *leak_search(const ALLOC_LIST *alloc_list) {
570 /* a trivial hash based on source file name *address* and line number */
571 unsigned i=(1777*(unsigned)(uintptr_t)alloc_list->alloc_file+
572 (unsigned)alloc_list->alloc_line)%LEAK_TABLE_SIZE;
573
574 while(!(leak_hash_table[i].alloc_line==0 ||
575 (leak_hash_table[i].alloc_line==alloc_list->alloc_line &&
576 leak_hash_table[i].alloc_file==alloc_list->alloc_file)))
577 i=(i+1)%LEAK_TABLE_SIZE;
578 return leak_hash_table+i;
579 }
580
581 void leak_table_utilization(void) {
582 int i, utilization=0;
583 int64_t grand_total=0;
584 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
585 STACK_OF(LEAK_ENTRY) *stats;
586 #endif /* OpenSSL version >= 1.1.1 */
587
588 /* leak_hash_table[] is only filled at the DEBUG logging level */
589 if(service_options.log_level<LOG_DEBUG)
590 return;
591
592 /* log total hash table utilization */
593 for(i=0; i<LEAK_TABLE_SIZE; ++i)
594 if(leak_hash_table[i].total) {
595 ++utilization;
596 grand_total+=leak_hash_table[i].total;
597 }
598 s_log(LOG_DEBUG, "Leak detection table utilization: %d/%d (%05.2f%%)",
599 utilization, LEAK_TABLE_SIZE, 100.0*utilization/LEAK_TABLE_SIZE);
600
601 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
602 /* log up to 5 most frequently used heap allocations */
603 stats=sk_LEAK_ENTRY_new_reserve(leak_cmp, utilization);
604 for(i=0; i<LEAK_TABLE_SIZE; ++i)
605 if(leak_hash_table[i].total)
606 sk_LEAK_ENTRY_push(stats, leak_hash_table + i);
607 sk_LEAK_ENTRY_sort(stats);
608 for(i=0; i<5 && sk_LEAK_ENTRY_num(stats); ++i) {
609 LEAK_ENTRY *entry=sk_LEAK_ENTRY_pop(stats);
610 s_log(LOG_DEBUG, "#%d: %05.2f%% of heap operations: %s:%d",
611 i+1, 100.0*(double)entry->total/(double)grand_total,
612 entry->alloc_file, entry->alloc_line);
613 }
614 sk_LEAK_ENTRY_free(stats);
615 #endif /* OpenSSL version >= 1.1.1 */
616 }
617
618 #if OPENSSL_VERSION_NUMBER >= 0x10101000L
619 NOEXPORT int leak_cmp(const LEAK_ENTRY *const *a, const LEAK_ENTRY *const *b) {
620 int64_t d = (*a)->total - (*b)->total;
621 if(d>0)
622 return 1;
623 if(d<0)
624 return -1;
625 return 0;
626 }
627 #endif /* OpenSSL version >= 1.1.1 */
628
629 /* report identified leaks */
630 NOEXPORT void leak_report(void) {
631 int i;
632 long limit;
633
634 limit=leak_threshold();
635
636 CRYPTO_THREAD_read_lock(stunnel_locks[LOCK_LEAK_RESULTS]);
637 for(i=0; i<leak_result_num; ++i)
638 if(leak_results[i] /* an officious compiler could reorder code */ &&
639 leak_results[i]->max>limit /* the limit could have changed */)
640 s_log(LOG_WARNING, "Possible memory leak at %s:%d: %d allocations",
641 leak_results[i]->alloc_file, leak_results[i]->alloc_line,
642 leak_results[i]->max);
643 CRYPTO_THREAD_unlock(stunnel_locks[LOCK_LEAK_RESULTS]);
644 }
645
646 NOEXPORT long leak_threshold(void) {
647 long limit;
648
649 limit=10000*((int)number_of_sections+1);
650 #ifndef USE_FORK
651 limit+=100*num_clients;
652 #endif
653 return limit;
654 }
655
656 /**************************************** memcmp() replacement */
657
658 /* a version of memcmp() with execution time not dependent on data values */
659 /* it does *not* allow testing whether s1 is greater or lesser than s2 */
660 int safe_memcmp(const void *s1, const void *s2, size_t n) {
661 #ifdef _WIN64
662 typedef unsigned long long TL;
663 #else
664 typedef unsigned long TL;
665 #endif
666 typedef unsigned char TS;
667 TL r=0;
668 const TL *pl1, *pl2;
669 const TS *ps1, *ps2;
670 int n1=(int)((uintptr_t)s1&(sizeof(TL)-1)); /* unaligned bytes in s1 */
671 int n2=(int)((uintptr_t)s2&(sizeof(TL)-1)); /* unaligned bytes in s2 */
672
673 if(n1 || n2) { /* either pointer unaligned */
674 ps1=(const TS *)s1;
675 ps2=(const TS *)s2;
676 } else { /* both pointers aligned -> compare full words */
677 pl1=(const TL *)s1;
678 pl2=(const TL *)s2;
679 while(n>=sizeof(TL)) {
680 n-=sizeof(TL);
681 r|=(*pl1++)^(*pl2++);
682 }
683 ps1=(const TS *)pl1;
684 ps2=(const TS *)pl2;
685 }
686 while(n--)
687 r|=(*ps1++)^(*ps2++);
688 return r!=0;
689 }
690
691 /**************************************** OpenSSL allocator hook */
692
693 #if OPENSSL_VERSION_NUMBER<0x10100000L
694 NOEXPORT void free_function(void *ptr) {
695 /* CRYPTO_set_mem_ex_functions() needs a function rather than a macro */
696 /* unfortunately, OpenSSL provides no file:line information here */
697 str_free_debug(ptr, "OpenSSL", 0);
698 }
699 #endif
700
701 /* end of str.c */