Debian Codegraph @

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 GC_PTR_STORE_AND_DIRTY(xptr, x);
328 x = 0;
329 if (argc != 2
330 || (n = atoi(argv[1])) <= 0) {
331 GC_printf("usage: test_cpp number-of-iterations\n"
332 "Assuming 10 iters\n");
333 n = 10;
334 }
335 # ifdef LINT2
336 if (n > 100 * 1000) n = 100 * 1000;
337 # endif
338
339 for (iters = 1; iters <= n; iters++) {
340 GC_printf( "Starting iteration %d\n", iters );
341
342 /* Allocate some uncollectible As and disguise their pointers.
343 Later we'll check to see if the objects are still there. We're
344 checking to make sure these objects really are uncollectible. */
345 GC_word as[ 1000 ];
346 GC_word bs[ 1000 ];
347 for (i = 0; i < 1000; i++) {
348 as[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) A(i) );
349 bs[ i ] = Disguise( new (GC_NS_QUALIFY(NoGC)) B(i) ); }
350
351 /* Allocate a fair number of finalizable Cs, Ds, and Fs.
352 Later we'll check to make sure they've gone away. */
353 for (i = 0; i < 1000; i++) {
354 C* c = new C( 2 );
355 C c1( 2 ); /* stack allocation should work too */
356 D* d;
357 F* f;
358 d = ::new (USE_GC, D::CleanUp, (void*)(GC_word)i) D( i );
359 GC_reachable_here(d);
360 f = new F;
361 F** fa = new F*[1];
362 fa[0] = f;
363 (void)fa;
364 delete[] fa;
365 if (0 == i % 10)
366 GC_CHECKED_DELETE(c);
367 }
368
369 /* Allocate a very large number of collectible As and Bs and
370 drop the references to them immediately, forcing many
371 collections. */
372 for (i = 0; i < 1000000; i++) {
373 A* a;
374 a = new (USE_GC) A( i );
375 GC_reachable_here(a);
376 B* b;
377 b = new B( i );
378 (void)b;
379 b = new (USE_GC) B( i );
380 if (0 == i % 10) {
381 B::Deleting( 1 );
382 GC_CHECKED_DELETE(b);
383 B::Deleting( 0 );}
384 # if defined(FINALIZE_ON_DEMAND) && !defined(GC_NO_FINALIZATION)
385 GC_invoke_finalizers();
386 # endif
387 }
388
389 /* Make sure the uncollectible As and Bs are still there. */
390 for (i = 0; i < 1000; i++) {
391 A* a = static_cast<A*>(Undisguise(as[i]));
392 B* b = static_cast<B*>(Undisguise(bs[i]));
393 a->Test( i );
394 # if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
395 // Workaround for ASan/MSan: the linker uses operator delete
396 // implementation from libclang_rt instead of gc_cpp (thus
397 // causing incompatible alloc/free).
398 GC_FREE(a);
399 # else
400 GC_CHECKED_DELETE(a);
401 # endif
402 b->Test( i );
403 B::Deleting( 1 );
404 GC_CHECKED_DELETE(b);
405 B::Deleting( 0 );
406 # if defined(FINALIZE_ON_DEMAND) && !defined(GC_NO_FINALIZATION)
407 GC_invoke_finalizers();
408 # endif
409 }
410
411 /* Make sure most of the finalizable Cs, Ds, and Fs have
412 gone away. */
413 C::Test();
414 D::Test();
415 F::Test();}
416
417 x = *xptr;
418 my_assert (29 == x[0]);
419 GC_printf( "The test appears to have succeeded.\n" );
420 return( 0 );
421 }