Debian Codegraph @

gc/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-2021 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 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*)GC_malloc(0);
1417 CHECK_OUT_OF_MEMORY(x);
1418 if (GC_size(x) != MIN_WORDS * sizeof(GC_word)) {
1419 GC_printf("GC_malloc(0) failed: GC_size returns %lu\n",
1420 (unsigned long)GC_size(x));
1421 FAIL;
1422 }
1423 AO_fetch_and_add1(&uncollectable_count);
1424 x = (char*)GC_malloc_uncollectable(0);
1425 CHECK_OUT_OF_MEMORY(x);
1426 if (GC_size(x) != MIN_WORDS * sizeof(GC_word)) {
1427 GC_printf("GC_malloc_uncollectable(0) failed\n");
1428 FAIL;
1429 }
1430 AO_fetch_and_add1(&collectable_count);
1431 x = (char*)GC_malloc(16);
1432 CHECK_OUT_OF_MEMORY(x);
1433 if (GC_base(GC_PTR_ADD(x, 13)) != x) {
1434 GC_printf("GC_base(heap ptr) produced incorrect result\n");
1435 FAIL;
1436 }
1437 if (!GC_is_heap_ptr(x)) {
1438 GC_printf("GC_is_heap_ptr(heap_ptr) produced incorrect result\n");
1439 FAIL;
1440 }
1441 if (GC_is_heap_ptr(&x)) {
1442 GC_printf("GC_is_heap_ptr(&local_var) produced incorrect result\n");
1443 FAIL;
1444 }
1445 if (GC_is_heap_ptr((void *)&fail_count) || GC_is_heap_ptr(NULL)) {
1446 GC_printf("GC_is_heap_ptr(&global_var) produced incorrect result\n");
1447 FAIL;
1448 }
1449 (void)GC_PRE_INCR(x, 0);
1450 (void)GC_POST_INCR(x);
1451 (void)GC_POST_DECR(x);
1452 if (GC_base(x) != x) {
1453 GC_printf("Bad INCR/DECR result\n");
1454 FAIL;
1455 }
1456 y = (char *)(GC_word)fail_proc1;
1457 # ifndef PCR
1458 if (GC_base(y) != 0) {
1459 GC_printf("GC_base(fn_ptr) produced incorrect result\n");
1460 FAIL;
1461 }
1462 # endif
1463 if (GC_same_obj(x+5, x) != x + 5) {
1464 GC_printf("GC_same_obj produced incorrect result\n");
1465 FAIL;
1466 }
1467 if (GC_is_visible(y) != y || GC_is_visible(x) != x) {
1468 GC_printf("GC_is_visible produced incorrect result\n");
1469 FAIL;
1470 }
1471 z = (char**)GC_malloc(8);
1472 CHECK_OUT_OF_MEMORY(z);
1473 AO_fetch_and_add1(&collectable_count);
1474 GC_PTR_STORE(z, x);
1475 GC_end_stubborn_change(z);
1476 if (*z != x) {
1477 GC_printf("GC_PTR_STORE failed: %p != %p\n", (void *)(*z), (void *)x);
1478 FAIL;
1479 }
1480 # if !defined(IA64) && !defined(POWERPC)
1481 if (!TEST_FAIL_COUNT(1)) {
1482 /* On POWERPCs function pointers point to a descriptor in the */
1483 /* data segment, so there should have been no failures. */
1484 /* The same applies to IA64. */
1485 GC_printf("GC_is_visible produced wrong failure indication\n");
1486 FAIL;
1487 }
1488 # endif
1489 if (GC_is_valid_displacement(y) != y
1490 || GC_is_valid_displacement(x) != x
1491 || GC_is_valid_displacement(x + 3) != x + 3) {
1492 GC_printf("GC_is_valid_displacement produced incorrect result\n");
1493 FAIL;
1494 }
1495 {
1496 size_t i;
1497
1498 (void)GC_malloc(17);
1499 AO_fetch_and_add1(&collectable_count);
1500 for (i = sizeof(GC_word); i < 512; i *= 2) {
1501 GC_word result = (GC_word) GC_memalign(i, 17);
1502 if (result % i != 0 || result == 0 || *(int *)result != 0) FAIL;
1503 }
1504 }
1505 # ifndef ALL_INTERIOR_POINTERS
1506 # if defined(POWERPC)
1507 if (!TEST_FAIL_COUNT(1))
1508 # else
1509 if (!TEST_FAIL_COUNT(GC_get_all_interior_pointers() ? 1 : 2))
1510 # endif
1511 {
1512 GC_printf(
1513 "GC_is_valid_displacement produced wrong failure indication\n");
1514 FAIL;
1515 }
1516 # endif
1517 # endif /* DBG_HDRS_ALL */
1518 /* Test floating point alignment */
1519 {
1520 double *dp = GC_NEW(double);
1521 CHECK_OUT_OF_MEMORY(dp);
1522 AO_fetch_and_add1(&collectable_count);
1523 *dp = 1.0;
1524 dp = GC_NEW(double);
1525 CHECK_OUT_OF_MEMORY(dp);
1526 AO_fetch_and_add1(&collectable_count);
1527 *dp = 1.0;
1528 }
1529 /* Test size 0 allocation a bit more */
1530 {
1531 size_t i;
1532 for (i = 0; i < 10000; ++i) {
1533 (void)checkOOM(GC_MALLOC(0));
1534 AO_fetch_and_add1(&collectable_count);
1535 GC_FREE(checkOOM(GC_MALLOC(0)));
1536 (void)checkOOM(GC_MALLOC_ATOMIC(0));
1537 AO_fetch_and_add1(&atomic_count);
1538 GC_FREE(checkOOM(GC_MALLOC_ATOMIC(0)));
1539 test_generic_malloc_or_special(GC_malloc_atomic(1));
1540 AO_fetch_and_add1(&atomic_count);
1541 GC_FREE(checkOOM(GC_MALLOC_ATOMIC_IGNORE_OFF_PAGE(1)));
1542 GC_FREE(checkOOM(GC_MALLOC_IGNORE_OFF_PAGE(2)));
1543 }
1544 }
1545 thr_hndl_sb.gc_thread_handle = GC_get_my_stackbottom(&thr_hndl_sb.sb);
1546 # ifdef GC_GCJ_SUPPORT
1547 GC_REGISTER_DISPLACEMENT(sizeof(struct fake_vtable *));
1548 GC_init_gcj_malloc(0, (void *)(GC_word)fake_gcj_mark_proc);
1549 # endif
1550 /* Make sure that fn arguments are visible to the collector. */
1551 uniq(
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 GC_malloc(12), GC_malloc(12), GC_malloc(12),
1561 (GC_gcollect(),GC_malloc(12)),
1562 (void *)0);
1563 /* GC_malloc(0) must return NULL or something we can deallocate. */
1564 GC_free(checkOOM(GC_malloc(0)));
1565 GC_free(checkOOM(GC_malloc_atomic(0)));
1566 GC_free(checkOOM(GC_malloc(0)));
1567 GC_free(checkOOM(GC_malloc_atomic(0)));
1568 # ifndef NO_TEST_HANDLE_FORK
1569 GC_atfork_prepare();
1570 pid = fork();
1571 if (pid != 0) {
1572 GC_atfork_parent();
1573 if (pid == -1) {
1574 GC_printf("Process fork failed\n");
1575 FAIL;
1576 }
1577 if (print_stats)
1578 GC_log_printf("Forked child process, pid= %ld\n", (long)pid);
1579 if (waitpid(pid, &wstatus, 0) == -1) {
1580 GC_printf("Wait for child process failed\n");
1581 FAIL;
1582 }
1583 if (!WIFEXITED(wstatus) || WEXITSTATUS(wstatus) != 0) {
1584 GC_printf("Child process failed, pid= %ld, status= 0x%x\n",
1585 (long)pid, wstatus);
1586 FAIL;
1587 }
1588 } else {
1589 pid_t child_pid = getpid();
1590
1591 GC_atfork_child();
1592 if (print_stats)
1593 GC_log_printf("Started a child process, pid= %ld\n",
1594 (long)child_pid);
1595 # ifdef PARALLEL_MARK
1596 GC_gcollect(); /* no parallel markers */
1597 # endif
1598 GC_start_mark_threads();
1599 GC_gcollect();
1600 # ifdef THREADS
1601 /* Skip "Premature finalization" check in the */
1602 /* child process because there could be a chance */
1603 /* that some other thread of the parent was */
1604 /* executing mktree at the moment of fork. */
1605 dropped_something = 1;
1606 # endif
1607 tree_test();
1608 # if !defined(DBG_HDRS_ALL) && !defined(NO_TYPED_TEST)
1609 typed_test();
1610 # endif
1611 # ifdef THREADS
1612 if (print_stats)
1613 GC_log_printf("Starting tiny reverse test, pid= %ld\n",
1614 (long)child_pid);
1615 tiny_reverse_test(0);
1616 GC_gcollect();
1617 # endif
1618 if (print_stats)
1619 GC_log_printf("Finished a child process, pid= %ld\n",
1620 (long)child_pid);
1621 exit(0);
1622 }
1623 # endif
1624 (void)GC_call_with_alloc_lock(set_stackbottom, &thr_hndl_sb);
1625
1626 /* Repeated list reversal test. */
1627 # ifndef NO_CLOCK
1628 GET_TIME(start_time);
1629 # endif
1630 reverse_test();
1631 # ifndef NO_CLOCK
1632 if (print_stats) {
1633 GET_TIME(reverse_time);
1634 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1635 GC_log_printf("Finished reverse_test at time %u (%p)\n",
1636 (unsigned) time_diff, (void *)&start_time);
1637 }
1638 # endif
1639 # if !defined(DBG_HDRS_ALL) && !defined(NO_TYPED_TEST)
1640 typed_test();
1641 # ifndef NO_CLOCK
1642 if (print_stats) {
1643 CLOCK_TYPE typed_time;
1644
1645 GET_TIME(typed_time);
1646 time_diff = MS_TIME_DIFF(typed_time, start_time);
1647 GC_log_printf("Finished typed_test at time %u (%p)\n",
1648 (unsigned) time_diff, (void *)&start_time);
1649 }
1650 # endif
1651 # endif /* DBG_HDRS_ALL */
1652 tree_test();
1653 # ifdef TEST_WITH_SYSTEM_MALLOC
1654 free(checkOOM(calloc(1,1)));
1655 free(checkOOM(realloc(NULL, 64)));
1656 # endif
1657 # ifndef NO_CLOCK
1658 if (print_stats) {
1659 CLOCK_TYPE tree_time;
1660
1661 GET_TIME(tree_time);
1662 time_diff = MS_TIME_DIFF(tree_time, start_time);
1663 GC_log_printf("Finished tree_test at time %u (%p)\n",
1664 (unsigned) time_diff, (void *)&start_time);
1665 }
1666 # endif
1667 /* Run reverse_test a second time, so we hopefully notice corruption. */
1668 reverse_test();
1669 # ifndef NO_CLOCK
1670 if (print_stats) {
1671 GET_TIME(reverse_time);
1672 time_diff = MS_TIME_DIFF(reverse_time, start_time);
1673 GC_log_printf("Finished second reverse_test at time %u (%p)\n",
1674 (unsigned)time_diff, (void *)&start_time);
1675 }
1676 # endif
1677 /* GC_allocate_ml and GC_need_to_lock are no longer exported, and */
1678 /* AO_fetch_and_add1() may be unavailable to update a counter. */
1679 (void)GC_call_with_alloc_lock(inc_int_counter, &n_tests);
1680 # ifndef NO_CLOCK
1681 if (print_stats)
1682 GC_log_printf("Finished %p\n", (void *)&start_time);
1683 # endif
1684 }
1685
1686 /* Execute some tests after termination of other test threads (if any). */
1687 void run_single_threaded_test(void) {
1688 GC_disable();
1689 GC_FREE(checkOOM(GC_MALLOC(100)));
1690 GC_enable();
1691 }
1692
1693 void GC_CALLBACK reachable_objs_counter(void *obj, size_t size,
1694 void *pcounter)
1695 {
1696 if (0 == size) {
1697 GC_printf("Reachable object has zero size\n");
1698 FAIL;
1699 }
1700 if (GC_base(obj) != obj) {
1701 GC_printf("Invalid reachable object base passed by enumerator: %p\n",
1702 obj);
1703 FAIL;
1704 }
1705 if (GC_size(obj) != size) {
1706 GC_printf("Invalid reachable object size passed by enumerator: %lu\n",
1707 (unsigned long)size);
1708 FAIL;
1709 }
1710 (*(unsigned *)pcounter)++;
1711 }
1712
1713 #define NUMBER_ROUND_UP(v, bound) ((((v) + (bound) - 1) / (bound)) * (bound))
1714
1715 void check_heap_stats(void)
1716 {
1717 size_t max_heap_sz;
1718 int i;
1719 # ifndef GC_NO_FINALIZATION
1720 # ifdef FINALIZE_ON_DEMAND
1721 int late_finalize_count = 0;
1722 # endif
1723 # endif
1724 unsigned obj_count = 0;
1725
1726 if (!GC_is_init_called()) {
1727 GC_printf("GC should be initialized!\n");
1728 FAIL;
1729 }
1730
1731 /* The upper bounds are a guess, which has been empirically */
1732 /* adjusted. On low-end uniprocessors with incremental GC */
1733 /* these may be particularly dubious, since empirically the */
1734 /* heap tends to grow largely as a result of the GC not */
1735 /* getting enough cycles. */
1736 # if CPP_WORDSZ == 64
1737 max_heap_sz = 26000000;
1738 # else
1739 max_heap_sz = 16000000;
1740 # endif
1741 # ifdef VERY_SMALL_CONFIG
1742 max_heap_sz /= 4;
1743 # endif
1744 # ifdef GC_DEBUG
1745 max_heap_sz *= 2;
1746 # ifdef SAVE_CALL_CHAIN
1747 max_heap_sz *= 3;
1748 # ifdef SAVE_CALL_COUNT
1749 max_heap_sz += max_heap_sz * NFRAMES / 4;
1750 # endif
1751 # endif
1752 # endif
1753 # if defined(ADDRESS_SANITIZER) && !defined(__clang__)
1754 max_heap_sz = max_heap_sz * 2 - max_heap_sz / 3;
1755 # endif
1756 # ifdef MEMORY_SANITIZER
1757 max_heap_sz += max_heap_sz / 4;
1758 # endif
1759 max_heap_sz *= n_tests;
1760 # if defined(USE_MMAP) || defined(MSWIN32)
1761 max_heap_sz = NUMBER_ROUND_UP(max_heap_sz, 4 * 1024 * 1024);
1762 # endif
1763 /* Garbage collect repeatedly so that all inaccessible objects */
1764 /* can be finalized. */
1765 if (!GC_is_disabled())
1766 while (GC_collect_a_little()) { }
1767 for (i = 0; i < 16; i++) {
1768 GC_gcollect();
1769 # ifndef GC_NO_FINALIZATION
1770 # ifdef FINALIZE_ON_DEMAND
1771 late_finalize_count +=
1772 # endif
1773 GC_invoke_finalizers();
1774 # endif
1775 }
1776 if (print_stats) {
1777 struct GC_stack_base sb;
1778 int res = GC_get_stack_base(&sb);
1779
1780 if (res == GC_SUCCESS) {
1781 GC_log_printf("Primordial thread stack bottom: %p\n", sb.mem_base);
1782 } else if (res == GC_UNIMPLEMENTED) {
1783 GC_log_printf("GC_get_stack_base() unimplemented\n");
1784 } else {
1785 GC_printf("GC_get_stack_base() failed: %d\n", res);
1786 FAIL;
1787 }
1788 }
1789 GC_alloc_lock();
1790 GC_enumerate_reachable_objects_inner(reachable_objs_counter, &obj_count);
1791 GC_alloc_unlock();
1792 GC_printf("Completed %u tests\n", n_tests);
1793 GC_printf("Allocated %d collectable objects\n", (int)collectable_count);
1794 GC_printf("Allocated %d uncollectable objects\n",
1795 (int)uncollectable_count);
1796 GC_printf("Allocated %d atomic objects\n", (int)atomic_count);
1797 GC_printf("Reallocated %d objects\n", (int)realloc_count);
1798 # ifndef NO_TEST_HANDLE_FORK
1799 GC_printf("Garbage collection after fork is tested too\n");
1800 # endif
1801 # ifndef GC_NO_FINALIZATION
1802 if (!GC_get_find_leak()) {
1803 int still_live = 0;
1804 # ifndef GC_LONG_REFS_NOT_NEEDED
1805 int still_long_live = 0;
1806 # endif
1807
1808 # ifdef FINALIZE_ON_DEMAND
1809 if (finalized_count != late_finalize_count) {
1810 GC_printf("Finalized %d/%d objects - demand finalization error\n",
1811 finalized_count, finalizable_count);
1812 FAIL;
1813 }
1814 # endif
1815 if (finalized_count > finalizable_count
1816 || finalized_count < finalizable_count/2) {
1817 GC_printf("Finalized %d/%d objects - "
1818 "finalization is probably broken\n",
1819 finalized_count, finalizable_count);
1820 FAIL;
1821 } else {
1822 GC_printf("Finalized %d/%d objects - finalization is probably OK\n",
1823 finalized_count, finalizable_count);
1824 }
1825 for (i = 0; i < MAX_FINALIZED; i++) {
1826 if (live_indicators[i] != 0) {
1827 still_live++;
1828 }
1829 # ifndef GC_LONG_REFS_NOT_NEEDED
1830 if (live_long_refs[i] != NULL) {
1831 still_long_live++;
1832 }
1833 # endif
1834 }
1835 i = finalizable_count - finalized_count - still_live;
1836 if (0 != i) {
1837 GC_printf("%d disappearing links remain and %d more objects "
1838 "were not finalized\n", still_live, i);
1839 if (i > 10) {
1840 GC_printf("\tVery suspicious!\n");
1841 } else {
1842 GC_printf("\tSlightly suspicious, but probably OK\n");
1843 }
1844 }
1845 # ifndef GC_LONG_REFS_NOT_NEEDED
1846 if (0 != still_long_live) {
1847 GC_printf("%d 'long' links remain\n", still_long_live);
1848 }
1849 # endif
1850 }
1851 # endif
1852 GC_printf("Total number of bytes allocated is %lu\n",
1853 (unsigned long)GC_get_total_bytes());
1854 GC_printf("Total memory use by allocated blocks is %lu bytes\n",
1855 (unsigned long)GC_get_memory_use());
1856 GC_printf("Final heap size is %lu bytes\n",
1857 (unsigned long)GC_get_heap_size());
1858 if (GC_get_total_bytes() < (size_t)n_tests *
1859 # ifdef VERY_SMALL_CONFIG
1860 2700000
1861 # else
1862 33500000
1863 # endif
1864 ) {
1865 GC_printf("Incorrect execution - missed some allocations\n");
1866 FAIL;
1867 }
1868 if (GC_get_heap_size() + GC_get_unmapped_bytes() > max_heap_sz
1869 && !GC_get_find_leak()) {
1870 GC_printf("Unexpected heap growth - collector may be broken"
1871 " (heapsize: %lu, expected: %lu)\n",
1872 (unsigned long)(GC_get_heap_size() + GC_get_unmapped_bytes()),
1873 (unsigned long)max_heap_sz);
1874 FAIL;
1875 }
1876 # ifdef USE_MUNMAP
1877 GC_printf("Obtained %lu bytes from OS (of which %lu bytes unmapped)\n",
1878 (unsigned long)GC_get_obtained_from_os_bytes(),
1879 (unsigned long)GC_get_unmapped_bytes());
1880 # else
1881 GC_printf("Obtained %lu bytes from OS\n",
1882 (unsigned long)GC_get_obtained_from_os_bytes());
1883 # endif
1884 GC_printf("Final number of reachable objects is %u\n", obj_count);
1885
1886 # ifndef GC_GET_HEAP_USAGE_NOT_NEEDED
1887 /* Get global counters (just to check the functions work). */
1888 GC_get_heap_usage_safe(NULL, NULL, NULL, NULL, NULL);
1889 {
1890 struct GC_prof_stats_s stats;
1891 (void)GC_get_prof_stats(&stats, sizeof(stats));
1892 # ifdef THREADS
1893 (void)GC_get_prof_stats_unsafe(&stats, sizeof(stats));
1894 # endif
1895 }
1896 (void)GC_get_size_map_at(-1);
1897 (void)GC_get_size_map_at(1);
1898 # endif
1899 if (GC_size(NULL) != 0) {
1900 GC_printf("GC_size(NULL) failed\n");
1901 FAIL;
1902 }
1903
1904 # ifdef NO_CLOCK
1905 GC_printf("Completed %u collections\n", (unsigned)GC_get_gc_no());
1906 # elif !defined(PARALLEL_MARK)
1907 GC_printf("Completed %u collections in %lu ms\n",
1908 (unsigned)GC_get_gc_no(), GC_get_full_gc_total_time());
1909 # else
1910 GC_printf("Completed %u collections in %lu ms"
1911 " (using %d marker threads)\n",
1912 (unsigned)GC_get_gc_no(), GC_get_full_gc_total_time(),
1913 GC_get_parallel() + 1);
1914 # endif
1915 GC_printf("Collector appears to work\n");
1916 }
1917
1918 #if defined(MACOS)
1919 void SetMinimumStack(long minSize)
1920 {
1921 if (minSize > LMGetDefltStack())
1922 {
1923 long newApplLimit = (long) GetApplLimit()
1924 - (minSize - LMGetDefltStack());
1925 SetApplLimit((Ptr) newApplLimit);
1926 MaxApplZone();
1927 }
1928 }
1929
1930 #define cMinStackSpace (512L * 1024L)
1931
1932 #endif
1933
1934 void GC_CALLBACK warn_proc(char *msg, GC_word p)
1935 {
1936 GC_printf(msg, (unsigned long)p);
1937 /*FAIL;*/
1938 }
1939
1940 void enable_incremental_mode(void)
1941 {
1942 # if (defined(TEST_DEFAULT_VDB) || defined(TEST_MANUAL_VDB) \
1943 || !defined(DEFAULT_VDB)) && !defined(GC_DISABLE_INCREMENTAL)
1944 # if !defined(MAKE_BACK_GRAPH) && !defined(NO_INCREMENTAL) \
1945 && !defined(REDIRECT_MALLOC) && !defined(USE_PROC_FOR_LIBRARIES)
1946 GC_enable_incremental();
1947 # endif
1948 if (GC_is_incremental_mode()) {
1949 # ifndef SMALL_CONFIG
1950 if (GC_get_manual_vdb_allowed()) {
1951 GC_printf("Switched to incremental mode (manual VDB)\n");
1952 } else
1953 # endif
1954 /* else */ {
1955 GC_printf("Switched to incremental mode\n");
1956 if (GC_incremental_protection_needs() == GC_PROTECTS_NONE) {
1957 # if defined(PROC_VDB) || defined(SOFT_VDB)
1958 GC_printf("Reading dirty bits from /proc\n");
1959 # elif defined(GWW_VDB)
1960 GC_printf("Using GetWriteWatch-based implementation\n");
1961 # endif
1962 } else {
1963 GC_printf("Emulating dirty bits with mprotect/signals\n");
1964 }
1965 }
1966 }
1967 # endif
1968 }
1969
1970 #if defined(CPPCHECK)
1971 # include "javaxfc.h" /* for GC_finalize_all */
1972 # define UNTESTED(sym) GC_noop1((word)&sym)
1973 #endif
1974
1975 #if defined(MSWINCE) && defined(UNDER_CE)
1976 # define WINMAIN_LPTSTR LPWSTR
1977 #else
1978 # define WINMAIN_LPTSTR LPSTR
1979 #endif
1980
1981 #if !defined(PCR) && !defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
1982
1983 #if defined(CPPCHECK) && defined(_MSC_VER) && !defined(_M_ARM) \
1984 && !defined(_M_ARM64) && !defined(_M_X64)
1985 # include "private/msvc_dbg.h"
1986 #endif
1987
1988 #if defined(_DEBUG) && (_MSC_VER >= 1900) /* VS 2015+ */
1989 # ifndef _CRTDBG_MAP_ALLOC
1990 # define _CRTDBG_MAP_ALLOC
1991 # endif
1992 # include <crtdbg.h>
1993 /* Ensure that there is no system-malloc-allocated objects at normal */
1994 /* exit (i.e. no such memory leaked). */
1995 # define CRTMEM_CHECK_INIT() \
1996 (void)_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF)
1997 # define CRTMEM_DUMP_LEAKS() \
1998 do { \
1999 if (_CrtDumpMemoryLeaks()) { \
2000 GC_printf("System-malloc-allocated memory leaked\n"); \
2001 FAIL; \
2002 } \
2003 } while (0)
2004 #else
2005 # define CRTMEM_CHECK_INIT() (void)0
2006 # define CRTMEM_DUMP_LEAKS() (void)0
2007 #endif /* !_MSC_VER */
2008
2009 #if ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE)) \
2010 && !defined(NO_WINMAIN_ENTRY)
2011 int APIENTRY WinMain(HINSTANCE instance GC_ATTR_UNUSED,
2012 HINSTANCE prev GC_ATTR_UNUSED,
2013 WINMAIN_LPTSTR cmd GC_ATTR_UNUSED,
2014 int n GC_ATTR_UNUSED)
2015 #elif defined(RTEMS)
2016 # include <bsp.h>
2017 # define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
2018 # define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER
2019 # define CONFIGURE_RTEMS_INIT_TASKS_TABLE
2020 # define CONFIGURE_MAXIMUM_TASKS 1
2021 # define CONFIGURE_INIT
2022 # define CONFIGURE_INIT_TASK_STACK_SIZE (64*1024)
2023 # include <rtems/confdefs.h>
2024 rtems_task Init(rtems_task_argument ignord)
2025 #else
2026 int main(void)
2027 #endif
2028 {
2029 CRTMEM_CHECK_INIT();
2030 # if defined(CPPCHECK) && !defined(NO_WINMAIN_ENTRY) \
2031 && ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE))
2032 GC_noop1((GC_word)&WinMain);
2033 # elif defined(CPPCHECK) && defined(RTEMS)
2034 GC_noop1((GC_word)&Init);
2035 # endif
2036 n_tests = 0;
2037 GC_clear_exclusion_table(); /* no-op as called before GC init */
2038 # if defined(MACOS)
2039 /* Make sure we have lots and lots of stack space. */
2040 SetMinimumStack(cMinStackSpace);
2041 /* Cheat and let stdio initialize toolbox for us. */
2042 printf("Testing GC Macintosh port\n");
2043 # endif
2044 GC_COND_INIT();
2045 GC_set_warn_proc(warn_proc);
2046 enable_incremental_mode();
2047 set_print_procs();
2048 GC_start_incremental_collection();
2049 run_one_test();
2050 # if NTHREADS > 0
2051 {
2052 int i;
2053 for (i = 0; i < NTHREADS; i++)
2054 run_one_test();
2055 }
2056 # endif
2057 run_single_threaded_test();
2058 check_heap_stats();
2059 # ifndef MSWINCE
2060 fflush(stdout);
2061 # endif
2062 # if defined(CPPCHECK)
2063 /* Entry points we should be testing, but aren't. */
2064 # ifndef GC_DEBUG
2065 UNTESTED(GC_debug_generic_or_special_malloc);
2066 UNTESTED(GC_debug_register_displacement);
2067 UNTESTED(GC_post_incr);
2068 UNTESTED(GC_pre_incr);
2069 # ifdef GC_GCJ_SUPPORT
2070 UNTESTED(GC_debug_gcj_malloc);
2071 # endif
2072 # endif
2073 # ifdef AMIGA
2074 # ifdef GC_AMIGA_FASTALLOC
2075 UNTESTED(GC_amiga_get_mem);
2076 # endif
2077 # ifndef GC_AMIGA_ONLYFAST
2078 UNTESTED(GC_amiga_set_toany);
2079 # endif
2080 # endif
2081 # if defined(MACOS) && defined(USE_TEMPORARY_MEMORY)
2082 UNTESTED(GC_MacTemporaryNewPtr);
2083 # endif
2084 # if !defined(_M_ARM) && !defined(_M_ARM64) \
2085 && !defined(_M_X64) && defined(_MSC_VER)
2086 UNTESTED(GetFileLineFromStack);
2087 UNTESTED(GetModuleNameFromStack);
2088 UNTESTED(GetSymbolNameFromStack);
2089 # endif
2090 UNTESTED(GC_abort_on_oom);
2091 UNTESTED(GC_malloc_explicitly_typed_ignore_off_page);
2092 UNTESTED(GC_debug_strndup);
2093 UNTESTED(GC_deinit);
2094 UNTESTED(GC_strndup);
2095 UNTESTED(GC_posix_memalign);
2096 UNTESTED(GC_new_proc);
2097 UNTESTED(GC_clear_roots);
2098 UNTESTED(GC_exclude_static_roots);
2099 UNTESTED(GC_expand_hp);
2100 UNTESTED(GC_register_describe_type_fn);
2101 UNTESTED(GC_register_has_static_roots_callback);
2102 # ifdef GC_GCJ_SUPPORT
2103 UNTESTED(GC_gcj_malloc_ignore_off_page);
2104 # endif
2105 # ifndef NO_DEBUGGING
2106 UNTESTED(GC_dump);
2107 UNTESTED(GC_dump_regions);
2108 UNTESTED(GC_is_tmp_root);
2109 UNTESTED(GC_print_free_list);
2110 # endif
2111 # ifdef TRACE_BUF
2112 UNTESTED(GC_print_trace);
2113 # endif
2114 # ifndef GC_NO_FINALIZATION
2115 UNTESTED(GC_debug_register_finalizer_unreachable);
2116 UNTESTED(GC_register_disappearing_link);
2117 UNTESTED(GC_should_invoke_finalizers);
2118 # ifndef JAVA_FINALIZATION_NOT_NEEDED
2119 UNTESTED(GC_finalize_all);
2120 # endif
2121 # ifndef NO_DEBUGGING
2122 UNTESTED(GC_dump_finalization);
2123 # endif
2124 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
2125 UNTESTED(GC_toggleref_add);
2126 # endif
2127 # endif
2128 # if !defined(OS2) && !defined(MACOS) && !defined(GC_ANDROID_LOG) \
2129 && !defined(MSWIN32) && !defined(MSWINCE)
2130 UNTESTED(GC_set_log_fd);
2131 # endif
2132 # ifndef REDIRECT_MALLOC_IN_HEADER
2133 # ifdef REDIRECT_MALLOC
2134 # ifndef strndup
2135 UNTESTED(strndup);
2136 # endif
2137 # ifndef strdup
2138 UNTESTED(strdup);
2139 # endif
2140 # endif
2141 # ifdef REDIRECT_REALLOC
2142 UNTESTED(realloc);
2143 # endif
2144 # endif /* !REDIRECT_MALLOC_IN_HEADER */
2145 # ifdef GC_REQUIRE_WCSDUP
2146 UNTESTED(GC_wcsdup);
2147 UNTESTED(GC_debug_wcsdup);
2148 # endif
2149 # endif
2150 # if defined(MSWIN32) || defined(MSWINCE) || defined(CYGWIN32)
2151 GC_win32_free_heap();
2152 # endif
2153 CRTMEM_DUMP_LEAKS();
2154 # ifdef RTEMS
2155 exit(0);
2156 # else
2157 return(0);
2158 # endif
2159 }
2160 # endif /* !GC_WIN32_THREADS && !GC_PTHREADS */
2161
2162 #if defined(GC_WIN32_THREADS) && !defined(GC_PTHREADS)
2163
2164 DWORD __stdcall thr_run_one_test(void * arg GC_ATTR_UNUSED)
2165 {
2166 run_one_test();
2167 return 0;
2168 }
2169
2170 #ifdef MSWINCE
2171 HANDLE win_created_h;
2172 HWND win_handle;
2173
2174 LRESULT CALLBACK window_proc(HWND hwnd, UINT uMsg, WPARAM wParam,
2175 LPARAM lParam)
2176 {
2177 LRESULT ret = 0;
2178 switch (uMsg) {
2179 case WM_HIBERNATE:
2180 GC_printf("Received WM_HIBERNATE, calling GC_gcollect\n");
2181 /* Force "unmap as much memory as possible" mode. */
2182 GC_gcollect_and_unmap();
2183 break;
2184 case WM_CLOSE:
2185 GC_printf("Received WM_CLOSE, closing window\n");
2186 DestroyWindow(hwnd);
2187 break;
2188 case WM_DESTROY:
2189 PostQuitMessage(0);
2190 break;
2191 default:
2192 ret = DefWindowProc(hwnd, uMsg, wParam, lParam);
2193 break;
2194 }
2195 return ret;
2196 }
2197
2198 DWORD __stdcall thr_window(void * arg GC_ATTR_UNUSED)
2199 {
2200 WNDCLASS win_class = {
2201 CS_NOCLOSE,
2202 window_proc,
2203 0,
2204 0,
2205 GetModuleHandle(NULL),
2206 NULL,
2207 NULL,
2208 (HBRUSH)(COLOR_APPWORKSPACE+1),
2209 NULL,
2210 TEXT("GCtestWindow")
2211 };
2212 MSG msg;
2213
2214 if (!RegisterClass(&win_class))
2215 FAIL;
2216
2217 win_handle = CreateWindowEx(
2218 0,
2219 TEXT("GCtestWindow"),
2220 TEXT("GCtest"),
2221 0,
2222 CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
2223 NULL,
2224 NULL,
2225 GetModuleHandle(NULL),
2226 NULL);
2227
2228 if (win_handle == NULL)
2229 FAIL;
2230
2231 SetEvent(win_created_h);
2232
2233 ShowWindow(win_handle, SW_SHOW);
2234 UpdateWindow(win_handle);
2235
2236 while (GetMessage(&msg, NULL, 0, 0)) {
2237 TranslateMessage(&msg);
2238 DispatchMessage(&msg);
2239 }
2240
2241 return 0;
2242 }
2243 #endif
2244
2245 #if !defined(NO_WINMAIN_ENTRY)
2246 int APIENTRY WinMain(HINSTANCE instance GC_ATTR_UNUSED,
2247 HINSTANCE prev GC_ATTR_UNUSED,
2248 WINMAIN_LPTSTR cmd GC_ATTR_UNUSED,
2249 int n GC_ATTR_UNUSED)
2250 #else
2251 int main(void)
2252 #endif
2253 {
2254 # if NTHREADS > 0
2255 HANDLE h[NTHREADS];
2256 int i;
2257 # endif
2258 # ifdef MSWINCE
2259 HANDLE win_thr_h;
2260 # endif
2261 DWORD thread_id;
2262
2263 # if defined(CPPCHECK) && !defined(NO_WINMAIN_ENTRY)
2264 GC_noop1((GC_word)&WinMain);
2265 # endif
2266 # if defined(GC_DLL) && !defined(GC_NO_THREADS_DISCOVERY) \
2267 && !defined(MSWINCE) && !defined(THREAD_LOCAL_ALLOC)
2268 GC_use_threads_discovery();
2269 /* Test with implicit thread registration if possible. */
2270 GC_printf("Using DllMain to track threads\n");
2271 # endif
2272 GC_COND_INIT();
2273 enable_incremental_mode();
2274 InitializeCriticalSection(&incr_cs);
2275 GC_set_warn_proc(warn_proc);
2276 # ifdef MSWINCE
2277 win_created_h = CreateEvent(NULL, FALSE, FALSE, NULL);
2278 if (win_created_h == (HANDLE)NULL) {
2279 GC_printf("Event creation failed, errcode= %d\n", (int)GetLastError());
2280 FAIL;
2281 }
2282 win_thr_h = CreateThread(NULL, 0, thr_window, 0, 0, &thread_id);
2283 if (win_thr_h == (HANDLE)NULL) {
2284 GC_printf("Thread creation failed, errcode= %d\n", (int)GetLastError());
2285 FAIL;
2286 }
2287 if (WaitForSingleObject(win_created_h, INFINITE) != WAIT_OBJECT_0)
2288 FAIL;
2289 CloseHandle(win_created_h);
2290 # endif
2291 set_print_procs();
2292 # if NTHREADS > 0
2293 for (i = 0; i < NTHREADS; i++) {
2294 h[i] = CreateThread(NULL, 0, thr_run_one_test, 0, 0, &thread_id);
2295 if (h[i] == (HANDLE)NULL) {
2296 GC_printf("Thread creation failed, errcode= %d\n", (int)GetLastError());
2297 FAIL;
2298 }
2299 }
2300 # endif /* NTHREADS > 0 */
2301 run_one_test();
2302 # if NTHREADS > 0
2303 for (i = 0; i < NTHREADS; i++) {
2304 if (WaitForSingleObject(h[i], INFINITE) != WAIT_OBJECT_0) {
2305 GC_printf("Thread wait failed, errcode= %d\n", (int)GetLastError());
2306 FAIL;
2307 }
2308 }
2309 # endif /* NTHREADS > 0 */
2310 # ifdef MSWINCE
2311 PostMessage(win_handle, WM_CLOSE, 0, 0);
2312 if (WaitForSingleObject(win_thr_h, INFINITE) != WAIT_OBJECT_0)
2313 FAIL;
2314 # endif
2315 run_single_threaded_test();
2316 check_heap_stats();
2317 # if defined(CPPCHECK) && defined(GC_WIN32_THREADS)
2318 UNTESTED(GC_ExitThread);
2319 # if !defined(MSWINCE) && !defined(CYGWIN32)
2320 UNTESTED(GC_beginthreadex);
2321 UNTESTED(GC_endthreadex);
2322 # endif
2323 # endif
2324 (void)GC_unregister_my_thread(); /* just to check it works (for main) */
2325 return(0);
2326 }
2327
2328 #endif /* GC_WIN32_THREADS */
2329
2330
2331 #ifdef PCR
2332 int test(void)
2333 {
2334 PCR_Th_T * th1;
2335 PCR_Th_T * th2;
2336 int code;
2337
2338 # if defined(CPPCHECK)
2339 GC_noop1((word)&PCR_GC_Run);
2340 GC_noop1((word)&PCR_GC_Setup);
2341 GC_noop1((word)&test);
2342 # endif
2343 n_tests = 0;
2344 /* GC_enable_incremental(); */
2345 GC_set_warn_proc(warn_proc);
2346 set_print_procs();
2347 th1 = PCR_Th_Fork(run_one_test, 0);
2348 th2 = PCR_Th_Fork(run_one_test, 0);
2349 run_one_test();
2350 if (PCR_Th_T_Join(th1, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
2351 != PCR_ERes_okay || code != 0) {
2352 GC_printf("Thread 1 failed\n");
2353 }
2354 if (PCR_Th_T_Join(th2, &code, NIL, PCR_allSigsBlocked, PCR_waitForever)
2355 != PCR_ERes_okay || code != 0) {
2356 GC_printf("Thread 2 failed\n");
2357 }
2358 run_single_threaded_test();
2359 check_heap_stats();
2360 return(0);
2361 }
2362 #endif
2363
2364 #if defined(GC_PTHREADS)
2365 # include <errno.h> /* for EAGAIN */
2366
2367 void * thr_run_one_test(void * arg GC_ATTR_UNUSED)
2368 {
2369 run_one_test();
2370 return(0);
2371 }
2372
2373 #ifdef GC_DEBUG
2374 # define GC_free GC_debug_free
2375 #endif
2376
2377 int main(void)
2378 {
2379 # if NTHREADS > 0
2380 pthread_t th[NTHREADS];
2381 int i, nthreads;
2382 # endif
2383 pthread_attr_t attr;
2384 int code;
2385 # ifdef GC_IRIX_THREADS
2386 /* Force a larger stack to be preallocated */
2387 /* Since the initial can't always grow later. */
2388 *((volatile char *)&code - 1024*1024) = 0; /* Require 1 MB */
2389 # endif /* GC_IRIX_THREADS */
2390 # if defined(GC_HPUX_THREADS)
2391 /* Default stack size is too small, especially with the 64 bit ABI */
2392 /* Increase it. */
2393 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
2394 GC_printf("pthread_default_stacksize_np failed\n");
2395 }
2396 # endif /* GC_HPUX_THREADS */
2397 # ifdef PTW32_STATIC_LIB
2398 pthread_win32_process_attach_np ();
2399 pthread_win32_thread_attach_np ();
2400 # endif
2401 # if defined(GC_DARWIN_THREADS) && !defined(GC_NO_THREADS_DISCOVERY) \
2402 && !defined(DARWIN_DONT_PARSE_STACK) && !defined(THREAD_LOCAL_ALLOC)
2403 /* Test with the Darwin implicit thread registration. */
2404 GC_use_threads_discovery();
2405 GC_printf("Using Darwin task-threads-based world stop and push\n");
2406 # endif
2407 GC_set_markers_count(0);
2408 GC_COND_INIT();
2409
2410 if ((code = pthread_attr_init(&attr)) != 0) {
2411 GC_printf("pthread_attr_init failed, errno= %d\n", code);
2412 FAIL;
2413 }
2414 # if defined(GC_IRIX_THREADS) || defined(GC_FREEBSD_THREADS) \
2415 || defined(GC_DARWIN_THREADS) || defined(GC_AIX_THREADS) \
2416 || defined(GC_OPENBSD_THREADS)
2417 if ((code = pthread_attr_setstacksize(&attr, 1000 * 1024)) != 0) {
2418 GC_printf("pthread_attr_setstacksize failed, errno= %d\n", code);
2419 FAIL;
2420 }
2421 # endif
2422 n_tests = 0;
2423 enable_incremental_mode();
2424 GC_set_min_bytes_allocd(1);
2425 if (GC_get_min_bytes_allocd() != 1)
2426 FAIL;
2427 GC_set_rate(10);
2428 GC_set_max_prior_attempts(1);
2429 if (GC_get_rate() != 10 || GC_get_max_prior_attempts() != 1)
2430 FAIL;
2431 GC_set_warn_proc(warn_proc);
2432 # ifndef VERY_SMALL_CONFIG
2433 if ((code = pthread_key_create(&fl_key, 0)) != 0) {
2434 GC_printf("Key creation failed, errno= %d\n", code);
2435 FAIL;
2436 }
2437 # endif
2438 set_print_procs();
2439 # if NTHREADS > 0
2440 for (i = 0; i < NTHREADS; ++i) {
2441 if ((code = pthread_create(th+i, &attr, thr_run_one_test, 0)) != 0) {
2442 GC_printf("Thread %d creation failed, errno= %d\n", i, code);
2443 if (i > 0 && EAGAIN == code)
2444 break; /* Resource temporarily unavailable */
2445 FAIL;
2446 }
2447 }
2448 nthreads = i;
2449 for (; i <= NTHREADS; i++)
2450 run_one_test();
2451 for (i = 0; i < nthreads; ++i) {
2452 if ((code = pthread_join(th[i], 0)) != 0) {
2453 GC_printf("Thread %d join failed, errno= %d\n", i, code);
2454 FAIL;
2455 }
2456 }
2457 # else
2458 run_one_test();
2459 # endif
2460 run_single_threaded_test();
2461 check_heap_stats();
2462 (void)fflush(stdout);
2463 (void)pthread_attr_destroy(&attr);
2464
2465 /* Dummy checking of various getters and setters. */
2466 (void)GC_get_bytes_since_gc();
2467 (void)GC_get_free_bytes();
2468 (void)GC_get_pages_executable();
2469 (void)GC_get_warn_proc();
2470 (void)GC_is_disabled();
2471 GC_set_allocd_bytes_per_finalizer(GC_get_allocd_bytes_per_finalizer());
2472 GC_set_disable_automatic_collection(GC_get_disable_automatic_collection());
2473 GC_set_dont_expand(GC_get_dont_expand());
2474 GC_set_dont_precollect(GC_get_dont_precollect());
2475 GC_set_finalize_on_demand(GC_get_finalize_on_demand());
2476 GC_set_finalizer_notifier(GC_get_finalizer_notifier());
2477 GC_set_force_unmap_on_gcollect(GC_get_force_unmap_on_gcollect());
2478 GC_set_free_space_divisor(GC_get_free_space_divisor());
2479 GC_set_full_freq(GC_get_full_freq());
2480 GC_set_java_finalization(GC_get_java_finalization());
2481 GC_set_max_retries(GC_get_max_retries());
2482 GC_set_no_dls(GC_get_no_dls());
2483 GC_set_non_gc_bytes(GC_get_non_gc_bytes());
2484 GC_set_on_collection_event(GC_get_on_collection_event());
2485 GC_set_on_heap_resize(GC_get_on_heap_resize());
2486 GC_set_on_thread_event(GC_get_on_thread_event());
2487 GC_set_oom_fn(GC_get_oom_fn());
2488 GC_set_push_other_roots(GC_get_push_other_roots());
2489 GC_set_sp_corrector(GC_get_sp_corrector());
2490 GC_set_start_callback(GC_get_start_callback());
2491 GC_set_stop_func(GC_get_stop_func());
2492 GC_set_suspend_signal(GC_get_suspend_signal());
2493 GC_set_thr_restart_signal(GC_get_thr_restart_signal());
2494 GC_set_time_limit(GC_get_time_limit());
2495 # if !defined(PCR) && !defined(SMALL_CONFIG)
2496 GC_set_abort_func(GC_get_abort_func());
2497 # endif
2498 # ifndef NO_CLOCK
2499 GC_set_time_limit_tv(GC_get_time_limit_tv());
2500 # endif
2501 # ifndef GC_NO_FINALIZATION
2502 GC_set_await_finalize_proc(GC_get_await_finalize_proc());
2503 # ifndef GC_TOGGLE_REFS_NOT_NEEDED
2504 GC_set_toggleref_func(GC_get_toggleref_func());
2505 # endif
2506 # endif
2507 # if defined(CPPCHECK)
2508 UNTESTED(GC_allow_register_threads);
2509 UNTESTED(GC_register_altstack);
2510 UNTESTED(GC_stop_world_external);
2511 UNTESTED(GC_start_world_external);
2512 # ifndef GC_NO_DLOPEN
2513 UNTESTED(GC_dlopen);
2514 # endif
2515 # ifndef GC_NO_PTHREAD_CANCEL
2516 UNTESTED(GC_pthread_cancel);
2517 # endif
2518 # ifdef GC_HAVE_PTHREAD_EXIT
2519 UNTESTED(GC_pthread_exit);
2520 # endif
2521 # ifndef GC_NO_PTHREAD_SIGMASK
2522 UNTESTED(GC_pthread_sigmask);
2523 # endif
2524 # ifdef NO_TEST_HANDLE_FORK
2525 UNTESTED(GC_atfork_child);
2526 UNTESTED(GC_atfork_parent);
2527 UNTESTED(GC_atfork_prepare);
2528 UNTESTED(GC_set_handle_fork);
2529 UNTESTED(GC_start_mark_threads);
2530 # endif
2531 # endif /* CPPCHECK */
2532 # ifdef PTW32_STATIC_LIB
2533 pthread_win32_thread_detach_np ();
2534 pthread_win32_process_detach_np ();
2535 # else
2536 (void)GC_unregister_my_thread();
2537 # endif
2538 return(0);
2539 }
2540 #endif /* GC_PTHREADS */