comparison mupdf-source/thirdparty/jbig2dec/memento.c @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 /* Copyright (C) 2001-2023 Artifex Software, Inc.
2 All Rights Reserved.
3
4 This software is provided AS-IS with no warranty, either express or
5 implied.
6
7 This software is distributed under license and may not be copied,
8 modified or distributed except as expressly authorized under the terms
9 of the license contained in the file LICENSE in this distribution.
10
11 Refer to licensing information at http://www.artifex.com or contact
12 Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
13 CA 94129, USA, for further information.
14 */
15
16 /* Inspired by Fortify by Simon P Bullen. */
17
18 /* Set the following if you're only looking for leaks, not memory overwrites
19 * to speed the operation */
20 /* #define MEMENTO_LEAKONLY */
21
22 /* Set the following to keep extra details about the history of blocks */
23 #define MEMENTO_DETAILS
24
25 /* Don't keep blocks around if they'd mean losing more than a quarter of
26 * the freelist. */
27 #define MEMENTO_FREELIST_MAX_SINGLE_BLOCK (MEMENTO_FREELIST_MAX/4)
28
29 #define COMPILING_MEMENTO_C
30
31 /* SHUT UP, MSVC. I KNOW WHAT I AM DOING. */
32 #define _CRT_SECURE_NO_WARNINGS
33
34 /* We have some GS specific tweaks; more for the GS build environment than
35 * anything else. */
36 /* #define MEMENTO_GS_HACKS */
37
38 #ifdef MEMENTO_GS_HACKS
39 /* For GS we include malloc_.h. Anyone else would just include memento.h */
40 #include "malloc_.h"
41 #include "memory_.h"
42 int atexit(void (*)(void));
43 #else
44 #include "memento.h"
45 #include <stdio.h>
46 #endif
47 #ifndef _MSC_VER
48 #include <stdint.h>
49 #include <limits.h>
50 #include <unistd.h>
51 #endif
52
53 #include <stdlib.h>
54 #include <stdarg.h>
55 #include <string.h>
56
57 #ifdef __ANDROID__
58 #define MEMENTO_ANDROID
59 #include <stdio.h>
60 #endif
61
62 /* Hacks to portably print large sizes */
63 #ifdef _MSC_VER
64 #define FMTZ "%llu"
65 #define FMTZ_CAST _int64
66 #define FMTP "0x%p"
67 #else
68 #define FMTZ "%zu"
69 #define FMTZ_CAST size_t
70 #define FMTP "%p"
71 #endif
72
73 #define UB(x) ((intptr_t)((x) & 0xFF))
74 #define B2I(x) (UB(x) | (UB(x)<<8) | (UB(x)<<16) | (UB(x)<<24))
75 #define B2P(x) ((void *)(B2I(x) | ((B2I(x)<<16)<<16)))
76 #define MEMENTO_PREFILL_UBYTE ((unsigned char)(MEMENTO_PREFILL))
77 #define MEMENTO_PREFILL_USHORT (((unsigned short)MEMENTO_PREFILL_UBYTE) | (((unsigned short)MEMENTO_PREFILL_UBYTE)<<8))
78 #define MEMENTO_PREFILL_UINT (((unsigned int)MEMENTO_PREFILL_USHORT) | (((unsigned int)MEMENTO_PREFILL_USHORT)<<16))
79 #define MEMENTO_PREFILL_PTR (void *)(((uintptr_t)MEMENTO_PREFILL_UINT) | ((((uintptr_t)MEMENTO_PREFILL_UINT)<<16)<<16))
80 #define MEMENTO_POSTFILL_UBYTE ((unsigned char)(MEMENTO_POSTFILL))
81 #define MEMENTO_POSTFILL_USHORT (((unsigned short)MEMENTO_POSTFILL_UBYTE) | (((unsigned short)MEMENTO_POSTFILL_UBYTE)<<8))
82 #define MEMENTO_POSTFILL_UINT (((unsigned int)MEMENTO_POSTFILL_USHORT) | (((unsigned int)MEMENTO_POSTFILL_USHORT)<<16))
83 #define MEMENTO_POSTFILL_PTR (void *)(((uintptr_t)MEMENTO_POSTFILL_UINT) | ((((uintptr_t)MEMENTO_POSTFILL_UINT)<<16)<<16))
84 #define MEMENTO_ALLOCFILL_UBYTE ((unsigned char)(MEMENTO_ALLOCFILL))
85 #define MEMENTO_ALLOCFILL_USHORT (((unsigned short)MEMENTO_ALLOCFILL_UBYTE) | (((unsigned short)MEMENTO_ALLOCFILL_UBYTE)<<8))
86 #define MEMENTO_ALLOCFILL_UINT (((unsigned int)MEMENTO_ALLOCFILL_USHORT) | (((unsigned int)MEMENTO_ALLOCFILL_USHORT)<<16))
87 #define MEMENTO_ALLOCFILL_PTR (void *)(((uintptr_t)MEMENTO_ALLOCFILL_UINT) | ((((uintptr_t)MEMENTO_ALLOCFILL_UINT)<<16)<<16))
88 #define MEMENTO_FREEFILL_UBYTE ((unsigned char)(MEMENTO_FREEFILL))
89 #define MEMENTO_FREEFILL_USHORT (((unsigned short)MEMENTO_FREEFILL_UBYTE) | (((unsigned short)MEMENTO_FREEFILL_UBYTE)<<8))
90 #define MEMENTO_FREEFILL_UINT (((unsigned int)MEMENTO_FREEFILL_USHORT) | (((unsigned int)MEMENTO_FREEFILL_USHORT)<<16))
91 #define MEMENTO_FREEFILL_PTR (void *)(((uintptr_t)MEMENTO_FREEFILL_UINT) | ((((uintptr_t)MEMENTO_FREEFILL_UINT)<<16)<<16))
92
93 #ifdef MEMENTO
94
95 #ifndef MEMENTO_CPP_EXTRAS_ONLY
96
97 #ifdef MEMENTO_ANDROID
98 #include <android/log.h>
99
100 static char log_buffer[4096];
101 static int log_fill = 0;
102
103 static char log_buffer2[4096];
104
105 static int
106 android_fprintf(FILE *file, const char *fmt, ...)
107 {
108 va_list args;
109 char *p, *q;
110
111 va_start(args, fmt);
112 vsnprintf(log_buffer2, sizeof(log_buffer2)-1, fmt, args);
113 va_end(args);
114
115 /* Ensure we are always null terminated */
116 log_buffer2[sizeof(log_buffer2)-1] = 0;
117
118 p = log_buffer2;
119 q = p;
120 do
121 {
122 /* Find the end of the string, or the next \n */
123 while (*p && *p != '\n')
124 p++;
125
126 /* We need to output from q to p. Limit ourselves to what
127 * will fit in the existing */
128 if (p - q >= sizeof(log_buffer)-1 - log_fill)
129 p = q + sizeof(log_buffer)-1 - log_fill;
130
131 memcpy(&log_buffer[log_fill], q, p-q);
132 log_fill += p-q;
133 if (*p == '\n')
134 {
135 log_buffer[log_fill] = 0;
136 __android_log_print(ANDROID_LOG_ERROR, "memento", "%s", log_buffer);
137 usleep(1);
138 log_fill = 0;
139 p++; /* Skip over the \n */
140 }
141 else if (log_fill >= sizeof(log_buffer)-1)
142 {
143 log_buffer[sizeof(log_buffer2)-1] = 0;
144 __android_log_print(ANDROID_LOG_ERROR, "memento", "%s", log_buffer);
145 usleep(1);
146 log_fill = 0;
147 }
148 q = p;
149 }
150 while (*p);
151
152 return 0;
153 }
154
155 #define fprintf android_fprintf
156 #define MEMENTO_STACKTRACE_METHOD 3
157 #endif
158
159 /* _WIN64 defined implies _WIN32 will be */
160 #ifdef _WIN32
161 #include <windows.h>
162
163 static int
164 windows_fprintf(FILE *file, const char *fmt, ...)
165 {
166 va_list args;
167 char text[4096];
168 int ret;
169
170 va_start(args, fmt);
171 ret = vfprintf(file, fmt, args);
172 va_end(args);
173
174 va_start(args, fmt);
175 vsnprintf(text, 4096, fmt, args);
176 OutputDebugStringA(text);
177 va_end(args);
178
179 return ret;
180 }
181
182 #define fprintf windows_fprintf
183 #endif
184
185 #ifndef MEMENTO_STACKTRACE_METHOD
186 #ifdef __GNUC__
187 #define MEMENTO_STACKTRACE_METHOD 1
188 #endif
189 #ifdef _WIN32
190 #define MEMENTO_STACKTRACE_METHOD 2
191 #endif
192 #endif
193
194 #if defined(__linux__)
195 #define MEMENTO_HAS_FORK
196 #elif defined(__APPLE__) && defined(__MACH__)
197 #define MEMENTO_HAS_FORK
198 #endif
199
200 /* Define the underlying allocators, just in case */
201 void *MEMENTO_UNDERLYING_MALLOC(size_t);
202 void MEMENTO_UNDERLYING_FREE(void *);
203 void *MEMENTO_UNDERLYING_REALLOC(void *,size_t);
204 void *MEMENTO_UNDERLYING_CALLOC(size_t,size_t);
205
206 /* And some other standard functions we use. We don't include the header
207 * files, just in case they pull in unexpected others. */
208 int atoi(const char *);
209 char *getenv(const char *);
210
211 /* How far to search for pointers in each block when calculating nestings */
212 /* mupdf needs at least 34000ish (sizeof(fz_shade))/ */
213 #define MEMENTO_PTRSEARCH 65536
214
215 #ifndef MEMENTO_MAXPATTERN
216 #define MEMENTO_MAXPATTERN 0
217 #endif
218
219 #ifdef MEMENTO_GS_HACKS
220 #include "valgrind.h"
221 #else
222 #ifdef HAVE_VALGRIND
223 #include "valgrind/memcheck.h"
224 #else
225 #define VALGRIND_MAKE_MEM_NOACCESS(p,s) do { } while (0==1)
226 #define VALGRIND_MAKE_MEM_UNDEFINED(p,s) do { } while (0==1)
227 #define VALGRIND_MAKE_MEM_DEFINED(p,s) do { } while (0==1)
228 #endif
229 #endif
230
231 enum {
232 Memento_PreSize = 16,
233 Memento_PostSize = 16
234 };
235
236 /* Some compile time checks */
237 typedef struct
238 {
239 char MEMENTO_PRESIZE_MUST_BE_A_MULTIPLE_OF_4[Memento_PreSize & 3 ? -1 : 1];
240 char MEMENTO_POSTSIZE_MUST_BE_A_MULTIPLE_OF_4[Memento_PostSize & 3 ? -1 : 1];
241 char MEMENTO_POSTSIZE_MUST_BE_AT_LEAST_4[Memento_PostSize >= 4 ? 1 : -1];
242 char MEMENTO_PRESIZE_MUST_BE_AT_LEAST_4[Memento_PreSize >= 4 ? 1 : -1];
243 } MEMENTO_SANITY_CHECK_STRUCT;
244
245 #define MEMENTO_UINT32 unsigned int
246 #define MEMENTO_UINT16 unsigned short
247
248 #define MEMENTO_PREFILL_UINT32 ((MEMENTO_UINT32)(MEMENTO_PREFILL | (MEMENTO_PREFILL <<8) | (MEMENTO_PREFILL <<16) |(MEMENTO_PREFILL <<24)))
249 #define MEMENTO_POSTFILL_UINT16 ((MEMENTO_UINT16)(MEMENTO_POSTFILL | (MEMENTO_POSTFILL<<8)))
250 #define MEMENTO_POSTFILL_UINT32 ((MEMENTO_UINT32)(MEMENTO_POSTFILL | (MEMENTO_POSTFILL<<8) | (MEMENTO_POSTFILL<<16) |(MEMENTO_POSTFILL<<24)))
251 #define MEMENTO_FREEFILL_UINT16 ((MEMENTO_UINT16)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8)))
252 #define MEMENTO_FREEFILL_UINT32 ((MEMENTO_UINT32)(MEMENTO_FREEFILL | (MEMENTO_FREEFILL<<8) | (MEMENTO_FREEFILL<<16) |(MEMENTO_FREEFILL<<24)))
253
254 enum {
255 Memento_Flag_OldBlock = 1,
256 Memento_Flag_HasParent = 2,
257 Memento_Flag_BreakOnFree = 4,
258 Memento_Flag_BreakOnRealloc = 8,
259 Memento_Flag_Freed = 16,
260 Memento_Flag_KnownLeak = 32,
261 Memento_Flag_Reported = 64
262 };
263
264 enum {
265 Memento_EventType_malloc = 0,
266 Memento_EventType_calloc = 1,
267 Memento_EventType_realloc = 2,
268 Memento_EventType_free = 3,
269 Memento_EventType_new = 4,
270 Memento_EventType_delete = 5,
271 Memento_EventType_newArray = 6,
272 Memento_EventType_deleteArray = 7,
273 Memento_EventType_takeRef = 8,
274 Memento_EventType_dropRef = 9,
275 Memento_EventType_reference = 10
276 };
277
278 static const char *eventType[] =
279 {
280 "malloc",
281 "calloc",
282 "realloc",
283 "free",
284 "new",
285 "delete",
286 "new[]",
287 "delete[]",
288 "takeRef",
289 "dropRef",
290 "reference"
291 };
292
293 /* When we list leaked blocks at the end of execution, we search for pointers
294 * between blocks in order to be able to give a nice nested view.
295 * Unfortunately, if you have are running your own allocator (such as
296 * postscript's chunk allocator) you can often find that the header of the
297 * block always contains pointers to next or previous blocks. This tends to
298 * mean the nesting displayed is "uninteresting" at best :)
299 *
300 * As a hack to get around this, we have a define MEMENTO_SKIP_SEARCH that
301 * indicates how many bytes to skip over at the start of the chunk.
302 * This may cause us to miss true nestings, but such is life...
303 */
304 #ifndef MEMENTO_SEARCH_SKIP
305 #ifdef MEMENTO_GS_HACKS
306 #define MEMENTO_SEARCH_SKIP (2*sizeof(void *))
307 #else
308 #define MEMENTO_SEARCH_SKIP 0
309 #endif
310 #endif
311
312 #define MEMENTO_CHILD_MAGIC ((Memento_BlkHeader *)('M' | ('3' << 8) | ('m' << 16) | ('3' << 24)))
313 #define MEMENTO_SIBLING_MAGIC ((Memento_BlkHeader *)('n' | ('t' << 8) | ('0' << 16) | ('!' << 24)))
314
315 #ifdef MEMENTO_DETAILS
316 typedef struct Memento_BlkDetails Memento_BlkDetails;
317
318 struct Memento_BlkDetails
319 {
320 Memento_BlkDetails *next;
321 char type;
322 char count;
323 int sequence;
324 void *stack[1];
325 };
326 #endif /* MEMENTO_DETAILS */
327
328 typedef struct Memento_BlkHeader Memento_BlkHeader;
329
330 struct Memento_BlkHeader
331 {
332 size_t rawsize;
333 int sequence;
334 int lastCheckedOK;
335 int flags;
336 Memento_BlkHeader *next;
337 Memento_BlkHeader *prev; /* Reused as 'parent' when printing nested list */
338
339 const char *label;
340
341 /* Entries for nesting display calculations. Set to magic
342 * values at all other time. */
343 Memento_BlkHeader *child;
344 Memento_BlkHeader *sibling;
345
346 #ifdef MEMENTO_DETAILS
347 Memento_BlkDetails *details;
348 Memento_BlkDetails **details_tail;
349 #endif
350
351 char preblk[Memento_PreSize];
352 };
353
354 /* In future this could (should) be a smarter data structure, like, say,
355 * splay trees. For now, we use a list.
356 */
357 typedef struct Memento_Blocks
358 {
359 Memento_BlkHeader *head;
360 Memento_BlkHeader *tail;
361 } Memento_Blocks;
362
363 /* What sort of Mutex should we use? */
364 #ifdef MEMENTO_LOCKLESS
365 typedef int Memento_mutex;
366
367 static void Memento_initMutex(Memento_mutex *m)
368 {
369 (void)m;
370 }
371
372 #define MEMENTO_DO_LOCK() do { } while (0)
373 #define MEMENTO_DO_UNLOCK() do { } while (0)
374
375 #else
376 #if defined(_WIN32) || defined(_WIN64)
377 /* Windows */
378 typedef CRITICAL_SECTION Memento_mutex;
379
380 static void Memento_initMutex(Memento_mutex *m)
381 {
382 InitializeCriticalSection(m);
383 }
384
385 #define MEMENTO_DO_LOCK() \
386 EnterCriticalSection(&memento.mutex)
387 #define MEMENTO_DO_UNLOCK() \
388 LeaveCriticalSection(&memento.mutex)
389
390 #else
391 #include <pthread.h>
392 typedef pthread_mutex_t Memento_mutex;
393
394 static void Memento_initMutex(Memento_mutex *m)
395 {
396 pthread_mutex_init(m, NULL);
397 }
398
399 #define MEMENTO_DO_LOCK() \
400 pthread_mutex_lock(&memento.mutex)
401 #define MEMENTO_DO_UNLOCK() \
402 pthread_mutex_unlock(&memento.mutex)
403
404 #endif
405 #endif
406
407 /* And our global structure */
408 static struct {
409 int inited;
410 Memento_Blocks used;
411 Memento_Blocks free;
412 size_t freeListSize;
413 int sequence;
414 int paranoia;
415 int paranoidAt;
416 int countdown;
417 int lastChecked;
418 int breakAt;
419 int failAt;
420 int failing;
421 int nextFailAt;
422 int squeezeAt;
423 int squeezing;
424 int segv;
425 int pattern;
426 int nextPattern;
427 int patternBit;
428 int leaking;
429 size_t maxMemory;
430 size_t alloc;
431 size_t peakAlloc;
432 size_t totalAlloc;
433 size_t numMallocs;
434 size_t numFrees;
435 size_t numReallocs;
436 Memento_mutex mutex;
437 } memento;
438
439 #define MEMENTO_EXTRASIZE (sizeof(Memento_BlkHeader) + Memento_PostSize)
440
441 /* Round up size S to the next multiple of N (where N is a power of 2) */
442 #define MEMENTO_ROUNDUP(S,N) ((S + N-1)&~(N-1))
443
444 #define MEMBLK_SIZE(s) MEMENTO_ROUNDUP(s + MEMENTO_EXTRASIZE, MEMENTO_MAXALIGN)
445
446 #define MEMBLK_FROMBLK(B) (&((Memento_BlkHeader*)(void *)(B))[-1])
447 #define MEMBLK_TOBLK(B) ((void*)(&((Memento_BlkHeader*)(void*)(B))[1]))
448 #define MEMBLK_POSTPTR(B) \
449 (&((unsigned char *)(void *)(B))[(B)->rawsize + sizeof(Memento_BlkHeader)])
450
451 enum
452 {
453 SkipStackBackTraceLevels = 4
454 };
455
456 #if defined(MEMENTO_STACKTRACE_METHOD) && MEMENTO_STACKTRACE_METHOD == 1
457 extern size_t backtrace(void **, int);
458 extern void backtrace_symbols_fd(void **, size_t, int);
459 extern char **backtrace_symbols(void **, size_t);
460
461 #define MEMENTO_BACKTRACE_MAX 256
462 static void (*print_stack_value)(void *address);
463
464 /* Libbacktrace gubbins - relies on us having libdl to load the .so */
465 #ifdef HAVE_LIBDL
466 #include <dlfcn.h>
467
468 typedef void (*backtrace_error_callback) (void *data, const char *msg, int errnum);
469
470 typedef struct backtrace_state *(*backtrace_create_state_type)(
471 const char *filename, int threaded,
472 backtrace_error_callback error_callback, void *data);
473
474 typedef int (*backtrace_full_callback) (void *data, uintptr_t pc,
475 const char *filename, int lineno,
476 const char *function);
477
478 typedef int (*backtrace_pcinfo_type)(struct backtrace_state *state,
479 uintptr_t pc,
480 backtrace_full_callback callback,
481 backtrace_error_callback error_callback,
482 void *data);
483
484 typedef void (*backtrace_syminfo_callback) (void *data, uintptr_t pc,
485 const char *symname,
486 uintptr_t symval,
487 uintptr_t symsize);
488
489 typedef int (*backtrace_syminfo_type)(struct backtrace_state *state,
490 uintptr_t addr,
491 backtrace_syminfo_callback callback,
492 backtrace_error_callback error_callback,
493 void *data);
494
495 static backtrace_syminfo_type backtrace_syminfo;
496 static backtrace_create_state_type backtrace_create_state;
497 static backtrace_pcinfo_type backtrace_pcinfo;
498 static struct backtrace_state *my_backtrace_state;
499 static void *libbt;
500 static char backtrace_exe[4096];
501 static void *current_addr;
502
503 static void error2_cb(void *data, const char *msg, int errnum)
504 {
505 }
506
507 static void syminfo_cb(void *data, uintptr_t pc, const char *symname, uintptr_t symval, uintptr_t symsize)
508 {
509 if (sizeof(void *) == 4)
510 fprintf(stderr, " 0x%08lx %s\n", pc, symname?symname:"?");
511 else
512 fprintf(stderr, " 0x%016lx %s\n", pc, symname?symname:"?");
513 }
514
515 static void error_cb(void *data, const char *msg, int errnum)
516 {
517 backtrace_syminfo(my_backtrace_state,
518 (uintptr_t)current_addr,
519 syminfo_cb,
520 error2_cb,
521 NULL);
522 }
523
524 static int full_cb(void *data, uintptr_t pc, const char *fname, int line, const char *fn)
525 {
526 if (sizeof(void *) == 4)
527 fprintf(stderr, " 0x%08lx %s(%s:%d)\n", pc, fn?fn:"?", fname?fname:"?", line);
528 else
529 fprintf(stderr, " 0x%016lx %s(%s:%d)\n", pc, fn?fn:"?", fname?fname:"?", line);
530 return 0;
531 }
532
533 static void print_stack_libbt(void *addr)
534 {
535 current_addr = addr;
536 backtrace_pcinfo(my_backtrace_state,
537 (uintptr_t)addr,
538 full_cb,
539 error_cb,
540 NULL);
541 }
542
543 static void print_stack_libbt_failed(void *addr)
544 {
545 char **strings;
546 #if 0
547 /* Let's use a hack from Julian Smith to call gdb to extract the information */
548 /* Disabled for now, as I can't make this work. */
549 static char command[1024];
550 int e;
551 static int gdb_invocation_failed = 0;
552
553 if (gdb_invocation_failed == 0)
554 {
555 snprintf(command, sizeof(command),
556 //"gdb -q --batch -p=%i -ex 'info line *%p' -ex quit 2>/dev/null",
557 "gdb -q --batch -p=%i -ex 'info line *%p' -ex quit 2>/dev/null| egrep -v '(Thread debugging using)|(Using host libthread_db library)|(A debugging session is active)|(will be detached)|(Quit anyway)|(No such file or directory)|(^0x)|(^$)'",
558 getpid(), addr);
559 printf("%s\n", command);
560 e = system(command);
561 if (e == 0)
562 return; /* That'll do! */
563 gdb_invocation_failed = 1; /* If it's failed once, it'll probably keep failing. */
564 }
565 #endif
566
567 /* We couldn't even get gdb! Make do. */
568 strings = backtrace_symbols(&addr, 1);
569
570 if (strings == NULL || strings[0] == NULL)
571 {
572 if (sizeof(void *) == 4)
573 fprintf(stderr, " [0x%08lx]\n", (uintptr_t)addr);
574 else
575 fprintf(stderr, " [0x%016lx]\n", (uintptr_t)addr);
576 }
577 else
578 {
579 fprintf(stderr, " %s\n", strings[0]);
580 }
581 (free)(strings);
582 }
583
584 static int init_libbt(void)
585 {
586 static int libbt_inited = 0;
587
588 if (libbt_inited)
589 return 0;
590 libbt_inited = 1;
591
592 libbt = dlopen("libbacktrace.so", RTLD_LAZY);
593 if (libbt == NULL)
594 libbt = dlopen("/opt/lib/libbacktrace.so", RTLD_LAZY);
595 if (libbt == NULL)
596 libbt = dlopen("/lib/libbacktrace.so", RTLD_LAZY);
597 if (libbt == NULL)
598 libbt = dlopen("/usr/lib/libbacktrace.so", RTLD_LAZY);
599 if (libbt == NULL)
600 libbt = dlopen("/usr/local/lib/libbacktrace.so", RTLD_LAZY);
601 if (libbt == NULL)
602 goto fail;
603
604 backtrace_create_state = dlsym(libbt, "backtrace_create_state");
605 backtrace_syminfo = dlsym(libbt, "backtrace_syminfo");
606 backtrace_pcinfo = dlsym(libbt, "backtrace_pcinfo");
607
608 if (backtrace_create_state == NULL ||
609 backtrace_syminfo == NULL ||
610 backtrace_pcinfo == NULL)
611 {
612 goto fail;
613 }
614
615 my_backtrace_state = backtrace_create_state(backtrace_exe,
616 1 /*BACKTRACE_SUPPORTS_THREADS*/,
617 error_cb,
618 NULL);
619 if (my_backtrace_state == NULL)
620 goto fail;
621
622 print_stack_value = print_stack_libbt;
623
624 return 1;
625
626 fail:
627 fprintf(stderr,
628 "MEMENTO: libbacktrace.so failed to load; backtraces will be sparse.\n"
629 "MEMENTO: See memento.h for how to rectify this.\n");
630 libbt = NULL;
631 backtrace_create_state = NULL;
632 backtrace_syminfo = NULL;
633 print_stack_value = print_stack_libbt_failed;
634 return 0;
635 }
636 #endif
637
638 static void print_stack_default(void *addr)
639 {
640 char **strings = backtrace_symbols(&addr, 1);
641
642 if (strings == NULL || strings[0] == NULL)
643 {
644 fprintf(stderr, " ["FMTP"]\n", addr);
645 }
646 #ifdef HAVE_LIBDL
647 else if (strchr(strings[0], ':') == NULL)
648 {
649 /* Probably a "path [address]" format string */
650 char *s = strchr(strings[0], ' ');
651
652 if (s != strings[0])
653 {
654 memcpy(backtrace_exe, strings[0], s - strings[0]);
655 backtrace_exe[s-strings[0]] = 0;
656 init_libbt();
657 print_stack_value(addr);
658 }
659 }
660 #endif
661 else
662 {
663 fprintf(stderr, " %s\n", strings[0]);
664 }
665 free(strings);
666 }
667
668 static void Memento_initStacktracer(void)
669 {
670 print_stack_value = print_stack_default;
671 }
672
673 static int Memento_getStacktrace(void **stack, int *skip)
674 {
675 size_t num;
676
677 num = backtrace(&stack[0], MEMENTO_BACKTRACE_MAX);
678
679 *skip = SkipStackBackTraceLevels;
680 if (num <= SkipStackBackTraceLevels)
681 return 0;
682 return (int)(num-SkipStackBackTraceLevels);
683 }
684
685 static void Memento_showStacktrace(void **stack, int numberOfFrames)
686 {
687 int i;
688
689 for (i = 0; i < numberOfFrames; i++)
690 {
691 print_stack_value(stack[i]);
692 }
693 }
694 #elif defined(MEMENTO_STACKTRACE_METHOD) && MEMENTO_STACKTRACE_METHOD == 2
695 #include <Windows.h>
696
697 /* We use DbgHelp.dll rather than DbgHelp.lib. This avoids us needing
698 * extra link time complications, and enables us to fall back gracefully
699 * if the DLL cannot be found.
700 *
701 * To achieve this we have our own potted versions of the required types
702 * inline here.
703 */
704 #ifdef _WIN64
705 typedef DWORD64 DWORD_NATIVESIZED;
706 #else
707 typedef DWORD DWORD_NATIVESIZED;
708 #endif
709
710 #define MEMENTO_BACKTRACE_MAX 64
711
712 typedef USHORT (__stdcall *My_CaptureStackBackTraceType)(__in ULONG, __in ULONG, __out PVOID*, __out_opt PULONG);
713
714 typedef struct MY_IMAGEHLP_LINE {
715 DWORD SizeOfStruct;
716 PVOID Key;
717 DWORD LineNumber;
718 PCHAR FileName;
719 DWORD_NATIVESIZED Address;
720 } MY_IMAGEHLP_LINE, *MY_PIMAGEHLP_LINE;
721
722 typedef BOOL (__stdcall *My_SymGetLineFromAddrType)(HANDLE hProcess, DWORD_NATIVESIZED dwAddr, PDWORD pdwDisplacement, MY_PIMAGEHLP_LINE Line);
723
724 typedef struct MY_SYMBOL_INFO {
725 ULONG SizeOfStruct;
726 ULONG TypeIndex; // Type Index of symbol
727 ULONG64 Reserved[2];
728 ULONG info;
729 ULONG Size;
730 ULONG64 ModBase; // Base Address of module containing this symbol
731 ULONG Flags;
732 ULONG64 Value; // Value of symbol, ValuePresent should be 1
733 ULONG64 Address; // Address of symbol including base address of module
734 ULONG Register; // register holding value or pointer to value
735 ULONG Scope; // scope of the symbol
736 ULONG Tag; // pdb classification
737 ULONG NameLen; // Actual length of name
738 ULONG MaxNameLen;
739 CHAR Name[1]; // Name of symbol
740 } MY_SYMBOL_INFO, *MY_PSYMBOL_INFO;
741
742 typedef BOOL (__stdcall *My_SymFromAddrType)(HANDLE hProcess, DWORD64 Address, PDWORD64 Displacement, MY_PSYMBOL_INFO Symbol);
743 typedef BOOL (__stdcall *My_SymInitializeType)(HANDLE hProcess, PSTR UserSearchPath, BOOL fInvadeProcess);
744
745 static My_CaptureStackBackTraceType Memento_CaptureStackBackTrace;
746 static My_SymGetLineFromAddrType Memento_SymGetLineFromAddr;
747 static My_SymFromAddrType Memento_SymFromAddr;
748 static My_SymInitializeType Memento_SymInitialize;
749 static HANDLE Memento_process;
750
751 static void Memento_initStacktracer(void)
752 {
753 HMODULE mod = LoadLibrary("kernel32.dll");
754
755 if (mod == NULL)
756 return;
757 Memento_CaptureStackBackTrace = (My_CaptureStackBackTraceType)(GetProcAddress(mod, "RtlCaptureStackBackTrace"));
758 if (Memento_CaptureStackBackTrace == NULL)
759 return;
760 mod = LoadLibrary("Dbghelp.dll");
761 if (mod == NULL) {
762 Memento_CaptureStackBackTrace = NULL;
763 return;
764 }
765 Memento_SymGetLineFromAddr =
766 (My_SymGetLineFromAddrType)(GetProcAddress(mod,
767 #ifdef _WIN64
768 "SymGetLineFromAddr64"
769 #else
770 "SymGetLineFromAddr"
771 #endif
772 ));
773 if (Memento_SymGetLineFromAddr == NULL) {
774 Memento_CaptureStackBackTrace = NULL;
775 return;
776 }
777 Memento_SymFromAddr = (My_SymFromAddrType)(GetProcAddress(mod, "SymFromAddr"));
778 if (Memento_SymFromAddr == NULL) {
779 Memento_CaptureStackBackTrace = NULL;
780 return;
781 }
782 Memento_SymInitialize = (My_SymInitializeType)(GetProcAddress(mod, "SymInitialize"));
783 if (Memento_SymInitialize == NULL) {
784 Memento_CaptureStackBackTrace = NULL;
785 return;
786 }
787 Memento_process = GetCurrentProcess();
788 Memento_SymInitialize(Memento_process, NULL, TRUE);
789 }
790
791 static int Memento_getStacktrace(void **stack, int *skip)
792 {
793 if (Memento_CaptureStackBackTrace == NULL)
794 return 0;
795
796 *skip = 0;
797 /* Limit us to 63 levels due to windows bug */
798 return Memento_CaptureStackBackTrace(SkipStackBackTraceLevels, 63-SkipStackBackTraceLevels, stack, NULL);
799 }
800
801 static void Memento_showStacktrace(void **stack, int numberOfFrames)
802 {
803 MY_IMAGEHLP_LINE line;
804 int i;
805 char symbol_buffer[sizeof(MY_SYMBOL_INFO) + 1024 + 1];
806 MY_SYMBOL_INFO *symbol = (MY_SYMBOL_INFO *)symbol_buffer;
807
808 symbol->MaxNameLen = 1024;
809 symbol->SizeOfStruct = sizeof(MY_SYMBOL_INFO);
810 line.SizeOfStruct = sizeof(MY_IMAGEHLP_LINE);
811 for (i = 0; i < numberOfFrames; i++)
812 {
813 DWORD64 dwDisplacement64;
814 DWORD dwDisplacement;
815 Memento_SymFromAddr(Memento_process, (DWORD64)(stack[i]), &dwDisplacement64, symbol);
816 Memento_SymGetLineFromAddr(Memento_process, (DWORD_NATIVESIZED)(stack[i]), &dwDisplacement, &line);
817 fprintf(stderr, " %s in %s:%d\n", symbol->Name, line.FileName, line.LineNumber);
818 }
819 }
820 #elif defined(MEMENTO_STACKTRACE_METHOD) && MEMENTO_STACKTRACE_METHOD == 3
821
822 #include <unwind.h>
823 #include <dlfcn.h>
824
825 /* From cxxabi.h */
826 extern char* __cxa_demangle(const char* mangled_name,
827 char* output_buffer,
828 size_t* length,
829 int* status);
830
831 static void Memento_initStacktracer(void)
832 {
833 }
834
835 #define MEMENTO_BACKTRACE_MAX 256
836
837 typedef struct
838 {
839 int count;
840 void **addr;
841 } my_unwind_details;
842
843 static _Unwind_Reason_Code unwind_populate_callback(struct _Unwind_Context *context,
844 void *arg)
845 {
846 my_unwind_details *uw = (my_unwind_details *)arg;
847 int count = uw->count;
848
849 if (count >= MEMENTO_BACKTRACE_MAX)
850 return _URC_END_OF_STACK;
851
852 uw->addr[count] = (void *)_Unwind_GetIP(context);
853 uw->count++;
854
855 return _URC_NO_REASON;
856 }
857
858 static int Memento_getStacktrace(void **stack, int *skip)
859 {
860 my_unwind_details uw = { 0, stack };
861
862 *skip = 0;
863
864 /* Collect the backtrace. Deliberately only unwind once,
865 * and avoid using malloc etc until this completes just
866 * in case. */
867 _Unwind_Backtrace(unwind_populate_callback, &uw);
868 if (uw.count <= SkipStackBackTraceLevels)
869 return 0;
870
871 *skip = SkipStackBackTraceLevels;
872 return uw.count-SkipStackBackTraceLevels;
873 }
874
875 static void Memento_showStacktrace(void **stack, int numberOfFrames)
876 {
877 int i;
878
879 for (i = 0; i < numberOfFrames; i++)
880 {
881 Dl_info info;
882 if (dladdr(stack[i], &info))
883 {
884 int status = 0;
885 const char *sym = info.dli_sname ? info.dli_sname : "<unknown>";
886 char *demangled = __cxa_demangle(sym, NULL, 0, &status);
887 int offset = stack[i] - info.dli_saddr;
888 fprintf(stderr, " ["FMTP"]%s(+0x%x)\n", stack[i], demangled && status == 0 ? demangled : sym, offset);
889 free(demangled);
890 }
891 else
892 {
893 fprintf(stderr, " ["FMTP"]\n", stack[i]);
894 }
895 }
896 }
897
898 #else
899 static void Memento_initStacktracer(void)
900 {
901 }
902
903 static int Memento_getStacktrace(void **stack, int *skip)
904 {
905 *skip = 0;
906 return 0;
907 }
908
909 static void Memento_showStacktrace(void **stack, int numberOfFrames)
910 {
911 }
912 #endif /* MEMENTO_STACKTRACE_METHOD */
913
914 #ifdef MEMENTO_DETAILS
915 static void Memento_storeDetails(Memento_BlkHeader *head, int type)
916 {
917 void *stack[MEMENTO_BACKTRACE_MAX];
918 Memento_BlkDetails *details;
919 int count;
920 int skip;
921
922 if (head == NULL)
923 return;
924
925 #ifdef MEMENTO_STACKTRACE_METHOD
926 count = Memento_getStacktrace(stack, &skip);
927 #else
928 skip = 0;
929 count = 0;
930 #endif
931
932 details = MEMENTO_UNDERLYING_MALLOC(sizeof(*details) + (count-1) * sizeof(void *));
933 if (details == NULL)
934 return;
935
936 if (count)
937 memcpy(&details->stack, &stack[skip], count * sizeof(void *));
938
939 details->type = type;
940 details->count = count;
941 details->sequence = memento.sequence;
942 details->next = NULL;
943 VALGRIND_MAKE_MEM_DEFINED(&head->details_tail, sizeof(head->details_tail));
944 *head->details_tail = details;
945 head->details_tail = &details->next;
946 VALGRIND_MAKE_MEM_NOACCESS(&head->details_tail, sizeof(head->details_tail));
947 }
948 #endif
949
950 void (Memento_bt)(void)
951 {
952 #ifdef MEMENTO_STACKTRACE_METHOD
953 void *stack[MEMENTO_BACKTRACE_MAX];
954 int count;
955 int skip;
956
957 count = Memento_getStacktrace(stack, &skip);
958 Memento_showStacktrace(&stack[skip-2], count-skip+2);
959 #endif
960 }
961
962 static void Memento_bt_internal(int skip2)
963 {
964 #ifdef MEMENTO_STACKTRACE_METHOD
965 void *stack[MEMENTO_BACKTRACE_MAX];
966 int count;
967 int skip;
968
969 count = Memento_getStacktrace(stack, &skip);
970 Memento_showStacktrace(&stack[skip+skip2], count-skip-skip2);
971 #endif
972 }
973
974 static int Memento_checkAllMemoryLocked(void);
975
976 void Memento_breakpoint(void)
977 {
978 /* A handy externally visible function for breakpointing */
979 #if 0 /* Enable this to force automatic breakpointing */
980 #ifndef NDEBUG
981 #ifdef _MSC_VER
982 __asm int 3;
983 #endif
984 #endif
985 #endif
986 }
987
988 static void Memento_init(void);
989
990 #define MEMENTO_LOCK() \
991 do { if (!memento.inited) Memento_init(); MEMENTO_DO_LOCK(); } while (0)
992
993 #define MEMENTO_UNLOCK() \
994 do { MEMENTO_DO_UNLOCK(); } while (0)
995
996 /* Do this as a macro to prevent another level in the callstack,
997 * which is annoying while stepping. */
998 #define Memento_breakpointLocked() \
999 do { MEMENTO_UNLOCK(); Memento_breakpoint(); MEMENTO_LOCK(); } while (0)
1000
1001 static void Memento_addBlockHead(Memento_Blocks *blks,
1002 Memento_BlkHeader *b,
1003 int type)
1004 {
1005 if (blks->tail == NULL)
1006 blks->tail = b;
1007 b->next = blks->head;
1008 b->prev = NULL;
1009 if (blks->head)
1010 {
1011 VALGRIND_MAKE_MEM_DEFINED(&blks->head->prev, sizeof(blks->head->prev));
1012 blks->head->prev = b;
1013 VALGRIND_MAKE_MEM_NOACCESS(&blks->head->prev, sizeof(blks->head->prev));
1014 }
1015 blks->head = b;
1016 #ifndef MEMENTO_LEAKONLY
1017 memset(b->preblk, MEMENTO_PREFILL, Memento_PreSize);
1018 memset(MEMBLK_POSTPTR(b), MEMENTO_POSTFILL, Memento_PostSize);
1019 #endif
1020 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(b), Memento_PostSize);
1021 if (type == 0) { /* malloc */
1022 VALGRIND_MAKE_MEM_UNDEFINED(MEMBLK_TOBLK(b), b->rawsize);
1023 } else if (type == 1) { /* free */
1024 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_TOBLK(b), b->rawsize);
1025 }
1026 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1027 }
1028
1029 static void Memento_addBlockTail(Memento_Blocks *blks,
1030 Memento_BlkHeader *b,
1031 int type)
1032 {
1033 VALGRIND_MAKE_MEM_DEFINED(&blks->tail, sizeof(Memento_BlkHeader *));
1034 if (blks->head == NULL)
1035 blks->head = b;
1036 b->prev = blks->tail;
1037 b->next = NULL;
1038 if (blks->tail) {
1039 VALGRIND_MAKE_MEM_DEFINED(&blks->tail->next, sizeof(blks->tail->next));
1040 blks->tail->next = b;
1041 VALGRIND_MAKE_MEM_NOACCESS(&blks->tail->next, sizeof(blks->tail->next));
1042 }
1043 blks->tail = b;
1044 #ifndef MEMENTO_LEAKONLY
1045 memset(b->preblk, MEMENTO_PREFILL, Memento_PreSize);
1046 memset(MEMBLK_POSTPTR(b), MEMENTO_POSTFILL, Memento_PostSize);
1047 #endif
1048 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(b), Memento_PostSize);
1049 if (type == 0) { /* malloc */
1050 VALGRIND_MAKE_MEM_UNDEFINED(MEMBLK_TOBLK(b), b->rawsize);
1051 } else if (type == 1) { /* free */
1052 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_TOBLK(b), b->rawsize);
1053 }
1054 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1055 VALGRIND_MAKE_MEM_NOACCESS(&blks->tail, sizeof(Memento_BlkHeader *));
1056 }
1057
1058 typedef struct BlkCheckData {
1059 int found;
1060 int preCorrupt;
1061 int postCorrupt;
1062 int freeCorrupt;
1063 size_t index;
1064 } BlkCheckData;
1065
1066 #ifndef MEMENTO_LEAKONLY
1067 static int Memento_Internal_checkAllocedBlock(Memento_BlkHeader *b, void *arg)
1068 {
1069 int i;
1070 MEMENTO_UINT32 *ip;
1071 unsigned char *p;
1072 BlkCheckData *data = (BlkCheckData *)arg;
1073
1074 ip = (MEMENTO_UINT32 *)(void *)(b->preblk);
1075 i = Memento_PreSize>>2;
1076 do {
1077 if (*ip++ != MEMENTO_PREFILL_UINT32)
1078 goto pre_corrupt;
1079 } while (--i);
1080 if (0) {
1081 pre_corrupt:
1082 data->preCorrupt = 1;
1083 }
1084 /* Postfill may not be aligned, so have to be slower */
1085 p = MEMBLK_POSTPTR(b);
1086 i = Memento_PostSize-4;
1087 if ((intptr_t)p & 1)
1088 {
1089 if (*p++ != MEMENTO_POSTFILL)
1090 goto post_corrupt;
1091 i--;
1092 }
1093 if ((intptr_t)p & 2)
1094 {
1095 if (*(MEMENTO_UINT16 *)p != MEMENTO_POSTFILL_UINT16)
1096 goto post_corrupt;
1097 p += 2;
1098 i -= 2;
1099 }
1100 do {
1101 if (*(MEMENTO_UINT32 *)p != MEMENTO_POSTFILL_UINT32)
1102 goto post_corrupt;
1103 p += 4;
1104 i -= 4;
1105 } while (i >= 0);
1106 if (i & 2)
1107 {
1108 if (*(MEMENTO_UINT16 *)p != MEMENTO_POSTFILL_UINT16)
1109 goto post_corrupt;
1110 p += 2;
1111 }
1112 if (i & 1)
1113 {
1114 if (*p != MEMENTO_POSTFILL)
1115 goto post_corrupt;
1116 }
1117 if (0) {
1118 post_corrupt:
1119 data->postCorrupt = 1;
1120 }
1121 if ((data->freeCorrupt | data->preCorrupt | data->postCorrupt) == 0) {
1122 b->lastCheckedOK = memento.sequence;
1123 }
1124 data->found |= 1;
1125 return 0;
1126 }
1127
1128 static int Memento_Internal_checkFreedBlock(Memento_BlkHeader *b, void *arg)
1129 {
1130 size_t i;
1131 unsigned char *p;
1132 BlkCheckData *data = (BlkCheckData *)arg;
1133
1134 p = MEMBLK_TOBLK(b); /* p will always be aligned */
1135 i = b->rawsize;
1136 /* Attempt to speed this up by checking an (aligned) int at a time */
1137 if (i >= 4) {
1138 i -= 4;
1139 do {
1140 if (*(MEMENTO_UINT32 *)p != MEMENTO_FREEFILL_UINT32)
1141 goto mismatch4;
1142 p += 4;
1143 i -= 4;
1144 } while (i > 0);
1145 i += 4;
1146 }
1147 if (i & 2) {
1148 if (*(MEMENTO_UINT16 *)p != MEMENTO_FREEFILL_UINT16)
1149 goto mismatch;
1150 p += 2;
1151 i -= 2;
1152 }
1153 if (0) {
1154 mismatch4:
1155 i += 4;
1156 }
1157 mismatch:
1158 while (i) {
1159 if (*p++ != (unsigned char)MEMENTO_FREEFILL)
1160 break;
1161 i--;
1162 }
1163 if (i) {
1164 data->freeCorrupt = 1;
1165 data->index = b->rawsize-i;
1166 }
1167 return Memento_Internal_checkAllocedBlock(b, arg);
1168 }
1169 #endif /* MEMENTO_LEAKONLY */
1170
1171 static void Memento_removeBlock(Memento_Blocks *blks,
1172 Memento_BlkHeader *b)
1173 {
1174 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(*b));
1175 if (b->next) {
1176 VALGRIND_MAKE_MEM_DEFINED(&b->next->prev, sizeof(b->next->prev));
1177 b->next->prev = b->prev;
1178 VALGRIND_MAKE_MEM_NOACCESS(&b->next->prev, sizeof(b->next->prev));
1179 }
1180 if (b->prev) {
1181 VALGRIND_MAKE_MEM_DEFINED(&b->prev->next, sizeof(b->prev->next));
1182 b->prev->next = b->next;
1183 VALGRIND_MAKE_MEM_NOACCESS(&b->prev->next, sizeof(b->prev->next));
1184 }
1185 if (blks->tail == b)
1186 blks->tail = b->prev;
1187 if (blks->head == b)
1188 blks->head = b->next;
1189 }
1190
1191 static void free_block(Memento_BlkHeader *head)
1192 {
1193 #ifdef MEMENTO_DETAILS
1194 Memento_BlkDetails *details = head->details;
1195
1196 while (details)
1197 {
1198 Memento_BlkDetails *next = details->next;
1199 MEMENTO_UNDERLYING_FREE(details);
1200 details = next;
1201 }
1202 #endif
1203 MEMENTO_UNDERLYING_FREE(head);
1204 }
1205
1206 static int Memento_Internal_makeSpace(size_t space)
1207 {
1208 /* If too big, it can never go on the freelist */
1209 if (space > MEMENTO_FREELIST_MAX_SINGLE_BLOCK)
1210 return 0;
1211 /* Pretend we added it on. */
1212 memento.freeListSize += space;
1213 /* Ditch blocks until it fits within our limit */
1214 while (memento.freeListSize > MEMENTO_FREELIST_MAX) {
1215 Memento_BlkHeader *head = memento.free.head;
1216 VALGRIND_MAKE_MEM_DEFINED(head, sizeof(*head));
1217 memento.free.head = head->next;
1218 memento.freeListSize -= MEMBLK_SIZE(head->rawsize);
1219 free_block(head);
1220 }
1221 /* Make sure we haven't just completely emptied the free list */
1222 /* (This should never happen, but belt and braces... */
1223 if (memento.free.head == NULL)
1224 memento.free.tail = NULL;
1225 return 1;
1226 }
1227
1228 static int Memento_appBlocks(Memento_Blocks *blks,
1229 int (*app)(Memento_BlkHeader *,
1230 void *),
1231 void *arg)
1232 {
1233 Memento_BlkHeader *head = blks->head;
1234 Memento_BlkHeader *next;
1235 int result;
1236 while (head) {
1237 VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
1238 VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head),
1239 head->rawsize + Memento_PostSize);
1240 result = app(head, arg);
1241 next = head->next;
1242 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(head), Memento_PostSize);
1243 VALGRIND_MAKE_MEM_NOACCESS(head, sizeof(Memento_BlkHeader));
1244 if (result)
1245 return result;
1246 head = next;
1247 }
1248 return 0;
1249 }
1250
1251 #ifndef MEMENTO_LEAKONLY
1252 /* Distrustful - check the block is a real one */
1253 static int Memento_appBlockUser(Memento_Blocks *blks,
1254 int (*app)(Memento_BlkHeader *,
1255 void *),
1256 void *arg,
1257 Memento_BlkHeader *b)
1258 {
1259 Memento_BlkHeader *head = blks->head;
1260 Memento_BlkHeader *next;
1261 int result;
1262 while (head && head != b) {
1263 VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
1264 next = head->next;
1265 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(head), Memento_PostSize);
1266 head = next;
1267 }
1268 if (head == b) {
1269 VALGRIND_MAKE_MEM_DEFINED(head, sizeof(Memento_BlkHeader));
1270 VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(head),
1271 head->rawsize + Memento_PostSize);
1272 result = app(head, arg);
1273 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(head), Memento_PostSize);
1274 VALGRIND_MAKE_MEM_NOACCESS(head, sizeof(Memento_BlkHeader));
1275 return result;
1276 }
1277 return 0;
1278 }
1279
1280 static int Memento_appBlock(Memento_Blocks *blks,
1281 int (*app)(Memento_BlkHeader *,
1282 void *),
1283 void *arg,
1284 Memento_BlkHeader *b)
1285 {
1286 int result;
1287 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(Memento_BlkHeader));
1288 VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(b),
1289 b->rawsize + Memento_PostSize);
1290 result = app(b, arg);
1291 VALGRIND_MAKE_MEM_NOACCESS(MEMBLK_POSTPTR(b), Memento_PostSize);
1292 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1293 return result;
1294 }
1295 #endif /* MEMENTO_LEAKONLY */
1296
1297 static int showBlock(Memento_BlkHeader *b, int space)
1298 {
1299 int seq;
1300 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(Memento_BlkHeader));
1301 fprintf(stderr, FMTP":(size=" FMTZ ",num=%d)",
1302 MEMBLK_TOBLK(b), (FMTZ_CAST)b->rawsize, b->sequence);
1303 if (b->label)
1304 fprintf(stderr, "%c(%s)", space, b->label);
1305 if (b->flags & Memento_Flag_KnownLeak)
1306 fprintf(stderr, "(Known Leak)");
1307 seq = b->sequence;
1308 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1309 return seq;
1310 }
1311
1312 static void blockDisplay(Memento_BlkHeader *b, int n)
1313 {
1314 n++;
1315 while (n > 40)
1316 {
1317 fprintf(stderr, "*");
1318 n -= 40;
1319 }
1320 while(n > 0)
1321 {
1322 int i = n;
1323 if (i > 32)
1324 i = 32;
1325 n -= i;
1326 fprintf(stderr, "%s", &" "[32-i]);
1327 }
1328 showBlock(b, '\t');
1329 fprintf(stderr, "\n");
1330 }
1331
1332 static int Memento_listBlock(Memento_BlkHeader *b,
1333 void *arg)
1334 {
1335 size_t *counts = (size_t *)arg;
1336 blockDisplay(b, 0);
1337 counts[0]++;
1338 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(Memento_BlkHeader));
1339 counts[1]+= b->rawsize;
1340 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1341 return 0;
1342 }
1343
1344 static void doNestedDisplay(Memento_BlkHeader *b,
1345 int depth)
1346 {
1347 /* Try and avoid recursion if we can help it */
1348 do {
1349 Memento_BlkHeader *c = NULL;
1350 blockDisplay(b, depth);
1351 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(Memento_BlkHeader));
1352 if (b->sibling) {
1353 c = b->child;
1354 b = b->sibling;
1355 } else {
1356 b = b->child;
1357 depth++;
1358 }
1359 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(Memento_BlkHeader));
1360 if (c)
1361 doNestedDisplay(c, depth+1);
1362 } while (b);
1363 }
1364
1365 static int ptrcmp(const void *a_, const void *b_)
1366 {
1367 const char **a = (const char **)a_;
1368 const char **b = (const char **)b_;
1369 return (int)(*a-*b);
1370 }
1371
1372 static
1373 int Memento_listBlocksNested(void)
1374 {
1375 int count, i;
1376 size_t size;
1377 Memento_BlkHeader *b, *prev;
1378 void **blocks, *minptr, *maxptr;
1379 intptr_t mask;
1380
1381 /* Count the blocks */
1382 count = 0;
1383 size = 0;
1384 for (b = memento.used.head; b; b = b->next) {
1385 VALGRIND_MAKE_MEM_DEFINED(b, sizeof(*b));
1386 size += b->rawsize;
1387 count++;
1388 }
1389
1390 /* Make our block list */
1391 blocks = MEMENTO_UNDERLYING_MALLOC(sizeof(void *) * count);
1392 if (blocks == NULL)
1393 return 1;
1394
1395 /* Populate our block list */
1396 b = memento.used.head;
1397 minptr = maxptr = MEMBLK_TOBLK(b);
1398 mask = (intptr_t)minptr;
1399 for (i = 0; b; b = b->next, i++) {
1400 void *p = MEMBLK_TOBLK(b);
1401 mask &= (intptr_t)p;
1402 if (p < minptr)
1403 minptr = p;
1404 if (p > maxptr)
1405 maxptr = p;
1406 blocks[i] = p;
1407 b->flags &= ~Memento_Flag_HasParent;
1408 b->child = NULL;
1409 b->sibling = NULL;
1410 b->prev = NULL; /* parent */
1411 }
1412 qsort(blocks, count, sizeof(void *), ptrcmp);
1413
1414 /* Now, calculate tree */
1415 for (b = memento.used.head; b; b = b->next) {
1416 char *p = MEMBLK_TOBLK(b);
1417 int end = (b->rawsize < MEMENTO_PTRSEARCH ? b->rawsize : MEMENTO_PTRSEARCH);
1418 for (i = MEMENTO_SEARCH_SKIP; i < end; i += sizeof(void *)) {
1419 void *q = *(void **)(&p[i]);
1420 void **r;
1421
1422 /* Do trivial checks on pointer */
1423 if ((mask & (intptr_t)q) != mask || q < minptr || q > maxptr)
1424 continue;
1425
1426 /* Search for pointer */
1427 r = bsearch(&q, blocks, count, sizeof(void *), ptrcmp);
1428 if (r) {
1429 /* Found child */
1430 Memento_BlkHeader *child = MEMBLK_FROMBLK(*r);
1431 Memento_BlkHeader *parent;
1432
1433 /* We're assuming tree structure, not graph - ignore second
1434 * and subsequent pointers. */
1435 if (child->prev != NULL) /* parent */
1436 continue;
1437 if (child->flags & Memento_Flag_HasParent)
1438 continue;
1439
1440 /* Not interested in pointers to ourself! */
1441 if (child == b)
1442 continue;
1443
1444 /* We're also assuming acyclicness here. If this is one of
1445 * our parents, ignore it. */
1446 parent = b->prev; /* parent */
1447 while (parent != NULL && parent != child)
1448 parent = parent->prev; /* parent */
1449 if (parent == child)
1450 continue;
1451
1452 child->sibling = b->child;
1453 b->child = child;
1454 child->prev = b; /* parent */
1455 child->flags |= Memento_Flag_HasParent;
1456 }
1457 }
1458 }
1459
1460 /* Now display with nesting */
1461 for (b = memento.used.head; b; b = b->next) {
1462 if ((b->flags & Memento_Flag_HasParent) == 0)
1463 doNestedDisplay(b, 0);
1464 }
1465 fprintf(stderr, " Total number of blocks = %d\n", count);
1466 fprintf(stderr, " Total size of blocks = "FMTZ"\n", (FMTZ_CAST)size);
1467
1468 MEMENTO_UNDERLYING_FREE(blocks);
1469
1470 /* Now put the blocks back for valgrind, and restore the prev
1471 * and magic values. */
1472 prev = NULL;
1473 for (b = memento.used.head; b;) {
1474 Memento_BlkHeader *next = b->next;
1475 b->prev = prev;
1476 b->child = MEMENTO_CHILD_MAGIC;
1477 b->sibling = MEMENTO_SIBLING_MAGIC;
1478 prev = b;
1479 VALGRIND_MAKE_MEM_NOACCESS(b, sizeof(*b));
1480 b = next;
1481 }
1482
1483 return 0;
1484 }
1485
1486 void Memento_listBlocks(void)
1487 {
1488 MEMENTO_LOCK();
1489 fprintf(stderr, "Allocated blocks:\n");
1490 if (Memento_listBlocksNested())
1491 {
1492 size_t counts[2];
1493 counts[0] = 0;
1494 counts[1] = 0;
1495 Memento_appBlocks(&memento.used, Memento_listBlock, &counts[0]);
1496 fprintf(stderr, " Total number of blocks = "FMTZ"\n", (FMTZ_CAST)counts[0]);
1497 fprintf(stderr, " Total size of blocks = "FMTZ"\n", (FMTZ_CAST)counts[1]);
1498 }
1499 MEMENTO_UNLOCK();
1500 }
1501
1502 static int Memento_listNewBlock(Memento_BlkHeader *b,
1503 void *arg)
1504 {
1505 if (b->flags & Memento_Flag_OldBlock)
1506 return 0;
1507 b->flags |= Memento_Flag_OldBlock;
1508 return Memento_listBlock(b, arg);
1509 }
1510
1511 void Memento_listNewBlocks(void)
1512 {
1513 size_t counts[2];
1514 MEMENTO_LOCK();
1515 counts[0] = 0;
1516 counts[1] = 0;
1517 fprintf(stderr, "Blocks allocated and still extant since last list:\n");
1518 Memento_appBlocks(&memento.used, Memento_listNewBlock, &counts[0]);
1519 fprintf(stderr, " Total number of blocks = "FMTZ"\n", (FMTZ_CAST)counts[0]);
1520 fprintf(stderr, " Total size of blocks = "FMTZ"\n", (FMTZ_CAST)counts[1]);
1521 MEMENTO_UNLOCK();
1522 }
1523
1524 static void Memento_endStats(void)
1525 {
1526 fprintf(stderr, "Total memory malloced = "FMTZ" bytes\n", (FMTZ_CAST)memento.totalAlloc);
1527 fprintf(stderr, "Peak memory malloced = "FMTZ" bytes\n", (FMTZ_CAST)memento.peakAlloc);
1528 fprintf(stderr, FMTZ" mallocs, "FMTZ" frees, "FMTZ" reallocs\n", (FMTZ_CAST)memento.numMallocs,
1529 (FMTZ_CAST)memento.numFrees, (FMTZ_CAST)memento.numReallocs);
1530 fprintf(stderr, "Average allocation size "FMTZ" bytes\n", (FMTZ_CAST)
1531 (memento.numMallocs != 0 ? memento.totalAlloc/memento.numMallocs: 0));
1532 }
1533
1534 void Memento_stats(void)
1535 {
1536 MEMENTO_LOCK();
1537 fprintf(stderr, "Current memory malloced = "FMTZ" bytes\n", (FMTZ_CAST)memento.alloc);
1538 Memento_endStats();
1539 MEMENTO_UNLOCK();
1540 }
1541
1542 #ifdef MEMENTO_DETAILS
1543 static int showInfo(Memento_BlkHeader *b, void *arg)
1544 {
1545 Memento_BlkDetails *details;
1546
1547 fprintf(stderr, FMTP":(size="FMTZ",num=%d)",
1548 MEMBLK_TOBLK(b), (FMTZ_CAST)b->rawsize, b->sequence);
1549 if (b->label)
1550 fprintf(stderr, " (%s)", b->label);
1551 fprintf(stderr, "\nEvents:\n");
1552
1553 details = b->details;
1554 while (details)
1555 {
1556 fprintf(stderr, " Event %d (%s)\n", details->sequence, eventType[(int)details->type]);
1557 Memento_showStacktrace(details->stack, details->count);
1558 details = details->next;
1559 }
1560 return 0;
1561 }
1562 #endif
1563
1564 void Memento_listBlockInfo(void)
1565 {
1566 #ifdef MEMENTO_DETAILS
1567 MEMENTO_LOCK();
1568 fprintf(stderr, "Details of allocated blocks:\n");
1569 Memento_appBlocks(&memento.used, showInfo, NULL);
1570 MEMENTO_UNLOCK();
1571 #endif
1572 }
1573
1574 static int Memento_nonLeakBlocksLeaked(void)
1575 {
1576 Memento_BlkHeader *blk = memento.used.head;
1577 while (blk)
1578 {
1579 Memento_BlkHeader *next;
1580 int leaked;
1581 VALGRIND_MAKE_MEM_DEFINED(blk, sizeof(*blk));
1582 leaked = ((blk->flags & Memento_Flag_KnownLeak) == 0);
1583 next = blk->next;
1584 VALGRIND_MAKE_MEM_DEFINED(blk, sizeof(*blk));
1585 if (leaked)
1586 return 1;
1587 blk = next;
1588 }
1589 return 0;
1590 }
1591
1592 void Memento_fin(void)
1593 {
1594 Memento_checkAllMemory();
1595 if (!memento.segv)
1596 {
1597 Memento_endStats();
1598 if (Memento_nonLeakBlocksLeaked()) {
1599 Memento_listBlocks();
1600 #ifdef MEMENTO_DETAILS
1601 fprintf(stderr, "\n");
1602 Memento_listBlockInfo();
1603 #endif
1604 Memento_breakpoint();
1605 }
1606 }
1607 if (memento.squeezing) {
1608 if (memento.pattern == 0)
1609 fprintf(stderr, "Memory squeezing @ %d complete%s\n", memento.squeezeAt, memento.segv ? " (with SEGV)" : "");
1610 else
1611 fprintf(stderr, "Memory squeezing @ %d (%d) complete%s\n", memento.squeezeAt, memento.pattern, memento.segv ? " (with SEGV)" : "");
1612 } else if (memento.segv) {
1613 fprintf(stderr, "Memento completed (with SEGV)\n");
1614 }
1615 if (memento.failing)
1616 {
1617 fprintf(stderr, "MEMENTO_FAILAT=%d\n", memento.failAt);
1618 fprintf(stderr, "MEMENTO_PATTERN=%d\n", memento.pattern);
1619 }
1620 if (memento.nextFailAt != 0)
1621 {
1622 fprintf(stderr, "MEMENTO_NEXTFAILAT=%d\n", memento.nextFailAt);
1623 fprintf(stderr, "MEMENTO_NEXTPATTERN=%d\n", memento.nextPattern);
1624 }
1625 }
1626
1627 static void Memento_init(void)
1628 {
1629 char *env;
1630 memset(&memento, 0, sizeof(memento));
1631 memento.inited = 1;
1632 memento.used.head = NULL;
1633 memento.used.tail = NULL;
1634 memento.free.head = NULL;
1635 memento.free.tail = NULL;
1636 memento.sequence = 0;
1637 memento.countdown = 1024;
1638
1639 env = getenv("MEMENTO_FAILAT");
1640 memento.failAt = (env ? atoi(env) : 0);
1641
1642 env = getenv("MEMENTO_BREAKAT");
1643 memento.breakAt = (env ? atoi(env) : 0);
1644
1645 env = getenv("MEMENTO_PARANOIA");
1646 memento.paranoia = (env ? atoi(env) : 0);
1647 if (memento.paranoia == 0)
1648 memento.paranoia = -1024;
1649
1650 env = getenv("MEMENTO_PARANOIDAT");
1651 memento.paranoidAt = (env ? atoi(env) : 0);
1652
1653 env = getenv("MEMENTO_SQUEEZEAT");
1654 memento.squeezeAt = (env ? atoi(env) : 0);
1655
1656 env = getenv("MEMENTO_PATTERN");
1657 memento.pattern = (env ? atoi(env) : 0);
1658
1659 env = getenv("MEMENTO_MAXMEMORY");
1660 memento.maxMemory = (env ? atoi(env) : 0);
1661
1662 atexit(Memento_fin);
1663
1664 Memento_initMutex(&memento.mutex);
1665
1666 Memento_initStacktracer();
1667
1668 Memento_breakpoint();
1669 }
1670
1671 typedef struct findBlkData {
1672 void *addr;
1673 Memento_BlkHeader *blk;
1674 int flags;
1675 } findBlkData;
1676
1677 static int Memento_containsAddr(Memento_BlkHeader *b,
1678 void *arg)
1679 {
1680 findBlkData *data = (findBlkData *)arg;
1681 char *blkend = &((char *)MEMBLK_TOBLK(b))[b->rawsize];
1682 if ((MEMBLK_TOBLK(b) <= data->addr) &&
1683 ((void *)blkend > data->addr)) {
1684 data->blk = b;
1685 data->flags = 1;
1686 return 1;
1687 }
1688 if (((void *)b <= data->addr) &&
1689 (MEMBLK_TOBLK(b) > data->addr)) {
1690 data->blk = b;
1691 data->flags = 2;
1692 return 1;
1693 }
1694 if (((void *)blkend <= data->addr) &&
1695 ((void *)(blkend + Memento_PostSize) > data->addr)) {
1696 data->blk = b;
1697 data->flags = 3;
1698 return 1;
1699 }
1700 return 0;
1701 }
1702
1703 void Memento_info(void *addr)
1704 {
1705 #ifdef MEMENTO_DETAILS
1706 findBlkData data;
1707
1708 MEMENTO_LOCK();
1709 data.addr = addr;
1710 data.blk = NULL;
1711 data.flags = 0;
1712 Memento_appBlocks(&memento.used, Memento_containsAddr, &data);
1713 if (data.blk != NULL)
1714 showInfo(data.blk, NULL);
1715 data.blk = NULL;
1716 data.flags = 0;
1717 Memento_appBlocks(&memento.free, Memento_containsAddr, &data);
1718 if (data.blk != NULL)
1719 showInfo(data.blk, NULL);
1720 MEMENTO_UNLOCK();
1721 #else
1722 printf("Memento not compiled with details support\n");
1723 #endif
1724 }
1725
1726 #ifdef MEMENTO_HAS_FORK
1727 #include <unistd.h>
1728 #include <sys/wait.h>
1729 #include <time.h>
1730 #ifdef MEMENTO_STACKTRACE_METHOD
1731 #if MEMENTO_STACKTRACE_METHOD == 1
1732 #include <signal.h>
1733 #endif
1734 #endif
1735
1736 /* FIXME: Find some portable way of getting this */
1737 /* MacOSX has 10240, Ubuntu seems to have 256 */
1738 #ifndef OPEN_MAX
1739 #define OPEN_MAX 10240
1740 #endif
1741
1742 /* stashed_map[j] = i means that file descriptor i-1 was duplicated to j */
1743 int stashed_map[OPEN_MAX];
1744
1745 static void Memento_signal(int sig)
1746 {
1747 (void)sig;
1748 fprintf(stderr, "SEGV at:\n");
1749 memento.segv = 1;
1750 Memento_bt_internal(0);
1751
1752 exit(1);
1753 }
1754
1755 static int squeeze(void)
1756 {
1757 pid_t pid;
1758 int i, status;
1759
1760 if (memento.patternBit < 0)
1761 return 1;
1762 if (memento.squeezing && memento.patternBit >= MEMENTO_MAXPATTERN)
1763 return 1;
1764
1765 if (memento.patternBit == 0)
1766 memento.squeezeAt = memento.sequence;
1767
1768 if (!memento.squeezing) {
1769 fprintf(stderr, "Memory squeezing @ %d\n", memento.squeezeAt);
1770 } else
1771 fprintf(stderr, "Memory squeezing @ %d (%x,%x)\n", memento.squeezeAt, memento.pattern, memento.patternBit);
1772
1773 /* When we fork below, the child is going to snaffle all our file pointers
1774 * and potentially corrupt them. Let's make copies of all of them before
1775 * we fork, so we can restore them when we restart. */
1776 for (i = 0; i < OPEN_MAX; i++) {
1777 if (stashed_map[i] == 0) {
1778 int j = dup(i);
1779 stashed_map[j] = i+1;
1780 }
1781 }
1782
1783 fprintf(stderr, "Failing at:\n");
1784 Memento_bt_internal(2);
1785 pid = fork();
1786 if (pid == 0) {
1787 /* Child */
1788 signal(SIGSEGV, Memento_signal);
1789 /* In the child, we always fail the next allocation. */
1790 if (memento.patternBit == 0) {
1791 memento.patternBit = 1;
1792 } else
1793 memento.patternBit <<= 1;
1794 memento.squeezing = 1;
1795 return 1;
1796 }
1797
1798 /* In the parent if we hit another allocation, pass it (and record the
1799 * fact we passed it in the pattern. */
1800 memento.pattern |= memento.patternBit;
1801 memento.patternBit <<= 1;
1802
1803 /* Wait for pid to finish, with a timeout. */
1804 {
1805 struct timespec tm = { 0, 10 * 1000 * 1000 }; /* 10ms = 100th sec */
1806 int timeout = 30 * 1000 * 1000; /* time out in microseconds! */
1807 while (waitpid(pid, &status, WNOHANG) == 0) {
1808 nanosleep(&tm, NULL);
1809 timeout -= (tm.tv_nsec/1000);
1810 tm.tv_nsec *= 2;
1811 if (tm.tv_nsec > 999999999)
1812 tm.tv_nsec = 999999999;
1813 if (timeout <= 0) {
1814 char text[32];
1815 fprintf(stderr, "Child is taking a long time to die. Killing it.\n");
1816 sprintf(text, "kill %d", pid);
1817 system(text);
1818 break;
1819 }
1820 }
1821 }
1822
1823 if (status != 0) {
1824 fprintf(stderr, "Child status=%d\n", status);
1825 }
1826
1827 /* Put the files back */
1828 for (i = 0; i < OPEN_MAX; i++) {
1829 if (stashed_map[i] != 0) {
1830 dup2(i, stashed_map[i]-1);
1831 close(i);
1832 stashed_map[i] = 0;
1833 }
1834 }
1835
1836 return 0;
1837 }
1838 #else
1839 #include <signal.h>
1840
1841 static void Memento_signal(int sig)
1842 {
1843 (void)sig;
1844 memento.segv = 1;
1845 /* If we just return from this function the SEGV will be unhandled, and
1846 * we'll launch into whatever JIT debugging system the OS provides. At
1847 * least fprintf(stderr, something useful first. If MEMENTO_NOJIT is set, then
1848 * just exit to avoid the JIT (and get the usual atexit handling). */
1849 if (getenv("MEMENTO_NOJIT"))
1850 exit(1);
1851 else
1852 Memento_fin();
1853 }
1854
1855 static int squeeze(void)
1856 {
1857 fprintf(stderr, "Memento memory squeezing disabled as no fork!\n");
1858 return 0;
1859 }
1860 #endif
1861
1862 static void Memento_startFailing(void)
1863 {
1864 if (!memento.failing) {
1865 fprintf(stderr, "Starting to fail...\n");
1866 Memento_bt();
1867 fflush(stderr);
1868 memento.failing = 1;
1869 memento.failAt = memento.sequence;
1870 memento.nextFailAt = memento.sequence+1;
1871 memento.pattern = 0;
1872 memento.patternBit = 0;
1873 signal(SIGSEGV, Memento_signal);
1874 signal(SIGABRT, Memento_signal);
1875 Memento_breakpointLocked();
1876 }
1877 }
1878
1879 static int Memento_event(void)
1880 {
1881 memento.sequence++;
1882 if ((memento.sequence >= memento.paranoidAt) && (memento.paranoidAt != 0)) {
1883 memento.paranoia = 1;
1884 memento.countdown = 1;
1885 }
1886 if (--memento.countdown == 0) {
1887 Memento_checkAllMemoryLocked();
1888 if (memento.paranoia > 0)
1889 memento.countdown = memento.paranoia;
1890 else
1891 {
1892 memento.countdown = -memento.paranoia;
1893 if (memento.paranoia > INT_MIN/2)
1894 memento.paranoia *= 2;
1895 }
1896 }
1897
1898 if (memento.sequence == memento.breakAt) {
1899 fprintf(stderr, "Breaking at event %d\n", memento.breakAt);
1900 return 1;
1901 }
1902 return 0;
1903 }
1904
1905 int Memento_sequence(void)
1906 {
1907 return memento.sequence;
1908 }
1909
1910 int Memento_breakAt(int event)
1911 {
1912 MEMENTO_LOCK();
1913 memento.breakAt = event;
1914 MEMENTO_UNLOCK();
1915 return event;
1916 }
1917
1918 static void *safe_find_block(void *ptr)
1919 {
1920 Memento_BlkHeader *block;
1921 int valid;
1922
1923 if (ptr == NULL)
1924 return NULL;
1925
1926 block = MEMBLK_FROMBLK(ptr);
1927 /* Sometimes wrapping allocators can mean Memento_label
1928 * is called with a value within the block, rather than
1929 * at the start of the block. If we detect this, find it
1930 * the slow way. */
1931 VALGRIND_MAKE_MEM_DEFINED(&block->child, sizeof(block->child));
1932 VALGRIND_MAKE_MEM_DEFINED(&block->sibling, sizeof(block->sibling));
1933 valid = (block->child == MEMENTO_CHILD_MAGIC &&
1934 block->sibling == MEMENTO_SIBLING_MAGIC);
1935 VALGRIND_MAKE_MEM_NOACCESS(&block->child, sizeof(block->child));
1936 VALGRIND_MAKE_MEM_NOACCESS(&block->sibling, sizeof(block->sibling));
1937 if (!valid)
1938 {
1939 findBlkData data;
1940
1941 data.addr = ptr;
1942 data.blk = NULL;
1943 data.flags = 0;
1944 Memento_appBlocks(&memento.used, Memento_containsAddr, &data);
1945 if (data.blk == NULL)
1946 return NULL;
1947 block = data.blk;
1948 }
1949 return block;
1950 }
1951
1952 void *Memento_label(void *ptr, const char *label)
1953 {
1954 Memento_BlkHeader *block;
1955
1956 if (ptr == NULL)
1957 return NULL;
1958 MEMENTO_LOCK();
1959 block = safe_find_block(ptr);
1960 if (block != NULL)
1961 {
1962 VALGRIND_MAKE_MEM_DEFINED(&block->label, sizeof(block->label));
1963 block->label = label;
1964 VALGRIND_MAKE_MEM_NOACCESS(&block->label, sizeof(block->label));
1965 }
1966 MEMENTO_UNLOCK();
1967 return ptr;
1968 }
1969
1970 void Memento_tick(void)
1971 {
1972 MEMENTO_LOCK();
1973 if (Memento_event()) Memento_breakpointLocked();
1974 MEMENTO_UNLOCK();
1975 }
1976
1977 static int Memento_failThisEventLocked(void)
1978 {
1979 int failThisOne;
1980
1981 if (Memento_event()) Memento_breakpointLocked();
1982
1983 if ((memento.sequence >= memento.failAt) && (memento.failAt != 0))
1984 Memento_startFailing();
1985 if ((memento.sequence >= memento.squeezeAt) && (memento.squeezeAt != 0)) {
1986 return squeeze();
1987 }
1988
1989 if (!memento.failing)
1990 return 0;
1991 failThisOne = ((memento.patternBit & memento.pattern) == 0);
1992 /* If we are failing, and we've reached the end of the pattern and we've
1993 * still got bits available in the pattern word, and we haven't already
1994 * set a nextPattern, then extend the pattern. */
1995 if (memento.failing &&
1996 ((~(memento.patternBit-1) & memento.pattern) == 0) &&
1997 (memento.patternBit != 0) &&
1998 memento.nextPattern == 0)
1999 {
2000 /* We'll fail this one, and set the 'next' one to pass it. */
2001 memento.nextFailAt = memento.failAt;
2002 memento.nextPattern = memento.pattern | memento.patternBit;
2003 }
2004 memento.patternBit = (memento.patternBit ? memento.patternBit << 1 : 1);
2005
2006 return failThisOne;
2007 }
2008
2009 int Memento_failThisEvent(void)
2010 {
2011 int ret;
2012
2013 if (!memento.inited)
2014 Memento_init();
2015
2016 MEMENTO_LOCK();
2017 ret = Memento_failThisEventLocked();
2018 MEMENTO_UNLOCK();
2019 return ret;
2020 }
2021
2022 static void *do_malloc(size_t s, int eventType)
2023 {
2024 Memento_BlkHeader *memblk;
2025 size_t smem = MEMBLK_SIZE(s);
2026
2027 if (Memento_failThisEventLocked())
2028 return NULL;
2029
2030 if (s == 0)
2031 return NULL;
2032
2033 memento.numMallocs++;
2034
2035 if (memento.maxMemory != 0 && memento.alloc + s > memento.maxMemory)
2036 return NULL;
2037
2038 memblk = MEMENTO_UNDERLYING_MALLOC(smem);
2039 if (memblk == NULL)
2040 return NULL;
2041
2042 memento.alloc += s;
2043 memento.totalAlloc += s;
2044 if (memento.peakAlloc < memento.alloc)
2045 memento.peakAlloc = memento.alloc;
2046 #ifndef MEMENTO_LEAKONLY
2047 memset(MEMBLK_TOBLK(memblk), MEMENTO_ALLOCFILL, s);
2048 #endif
2049 memblk->rawsize = s;
2050 memblk->sequence = memento.sequence;
2051 memblk->lastCheckedOK = memblk->sequence;
2052 memblk->flags = 0;
2053 memblk->label = 0;
2054 memblk->child = MEMENTO_CHILD_MAGIC;
2055 memblk->sibling = MEMENTO_SIBLING_MAGIC;
2056 #ifdef MEMENTO_DETAILS
2057 memblk->details = NULL;
2058 memblk->details_tail = &memblk->details;
2059 Memento_storeDetails(memblk, Memento_EventType_malloc);
2060 #endif /* MEMENTO_DETAILS */
2061 Memento_addBlockHead(&memento.used, memblk, 0);
2062
2063 if (memento.leaking > 0)
2064 memblk->flags |= Memento_Flag_KnownLeak;
2065
2066 return MEMBLK_TOBLK(memblk);
2067 }
2068
2069 void *Memento_malloc(size_t s)
2070 {
2071 void *ret;
2072
2073 if (!memento.inited)
2074 Memento_init();
2075
2076 MEMENTO_LOCK();
2077 ret = do_malloc(s, Memento_EventType_malloc);
2078 MEMENTO_UNLOCK();
2079 return ret;
2080 }
2081
2082 void *Memento_calloc(size_t n, size_t s)
2083 {
2084 void *block;
2085
2086 if (!memento.inited)
2087 Memento_init();
2088
2089 MEMENTO_LOCK();
2090 block = do_malloc(n*s, Memento_EventType_calloc);
2091 if (block)
2092 memset(block, 0, n*s);
2093 MEMENTO_UNLOCK();
2094 return block;
2095 }
2096
2097 static void do_reference(Memento_BlkHeader *blk, int event)
2098 {
2099 #ifdef MEMENTO_DETAILS
2100 Memento_storeDetails(blk, event);
2101 #endif /* MEMENTO_DETAILS */
2102 }
2103
2104 int Memento_checkPointerOrNull(void *blk)
2105 {
2106 if (blk == NULL)
2107 return 0;
2108 if (blk == MEMENTO_PREFILL_PTR)
2109 fprintf(stderr, "Prefill value found as pointer - buffer underrun?\n");
2110 else if (blk == MEMENTO_POSTFILL_PTR)
2111 fprintf(stderr, "Postfill value found as pointer - buffer overrun?\n");
2112 else if (blk == MEMENTO_ALLOCFILL_PTR)
2113 fprintf(stderr, "Allocfill value found as pointer - use of uninitialised value?\n");
2114 else if (blk == MEMENTO_FREEFILL_PTR)
2115 fprintf(stderr, "Allocfill value found as pointer - use after free?\n");
2116 else
2117 return 0;
2118 #ifdef MEMENTO_DETAILS
2119 fprintf(stderr, "Current backtrace:\n");
2120 Memento_bt();
2121 fprintf(stderr, "History:\n");
2122 Memento_info(blk);
2123 #endif
2124 return 1;
2125 }
2126
2127 int Memento_checkBytePointerOrNull(void *blk)
2128 {
2129 unsigned char i;
2130 if (blk == NULL)
2131 return 0;
2132 Memento_checkPointerOrNull(blk);
2133
2134 i = *(unsigned int *)blk;
2135
2136 if (i == MEMENTO_PREFILL_UBYTE)
2137 fprintf(stderr, "Prefill value found - buffer underrun?\n");
2138 else if (i == MEMENTO_POSTFILL_UBYTE)
2139 fprintf(stderr, "Postfill value found - buffer overrun?\n");
2140 else if (i == MEMENTO_ALLOCFILL_UBYTE)
2141 fprintf(stderr, "Allocfill value found - use of uninitialised value?\n");
2142 else if (i == MEMENTO_FREEFILL_UBYTE)
2143 fprintf(stderr, "Allocfill value found - use after free?\n");
2144 else
2145 return 0;
2146 #ifdef MEMENTO_DETAILS
2147 fprintf(stderr, "Current backtrace:\n");
2148 Memento_bt();
2149 fprintf(stderr, "History:\n");
2150 Memento_info(blk);
2151 #endif
2152 Memento_breakpoint();
2153 return 1;
2154 }
2155
2156 int Memento_checkShortPointerOrNull(void *blk)
2157 {
2158 unsigned short i;
2159 if (blk == NULL)
2160 return 0;
2161 Memento_checkPointerOrNull(blk);
2162
2163 i = *(unsigned short *)blk;
2164
2165 if (i == MEMENTO_PREFILL_USHORT)
2166 fprintf(stderr, "Prefill value found - buffer underrun?\n");
2167 else if (i == MEMENTO_POSTFILL_USHORT)
2168 fprintf(stderr, "Postfill value found - buffer overrun?\n");
2169 else if (i == MEMENTO_ALLOCFILL_USHORT)
2170 fprintf(stderr, "Allocfill value found - use of uninitialised value?\n");
2171 else if (i == MEMENTO_FREEFILL_USHORT)
2172 fprintf(stderr, "Allocfill value found - use after free?\n");
2173 else
2174 return 0;
2175 #ifdef MEMENTO_DETAILS
2176 fprintf(stderr, "Current backtrace:\n");
2177 Memento_bt();
2178 fprintf(stderr, "History:\n");
2179 Memento_info(blk);
2180 #endif
2181 Memento_breakpoint();
2182 return 1;
2183 }
2184
2185 int Memento_checkIntPointerOrNull(void *blk)
2186 {
2187 unsigned int i;
2188 if (blk == NULL)
2189 return 0;
2190 Memento_checkPointerOrNull(blk);
2191
2192 i = *(unsigned int *)blk;
2193
2194 if (i == MEMENTO_PREFILL_UINT)
2195 fprintf(stderr, "Prefill value found - buffer underrun?\n");
2196 else if (i == MEMENTO_POSTFILL_UINT)
2197 fprintf(stderr, "Postfill value found - buffer overrun?\n");
2198 else if (i == MEMENTO_ALLOCFILL_UINT)
2199 fprintf(stderr, "Allocfill value found - use of uninitialised value?\n");
2200 else if (i == MEMENTO_FREEFILL_UINT)
2201 fprintf(stderr, "Allocfill value found - use after free?\n");
2202 else
2203 return 0;
2204 #ifdef MEMENTO_DETAILS
2205 fprintf(stderr, "Current backtrace:\n");
2206 Memento_bt();
2207 fprintf(stderr, "History:\n");
2208 Memento_info(blk);
2209 #endif
2210 Memento_breakpoint();
2211 return 1;
2212 }
2213
2214 static void *do_takeRef(void *blk)
2215 {
2216 MEMENTO_LOCK();
2217 do_reference(safe_find_block(blk), Memento_EventType_takeRef);
2218 MEMENTO_UNLOCK();
2219 return blk;
2220 }
2221
2222 void *Memento_takeByteRef(void *blk)
2223 {
2224 if (!memento.inited)
2225 Memento_init();
2226
2227 if (Memento_event()) Memento_breakpoint();
2228
2229 if (!blk)
2230 return NULL;
2231
2232 (void)Memento_checkBytePointerOrNull(blk);
2233
2234 return do_takeRef(blk);
2235 }
2236
2237 void *Memento_takeShortRef(void *blk)
2238 {
2239 if (!memento.inited)
2240 Memento_init();
2241
2242 if (Memento_event()) Memento_breakpoint();
2243
2244 if (!blk)
2245 return NULL;
2246
2247 (void)Memento_checkShortPointerOrNull(blk);
2248
2249 return do_takeRef(blk);
2250 }
2251
2252 void *Memento_takeIntRef(void *blk)
2253 {
2254 if (!memento.inited)
2255 Memento_init();
2256
2257 if (Memento_event()) Memento_breakpoint();
2258
2259 if (!blk)
2260 return NULL;
2261
2262 (void)Memento_checkIntPointerOrNull(blk);
2263
2264 return do_takeRef(blk);
2265 }
2266
2267 void *Memento_takeRef(void *blk)
2268 {
2269 if (!memento.inited)
2270 Memento_init();
2271
2272 if (Memento_event()) Memento_breakpoint();
2273
2274 if (!blk)
2275 return NULL;
2276
2277 return do_takeRef(blk);
2278 }
2279
2280 static void *do_dropRef(void *blk)
2281 {
2282 MEMENTO_LOCK();
2283 do_reference(safe_find_block(blk), Memento_EventType_dropRef);
2284 MEMENTO_UNLOCK();
2285 return blk;
2286 }
2287
2288 void *Memento_dropByteRef(void *blk)
2289 {
2290 if (!memento.inited)
2291 Memento_init();
2292
2293 if (Memento_event()) Memento_breakpoint();
2294
2295 if (!blk)
2296 return NULL;
2297
2298 Memento_checkBytePointerOrNull(blk);
2299
2300 return do_dropRef(blk);
2301 }
2302
2303 void *Memento_dropShortRef(void *blk)
2304 {
2305 if (!memento.inited)
2306 Memento_init();
2307
2308 if (Memento_event()) Memento_breakpoint();
2309
2310 if (!blk)
2311 return NULL;
2312
2313 Memento_checkShortPointerOrNull(blk);
2314
2315 return do_dropRef(blk);
2316 }
2317
2318 void *Memento_dropIntRef(void *blk)
2319 {
2320 if (!memento.inited)
2321 Memento_init();
2322
2323 if (Memento_event()) Memento_breakpoint();
2324
2325 if (!blk)
2326 return NULL;
2327
2328 Memento_checkIntPointerOrNull(blk);
2329
2330 return do_dropRef(blk);
2331 }
2332
2333 void *Memento_dropRef(void *blk)
2334 {
2335 if (!memento.inited)
2336 Memento_init();
2337
2338 if (Memento_event()) Memento_breakpoint();
2339
2340 if (!blk)
2341 return NULL;
2342
2343 return do_dropRef(blk);
2344 }
2345
2346 void *Memento_adjustRef(void *blk, int adjust)
2347 {
2348 if (Memento_event()) Memento_breakpoint();
2349
2350 if (blk == NULL)
2351 return NULL;
2352
2353 while (adjust > 0)
2354 {
2355 do_takeRef(blk);
2356 adjust--;
2357 }
2358 while (adjust < 0)
2359 {
2360 do_dropRef(blk);
2361 adjust++;
2362 }
2363
2364 return blk;
2365 }
2366
2367 void *Memento_reference(void *blk)
2368 {
2369 if (!blk)
2370 return NULL;
2371
2372 if (!memento.inited)
2373 Memento_init();
2374
2375 MEMENTO_LOCK();
2376 do_reference(safe_find_block(blk), Memento_EventType_reference);
2377 MEMENTO_UNLOCK();
2378 return blk;
2379 }
2380
2381 /* Treat blocks from the user with suspicion, and check them the slow
2382 * but safe way. */
2383 static int checkBlockUser(Memento_BlkHeader *memblk, const char *action)
2384 {
2385 #ifndef MEMENTO_LEAKONLY
2386 BlkCheckData data;
2387
2388 memset(&data, 0, sizeof(data));
2389 Memento_appBlockUser(&memento.used, Memento_Internal_checkAllocedBlock,
2390 &data, memblk);
2391 if (!data.found) {
2392 /* Failure! */
2393 fprintf(stderr, "Attempt to %s block ", action);
2394 showBlock(memblk, 32);
2395 fprintf(stderr, "\n");
2396 Memento_breakpointLocked();
2397 return 1;
2398 } else if (data.preCorrupt || data.postCorrupt) {
2399 fprintf(stderr, "Block ");
2400 showBlock(memblk, ' ');
2401 fprintf(stderr, " found to be corrupted on %s!\n", action);
2402 if (data.preCorrupt) {
2403 fprintf(stderr, "Preguard corrupted\n");
2404 }
2405 if (data.postCorrupt) {
2406 fprintf(stderr, "Postguard corrupted\n");
2407 }
2408 fprintf(stderr, "Block last checked OK at allocation %d. Now %d.\n",
2409 memblk->lastCheckedOK, memento.sequence);
2410 if ((memblk->flags & Memento_Flag_Reported) == 0)
2411 {
2412 memblk->flags |= Memento_Flag_Reported;
2413 Memento_breakpointLocked();
2414 }
2415 return 1;
2416 }
2417 #endif
2418 return 0;
2419 }
2420
2421 static int checkBlock(Memento_BlkHeader *memblk, const char *action)
2422 {
2423 #ifndef MEMENTO_LEAKONLY
2424 BlkCheckData data;
2425 #endif
2426
2427 if (memblk->child != MEMENTO_CHILD_MAGIC ||
2428 memblk->sibling != MEMENTO_SIBLING_MAGIC)
2429 {
2430 /* Failure! */
2431 fprintf(stderr, "Attempt to %s invalid block ", action);
2432 showBlock(memblk, 32);
2433 fprintf(stderr, "\n");
2434 Memento_breakpointLocked();
2435 return 1;
2436 }
2437
2438 #ifndef MEMENTO_LEAKONLY
2439 memset(&data, 0, sizeof(data));
2440 Memento_appBlock(&memento.used, Memento_Internal_checkAllocedBlock,
2441 &data, memblk);
2442 if (!data.found) {
2443 /* Failure! */
2444 fprintf(stderr, "Attempt to %s block ", action);
2445 showBlock(memblk, 32);
2446 fprintf(stderr, "\n");
2447 Memento_breakpointLocked();
2448 return 1;
2449 } else if (data.preCorrupt || data.postCorrupt) {
2450 fprintf(stderr, "Block ");
2451 showBlock(memblk, ' ');
2452 fprintf(stderr, " found to be corrupted on %s!\n", action);
2453 if (data.preCorrupt) {
2454 fprintf(stderr, "Preguard corrupted\n");
2455 }
2456 if (data.postCorrupt) {
2457 fprintf(stderr, "Postguard corrupted\n");
2458 }
2459 fprintf(stderr, "Block last checked OK at allocation %d. Now %d.\n",
2460 memblk->lastCheckedOK, memento.sequence);
2461 if ((memblk->flags & Memento_Flag_Reported) == 0)
2462 {
2463 memblk->flags |= Memento_Flag_Reported;
2464 Memento_breakpointLocked();
2465 }
2466 return 1;
2467 }
2468 #endif
2469 return 0;
2470 }
2471
2472 static void do_free(void *blk, int eventType)
2473 {
2474 Memento_BlkHeader *memblk;
2475
2476 if (Memento_event()) Memento_breakpointLocked();
2477
2478 if (blk == NULL)
2479 return;
2480
2481 memblk = MEMBLK_FROMBLK(blk);
2482 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2483 if (checkBlock(memblk, "free"))
2484 return;
2485
2486 #ifdef MEMENTO_DETAILS
2487 Memento_storeDetails(memblk, Memento_EventType_free);
2488 #endif
2489
2490 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2491 if (memblk->flags & Memento_Flag_BreakOnFree)
2492 Memento_breakpointLocked();
2493
2494 memento.alloc -= memblk->rawsize;
2495 memento.numFrees++;
2496
2497 Memento_removeBlock(&memento.used, memblk);
2498
2499 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2500 if (Memento_Internal_makeSpace(MEMBLK_SIZE(memblk->rawsize))) {
2501 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2502 VALGRIND_MAKE_MEM_DEFINED(MEMBLK_TOBLK(memblk),
2503 memblk->rawsize + Memento_PostSize);
2504 #ifndef MEMENTO_LEAKONLY
2505 memset(MEMBLK_TOBLK(memblk), MEMENTO_FREEFILL, memblk->rawsize);
2506 #endif
2507 memblk->flags |= Memento_Flag_Freed;
2508 Memento_addBlockTail(&memento.free, memblk, 1);
2509 } else {
2510 free_block(memblk);
2511 }
2512 }
2513
2514 void Memento_free(void *blk)
2515 {
2516 if (!memento.inited)
2517 Memento_init();
2518
2519 MEMENTO_LOCK();
2520 do_free(blk, Memento_EventType_free);
2521 MEMENTO_UNLOCK();
2522 }
2523
2524 static void *do_realloc(void *blk, size_t newsize, int type)
2525 {
2526 Memento_BlkHeader *memblk, *newmemblk;
2527 size_t newsizemem;
2528 int flags;
2529
2530 if (Memento_failThisEventLocked())
2531 return NULL;
2532
2533 memblk = MEMBLK_FROMBLK(blk);
2534 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2535 if (checkBlock(memblk, "realloc"))
2536 return NULL;
2537
2538 #ifdef MEMENTO_DETAILS
2539 Memento_storeDetails(memblk, type);
2540 #endif
2541
2542 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2543 if (memblk->flags & Memento_Flag_BreakOnRealloc)
2544 Memento_breakpointLocked();
2545
2546 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2547 if (memento.maxMemory != 0 && memento.alloc - memblk->rawsize + newsize > memento.maxMemory)
2548 return NULL;
2549
2550 newsizemem = MEMBLK_SIZE(newsize);
2551 Memento_removeBlock(&memento.used, memblk);
2552 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(*memblk));
2553 flags = memblk->flags;
2554 newmemblk = MEMENTO_UNDERLYING_REALLOC(memblk, newsizemem);
2555 if (newmemblk == NULL)
2556 {
2557 Memento_addBlockHead(&memento.used, memblk, 2);
2558 return NULL;
2559 }
2560 memento.numReallocs++;
2561 memento.totalAlloc += newsize;
2562 memento.alloc -= newmemblk->rawsize;
2563 memento.alloc += newsize;
2564 if (memento.peakAlloc < memento.alloc)
2565 memento.peakAlloc = memento.alloc;
2566 newmemblk->flags = flags;
2567 #ifndef MEMENTO_LEAKONLY
2568 if (newmemblk->rawsize < newsize) {
2569 char *newbytes = ((char *)MEMBLK_TOBLK(newmemblk))+newmemblk->rawsize;
2570 VALGRIND_MAKE_MEM_DEFINED(newbytes, newsize - newmemblk->rawsize);
2571 memset(newbytes, MEMENTO_ALLOCFILL, newsize - newmemblk->rawsize);
2572 VALGRIND_MAKE_MEM_UNDEFINED(newbytes, newsize - newmemblk->rawsize);
2573 }
2574 #endif
2575 newmemblk->rawsize = newsize;
2576 #ifndef MEMENTO_LEAKONLY
2577 VALGRIND_MAKE_MEM_DEFINED(newmemblk->preblk, Memento_PreSize);
2578 memset(newmemblk->preblk, MEMENTO_PREFILL, Memento_PreSize);
2579 VALGRIND_MAKE_MEM_UNDEFINED(newmemblk->preblk, Memento_PreSize);
2580 VALGRIND_MAKE_MEM_DEFINED(MEMBLK_POSTPTR(newmemblk), Memento_PostSize);
2581 memset(MEMBLK_POSTPTR(newmemblk), MEMENTO_POSTFILL, Memento_PostSize);
2582 VALGRIND_MAKE_MEM_UNDEFINED(MEMBLK_POSTPTR(newmemblk), Memento_PostSize);
2583 #endif
2584 Memento_addBlockHead(&memento.used, newmemblk, 2);
2585 return MEMBLK_TOBLK(newmemblk);
2586 }
2587
2588 void *Memento_realloc(void *blk, size_t newsize)
2589 {
2590 void *ret;
2591
2592 if (!memento.inited)
2593 Memento_init();
2594
2595 if (blk == NULL)
2596 {
2597 MEMENTO_LOCK();
2598 ret = do_malloc(newsize, Memento_EventType_realloc);
2599 MEMENTO_UNLOCK();
2600 return ret;
2601 }
2602 if (newsize == 0) {
2603 MEMENTO_LOCK();
2604 do_free(blk, Memento_EventType_realloc);
2605 MEMENTO_UNLOCK();
2606 return NULL;
2607 }
2608
2609 MEMENTO_LOCK();
2610 ret = do_realloc(blk, newsize, Memento_EventType_realloc);
2611 MEMENTO_UNLOCK();
2612 return ret;
2613 }
2614
2615 int Memento_checkBlock(void *blk)
2616 {
2617 Memento_BlkHeader *memblk;
2618 int ret;
2619
2620 if (blk == NULL)
2621 return 0;
2622
2623 MEMENTO_LOCK();
2624 memblk = MEMBLK_FROMBLK(blk);
2625 ret = checkBlockUser(memblk, "check");
2626 MEMENTO_UNLOCK();
2627 return ret;
2628 }
2629
2630 #ifndef MEMENTO_LEAKONLY
2631 static int Memento_Internal_checkAllAlloced(Memento_BlkHeader *memblk, void *arg)
2632 {
2633 BlkCheckData *data = (BlkCheckData *)arg;
2634
2635 Memento_Internal_checkAllocedBlock(memblk, data);
2636 if (data->preCorrupt || data->postCorrupt) {
2637 if ((data->found & 2) == 0) {
2638 fprintf(stderr, "Allocated blocks:\n");
2639 data->found |= 2;
2640 }
2641 fprintf(stderr, " Block ");
2642 showBlock(memblk, ' ');
2643 if (data->preCorrupt) {
2644 fprintf(stderr, " Preguard ");
2645 }
2646 if (data->postCorrupt) {
2647 fprintf(stderr, "%s Postguard ",
2648 (data->preCorrupt ? "&" : ""));
2649 }
2650 fprintf(stderr, "corrupted.\n "
2651 "Block last checked OK at allocation %d. Now %d.\n",
2652 memblk->lastCheckedOK, memento.sequence);
2653 data->preCorrupt = 0;
2654 data->postCorrupt = 0;
2655 data->freeCorrupt = 0;
2656 if ((memblk->flags & Memento_Flag_Reported) == 0)
2657 {
2658 memblk->flags |= Memento_Flag_Reported;
2659 Memento_breakpointLocked();
2660 }
2661 }
2662 else
2663 memblk->lastCheckedOK = memento.sequence;
2664 return 0;
2665 }
2666
2667 static int Memento_Internal_checkAllFreed(Memento_BlkHeader *memblk, void *arg)
2668 {
2669 BlkCheckData *data = (BlkCheckData *)arg;
2670
2671 Memento_Internal_checkFreedBlock(memblk, data);
2672 if (data->preCorrupt || data->postCorrupt || data->freeCorrupt) {
2673 if ((data->found & 4) == 0) {
2674 fprintf(stderr, "Freed blocks:\n");
2675 data->found |= 4;
2676 }
2677 fprintf(stderr, " ");
2678 showBlock(memblk, ' ');
2679 if (data->freeCorrupt) {
2680 fprintf(stderr, " index %d (address "FMTP") onwards", (int)data->index,
2681 &((char *)MEMBLK_TOBLK(memblk))[data->index]);
2682 if (data->preCorrupt) {
2683 fprintf(stderr, "+ preguard");
2684 }
2685 if (data->postCorrupt) {
2686 fprintf(stderr, "+ postguard");
2687 }
2688 } else {
2689 if (data->preCorrupt) {
2690 fprintf(stderr, " preguard");
2691 }
2692 if (data->postCorrupt) {
2693 fprintf(stderr, "%s Postguard",
2694 (data->preCorrupt ? "+" : ""));
2695 }
2696 }
2697 VALGRIND_MAKE_MEM_DEFINED(memblk, sizeof(Memento_BlkHeader));
2698 fprintf(stderr, " corrupted.\n"
2699 " Block last checked OK at allocation %d. Now %d.\n",
2700 memblk->lastCheckedOK, memento.sequence);
2701 if ((memblk->flags & Memento_Flag_Reported) == 0)
2702 {
2703 memblk->flags |= Memento_Flag_Reported;
2704 Memento_breakpointLocked();
2705 }
2706 VALGRIND_MAKE_MEM_NOACCESS(memblk, sizeof(Memento_BlkHeader));
2707 data->preCorrupt = 0;
2708 data->postCorrupt = 0;
2709 data->freeCorrupt = 0;
2710 }
2711 else
2712 memblk->lastCheckedOK = memento.sequence;
2713 return 0;
2714 }
2715 #endif /* MEMENTO_LEAKONLY */
2716
2717 static int Memento_checkAllMemoryLocked(void)
2718 {
2719 #ifndef MEMENTO_LEAKONLY
2720 BlkCheckData data;
2721
2722 memset(&data, 0, sizeof(data));
2723 Memento_appBlocks(&memento.used, Memento_Internal_checkAllAlloced, &data);
2724 Memento_appBlocks(&memento.free, Memento_Internal_checkAllFreed, &data);
2725 return data.found;
2726 #else
2727 return 0;
2728 #endif
2729 }
2730
2731 int Memento_checkAllMemory(void)
2732 {
2733 #ifndef MEMENTO_LEAKONLY
2734 int ret;
2735
2736 MEMENTO_LOCK();
2737 ret = Memento_checkAllMemoryLocked();
2738 MEMENTO_UNLOCK();
2739 if (ret & 6) {
2740 Memento_breakpoint();
2741 return 1;
2742 }
2743 return 0;
2744 #endif
2745 }
2746
2747 int Memento_setParanoia(int i)
2748 {
2749 memento.paranoia = i;
2750 if (memento.paranoia > 0)
2751 memento.countdown = memento.paranoia;
2752 else
2753 memento.countdown = -memento.paranoia;
2754 return i;
2755 }
2756
2757 int Memento_paranoidAt(int i)
2758 {
2759 memento.paranoidAt = i;
2760 return i;
2761 }
2762
2763 int Memento_getBlockNum(void *b)
2764 {
2765 Memento_BlkHeader *memblk;
2766 if (b == NULL)
2767 return 0;
2768 memblk = MEMBLK_FROMBLK(b);
2769 return (memblk->sequence);
2770 }
2771
2772 int Memento_check(void)
2773 {
2774 int result;
2775
2776 fprintf(stderr, "Checking memory\n");
2777 result = Memento_checkAllMemory();
2778 fprintf(stderr, "Memory checked!\n");
2779 return result;
2780 }
2781
2782 int Memento_find(void *a)
2783 {
2784 findBlkData data;
2785 int s;
2786
2787 MEMENTO_LOCK();
2788 data.addr = a;
2789 data.blk = NULL;
2790 data.flags = 0;
2791 Memento_appBlocks(&memento.used, Memento_containsAddr, &data);
2792 if (data.blk != NULL) {
2793 fprintf(stderr, "Address "FMTP" is in %sallocated block ",
2794 data.addr,
2795 (data.flags == 1 ? "" : (data.flags == 2 ?
2796 "preguard of " : "postguard of ")));
2797 s = showBlock(data.blk, ' ');
2798 fprintf(stderr, "\n");
2799 MEMENTO_UNLOCK();
2800 return s;
2801 }
2802 data.blk = NULL;
2803 data.flags = 0;
2804 Memento_appBlocks(&memento.free, Memento_containsAddr, &data);
2805 if (data.blk != NULL) {
2806 fprintf(stderr, "Address "FMTP" is in %sfreed block ",
2807 data.addr,
2808 (data.flags == 1 ? "" : (data.flags == 2 ?
2809 "preguard of " : "postguard of ")));
2810 s = showBlock(data.blk, ' ');
2811 fprintf(stderr, "\n");
2812 MEMENTO_UNLOCK();
2813 return s;
2814 }
2815 MEMENTO_UNLOCK();
2816 return 0;
2817 }
2818
2819 void Memento_breakOnFree(void *a)
2820 {
2821 findBlkData data;
2822
2823 MEMENTO_LOCK();
2824 data.addr = a;
2825 data.blk = NULL;
2826 data.flags = 0;
2827 Memento_appBlocks(&memento.used, Memento_containsAddr, &data);
2828 if (data.blk != NULL) {
2829 fprintf(stderr, "Will stop when address "FMTP" (in %sallocated block ",
2830 data.addr,
2831 (data.flags == 1 ? "" : (data.flags == 2 ?
2832 "preguard of " : "postguard of ")));
2833 showBlock(data.blk, ' ');
2834 fprintf(stderr, ") is freed\n");
2835 VALGRIND_MAKE_MEM_DEFINED(data.blk, sizeof(Memento_BlkHeader));
2836 data.blk->flags |= Memento_Flag_BreakOnFree;
2837 VALGRIND_MAKE_MEM_NOACCESS(data.blk, sizeof(Memento_BlkHeader));
2838 MEMENTO_UNLOCK();
2839 return;
2840 }
2841 data.blk = NULL;
2842 data.flags = 0;
2843 Memento_appBlocks(&memento.free, Memento_containsAddr, &data);
2844 if (data.blk != NULL) {
2845 fprintf(stderr, "Can't stop on free; address "FMTP" is in %sfreed block ",
2846 data.addr,
2847 (data.flags == 1 ? "" : (data.flags == 2 ?
2848 "preguard of " : "postguard of ")));
2849 showBlock(data.blk, ' ');
2850 fprintf(stderr, "\n");
2851 MEMENTO_UNLOCK();
2852 return;
2853 }
2854 fprintf(stderr, "Can't stop on free; address "FMTP" is not in a known block.\n", a);
2855 MEMENTO_UNLOCK();
2856 }
2857
2858 void Memento_breakOnRealloc(void *a)
2859 {
2860 findBlkData data;
2861
2862 MEMENTO_LOCK();
2863 data.addr = a;
2864 data.blk = NULL;
2865 data.flags = 0;
2866 Memento_appBlocks(&memento.used, Memento_containsAddr, &data);
2867 if (data.blk != NULL) {
2868 fprintf(stderr, "Will stop when address "FMTP" (in %sallocated block ",
2869 data.addr,
2870 (data.flags == 1 ? "" : (data.flags == 2 ?
2871 "preguard of " : "postguard of ")));
2872 showBlock(data.blk, ' ');
2873 fprintf(stderr, ") is freed (or realloced)\n");
2874 VALGRIND_MAKE_MEM_DEFINED(data.blk, sizeof(Memento_BlkHeader));
2875 data.blk->flags |= Memento_Flag_BreakOnFree | Memento_Flag_BreakOnRealloc;
2876 VALGRIND_MAKE_MEM_NOACCESS(data.blk, sizeof(Memento_BlkHeader));
2877 MEMENTO_UNLOCK();
2878 return;
2879 }
2880 data.blk = NULL;
2881 data.flags = 0;
2882 Memento_appBlocks(&memento.free, Memento_containsAddr, &data);
2883 if (data.blk != NULL) {
2884 fprintf(stderr, "Can't stop on free/realloc; address "FMTP" is in %sfreed block ",
2885 data.addr,
2886 (data.flags == 1 ? "" : (data.flags == 2 ?
2887 "preguard of " : "postguard of ")));
2888 showBlock(data.blk, ' ');
2889 fprintf(stderr, "\n");
2890 MEMENTO_UNLOCK();
2891 return;
2892 }
2893 fprintf(stderr, "Can't stop on free/realloc; address "FMTP" is not in a known block.\n", a);
2894 MEMENTO_UNLOCK();
2895 }
2896
2897 int Memento_failAt(int i)
2898 {
2899 memento.failAt = i;
2900 if ((memento.sequence > memento.failAt) &&
2901 (memento.failing != 0))
2902 Memento_startFailing();
2903 return i;
2904 }
2905
2906 size_t Memento_setMax(size_t max)
2907 {
2908 memento.maxMemory = max;
2909 return max;
2910 }
2911
2912 void Memento_startLeaking(void)
2913 {
2914 memento.leaking++;
2915 }
2916
2917 void Memento_stopLeaking(void)
2918 {
2919 memento.leaking--;
2920 }
2921
2922 int Memento_squeezing(void)
2923 {
2924 return memento.squeezing;
2925 }
2926
2927 #endif /* MEMENTO_CPP_EXTRAS_ONLY */
2928
2929 #ifdef __cplusplus
2930 /* Dumb overrides for the new and delete operators */
2931
2932 void *operator new(size_t size)
2933 {
2934 void *ret;
2935
2936 if (!memento.inited)
2937 Memento_init();
2938
2939 if (size == 0)
2940 size = 1;
2941 MEMENTO_LOCK();
2942 ret = do_malloc(size, Memento_EventType_new);
2943 MEMENTO_UNLOCK();
2944 return ret;
2945 }
2946
2947 void operator delete(void *pointer)
2948 {
2949 if (!pointer)
2950 return;
2951
2952 MEMENTO_LOCK();
2953 do_free(pointer, Memento_EventType_delete);
2954 MEMENTO_UNLOCK();
2955 }
2956
2957 /* Some C++ systems (apparently) don't provide new[] or delete[]
2958 * operators. Provide a way to cope with this */
2959 #ifndef MEMENTO_CPP_NO_ARRAY_CONSTRUCTORS
2960 void *operator new[](size_t size)
2961 {
2962 void *ret;
2963 if (!memento.inited)
2964 Memento_init();
2965
2966 if (size == 0)
2967 size = 1;
2968 MEMENTO_LOCK();
2969 ret = do_malloc(size, Memento_EventType_newArray);
2970 MEMENTO_UNLOCK();
2971 return ret;
2972 }
2973
2974 void operator delete[](void *pointer)
2975 {
2976 MEMENTO_LOCK();
2977 do_free(pointer, Memento_EventType_deleteArray);
2978 MEMENTO_UNLOCK();
2979 }
2980 #endif /* MEMENTO_CPP_NO_ARRAY_CONSTRUCTORS */
2981 #endif /* __cplusplus */
2982
2983 #else
2984
2985 /* Just in case anyone has left some debugging code in... */
2986 void (Memento_breakpoint)(void)
2987 {
2988 }
2989
2990 int (Memento_checkBlock)(void *b)
2991 {
2992 return 0;
2993 }
2994
2995 int (Memento_checkAllMemory)(void)
2996 {
2997 return 0;
2998 }
2999
3000 int (Memento_check)(void)
3001 {
3002 return 0;
3003 }
3004
3005 int (Memento_setParanoia)(int i)
3006 {
3007 return 0;
3008 }
3009
3010 int (Memento_paranoidAt)(int i)
3011 {
3012 return 0;
3013 }
3014
3015 int (Memento_breakAt)(int i)
3016 {
3017 return 0;
3018 }
3019
3020 int (Memento_getBlockNum)(void *i)
3021 {
3022 return 0;
3023 }
3024
3025 int (Memento_find)(void *a)
3026 {
3027 return 0;
3028 }
3029
3030 int (Memento_failAt)(int i)
3031 {
3032 return 0;
3033 }
3034
3035 void (Memento_breakOnFree)(void *a)
3036 {
3037 }
3038
3039 void (Memento_breakOnRealloc)(void *a)
3040 {
3041 }
3042
3043 void *(Memento_takeRef)(void *a)
3044 {
3045 return a;
3046 }
3047
3048 void *(Memento_dropRef)(void *a)
3049 {
3050 return a;
3051 }
3052
3053 void *(Memento_adjustRef)(void *a, int adjust)
3054 {
3055 return a;
3056 }
3057
3058 void *(Memento_reference)(void *a)
3059 {
3060 return a;
3061 }
3062
3063 #undef Memento_malloc
3064 #undef Memento_free
3065 #undef Memento_realloc
3066 #undef Memento_calloc
3067
3068 void *Memento_malloc(size_t size)
3069 {
3070 return MEMENTO_UNDERLYING_MALLOC(size);
3071 }
3072
3073 void Memento_free(void *b)
3074 {
3075 MEMENTO_UNDERLYING_FREE(b);
3076 }
3077
3078 void *Memento_realloc(void *b, size_t s)
3079 {
3080 return MEMENTO_UNDERLYING_REALLOC(b, s);
3081 }
3082
3083 void *Memento_calloc(size_t n, size_t s)
3084 {
3085 return MEMENTO_UNDERLYING_CALLOC(n, s);
3086 }
3087
3088 void (Memento_listBlocks)(void)
3089 {
3090 }
3091
3092 void (Memento_listNewBlocks)(void)
3093 {
3094 }
3095
3096 size_t (Memento_setMax)(size_t max)
3097 {
3098 return 0;
3099 }
3100
3101 void (Memento_stats)(void)
3102 {
3103 }
3104
3105 void *(Memento_label)(void *ptr, const char *label)
3106 {
3107 return ptr;
3108 }
3109
3110 void (Memento_info)(void *addr)
3111 {
3112 }
3113
3114 void (Memento_listBlockInfo)(void)
3115 {
3116 }
3117
3118 void (Memento_startLeaking)(void)
3119 {
3120 }
3121
3122 void (Memento_stopLeaking)(void)
3123 {
3124 }
3125
3126 int (Memento_squeezing)(void)
3127 {
3128 return 0;
3129 }
3130
3131 #endif