Debian Codegraph @

external/bdwgc/tests/test.c

raw
1 /*
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.
5 *
6 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
7 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 *
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.
14 */
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. */
20
21 # ifdef HAVE_CONFIG_H
22 # include "config.h"
23 # endif
24
25 # undef GC_BUILD
26
27 #if (defined(DBG_HDRS_ALL) || defined(MAKE_BACK_GRAPH)) \
28 && !defined(GC_DEBUG) && !defined(CPPCHECK)
29 # define GC_DEBUG
30 #endif
31
32 #ifdef DEFAULT_VDB /* specified manually (e.g. passed to CFLAGS) */
33 # define TEST_DEFAULT_VDB
34 #endif
35
36 #if defined(CPPCHECK) && defined(GC_PTHREADS) && !defined(_GNU_SOURCE)
37 # define _GNU_SOURCE 1
38 #endif
39 #undef GC_NO_THREAD_REDIRECTS
40 #include "gc.h"
41
42 #ifndef NTHREADS /* Number of additional threads to fork. */
43 # define NTHREADS 5 /* excludes main thread, which also runs a test. */
44 /* Not respected by PCR test. */
45 #endif
46
47 # if defined(mips) && defined(SYSTYPE_BSD43)
48 /* MIPS RISCOS 4 */
49 # else
50 # include <stdlib.h>
51 # endif
52 # include <stdio.h>
53 # if defined(_WIN32_WCE) && !defined(__GNUC__)
54 # include <winbase.h>
55 /* # define assert ASSERT */
56 # else
57 # include <assert.h> /* Not normally used, but handy for debugging. */
58 # endif
59
60 # include "gc_typed.h"
61 # include "private/gc_priv.h" /* For output, locking, MIN_WORDS, */
62 /* some statistics and gcconfig.h. */
63
64 # if defined(MSWIN32) || defined(MSWINCE)
65 # include <windows.h>
66 # endif
67
68 #ifdef GC_PRINT_VERBOSE_STATS
69 # define print_stats VERBOSE
70 # define INIT_PRINT_STATS /* empty */
71 #else
72 /* Use own variable as GC_print_stats might not be exported. */
73 static int print_stats = 0;
74 # ifdef GC_READ_ENV_FILE
75 /* GETENV uses GC internal function in this case. */
76 # define INIT_PRINT_STATS /* empty */
77 # else
78 # define INIT_PRINT_STATS \
79 { \
80 if (0 != GETENV("GC_PRINT_VERBOSE_STATS")) \
81 print_stats = VERBOSE; \
82 else if (0 != GETENV("GC_PRINT_STATS")) \
83 print_stats = 1; \
84 }
85 # endif
86 #endif /* !GC_PRINT_VERBOSE_STATS */
87
88 # ifdef PCR
89 # include "th/PCR_ThCrSec.h"
90 # include "th/PCR_Th.h"
91 # define GC_printf printf
92 # endif
93
94 # if defined(GC_PTHREADS) && !defined(GC_WIN32_PTHREADS)
95 # include <pthread.h>
96 # else
97 # define NO_TEST_HANDLE_FORK
98 # endif
99
100 # if (!defined(THREADS) || !defined(HANDLE_FORK) \
101 || (defined(DARWIN) && defined(MPROTECT_VDB) \
102 && !defined(NO_INCREMENTAL) && !defined(MAKE_BACK_GRAPH))) \
103 && !defined(NO_TEST_HANDLE_FORK) && !defined(TEST_HANDLE_FORK) \
104 && !defined(TEST_FORK_WITHOUT_ATFORK)
105 # define NO_TEST_HANDLE_FORK
106 # endif
107
108 # ifndef NO_TEST_HANDLE_FORK
109 # include <unistd.h>
110 # include <sys/types.h>
111 # include <sys/wait.h>
112 # if defined(HANDLE_FORK) && defined(CAN_CALL_ATFORK)
113 # define INIT_FORK_SUPPORT GC_set_handle_fork(1)
114 /* Causes abort in GC_init on pthread_atfork failure. */
115 # elif !defined(TEST_FORK_WITHOUT_ATFORK)
116 # define INIT_FORK_SUPPORT GC_set_handle_fork(-1)
117 /* Passing -1 implies fork() should be as well manually */
118 /* surrounded with GC_atfork_prepare/parent/child. */
119 # endif
120 # endif
121
122 # ifndef INIT_FORK_SUPPORT
123 # define INIT_FORK_SUPPORT /* empty */
124 # endif
125
126 #ifdef PCR
127 # define FINALIZER_LOCK() PCR_ThCrSec_EnterSys()
128 # define FINALIZER_UNLOCK() PCR_ThCrSec_ExitSys()
129 #elif defined(GC_PTHREADS)
130 static pthread_mutex_t incr_lock = PTHREAD_MUTEX_INITIALIZER;
131 # define FINALIZER_LOCK() pthread_mutex_lock(&incr_lock)
132 # define FINALIZER_UNLOCK() pthread_mutex_unlock(&incr_lock)
133 #elif defined(GC_WIN32_THREADS)
134 static CRITICAL_SECTION incr_cs;
135 # define FINALIZER_LOCK() EnterCriticalSection(&incr_cs)
136 # define FINALIZER_UNLOCK() LeaveCriticalSection(&incr_cs)
137 #else
138 # define FINALIZER_LOCK() (void)0
139 # define FINALIZER_UNLOCK() (void)0
140 #endif /* !THREADS */
141
142 #include <stdarg.h>
143
144 #define CHECK_GCLIB_VERSION \
145 if (GC_get_version() != ((GC_VERSION_MAJOR<<16) \
146 | (GC_VERSION_MINOR<<8) \
147 | GC_VERSION_MICRO)) { \
148 GC_printf("libgc version mismatch\n"); \
149 exit(1); \
150 }
151
152 /* Call GC_INIT only on platforms on which we think we really need it, */
153 /* so that we can test automatic initialization on the rest. */
154 #if defined(TEST_EXPLICIT_GC_INIT) || defined(AIX) || defined(CYGWIN32) \
155 || defined(DARWIN) || defined(HOST_ANDROID) \
156 || (defined(MSWINCE) && !defined(GC_WINMAIN_REDIRECT))
157 # define GC_OPT_INIT GC_INIT()
158 #else
159 # define GC_OPT_INIT /* empty */
160 #endif
161
162 #ifdef NO_CLOCK
163 # define INIT_PERF_MEASUREMENT (void)0
164 #else
165 # define INIT_PERF_MEASUREMENT GC_start_performance_measurement()
166 #endif
167
168 #define GC_COND_INIT() \
169 INIT_FORK_SUPPORT; GC_OPT_INIT; CHECK_GCLIB_VERSION; \
170 INIT_PRINT_STATS; INIT_PERF_MEASUREMENT
171
172 #define CHECK_OUT_OF_MEMORY(p) \
173 if ((p) == NULL) { \
174 GC_printf("Out of memory\n"); \
175 exit(1); \
176 }
177
178 /* Define AO primitives for a single-threaded mode. */
179 #ifndef AO_CLEAR
180 /* AO_t not defined. */
181 # define AO_t GC_word
182 #endif
183 #ifndef AO_HAVE_load_acquire
184 static AO_t AO_load_acquire(const volatile AO_t *addr)
185 {
186 AO_t result;
187
188 FINALIZER_LOCK();
189 result = *addr;
190 FINALIZER_UNLOCK();
191 return result;
192 }
193 #endif
194 #ifndef AO_HAVE_store_release
195 /* Not a macro as new_val argument should be evaluated before the lock. */
196 static void AO_store_release(volatile AO_t *addr, AO_t new_val)
197 {
198 FINALIZER_LOCK();
199 *addr = new_val;
200 FINALIZER_UNLOCK();
201 }
202 #endif
203 #ifndef AO_HAVE_fetch_and_add1
204 # define AO_fetch_and_add1(p) ((*(p))++)
205 /* This is used only to update counters. */
206 #endif
207
208 /* Allocation Statistics. Synchronization is not strictly necessary. */
209 volatile AO_t uncollectable_count = 0;
210 volatile AO_t collectable_count = 0;
211 volatile AO_t atomic_count = 0;
212 volatile AO_t realloc_count = 0;
213
214 volatile AO_t extra_count = 0; /* Amount of space wasted in cons node; */
215 /* also used in gcj_cons, mktree and */
216 /* chktree (for other purposes). */
217
218 #if defined(GC_AMIGA_FASTALLOC) && defined(AMIGA)
219 EXTERN_C_BEGIN
220 void GC_amiga_free_all_mem(void);
221 EXTERN_C_END
222
223 void Amiga_Fail(void){GC_amiga_free_all_mem();abort();}
224 # define FAIL Amiga_Fail()
225 void *GC_amiga_gctest_malloc_explicitly_typed(size_t lb, GC_descr d){
226 void *ret=GC_malloc_explicitly_typed(lb,d);
227 if(ret==NULL){
228 GC_gcollect();
229 ret=GC_malloc_explicitly_typed(lb,d);
230 if(ret==NULL){
231 GC_printf("Out of memory, (typed allocations are not directly "
232 "supported with the GC_AMIGA_FASTALLOC option.)\n");
233 FAIL;
234 }
235 }
236 return ret;
237 }
238 void *GC_amiga_gctest_calloc_explicitly_typed(size_t a,size_t lb, GC_descr d){
239 void *ret=GC_calloc_explicitly_typed(a,lb,d);
240 if(ret==NULL){
241 GC_gcollect();
242 ret=GC_calloc_explicitly_typed(a,lb,d);
243 if(ret==NULL){
244 GC_printf("Out of memory, (typed allocations are not directly "
245 "supported with the GC_AMIGA_FASTALLOC option.)\n");
246 FAIL;
247 }
248 }
249 return ret;
250 }
251 # define GC_malloc_explicitly_typed(a,b) GC_amiga_gctest_malloc_explicitly_typed(a,b)
252 # define GC_calloc_explicitly_typed(a,b,c) GC_amiga_gctest_calloc_explicitly_typed(a,b,c)
253
254 #else /* !AMIGA_FASTALLOC */
255
256 # if defined(PCR) || defined(LINT2)
257 # define FAIL abort()
258 # else
259 # define FAIL ABORT("Test failed")
260 # endif
261
262 #endif /* !AMIGA_FASTALLOC */
263
264 /* AT_END may be defined to exercise the interior pointer test */
265 /* if the collector is configured with ALL_INTERIOR_POINTERS. */
266 /* As it stands, this test should succeed with either */
267 /* configuration. In the FIND_LEAK configuration, it should */
268 /* find lots of leaks, since we free almost nothing. */
269
270 struct SEXPR {
271 struct SEXPR * sexpr_car;
272 struct SEXPR * sexpr_cdr;
273 };
274
275
276 typedef struct SEXPR * sexpr;
277
278 # define INT_TO_SEXPR(x) ((sexpr)(GC_word)(x))
279 # define SEXPR_TO_INT(x) ((int)(GC_word)(x))
280
281 # undef nil
282 # define nil (INT_TO_SEXPR(0))
283 # define car(x) ((x) -> sexpr_car)
284 # define cdr(x) ((x) -> sexpr_cdr)
285 # define is_nil(x) ((x) == nil)
286
287 /* Silly implementation of Lisp cons. Intentionally wastes lots of space */
288 /* to test collector. */
289 # ifdef VERY_SMALL_CONFIG
290 # define cons small_cons
291 # else
292 sexpr cons (sexpr x, sexpr y)
293 {
294 sexpr r;
295 int *p;
296 unsigned my_extra = (unsigned)AO_fetch_and_add1(&extra_count) % 5000;
297
298 r = (sexpr)GC_MALLOC(sizeof(struct SEXPR) + my_extra);
299 CHECK_OUT_OF_MEMORY(r);
300 AO_fetch_and_add1(&collectable_count);
301 for (p = (int *)r;
302 (word)p < (word)r + my_extra + sizeof(struct SEXPR); p++) {
303 if (*p) {
304 GC_printf("Found nonzero at %p - allocator is broken\n",
305 (void *)p);
306 FAIL;
307 }
308 *p = (int)((13 << 12) + ((p - (int *)r) & 0xfff));
309 }
310 # ifdef AT_END
311 r = (sexpr)((char *)r + (my_extra & ~7));
312 # endif
313 r -> sexpr_car = x;
314 r -> sexpr_cdr = y;
315 GC_END_STUBBORN_CHANGE(r);
316 return(r);
317 }
318 # endif
319
320 #include "gc_mark.h"
321
322 #ifdef GC_GCJ_SUPPORT
323
324 #include "gc_gcj.h"
325
326 /* The following struct emulates the vtable in gcj. */
327 /* This assumes the default value of MARK_DESCR_OFFSET. */
328 struct fake_vtable {
329 void * dummy; /* class pointer in real gcj. */
330 GC_word descr;
331 };
332
333 struct fake_vtable gcj_class_struct1 = { 0, sizeof(struct SEXPR)
334 + sizeof(struct fake_vtable *) };
335 /* length based descriptor. */
336 struct fake_vtable gcj_class_struct2 =
337 { 0, ((GC_word)3 << (CPP_WORDSZ - 3)) | GC_DS_BITMAP};
338 /* Bitmap based descriptor. */
339
340 struct GC_ms_entry * fake_gcj_mark_proc(word * addr,
341 struct GC_ms_entry *mark_stack_ptr,
342 struct GC_ms_entry *mark_stack_limit,
343 word env )
344 {
345 sexpr x;
346 if (1 == env) {
347 /* Object allocated with debug allocator. */
348 addr = (word *)GC_USR_PTR_FROM_BASE(addr);
349 }
350 x = (sexpr)(addr + 1); /* Skip the vtable pointer. */
351 mark_stack_ptr = GC_MARK_AND_PUSH(
352 (void *)(x -> sexpr_cdr), mark_stack_ptr,
353 mark_stack_limit, (void * *)&(x -> sexpr_cdr));
354 mark_stack_ptr = GC_MARK_AND_PUSH(
355 (void *)(x -> sexpr_car), mark_stack_ptr,
356 mark_stack_limit, (void * *)&(x -> sexpr_car));
357 return(mark_stack_ptr);
358 }
359
360 #endif /* GC_GCJ_SUPPORT */
361
362
363 sexpr small_cons (sexpr x, sexpr y)
364 {
365 sexpr r = GC_NEW(struct SEXPR);
366
367 CHECK_OUT_OF_MEMORY(r);
368 AO_fetch_and_add1(&collectable_count);
369 r -> sexpr_car = x;
370 r -> sexpr_cdr = y;
371 GC_END_STUBBORN_CHANGE(r);
372 return(r);
373 }
374
375 sexpr small_cons_uncollectable (sexpr x, sexpr y)
376 {
377 sexpr r = (sexpr)GC_MALLOC_UNCOLLECTABLE(sizeof(struct SEXPR));
378
379 CHECK_OUT_OF_MEMORY(r);
380 AO_fetch_and_add1(&uncollectable_count);
381 r -> sexpr_car = x;
382 r -> sexpr_cdr = (sexpr)(~(GC_word)y);
383 GC_END_STUBBORN_CHANGE(r);
384 return(r);
385 }
386
387 #ifdef GC_GCJ_SUPPORT
388 sexpr gcj_cons(sexpr x, sexpr y)
389 {
390 sexpr result;
391 GC_word * r = (GC_word *)GC_GCJ_MALLOC(
392 sizeof(struct SEXPR) + sizeof(struct fake_vtable*),
393 (AO_fetch_and_add1(&extra_count) & 1) != 0
394 ? &gcj_class_struct1
395 : &gcj_class_struct2);
396
397 CHECK_OUT_OF_MEMORY(r);
398 result = (sexpr)(r + 1);
399 result -> sexpr_car = x;
400 result -> sexpr_cdr = y;
401 GC_END_STUBBORN_CHANGE(r);
402 return(result);
403 }
404 #endif /* GC_GCJ_SUPPORT */
405
406 /* Return reverse(x) concatenated with y */
407 sexpr reverse1(sexpr x, sexpr y)
408 {
409 if (is_nil(x)) {
410 return(y);
411 } else {
412 return( reverse1(cdr(x), cons(car(x), y)) );
413 }
414 }
415
416 sexpr reverse(sexpr x)
417 {
418 # ifdef TEST_WITH_SYSTEM_MALLOC
419 GC_noop1(GC_HIDE_POINTER(malloc(100000)));
420 # endif
421 return( reverse1(x, nil) );
422 }
423
424 sexpr ints(int low, int up)
425 {
426 if (low > up) {
427 return(nil);
428 } else {
429 return(small_cons(small_cons(INT_TO_SEXPR(low), nil), ints(low+1, up)));
430 }
431 }
432
433 #ifdef GC_GCJ_SUPPORT
434 /* Return reverse(x) concatenated with y */
435 sexpr gcj_reverse1(sexpr x, sexpr y)
436 {
437 if (is_nil(x)) {
438 return(y);
439 } else {
440 return( gcj_reverse1(cdr(x), gcj_cons(car(x), y)) );
441 }
442 }
443
444 sexpr gcj_reverse(sexpr x)
445 {
446 return( gcj_reverse1(x, nil) );
447 }
448
449 sexpr gcj_ints(int low, int up)
450 {
451 if (low > up) {
452 return(nil);
453 } else {
454 return(gcj_cons(gcj_cons(INT_TO_SEXPR(low), nil), gcj_ints(low+1, up)));
455 }
456 }
457 #endif /* GC_GCJ_SUPPORT */
458
459 /* To check uncollectible allocation we build lists with disguised cdr */
460 /* pointers, and make sure they don't go away. */
461 sexpr uncollectable_ints(int low, int up)
462 {
463 if (low > up) {
464 return(nil);
465 } else {
466 return(small_cons_uncollectable(small_cons(INT_TO_SEXPR(low), nil),
467 uncollectable_ints(low+1, up)));
468 }
469 }
470
471 void check_ints(sexpr list, int low, int up)
472 {
473 if (is_nil(list)) {
474 GC_printf("list is nil\n");
475 FAIL;
476 }
477 if (SEXPR_TO_INT(car(car(list))) != low) {
478 GC_printf(
479 "List reversal produced incorrect list - collector is broken\n");
480 FAIL;
481 }
482 if (low == up) {
483 if (cdr(list) != nil) {
484 GC_printf("List too long - collector is broken\n");
485 FAIL;
486 }
487 } else {
488 check_ints(cdr(list), low+1, up);
489 }
490 }
491
492 # define UNCOLLECTABLE_CDR(x) (sexpr)(~(GC_word)(cdr(x)))
493
494 void check_uncollectable_ints(sexpr list, int low, int up)
495 {
496 if (SEXPR_TO_INT(car(car(list))) != low) {
497 GC_printf("Uncollectable list corrupted - collector is broken\n");
498 FAIL;
499 }
500 if (low == up) {
501 if (UNCOLLECTABLE_CDR(list) != nil) {
502 GC_printf("Uncollectable list too long - collector is broken\n");
503 FAIL;
504 }
505 } else {
506 check_uncollectable_ints(UNCOLLECTABLE_CDR(list), low+1, up);
507 }
508 }
509
510 /* Not used, but useful for debugging: */
511 void print_int_list(sexpr x)
512 {
513 if (is_nil(x)) {
514 GC_printf("NIL\n");
515 } else {
516 GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
517 if (!is_nil(cdr(x))) {
518 GC_printf(", ");
519 print_int_list(cdr(x));
520 } else {
521 GC_printf("\n");
522 }
523 }
524 }
525
526 /* ditto: */
527 void check_marks_int_list(sexpr x)
528 {
529 if (!GC_is_marked(x))
530 GC_printf("[unm:%p]", (void *)x);
531 else
532 GC_printf("[mkd:%p]", (void *)x);
533 if (is_nil(x)) {
534 GC_printf("NIL\n");
535 } else {
536 if (!GC_is_marked(car(x)))
537 GC_printf("[unm car:%p]", (void *)car(x));
538 GC_printf("(%d)", SEXPR_TO_INT(car(car(x))));
539 if (!is_nil(cdr(x))) {
540 GC_printf(", ");
541 check_marks_int_list(cdr(x));
542 } else {
543 GC_printf("\n");
544 }
545 }
546 }
547
548 /*
549 * A tiny list reversal test to check thread creation.
550 */
551 #ifdef THREADS
552
553 # ifdef VERY_SMALL_CONFIG
554 # define TINY_REVERSE_UPPER_VALUE 4
555 # else
556 # define TINY_REVERSE_UPPER_VALUE 10
557 # endif
558
559 # if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
560 DWORD __stdcall tiny_reverse_test(void * arg GC_ATTR_UNUSED)
561 # else
562 void * tiny_reverse_test(void * arg GC_ATTR_UNUSED)
563 # endif
564 {
565 int i;
566 for (i = 0; i < 5; ++i) {
567 check_ints(reverse(reverse(ints(1, TINY_REVERSE_UPPER_VALUE))),
568 1, TINY_REVERSE_UPPER_VALUE);
569 }
570 # if defined(GC_ENABLE_SUSPEND_THREAD)
571 /* Force collection from a thread. */
572 GC_gcollect();
573 # endif
574 return 0;
575 }
576
577 # if defined(GC_PTHREADS)
578 # if defined(GC_ENABLE_SUSPEND_THREAD)
579 # include "javaxfc.h"
580 # endif
581
582 void fork_a_thread(void)
583 {
584 pthread_t t;
585 int code;
586
587 code = pthread_create(&t, NULL, tiny_reverse_test, 0);
588 if (code != 0) {
589 GC_printf("Small thread creation failed %d\n", code);
590 FAIL;
591 }
592 # if defined(GC_ENABLE_SUSPEND_THREAD) && !defined(GC_DARWIN_THREADS) \
593 && !defined(GC_OPENBSD_UTHREADS) && !defined(GC_WIN32_THREADS) \
594 && !defined(NACL) && !defined(GC_OSF1_THREADS)
595 if (GC_is_thread_suspended(t)) {
596 GC_printf("Running thread should be not suspended\n");
597 FAIL;
598 }
599 /* Thread could be running or already terminated (but not joined). */
600 GC_suspend_thread(t);
601 if (!GC_is_thread_suspended(t)) {
602 GC_printf("Thread expected to be suspended\n");
603 FAIL;
604 }
605 GC_suspend_thread(t); /* should be no-op */
606 GC_resume_thread(t);
607 if (GC_is_thread_suspended(t)) {
608 GC_printf("Resumed thread should be not suspended\n");
609 FAIL;
610 }
611 GC_resume_thread(t); /* should be no-op */
612 # endif
613 if ((code = pthread_join(t, 0)) != 0) {
614 GC_printf("Small thread join failed %d\n", code);
615 FAIL;
616 }
617 }
618
619 # elif defined(GC_WIN32_THREADS)
620 void fork_a_thread(void)
621 {
622 DWORD thread_id;
623 HANDLE h;
624 h = GC_CreateThread((SECURITY_ATTRIBUTES *)NULL, (word)0,
625 tiny_reverse_test, NULL, (DWORD)0, &thread_id);
626 /* Explicitly specify types of the */
627 /* arguments to test the prototype. */
628 if (h == (HANDLE)NULL) {
629 GC_printf("Small thread creation failed %d\n",
630 (int)GetLastError());
631 FAIL;
632 }
633 if (WaitForSingleObject(h, INFINITE) != WAIT_OBJECT_0) {
634 GC_printf("Small thread wait failed %d\n",
635 (int)GetLastError());
636 FAIL;
637 }
638 }
639
640 # endif
641
642 #endif
643
644 void test_generic_malloc_or_special(void *p) {
645 size_t size;
646 int kind = GC_get_kind_and_size(p, &size);
647 void *p2;
648
649 if (size != GC_size(p)) {
650 GC_printf("GC_get_kind_and_size returned size not matching GC_size\n");
651 FAIL;
652 }
653 p2 = GC_GENERIC_OR_SPECIAL_MALLOC(10, kind);
654 CHECK_OUT_OF_MEMORY(p2);
655 if (GC_get_kind_and_size(p2, NULL) != kind) {
656 GC_printf("GC_generic_or_special_malloc:"
657 " unexpected kind of returned object\n");
658 FAIL;
659 }
660 GC_FREE(p2);
661 }
662
663 /* Try to force a to be strangely aligned */
664 volatile struct A_s {
665 char dummy;
666 AO_t aa;
667 } A;
668 #define a_set(p) AO_store_release(&A.aa, (AO_t)(p))
669 #define a_get() (sexpr)AO_load_acquire(&A.aa)
670
671 /*
672 * Repeatedly reverse lists built out of very different sized cons cells.
673 * Check that we didn't lose anything.
674 */
675 void *GC_CALLBACK reverse_test_inner(void *data)
676 {
677 int i;
678 sexpr b;
679 sexpr c;
680 sexpr d;
681 sexpr e;
682 sexpr *f, *g, *h;
683
684 if (data == 0) {
685 /* This stack frame is not guaranteed to be scanned. */
686 return GC_call_with_gc_active(reverse_test_inner, (void*)(word)1);
687 }
688
689 # if defined(MACOS) \
690 || (defined(UNIX_LIKE) && defined(NO_GETCONTEXT)) /* e.g. musl */
691 /* Assume 128K stacks at least. */
692 # define BIG 1000
693 # elif defined(PCR)
694 /* PCR default stack is 100K. Stack frames are up to 120 bytes. */
695 # define BIG 700
696 # elif defined(MSWINCE) || defined(RTEMS)
697 /* WinCE only allows 64K stacks */
698 # define BIG 500
699 # elif defined(OSF1)
700 /* OSF has limited stack space by default, and large frames. */
701 # define BIG 200
702 # elif defined(__MACH__) && defined(__ppc64__)
703 # define BIG 2500
704 # else
705 # define BIG 4500
706 # endif
707
708 a_set(ints(1, 49));
709 b = ints(1, 50);
710 c = ints(1, BIG);
711 d = uncollectable_ints(1, 100);
712 test_generic_malloc_or_special(d);
713 e = uncollectable_ints(1, 1);
714 /* Check that realloc updates object descriptors correctly */
715 AO_fetch_and_add1(&collectable_count);
716 f = (sexpr *)GC_MALLOC(4 * sizeof(sexpr));
717 f = (sexpr *)GC_REALLOC((void *)f, 6 * sizeof(sexpr));
718 CHECK_OUT_OF_MEMORY(f);
719 AO_fetch_and_add1(&realloc_count);
720 f[5] = ints(1,17);
721 AO_fetch_and_add1(&collectable_count);
722 g = (sexpr *)GC_MALLOC(513 * sizeof(sexpr));
723 test_generic_malloc_or_special(g);
724 g = (sexpr *)GC_REALLOC((void *)g, 800 * sizeof(sexpr));
725 CHECK_OUT_OF_MEMORY(g);
726 AO_fetch_and_add1(&realloc_count);
727 g[799] = ints(1,18);
728 AO_fetch_and_add1(&collectable_count);
729 h = (sexpr *)GC_MALLOC(1025 * sizeof(sexpr));
730 h = (sexpr *)GC_REALLOC((void *)h, 2000 * sizeof(sexpr));
731 CHECK_OUT_OF_MEMORY(h);
732 AO_fetch_and_add1(&realloc_count);
733 # ifdef GC_GCJ_SUPPORT
734 h[1999] = gcj_ints(1,200);
735 for (i = 0; i < 51; ++i)
736 h[1999] = gcj_reverse(h[1999]);
737 /* Leave it as the reversed list for now. */
738 # else
739 h[1999] = ints(1,200);
740 # endif
741 /* Try to force some collections and reuse of small list elements */
742 for (i = 0; i < 10; i++) {
743 (void)ints(1, BIG);
744 }
745 /* Superficially test interior pointer recognition on stack */
746 c = (sexpr)((char *)c + sizeof(char *));
747 d = (sexpr)((char *)d + sizeof(char *));
748
749 GC_FREE((void *)e);
750
751 check_ints(b,1,50);
752 check_ints(a_get(),1,49);
753 for (i = 0; i < 50; i++) {
754 check_ints(b,1,50);
755 b = reverse(reverse(b));
756 }
757 check_ints(b,1,50);
758 check_ints(a_get(),1,49);
759 for (i = 0; i < 60; i++) {
760 # if (defined(GC_PTHREADS) || defined(GC_WIN32_THREADS)) \
761 && (NTHREADS > 0)
762 if (i % 10 == 0) fork_a_thread();
763 # endif
764 /* This maintains the invariant that a always points to a list */
765 /* of 49 integers. Thus, this is thread safe without locks, */
766 /* assuming acquire/release barriers in a_get/set() and atomic */
767 /* pointer assignments (otherwise, e.g., check_ints() may see */
768 /* an uninitialized object returned by GC_MALLOC). */
769 a_set(reverse(reverse(a_get())));
770 # if !defined(AT_END) && !defined(THREADS)
771 /* This is not thread safe, since realloc explicitly deallocates */
772 a_set(GC_REALLOC(a_get(), (i & 1) != 0 ? 500 : 8200));
773 AO_fetch_and_add1(&realloc_count);
774 # endif
775 }
776 check_ints(a_get(),1,49);
777 check_ints(b,1,50);
778
779 /* Restore c and d values. */
780 c = (sexpr)((char *)c - sizeof(char *));
781 d = (sexpr)((char *)d - sizeof(char *));
782
783 check_ints(c,1,BIG);
784 check_uncollectable_ints(d, 1, 100);
785 check_ints(f[5], 1,17);
786 check_ints(g[799], 1,18);
787 # ifdef GC_GCJ_SUPPORT
788 h[1999] = gcj_reverse(h[1999]);
789 # endif
790 check_ints(h[1999], 1,200);
791 # ifndef THREADS
792 a_set(NULL);
793 # endif
794 *(sexpr volatile *)&b = 0;
795 *(sexpr volatile *)&c = 0;
796 return 0;
797 }
798
799 void reverse_test(void)
800 {
801 /* Test GC_do_blocking/GC_call_with_gc_active. */
802 (void)GC_do_blocking(reverse_test_inner, 0);
803 }
804
805 /*
806 * The rest of this builds balanced binary trees, checks that they don't
807 * disappear, and tests finalization.
808 */
809 typedef struct treenode {
810 int level;
811 struct treenode * lchild;
812 struct treenode * rchild;
813 } tn;
814
815 int finalizable_count = 0;
816 int finalized_count = 0;
817 int dropped_something = 0;
818
819 void GC_CALLBACK finalizer(void * obj, void * client_data)
820 {
821 tn * t = (tn *)obj;
822
823 FINALIZER_LOCK();
824 if ((int)(GC_word)client_data != t -> level) {
825 GC_printf("Wrong finalization data - collector is broken\n");
826 FAIL;
827 }
828 finalized_count++;
829 t -> level = -1; /* detect duplicate finalization immediately */
830 FINALIZER_UNLOCK();
831 }
832
833 # define MAX_FINALIZED ((NTHREADS+1)*4000)
834
835 # if !defined(MACOS)
836 GC_FAR GC_word live_indicators[MAX_FINALIZED] = {0};
837 # ifndef GC_LONG_REFS_NOT_NEEDED
838 GC_FAR void *live_long_refs[MAX_FINALIZED] = { NULL };
839 # endif
840 #else
841 /* Too big for THINK_C. have to allocate it dynamically. */
842 GC_word *live_indicators = 0;
843 # ifndef GC_LONG_REFS_NOT_NEEDED
844 # define GC_LONG_REFS_NOT_NEEDED
845 # endif
846 #endif
847
848 int live_indicators_count = 0;
849
850 tn * mktree(int n)
851 {
852 tn * result = GC_NEW(tn);
853
854 AO_fetch_and_add1(&collectable_count);
855 # if defined(MACOS)
856 /* get around static data limitations. */
857 if (!live_indicators) {
858 live_indicators =
859 (GC_word*)NewPtrClear(MAX_FINALIZED * sizeof(GC_word));
860 CHECK_OUT_OF_MEMORY(live_indicators);
861 }
862 # endif
863 if (n == 0) return(0);
864 CHECK_OUT_OF_MEMORY(result);
865 result -> level = n;
866 result -> lchild = mktree(n-1);
867 result -> rchild = mktree(n-1);
868 if (AO_fetch_and_add1(&extra_count) % 17 == 0 && n >= 2) {
869 tn * tmp;
870 tn * left = result -> lchild;
871 tn * right = result -> rchild;
872
873 CHECK_OUT_OF_MEMORY(left);
874 tmp = left -> rchild;
875 CHECK_OUT_OF_MEMORY(right);
876 left -> rchild = right -> lchild;
877 right -> lchild = tmp;
878 GC_END_STUBBORN_CHANGE(left);
879 GC_END_STUBBORN_CHANGE(right);
880 }
881 if (AO_fetch_and_add1(&extra_count) % 119 == 0) {
882 # ifndef GC_NO_FINALIZATION
883 int my_index;
884 void *new_link;
885 # endif
886
887 {
888 FINALIZER_LOCK();
889 /* Losing a count here causes erroneous report of failure. */
890 finalizable_count++;
891 # ifndef GC_NO_FINALIZATION
892 my_index = live_indicators_count++;
893 # endif
894 FINALIZER_UNLOCK();
895 }
896
897 # ifndef GC_NO_FINALIZATION
898 GC_REGISTER_FINALIZER((void *)result, finalizer, (void *)(GC_word)n,
899 (GC_finalization_proc *)0, (void * *)0);
900 if (my_index >= MAX_FINALIZED) {
901 GC_printf("live_indicators overflowed\n");
902 FAIL;
903 }
904 live_indicators[my_index] = 13;
905 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
906 (void * *)(&(live_indicators[my_index])), result) != 0) {
907 GC_printf("GC_general_register_disappearing_link failed\n");
908 FAIL;
909 }
910 if (GC_move_disappearing_link((void **)(&(live_indicators[my_index])),
911 (void **)(&(live_indicators[my_index]))) != GC_SUCCESS) {
912 GC_printf("GC_move_disappearing_link(link,link) failed\n");
913 FAIL;
914 }
915 new_link = (void *)live_indicators[my_index];
916 if (GC_move_disappearing_link((void **)(&(live_indicators[my_index])),
917 &new_link) != GC_SUCCESS) {
918 GC_printf("GC_move_disappearing_link(new_link) failed\n");
919 FAIL;
920 }
921 if (GC_unregister_disappearing_link(&new_link) == 0) {
922 GC_printf("GC_unregister_disappearing_link failed\n");
923 FAIL;
924 }
925 if (GC_move_disappearing_link((void **)(&(live_indicators[my_index])),
926 &new_link) != GC_NOT_FOUND) {
927 GC_printf("GC_move_disappearing_link(new_link) failed 2\n");
928 FAIL;
929 }
930 if (GC_GENERAL_REGISTER_DISAPPEARING_LINK(
931 (void * *)(&(live_indicators[my_index])), result) != 0) {
932 GC_printf("GC_general_register_disappearing_link failed 2\n");
933 FAIL;
934 }
935 # ifndef GC_LONG_REFS_NOT_NEEDED
936 if (GC_REGISTER_LONG_LINK(&live_long_refs[my_index], result) != 0) {
937 GC_printf("GC_register_long_link failed\n");
938 FAIL;
939 }
940 if (GC_move_long_link(&live_long_refs[my_index],
941 &live_long_refs[my_index]) != GC_SUCCESS) {
942 GC_printf("GC_move_long_link(link,link) failed\n");
943 FAIL;
944 }
945 new_link = live_long_refs[my_index];
946 if (GC_move_long_link(&live_long_refs[my_index],
947 &new_link) != GC_SUCCESS) {
948 GC_printf("GC_move_long_link(new_link) failed\n");
949 FAIL;
950 }
951 if (GC_unregister_long_link(&new_link) == 0) {
952 GC_printf("GC_unregister_long_link failed\n");
953 FAIL;
954 }
955 if (GC_move_long_link(&live_long_refs[my_index],
956 &new_link) != GC_NOT_FOUND) {
957 GC_printf("GC_move_long_link(new_link) failed 2\n");
958 FAIL;
959 }
960 if (GC_REGISTER_LONG_LINK(&live_long_refs[my_index], result) != 0) {
961 GC_printf("GC_register_long_link failed 2\n");
962 FAIL;
963 }
964 # endif
965 # endif
966 GC_reachable_here(result);
967 }
968 GC_END_STUBBORN_CHANGE(result);
969 return(result);
970 }
971
972 void chktree(tn *t, int n)
973 {
974 if (0 == n) {
975 if (NULL == t) /* is a leaf? */
976 return;
977 GC_printf("Clobbered a leaf - collector is broken\n");
978 FAIL;
979 }
980 if (t -> level != n) {
981 GC_printf("Lost a node at level %d - collector is broken\n", n);
982 FAIL;
983 }
984 if (AO_fetch_and_add1(&extra_count) % 373 == 0) {
985 (void)GC_MALLOC((unsigned)AO_fetch_and_add1(&extra_count) % 5001);
986 AO_fetch_and_add1(&collectable_count);
987 }
988 chktree(t -> lchild, n-1);
989 if (AO_fetch_and_add1(&extra_count) % 73 == 0) {
990 (void)GC_MALLOC((unsigned)AO_fetch_and_add1(&extra_count) % 373);
991 AO_fetch_and_add1(&collectable_count);
992 }
993 chktree(t -> rchild, n-1);
994 }
995
996
997 #if defined(GC_PTHREADS)
998 pthread_key_t fl_key;
999
1000 void * alloc8bytes(void)
1001 {
1002 # if defined(SMALL_CONFIG) || defined(GC_DEBUG)
1003 AO_fetch_and_add1(&collectable_count);
1004 return(GC_MALLOC(8));
1005 # else
1006 void ** my_free_list_ptr;
1007 void * my_free_list;
1008
1009 my_free_list_ptr = (void **)pthread_getspecific(fl_key);
1010 if (my_free_list_ptr == 0) {
1011 my_free_list_ptr = GC_NEW_UNCOLLECTABLE(void *);
1012 CHECK_OUT_OF_MEMORY(my_free_list_ptr);
1013 AO_fetch_and_add1(&uncollectable_count);
1014 if (pthread_setspecific(fl_key, my_free_list_ptr) != 0) {
1015 GC_printf("pthread_setspecific failed\n");
1016 FAIL;
1017 }
1018 }
1019 my_free_list = *my_free_list_ptr;
1020 if (my_free_list == 0) {
1021 my_free_list = GC_malloc_many(8);
1022 CHECK_OUT_OF_MEMORY(my_free_list);
1023 }
1024 *my_free_list_ptr = GC_NEXT(my_free_list);
1025 GC_NEXT(my_free_list) = 0;
1026 GC_END_STUBBORN_CHANGE(my_free_list_ptr);
1027 AO_fetch_and_add1(&collectable_count);
1028 return(my_free_list);
1029 # endif
1030 }
1031
1032 #else
1033 # define alloc8bytes() GC_MALLOC_ATOMIC(8)
1034 #endif
1035
1036 #include "gc_inline.h"
1037
1038 void test_tinyfl(void)
1039 {
1040 void *results[3];
1041 void *tfls[3][GC_TINY_FREELISTS];
1042
1043 # ifndef DONT_ADD_BYTE_AT_END
1044 if (GC_get_all_interior_pointers()) return; /* skip */
1045 # endif
1046 BZERO(tfls, sizeof(tfls));
1047 /* TODO: Improve testing of FAST_MALLOC functionality. */
1048 GC_MALLOC_WORDS(results[0], 11, tfls[0]);
1049 GC_MALLOC_ATOMIC_WORDS(results[1], 20, tfls[1]);
1050 GC_CONS(results[2], results[0], results[1], tfls[2]);
1051 }
1052
1053 void alloc_small(int n)
1054 {
1055 int i;
1056
1057 for (i = 0; i < n; i += 8) {
1058 if (alloc8bytes() == 0) {
1059 GC_printf("Out of memory\n");
1060 FAIL;
1061 }
1062 AO_fetch_and_add1(&atomic_count);
1063 }
1064 }
1065
1066 # if defined(THREADS) && defined(GC_DEBUG)
1067 # ifdef VERY_SMALL_CONFIG
1068 # define TREE_HEIGHT 12
1069 # else
1070 # define TREE_HEIGHT 15
1071 # endif
1072 # else
1073 # ifdef VERY_SMALL_CONFIG
1074 # define TREE_HEIGHT 13
1075 # else
1076 # define TREE_HEIGHT 16
1077 # endif
1078 # endif
1079 void tree_test(void)
1080 {
1081 tn * root;
1082 int i;
1083
1084 root = mktree(TREE_HEIGHT);
1085 # ifndef VERY_SMALL_CONFIG
1086 alloc_small(5000000);
1087 # endif
1088 chktree(root, TREE_HEIGHT);
1089 FINALIZER_LOCK();
1090 if (finalized_count && !dropped_something) {
1091 GC_printf("Premature finalization - collector is broken\n");
1092 FAIL;
1093 }
1094 dropped_something = 1;
1095 FINALIZER_UNLOCK();
1096 GC_noop1((word)root); /* Root needs to remain live until */
1097 /* dropped_something is set. */
1098 root = mktree(TREE_HEIGHT);
1099 chktree(root, TREE_HEIGHT);
1100 for (i = TREE_HEIGHT; i >= 0; i--) {
1101 root = mktree(i);
1102 chktree(root, i);
1103 }
1104 # ifndef VERY_SMALL_CONFIG
1105 alloc_small(5000000);
1106 # endif
1107 }
1108
1109 unsigned n_tests = 0;
1110
1111 const GC_word bm_huge[320 / CPP_WORDSZ] = {
1112 # if CPP_WORDSZ == 32
1113 0xffffffff,
1114 0xffffffff,
1115 0xffffffff,
1116 0xffffffff,
1117 0xffffffff,
1118 # endif
1119 (GC_word)((GC_signed_word)-1),
1120 (GC_word)((GC_signed_word)-1),
1121 (GC_word)((GC_signed_word)-1),
1122 (GC_word)((GC_signed_word)-1),
1123 ((GC_word)((GC_signed_word)-1)) >> 8 /* highest byte is zero */
1124 };
1125
1126 /* A very simple test of explicitly typed allocation */
1127 void typed_test(void)
1128 {
1129 GC_word * old, * newP;
1130 GC_word bm3[1] = {0};
1131 GC_word bm2[1] = {0};
1132 GC_word bm_large[1] = { 0xf7ff7fff };
1133 GC_descr d1;
1134 GC_descr d2;
1135 GC_descr d3 = GC_make_descriptor(bm_large, 32);
1136 GC_descr d4 = GC_make_descriptor(bm_huge, 320);
1137 GC_word * x = (GC_word *)GC_malloc_explicitly_typed(
1138 320 * sizeof(GC_word) + 123, d4);
1139 int i;
1140
1141 AO_fetch_and_add1(&collectable_count);
1142 (void)GC_make_descriptor(bm_large, 32);
1143 if (GC_get_bit(bm_huge, 32) == 0 || GC_get_bit(bm_huge, 311) == 0
1144 || GC_get_bit(bm_huge, 319) != 0) {
1145 GC_printf("Bad GC_get_bit() or bm_huge initialization\n");
1146 FAIL;
1147 }
1148 GC_set_bit(bm3, 0);
1149 GC_set_bit(bm3, 1);
1150 d1 = GC_make_descriptor(bm3, 2);
1151 GC_set_bit(bm2, 1);
1152 d2 = GC_make_descriptor(bm2, 2);
1153 old = 0;
1154 for (i = 0; i < 4000; i++) {
1155 newP = (GC_word *)GC_malloc_explicitly_typed(4 * sizeof(GC_word), d1);
1156 CHECK_OUT_OF_MEMORY(newP);
1157 AO_fetch_and_add1(&collectable_count);
1158 if (newP[0] != 0 || newP[1] != 0) {
1159 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
1160 FAIL;
1161 }
1162 newP[0] = 17;
1163 newP[1] = (GC_word)old;
1164 old = newP;
1165 AO_fetch_and_add1(&collectable_count);
1166 newP = (GC_word *)GC_malloc_explicitly_typed(4 * sizeof(GC_word), d2);
1167 CHECK_OUT_OF_MEMORY(newP);
1168 newP[0] = 17;
1169 newP[1] = (GC_word)old;
1170 GC_END_STUBBORN_CHANGE(newP);
1171 old = newP;
1172 AO_fetch_and_add1(&collectable_count);
1173 newP = (GC_word*)GC_malloc_explicitly_typed(33 * sizeof(GC_word), d3);
1174 CHECK_OUT_OF_MEMORY(newP);
1175 newP[0] = 17;
1176 newP[1] = (GC_word)old;
1177 GC_END_STUBBORN_CHANGE(newP);
1178 old = newP;
1179 AO_fetch_and_add1(&collectable_count);
1180 newP = (GC_word *)GC_calloc_explicitly_typed(4, 2 * sizeof(GC_word),
1181 d1);
1182 CHECK_OUT_OF_MEMORY(newP);
1183 newP[0] = 17;
1184 newP[1] = (GC_word)old;
1185 GC_END_STUBBORN_CHANGE(newP);
1186 old = newP;
1187 AO_fetch_and_add1(&collectable_count);
1188 if (i & 0xff) {
1189 newP = (GC_word *)GC_calloc_explicitly_typed(7, 3 * sizeof(GC_word),
1190 d2);
1191 } else {
1192 newP = (GC_word *)GC_calloc_explicitly_typed(1001,
1193 3 * sizeof(GC_word),
1194 d2);
1195 if (newP != NULL && (newP[0] != 0 || newP[1] != 0)) {
1196 GC_printf("Bad initialization by GC_malloc_explicitly_typed\n");
1197 FAIL;
1198 }
1199 }
1200 CHECK_OUT_OF_MEMORY(newP);
1201 newP[0] = 17;
1202 newP[1] = (GC_word)old;
1203 GC_END_STUBBORN_CHANGE(newP);
1204 old = newP;
1205 }
1206 for (i = 0; i < 20000; i++) {
1207 if (newP[0] != 17) {
1208 GC_printf("Typed alloc failed at %d\n", i);
1209 FAIL;
1210 }
1211 newP[0] = 0;
1212 old = newP;
1213 newP = (GC_word *)old[1];
1214 }
1215 GC_gcollect();
1216 GC_noop1((word)x);
1217 }
1218
1219 #ifdef DBG_HDRS_ALL
1220 # define set_print_procs() (void)(A.dummy = 17)
1221 #else
1222 int fail_count = 0;
1223
1224 void GC_CALLBACK fail_proc1(void *x GC_ATTR_UNUSED)
1225 {
1226 fail_count++;
1227 }
1228
1229 void set_print_procs(void)
1230 {
1231 /* Set these global variables just once to avoid TSan false positives. */
1232 A.dummy = 17;
1233 GC_is_valid_displacement_print_proc = fail_proc1;
1234 GC_is_visible_print_proc = fail_proc1;
1235 }
1236
1237 # ifdef THREADS
1238 # define TEST_FAIL_COUNT(n) 1
1239 # else
1240 # define TEST_FAIL_COUNT(n) (fail_count >= (n))
1241 # endif
1242 #endif /* !DBG_HDRS_ALL */
1243
1244 static void uniq(void *p, ...) {
1245 va_list a;
1246 void *q[100];
1247 int n = 0, i, j;
1248 q[n++] = p;
1249 va_start(a,p);
1250 for (;(q[n] = va_arg(a,void *)) != NULL;n++) ;
1251 va_end(a);
1252 for (i=0; i<n; i++)
1253 for (j=0; j<i; j++)
1254 if (q[i] == q[j]) {
1255 GC_printf(
1256 "Apparently failed to mark from some function arguments.\n"
1257 "Perhaps GC_push_regs was configured incorrectly?\n"
1258 );
1259 FAIL;
1260 }
1261 }
1262
1263 void * GC_CALLBACK inc_int_counter(void *pcounter)
1264 {
1265 ++(*(int *)pcounter);
1266 return NULL;
1267 }
1268
1269 void run_one_test(void)
1270 {
1271 # ifndef DBG_HDRS_ALL
1272 char *x;
1273 char **z;
1274 char *y = (char *)(GC_word)fail_proc1;
1275 # endif
1276 # ifndef NO_CLOCK
1277 CLOCK_TYPE start_time;
1278 CLOCK_TYPE reverse_time;
1279 unsigned long time_diff;
1280 # endif
1281 # ifndef NO_TEST_HANDLE_FORK
1282 pid_t pid;
1283 int wstatus;
1284 # endif
1285
1286 # ifdef FIND_LEAK
1287 GC_printf(
1288 "This test program is not designed for leak detection mode\n");
1289 GC_printf("Expect lots of problems\n");
1290 # endif
1291 GC_FREE(0);
1292 # ifdef THREADS
1293 if (!GC_thread_is_registered()) {
1294 GC_printf("Current thread is not registered with GC\n");
1295 FAIL;
1296 }
1297 # endif
1298 test_tinyfl();
1299 # ifndef DBG_HDRS_ALL
1300 AO_fetch_and_add1(&collectable_count); /* 1 */
1301 AO_fetch_and_add1(&collectable_count); /* 2 */
1302 AO_fetch_and_add1(&collectable_count); /* 3 */
1303 if ((GC_size(GC_malloc(7)) != 8 &&
1304 GC_size(GC_malloc(7)) != MIN_WORDS * sizeof(GC_word))
1305 || GC_size(GC_malloc(15)) != 16) {
1306 GC_printf("GC_size produced unexpected results\n");
1307 FAIL;
1308 }
1309 AO_fetch_and_add1(&collectable_count);
1310 if (GC_size(GC_malloc(0)) != MIN_WORDS * sizeof(GC_word)) {
1311 GC_printf("GC_malloc(0) failed: GC_size returns %lu\n",
1312 (unsigned long)GC_size(GC_malloc(0)));
1313 FAIL;
1314 }
1315 AO_fetch_and_add1(&uncollectable_count);
1316 if (GC_size(GC_malloc_uncollectable(0)) != MIN_WORDS * sizeof(GC_word)) {
1317 GC_printf("GC_malloc_uncollectable(0) failed\n");
1318 FAIL;
1319 }
1320 AO_fetch_and_add1(&collectable_count);
1321 x = (char*)GC_malloc(16);
1322 if (GC_base(GC_PTR_ADD(x, 13)) != x) {
1323 GC_printf("GC_base(heap ptr) produced incorrect result\n");
1324 FAIL;
1325 }
1326 if (!GC_is_heap_ptr(x)) {
1327 GC_printf("GC_is_heap_ptr(heap_ptr) produced incorrect result\n");
1328 FAIL;
1329 }
1330 if (GC_is_heap_ptr(&x)) {
1331 GC_printf("GC_is_heap_ptr(&local_var) produced incorrect result\n");
1332 FAIL;
1333 }
1334 if (GC_is_heap_ptr(&fail_count) || GC_is_heap_ptr(NULL)) {
1335 GC_printf("GC_is_heap_ptr(&global_var) produced incorrect result\n");
1336 FAIL;
1337 }
1338 (void)GC_PRE_INCR(x, 0);
1339 (void)GC_POST_INCR(x);
1340 (void)GC_POST_DECR(x);
1341 if (GC_base(x) != x) {
1342 GC_printf("Bad INCR/DECR result\n");
1343 FAIL;
1344 }
1345 # ifndef PCR
1346 if (GC_base(y) != 0) {
1347 GC_printf("GC_base(fn_ptr) produced incorrect result\n");
1348 FAIL;
1349 }
1350 # endif
1351 if (GC_same_obj(x+5, x) != x + 5) {
1352 GC_printf("GC_same_obj produced incorrect result\n");
1353 FAIL;
1354 }
1355 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1356 GC_printf("GC_is_visible produced incorrect result\n");
1357 FAIL;
1358 }
1359 z = (char**)GC_malloc(8);
1360 CHECK_OUT_OF_MEMORY(z);
1361 AO_fetch_and_add1(&collectable_count);
1362 GC_PTR_STORE(z, x);
1363 GC_end_stubborn_change(z);
1364 if (*z != x) {
1365 GC_printf("GC_PTR_STORE failed: %p != %p\n", (void *)(*z), (void *)x);
1366 FAIL;
1367 }
1368 if (!TEST_FAIL_COUNT(1)) {
1369 # if!(defined(POWERPC) || defined(IA64)) || defined(M68K)
1370 /* On POWERPCs function pointers point to a descriptor in the */
1371 /* data segment, so there should have been no failures. */
1372 /* The same applies to IA64. Something similar seems to */
1373 /* be going on with NetBSD/M68K. */
1374 GC_printf("GC_is_visible produced wrong failure indication\n");
1375 FAIL;
1376 # endif
1377 }
1378 if (GC_is_valid_displacement(y) != y
1379 || GC_is_valid_displacement(x) != x
1380 || GC_is_valid_displacement(x + 3) != x + 3) {
1381 GC_printf("GC_is_valid_displacement produced incorrect result\n");
1382 FAIL;
1383 }
1384 {
1385 size_t i;
1386
1387 (void)GC_malloc(17);
1388 AO_fetch_and_add1(&collectable_count);
1389 for (i = sizeof(GC_word); i < 512; i *= 2) {
1390 GC_word result = (GC_word) GC_memalign(i, 17);
1391 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1392 }
1393 }
1394 # ifndef ALL_INTERIOR_POINTERS
1395 # if defined(RS6000) || defined(POWERPC)
1396 if (!TEST_FAIL_COUNT(1))
1397 # else
1398 if (!TEST_FAIL_COUNT(GC_get_all_interior_pointers() ? 1 : 2))
1399 # endif
1400 {
1401 GC_printf(
1402 "GC_is_valid_displacement produced wrong failure indication\n");
1403 FAIL;
1404 }
1405 # endif
1406 # endif /* DBG_HDRS_ALL */
1407 /* Test floating point alignment */
1408 {
1409 double *dp = GC_NEW(double);
1410 CHECK_OUT_OF_MEMORY(dp);
1411 AO_fetch_and_add1(&collectable_count);
1412 *dp = 1.0;
1413 dp = GC_NEW(double);
1414 CHECK_OUT_OF_MEMORY(dp);
1415 AO_fetch_and_add1(&collectable_count);
1416 *dp = 1.0;
1417 }
1418 /* Test size 0 allocation a bit more */
1419 {
1420 size_t i;
1421 for (i = 0; i < 10000; ++i) {
1422 (void)GC_MALLOC(0);
1423 AO_fetch_and_add1(&collectable_count);
1424 GC_FREE(GC_MALLOC(0));
1425 (void)GC_MALLOC_ATOMIC(0);
1426 AO_fetch_and_add1(&atomic_count);
1427 GC_FREE(GC_MALLOC_ATOMIC(0));
1428 test_generic_malloc_or_special(GC_malloc_atomic(1));
1429 AO_fetch_and_add1(&atomic_count);
1430 }
1431 }
1432 # ifdef GC_GCJ_SUPPORT
1433 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1434 GC_init_gcj_malloc(0, (void *)(GC_word)fake_gcj_mark_proc);
1435 # endif
1436 /* Make sure that fn arguments are visible to the collector. */
1437 uniq(
1438 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1439 (GC_gcollect(),GC_malloc(12)),
1440 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1441 (GC_gcollect(),GC_malloc(12)),
1442 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1443 (GC_gcollect(),GC_malloc(12)),
1444 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1445 (GC_gcollect(),GC_malloc(12)),
1446 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1447 (GC_gcollect(),GC_malloc(12)),
1448 (void *)0);
1449 /* GC_malloc(0) must return NULL or something we can deallocate. */
1450 GC_free(GC_malloc(0));
1451 GC_free(GC_malloc_atomic(0));
1452 GC_free(GC_malloc(0));
1453 GC_free(GC_malloc_atomic(0));
1454 # ifndef NO_TEST_HANDLE_FORK
1455 GC_atfork_prepare();
1456 pid = fork();
1457 if (pid != 0) {
1458 GC_atfork_parent();
1459 if (pid == -1) {
1460 GC_printf("Process fork failed\n");
1461 FAIL;
1462 }
1463 if (print_stats)
1464 GC_log_printf("Forked child process\n");
1465 if (waitpid(pid, &wstatus, 0) == -1) {
1466 GC_printf("Wait for child process failed\n");
1467 FAIL;
1468 }
1469 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
1470 GC_printf("Child process failed, status= 0x%x\n", wstatus);
1471 FAIL;
1472 }
1473 } else {
1474 GC_atfork_child();
1475 if (print_stats)
1476 GC_log_printf("Started a child process\n");
1477 # ifdef THREADS
1478 # ifdef PARALLEL_MARK
1479 GC_gcollect(); /* no parallel markers */
1480 # endif
1481 GC_start_mark_threads();
1482 # endif
1483 GC_gcollect();
1484 # ifdef THREADS
1485 tiny_reverse_test(0);
1486 GC_gcollect();
1487 # endif
1488 if (print_stats)
1489 GC_log_printf("Finished a child process\n");
1490 exit(0);
1491 }
1492 # endif
1493 /* Repeated list reversal test. */
1494 # ifndef NO_CLOCK
1495 GET_TIME(start_time);
1496 # endif
1497 reverse_test();
1498 # ifndef NO_CLOCK
1499 if (print_stats) {
1500 GET_TIME(reverse_time);
1501 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1502 GC_log_printf("-------------Finished reverse_test at time %u (%p)\n",
1503 (unsigned) time_diff, (void *)&start_time);
1504 }
1505 # endif
1506 # ifndef DBG_HDRS_ALL
1507 typed_test();
1508 # ifndef NO_CLOCK
1509 if (print_stats) {
1510 CLOCK_TYPE typed_time;
1511
1512 GET_TIME(typed_time);
1513 time_diff = MS_TIME_DIFF(typed_time, start_time);
1514 GC_log_printf("-------------Finished typed_test at time %u (%p)\n",
1515 (unsigned) time_diff, (void *)&start_time);
1516 }
1517 # endif
1518 # endif /* DBG_HDRS_ALL */
1519 tree_test();
1520 # ifndef NO_CLOCK
1521 if (print_stats) {
1522 CLOCK_TYPE tree_time;
1523
1524 GET_TIME(tree_time);
1525 time_diff = MS_TIME_DIFF(tree_time, start_time);
1526 GC_log_printf("-------------Finished tree_test at time %u (%p)\n",
1527 (unsigned) time_diff, (void *)&start_time);
1528 }
1529 # endif
1530 /* Run reverse_test a second time, so we hopefully notice corruption. */
1531 reverse_test();
1532 # ifndef NO_CLOCK
1533 if (print_stats) {
1534 GET_TIME(reverse_time);
1535 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1536 GC_log_printf(
1537 "-------------Finished second reverse_test at time %u (%p)\n",
1538 (unsigned)time_diff, (void *)&start_time);
1539 }
1540 # endif
1541 /* GC_allocate_ml and GC_need_to_lock are no longer exported, and */
1542 /* AO_fetch_and_add1() may be unavailable to update a counter. */
1543 (void)GC_call_with_alloc_lock(inc_int_counter, &n_tests);
1544 # ifndef NO_CLOCK
1545 if (print_stats)
1546 GC_log_printf("Finished %p\n", (void *)&start_time);
1547 # endif
1548 }
1549
1550 void GC_CALLBACK reachable_objs_counter(void *obj, size_t size,
1551 void *pcounter)
1552 {
1553 if (0 == size) {
1554 GC_printf("Reachable object has zero size\n");
1555 FAIL;
1556 }
1557 if (GC_base(obj) != obj) {
1558 GC_printf("Invalid reachable object base passed by enumerator: %p\n",
1559 obj);
1560 FAIL;
1561 }
1562 if (GC_size(obj) != size) {
1563 GC_printf("Invalid reachable object size passed by enumerator: %lu\n",
1564 (unsigned long)size);
1565 FAIL;
1566 }
1567 (*(unsigned *)pcounter)++;
1568 }
1569
1570 void * GC_CALLBACK reachable_objs_count_enumerator(void *pcounter)
1571 {
1572 GC_enumerate_reachable_objects_inner(reachable_objs_counter, pcounter);
1573 return NULL;
1574 }
1575
1576 #define NUMBER_ROUND_UP(v, bound) ((((v) + (bound) - 1) / (bound)) * (bound))
1577
1578 void check_heap_stats(void)
1579 {
1580 size_t max_heap_sz;
1581 int i;
1582 # ifndef GC_NO_FINALIZATION
1583 int still_live;
1584 # ifndef GC_LONG_REFS_NOT_NEEDED
1585 int still_long_live = 0;
1586 # endif
1587 # ifdef FINALIZE_ON_DEMAND
1588 int late_finalize_count = 0;
1589 # endif
1590 # endif
1591 unsigned obj_count = 0;
1592
1593 if (!GC_is_init_called()) {
1594 GC_printf("GC should be initialized!\n");
1595 FAIL;
1596 }
1597 # ifdef VERY_SMALL_CONFIG
1598 /* The upper bounds are a guess, which has been empirically */
1599 /* adjusted. On low end uniprocessors with incremental GC */
1600 /* these may be particularly dubious, since empirically the */
1601 /* heap tends to grow largely as a result of the GC not */
1602 /* getting enough cycles. */
1603 # if CPP_WORDSZ == 64
1604 max_heap_sz = 4500000;
1605 # else
1606 max_heap_sz = 2800000;
1607 # endif
1608 # else
1609 # if CPP_WORDSZ == 64
1610 max_heap_sz = 25000000;
1611 # else
1612 max_heap_sz = 16000000;
1613 # endif
1614 # endif
1615 # ifdef GC_DEBUG
1616 max_heap_sz *= 2;
1617 # ifdef SAVE_CALL_CHAIN
1618 max_heap_sz *= 3;
1619 # ifdef SAVE_CALL_COUNT
1620 max_heap_sz += max_heap_sz * NFRAMES / 4;
1621 # endif
1622 # endif
1623 # endif
1624 # if defined(ADDRESS_SANITIZER) && !defined(__clang__)
1625 max_heap_sz = max_heap_sz * 2 - max_heap_sz / 3;
1626 # endif
1627 # ifdef MEMORY_SANITIZER
1628 max_heap_sz += max_heap_sz / 4;
1629 # endif
1630 max_heap_sz *= n_tests;
1631 # if defined(USE_MMAP) || defined(MSWIN32)
1632 max_heap_sz = NUMBER_ROUND_UP(max_heap_sz, 4 * 1024 * 1024);
1633 # endif
1634 /* Garbage collect repeatedly so that all inaccessible objects */
1635 /* can be finalized. */
1636 while (GC_collect_a_little()) { }
1637 for (i = 0; i < 16; i++) {
1638 GC_gcollect();
1639 # ifndef GC_NO_FINALIZATION
1640 # ifdef FINALIZE_ON_DEMAND
1641 late_finalize_count +=
1642 # endif
1643 GC_invoke_finalizers();
1644 # endif
1645 }
1646 if (print_stats) {
1647 struct GC_stack_base sb;
1648 int res = GC_get_stack_base(&sb);
1649
1650 if (res == GC_SUCCESS) {
1651 GC_log_printf("Primordial thread stack bottom: %p\n", sb.mem_base);
1652 } else if (res == GC_UNIMPLEMENTED) {
1653 GC_log_printf("GC_get_stack_base() unimplemented\n");
1654 } else {
1655 GC_printf("GC_get_stack_base() failed: %d\n", res);
1656 FAIL;
1657 }
1658 }
1659 (void)GC_call_with_alloc_lock(reachable_objs_count_enumerator,
1660 &obj_count);
1661 GC_printf("Completed %u tests\n", n_tests);
1662 GC_printf("Allocated %d collectable objects\n", (int)collectable_count);
1663 GC_printf("Allocated %d uncollectable objects\n",
1664 (int)uncollectable_count);
1665 GC_printf("Allocated %d atomic objects\n", (int)atomic_count);
1666 GC_printf("Reallocated %d objects\n", (int)realloc_count);
1667 GC_printf("Finalized %d/%d objects - ",
1668 finalized_count, finalizable_count);
1669 # ifndef GC_NO_FINALIZATION
1670 # ifdef FINALIZE_ON_DEMAND
1671 if (finalized_count != late_finalize_count) {
1672 GC_printf("Demand finalization error\n");
1673 FAIL;
1674 }
1675 # endif
1676 if (finalized_count > finalizable_count
1677 || finalized_count < finalizable_count/2) {
1678 GC_printf("finalization is probably broken\n");
1679 FAIL;
1680 } else {
1681 GC_printf("finalization is probably ok\n");
1682 }
1683 still_live = 0;
1684 for (i = 0; i < MAX_FINALIZED; i++) {
1685 if (live_indicators[i] != 0) {
1686 still_live++;
1687 }
1688 # ifndef GC_LONG_REFS_NOT_NEEDED
1689 if (live_long_refs[i] != NULL) {
1690 still_long_live++;
1691 }
1692 # endif
1693 }
1694 i = finalizable_count - finalized_count - still_live;
1695 if (0 != i) {
1696 GC_printf("%d disappearing links remain and %d more objects "
1697 "were not finalized\n", still_live, i);
1698 if (i > 10) {
1699 GC_printf("\tVery suspicious!\n");
1700 } else {
1701 GC_printf("\tSlightly suspicious, but probably OK\n");
1702 }
1703 }
1704 # ifndef GC_LONG_REFS_NOT_NEEDED
1705 if (0 != still_long_live) {
1706 GC_printf("%d 'long' links remain\n", still_long_live);
1707 }
1708 # endif
1709 # endif
1710 GC_printf("Total number of bytes allocated is %lu\n",
1711 (unsigned long)GC_get_total_bytes());
1712 GC_printf("Total memory use by allocated blocks is %lu bytes\n",
1713 (unsigned long)GC_get_memory_use());
1714 GC_printf("Final heap size is %lu bytes\n",
1715 (unsigned long)GC_get_heap_size());
1716 if (GC_get_total_bytes() < (size_t)n_tests *
1717 # ifdef VERY_SMALL_CONFIG
1718 2700000
1719 # else
1720 33500000
1721 # endif
1722 ) {
1723 GC_printf("Incorrect execution - missed some allocations\n");
1724 FAIL;
1725 }
1726 if (GC_get_heap_size() + GC_get_unmapped_bytes() > max_heap_sz) {
1727 GC_printf("Unexpected heap growth - collector may be broken"
1728 " (heapsize: %lu, expected: %lu)\n",
1729 (unsigned long)(GC_get_heap_size() + GC_get_unmapped_bytes()),
1730 (unsigned long)max_heap_sz);
1731 FAIL;
1732 }
1733 GC_printf("Final number of reachable objects is %u\n", obj_count);
1734
1735 # ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
1736 /* Get global counters (just to check the functions work). */
1737 GC_get_heap_usage_safe(NULL, NULL, NULL, NULL, NULL);
1738 {
1739 struct GC_prof_stats_s stats;
1740 (void)GC_get_prof_stats(&stats, sizeof(stats));
1741 # ifdef THREADS
1742 (void)GC_get_prof_stats_unsafe(&stats, sizeof(stats));
1743 # endif
1744 }
1745 (void)GC_get_size_map_at(-1);
1746 (void)GC_get_size_map_at(1);
1747 # endif
1748
1749 # ifdef THREADS
1750 GC_unregister_my_thread(); /* just to check it works (for main) */
1751 # endif
1752 GC_printf("Completed %u collections", (unsigned)GC_get_gc_no());
1753 # ifndef NO_CLOCK
1754 GC_printf(" in %lu msecs", GC_get_full_gc_total_time());
1755 # endif
1756 # ifdef PARALLEL_MARK
1757 GC_printf(" (using %d marker threads)", GC_get_parallel() + 1);
1758 # endif
1759 GC_printf("\n" "Collector appears to work\n");
1760 }
1761
1762 #if defined(MACOS)
1763 void SetMinimumStack(long minSize)
1764 {
1765 if (minSize > LMGetDefltStack())
1766 {
1767 long newApplLimit = (long) GetApplLimit()
1768 - (minSize - LMGetDefltStack());
1769 SetApplLimit((Ptr) newApplLimit);
1770 MaxApplZone();
1771 }
1772 }
1773
1774 #define cMinStackSpace (512L * 1024L)
1775
1776 #endif
1777
1778 void GC_CALLBACK warn_proc(char *msg, GC_word p)
1779 {
1780 GC_printf(msg, (unsigned long)p);
1781 /*FAIL;*/
1782 }
1783
1784 #if defined(CPPCHECK)
1785 # include "javaxfc.h" /* for GC_finalize_all */
1786 # define UNTESTED(sym) GC_noop1((word)&sym)
1787 #endif
1788
1789 #if defined(MSWINCE) && defined(UNDER_CE)
1790 # define WINMAIN_LPTSTR LPWSTR
1791 #else
1792 # define WINMAIN_LPTSTR LPSTR
1793 #endif
1794
1795 #if !defined(PCR) && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
1796
1797 #if ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE)) \
1798 && !defined(NO_WINMAIN_ENTRY)
1799 int APIENTRY WinMain(HINSTANCE instance GC_ATTR_UNUSED,
1800 HINSTANCE prev GC_ATTR_UNUSED,
1801 WINMAIN_LPTSTR cmd GC_ATTR_UNUSED,
1802 int n GC_ATTR_UNUSED)
1803 #elif defined(RTEMS)
1804 # include <bsp.h>
1805 # define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
1806 # define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
1807 # define CONFIGURE_RTEMS_INIT_TASKS_TABLE
1808 # define CONFIGURE_MAXIMUM_TASKS 1
1809 # define CONFIGURE_INIT
1810 # define CONFIGURE_INIT_TASK_STACK_SIZE (64*1024)
1811 # include <rtems/confdefs.h>
1812 rtems_task Init(rtems_task_argument ignord)
1813 #else
1814 int main(void)
1815 #endif
1816 {
1817 # if defined(CPPCHECK) && !defined(NO_WINMAIN_ENTRY) \
1818 && ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE))
1819 GC_noop1((GC_word)&WinMain);
1820 # elif defined(CPPCHECK) && defined(RTEMS)
1821 GC_noop1((GC_word)&Init);
1822 # endif
1823 n_tests = 0;
1824 # if defined(MACOS)
1825 /* Make sure we have lots and lots of stack space. */
1826 SetMinimumStack(cMinStackSpace);
1827 /* Cheat and let stdio initialize toolbox for us. */
1828 printf("Testing GC Macintosh port\n");
1829 # endif
1830 GC_COND_INIT();
1831 GC_set_warn_proc(warn_proc);
1832 # if !defined(GC_DISABLE_INCREMENTAL) \
1833 && (defined(TEST_DEFAULT_VDB) || !defined(DEFAULT_VDB))
1834 # if !defined(MAKE_BACK_GRAPH) && !defined(NO_INCREMENTAL) \
1835 && !(defined(MPROTECT_VDB) && defined(USE_MUNMAP))
1836 GC_enable_incremental();
1837 # endif
1838 if (GC_is_incremental_mode()) {
1839 GC_printf("Switched to incremental mode\n");
1840 # ifdef PROC_VDB
1841 GC_printf("Reading dirty bits from /proc\n");
1842 # elif defined(GWW_VDB)
1843 GC_printf("Using GetWriteWatch-based implementation\n");
1844 # ifdef MPROTECT_VDB
1845 GC_printf("Or emulating dirty bits with mprotect/signals\n");
1846 # endif
1847 # elif defined(MPROTECT_VDB)
1848 GC_printf("Emulating dirty bits with mprotect/signals\n");
1849 # endif /* MPROTECT_VDB && !GWW_VDB */
1850 }
1851 # endif
1852 set_print_procs();
1853 run_one_test();
1854 check_heap_stats();
1855 # ifndef MSWINCE
1856 fflush(stdout);
1857 # endif
1858 # if defined(CPPCHECK)
1859 /* Entry points we should be testing, but aren't. */
1860 # ifndef GC_DEBUG
1861 UNTESTED(GC_debug_generic_or_special_malloc);
1862 UNTESTED(GC_debug_register_displacement);
1863 UNTESTED(GC_post_incr);
1864 UNTESTED(GC_pre_incr);
1865 # ifdef GC_GCJ_SUPPORT
1866 UNTESTED(GC_debug_gcj_malloc);
1867 # endif
1868 # endif
1869 # ifdef AMIGA
1870 # ifdef GC_AMIGA_FASTALLOC
1871 UNTESTED(GC_amiga_get_mem);
1872 # endif
1873 # ifndef GC_AMIGA_ONLYFAST
1874 UNTESTED(GC_amiga_set_toany);
1875 # endif
1876 # endif
1877 # if defined(MACOS) && defined(USE_TEMPORARY_MEMORY)
1878 UNTESTED(GC_MacTemporaryNewPtr);
1879 # endif
1880 # if !defined(_M_AMD64) && defined(_MSC_VER)
1881 UNTESTED(GetFileLineFromStack);
1882 UNTESTED(GetModuleNameFromStack);
1883 UNTESTED(GetSymbolNameFromStack);
1884 # endif
1885 UNTESTED(GC_get_bytes_since_gc);
1886 UNTESTED(GC_get_dont_expand);
1887 UNTESTED(GC_get_dont_precollect);
1888 UNTESTED(GC_get_finalize_on_demand);
1889 UNTESTED(GC_get_finalizer_notifier);
1890 UNTESTED(GC_get_find_leak);
1891 UNTESTED(GC_get_force_unmap_on_gcollect);
1892 UNTESTED(GC_get_free_bytes);
1893 UNTESTED(GC_get_free_space_divisor);
1894 UNTESTED(GC_get_full_freq);
1895 UNTESTED(GC_get_java_finalization);
1896 UNTESTED(GC_get_max_retries);
1897 UNTESTED(GC_get_no_dls);
1898 UNTESTED(GC_get_non_gc_bytes);
1899 UNTESTED(GC_get_on_collection_event);
1900 UNTESTED(GC_get_on_heap_resize);
1901 UNTESTED(GC_get_pages_executable);
1902 UNTESTED(GC_get_push_other_roots);
1903 UNTESTED(GC_get_start_callback);
1904 UNTESTED(GC_get_stop_func);
1905 UNTESTED(GC_get_time_limit);
1906 UNTESTED(GC_get_warn_proc);
1907 UNTESTED(GC_is_disabled);
1908 UNTESTED(GC_set_dont_precollect);
1909 UNTESTED(GC_set_finalize_on_demand);
1910 UNTESTED(GC_set_finalizer_notifier);
1911 UNTESTED(GC_set_free_space_divisor);
1912 UNTESTED(GC_set_full_freq);
1913 UNTESTED(GC_set_java_finalization);
1914 UNTESTED(GC_set_max_retries);
1915 UNTESTED(GC_set_no_dls);
1916 UNTESTED(GC_set_non_gc_bytes);
1917 UNTESTED(GC_set_on_collection_event);
1918 UNTESTED(GC_set_on_heap_resize);
1919 UNTESTED(GC_set_oom_fn);
1920 UNTESTED(GC_set_pages_executable);
1921 UNTESTED(GC_set_push_other_roots);
1922 UNTESTED(GC_set_start_callback);
1923 UNTESTED(GC_set_stop_func);
1924 UNTESTED(GC_set_time_limit);
1925 UNTESTED(GC_malloc_explicitly_typed_ignore_off_page);
1926 UNTESTED(GC_debug_change_stubborn);
1927 UNTESTED(GC_debug_strndup);
1928 UNTESTED(GC_deinit);
1929 UNTESTED(GC_strndup);
1930 UNTESTED(GC_posix_memalign);
1931 UNTESTED(GC_new_free_list);
1932 UNTESTED(GC_new_kind);
1933 UNTESTED(GC_new_proc);
1934 UNTESTED(GC_clear_roots);
1935 UNTESTED(GC_exclude_static_roots);
1936 UNTESTED(GC_expand_hp);
1937 UNTESTED(GC_register_describe_type_fn);
1938 UNTESTED(GC_register_has_static_roots_callback);
1939 # if !defined(PCR) && !defined(SMALL_CONFIG)
1940 UNTESTED(GC_get_abort_func);
1941 UNTESTED(GC_set_abort_func);
1942 # endif
1943 # ifdef GC_GCJ_SUPPORT
1944 UNTESTED(GC_gcj_malloc_ignore_off_page);
1945 # endif
1946 # ifndef NO_DEBUGGING
1947 UNTESTED(GC_dump_regions);
1948 UNTESTED(GC_is_tmp_root);
1949 UNTESTED(GC_print_free_list);
1950 # endif
1951 # ifdef TRACE_BUF
1952 UNTESTED(GC_print_trace);
1953 # endif
1954 # ifndef GC_NO_FINALIZATION
1955 UNTESTED(GC_debug_register_finalizer_unreachable);
1956 UNTESTED(GC_get_await_finalize_proc);
1957 UNTESTED(GC_register_disappearing_link);
1958 UNTESTED(GC_set_await_finalize_proc);
1959 UNTESTED(GC_should_invoke_finalizers);
1960 # ifndef JAVA_FINALIZATION_NOT_NEEDED
1961 UNTESTED(GC_finalize_all);
1962 # endif
1963 # ifndef NO_DEBUGGING
1964 UNTESTED(GC_dump_finalization);
1965 # endif
1966 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
1967 UNTESTED(GC_get_toggleref_func);
1968 UNTESTED(GC_set_toggleref_func);
1969 UNTESTED(GC_toggleref_add);
1970 # endif
1971 # endif
1972 # if !defined(OS2) && !defined(MACOS) && !defined(GC_ANDROID_LOG) \
1973 && !defined(MSWIN32) && !defined(MSWINCE)
1974 UNTESTED(GC_set_log_fd);
1975 # endif
1976 # ifdef THREADS
1977 UNTESTED(GC_allow_register_threads);
1978 UNTESTED(GC_get_on_thread_event);
1979 UNTESTED(GC_register_altstack);
1980 UNTESTED(GC_set_on_thread_event);
1981 # endif
1982 # ifndef REDIRECT_MALLOC_IN_HEADER
1983 # ifdef REDIRECT_MALLOC
1984 # ifndef strndup
1985 UNTESTED(strndup);
1986 # endif
1987 # ifndef strdup
1988 UNTESTED(strdup);
1989 # endif
1990 # endif
1991 # ifdef REDIRECT_REALLOC
1992 UNTESTED(realloc);
1993 # endif
1994 # endif /* !REDIRECT_MALLOC_IN_HEADER */
1995 # ifdef GC_REQUIRE_WCSDUP
1996 UNTESTED(GC_wcsdup);
1997 UNTESTED(GC_debug_wcsdup);
1998 # endif
1999 # endif
2000 # ifdef MSWIN32
2001 GC_win32_free_heap();
2002 # endif
2003 # ifdef RTEMS
2004 exit(0);
2005 # else
2006 return(0);
2007 # endif
2008 }
2009 # endif /* !GC_WIN32_THREADS && !GC_PTHREADS */
2010
2011 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
2012
2013 DWORD __stdcall thr_run_one_test(void * arg GC_ATTR_UNUSED)
2014 {
2015 run_one_test();
2016 return 0;
2017 }
2018
2019 #ifdef MSWINCE
2020 HANDLE win_created_h;
2021 HWND win_handle;
2022
2023 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
2024 LPARAM lParam)
2025 {
2026 LRESULT ret = 0;
2027 switch (uMsg) {
2028 case WM_HIBERNATE:
2029 GC_printf("Received WM_HIBERNATE, calling GC_gcollect\n");
2030 /* Force "unmap as much memory as possible" mode. */
2031 GC_gcollect_and_unmap();
2032 break;
2033 case WM_CLOSE:
2034 GC_printf("Received WM_CLOSE, closing window\n");
2035 DestroyWindow(hwnd);
2036 break;
2037 case WM_DESTROY:
2038 PostQuitMessage(0);
2039 break;
2040 default:
2041 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
2042 break;
2043 }
2044 return ret;
2045 }
2046
2047 DWORD __stdcall thr_window(void * arg GC_ATTR_UNUSED)
2048 {
2049 WNDCLASS win_class = {
2050 CS_NOCLOSE,
2051 window_proc,
2052 0,
2053 0,
2054 GetModuleHandle(NULL),
2055 NULL,
2056 NULL,
2057 (HBRUSH)(COLOR_APPWORKSPACE+1),
2058 NULL,
2059 TEXT("GCtestWindow")
2060 };
2061 MSG msg;
2062
2063 if (!RegisterClass(&win_class))
2064 FAIL;
2065
2066 win_handle = CreateWindowEx(
2067 0,
2068 TEXT("GCtestWindow"),
2069 TEXT("GCtest"),
2070 0,
2071 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2072 NULL,
2073 NULL,
2074 GetModuleHandle(NULL),
2075 NULL);
2076
2077 if (win_handle == NULL)
2078 FAIL;
2079
2080 SetEvent(win_created_h);
2081
2082 ShowWindow(win_handle, SW_SHOW);
2083 UpdateWindow(win_handle);
2084
2085 while (GetMessage(&msg, NULL, 0, 0)) {
2086 TranslateMessage(&msg);
2087 DispatchMessage(&msg);
2088 }
2089
2090 return 0;
2091 }
2092 #endif
2093
2094 #if !defined(NO_WINMAIN_ENTRY)
2095 int APIENTRY WinMain(HINSTANCE instance GC_ATTR_UNUSED,
2096 HINSTANCE prev GC_ATTR_UNUSED,
2097 WINMAIN_LPTSTR cmd GC_ATTR_UNUSED,
2098 int n GC_ATTR_UNUSED)
2099 #else
2100 int main(void)
2101 #endif
2102 {
2103 # if NTHREADS > 0
2104 HANDLE h[NTHREADS];
2105 int i;
2106 # endif
2107 # ifdef MSWINCE
2108 HANDLE win_thr_h;
2109 # endif
2110 DWORD thread_id;
2111
2112 # if defined(CPPCHECK) && !defined(NO_WINMAIN_ENTRY)
2113 GC_noop1((GC_word)&WinMain);
2114 # endif
2115 # if defined(GC_DLL) && !defined(GC_NO_THREADS_DISCOVERY) \
2116 && !defined(MSWINCE) && !defined(THREAD_LOCAL_ALLOC) \
2117 && !defined(PARALLEL_MARK)
2118 GC_use_threads_discovery();
2119 /* Test with implicit thread registration if possible. */
2120 GC_printf("Using DllMain to track threads\n");
2121 # endif
2122 GC_COND_INIT();
2123 # if !defined(MAKE_BACK_GRAPH) && !defined(NO_INCREMENTAL)
2124 GC_enable_incremental();
2125 # endif
2126 InitializeCriticalSection(&incr_cs);
2127 GC_set_warn_proc(warn_proc);
2128 # ifdef MSWINCE
2129 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
2130 if (win_created_h == (HANDLE)NULL) {
2131 GC_printf("Event creation failed %d\n", (int)GetLastError());
2132 FAIL;
2133 }
2134 win_thr_h = GC_CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
2135 if (win_thr_h == (HANDLE)NULL) {
2136 GC_printf("Thread creation failed %d\n", (int)GetLastError());
2137 FAIL;
2138 }
2139 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
2140 FAIL;
2141 CloseHandle(win_created_h);
2142 # endif
2143 set_print_procs();
2144 # if NTHREADS > 0
2145 for (i = 0; i < NTHREADS; i++) {
2146 h[i] = GC_CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
2147 if (h[i] == (HANDLE)NULL) {
2148 GC_printf("Thread creation failed %d\n", (int)GetLastError());
2149 FAIL;
2150 }
2151 }
2152 # endif /* NTHREADS > 0 */
2153 run_one_test();
2154 # if NTHREADS > 0
2155 for (i = 0; i < NTHREADS; i++) {
2156 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
2157 GC_printf("Thread wait failed %d\n", (int)GetLastError());
2158 FAIL;
2159 }
2160 }
2161 # endif /* NTHREADS > 0 */
2162 # ifdef MSWINCE
2163 PostMessage(win_handle, WM_CLOSE, 0, 0);
2164 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
2165 FAIL;
2166 # endif
2167 check_heap_stats();
2168 # if defined(CPPCHECK) && defined(GC_WIN32_THREADS)
2169 UNTESTED(GC_ExitThread);
2170 # if !defined(MSWINCE) && !defined(CYGWIN32)
2171 UNTESTED(GC_beginthreadex);
2172 UNTESTED(GC_endthreadex);
2173 # endif
2174 # endif
2175 return(0);
2176 }
2177
2178 #endif /* GC_WIN32_THREADS */
2179
2180
2181 #ifdef PCR
2182 int test(void)
2183 {
2184 PCR_Th_T * th1;
2185 PCR_Th_T * th2;
2186 int code;
2187
2188 # if defined(CPPCHECK)
2189 GC_noop1((word)&PCR_GC_Run);
2190 GC_noop1((word)&PCR_GC_Setup);
2191 GC_noop1((word)&test);
2192 # endif
2193 n_tests = 0;
2194 /* GC_enable_incremental(); */
2195 GC_set_warn_proc(warn_proc);
2196 set_print_procs();
2197 th1 = PCR_Th_Fork(run_one_test, 0);
2198 th2 = PCR_Th_Fork(run_one_test, 0);
2199 run_one_test();
2200 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
2201 != PCR_ERes_okay || code != 0) {
2202 GC_printf("Thread 1 failed\n");
2203 }
2204 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
2205 != PCR_ERes_okay || code != 0) {
2206 GC_printf("Thread 2 failed\n");
2207 }
2208 check_heap_stats();
2209 return(0);
2210 }
2211 #endif
2212
2213 #if defined(GC_PTHREADS)
2214 void * thr_run_one_test(void * arg GC_ATTR_UNUSED)
2215 {
2216 run_one_test();
2217 return(0);
2218 }
2219
2220 #ifdef GC_DEBUG
2221 # define GC_free GC_debug_free
2222 #endif
2223
2224 int main(void)
2225 {
2226 # if NTHREADS > 0
2227 pthread_t th[NTHREADS];
2228 int i;
2229 # endif
2230 pthread_attr_t attr;
2231 int code;
2232 # ifdef GC_IRIX_THREADS
2233 /* Force a larger stack to be preallocated */
2234 /* Since the initial can't always grow later. */
2235 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 MB */
2236 # endif /* GC_IRIX_THREADS */
2237 # if defined(GC_HPUX_THREADS)
2238 /* Default stack size is too small, especially with the 64 bit ABI */
2239 /* Increase it. */
2240 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
2241 GC_printf("pthread_default_stacksize_np failed\n");
2242 }
2243 # endif /* GC_HPUX_THREADS */
2244 # ifdef PTW32_STATIC_LIB
2245 pthread_win32_process_attach_np ();
2246 pthread_win32_thread_attach_np ();
2247 # endif
2248 # if defined(GC_DARWIN_THREADS) && !defined(GC_NO_THREADS_DISCOVERY) \
2249 && !defined(DARWIN_DONT_PARSE_STACK) && !defined(THREAD_LOCAL_ALLOC)
2250 /* Test with the Darwin implicit thread registration. */
2251 GC_use_threads_discovery();
2252 GC_printf("Using Darwin task-threads-based world stop and push\n");
2253 # endif
2254 GC_COND_INIT();
2255
2256 if ((code = pthread_attr_init(&attr)) != 0) {
2257 GC_printf("pthread_attr_init failed, error=%d\n", code);
2258 FAIL;
2259 }
2260 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
2261 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) \
2262 || defined(GC_OPENBSD_THREADS)
2263 if ((code = pthread_attr_setstacksize(&attr, 1000 * 1024)) != 0) {
2264 GC_printf("pthread_attr_setstacksize failed, error=%d\n", code);
2265 FAIL;
2266 }
2267 # endif
2268 n_tests = 0;
2269 # if !defined(GC_DISABLE_INCREMENTAL) \
2270 && (defined(TEST_DEFAULT_VDB) || !defined(DEFAULT_VDB))
2271 # if !defined(REDIRECT_MALLOC) && !defined(MAKE_BACK_GRAPH) \
2272 && !defined(USE_PROC_FOR_LIBRARIES) && !defined(NO_INCREMENTAL) \
2273 && !defined(USE_MUNMAP)
2274 GC_enable_incremental();
2275 # endif
2276 if (GC_is_incremental_mode()) {
2277 GC_printf("Switched to incremental mode\n");
2278 # ifdef MPROTECT_VDB
2279 GC_printf("Emulating dirty bits with mprotect/signals\n");
2280 # endif
2281 }
2282 # endif
2283 GC_set_min_bytes_allocd(1);
2284 if (GC_get_min_bytes_allocd() != 1)
2285 FAIL;
2286 GC_set_rate(10);
2287 GC_set_max_prior_attempts(1);
2288 if (GC_get_rate() != 10 || GC_get_max_prior_attempts() != 1)
2289 FAIL;
2290 GC_set_warn_proc(warn_proc);
2291 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
2292 GC_printf("Key creation failed %d\n", code);
2293 FAIL;
2294 }
2295 set_print_procs();
2296 # if NTHREADS > 0
2297 for (i = 0; i < NTHREADS; ++i) {
2298 if ((code = pthread_create(th+i, &attr, thr_run_one_test, 0)) != 0) {
2299 GC_printf("Thread %d creation failed %d\n", i, code);
2300 FAIL;
2301 }
2302 }
2303 # endif
2304 run_one_test();
2305 # if NTHREADS > 0
2306 for (i = 0; i < NTHREADS; ++i) {
2307 if ((code = pthread_join(th[i], 0)) != 0) {
2308 GC_printf("Thread %d failed %d\n", i, code);
2309 FAIL;
2310 }
2311 }
2312 # endif
2313 check_heap_stats();
2314 (void)fflush(stdout);
2315 (void)pthread_attr_destroy(&attr);
2316 # if defined(CPPCHECK)
2317 UNTESTED(GC_set_suspend_signal);
2318 UNTESTED(GC_set_thr_restart_signal);
2319 # ifndef GC_NO_DLOPEN
2320 UNTESTED(GC_dlopen);
2321 # endif
2322 # ifndef GC_NO_PTHREAD_CANCEL
2323 UNTESTED(GC_pthread_cancel);
2324 # endif
2325 # ifdef GC_HAVE_PTHREAD_EXIT
2326 UNTESTED(GC_pthread_exit);
2327 # endif
2328 # ifndef GC_NO_PTHREAD_SIGMASK
2329 UNTESTED(GC_pthread_sigmask);
2330 # endif
2331 # ifdef NO_TEST_HANDLE_FORK
2332 UNTESTED(GC_atfork_child);
2333 UNTESTED(GC_atfork_parent);
2334 UNTESTED(GC_atfork_prepare);
2335 UNTESTED(GC_set_handle_fork);
2336 UNTESTED(GC_start_mark_threads);
2337 # endif
2338 # endif /* CPPCHECK */
2339 # ifdef PTW32_STATIC_LIB
2340 pthread_win32_thread_detach_np ();
2341 pthread_win32_process_detach_np ();
2342 # endif
2343 return(0);
2344 }
2345 #endif /* GC_PTHREADS */