Debian Codegraph @

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