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. */
23 #if defined(DBG_HDRS_ALL) || defined(MAKE_BACK_GRAPH)
27 # if defined(mips) && defined(SYSTYPE_BSD43)
35 # define assert ASSERT
37 # include <assert.h> /* Not normally used, but handy for debugging. */
39 # include <assert.h> /* Not normally used, but handy for debugging. */
41 # include "gc_typed.h"
42 # ifdef THREAD_LOCAL_ALLOC
43 # include "gc_local_alloc.h"
45 # include "private/gc_priv.h" /* For output, locking, MIN_WORDS, */
46 /* and some statistics. */
47 # include "private/gcconfig.h"
49 # if defined(MSWIN32) || defined(MSWINCE)
54 # include "th/PCR_ThCrSec.h"
55 # include "th/PCR_Th.h"
57 # define GC_printf0 printf
59 # define GC_printf1 printf
62 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
67 # if defined(GC_PTHREADS)
71 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
72 static CRITICAL_SECTION incr_cs;
80 /* Allocation Statistics */
81 int stubborn_count = 0;
82 int uncollectable_count = 0;
83 int collectable_count = 0;
85 int realloc_count = 0;
87 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
89 extern void GC_amiga_free_all_mem(void);
90 void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
91 # define FAIL (void)Amiga_Fail()
92 void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
93 void *ret=GC_malloc_explicitly_typed(lb,d);
97 ret=GC_malloc_explicitly_typed(lb,d);
100 GC_printf0("Out of memory, (typed allocations are not directly "
101 "supported with the GC_AMIGA_FASTALLOC option.)\n");
107 void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
108 void *ret=GC_calloc_explicitly_typed(a,lb,d);
112 ret=GC_calloc_explicitly_typed(a,lb,d);
115 GC_printf0("Out of memory, (typed allocations are not directly "
116 "supported with the GC_AMIGA_FASTALLOC option.)\n");
122 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b)
123 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c)
125 #else /* !AMIGA_FASTALLOC */
128 # define FAIL (void)abort()
131 # define FAIL DebugBreak()
133 # define FAIL GC_abort("Test failed");
137 #endif /* !AMIGA_FASTALLOC */
139 /* AT_END may be defined to exercise the interior pointer test */
140 /* if the collector is configured with ALL_INTERIOR_POINTERS. */
141 /* As it stands, this test should succeed with either */
142 /* configuration. In the FIND_LEAK configuration, it should */
143 /* find lots of leaks, since we free almost nothing. */
146 struct SEXPR * sexpr_car;
147 struct SEXPR * sexpr_cdr;
151 typedef struct SEXPR * sexpr;
153 # define INT_TO_SEXPR(x) ((sexpr)(unsigned long)(x))
156 # define nil (INT_TO_SEXPR(0))
157 # define car(x) ((x) -> sexpr_car)
158 # define cdr(x) ((x) -> sexpr_cdr)
159 # define is_nil(x) ((x) == nil)
162 int extra_count = 0; /* Amount of space wasted in cons node */
164 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
165 /* to test collector. */
166 # ifdef VERY_SMALL_CONFIG
167 # define cons small_cons
175 register int my_extra = extra_count;
178 r = (sexpr) GC_MALLOC_STUBBORN(sizeof(struct SEXPR) + my_extra);
180 (void)GC_printf0("Out of memory\n");
184 ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
186 (void)GC_printf1("Found nonzero at 0x%lx - allocator is broken\n",
193 r = (sexpr)((char *)r + (my_extra & ~7));
198 if ( my_extra >= 5000 ) {
201 extra_count = my_extra;
203 GC_END_STUBBORN_CHANGE((char *)r);
208 #ifdef GC_GCJ_SUPPORT
213 /* The following struct emulates the vtable in gcj. */
214 /* This assumes the default value of MARK_DESCR_OFFSET. */
216 void * dummy; /* class pointer in real gcj. */
220 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
221 + sizeof(struct fake_vtable *) };
222 /* length based descriptor. */
223 struct fake_vtable gcj_class_struct2 =
224 { 0, (3l << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
225 /* Bitmap based descriptor. */
227 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
228 struct GC_ms_entry *mark_stack_ptr,
229 struct GC_ms_entry *mark_stack_limit,
234 /* Object allocated with debug allocator. */
235 addr = (word *)GC_USR_PTR_FROM_BASE(addr);
237 x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
238 mark_stack_ptr = GC_MARK_AND_PUSH(
239 (GC_PTR)(x -> sexpr_cdr), mark_stack_ptr,
240 mark_stack_limit, (GC_PTR *)&(x -> sexpr_cdr));
241 mark_stack_ptr = GC_MARK_AND_PUSH(
242 (GC_PTR)(x -> sexpr_car), mark_stack_ptr,
243 mark_stack_limit, (GC_PTR *)&(x -> sexpr_car));
244 return(mark_stack_ptr);
247 #endif /* GC_GCJ_SUPPORT */
249 #ifdef THREAD_LOCAL_ALLOC
251 #undef GC_REDIRECT_TO_LOCAL
252 #include "gc_local_alloc.h"
254 sexpr local_cons (x, y)
260 register int my_extra = extra_count;
261 static int my_random = 0;
264 r = (sexpr) GC_LOCAL_MALLOC(sizeof(struct SEXPR) + my_extra);
265 # ifdef GC_GCJ_SUPPORT
266 if (collectable_count % 2 == 0) {
267 r = (sexpr) GC_LOCAL_GCJ_MALLOC(sizeof(struct SEXPR) + sizeof(GC_word) + my_extra,
269 r = (sexpr) ((GC_word *)r + 1);
273 (void)GC_printf0("Out of memory\n");
277 ((char *)p) < ((char *)r) + my_extra + sizeof(struct SEXPR); p++) {
279 (void)GC_printf1("Found nonzero at 0x%lx (local) - allocator is broken\n",
288 if ( my_extra >= 5000 || my_extra == 200 && ++my_random % 37 != 0) {
291 extra_count = my_extra;
295 #endif /* THREAD_LOCAL_ALLOC */
297 sexpr small_cons (x, y)
304 r = (sexpr) GC_MALLOC(sizeof(struct SEXPR));
306 (void)GC_printf0("Out of memory\n");
314 sexpr small_cons_uncollectable (x, y)
320 uncollectable_count++;
321 r = (sexpr) GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
323 (void)GC_printf0("Out of memory\n");
327 r -> sexpr_cdr = (sexpr)(~(unsigned long)y);
331 #ifdef GC_GCJ_SUPPORT
340 static int count = 0;
343 # ifdef USE_MARK_BYTES
344 r = (GC_word *) GC_GCJ_FAST_MALLOC(4, &gcj_class_struct1);
346 r = (GC_word *) GC_GCJ_FAST_MALLOC(3, &gcj_class_struct1);
349 r = (GC_word *) GC_GCJ_MALLOC(sizeof(struct SEXPR)
350 + sizeof(struct fake_vtable*),
354 (void)GC_printf0("Out of memory\n");
357 result = (sexpr)(r + 1);
358 result -> sexpr_car = x;
359 result -> sexpr_cdr = y;
364 /* Return reverse(x) concatenated with y */
371 return( reverse1(cdr(x), cons(car(x), y)) );
378 # ifdef TEST_WITH_SYSTEM_MALLOC
381 return( reverse1(x, nil) );
390 return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
394 #ifdef GC_GCJ_SUPPORT
395 /* Return reverse(x) concatenated with y */
396 sexpr gcj_reverse1(x, y)
402 return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
409 return( gcj_reverse1(x, nil) );
412 sexpr gcj_ints(low, up)
418 return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
421 #endif /* GC_GCJ_SUPPORT */
423 #ifdef THREAD_LOCAL_ALLOC
424 /* Return reverse(x) concatenated with y */
425 sexpr local_reverse1(x, y)
431 return( local_reverse1(cdr(x), local_cons(car(x), y)) );
435 sexpr local_reverse(x)
438 return( local_reverse1(x, nil) );
441 sexpr local_ints(low, up)
447 return(local_cons(local_cons(INT_TO_SEXPR(low), nil), local_ints(low+1, up)));
450 #endif /* THREAD_LOCAL_ALLOC */
452 /* To check uncollectable allocation we build lists with disguised cdr */
453 /* pointers, and make sure they don't go away. */
454 sexpr uncollectable_ints(low, up)
460 return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
461 uncollectable_ints(low+1, up)));
465 void check_ints(list, low, up)
469 if ((int)(GC_word)(car(car(list))) != low) {
471 "List reversal produced incorrect list - collector is broken\n");
475 if (cdr(list) != nil) {
476 (void)GC_printf0("List too long - collector is broken\n");
480 check_ints(cdr(list), low+1, up);
484 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(unsigned long)(cdr(x)))
486 void check_uncollectable_ints(list, low, up)
490 if ((int)(GC_word)(car(car(list))) != low) {
492 "Uncollectable list corrupted - collector is broken\n");
496 if (UNCOLLECTABLE_CDR(list) != nil) {
497 (void)GC_printf0("Uncollectable list too long - collector is broken\n");
501 check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
505 /* Not used, but useful for debugging: */
506 void print_int_list(x)
510 (void)GC_printf0("NIL\n");
512 (void)GC_printf1("(%ld)", (long)(car(car(x))));
513 if (!is_nil(cdr(x))) {
514 (void)GC_printf0(", ");
515 (void)print_int_list(cdr(x));
517 (void)GC_printf0("\n");
523 * A tiny list reversal test to check thread creation.
527 # if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)
528 DWORD __stdcall tiny_reverse_test(void * arg)
530 void * tiny_reverse_test(void * arg)
534 for (i = 0; i < 5; ++i) {
535 check_ints(reverse(reverse(ints(1,10))), 1, 10);
536 # ifdef THREAD_LOCAL_ALLOC
537 check_ints(local_reverse(local_reverse(local_ints(1,10))), 1, 10);
543 # if defined(GC_PTHREADS)
548 if ((code = pthread_create(&t, 0, tiny_reverse_test, 0)) != 0) {
549 (void)GC_printf1("Small thread creation failed %lu\n",
550 (unsigned long)code);
553 if ((code = pthread_join(t, 0)) != 0) {
554 (void)GC_printf1("Small thread join failed %lu\n",
555 (unsigned long)code);
560 # elif defined(GC_WIN32_THREADS)
565 h = GC_CreateThread(NULL, 0, tiny_reverse_test, 0, 0, &thread_id);
566 if (h == (HANDLE)NULL) {
567 (void)GC_printf1("Small thread creation failed %lu\n",
568 (unsigned long)GetLastError());
571 if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
572 (void)GC_printf1("Small thread wait failed %lu\n",
573 (unsigned long)GetLastError());
578 /* # elif defined(GC_SOLARIS_THREADS) */
582 # define fork_a_thread()
588 # define fork_a_thread()
592 /* Try to force a to be strangely aligned */
600 * Repeatedly reverse lists built out of very different sized cons cells.
601 * Check that we didn't lose anything.
611 # if defined(MSWIN32) || defined(MACOS)
612 /* Win32S only allows 128K stacks */
616 /* PCR default stack is 100K. Stack frames are up to 120 bytes. */
620 /* WinCE only allows 64K stacks */
624 /* OSF has limited stack space by default, and large frames. */
637 d = uncollectable_ints(1, 100);
638 e = uncollectable_ints(1, 1);
639 /* Check that realloc updates object descriptors correctly */
641 f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
643 f = (sexpr *)GC_REALLOC((GC_PTR)f, 6 * sizeof(sexpr));
646 g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
648 g = (sexpr *)GC_REALLOC((GC_PTR)g, 800 * sizeof(sexpr));
651 h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
653 h = (sexpr *)GC_REALLOC((GC_PTR)h, 2000 * sizeof(sexpr));
654 # ifdef GC_GCJ_SUPPORT
655 h[1999] = gcj_ints(1,200);
656 for (i = 0; i < 51; ++i)
657 h[1999] = gcj_reverse(h[1999]);
658 /* Leave it as the reveresed list for now. */
660 h[1999] = ints(1,200);
662 /* Try to force some collections and reuse of small list elements */
663 for (i = 0; i < 10; i++) {
666 /* Superficially test interior pointer recognition on stack */
667 c = (sexpr)((char *)c + sizeof(char *));
668 d = (sexpr)((char *)d + sizeof(char *));
677 for (i = 0; i < 50; i++) {
679 b = reverse(reverse(b));
683 for (i = 0; i < 60; i++) {
684 if (i % 10 == 0) fork_a_thread();
685 /* This maintains the invariant that a always points to a list of */
686 /* 49 integers. Thus this is thread safe without locks, */
687 /* assuming atomic pointer assignments. */
688 a = reverse(reverse(a));
689 # ifdef THREAD_LOCAL_ALLOC
690 a = local_reverse(local_reverse(a));
692 # if !defined(AT_END) && !defined(THREADS)
693 /* This is not thread safe, since realloc explicitly deallocates */
695 a = (sexpr)GC_REALLOC((GC_PTR)a, 500);
697 a = (sexpr)GC_REALLOC((GC_PTR)a, 8200);
703 c = (sexpr)((char *)c - sizeof(char *));
704 d = (sexpr)((char *)d - sizeof(char *));
706 check_uncollectable_ints(d, 1, 100);
707 check_ints(f[5], 1,17);
708 check_ints(g[799], 1,18);
709 # ifdef GC_GCJ_SUPPORT
710 h[1999] = gcj_reverse(h[1999]);
712 check_ints(h[1999], 1,200);
722 * The rest of this builds balanced binary trees, checks that they don't
723 * disappear, and tests finalization.
725 typedef struct treenode {
727 struct treenode * lchild;
728 struct treenode * rchild;
731 int finalizable_count = 0;
732 int finalized_count = 0;
733 VOLATILE int dropped_something = 0;
736 void finalizer(void * obj, void * client_data)
738 void finalizer(obj, client_data)
746 PCR_ThCrSec_EnterSys();
748 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
749 static mutex_t incr_lock;
750 mutex_lock(&incr_lock);
752 # if defined(GC_PTHREADS)
753 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
754 pthread_mutex_lock(&incr_lock);
756 # ifdef GC_WIN32_THREADS
757 EnterCriticalSection(&incr_cs);
760 if ((int)(GC_word)client_data != t -> level) {
761 (void)GC_printf0("Wrong finalization data - collector is broken\n");
765 t -> level = -1; /* detect duplicate finalization immediately */
767 PCR_ThCrSec_ExitSys();
769 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
770 mutex_unlock(&incr_lock);
772 # if defined(GC_PTHREADS)
773 pthread_mutex_unlock(&incr_lock);
775 # ifdef GC_WIN32_THREADS
776 LeaveCriticalSection(&incr_cs);
783 # define MAX_FINALIZED 8000
786 GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
788 /* Too big for THINK_C. have to allocate it dynamically. */
789 GC_word *live_indicators = 0;
792 int live_indicators_count = 0;
797 # ifdef THREAD_LOCAL_ALLOC
798 tn * result = (tn *)GC_LOCAL_MALLOC(sizeof(tn));
800 tn * result = (tn *)GC_MALLOC(sizeof(tn));
804 # ifdef THREAD_LOCAL_ALLOC
805 /* Minimally exercise thread local allocation */
807 char * result = (char *)GC_LOCAL_MALLOC_ATOMIC(17);
808 memset(result, 'a', 17);
810 # endif /* THREAD_LOCAL_ALLOC */
812 /* get around static data limitations. */
813 if (!live_indicators)
815 (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
816 if (!live_indicators) {
817 (void)GC_printf0("Out of memory\n");
821 if (n == 0) return(0);
823 (void)GC_printf0("Out of memory\n");
827 result -> lchild = mktree(n-1);
828 result -> rchild = mktree(n-1);
829 if (counter++ % 17 == 0 && n >= 2) {
830 tn * tmp = result -> lchild -> rchild;
832 result -> lchild -> rchild = result -> rchild -> lchild;
833 result -> rchild -> lchild = tmp;
835 if (counter++ % 119 == 0) {
840 PCR_ThCrSec_EnterSys();
842 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
843 static mutex_t incr_lock;
844 mutex_lock(&incr_lock);
846 # if defined(GC_PTHREADS)
847 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
848 pthread_mutex_lock(&incr_lock);
850 # ifdef GC_WIN32_THREADS
851 EnterCriticalSection(&incr_cs);
854 /* Losing a count here causes erroneous report of failure. */
856 my_index = live_indicators_count++;
858 PCR_ThCrSec_ExitSys();
860 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
861 mutex_unlock(&incr_lock);
863 # if defined(GC_PTHREADS)
864 pthread_mutex_unlock(&incr_lock);
866 # ifdef GC_WIN32_THREADS
867 LeaveCriticalSection(&incr_cs);
872 GC_REGISTER_FINALIZER((GC_PTR)result, finalizer, (GC_PTR)(GC_word)n,
873 (GC_finalization_proc *)0, (GC_PTR *)0);
874 if (my_index >= MAX_FINALIZED) {
875 GC_printf0("live_indicators overflowed\n");
878 live_indicators[my_index] = 13;
879 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
880 (GC_PTR *)(&(live_indicators[my_index])),
881 (GC_PTR)result) != 0) {
882 GC_printf0("GC_general_register_disappearing_link failed\n");
885 if (GC_unregister_disappearing_link(
887 (&(live_indicators[my_index]))) == 0) {
888 GC_printf0("GC_unregister_disappearing_link failed\n");
891 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
892 (GC_PTR *)(&(live_indicators[my_index])),
893 (GC_PTR)result) != 0) {
894 GC_printf0("GC_general_register_disappearing_link failed 2\n");
905 if (n == 0 && t != 0) {
906 (void)GC_printf0("Clobbered a leaf - collector is broken\n");
910 if (t -> level != n) {
911 (void)GC_printf1("Lost a node at level %lu - collector is broken\n",
915 if (counter++ % 373 == 0) {
917 (void) GC_MALLOC(counter%5001);
919 chktree(t -> lchild, n-1);
920 if (counter++ % 73 == 0) {
922 (void) GC_MALLOC(counter%373);
924 chktree(t -> rchild, n-1);
927 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
932 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
934 return(GC_MALLOC(8));
936 void ** my_free_list_ptr;
939 if (thr_getspecific(fl_key, (void **)(&my_free_list_ptr)) != 0) {
940 (void)GC_printf0("thr_getspecific failed\n");
943 if (my_free_list_ptr == 0) {
944 uncollectable_count++;
945 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
946 if (thr_setspecific(fl_key, my_free_list_ptr) != 0) {
947 (void)GC_printf0("thr_setspecific failed\n");
951 my_free_list = *my_free_list_ptr;
952 if (my_free_list == 0) {
954 my_free_list = GC_malloc_many(8);
955 if (my_free_list == 0) {
956 (void)GC_printf0("alloc8bytes out of memory\n");
960 *my_free_list_ptr = GC_NEXT(my_free_list);
961 GC_NEXT(my_free_list) = 0;
962 return(my_free_list);
968 # if defined(GC_PTHREADS)
969 pthread_key_t fl_key;
973 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
975 return(GC_MALLOC(8));
977 void ** my_free_list_ptr;
980 my_free_list_ptr = (void **)pthread_getspecific(fl_key);
981 if (my_free_list_ptr == 0) {
982 uncollectable_count++;
983 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
984 if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
985 (void)GC_printf0("pthread_setspecific failed\n");
989 my_free_list = *my_free_list_ptr;
990 if (my_free_list == 0) {
991 my_free_list = GC_malloc_many(8);
992 if (my_free_list == 0) {
993 (void)GC_printf0("alloc8bytes out of memory\n");
997 *my_free_list_ptr = GC_NEXT(my_free_list);
998 GC_NEXT(my_free_list) = 0;
1000 return(my_free_list);
1005 # define alloc8bytes() GC_MALLOC_ATOMIC(8)
1014 for (i = 0; i < n; i += 8) {
1016 if (alloc8bytes() == 0) {
1017 (void)GC_printf0("Out of memory\n");
1023 # if defined(THREADS) && defined(GC_DEBUG)
1024 # ifdef VERY_SMALL_CONFIG
1025 # define TREE_HEIGHT 12
1027 # define TREE_HEIGHT 15
1030 # ifdef VERY_SMALL_CONFIG
1031 # define TREE_HEIGHT 13
1033 # define TREE_HEIGHT 16
1041 root = mktree(TREE_HEIGHT);
1042 # ifndef VERY_SMALL_CONFIG
1043 alloc_small(5000000);
1045 chktree(root, TREE_HEIGHT);
1046 if (finalized_count && ! dropped_something) {
1047 (void)GC_printf0("Premature finalization - collector is broken\n");
1050 dropped_something = 1;
1051 GC_noop(root); /* Root needs to remain live until */
1052 /* dropped_something is set. */
1053 root = mktree(TREE_HEIGHT);
1054 chktree(root, TREE_HEIGHT);
1055 for (i = TREE_HEIGHT; i >= 0; i--) {
1059 # ifndef VERY_SMALL_CONFIG
1060 alloc_small(5000000);
1064 unsigned n_tests = 0;
1066 GC_word bm_huge[10] = {
1079 /* A very simple test of explicitly typed allocation */
1082 GC_word * old, * new;
1085 GC_word bm_large = 0xf7ff7fff;
1086 GC_descr d1 = GC_make_descriptor(&bm3, 2);
1087 GC_descr d2 = GC_make_descriptor(&bm2, 2);
1089 GC_descr dummy = GC_make_descriptor(&bm_large, 32);
1091 GC_descr d3 = GC_make_descriptor(&bm_large, 32);
1092 GC_descr d4 = GC_make_descriptor(bm_huge, 320);
1093 GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
1096 collectable_count++;
1098 for (i = 0; i < 4000; i++) {
1099 collectable_count++;
1100 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
1101 if (0 != new[0] || 0 != new[1]) {
1102 GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1106 new[1] = (GC_word)old;
1108 collectable_count++;
1109 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
1111 new[1] = (GC_word)old;
1113 collectable_count++;
1114 new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
1116 new[1] = (GC_word)old;
1118 collectable_count++;
1119 new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
1122 new[1] = (GC_word)old;
1124 collectable_count++;
1126 new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
1129 new = (GC_word *) GC_calloc_explicitly_typed(1001,
1130 3 * sizeof(GC_word),
1132 if (0 != new[0] || 0 != new[1]) {
1133 GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1138 new[1] = (GC_word)old;
1141 for (i = 0; i < 20000; i++) {
1143 (void)GC_printf1("typed alloc failed at %lu\n",
1149 new = (GC_word *)(old[1]);
1168 void fail_proc1(GC_PTR x)
1173 static void uniq(void *p, ...) {
1179 for (;(q[n] = va_arg(a,void *));n++) ;
1185 "Apparently failed to mark form some function arguments.\n"
1186 "Perhaps GC_push_regs was configured incorrectly?\n"
1192 #endif /* __STDC__ */
1195 # define TEST_FAIL_COUNT(n) 1
1197 # define TEST_FAIL_COUNT(n) (fail_count >= (n))
1206 char *y = (char *)(size_t)fail_proc1;
1212 "This test program is not designed for leak detection mode\n");
1213 (void)GC_printf0("Expect lots of problems.\n");
1216 # ifndef DBG_HDRS_ALL
1217 collectable_count += 3;
1218 if (GC_size(GC_malloc(7)) != 8 &&
1219 GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word)
1220 || GC_size(GC_malloc(15)) != 16) {
1221 (void)GC_printf0("GC_size produced unexpected results\n");
1224 collectable_count += 1;
1225 if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1226 (void)GC_printf1("GC_malloc(0) failed: GC_size returns %ld\n",
1227 GC_size(GC_malloc(0)));
1230 collectable_count += 1;
1231 if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1232 (void)GC_printf0("GC_malloc_uncollectable(0) failed\n");
1235 GC_is_valid_displacement_print_proc = fail_proc1;
1236 GC_is_visible_print_proc = fail_proc1;
1237 collectable_count += 1;
1239 if (GC_base(x + 13) != x) {
1240 (void)GC_printf0("GC_base(heap ptr) produced incorrect result\n");
1244 if (GC_base(y) != 0) {
1245 (void)GC_printf0("GC_base(fn_ptr) produced incorrect result\n");
1249 if (GC_same_obj(x+5, x) != x + 5) {
1250 (void)GC_printf0("GC_same_obj produced incorrect result\n");
1253 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1254 (void)GC_printf0("GC_is_visible produced incorrect result\n");
1257 if (!TEST_FAIL_COUNT(1)) {
1258 # if!(defined(RS6000) || defined(POWERPC) || defined(IA64)) || defined(M68K)
1259 /* ON RS6000s function pointers point to a descriptor in the */
1260 /* data segment, so there should have been no failures. */
1261 /* The same applies to IA64. Something similar seems to */
1262 /* be going on with NetBSD/M68K. */
1263 (void)GC_printf0("GC_is_visible produced wrong failure indication\n");
1267 if (GC_is_valid_displacement(y) != y
1268 || GC_is_valid_displacement(x) != x
1269 || GC_is_valid_displacement(x + 3) != x + 3) {
1271 "GC_is_valid_displacement produced incorrect result\n");
1274 # if defined(__STDC__) && !defined(MSWIN32) && !defined(MSWINCE)
1275 /* Harder to test under Windows without a gc.h declaration. */
1278 extern void *GC_memalign();
1281 for (i = sizeof(GC_word); i < 512; i *= 2) {
1282 GC_word result = (GC_word) GC_memalign(i, 17);
1283 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1287 # ifndef ALL_INTERIOR_POINTERS
1288 # if defined(RS6000) || defined(POWERPC)
1289 if (!TEST_FAIL_COUNT(1)) {
1291 if (GC_all_interior_pointers && !TEST_FAIL_COUNT(1)
1292 || !GC_all_interior_pointers && !TEST_FAIL_COUNT(2)) {
1294 (void)GC_printf0("GC_is_valid_displacement produced wrong failure indication\n");
1298 # endif /* DBG_HDRS_ALL */
1299 /* Test floating point alignment */
1300 collectable_count += 2;
1301 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1302 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1303 # ifdef GC_GCJ_SUPPORT
1304 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1305 GC_init_gcj_malloc(0, (void *)fake_gcj_mark_proc);
1307 /* Make sure that fn arguments are visible to the collector. */
1310 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1311 (GC_gcollect(),GC_malloc(12)),
1312 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1313 (GC_gcollect(),GC_malloc(12)),
1314 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1315 (GC_gcollect(),GC_malloc(12)),
1316 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1317 (GC_gcollect(),GC_malloc(12)),
1318 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1319 (GC_gcollect(),GC_malloc(12)),
1322 /* Repeated list reversal test. */
1325 GC_printf0("-------------Finished reverse_test\n");
1327 # ifndef DBG_HDRS_ALL
1330 GC_printf0("-------------Finished typed_test\n");
1332 # endif /* DBG_HDRS_ALL */
1337 # if defined(THREADS) && defined(HANDLE_FORK)
1340 tiny_reverse_test(0);
1342 GC_printf0("Finished a child process\n");
1346 /* GC_printf1("Finished %x\n", pthread_self()); */
1349 void check_heap_stats()
1351 unsigned long max_heap_sz;
1354 int late_finalize_count = 0;
1356 # ifdef VERY_SMALL_CONFIG
1357 /* these are something of a guess */
1358 if (sizeof(char *) > 4) {
1359 max_heap_sz = 4500000;
1361 max_heap_sz = 2800000;
1364 if (sizeof(char *) > 4) {
1365 max_heap_sz = 19000000;
1367 max_heap_sz = 11000000;
1370 # ifndef ALIGN_DOUBLE
1371 /* We end up needing more small object pages. */
1372 max_heap_sz += 2000000;
1376 # ifdef SAVE_CALL_CHAIN
1378 # ifdef SAVE_CALL_COUNT
1379 max_heap_sz += max_heap_sz * SAVE_CALL_COUNT/4;
1383 /* Garbage collect repeatedly so that all inaccessible objects */
1384 /* can be finalized. */
1385 while (GC_collect_a_little()) { }
1386 for (i = 0; i < 16; i++) {
1388 late_finalize_count += GC_invoke_finalizers();
1390 (void)GC_printf1("Completed %lu tests\n", (unsigned long)n_tests);
1391 (void)GC_printf1("Allocated %lu collectable objects\n", (unsigned long)collectable_count);
1392 (void)GC_printf1("Allocated %lu uncollectable objects\n", (unsigned long)uncollectable_count);
1393 (void)GC_printf1("Allocated %lu atomic objects\n", (unsigned long)atomic_count);
1394 (void)GC_printf1("Allocated %lu stubborn objects\n", (unsigned long)stubborn_count);
1395 (void)GC_printf2("Finalized %lu/%lu objects - ",
1396 (unsigned long)finalized_count,
1397 (unsigned long)finalizable_count);
1398 # ifdef FINALIZE_ON_DEMAND
1399 if (finalized_count != late_finalize_count) {
1400 (void)GC_printf0("Demand finalization error\n");
1404 if (finalized_count > finalizable_count
1405 || finalized_count < finalizable_count/2) {
1406 (void)GC_printf0("finalization is probably broken\n");
1409 (void)GC_printf0("finalization is probably ok\n");
1412 for (i = 0; i < MAX_FINALIZED; i++) {
1413 if (live_indicators[i] != 0) {
1417 i = finalizable_count - finalized_count - still_live;
1420 ("%lu disappearing links remain and %ld more objects were not finalized\n",
1421 (unsigned long) still_live, (long)i);
1423 GC_printf0("\tVery suspicious!\n");
1425 GC_printf0("\tSlightly suspicious, but probably OK.\n");
1428 (void)GC_printf1("Total number of bytes allocated is %lu\n",
1430 WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc));
1431 (void)GC_printf1("Final heap size is %lu bytes\n",
1432 (unsigned long)GC_get_heap_size());
1433 if (WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)
1434 # ifdef VERY_SMALL_CONFIG
1435 < 2700000*n_tests) {
1437 < 33500000*n_tests) {
1439 (void)GC_printf0("Incorrect execution - missed some allocations\n");
1442 if (GC_get_heap_size() > max_heap_sz*n_tests) {
1443 (void)GC_printf0("Unexpected heap growth - collector may be broken\n");
1446 (void)GC_printf0("Collector appears to work\n");
1450 void SetMinimumStack(long minSize)
1454 if (minSize > LMGetDefltStack())
1456 newApplLimit = (long) GetApplLimit()
1457 - (minSize - LMGetDefltStack());
1458 SetApplLimit((Ptr) newApplLimit);
1463 #define cMinStackSpace (512L * 1024L)
1468 void warn_proc(char *msg, GC_word p)
1470 void warn_proc(msg, p)
1475 GC_printf1(msg, (unsigned long)p);
1480 #if !defined(PCR) && !defined(GC_SOLARIS_THREADS) \
1481 && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \
1483 #if defined(MSWIN32) && !defined(__MINGW32__)
1484 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)
1495 /* No good way to determine stack base from library; do it */
1496 /* manually on this platform. */
1497 GC_stackbottom = (GC_PTR)(&dummy);
1500 /* Make sure we have lots and lots of stack space. */
1501 SetMinimumStack(cMinStackSpace);
1502 /* Cheat and let stdio initialize toolbox for us. */
1503 printf("Testing GC Macintosh port.\n");
1505 GC_INIT(); /* Only needed on a few platforms. */
1506 (void) GC_set_warn_proc(warn_proc);
1507 # if (defined(MPROTECT_VDB) || defined(PROC_VDB)) \
1508 && !defined(MAKE_BACK_GRAPH)
1509 GC_enable_incremental();
1510 (void) GC_printf0("Switched to incremental mode\n");
1511 # if defined(MPROTECT_VDB)
1512 (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
1515 (void)GC_printf0("Reading dirty bits from /proc\n");
1517 (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
1524 (void)fflush(stdout);
1527 /* Entry points we should be testing, but aren't. */
1528 /* Some can be tested by defining GC_DEBUG at the top of this file */
1529 /* This is a bit SunOS4 specific. */
1530 GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
1531 GC_register_disappearing_link,
1532 GC_register_finalizer_ignore_self,
1533 GC_debug_register_displacement,
1534 GC_print_obj, GC_debug_change_stubborn,
1535 GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
1536 GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small,
1537 GC_init, GC_make_closure, GC_debug_invoke_finalizer,
1538 GC_page_was_ever_dirty, GC_is_fresh,
1539 GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
1540 GC_set_max_heap_size, GC_get_bytes_since_gc,
1541 GC_get_total_bytes, GC_pre_incr, GC_post_incr);
1544 GC_win32_free_heap();
1550 #if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)
1552 DWORD __stdcall thr_run_one_test(void *arg)
1559 HANDLE win_created_h;
1562 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1567 GC_printf0("Received WM_HIBERNATE, calling GC_gcollect\n");
1571 GC_printf0("Received WM_CLOSE, closing window\n");
1572 DestroyWindow(hwnd);
1578 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
1584 DWORD __stdcall thr_window(void *arg)
1586 WNDCLASS win_class = {
1591 GetModuleHandle(NULL),
1594 (HBRUSH)(COLOR_APPWORKSPACE+1),
1600 if (!RegisterClass(&win_class))
1603 win_handle = CreateWindowEx(
1608 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1611 GetModuleHandle(NULL),
1614 if (win_handle == NULL)
1617 SetEvent(win_created_h);
1619 ShowWindow(win_handle, SW_SHOW);
1620 UpdateWindow(win_handle);
1622 while (GetMessage(&msg, NULL, 0, 0)) {
1623 TranslateMessage(&msg);
1624 DispatchMessage(&msg);
1634 int APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)
1636 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)
1648 GC_enable_incremental();
1651 InitializeCriticalSection(&incr_cs);
1652 (void) GC_set_warn_proc(warn_proc);
1654 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
1655 if (win_created_h == (HANDLE)NULL) {
1656 (void)GC_printf1("Event creation failed %lu\n", (unsigned long)GetLastError());
1659 win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
1660 if (win_thr_h == (HANDLE)NULL) {
1661 (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1664 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
1666 CloseHandle(win_created_h);
1669 for (i = 0; i < NTEST; i++) {
1670 h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
1671 if (h[i] == (HANDLE)NULL) {
1672 (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1676 # endif /* NTEST > 0 */
1679 for (i = 0; i < NTEST; i++) {
1680 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
1681 (void)GC_printf1("Thread wait failed %lu\n", (unsigned long)GetLastError());
1685 # endif /* NTEST > 0 */
1687 PostMessage(win_handle, WM_CLOSE, 0, 0);
1688 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
1695 #endif /* GC_WIN32_THREADS */
1706 /* GC_enable_incremental(); */
1707 (void) GC_set_warn_proc(warn_proc);
1708 th1 = PCR_Th_Fork(run_one_test, 0);
1709 th2 = PCR_Th_Fork(run_one_test, 0);
1711 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1712 != PCR_ERes_okay || code != 0) {
1713 (void)GC_printf0("Thread 1 failed\n");
1715 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1716 != PCR_ERes_okay || code != 0) {
1717 (void)GC_printf0("Thread 2 failed\n");
1724 #if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS)
1725 void * thr_run_one_test(void * arg)
1732 # define GC_free GC_debug_free
1735 #if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
1743 GC_INIT(); /* Only needed if gc is dynamic library. */
1744 # ifndef MAKE_BACK_GRAPH
1745 GC_enable_incremental();
1747 (void) GC_set_warn_proc(warn_proc);
1748 if (thr_keycreate(&fl_key, GC_free) != 0) {
1749 (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1752 if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, 0, &th1)) != 0) {
1753 (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1756 if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, THR_NEW_LWP, &th2)) != 0) {
1757 (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1761 if ((code = thr_join(th1, 0, 0)) != 0) {
1762 (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1765 if (thr_join(th2, 0, 0) != 0) {
1766 (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1770 (void)fflush(stdout);
1773 #else /* pthreads */
1783 pthread_attr_t attr;
1786 # ifdef GC_IRIX_THREADS
1787 /* Force a larger stack to be preallocated */
1788 /* Since the initial cant always grow later. */
1789 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 Mb */
1790 # endif /* GC_IRIX_THREADS */
1791 # if defined(GC_HPUX_THREADS)
1792 /* Default stack size is too small, especially with the 64 bit ABI */
1794 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
1795 (void)GC_printf0("pthread_default_stacksize_np failed.\n");
1797 # endif /* GC_HPUX_THREADS */
1800 pthread_attr_init(&attr);
1801 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
1802 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS)
1803 pthread_attr_setstacksize(&attr, 1000000);
1806 # if (defined(MPROTECT_VDB)) \
1807 && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \
1808 && !defined(MAKE_BACK_GRAPH)
1809 GC_enable_incremental();
1810 (void) GC_printf0("Switched to incremental mode\n");
1811 # if defined(MPROTECT_VDB)
1812 (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
1815 (void)GC_printf0("Reading dirty bits from /proc\n");
1817 (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
1821 (void) GC_set_warn_proc(warn_proc);
1822 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
1823 (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1826 if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) {
1827 (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1830 if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) {
1831 (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1835 if ((code = pthread_join(th1, 0)) != 0) {
1836 (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1839 if (pthread_join(th2, 0) != 0) {
1840 (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1844 (void)fflush(stdout);
1845 pthread_attr_destroy(&attr);
1846 GC_printf1("Completed %d collections\n", GC_gc_no);
1849 #endif /* GC_PTHREADS */
1850 #endif /* GC_SOLARIS_THREADS || GC_PTHREADS */