Revert "[aot] Improve error checking when setting up class vtable."
[mono-project.git] / libgc / mark_rts.c
blobb3e996a29c2d51645a8cd9374ff53194dad84e7d
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 # include <stdio.h>
15 # include "private/gc_priv.h"
17 /* Data structure for list of root sets. */
18 /* We keep a hash table, so that we can filter out duplicate additions. */
19 /* Under Win32, we need to do a better job of filtering overlaps, so */
20 /* we resort to sequential search, and pay the price. */
21 /* This is really declared in gc_priv.h:
22 struct roots {
23 ptr_t r_start;
24 ptr_t r_end;
25 # if !defined(MSWIN32) && !defined(MSWINCE)
26 struct roots * r_next;
27 # endif
28 GC_bool r_tmp;
29 -- Delete before registering new dynamic libraries
32 struct roots GC_static_roots[MAX_ROOT_SETS];
35 int GC_no_dls = 0; /* Register dynamic library data segments. */
37 static int n_root_sets = 0;
39 /* GC_static_roots[0..n_root_sets) contains the valid root sets. */
41 # if !defined(NO_DEBUGGING)
42 /* For debugging: */
43 void GC_print_static_roots()
45 register int i;
46 size_t total = 0;
48 for (i = 0; i < n_root_sets; i++) {
49 GC_printf2("From 0x%lx to 0x%lx ",
50 (unsigned long) GC_static_roots[i].r_start,
51 (unsigned long) GC_static_roots[i].r_end);
52 if (GC_static_roots[i].r_tmp) {
53 GC_printf0(" (temporary)\n");
54 } else {
55 GC_printf0("\n");
57 total += GC_static_roots[i].r_end - GC_static_roots[i].r_start;
59 GC_printf1("Total size: %ld\n", (unsigned long) total);
60 if (GC_root_size != total) {
61 GC_printf1("GC_root_size incorrect: %ld!!\n",
62 (unsigned long) GC_root_size);
65 # endif /* NO_DEBUGGING */
67 /* Primarily for debugging support: */
68 /* Is the address p in one of the registered static */
69 /* root sections? */
70 GC_bool GC_is_static_root(p)
71 ptr_t p;
73 static int last_root_set = MAX_ROOT_SETS;
74 register int i;
77 if (last_root_set < n_root_sets
78 && p >= GC_static_roots[last_root_set].r_start
79 && p < GC_static_roots[last_root_set].r_end) return(TRUE);
80 for (i = 0; i < n_root_sets; i++) {
81 if (p >= GC_static_roots[i].r_start
82 && p < GC_static_roots[i].r_end) {
83 last_root_set = i;
84 return(TRUE);
87 return(FALSE);
90 #if !defined(MSWIN32) && !defined(MSWINCE)
91 /*
92 # define LOG_RT_SIZE 6
93 # define RT_SIZE (1 << LOG_RT_SIZE) -- Power of 2, may be != MAX_ROOT_SETS
95 struct roots * GC_root_index[RT_SIZE];
96 -- Hash table header. Used only to check whether a range is
97 -- already present.
98 -- really defined in gc_priv.h
101 static int rt_hash(addr)
102 char * addr;
104 word result = (word) addr;
105 # if CPP_WORDSZ > 8*LOG_RT_SIZE
106 result ^= result >> 8*LOG_RT_SIZE;
107 # endif
108 # if CPP_WORDSZ > 4*LOG_RT_SIZE
109 result ^= result >> 4*LOG_RT_SIZE;
110 # endif
111 result ^= result >> 2*LOG_RT_SIZE;
112 result ^= result >> LOG_RT_SIZE;
113 result &= (RT_SIZE-1);
114 return(result);
117 /* Is a range starting at b already in the table? If so return a */
118 /* pointer to it, else NIL. */
119 struct roots * GC_roots_present(b)
120 char *b;
122 register int h = rt_hash(b);
123 register struct roots *p = GC_root_index[h];
125 while (p != 0) {
126 if (p -> r_start == (ptr_t)b) return(p);
127 p = p -> r_next;
129 return(FALSE);
132 /* Add the given root structure to the index. */
133 static void add_roots_to_index(p)
134 struct roots *p;
136 register int h = rt_hash(p -> r_start);
138 p -> r_next = GC_root_index[h];
139 GC_root_index[h] = p;
142 # else /* MSWIN32 || MSWINCE */
144 # define add_roots_to_index(p)
146 # endif
151 word GC_root_size = 0;
153 void GC_add_roots(b, e)
154 char * b; char * e;
156 DCL_LOCK_STATE;
158 DISABLE_SIGNALS();
159 LOCK();
160 GC_add_roots_inner(b, e, FALSE);
161 UNLOCK();
162 ENABLE_SIGNALS();
166 /* Add [b,e) to the root set. Adding the same interval a second time */
167 /* is a moderately fast noop, and hence benign. We do not handle */
168 /* different but overlapping intervals efficiently. (We do handle */
169 /* them correctly.) */
170 /* Tmp specifies that the interval may be deleted before */
171 /* reregistering dynamic libraries. */
172 void GC_add_roots_inner(b, e, tmp)
173 char * b; char * e;
174 GC_bool tmp;
176 struct roots * old;
178 # if defined(MSWIN32) || defined(MSWINCE)
179 /* Spend the time to ensure that there are no overlapping */
180 /* or adjacent intervals. */
181 /* This could be done faster with e.g. a */
182 /* balanced tree. But the execution time here is */
183 /* virtually guaranteed to be dominated by the time it */
184 /* takes to scan the roots. */
186 register int i;
188 for (i = 0; i < n_root_sets; i++) {
189 old = GC_static_roots + i;
190 if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
191 if ((ptr_t)b < old -> r_start) {
192 old -> r_start = (ptr_t)b;
193 GC_root_size += (old -> r_start - (ptr_t)b);
195 if ((ptr_t)e > old -> r_end) {
196 old -> r_end = (ptr_t)e;
197 GC_root_size += ((ptr_t)e - old -> r_end);
199 old -> r_tmp &= tmp;
200 break;
203 if (i < n_root_sets) {
204 /* merge other overlapping intervals */
205 struct roots *other;
207 for (i++; i < n_root_sets; i++) {
208 other = GC_static_roots + i;
209 b = (char *)(other -> r_start);
210 e = (char *)(other -> r_end);
211 if ((ptr_t)b <= old -> r_end && (ptr_t)e >= old -> r_start) {
212 if ((ptr_t)b < old -> r_start) {
213 old -> r_start = (ptr_t)b;
214 GC_root_size += (old -> r_start - (ptr_t)b);
216 if ((ptr_t)e > old -> r_end) {
217 old -> r_end = (ptr_t)e;
218 GC_root_size += ((ptr_t)e - old -> r_end);
220 old -> r_tmp &= other -> r_tmp;
221 /* Delete this entry. */
222 GC_root_size -= (other -> r_end - other -> r_start);
223 other -> r_start = GC_static_roots[n_root_sets-1].r_start;
224 other -> r_end = GC_static_roots[n_root_sets-1].r_end;
225 n_root_sets--;
228 return;
231 # else
232 old = GC_roots_present(b);
233 if (old != 0) {
234 if ((ptr_t)e <= old -> r_end) /* already there */ return;
235 /* else extend */
236 GC_root_size += (ptr_t)e - old -> r_end;
237 old -> r_end = (ptr_t)e;
238 return;
240 # endif
241 if (n_root_sets == MAX_ROOT_SETS) {
242 ABORT("Too many root sets\n");
244 GC_static_roots[n_root_sets].r_start = (ptr_t)b;
245 GC_static_roots[n_root_sets].r_end = (ptr_t)e;
246 GC_static_roots[n_root_sets].r_tmp = tmp;
247 # if !defined(MSWIN32) && !defined(MSWINCE)
248 GC_static_roots[n_root_sets].r_next = 0;
249 # endif
250 add_roots_to_index(GC_static_roots + n_root_sets);
251 GC_root_size += (ptr_t)e - (ptr_t)b;
252 n_root_sets++;
255 static GC_bool roots_were_cleared = FALSE;
257 void GC_clear_roots GC_PROTO((void))
259 DCL_LOCK_STATE;
261 DISABLE_SIGNALS();
262 LOCK();
263 roots_were_cleared = TRUE;
264 n_root_sets = 0;
265 GC_root_size = 0;
266 # if !defined(MSWIN32) && !defined(MSWINCE)
268 register int i;
270 for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
272 # endif
273 UNLOCK();
274 ENABLE_SIGNALS();
277 /* Internal use only; lock held. */
278 static void GC_remove_root_at_pos(i)
279 int i;
281 GC_root_size -= (GC_static_roots[i].r_end - GC_static_roots[i].r_start);
282 GC_static_roots[i].r_start = GC_static_roots[n_root_sets-1].r_start;
283 GC_static_roots[i].r_end = GC_static_roots[n_root_sets-1].r_end;
284 GC_static_roots[i].r_tmp = GC_static_roots[n_root_sets-1].r_tmp;
285 n_root_sets--;
288 #if !defined(MSWIN32) && !defined(MSWINCE)
289 static void GC_rebuild_root_index()
291 register int i;
293 for (i = 0; i < RT_SIZE; i++) GC_root_index[i] = 0;
294 for (i = 0; i < n_root_sets; i++)
295 add_roots_to_index(GC_static_roots + i);
297 #endif
299 /* Internal use only; lock held. */
300 void GC_remove_tmp_roots()
302 register int i;
304 for (i = 0; i < n_root_sets; ) {
305 if (GC_static_roots[i].r_tmp) {
306 GC_remove_root_at_pos(i);
307 } else {
308 i++;
311 #if !defined(MSWIN32) && !defined(MSWINCE)
312 GC_rebuild_root_index();
313 #endif
316 #if !defined(MSWIN32) && !defined(MSWINCE)
317 void GC_remove_roots(b, e)
318 char * b; char * e;
320 DCL_LOCK_STATE;
322 DISABLE_SIGNALS();
323 LOCK();
324 GC_remove_roots_inner(b, e);
325 UNLOCK();
326 ENABLE_SIGNALS();
329 /* Should only be called when the lock is held */
330 void GC_remove_roots_inner(b,e)
331 char * b; char * e;
333 int i;
334 for (i = 0; i < n_root_sets; ) {
335 if (GC_static_roots[i].r_start >= (ptr_t)b && GC_static_roots[i].r_end <= (ptr_t)e) {
336 GC_remove_root_at_pos(i);
337 } else {
338 i++;
341 GC_rebuild_root_index();
343 #endif /* !defined(MSWIN32) && !defined(MSWINCE) */
345 #if defined(MSWIN32) || defined(_WIN32_WCE_EMULATION)
346 /* Workaround for the OS mapping and unmapping behind our back: */
347 /* Is the address p in one of the temporary static root sections? */
348 GC_bool GC_is_tmp_root(p)
349 ptr_t p;
351 static int last_root_set = MAX_ROOT_SETS;
352 register int i;
354 if (last_root_set < n_root_sets
355 && p >= GC_static_roots[last_root_set].r_start
356 && p < GC_static_roots[last_root_set].r_end)
357 return GC_static_roots[last_root_set].r_tmp;
358 for (i = 0; i < n_root_sets; i++) {
359 if (p >= GC_static_roots[i].r_start
360 && p < GC_static_roots[i].r_end) {
361 last_root_set = i;
362 return GC_static_roots[i].r_tmp;
365 return(FALSE);
367 #endif /* MSWIN32 || _WIN32_WCE_EMULATION */
369 ptr_t GC_approx_sp()
371 VOLATILE word dummy;
373 dummy = 42; /* Force stack to grow if necessary. Otherwise the */
374 /* later accesses might cause the kernel to think we're */
375 /* doing something wrong. */
376 # ifdef _MSC_VER
377 # pragma warning(disable:4172)
378 # endif
379 # if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 408)
380 # pragma GCC diagnostic push
381 # pragma GCC diagnostic ignored "-Wreturn-local-addr"
382 # endif
383 return((ptr_t)(&dummy));
384 # if defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 408)
385 # pragma GCC diagnostic pop
386 # endif
387 # ifdef _MSC_VER
388 # pragma warning(default:4172)
389 # endif
393 * Data structure for excluded static roots.
394 * Real declaration is in gc_priv.h.
396 struct exclusion {
397 ptr_t e_start;
398 ptr_t e_end;
401 struct exclusion GC_excl_table[MAX_EXCLUSIONS];
402 -- Array of exclusions, ascending
403 -- address order.
406 size_t GC_excl_table_entries = 0; /* Number of entries in use. */
408 /* Return the first exclusion range that includes an address >= start_addr */
409 /* Assumes the exclusion table contains at least one entry (namely the */
410 /* GC data structures). */
411 struct exclusion * GC_next_exclusion(start_addr)
412 ptr_t start_addr;
414 size_t low = 0;
415 size_t high = GC_excl_table_entries - 1;
416 size_t mid;
418 while (high > low) {
419 mid = (low + high) >> 1;
420 /* low <= mid < high */
421 if ((word) GC_excl_table[mid].e_end <= (word) start_addr) {
422 low = mid + 1;
423 } else {
424 high = mid;
427 if ((word) GC_excl_table[low].e_end <= (word) start_addr) return 0;
428 return GC_excl_table + low;
431 void GC_exclude_static_roots(start, finish)
432 GC_PTR start;
433 GC_PTR finish;
435 struct exclusion * next;
436 size_t next_index, i;
438 if (0 == GC_excl_table_entries) {
439 next = 0;
440 } else {
441 next = GC_next_exclusion(start);
443 if (0 != next) {
444 if ((word)(next -> e_start) < (word) finish) {
445 /* incomplete error check. */
446 ABORT("exclusion ranges overlap");
448 if ((word)(next -> e_start) == (word) finish) {
449 /* extend old range backwards */
450 next -> e_start = (ptr_t)start;
451 return;
453 next_index = next - GC_excl_table;
454 for (i = GC_excl_table_entries; i > next_index; --i) {
455 GC_excl_table[i] = GC_excl_table[i-1];
457 } else {
458 next_index = GC_excl_table_entries;
460 if (GC_excl_table_entries == MAX_EXCLUSIONS) ABORT("Too many exclusions");
461 GC_excl_table[next_index].e_start = (ptr_t)start;
462 GC_excl_table[next_index].e_end = (ptr_t)finish;
463 ++GC_excl_table_entries;
466 /* Invoke push_conditional on ranges that are not excluded. */
467 void GC_push_conditional_with_exclusions(bottom, top, all)
468 ptr_t bottom;
469 ptr_t top;
470 int all;
472 struct exclusion * next;
473 ptr_t excl_start;
475 while (bottom < top) {
476 next = GC_next_exclusion(bottom);
477 if (0 == next || (excl_start = next -> e_start) >= top) {
478 GC_push_conditional(bottom, top, all);
479 return;
481 if (excl_start > bottom) GC_push_conditional(bottom, excl_start, all);
482 bottom = next -> e_end;
487 * In the absence of threads, push the stack contents.
488 * In the presence of threads, push enough of the current stack
489 * to ensure that callee-save registers saved in collector frames have been
490 * seen.
492 void GC_push_current_stack(cold_gc_frame)
493 ptr_t cold_gc_frame;
495 # if defined(THREADS)
496 if (0 == cold_gc_frame) return;
497 # ifdef STACK_GROWS_DOWN
498 GC_push_all_eager(GC_approx_sp(), cold_gc_frame);
499 /* For IA64, the register stack backing store is handled */
500 /* in the thread-specific code. */
501 # else
502 GC_push_all_eager( cold_gc_frame, GC_approx_sp() );
503 # endif
504 # else
505 # ifdef STACK_GROWS_DOWN
506 GC_push_all_stack_partially_eager( GC_approx_sp(), GC_stackbottom,
507 cold_gc_frame );
508 # ifdef IA64
509 /* We also need to push the register stack backing store. */
510 /* This should really be done in the same way as the */
511 /* regular stack. For now we fudge it a bit. */
512 /* Note that the backing store grows up, so we can't use */
513 /* GC_push_all_stack_partially_eager. */
515 extern word GC_save_regs_ret_val;
516 /* Previously set to backing store pointer. */
517 ptr_t bsp = (ptr_t) GC_save_regs_ret_val;
518 ptr_t cold_gc_bs_pointer;
519 if (GC_all_interior_pointers) {
520 cold_gc_bs_pointer = bsp - 2048;
521 if (cold_gc_bs_pointer < BACKING_STORE_BASE) {
522 cold_gc_bs_pointer = BACKING_STORE_BASE;
523 } else {
524 GC_push_all_stack(BACKING_STORE_BASE, cold_gc_bs_pointer);
526 } else {
527 cold_gc_bs_pointer = BACKING_STORE_BASE;
529 GC_push_all_eager(cold_gc_bs_pointer, bsp);
530 /* All values should be sufficiently aligned that we */
531 /* dont have to worry about the boundary. */
533 # endif
534 # else
535 GC_push_all_stack_partially_eager( GC_stackbottom, GC_approx_sp(),
536 cold_gc_frame );
537 # endif
538 # endif /* !THREADS */
542 * Push GC internal roots. Only called if there is some reason to believe
543 * these would not otherwise get registered.
545 void GC_push_gc_structures GC_PROTO((void))
547 GC_push_finalizer_structures();
548 GC_push_stubborn_structures();
549 # if defined(THREADS)
550 GC_push_thread_structures();
551 # endif
554 #ifdef THREAD_LOCAL_ALLOC
555 void GC_mark_thread_local_free_lists();
556 #endif
558 void GC_cond_register_dynamic_libraries()
560 # if (defined(DYNAMIC_LOADING) || defined(MSWIN32) || defined(MSWINCE) \
561 || defined(PCR)) && !defined(SRC_M3)
562 GC_remove_tmp_roots();
563 if (!GC_no_dls) GC_register_dynamic_libraries();
564 # else
565 GC_no_dls = TRUE;
566 # endif
570 * Call the mark routines (GC_tl_push for a single pointer, GC_push_conditional
571 * on groups of pointers) on every top level accessible pointer.
572 * If all is FALSE, arrange to push only possibly altered values.
573 * Cold_gc_frame is an address inside a GC frame that
574 * remains valid until all marking is complete.
575 * A zero value indicates that it's OK to miss some
576 * register values.
578 void GC_push_roots(all, cold_gc_frame)
579 GC_bool all;
580 ptr_t cold_gc_frame;
582 int i;
583 int kind;
586 * Next push static data. This must happen early on, since it's
587 * not robust against mark stack overflow.
589 /* Reregister dynamic libraries, in case one got added. */
590 /* There is some argument for doing this as late as possible, */
591 /* especially on win32, where it can change asynchronously. */
592 /* In those cases, we do it here. But on other platforms, it's */
593 /* not safe with the world stopped, so we do it earlier. */
594 # if !defined(REGISTER_LIBRARIES_EARLY)
595 GC_cond_register_dynamic_libraries();
596 # endif
598 /* Mark everything in static data areas */
599 for (i = 0; i < n_root_sets; i++) {
600 GC_push_conditional_with_exclusions(
601 GC_static_roots[i].r_start,
602 GC_static_roots[i].r_end, all);
605 /* Mark all free list header blocks, if those were allocated from */
606 /* the garbage collected heap. This makes sure they don't */
607 /* disappear if we are not marking from static data. It also */
608 /* saves us the trouble of scanning them, and possibly that of */
609 /* marking the freelists. */
610 for (kind = 0; kind < GC_n_kinds; kind++) {
611 GC_PTR base = GC_base(GC_obj_kinds[kind].ok_freelist);
612 if (0 != base) {
613 GC_set_mark_bit(base);
617 /* Mark from GC internal roots if those might otherwise have */
618 /* been excluded. */
619 if (GC_no_dls || roots_were_cleared) {
620 GC_push_gc_structures();
623 /* Mark thread local free lists, even if their mark */
624 /* descriptor excludes the link field. */
625 /* If the world is not stopped, this is unsafe. It is */
626 /* also unnecessary, since we will do this again with the */
627 /* world stopped. */
628 # ifdef THREAD_LOCAL_ALLOC
629 if (GC_world_stopped) GC_mark_thread_local_free_lists();
630 # endif
633 * Now traverse stacks, and mark from register contents.
634 * These must be done last, since they can legitimately overflow
635 * the mark stack.
637 # ifdef USE_GENERIC_PUSH_REGS
638 GC_generic_push_regs(cold_gc_frame);
639 /* Also pushes stack, so that we catch callee-save registers */
640 /* saved inside the GC_push_regs frame. */
641 # else
643 * push registers - i.e., call GC_push_one(r) for each
644 * register contents r.
646 GC_push_regs(); /* usually defined in machine_dep.c */
647 GC_push_current_stack(cold_gc_frame);
648 /* In the threads case, this only pushes collector frames. */
649 /* In the case of linux threads on IA64, the hot section of */
650 /* the main stack is marked here, but the register stack */
651 /* backing store is handled in the threads-specific code. */
652 # endif
653 if (GC_push_other_roots != 0) (*GC_push_other_roots)();
654 /* In the threads case, this also pushes thread stacks. */
655 /* Note that without interior pointer recognition lots */
656 /* of stuff may have been pushed already, and this */
657 /* should be careful about mark stack overflows. */