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. */
627 # if defined(__MACH__) && defined(__ppc64__)
628 /* Small stack and largish frames. */
642 d = uncollectable_ints(1, 100);
643 e = uncollectable_ints(1, 1);
644 /* Check that realloc updates object descriptors correctly */
646 f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
648 f = (sexpr *)GC_REALLOC((GC_PTR)f, 6 * sizeof(sexpr));
651 g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
653 g = (sexpr *)GC_REALLOC((GC_PTR)g, 800 * sizeof(sexpr));
656 h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
658 h = (sexpr *)GC_REALLOC((GC_PTR)h, 2000 * sizeof(sexpr));
659 # ifdef GC_GCJ_SUPPORT
660 h[1999] = gcj_ints(1,200);
661 for (i = 0; i < 51; ++i)
662 h[1999] = gcj_reverse(h[1999]);
663 /* Leave it as the reveresed list for now. */
665 h[1999] = ints(1,200);
667 /* Try to force some collections and reuse of small list elements */
668 for (i = 0; i < 10; i++) {
671 /* Superficially test interior pointer recognition on stack */
672 c = (sexpr)((char *)c + sizeof(char *));
673 d = (sexpr)((char *)d + sizeof(char *));
682 for (i = 0; i < 50; i++) {
684 b = reverse(reverse(b));
688 for (i = 0; i < 60; i++) {
689 if (i % 10 == 0) fork_a_thread();
690 /* This maintains the invariant that a always points to a list of */
691 /* 49 integers. Thus this is thread safe without locks, */
692 /* assuming atomic pointer assignments. */
693 a = reverse(reverse(a));
694 # ifdef THREAD_LOCAL_ALLOC
695 a = local_reverse(local_reverse(a));
697 # if !defined(AT_END) && !defined(THREADS)
698 /* This is not thread safe, since realloc explicitly deallocates */
700 a = (sexpr)GC_REALLOC((GC_PTR)a, 500);
702 a = (sexpr)GC_REALLOC((GC_PTR)a, 8200);
708 c = (sexpr)((char *)c - sizeof(char *));
709 d = (sexpr)((char *)d - sizeof(char *));
711 check_uncollectable_ints(d, 1, 100);
712 check_ints(f[5], 1,17);
713 check_ints(g[799], 1,18);
714 # ifdef GC_GCJ_SUPPORT
715 h[1999] = gcj_reverse(h[1999]);
717 check_ints(h[1999], 1,200);
727 * The rest of this builds balanced binary trees, checks that they don't
728 * disappear, and tests finalization.
730 typedef struct treenode {
732 struct treenode * lchild;
733 struct treenode * rchild;
736 int finalizable_count = 0;
737 int finalized_count = 0;
738 VOLATILE int dropped_something = 0;
741 void finalizer(void * obj, void * client_data)
743 void finalizer(obj, client_data)
751 PCR_ThCrSec_EnterSys();
753 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
754 static mutex_t incr_lock;
755 mutex_lock(&incr_lock);
757 # if defined(GC_PTHREADS)
758 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
759 pthread_mutex_lock(&incr_lock);
761 # ifdef GC_WIN32_THREADS
762 EnterCriticalSection(&incr_cs);
765 if ((int)(GC_word)client_data != t -> level) {
766 (void)GC_printf0("Wrong finalization data - collector is broken\n");
770 t -> level = -1; /* detect duplicate finalization immediately */
772 PCR_ThCrSec_ExitSys();
774 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
775 mutex_unlock(&incr_lock);
777 # if defined(GC_PTHREADS)
778 pthread_mutex_unlock(&incr_lock);
780 # ifdef GC_WIN32_THREADS
781 LeaveCriticalSection(&incr_cs);
788 # define MAX_FINALIZED 8000
791 GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
793 /* Too big for THINK_C. have to allocate it dynamically. */
794 GC_word *live_indicators = 0;
797 int live_indicators_count = 0;
802 # ifdef THREAD_LOCAL_ALLOC
803 tn * result = (tn *)GC_LOCAL_MALLOC(sizeof(tn));
805 tn * result = (tn *)GC_MALLOC(sizeof(tn));
809 # ifdef THREAD_LOCAL_ALLOC
810 /* Minimally exercise thread local allocation */
812 char * result = (char *)GC_LOCAL_MALLOC_ATOMIC(17);
813 memset(result, 'a', 17);
815 # endif /* THREAD_LOCAL_ALLOC */
817 /* get around static data limitations. */
818 if (!live_indicators)
820 (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
821 if (!live_indicators) {
822 (void)GC_printf0("Out of memory\n");
826 if (n == 0) return(0);
828 (void)GC_printf0("Out of memory\n");
832 result -> lchild = mktree(n-1);
833 result -> rchild = mktree(n-1);
834 if (counter++ % 17 == 0 && n >= 2) {
835 tn * tmp = result -> lchild -> rchild;
837 result -> lchild -> rchild = result -> rchild -> lchild;
838 result -> rchild -> lchild = tmp;
840 if (counter++ % 119 == 0) {
845 PCR_ThCrSec_EnterSys();
847 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
848 static mutex_t incr_lock;
849 mutex_lock(&incr_lock);
851 # if defined(GC_PTHREADS)
852 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
853 pthread_mutex_lock(&incr_lock);
855 # ifdef GC_WIN32_THREADS
856 EnterCriticalSection(&incr_cs);
859 /* Losing a count here causes erroneous report of failure. */
861 my_index = live_indicators_count++;
863 PCR_ThCrSec_ExitSys();
865 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
866 mutex_unlock(&incr_lock);
868 # if defined(GC_PTHREADS)
869 pthread_mutex_unlock(&incr_lock);
871 # ifdef GC_WIN32_THREADS
872 LeaveCriticalSection(&incr_cs);
877 GC_REGISTER_FINALIZER((GC_PTR)result, finalizer, (GC_PTR)(GC_word)n,
878 (GC_finalization_proc *)0, (GC_PTR *)0);
879 if (my_index >= MAX_FINALIZED) {
880 GC_printf0("live_indicators overflowed\n");
883 live_indicators[my_index] = 13;
884 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
885 (GC_PTR *)(&(live_indicators[my_index])),
886 (GC_PTR)result) != 0) {
887 GC_printf0("GC_general_register_disappearing_link failed\n");
890 if (GC_unregister_disappearing_link(
892 (&(live_indicators[my_index]))) == 0) {
893 GC_printf0("GC_unregister_disappearing_link failed\n");
896 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
897 (GC_PTR *)(&(live_indicators[my_index])),
898 (GC_PTR)result) != 0) {
899 GC_printf0("GC_general_register_disappearing_link failed 2\n");
910 if (n == 0 && t != 0) {
911 (void)GC_printf0("Clobbered a leaf - collector is broken\n");
915 if (t -> level != n) {
916 (void)GC_printf1("Lost a node at level %lu - collector is broken\n",
920 if (counter++ % 373 == 0) {
922 (void) GC_MALLOC(counter%5001);
924 chktree(t -> lchild, n-1);
925 if (counter++ % 73 == 0) {
927 (void) GC_MALLOC(counter%373);
929 chktree(t -> rchild, n-1);
932 # if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
937 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
939 return(GC_MALLOC(8));
941 void ** my_free_list_ptr;
944 if (thr_getspecific(fl_key, (void **)(&my_free_list_ptr)) != 0) {
945 (void)GC_printf0("thr_getspecific failed\n");
948 if (my_free_list_ptr == 0) {
949 uncollectable_count++;
950 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
951 if (thr_setspecific(fl_key, my_free_list_ptr) != 0) {
952 (void)GC_printf0("thr_setspecific failed\n");
956 my_free_list = *my_free_list_ptr;
957 if (my_free_list == 0) {
959 my_free_list = GC_malloc_many(8);
960 if (my_free_list == 0) {
961 (void)GC_printf0("alloc8bytes out of memory\n");
965 *my_free_list_ptr = GC_NEXT(my_free_list);
966 GC_NEXT(my_free_list) = 0;
967 return(my_free_list);
973 # if defined(GC_PTHREADS)
974 pthread_key_t fl_key;
978 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
980 return(GC_MALLOC(8));
982 void ** my_free_list_ptr;
985 my_free_list_ptr = (void **)pthread_getspecific(fl_key);
986 if (my_free_list_ptr == 0) {
987 uncollectable_count++;
988 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
989 if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
990 (void)GC_printf0("pthread_setspecific failed\n");
994 my_free_list = *my_free_list_ptr;
995 if (my_free_list == 0) {
996 my_free_list = GC_malloc_many(8);
997 if (my_free_list == 0) {
998 (void)GC_printf0("alloc8bytes out of memory\n");
1002 *my_free_list_ptr = GC_NEXT(my_free_list);
1003 GC_NEXT(my_free_list) = 0;
1004 collectable_count++;
1005 return(my_free_list);
1010 # define alloc8bytes() GC_MALLOC_ATOMIC(8)
1019 for (i = 0; i < n; i += 8) {
1021 if (alloc8bytes() == 0) {
1022 (void)GC_printf0("Out of memory\n");
1028 # if defined(THREADS) && defined(GC_DEBUG)
1029 # ifdef VERY_SMALL_CONFIG
1030 # define TREE_HEIGHT 12
1032 # define TREE_HEIGHT 15
1035 # ifdef VERY_SMALL_CONFIG
1036 # define TREE_HEIGHT 13
1038 # define TREE_HEIGHT 16
1046 root = mktree(TREE_HEIGHT);
1047 # ifndef VERY_SMALL_CONFIG
1048 alloc_small(5000000);
1050 chktree(root, TREE_HEIGHT);
1051 if (finalized_count && ! dropped_something) {
1052 (void)GC_printf0("Premature finalization - collector is broken\n");
1055 dropped_something = 1;
1056 GC_noop(root); /* Root needs to remain live until */
1057 /* dropped_something is set. */
1058 root = mktree(TREE_HEIGHT);
1059 chktree(root, TREE_HEIGHT);
1060 for (i = TREE_HEIGHT; i >= 0; i--) {
1064 # ifndef VERY_SMALL_CONFIG
1065 alloc_small(5000000);
1069 unsigned n_tests = 0;
1071 GC_word bm_huge[10] = {
1084 /* A very simple test of explicitly typed allocation */
1087 GC_word * old, * new;
1090 GC_word bm_large = 0xf7ff7fff;
1091 GC_descr d1 = GC_make_descriptor(&bm3, 2);
1092 GC_descr d2 = GC_make_descriptor(&bm2, 2);
1094 GC_descr dummy = GC_make_descriptor(&bm_large, 32);
1096 GC_descr d3 = GC_make_descriptor(&bm_large, 32);
1097 GC_descr d4 = GC_make_descriptor(bm_huge, 320);
1098 GC_word * x = (GC_word *)GC_malloc_explicitly_typed(2000, d4);
1101 collectable_count++;
1103 for (i = 0; i < 4000; i++) {
1104 collectable_count++;
1105 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
1106 if (0 != new[0] || 0 != new[1]) {
1107 GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1111 new[1] = (GC_word)old;
1113 collectable_count++;
1114 new = (GC_word *) GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
1116 new[1] = (GC_word)old;
1118 collectable_count++;
1119 new = (GC_word *) GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
1121 new[1] = (GC_word)old;
1123 collectable_count++;
1124 new = (GC_word *) GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
1127 new[1] = (GC_word)old;
1129 collectable_count++;
1131 new = (GC_word *) GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
1134 new = (GC_word *) GC_calloc_explicitly_typed(1001,
1135 3 * sizeof(GC_word),
1137 if (0 != new[0] || 0 != new[1]) {
1138 GC_printf0("Bad initialization by GC_malloc_explicitly_typed\n");
1143 new[1] = (GC_word)old;
1146 for (i = 0; i < 20000; i++) {
1148 (void)GC_printf1("typed alloc failed at %lu\n",
1154 new = (GC_word *)(old[1]);
1173 void fail_proc1(GC_PTR x)
1178 static void uniq(void *p, ...) {
1184 for (;(q[n] = va_arg(a,void *));n++) ;
1190 "Apparently failed to mark form some function arguments.\n"
1191 "Perhaps GC_push_regs was configured incorrectly?\n"
1197 #endif /* __STDC__ */
1200 # define TEST_FAIL_COUNT(n) 1
1202 # define TEST_FAIL_COUNT(n) (fail_count >= (n))
1211 char *y = (char *)(size_t)fail_proc1;
1217 "This test program is not designed for leak detection mode\n");
1218 (void)GC_printf0("Expect lots of problems.\n");
1221 # ifndef DBG_HDRS_ALL
1222 collectable_count += 3;
1223 if (GC_size(GC_malloc(7)) != 8 &&
1224 GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word)
1225 || GC_size(GC_malloc(15)) != 16) {
1226 (void)GC_printf0("GC_size produced unexpected results\n");
1229 collectable_count += 1;
1230 if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1231 (void)GC_printf1("GC_malloc(0) failed: GC_size returns %ld\n",
1232 GC_size(GC_malloc(0)));
1235 collectable_count += 1;
1236 if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1237 (void)GC_printf0("GC_malloc_uncollectable(0) failed\n");
1240 GC_is_valid_displacement_print_proc = fail_proc1;
1241 GC_is_visible_print_proc = fail_proc1;
1242 collectable_count += 1;
1244 if (GC_base(x + 13) != x) {
1245 (void)GC_printf0("GC_base(heap ptr) produced incorrect result\n");
1249 if (GC_base(y) != 0) {
1250 (void)GC_printf0("GC_base(fn_ptr) produced incorrect result\n");
1254 if (GC_same_obj(x+5, x) != x + 5) {
1255 (void)GC_printf0("GC_same_obj produced incorrect result\n");
1258 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1259 (void)GC_printf0("GC_is_visible produced incorrect result\n");
1262 if (!TEST_FAIL_COUNT(1)) {
1263 # if!(defined(RS6000) || defined(POWERPC) || defined(IA64)) || defined(M68K)
1264 /* ON RS6000s function pointers point to a descriptor in the */
1265 /* data segment, so there should have been no failures. */
1266 /* The same applies to IA64. Something similar seems to */
1267 /* be going on with NetBSD/M68K. */
1268 (void)GC_printf0("GC_is_visible produced wrong failure indication\n");
1272 if (GC_is_valid_displacement(y) != y
1273 || GC_is_valid_displacement(x) != x
1274 || GC_is_valid_displacement(x + 3) != x + 3) {
1276 "GC_is_valid_displacement produced incorrect result\n");
1279 # if defined(__STDC__) && !defined(MSWIN32) && !defined(MSWINCE)
1280 /* Harder to test under Windows without a gc.h declaration. */
1283 extern void *GC_memalign();
1286 for (i = sizeof(GC_word); i < 512; i *= 2) {
1287 GC_word result = (GC_word) GC_memalign(i, 17);
1288 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1292 # ifndef ALL_INTERIOR_POINTERS
1293 # if defined(RS6000) || defined(POWERPC)
1294 if (!TEST_FAIL_COUNT(1)) {
1296 if (GC_all_interior_pointers && !TEST_FAIL_COUNT(1)
1297 || !GC_all_interior_pointers && !TEST_FAIL_COUNT(2)) {
1299 (void)GC_printf0("GC_is_valid_displacement produced wrong failure indication\n");
1303 # endif /* DBG_HDRS_ALL */
1304 /* Test floating point alignment */
1305 collectable_count += 2;
1306 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1307 *(double *)GC_MALLOC(sizeof(double)) = 1.0;
1308 # ifdef GC_GCJ_SUPPORT
1309 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1310 GC_init_gcj_malloc(0, (void *)fake_gcj_mark_proc);
1312 /* Make sure that fn arguments are visible to the collector. */
1315 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1316 (GC_gcollect(),GC_malloc(12)),
1317 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1318 (GC_gcollect(),GC_malloc(12)),
1319 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1320 (GC_gcollect(),GC_malloc(12)),
1321 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1322 (GC_gcollect(),GC_malloc(12)),
1323 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1324 (GC_gcollect(),GC_malloc(12)),
1327 /* Repeated list reversal test. */
1330 GC_printf0("-------------Finished reverse_test\n");
1332 # ifndef DBG_HDRS_ALL
1335 GC_printf0("-------------Finished typed_test\n");
1337 # endif /* DBG_HDRS_ALL */
1342 # if defined(THREADS) && defined(HANDLE_FORK)
1345 tiny_reverse_test(0);
1347 GC_printf0("Finished a child process\n");
1351 /* GC_printf1("Finished %x\n", pthread_self()); */
1354 void check_heap_stats()
1356 unsigned long max_heap_sz;
1359 int late_finalize_count = 0;
1361 # ifdef VERY_SMALL_CONFIG
1362 /* these are something of a guess */
1363 if (sizeof(char *) > 4) {
1364 max_heap_sz = 4500000;
1366 max_heap_sz = 2800000;
1369 if (sizeof(char *) > 4) {
1370 max_heap_sz = 19000000;
1372 max_heap_sz = 11000000;
1375 # ifndef ALIGN_DOUBLE
1376 /* We end up needing more small object pages. */
1377 max_heap_sz += 2000000;
1381 # ifdef SAVE_CALL_CHAIN
1383 # ifdef SAVE_CALL_COUNT
1384 max_heap_sz += max_heap_sz * SAVE_CALL_COUNT/4;
1388 /* Garbage collect repeatedly so that all inaccessible objects */
1389 /* can be finalized. */
1390 while (GC_collect_a_little()) { }
1391 for (i = 0; i < 16; i++) {
1393 late_finalize_count += GC_invoke_finalizers();
1395 (void)GC_printf1("Completed %lu tests\n", (unsigned long)n_tests);
1396 (void)GC_printf1("Allocated %lu collectable objects\n", (unsigned long)collectable_count);
1397 (void)GC_printf1("Allocated %lu uncollectable objects\n", (unsigned long)uncollectable_count);
1398 (void)GC_printf1("Allocated %lu atomic objects\n", (unsigned long)atomic_count);
1399 (void)GC_printf1("Allocated %lu stubborn objects\n", (unsigned long)stubborn_count);
1400 (void)GC_printf2("Finalized %lu/%lu objects - ",
1401 (unsigned long)finalized_count,
1402 (unsigned long)finalizable_count);
1403 # ifdef FINALIZE_ON_DEMAND
1404 if (finalized_count != late_finalize_count) {
1405 (void)GC_printf0("Demand finalization error\n");
1409 if (finalized_count > finalizable_count
1410 || finalized_count < finalizable_count/2) {
1411 (void)GC_printf0("finalization is probably broken\n");
1414 (void)GC_printf0("finalization is probably ok\n");
1417 for (i = 0; i < MAX_FINALIZED; i++) {
1418 if (live_indicators[i] != 0) {
1422 i = finalizable_count - finalized_count - still_live;
1425 ("%lu disappearing links remain and %ld more objects were not finalized\n",
1426 (unsigned long) still_live, (long)i);
1428 GC_printf0("\tVery suspicious!\n");
1430 GC_printf0("\tSlightly suspicious, but probably OK.\n");
1433 (void)GC_printf1("Total number of bytes allocated is %lu\n",
1435 WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc));
1436 (void)GC_printf1("Final heap size is %lu bytes\n",
1437 (unsigned long)GC_get_heap_size());
1438 if (WORDS_TO_BYTES(GC_words_allocd + GC_words_allocd_before_gc)
1439 # ifdef VERY_SMALL_CONFIG
1440 < 2700000*n_tests) {
1442 < 33500000*n_tests) {
1444 (void)GC_printf0("Incorrect execution - missed some allocations\n");
1447 if (GC_get_heap_size() > max_heap_sz*n_tests) {
1448 (void)GC_printf0("Unexpected heap growth - collector may be broken\n");
1451 (void)GC_printf0("Collector appears to work\n");
1455 void SetMinimumStack(long minSize)
1459 if (minSize > LMGetDefltStack())
1461 newApplLimit = (long) GetApplLimit()
1462 - (minSize - LMGetDefltStack());
1463 SetApplLimit((Ptr) newApplLimit);
1468 #define cMinStackSpace (512L * 1024L)
1473 void warn_proc(char *msg, GC_word p)
1475 void warn_proc(msg, p)
1480 GC_printf1(msg, (unsigned long)p);
1485 #if !defined(PCR) && !defined(GC_SOLARIS_THREADS) \
1486 && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS) \
1488 #if defined(MSWIN32) && !defined(__MINGW32__)
1489 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPTSTR cmd, int n)
1500 /* No good way to determine stack base from library; do it */
1501 /* manually on this platform. */
1502 GC_stackbottom = (GC_PTR)(&dummy);
1505 /* Make sure we have lots and lots of stack space. */
1506 SetMinimumStack(cMinStackSpace);
1507 /* Cheat and let stdio initialize toolbox for us. */
1508 printf("Testing GC Macintosh port.\n");
1510 GC_INIT(); /* Only needed on a few platforms. */
1511 (void) GC_set_warn_proc(warn_proc);
1512 # if (defined(MPROTECT_VDB) || defined(PROC_VDB)) \
1513 && !defined(MAKE_BACK_GRAPH)
1514 GC_enable_incremental();
1515 (void) GC_printf0("Switched to incremental mode\n");
1516 # if defined(MPROTECT_VDB)
1517 (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
1520 (void)GC_printf0("Reading dirty bits from /proc\n");
1522 (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
1529 (void)fflush(stdout);
1532 /* Entry points we should be testing, but aren't. */
1533 /* Some can be tested by defining GC_DEBUG at the top of this file */
1534 /* This is a bit SunOS4 specific. */
1535 GC_noop(GC_expand_hp, GC_add_roots, GC_clear_roots,
1536 GC_register_disappearing_link,
1537 GC_register_finalizer_ignore_self,
1538 GC_debug_register_displacement,
1539 GC_print_obj, GC_debug_change_stubborn,
1540 GC_debug_end_stubborn_change, GC_debug_malloc_uncollectable,
1541 GC_debug_free, GC_debug_realloc, GC_generic_malloc_words_small,
1542 GC_init, GC_make_closure, GC_debug_invoke_finalizer,
1543 GC_page_was_ever_dirty, GC_is_fresh,
1544 GC_malloc_ignore_off_page, GC_malloc_atomic_ignore_off_page,
1545 GC_set_max_heap_size, GC_get_bytes_since_gc,
1546 GC_get_total_bytes, GC_pre_incr, GC_post_incr);
1549 GC_win32_free_heap();
1555 #if defined(GC_WIN32_THREADS) && !defined(CYGWIN32)
1557 DWORD __stdcall thr_run_one_test(void *arg)
1564 HANDLE win_created_h;
1567 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
1572 GC_printf0("Received WM_HIBERNATE, calling GC_gcollect\n");
1576 GC_printf0("Received WM_CLOSE, closing window\n");
1577 DestroyWindow(hwnd);
1583 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
1589 DWORD __stdcall thr_window(void *arg)
1591 WNDCLASS win_class = {
1596 GetModuleHandle(NULL),
1599 (HBRUSH)(COLOR_APPWORKSPACE+1),
1605 if (!RegisterClass(&win_class))
1608 win_handle = CreateWindowEx(
1613 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
1616 GetModuleHandle(NULL),
1619 if (win_handle == NULL)
1622 SetEvent(win_created_h);
1624 ShowWindow(win_handle, SW_SHOW);
1625 UpdateWindow(win_handle);
1627 while (GetMessage(&msg, NULL, 0, 0)) {
1628 TranslateMessage(&msg);
1629 DispatchMessage(&msg);
1639 int APIENTRY GC_WinMain(HINSTANCE instance, HINSTANCE prev, LPWSTR cmd, int n)
1641 int APIENTRY WinMain(HINSTANCE instance, HINSTANCE prev, LPSTR cmd, int n)
1653 GC_enable_incremental();
1656 InitializeCriticalSection(&incr_cs);
1657 (void) GC_set_warn_proc(warn_proc);
1659 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
1660 if (win_created_h == (HANDLE)NULL) {
1661 (void)GC_printf1("Event creation failed %lu\n", (unsigned long)GetLastError());
1664 win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
1665 if (win_thr_h == (HANDLE)NULL) {
1666 (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1669 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
1671 CloseHandle(win_created_h);
1674 for (i = 0; i < NTEST; i++) {
1675 h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
1676 if (h[i] == (HANDLE)NULL) {
1677 (void)GC_printf1("Thread creation failed %lu\n", (unsigned long)GetLastError());
1681 # endif /* NTEST > 0 */
1684 for (i = 0; i < NTEST; i++) {
1685 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
1686 (void)GC_printf1("Thread wait failed %lu\n", (unsigned long)GetLastError());
1690 # endif /* NTEST > 0 */
1692 PostMessage(win_handle, WM_CLOSE, 0, 0);
1693 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
1700 #endif /* GC_WIN32_THREADS */
1712 /* GC_enable_incremental(); */
1713 (void) GC_set_warn_proc(warn_proc);
1714 th1 = PCR_Th_Fork(run_one_test, 0);
1715 th2 = PCR_Th_Fork(run_one_test, 0);
1717 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1718 != PCR_ERes_okay || code != 0) {
1719 (void)GC_printf0("Thread 1 failed\n");
1721 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
1722 != PCR_ERes_okay || code != 0) {
1723 (void)GC_printf0("Thread 2 failed\n");
1730 #if defined(GC_SOLARIS_THREADS) || defined(GC_PTHREADS)
1731 void * thr_run_one_test(void * arg)
1738 # define GC_free GC_debug_free
1741 #if defined(GC_SOLARIS_THREADS) && !defined(GC_SOLARIS_PTHREADS)
1750 GC_INIT(); /* Only needed if gc is dynamic library. */
1751 # ifndef MAKE_BACK_GRAPH
1752 GC_enable_incremental();
1754 (void) GC_set_warn_proc(warn_proc);
1755 if (thr_keycreate(&fl_key, GC_free) != 0) {
1756 (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1759 if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, 0, &th1)) != 0) {
1760 (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1763 if ((code = thr_create(0, 1024*1024, thr_run_one_test, 0, THR_NEW_LWP, &th2)) != 0) {
1764 (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1768 if ((code = thr_join(th1, 0, 0)) != 0) {
1769 (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1772 if (thr_join(th2, 0, 0) != 0) {
1773 (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1777 (void)fflush(stdout);
1780 #else /* pthreads */
1791 pthread_attr_t attr;
1794 # ifdef GC_IRIX_THREADS
1795 /* Force a larger stack to be preallocated */
1796 /* Since the initial cant always grow later. */
1797 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 Mb */
1798 # endif /* GC_IRIX_THREADS */
1799 # if defined(GC_HPUX_THREADS)
1800 /* Default stack size is too small, especially with the 64 bit ABI */
1802 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
1803 (void)GC_printf0("pthread_default_stacksize_np failed.\n");
1805 # endif /* GC_HPUX_THREADS */
1808 pthread_attr_init(&attr);
1809 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
1810 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS)
1811 pthread_attr_setstacksize(&attr, 1000000);
1814 # if (defined(MPROTECT_VDB)) \
1815 && !defined(PARALLEL_MARK) &&!defined(REDIRECT_MALLOC) \
1816 && !defined(MAKE_BACK_GRAPH)
1817 GC_enable_incremental();
1818 (void) GC_printf0("Switched to incremental mode\n");
1819 # if defined(MPROTECT_VDB)
1820 (void)GC_printf0("Emulating dirty bits with mprotect/signals\n");
1823 (void)GC_printf0("Reading dirty bits from /proc\n");
1825 (void)GC_printf0("Using DEFAULT_VDB dirty bit implementation\n");
1829 (void) GC_set_warn_proc(warn_proc);
1830 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
1831 (void)GC_printf1("Key creation failed %lu\n", (unsigned long)code);
1834 if ((code = pthread_create(&th1, &attr, thr_run_one_test, 0)) != 0) {
1835 (void)GC_printf1("Thread 1 creation failed %lu\n", (unsigned long)code);
1838 if ((code = pthread_create(&th2, &attr, thr_run_one_test, 0)) != 0) {
1839 (void)GC_printf1("Thread 2 creation failed %lu\n", (unsigned long)code);
1843 if ((code = pthread_join(th1, 0)) != 0) {
1844 (void)GC_printf1("Thread 1 failed %lu\n", (unsigned long)code);
1847 if (pthread_join(th2, 0) != 0) {
1848 (void)GC_printf1("Thread 2 failed %lu\n", (unsigned long)code);
1852 (void)fflush(stdout);
1853 pthread_attr_destroy(&attr);
1854 GC_printf1("Completed %d collections\n", GC_gc_no);
1857 #endif /* GC_PTHREADS */
1858 #endif /* GC_SOLARIS_THREADS || GC_PTHREADS */