PR c++/85076
[official-gcc.git] / gcc / ggc-tests.c
blobf62f474c0f67fb8e79a162ac2ca85cc0d9ef3b80
1 /* Unit tests for GCC's garbage collector (and gengtype etc).
2 Copyright (C) 2015-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree-core.h"
24 #include "tree.h"
25 #include "ggc-internal.h" /* (for ggc_force_collect). */
26 #include "selftest.h"
28 #if CHECKING_P
30 /* A helper function for writing ggc tests. */
32 void
33 selftest::forcibly_ggc_collect ()
35 ggc_force_collect = true;
36 ggc_collect ();
37 ggc_force_collect = false;
40 /* The various GTY markers must be outside of a namespace to be seen by
41 gengtype, so we don't put this file within the selftest namespace. */
45 /* Verify that a simple struct works, and that it can
46 own references to non-roots, and have them be marked. */
48 struct GTY(()) test_struct
50 struct test_struct *other;
53 static GTY(()) test_struct *root_test_struct;
55 static void
56 test_basic_struct ()
58 root_test_struct = ggc_cleared_alloc <test_struct> ();
59 root_test_struct->other = ggc_cleared_alloc <test_struct> ();
61 selftest::forcibly_ggc_collect ();
63 ASSERT_TRUE (ggc_marked_p (root_test_struct));
64 ASSERT_TRUE (ggc_marked_p (root_test_struct->other));
69 /* Selftest for GTY((length)). */
71 /* A test struct using GTY((length)). */
73 struct GTY(()) test_of_length
75 int num_elem;
76 struct test_of_length * GTY ((length ("%h.num_elem"))) elem[1];
79 static GTY(()) test_of_length *root_test_of_length;
81 static void
82 test_length ()
84 const int count = 5;
85 size_t sz = sizeof (test_of_length) + (count- 1) * sizeof (test_of_length *);
86 root_test_of_length = (test_of_length *)ggc_internal_cleared_alloc (sz);
87 root_test_of_length->num_elem = count;
88 for (int i = 0; i < count; i++)
89 root_test_of_length->elem[i] = ggc_cleared_alloc <test_of_length> ();
91 selftest::forcibly_ggc_collect ();
93 ASSERT_TRUE (ggc_marked_p (root_test_of_length));
94 for (int i = 0; i < count; i++)
95 ASSERT_TRUE (ggc_marked_p (root_test_of_length->elem[i]));
100 /* Selftest for unions, GTY((tag)), and GTY((desc)). */
102 /* A struct with a reference that's an a different offset to test_struct,
103 to ensure that we're using the correct types. */
105 struct GTY(()) test_other
107 char dummy[256];
108 test_struct *m_ptr;
111 enum which_field
113 WHICH_FIELD_USE_TEST_STRUCT,
114 WHICH_FIELD_USE_TEST_OTHER
117 /* An example function for use by a GTY((desc)) marker. */
119 static enum which_field
120 calc_desc (int kind)
122 switch (kind)
124 case 0: return WHICH_FIELD_USE_TEST_STRUCT;
125 case 1: return WHICH_FIELD_USE_TEST_OTHER;
126 default:
127 gcc_unreachable ();
131 /* A struct containing an example of a union, showing the "tag" and
132 "desc" markers. */
134 struct GTY(()) test_of_union
136 int m_kind;
137 union u {
138 test_struct * GTY ((tag ("WHICH_FIELD_USE_TEST_STRUCT") )) u_test_struct;
139 test_other * GTY ((tag ("WHICH_FIELD_USE_TEST_OTHER") )) u_test_other;
140 } GTY ((desc ("calc_desc (%0.m_kind)"))) m_u;
143 /* Example roots. */
145 static GTY(()) test_of_union *root_test_of_union_1;
146 static GTY(()) test_of_union *root_test_of_union_2;
148 /* Verify that the above work correctly. */
150 static void
151 test_union ()
153 root_test_of_union_1 = ggc_cleared_alloc <test_of_union> ();
154 root_test_of_union_1->m_kind = 0;
155 test_struct *ts = ggc_cleared_alloc <test_struct> ();
156 root_test_of_union_1->m_u.u_test_struct = ts;
158 root_test_of_union_2 = ggc_cleared_alloc <test_of_union> ();
159 root_test_of_union_2->m_kind = 1;
160 test_other *other = ggc_cleared_alloc <test_other> ();
161 root_test_of_union_2->m_u.u_test_other = other;
162 test_struct *referenced_by_other = ggc_cleared_alloc <test_struct> ();
163 other->m_ptr = referenced_by_other;
165 selftest::forcibly_ggc_collect ();
167 ASSERT_TRUE (ggc_marked_p (root_test_of_union_1));
168 ASSERT_TRUE (ggc_marked_p (ts));
170 ASSERT_TRUE (ggc_marked_p (root_test_of_union_2));
171 ASSERT_TRUE (ggc_marked_p (other));
172 ASSERT_TRUE (ggc_marked_p (referenced_by_other));
177 /* Verify that destructors get run when instances are collected. */
179 struct GTY(()) test_struct_with_dtor
181 /* This struct has a destructor; it *ought* to be called
182 by the ggc machinery when instances are collected. */
183 ~test_struct_with_dtor () { dtor_call_count++; }
185 static int dtor_call_count;
188 int test_struct_with_dtor::dtor_call_count;
190 static void
191 test_finalization ()
193 #if GCC_VERSION >= 4003
194 ASSERT_FALSE (need_finalization_p <test_struct> ());
195 ASSERT_TRUE (need_finalization_p <test_struct_with_dtor> ());
196 #endif
198 /* Create some garbage. */
199 const int count = 10;
200 for (int i = 0; i < count; i++)
201 ggc_cleared_alloc <test_struct_with_dtor> ();
203 test_struct_with_dtor::dtor_call_count = 0;
205 selftest::forcibly_ggc_collect ();
207 /* Verify that the destructor was run for each instance. */
208 ASSERT_EQ (count, test_struct_with_dtor::dtor_call_count);
213 /* Verify that a global can be marked as "deletable". */
215 static GTY((deletable)) test_struct *test_of_deletable;
217 static void
218 test_deletable_global ()
220 test_of_deletable = ggc_cleared_alloc <test_struct> ();
221 ASSERT_TRUE (test_of_deletable != NULL);
223 selftest::forcibly_ggc_collect ();
225 ASSERT_EQ (NULL, test_of_deletable);
230 /* Verify that gengtype etc can cope with inheritance. */
232 class GTY((desc("%h.m_kind"), tag("0"))) example_base
234 public:
235 example_base ()
236 : m_kind (0),
237 m_a (ggc_cleared_alloc <test_struct> ())
240 void *
241 operator new (size_t sz)
243 return ggc_internal_cleared_alloc (sz);
246 protected:
247 example_base (int kind)
248 : m_kind (kind),
249 m_a (ggc_cleared_alloc <test_struct> ())
252 public:
253 int m_kind;
254 test_struct *m_a;
257 class GTY((tag("1"))) some_subclass : public example_base
259 public:
260 some_subclass ()
261 : example_base (1),
262 m_b (ggc_cleared_alloc <test_struct> ())
265 test_struct *m_b;
268 class GTY((tag("2"))) some_other_subclass : public example_base
270 public:
271 some_other_subclass ()
272 : example_base (2),
273 m_c (ggc_cleared_alloc <test_struct> ())
276 test_struct *m_c;
279 /* Various test roots, both expressed as a ptr to the actual class, and
280 as a ptr to the base class. */
281 static GTY(()) example_base *test_example_base;
282 static GTY(()) some_subclass *test_some_subclass;
283 static GTY(()) some_other_subclass *test_some_other_subclass;
284 static GTY(()) example_base *test_some_subclass_as_base_ptr;
285 static GTY(()) example_base *test_some_other_subclass_as_base_ptr;
287 static void
288 test_inheritance ()
290 test_example_base = new example_base ();
291 test_some_subclass = new some_subclass ();
292 test_some_other_subclass = new some_other_subclass ();
293 test_some_subclass_as_base_ptr = new some_subclass ();
294 test_some_other_subclass_as_base_ptr = new some_other_subclass ();
296 selftest::forcibly_ggc_collect ();
298 /* Verify that the roots and everything referenced by them got marked
299 (both for fields in the base class and those in subclasses). */
300 ASSERT_TRUE (ggc_marked_p (test_example_base));
301 ASSERT_TRUE (ggc_marked_p (test_example_base->m_a));
303 ASSERT_TRUE (ggc_marked_p (test_some_subclass));
304 ASSERT_TRUE (ggc_marked_p (test_some_subclass->m_a));
305 ASSERT_TRUE (ggc_marked_p (test_some_subclass->m_b));
307 ASSERT_TRUE (ggc_marked_p (test_some_other_subclass));
308 ASSERT_TRUE (ggc_marked_p (test_some_other_subclass->m_a));
309 ASSERT_TRUE (ggc_marked_p (test_some_other_subclass->m_c));
311 ASSERT_TRUE (ggc_marked_p (test_some_subclass_as_base_ptr));
312 ASSERT_TRUE (ggc_marked_p (test_some_subclass_as_base_ptr->m_a));
313 ASSERT_TRUE (ggc_marked_p (((some_subclass *)
314 test_some_subclass_as_base_ptr)->m_b));
316 ASSERT_TRUE (ggc_marked_p (test_some_other_subclass_as_base_ptr));
317 ASSERT_TRUE (ggc_marked_p (test_some_other_subclass_as_base_ptr->m_a));
318 ASSERT_TRUE (ggc_marked_p (((some_other_subclass *)
319 test_some_other_subclass_as_base_ptr)->m_c));
324 /* Test of chain_next/chain_prev
326 Construct a very long linked list, so that without
327 the chain_next/chain_prev optimization we'd have
328 a stack overflow when gt_ggc_mx_test_node recurses. */
330 struct GTY(( chain_next ("%h.m_next"),
331 chain_prev ("%h.m_prev") )) test_node
333 test_node *m_prev;
334 test_node *m_next;
335 int m_idx;
338 static GTY(()) test_node *root_test_node;
340 static void
341 test_chain_next ()
343 /* Ideally we would construct a long list so that the number of
344 stack frames would be deep enough to crash if gengtype has created
345 something that recurses.
347 However, as the list is lengthened to increase the chance of
348 overflowing the stack, the test will require more time and memory
349 to run. On a Fedora 20 x86_64 box with 128GB of RAM, count=2000000
350 without the chain_next optimization reliably overflowed the stack,
351 but the test took 0.5s to run.
353 For now this test runs with a low value for "count", which defeats
354 the main purpose of the test - though it at least gives us coverage
355 for walking a GTY((chain_next)) list.
357 We could potentially increase this value once we have a better sense
358 of the time and space requirements of the test on different hosts,
359 or perhaps find a way to reduce the stack size when running this
360 testcase. */
361 const int count = 10;
363 /* Build the linked list. */
364 root_test_node = ggc_cleared_alloc <test_node> ();
365 test_node *tail_node = root_test_node;
366 for (int i = 0; i < count; i++)
368 test_node *new_node = ggc_cleared_alloc <test_node> ();
369 tail_node->m_next = new_node;
370 new_node->m_prev = tail_node;
371 new_node->m_idx = i;
372 tail_node = new_node;
375 selftest::forcibly_ggc_collect ();
377 /* If we got here, we survived. */
379 /* Verify that all nodes in the list were marked. */
380 ASSERT_TRUE (ggc_marked_p (root_test_node));
381 test_node *iter_node = root_test_node->m_next;
382 for (int i = 0; i < count; i++)
384 ASSERT_TRUE (ggc_marked_p (iter_node));
385 ASSERT_EQ (i, iter_node->m_idx);
386 iter_node = iter_node->m_next;
392 /* Test for GTY((user)). */
394 struct GTY((user)) user_struct
396 char dummy[16];
397 test_struct *m_ptr;
400 static GTY(()) user_struct *root_user_struct_ptr;
402 /* A global for verifying that the user-provided gt_ggc_mx gets
403 called. */
404 static int num_calls_to_user_gt_ggc_mx;
406 /* User-provided implementation of gt_ggc_mx. */
408 static void
409 gt_ggc_mx (user_struct *p)
411 num_calls_to_user_gt_ggc_mx++;
412 gt_ggc_mx_test_struct (p->m_ptr);
415 /* User-provided implementation of gt_pch_nx. */
417 static void
418 gt_pch_nx (user_struct *p)
420 gt_pch_nx_test_struct (p->m_ptr);
423 /* User-provided implementation of gt_pch_nx. */
425 static void
426 gt_pch_nx (user_struct *p, gt_pointer_operator op, void *cookie)
428 op (&(p->m_ptr), cookie);
431 /* Verify that GTY((user)) works. */
433 static void
434 test_user_struct ()
436 root_user_struct_ptr = ggc_cleared_alloc <user_struct> ();
437 test_struct *referenced = ggc_cleared_alloc <test_struct> ();
438 root_user_struct_ptr->m_ptr = referenced;
440 num_calls_to_user_gt_ggc_mx = 0;
442 selftest::forcibly_ggc_collect ();
444 ASSERT_TRUE (ggc_marked_p (root_user_struct_ptr));
445 ASSERT_TRUE (ggc_marked_p (referenced));
446 ASSERT_TRUE (num_calls_to_user_gt_ggc_mx > 0);
451 /* Smoketest to ensure that the tree type is marked. */
453 static GTY(()) tree dummy_unittesting_tree;
455 static void
456 test_tree_marking ()
458 dummy_unittesting_tree = build_int_cst (integer_type_node, 1066);
460 selftest::forcibly_ggc_collect ();
462 ASSERT_TRUE (ggc_marked_p (dummy_unittesting_tree));
467 /* Ideas for other tests:
468 - pch-handling */
470 namespace selftest {
472 /* Run all of the selftests within this file. */
474 void
475 ggc_tests_c_tests ()
477 test_basic_struct ();
478 test_length ();
479 test_union ();
480 test_finalization ();
481 test_deletable_global ();
482 test_inheritance ();
483 test_chain_next ();
484 test_user_struct ();
485 test_tree_marking ();
488 } // namespace selftest
490 #include "gt-ggc-tests.h"
492 #else /* #if CHECKING_P */
494 /* The #if CHECKING_P code above has various GTY-marked roots.
495 gengtype has no knowledge of the preprocessor, and so detects
496 these roots and writes them out to gt-ggc-tests.h.
497 In a !CHECKING_P build we can ignore gt-ggc-tests.h, but the
498 root tables are referenced in the various generated gtype-*.c
499 files like this:
501 ...snip...
502 extern const struct ggc_root_tab gt_ggc_r_gt_ggc_tests_h[];
503 ...snip...
505 EXPORTED_CONST struct ggc_root_tab * const gt_ggc_rtab[] = {
506 ...snip...
507 gt_ggc_r_gt_ggc_tests_h,
508 ...snip...
511 Hence to avoid a link failure, we provide dummy implementations
512 of these root tables in an unchecked build.
514 Note that these conditional roots imply that PCH files are
515 incompatible between checked and unchecked builds. */
517 EXPORTED_CONST struct ggc_root_tab gt_ggc_r_gt_ggc_tests_h[] = {
518 LAST_GGC_ROOT_TAB
521 EXPORTED_CONST struct ggc_root_tab gt_ggc_rd_gt_ggc_tests_h[] = {
522 LAST_GGC_ROOT_TAB
525 #endif /* #else clause of #if CHECKING_P */