test.c 70 KB

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