2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
4 * Copyright (c) 1996 by Silicon Graphics. All rights reserved.
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
9 * Permission is hereby granted to use or copy this program
10 * for any purpose, provided the above notices are retained on all copies.
11 * Permission to modify the code and to distribute modified code is granted,
12 * provided the above notices are retained, and a notice that the code was
13 * modified is included with the above copyright notice.
15 /* An incomplete test for the garbage collector. */
16 /* Some more obscure entry points are not tested at all. */
17 /* This must be compiled with the same flags used to build the */
18 /* GC. It uses GC internals to allow more precise results */
19 /* checking for some of the tests. */
22 # include "private/config.h"
27 #if (defined(DBG_HDRS_ALL) || defined(MAKE_BACK_GRAPH)) && !defined(GC_DEBUG)
33 #ifndef NTHREADS /* Number of additional threads to fork. */
34 # define NTHREADS 5 /* excludes main thread, which also runs a test. */
35 /* Not respected by PCR test. */
38 # if defined(mips) && defined(SYSTYPE_BSD43)
44 # if defined(_WIN32_WCE) && !defined(__GNUC__)
46 /* # define assert ASSERT */
48 # include <assert.h> /* Not normally used, but handy for debugging. */
51 # include "gc_typed.h"
52 # include "private/gc_priv.h" /* For output, locking, MIN_WORDS, */
53 /* and some statistics, and gcconfig.h. */
55 # if defined(MSWIN32) || defined(MSWINCE)
60 # ifdef GC_PRINT_VERBOSE_STATS
61 # define GC_print_stats VERBOSE
63 # define GC_print_stats 0 /* Not exported from DLL */
64 /* Redefine to 1 to generate output. */
69 # include "th/PCR_ThCrSec.h"
70 # include "th/PCR_Th.h"
71 # define GC_printf printf
74 # if defined(GC_PTHREADS)
78 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
79 static CRITICAL_SECTION incr_cs;
84 /* Call GC_INIT only on platforms on which we think we really need it, */
85 /* so that we can test automatic initialization on the rest. */
86 #if defined(CYGWIN32) || defined (AIX) || defined(DARWIN) \
87 || defined(THREAD_LOCAL_ALLOC) \
88 || (defined(MSWINCE) && !defined(GC_WINMAIN_REDIRECT))
89 # define GC_COND_INIT() GC_INIT()
91 # define GC_COND_INIT()
94 /* Allocation Statistics. Incremented without synchronization. */
95 /* FIXME: We should be using synchronization. */
96 int stubborn_count = 0;
97 int uncollectable_count = 0;
98 int collectable_count = 0;
100 int realloc_count = 0;
102 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
104 void GC_amiga_free_all_mem(void);
105 void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
106 # define FAIL (void)Amiga_Fail()
107 void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
108 void *ret=GC_malloc_explicitly_typed(lb,d);
112 ret=GC_malloc_explicitly_typed(lb,d);
115 GC_printf("Out of memory, (typed allocations are not directly "
116 "supported with the GC_AMIGA_FASTALLOC option.)\n");
122 void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
123 void *ret=GC_calloc_explicitly_typed(a,lb,d);
127 ret=GC_calloc_explicitly_typed(a,lb,d);
130 GC_printf("Out of memory, (typed allocations are not directly "
131 "supported with the GC_AMIGA_FASTALLOC option.)\n");
137 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b)
138 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c)
140 #else /* !AMIGA_FASTALLOC */
143 # define FAIL (void)abort()
145 # define FAIL ABORT("Test failed")
148 #endif /* !AMIGA_FASTALLOC */
150 /* AT_END may be defined to exercise the interior pointer test */
151 /* if the collector is configured with ALL_INTERIOR_POINTERS. */
152 /* As it stands, this test should succeed with either */
153 /* configuration. In the FIND_LEAK configuration, it should */
154 /* find lots of leaks, since we free almost nothing. */
157 struct SEXPR * sexpr_car;
158 struct SEXPR * sexpr_cdr;
162 typedef struct SEXPR * sexpr;
164 # define INT_TO_SEXPR(x) ((sexpr)(GC_word)(x))
165 # define SEXPR_TO_INT(x) ((int)(GC_word)(x))
168 # define nil (INT_TO_SEXPR(0))
169 # define car(x) ((x) -> sexpr_car)
170 # define cdr(x) ((x) -> sexpr_cdr)
171 # define is_nil(x) ((x) == nil)
174 int extra_count = 0; /* Amount of space wasted in cons node */
176 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
177 /* to test collector. */
178 # ifdef VERY_SMALL_CONFIG
179 # define cons small_cons
181 sexpr cons (sexpr x, sexpr y)
185 int my_extra = extra_count;
188 r = (sexpr) GC_MALLOC_STUBBORN(sizeof(struct SEXPR) + my_extra);
190 (void)GC_printf("Out of memory\n");
194 ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
196 (void)GC_printf("Found nonzero at %p - allocator is broken\n", p);
199 *p = (int)((13 << 12) + ((p - (int *)r) & 0xfff));
202 r = (sexpr)((char *)r + (my_extra & ~7));
207 if ( my_extra >= 5000 ) {
210 extra_count = my_extra;
212 GC_END_STUBBORN_CHANGE((char *)r);
217 #ifdef GC_GCJ_SUPPORT
222 /* The following struct emulates the vtable in gcj. */
223 /* This assumes the default value of MARK_DESCR_OFFSET. */
225 void * dummy; /* class pointer in real gcj. */
229 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
230 + sizeof(struct fake_vtable *) };
231 /* length based descriptor. */
232 struct fake_vtable gcj_class_struct2 =
233 { 0, ((GC_word)3 << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
234 /* Bitmap based descriptor. */
236 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
237 struct GC_ms_entry *mark_stack_ptr,
238 struct GC_ms_entry *mark_stack_limit,
243 /* Object allocated with debug allocator. */
244 addr = (word *)GC_USR_PTR_FROM_BASE(addr);
246 x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
247 mark_stack_ptr = GC_MARK_AND_PUSH(
248 (void *)(x -> sexpr_cdr), mark_stack_ptr,
249 mark_stack_limit, (void * *)&(x -> sexpr_cdr));
250 mark_stack_ptr = GC_MARK_AND_PUSH(
251 (void *)(x -> sexpr_car), mark_stack_ptr,
252 mark_stack_limit, (void * *)&(x -> sexpr_car));
253 return(mark_stack_ptr);
256 #endif /* GC_GCJ_SUPPORT */
259 sexpr small_cons (sexpr x, sexpr y)
264 r = (sexpr) GC_MALLOC(sizeof(struct SEXPR));
266 (void)GC_printf("Out of memory\n");
274 sexpr small_cons_uncollectable (sexpr x, sexpr y)
278 uncollectable_count++;
279 r = (sexpr) GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
281 (void)GC_printf("Out of memory\n");
285 r -> sexpr_cdr = (sexpr)(~(GC_word)y);
289 #ifdef GC_GCJ_SUPPORT
292 sexpr gcj_cons(sexpr x, sexpr y)
297 r = (GC_word *) GC_GCJ_MALLOC(sizeof(struct SEXPR)
298 + sizeof(struct fake_vtable*),
301 (void)GC_printf("Out of memory\n");
304 result = (sexpr)(r + 1);
305 result -> sexpr_car = x;
306 result -> sexpr_cdr = y;
311 /* Return reverse(x) concatenated with y */
312 sexpr reverse1(sexpr x, sexpr y)
317 return( reverse1(cdr(x), cons(car(x), y)) );
321 sexpr reverse(sexpr x)
323 # ifdef TEST_WITH_SYSTEM_MALLOC
326 return( reverse1(x, nil) );
329 sexpr ints(int low, int up)
334 return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
338 #ifdef GC_GCJ_SUPPORT
339 /* Return reverse(x) concatenated with y */
340 sexpr gcj_reverse1(sexpr x, sexpr y)
345 return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
349 sexpr gcj_reverse(sexpr x)
351 return( gcj_reverse1(x, nil) );
354 sexpr gcj_ints(int low, int up)
359 return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
362 #endif /* GC_GCJ_SUPPORT */
364 /* To check uncollectable allocation we build lists with disguised cdr */
365 /* pointers, and make sure they don't go away. */
366 sexpr uncollectable_ints(int low, int up)
371 return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
372 uncollectable_ints(low+1, up)));
376 void check_ints(sexpr list, int low, int up)
378 if (SEXPR_TO_INT(car(car(list))) != low) {
380 "List reversal produced incorrect list - collector is broken\n");
384 if (cdr(list) != nil) {
385 (void)GC_printf("List too long - collector is broken\n");
389 check_ints(cdr(list), low+1, up);
393 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(GC_word)(cdr(x)))
395 void check_uncollectable_ints(sexpr list, int low, int up)
397 if (SEXPR_TO_INT(car(car(list))) != low) {
399 "Uncollectable list corrupted - collector is broken\n");
403 if (UNCOLLECTABLE_CDR(list) != nil) {
404 (void)GC_printf("Uncollectable list too long - collector is broken\n");
408 check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
412 /* Not used, but useful for debugging: */
413 void print_int_list(sexpr x)
416 (void)GC_printf("NIL\n");
418 (void)GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
419 if (!is_nil(cdr(x))) {
420 (void)GC_printf(", ");
421 (void)print_int_list(cdr(x));
423 (void)GC_printf("\n");
429 void check_marks_int_list(sexpr x)
431 if (!GC_is_marked((ptr_t)x))
432 GC_printf("[unm:%p]", x);
434 GC_printf("[mkd:%p]", x);
436 (void)GC_printf("NIL\n");
438 if (!GC_is_marked((ptr_t)car(x))) GC_printf("[unm car:%p]", car(x));
439 (void)GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
440 if (!is_nil(cdr(x))) {
441 (void)GC_printf(", ");
442 (void)check_marks_int_list(cdr(x));
444 (void)GC_printf("\n");
450 * A tiny list reversal test to check thread creation.
454 # ifdef VERY_SMALL_CONFIG
455 # define TINY_REVERSE_UPPER_VALUE 4
457 # define TINY_REVERSE_UPPER_VALUE 10
460 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
461 DWORD __stdcall tiny_reverse_test(void * arg)
463 void * tiny_reverse_test(void * arg)
467 for (i = 0; i < 5; ++i) {
468 check_ints(reverse(reverse(ints(1, TINY_REVERSE_UPPER_VALUE))),
469 1, TINY_REVERSE_UPPER_VALUE);
474 # if defined(GC_PTHREADS)
475 void fork_a_thread(void)
479 if ((code = pthread_create(&t, 0, tiny_reverse_test, 0)) != 0) {
480 (void)GC_printf("Small thread creation failed %d\n", code);
483 if ((code = pthread_join(t, 0)) != 0) {
484 (void)GC_printf("Small thread join failed %d\n", code);
489 # elif defined(GC_WIN32_THREADS)
490 void fork_a_thread(void)
494 h = GC_CreateThread(NULL, 0, tiny_reverse_test, 0, 0, &thread_id);
495 if (h == (HANDLE)NULL) {
496 (void)GC_printf("Small thread creation failed %d\n",
497 (int)GetLastError());
500 if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
501 (void)GC_printf("Small thread wait failed %d\n",
502 (int)GetLastError());
511 /* Try to force a to be strangely aligned */
519 * Repeatedly reverse lists built out of very different sized cons cells.
520 * Check that we didn't lose anything.
522 void reverse_test(void)
530 # if defined(MSWIN32) || defined(MACOS)
531 /* Win32S only allows 128K stacks */
535 /* PCR default stack is 100K. Stack frames are up to 120 bytes. */
538 # if defined(MSWINCE)
539 /* WinCE only allows 64K stacks */
543 /* OSF has limited stack space by default, and large frames. */
556 d = uncollectable_ints(1, 100);
557 e = uncollectable_ints(1, 1);
558 /* Check that realloc updates object descriptors correctly */
560 f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
562 f = (sexpr *)GC_REALLOC((void *)f, 6 * sizeof(sexpr));
565 g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
567 g = (sexpr *)GC_REALLOC((void *)g, 800 * sizeof(sexpr));
570 h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
572 h = (sexpr *)GC_REALLOC((void *)h, 2000 * sizeof(sexpr));
573 # ifdef GC_GCJ_SUPPORT
574 h[1999] = gcj_ints(1,200);
575 for (i = 0; i < 51; ++i)
576 h[1999] = gcj_reverse(h[1999]);
577 /* Leave it as the reveresed list for now. */
579 h[1999] = ints(1,200);
581 /* Try to force some collections and reuse of small list elements */
582 for (i = 0; i < 10; i++) {
585 /* Superficially test interior pointer recognition on stack */
586 c = (sexpr)((char *)c + sizeof(char *));
587 d = (sexpr)((char *)d + sizeof(char *));
593 for (i = 0; i < 50; i++) {
595 b = reverse(reverse(b));
599 for (i = 0; i < 60; i++) {
600 # if defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)
601 if (i % 10 == 0) fork_a_thread();
603 /* This maintains the invariant that a always points to a list of */
604 /* 49 integers. Thus this is thread safe without locks, */
605 /* assuming atomic pointer assignments. */
606 a = reverse(reverse(a));
607 # if !defined(AT_END) && !defined(THREADS)
608 /* This is not thread safe, since realloc explicitly deallocates */
610 a = (sexpr)GC_REALLOC((void *)a, 500);
612 a = (sexpr)GC_REALLOC((void *)a, 8200);
618 c = (sexpr)((char *)c - sizeof(char *));
619 d = (sexpr)((char *)d - sizeof(char *));
621 check_uncollectable_ints(d, 1, 100);
622 check_ints(f[5], 1,17);
623 check_ints(g[799], 1,18);
624 # ifdef GC_GCJ_SUPPORT
625 h[1999] = gcj_reverse(h[1999]);
627 check_ints(h[1999], 1,200);
631 *(volatile void **)&b = 0;
632 *(volatile void **)&c = 0;
638 * The rest of this builds balanced binary trees, checks that they don't
639 * disappear, and tests finalization.
641 typedef struct treenode {
643 struct treenode * lchild;
644 struct treenode * rchild;
647 int finalizable_count = 0;
648 int finalized_count = 0;
649 volatile int dropped_something = 0;
651 void GC_CALLBACK finalizer(void * obj, void * client_data)
656 PCR_ThCrSec_EnterSys();
658 # if defined(GC_PTHREADS)
659 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
660 pthread_mutex_lock(&incr_lock);
661 # elif defined(GC_WIN32_THREADS)
662 EnterCriticalSection(&incr_cs);
664 if ((int)(GC_word)client_data != t -> level) {
665 (void)GC_printf("Wrong finalization data - collector is broken\n");
669 t -> level = -1; /* detect duplicate finalization immediately */
671 PCR_ThCrSec_ExitSys();
673 # if defined(GC_PTHREADS)
674 pthread_mutex_unlock(&incr_lock);
675 # elif defined(GC_WIN32_THREADS)
676 LeaveCriticalSection(&incr_cs);
682 # define MAX_FINALIZED (NTHREADS*4000)
685 GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
687 /* Too big for THINK_C. have to allocate it dynamically. */
688 GC_word *live_indicators = 0;
691 int live_indicators_count = 0;
695 tn * result = (tn *)GC_MALLOC(sizeof(tn));
699 /* get around static data limitations. */
700 if (!live_indicators)
702 (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
703 if (!live_indicators) {
704 (void)GC_printf("Out of memory\n");
708 if (n == 0) return(0);
710 (void)GC_printf("Out of memory\n");
714 result -> lchild = mktree(n-1);
715 result -> rchild = mktree(n-1);
716 if (counter++ % 17 == 0 && n >= 2) {
717 tn * tmp = result -> lchild -> rchild;
719 result -> lchild -> rchild = result -> rchild -> lchild;
720 result -> rchild -> lchild = tmp;
722 if (counter++ % 119 == 0) {
727 PCR_ThCrSec_EnterSys();
729 # if defined(GC_PTHREADS)
730 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
731 pthread_mutex_lock(&incr_lock);
732 # elif defined(GC_WIN32_THREADS)
733 EnterCriticalSection(&incr_cs);
735 /* Losing a count here causes erroneous report of failure. */
737 my_index = live_indicators_count++;
739 PCR_ThCrSec_ExitSys();
741 # if defined(GC_PTHREADS)
742 pthread_mutex_unlock(&incr_lock);
743 # elif defined(GC_WIN32_THREADS)
744 LeaveCriticalSection(&incr_cs);
748 GC_REGISTER_FINALIZER((void *)result, finalizer, (void *)(GC_word)n,
749 (GC_finalization_proc *)0, (void * *)0);
750 if (my_index >= MAX_FINALIZED) {
751 GC_printf("live_indicators overflowed\n");
754 live_indicators[my_index] = 13;
755 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
756 (void * *)(&(live_indicators[my_index])),
757 (void *)result) != 0) {
758 GC_printf("GC_general_register_disappearing_link failed\n");
761 if (GC_unregister_disappearing_link(
763 (&(live_indicators[my_index]))) == 0) {
764 GC_printf("GC_unregister_disappearing_link failed\n");
767 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
768 (void * *)(&(live_indicators[my_index])),
769 (void *)result) != 0) {
770 GC_printf("GC_general_register_disappearing_link failed 2\n");
773 GC_reachable_here(result);
778 void chktree(tn *t, int n)
780 if (n == 0 && t != 0) {
781 (void)GC_printf("Clobbered a leaf - collector is broken\n");
785 if (t -> level != n) {
786 (void)GC_printf("Lost a node at level %d - collector is broken\n", n);
789 if (counter++ % 373 == 0) {
791 (void) GC_MALLOC(counter%5001);
793 chktree(t -> lchild, n-1);
794 if (counter++ % 73 == 0) {
796 (void) GC_MALLOC(counter%373);
798 chktree(t -> rchild, n-1);
802 #if defined(GC_PTHREADS)
803 pthread_key_t fl_key;
805 void * alloc8bytes(void)
807 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
809 return(GC_MALLOC(8));
811 void ** my_free_list_ptr;
814 my_free_list_ptr = (void **)pthread_getspecific(fl_key);
815 if (my_free_list_ptr == 0) {
816 uncollectable_count++;
817 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
818 if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
819 (void)GC_printf("pthread_setspecific failed\n");
823 my_free_list = *my_free_list_ptr;
824 if (my_free_list == 0) {
825 my_free_list = GC_malloc_many(8);
826 if (my_free_list == 0) {
827 (void)GC_printf("alloc8bytes out of memory\n");
831 *my_free_list_ptr = GC_NEXT(my_free_list);
832 GC_NEXT(my_free_list) = 0;
834 return(my_free_list);
839 # define alloc8bytes() GC_MALLOC_ATOMIC(8)
842 void alloc_small(int n)
846 for (i = 0; i < n; i += 8) {
848 if (alloc8bytes() == 0) {
849 (void)GC_printf("Out of memory\n");
855 # if defined(THREADS) && defined(GC_DEBUG)
856 # ifdef VERY_SMALL_CONFIG
857 # define TREE_HEIGHT 12
859 # define TREE_HEIGHT 15
862 # ifdef VERY_SMALL_CONFIG
863 # define TREE_HEIGHT 13
865 # define TREE_HEIGHT 16
873 root = mktree(TREE_HEIGHT);
874 # ifndef VERY_SMALL_CONFIG
875 alloc_small(5000000);
877 chktree(root, TREE_HEIGHT);
878 if (finalized_count && ! dropped_something) {
879 (void)GC_printf("Premature finalization - collector is broken\n");
882 dropped_something = 1;
883 GC_noop1((word)root); /* Root needs to remain live until */
884 /* dropped_something is set. */
885 root = mktree(TREE_HEIGHT);
886 chktree(root, TREE_HEIGHT);
887 for (i = TREE_HEIGHT; i >= 0; i--) {
891 # ifndef VERY_SMALL_CONFIG
892 alloc_small(5000000);
896 unsigned n_tests = 0;
898 GC_word bm_huge[10] = {
911 /* A very simple test of explicitly typed allocation */
912 void typed_test(void)
914 GC_word * old, * new;
917 GC_word bm_large = 0xf7ff7fff;
918 GC_descr d1 = GC_make_descriptor(&bm3, 2);
919 GC_descr d2 = GC_make_descriptor(&bm2, 2);
920 GC_descr d3 = GC_make_descriptor(&bm_large, 32);
921 GC_descr d4 = GC_make_descriptor(bm_huge, 320);
922 GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
926 (void)GC_make_descriptor(&bm_large, 32);
930 for (i = 0; i < 4000; i++) {
932 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
933 if (0 != new[0] || 0 != new[1]) {
934 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
938 new[1] = (GC_word)old;
941 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
943 new[1] = (GC_word)old;
946 new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
948 new[1] = (GC_word)old;
951 new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
954 new[1] = (GC_word)old;
958 new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
961 new = (GC_word *) GC_calloc_explicitly_typed(1001,
964 if (0 != new[0] || 0 != new[1]) {
965 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
970 new[1] = (GC_word)old;
973 for (i = 0; i < 20000; i++) {
975 (void)GC_printf("typed alloc failed at %lu\n",
981 new = (GC_word *)(old[1]);
990 void GC_CALLBACK fail_proc1(void * x)
995 static void uniq(void *p, ...) {
1001 for (;(q[n] = va_arg(a,void *)) != NULL;n++) ;
1007 "Apparently failed to mark from some function arguments.\n"
1008 "Perhaps GC_push_regs was configured incorrectly?\n"
1015 # define TEST_FAIL_COUNT(n) 1
1017 # define TEST_FAIL_COUNT(n) (fail_count >= (n))
1020 void * GC_CALLBACK inc_int_counter(void *pcounter)
1022 ++(*(int *)pcounter);
1026 void run_one_test(void)
1028 # ifndef DBG_HDRS_ALL
1034 char *y = (char *)(GC_word)fail_proc1;
1036 CLOCK_TYPE typed_time;
1038 CLOCK_TYPE start_time;
1039 CLOCK_TYPE reverse_time;
1040 CLOCK_TYPE tree_time;
1041 unsigned long time_diff;
1045 "This test program is not designed for leak detection mode\n");
1046 GC_printf("Expect lots of problems.\n");
1049 # ifndef DBG_HDRS_ALL
1050 collectable_count += 3;
1051 if ((GC_size(GC_malloc(7)) != 8 &&
1052 GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word))
1053 || GC_size(GC_malloc(15)) != 16) {
1054 GC_printf("GC_size produced unexpected results\n");
1057 collectable_count += 1;
1058 if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1059 GC_printf("GC_malloc(0) failed: GC_size returns %ld\n",
1060 (unsigned long)GC_size(GC_malloc(0)));
1063 collectable_count += 1;
1064 if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1065 GC_printf("GC_malloc_uncollectable(0) failed\n");
1068 GC_is_valid_displacement_print_proc = fail_proc1;
1069 GC_is_visible_print_proc = fail_proc1;
1070 collectable_count += 1;
1072 if (GC_base(GC_PTR_ADD(x, 13)) != x) {
1073 GC_printf("GC_base(heap ptr) produced incorrect result\n");
1076 (void)GC_PRE_INCR(x, 0);
1077 (void)GC_POST_INCR(x);
1078 (void)GC_POST_DECR(x);
1079 if (GC_base(x) != x) {
1080 GC_printf("Bad INCR/DECR result\n");
1084 if (GC_base(y) != 0) {
1085 GC_printf("GC_base(fn_ptr) produced incorrect result\n");
1089 if (GC_same_obj(x+5, x) != x + 5) {
1090 GC_printf("GC_same_obj produced incorrect result\n");
1093 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1094 GC_printf("GC_is_visible produced incorrect result\n");
1100 GC_printf("GC_PTR_STORE failed: %p != %p\n", *z, x);
1103 if (!TEST_FAIL_COUNT(1)) {
1104 # if!(defined(POWERPC) || defined(IA64)) || defined(M68K)
1105 /* On POWERPCs function pointers point to a descriptor in the */
1106 /* data segment, so there should have been no failures. */
1107 /* The same applies to IA64. Something similar seems to */
1108 /* be going on with NetBSD/M68K. */
1109 GC_printf("GC_is_visible produced wrong failure indication\n");
1113 if (GC_is_valid_displacement(y) != y
1114 || GC_is_valid_displacement(x) != x
1115 || GC_is_valid_displacement(x + 3) != x + 3) {
1117 "GC_is_valid_displacement produced incorrect result\n");
1124 for (i = sizeof(GC_word); i < 512; i *= 2) {
1125 GC_word result = (GC_word) GC_memalign(i, 17);
1126 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1129 # ifndef ALL_INTERIOR_POINTERS
1130 # if defined(RS6000) || defined(POWERPC)
1131 if (!TEST_FAIL_COUNT(1))
1133 if ((GC_all_interior_pointers && !TEST_FAIL_COUNT(1))
1134 || (!GC_all_interior_pointers && !TEST_FAIL_COUNT(2)))
1137 GC_printf("GC_is_valid_displacement produced wrong failure indication\n");
1141 # endif /* DBG_HDRS_ALL */
1142 /* Test floating point alignment */
1143 collectable_count += 2;
1144 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1145 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1146 /* Test size 0 allocation a bit more */
1149 for (i = 0; i < 10000; ++i) {
1151 GC_FREE(GC_MALLOC(0));
1152 GC_MALLOC_ATOMIC(0);
1153 GC_FREE(GC_MALLOC_ATOMIC(0));
1156 # ifdef GC_GCJ_SUPPORT
1157 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1158 GC_init_gcj_malloc(0, (void *)(GC_word)fake_gcj_mark_proc);
1160 /* Make sure that fn arguments are visible to the collector. */
1162 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1163 (GC_gcollect(),GC_malloc(12)),
1164 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1165 (GC_gcollect(),GC_malloc(12)),
1166 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1167 (GC_gcollect(),GC_malloc(12)),
1168 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1169 (GC_gcollect(),GC_malloc(12)),
1170 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1171 (GC_gcollect(),GC_malloc(12)),
1173 /* GC_malloc(0) must return NULL or something we can deallocate. */
1174 GC_free(GC_malloc(0));
1175 GC_free(GC_malloc_atomic(0));
1176 GC_free(GC_malloc(0));
1177 GC_free(GC_malloc_atomic(0));
1178 /* Repeated list reversal test. */
1179 GET_TIME(start_time);
1181 if (GC_print_stats) {
1182 GET_TIME(reverse_time);
1183 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1184 GC_log_printf("-------------Finished reverse_test at time %u (%p)\n",
1185 (unsigned) time_diff, &start_time);
1187 # ifndef DBG_HDRS_ALL
1189 if (GC_print_stats) {
1190 GET_TIME(typed_time);
1191 time_diff = MS_TIME_DIFF(typed_time, start_time);
1192 GC_log_printf("-------------Finished typed_test at time %u (%p)\n",
1193 (unsigned) time_diff, &start_time);
1195 # endif /* DBG_HDRS_ALL */
1197 if (GC_print_stats) {
1198 GET_TIME(tree_time);
1199 time_diff = MS_TIME_DIFF(tree_time, start_time);
1200 GC_log_printf("-------------Finished tree_test at time %u (%p)\n",
1201 (unsigned) time_diff, &start_time);
1203 /* Run reverse_test a second time, so we hopefully notice corruption. */
1205 if (GC_print_stats) {
1206 GET_TIME(reverse_time);
1207 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1208 GC_log_printf("-------------Finished second reverse_test at time %u (%p)\n",
1209 (unsigned) time_diff, &start_time);
1211 /* GC_allocate_ml and GC_need_to_lock are no longer exported, and */
1212 /* AO_fetch_and_add1() may be unavailable to update a counter. */
1213 (void)GC_call_with_alloc_lock(inc_int_counter, &n_tests);
1214 # if defined(THREADS) && defined(HANDLE_FORK)
1217 tiny_reverse_test(0);
1220 GC_log_printf("Finished a child process\n");
1225 GC_log_printf("Finished %p\n", &start_time);
1228 void check_heap_stats(void)
1233 # ifdef FINALIZE_ON_DEMAND
1234 int late_finalize_count = 0;
1237 # ifdef VERY_SMALL_CONFIG
1238 /* The upper bounds are a guess, which has been empirically */
1239 /* adjusted. On low end uniprocessors with incremental GC */
1240 /* these may be particularly dubious, since empirically the */
1241 /* heap tends to grow largely as a result of the GC not */
1242 /* getting enough cycles. */
1243 # if CPP_WORDSZ == 64
1244 max_heap_sz = 4500000;
1246 max_heap_sz = 2800000;
1249 # if CPP_WORDSZ == 64
1250 max_heap_sz = 19000000;
1252 max_heap_sz = 12000000;
1257 # ifdef SAVE_CALL_CHAIN
1259 # ifdef SAVE_CALL_COUNT
1260 max_heap_sz += max_heap_sz * SAVE_CALL_COUNT/4;
1264 /* Garbage collect repeatedly so that all inaccessible objects */
1265 /* can be finalized. */
1266 while (GC_collect_a_little()) { }
1267 for (i = 0; i < 16; i++) {
1269 # ifdef FINALIZE_ON_DEMAND
1270 late_finalize_count +=
1272 GC_invoke_finalizers();
1274 (void)GC_printf("Completed %u tests\n", n_tests);
1275 (void)GC_printf("Allocated %d collectable objects\n", collectable_count);
1276 (void)GC_printf("Allocated %d uncollectable objects\n",
1277 uncollectable_count);
1278 (void)GC_printf("Allocated %d atomic objects\n", atomic_count);
1279 (void)GC_printf("Allocated %d stubborn objects\n", stubborn_count);
1280 (void)GC_printf("Finalized %d/%d objects - ",
1281 finalized_count, finalizable_count);
1282 # ifdef FINALIZE_ON_DEMAND
1283 if (finalized_count != late_finalize_count) {
1284 (void)GC_printf("Demand finalization error\n");
1288 if (finalized_count > finalizable_count
1289 || finalized_count < finalizable_count/2) {
1290 (void)GC_printf("finalization is probably broken\n");
1293 (void)GC_printf("finalization is probably ok\n");
1296 for (i = 0; i < MAX_FINALIZED; i++) {
1297 if (live_indicators[i] != 0) {
1301 i = finalizable_count - finalized_count - still_live;
1303 GC_printf("%d disappearing links remain and %d more objects "
1304 "were not finalized\n", still_live, i);
1306 GC_printf("\tVery suspicious!\n");
1308 GC_printf("\tSlightly suspicious, but probably OK.\n");
1311 (void)GC_printf("Total number of bytes allocated is %lu\n",
1313 (GC_bytes_allocd + GC_bytes_allocd_before_gc));
1314 (void)GC_printf("Final heap size is %lu bytes\n",
1315 (unsigned long)GC_get_heap_size());
1316 if (GC_bytes_allocd + GC_bytes_allocd_before_gc < n_tests *
1317 # ifdef VERY_SMALL_CONFIG
1323 (void)GC_printf("Incorrect execution - missed some allocations\n");
1326 if (GC_get_heap_size() + GC_get_unmapped_bytes() > max_heap_sz*n_tests) {
1327 (void)GC_printf("Unexpected heap growth - collector may be broken\n");
1330 (void)GC_printf("Collector appears to work\n");
1334 void SetMinimumStack(long minSize)
1338 if (minSize > LMGetDefltStack())
1340 newApplLimit = (long) GetApplLimit()
1341 - (minSize - LMGetDefltStack());
1342 SetApplLimit((Ptr) newApplLimit);
1347 #define cMinStackSpace (512L * 1024L)
1351 void GC_CALLBACK warn_proc(char *msg, GC_word p)
1353 GC_printf(msg, (unsigned long)p);
1357 #if defined(MSWINCE) && defined(UNDER_CE)
1358 # define WINMAIN_LPTSTR LPWSTR
1360 # define WINMAIN_LPTSTR LPSTR
1364 && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \
1366 #if defined(MSWIN32) && !defined(__MINGW32__) || defined(MSWINCE)
1367 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev,
1368 WINMAIN_LPTSTR cmd, int n)
1375 /* Make sure we have lots and lots of stack space. */
1376 SetMinimumStack(cMinStackSpace);
1377 /* Cheat and let stdio initialize toolbox for us. */
1378 printf("Testing GC Macintosh port.\n");
1381 GC_set_warn_proc(warn_proc);
1382 # if (defined(MPROTECT_VDB) || defined(PROC_VDB) || defined(GWW_VDB)) \
1383 && !defined(MAKE_BACK_GRAPH) && !defined(NO_INCREMENTAL)
1384 GC_enable_incremental();
1385 GC_printf("Switched to incremental mode\n");
1386 # if defined(MPROTECT_VDB)
1387 GC_printf("Emulating dirty bits with mprotect/signals\n");
1390 GC_printf("Reading dirty bits from /proc\n");
1392 GC_printf("Using DEFAULT_VDB dirty bit implementation\n");
1402 /* Entry points we should be testing, but aren't. */
1403 /* Some can be tested by defining GC_DEBUG at the top of this file */
1404 /* This is a bit SunOS4 specific. */
1405 GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
1406 GC_register_disappearing_link,
1407 GC_register_finalizer_ignore_self,
1408 GC_debug_register_displacement, GC_debug_change_stubborn,
1409 GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
1410 GC_debug_free, GC_debug_realloc,
1411 GC_generic_malloc_words_small, GC_init,
1412 GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
1413 GC_set_max_heap_size, GC_get_bytes_since_gc,
1414 GC_get_total_bytes, GC_pre_incr, GC_post_incr);
1417 GC_win32_free_heap();
1423 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
1425 DWORD __stdcall thr_run_one_test(void *arg)
1432 HANDLE win_created_h;
1435 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1440 GC_printf("Received WM_HIBERNATE, calling GC_gcollect\n");
1441 /* Force "unmap as much memory as possible" mode. */
1442 GC_gcollect_and_unmap();
1445 GC_printf("Received WM_CLOSE, closing window\n");
1446 DestroyWindow(hwnd);
1452 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
1458 DWORD __stdcall thr_window(void *arg)
1460 WNDCLASS win_class = {
1465 GetModuleHandle(NULL),
1468 (HBRUSH)(COLOR_APPWORKSPACE+1),
1470 TEXT("GCtestWindow")
1474 if (!RegisterClass(&win_class))
1477 win_handle = CreateWindowEx(
1479 TEXT("GCtestWindow"),
1482 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1485 GetModuleHandle(NULL),
1488 if (win_handle == NULL)
1491 SetEvent(win_created_h);
1493 ShowWindow(win_handle, SW_SHOW);
1494 UpdateWindow(win_handle);
1496 while (GetMessage(&msg, NULL, 0, 0)) {
1497 TranslateMessage(&msg);
1498 DispatchMessage(&msg);
1505 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev,
1506 WINMAIN_LPTSTR cmd, int n)
1516 # if defined(GC_DLL) && !defined(GC_NO_DLLMAIN) && !defined(MSWINCE) \
1517 && !defined(THREAD_LOCAL_ALLOC) && !defined(PARALLEL_MARK)
1518 GC_use_DllMain(); /* Test with implicit thread registration if possible. */
1519 GC_printf("Using DllMain to track threads\n");
1522 # ifndef NO_INCREMENTAL
1523 GC_enable_incremental();
1525 InitializeCriticalSection(&incr_cs);
1526 GC_set_warn_proc(warn_proc);
1528 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
1529 if (win_created_h == (HANDLE)NULL) {
1530 (void)GC_printf("Event creation failed %d\n", (int)GetLastError());
1533 win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
1534 if (win_thr_h == (HANDLE)NULL) {
1535 (void)GC_printf("Thread creation failed %d\n", (int)GetLastError());
1538 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
1540 CloseHandle(win_created_h);
1543 for (i = 0; i < NTHREADS; i++) {
1544 h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
1545 if (h[i] == (HANDLE)NULL) {
1546 (void)GC_printf("Thread creation failed %d\n", (int)GetLastError());
1550 # endif /* NTHREADS > 0 */
1553 for (i = 0; i < NTHREADS; i++) {
1554 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
1555 (void)GC_printf("Thread wait failed %d\n", (int)GetLastError());
1559 # endif /* NTHREADS > 0 */
1561 PostMessage(win_handle, WM_CLOSE, 0, 0);
1562 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
1569 #endif /* GC_WIN32_THREADS */
1580 /* GC_enable_incremental(); */
1581 GC_set_warn_proc(warn_proc);
1582 th1 = PCR_Th_Fork(run_one_test, 0);
1583 th2 = PCR_Th_Fork(run_one_test, 0);
1585 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1586 != PCR_ERes_okay || code != 0) {
1587 (void)GC_printf("Thread 1 failed\n");
1589 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1590 != PCR_ERes_okay || code != 0) {
1591 (void)GC_printf("Thread 2 failed\n");
1598 #if defined(GC_PTHREADS)
1599 void * thr_run_one_test(void * arg)
1606 # define GC_free GC_debug_free
1611 pthread_t th[NTHREADS];
1612 pthread_attr_t attr;
1615 # ifdef GC_IRIX_THREADS
1616 /* Force a larger stack to be preallocated */
1617 /* Since the initial can't always grow later. */
1618 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 MB */
1619 # endif /* GC_IRIX_THREADS */
1620 # if defined(GC_HPUX_THREADS)
1621 /* Default stack size is too small, especially with the 64 bit ABI */
1623 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
1624 (void)GC_printf("pthread_default_stacksize_np failed.\n");
1626 # endif /* GC_HPUX_THREADS */
1627 # ifdef PTW32_STATIC_LIB
1628 pthread_win32_process_attach_np ();
1629 pthread_win32_thread_attach_np ();
1633 pthread_attr_init(&attr);
1634 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
1635 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) \
1636 || defined(GC_OPENBSD_THREADS)
1637 pthread_attr_setstacksize(&attr, 1000000);
1640 # if (defined(MPROTECT_VDB)) \
1641 && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \
1642 && !defined(MAKE_BACK_GRAPH) && !defined(USE_PROC_FOR_LIBRARIES) \
1643 && !defined(NO_INCREMENTAL)
1644 GC_enable_incremental();
1645 (void) GC_printf("Switched to incremental mode\n");
1646 # if defined(MPROTECT_VDB)
1647 (void)GC_printf("Emulating dirty bits with mprotect/signals\n");
1650 (void)GC_printf("Reading dirty bits from /proc\n");
1652 (void)GC_printf("Using DEFAULT_VDB dirty bit implementation\n");
1656 GC_set_warn_proc(warn_proc);
1657 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
1658 (void)GC_printf("Key creation failed %d\n", code);
1661 for (i = 0; i < NTHREADS; ++i) {
1662 if ((code = pthread_create(th+i, &attr, thr_run_one_test, 0)) != 0) {
1663 (void)GC_printf("Thread %d creation failed %d\n", i, code);
1668 for (i = 0; i < NTHREADS; ++i) {
1669 if ((code = pthread_join(th[i], 0)) != 0) {
1670 (void)GC_printf("Thread %d failed %d\n", i, code);
1675 (void)fflush(stdout);
1676 pthread_attr_destroy(&attr);
1677 GC_printf("Completed %u collections\n", (unsigned)GC_gc_no);
1678 # ifdef PTW32_STATIC_LIB
1679 pthread_win32_thread_detach_np ();
1680 pthread_win32_process_detach_np ();
1684 #endif /* GC_PTHREADS */