Debian Codegraph @

gc/tests/test_cpp.cc

raw
1 /*
2 * Copyright (c) 1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /****************************************************************************
15 usage: test_cpp number-of-iterations
16
17 This program tries to test the specific C++ functionality provided by
18 gc_cpp.h that isn't tested by the more general test routines of the
19 collector.
20
21 A recommended value for number-of-iterations is 10, which will take a
22 few minutes to complete.
23
24 ***************************************************************************/
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #undef GC_BUILD
31
32 #define GC_DONT_INCL_WINDOWS_H
33 #include "gc_cpp.h"
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "gc_allocator.h"
40
41 # include "private/gcconfig.h"
42
43 # ifndef GC_API_PRIV
44 # define GC_API_PRIV GC_API
45 # endif
46 extern "C" {
47 GC_API_PRIV void GC_printf(const char * format, ...);
48 /* Use GC private output to reach the same log file. */
49 /* Don't include gc_priv.h, since that may include Windows system */
50 /* header files that don't take kindly to this context. */
51 }
52
53 #ifdef MSWIN32
54 # ifndef WIN32_LEAN_AND_MEAN
55 # define WIN32_LEAN_AND_MEAN 1
56 # endif
57 # define NOSERVICE
58 # include <windows.h>
59 #endif
60
61 #ifdef GC_NAME_CONFLICT
62 # define USE_GC GC_NS_QUALIFY(UseGC)
63 struct foo * GC;
64 #else
65 # define USE_GC GC_NS_QUALIFY(GC)
66 #endif
67
68 #define my_assert( e ) \
69 if (! (e)) { \
70 GC_printf( "Assertion failure in " __FILE__ ", line %d: " #e "\n", \
71 __LINE__ ); \
72 exit( 1 ); }
73
74 #if defined(__powerpc64__) && !defined(__clang__) && GC_GNUC_PREREQ(10, 0)
75 /* Suppress "layout of aggregates ... has changed" GCC note. */
76 # define A_I_TYPE short
77 #else
78 # define A_I_TYPE int
79 #endif
80
81 class A {public:
82 /* An uncollectible class. */
83
84 GC_ATTR_EXPLICIT A( int iArg ): i((A_I_TYPE)iArg) {}
85 void Test( int iArg ) {
86 my_assert( i == iArg );}
87 virtual ~A() {}
88 A_I_TYPE i; };
89
90
91 class B: public GC_NS_QUALIFY(gc), public A { public:
92 /* A collectible class. */
93
94 GC_ATTR_EXPLICIT B( int j ): A( j ) {}
95 virtual ~B() {
96 my_assert( deleting );}
97 static void Deleting( int on ) {
98 deleting = on;}
99 static int deleting;};
100
101 int B::deleting = 0;
102
103 #define C_INIT_LEFT_RIGHT(arg_l, arg_r) \
104 { \
105 C *l = new C(arg_l); \
106 C *r = new C(arg_r); \
107 left = l; \
108 right = r; \
109 if (GC_is_heap_ptr(this)) { \
110 GC_END_STUBBORN_CHANGE(this); \
111 GC_reachable_here(l); \
112 GC_reachable_here(r); \
113 } \
114 }
115
116 class C: public GC_NS_QUALIFY(gc_cleanup), public A { public:
117 /* A collectible class with cleanup and virtual multiple inheritance. */
118
119 // The class uses dynamic memory/resource allocation, so provide both
120 // a copy constructor and an assignment operator to workaround a cppcheck
121 // warning.
122 C(const C& c) : A(c.i), level(c.level), left(0), right(0) {
123 if (level > 0)
124 C_INIT_LEFT_RIGHT(*c.left, *c.right);
125 }
126
127 C& operator=(const C& c) {
128 if (this != &c) {
129 delete left;
130 delete right;
131 i = c.i;
132 level = c.level;
133 left = 0;
134 right = 0;
135 if (level > 0)
136 C_INIT_LEFT_RIGHT(*c.left, *c.right);
137 }
138 return *this;
139 }
140
141 GC_ATTR_EXPLICIT C( int levelArg ): A( levelArg ), level( levelArg ) {
142 nAllocated++;
143 if (level > 0) {
144 C_INIT_LEFT_RIGHT(level - 1, level - 1);
145 } else {
146 left = right = 0;}}
147 ~C() {
148 this->A::Test( level );
149 nFreed++;
150 my_assert( level == 0 ?
151 left == 0 && right == 0 :
152 level == left->level + 1 && level == right->level + 1 );
153 left = right = 0;
154 level = -123456;}
155 static void Test() {
156 if (GC_is_incremental_mode() && nFreed < (nAllocated / 5) * 4) {
157 // An explicit GC might be needed to reach the expected number
158 // of the finalized objects.
159 GC_gcollect();
160 }
161 my_assert(nFreed <= nAllocated);
162 # ifndef GC_NO_FINALIZATION
163 my_assert(nFreed >= (nAllocated / 5) * 4 || GC_get_find_leak());
164 # endif
165 }
166
167 static int nFreed;
168 static int nAllocated;
169 int level;
170 C* left;
171 C* right;};
172
173 int C::nFreed = 0;
174 int C::nAllocated = 0;
175
176
177 class D: public GC_NS_QUALIFY(gc) { public:
178 /* A collectible class with a static member function to be used as
179 an explicit clean-up function supplied to ::new. */
180
181 GC_ATTR_EXPLICIT D( int iArg ): i( iArg ) {
182 nAllocated++;}
183 static void CleanUp( void* obj, void* data ) {
184 D* self = static_cast<D*>(obj);
185 nFreed++;
186 my_assert( (GC_word)self->i == (GC_word)data );}
187 static void Test() {
188 # ifndef GC_NO_FINALIZATION
189 my_assert(nFreed >= (nAllocated / 5) * 4 || GC_get_find_leak());
190 # endif
191 }
192
193 int i;
194 static int nFreed;
195 static int nAllocated;};
196
197 int D::nFreed = 0;
198 int D::nAllocated = 0;
199
200
201 class E: public GC_NS_QUALIFY(gc_cleanup) { public:
202 /* A collectible class with clean-up for use by F. */
203
204 E() {
205 nAllocated++;}
206 ~E() {
207 nFreed++;}
208
209 static int nFreed;
210 static int nAllocated;};
211
212 int E::nFreed = 0;
213 int E::nAllocated = 0;
214
215
216 class F: public E {public:
217 /* A collectible class with clean-up, a base with clean-up, and a
218 member with clean-up. */
219
220 F() {
221 nAllocatedF++;
222 }
223
224 ~F() {
225 nFreedF++;
226 }
227
228 static void Test() {
229 # ifndef GC_NO_FINALIZATION
230 my_assert(nFreedF >= (nAllocatedF / 5) * 4 || GC_get_find_leak());
231 # endif
232 my_assert(2 * nFreedF == nFreed);
233 }
234
235 E e;
236 static int nFreedF;
237 static int nAllocatedF;
238 };
239
240 int F::nFreedF = 0;
241 int F::nAllocatedF = 0;
242
243
244 GC_word Disguise( void* p ) {
245 return ~ (GC_word) p;}
246
247 void* Undisguise( GC_word i ) {
248 return (void*) ~ i;}
249
250 #define GC_CHECKED_DELETE(p) \
251 { \
252 size_t freed_before = GC_get_expl_freed_bytes_since_gc(); \
253 delete p; /* the operator should invoke GC_FREE() */ \
254 size_t freed_after = GC_get_expl_freed_bytes_since_gc(); \
255 my_assert(freed_before != freed_after); \
256 }
257
258 #if ((defined(MSWIN32) && !defined(__MINGW32__)) || defined(MSWINCE)) \
259 && !defined(NO_WINMAIN_ENTRY)
260 int APIENTRY WinMain( HINSTANCE /* instance */, HINSTANCE /* prev */,
261 LPSTR cmd, int /* cmdShow */)
262 {
263 int argc = 0;
264 char* argv[ 3 ];
265
266 # if defined(CPPCHECK)
267 GC_noop1((GC_word)&WinMain);
268 # endif
269 if (cmd != 0)
270 for (argc = 1; argc < (int)(sizeof(argv) / sizeof(argv[0])); argc++) {
271 // Parse the command-line string. Non-reentrant strtok() is not used
272 // to avoid complains of static analysis tools. (And, strtok_r() is
273 // not available on some platforms.) The code is equivalent to:
274 // if (!(argv[argc] = strtok(argc == 1 ? cmd : 0, " \t"))) break;
275 if (NULL == cmd) {
276 argv[argc] = NULL;
277 break;
278 }
279 for (; *cmd != '\0'; cmd++) {
280 if (*cmd != ' ' && *cmd != '\t')
281 break;
282 }
283 if ('\0' == *cmd) {
284 argv[argc] = NULL;
285 break;
286 }
287 argv[argc] = cmd;
288 while (*(++cmd) != '\0') {
289 if (*cmd == ' ' || *cmd == '\t')
290 break;
291 }
292 if (*cmd != '\0') {
293 *(cmd++) = '\0';
294 } else {
295 cmd = NULL;
296 }
297 }
298 #elif defined(MACOS)
299 int main() {
300 char* argv_[] = {"test_cpp", "10"}; // MacOS doesn't have a command line
301 argv = argv_;
302 argc = sizeof(argv_)/sizeof(argv_[0]);
303 #else
304 int main( int argc, char* argv[] ) {
305 #endif
306
307 GC_set_all_interior_pointers(1);
308 /* needed due to C++ multiple inheritance used */
309
310 # ifdef TEST_MANUAL_VDB
311 GC_set_manual_vdb_allowed(1);
312 # endif
313 GC_INIT();
314 # ifndef NO_INCREMENTAL
315 GC_enable_incremental();
316 # endif
317 if (GC_get_find_leak())
318 GC_printf("This test program is not designed for leak detection mode\n");
319
320 int i, iters, n;
321 int *x = gc_allocator<int>().allocate(1);
322 int *xio;
323 xio = gc_allocator_ignore_off_page<int>().allocate(1);
324 GC_reachable_here(xio);
325 int **xptr = traceable_allocator<int *>().allocate(1);
326 *x = 29;
327 if (!xptr) {
328 fprintf(stderr, "Out of memory!\n");
329 exit(3);
330 }
331 GC_PTR_STORE_AND_DIRTY(xptr, x);
332 x = 0;
333 if (argc != 2
334 || (n = atoi(argv[1])) <= 0) {
335 GC_printf("usage: test_cpp number-of-iterations\n"
336 "Assuming 10 iters\n");
337 n = 10;
338 }
339 # ifdef LINT2
340 if (n > 100 * 1000) n = 100 * 1000;
341 # endif
342
343 for (iters = 1; iters <= n; iters++) {
344 GC_printf( "Starting iteration %d\n", iters );
345
346 /* Allocate some uncollectible As and disguise their pointers.
347 Later we'll check to see if the objects are still there. We're
348 checking to make sure these objects really are uncollectible. */
349 GC_word as[ 1000 ];
350 GC_word bs[ 1000 ];
351 for (i = 0; i < 1000; i++) {
352 as[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) A(i) );
353 bs[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) B(i) ); }
354
355 /* Allocate a fair number of finalizable Cs, Ds, and Fs.
356 Later we'll check to make sure they've gone away. */
357 for (i = 0; i < 1000; i++) {
358 C* c = new C( 2 );
359 C c1( 2 ); /* stack allocation should work too */
360 D* d;
361 F* f;
362 d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
363 GC_reachable_here(d);
364 f = new F;
365 F** fa = new F*[1];
366 fa[0] = f;
367 (void)fa;
368 delete[] fa;
369 if (0 == i % 10)
370 GC_CHECKED_DELETE(c);
371 }
372
373 /* Allocate a very large number of collectible As and Bs and
374 drop the references to them immediately, forcing many
375 collections. */
376 for (i = 0; i < 1000000; i++) {
377 A* a;
378 a = new (USE_GC) A( i );
379 GC_reachable_here(a);
380 B* b;
381 b = new B( i );
382 (void)b;
383 b = new (USE_GC) B( i );
384 if (0 == i % 10) {
385 B::Deleting( 1 );
386 GC_CHECKED_DELETE(b);
387 B::Deleting( 0 );}
388 # if defined(FINALIZE_ON_DEMAND) && !defined(GC_NO_FINALIZATION)
389 GC_invoke_finalizers();
390 # endif
391 }
392
393 /* Make sure the uncollectible As and Bs are still there. */
394 for (i = 0; i < 1000; i++) {
395 A* a = static_cast<A*>(Undisguise(as[i]));
396 B* b = static_cast<B*>(Undisguise(bs[i]));
397 a->Test( i );
398 # if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
399 // Workaround for ASan/MSan: the linker uses operator delete
400 // implementation from libclang_rt instead of gc_cpp (thus
401 // causing incompatible alloc/free).
402 GC_FREE(a);
403 # else
404 GC_CHECKED_DELETE(a);
405 # endif
406 b->Test( i );
407 B::Deleting( 1 );
408 GC_CHECKED_DELETE(b);
409 B::Deleting( 0 );
410 # if defined(FINALIZE_ON_DEMAND) && !defined(GC_NO_FINALIZATION)
411 GC_invoke_finalizers();
412 # endif
413 }
414
415 /* Make sure most of the finalizable Cs, Ds, and Fs have
416 gone away. */
417 C::Test();
418 D::Test();
419 F::Test();}
420
421 x = *xptr;
422 my_assert (29 == x[0]);
423 GC_printf( "The test appears to have succeeded.\n" );
424 return( 0 );
425 }