Improve a safety check when writing data into StatBuffer
[mono.git] / mono / sgen / sgen-minor-copy-object.h
1 /*
2  * sgen-minor-copy-object.h: Copy functions for nursery collections.
3  *
4  * Copyright 2001-2003 Ximian, Inc
5  * Copyright 2003-2010 Novell, Inc.
6  * Copyright (C) 2012 Xamarin Inc
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License 2.0 as published by the Free Software Foundation;
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License 2.0 along with this library; if not, write to the Free
19  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define collector_pin_object(obj, queue) sgen_pin_object (obj, queue);
23 #define COLLECTOR_SERIAL_ALLOC_FOR_PROMOTION alloc_for_promotion
24
25 extern guint64 stat_nursery_copy_object_failed_to_space; /* from sgen-gc.c */
26
27 #include "sgen-copy-object.h"
28
29 /*
30  * This is how the copying happens from the nursery to the old generation.
31  * We assume that at this time all the pinned objects have been identified and
32  * marked as such.
33  * We run scan_object() for each pinned object so that each referenced
34  * objects if possible are copied. The new gray objects created can have
35  * scan_object() run on them right away, too.
36  * Then we run copy_object() for the precisely tracked roots. At this point
37  * all the roots are either gray or black. We run scan_object() on the gray
38  * objects until no more gray objects are created.
39  * At the end of the process we walk again the pinned list and we unmark
40  * the pinned flag. As we go we also create the list of free space for use
41  * in the next allocation runs.
42  *
43  * We need to remember objects from the old generation that point to the new one
44  * (or just addresses?).
45  *
46  * copy_object could be made into a macro once debugged (use inline for now).
47  */
48
49 static MONO_ALWAYS_INLINE void
50 SERIAL_COPY_OBJECT (GCObject **obj_slot, SgenGrayQueue *queue) 
51 {
52         GCObject *forwarded;
53         GCObject *copy;
54         GCObject *obj = *obj_slot;
55
56         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy from a %d generation collection", current_collection_generation);
57
58         HEAVY_STAT (++stat_copy_object_called_nursery);
59
60         if (!sgen_ptr_in_nursery (obj)) {
61                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
62                 return;
63         }
64
65         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
66
67         /*
68          * Before we can copy the object we must make sure that we are
69          * allowed to, i.e. that the object not pinned, not already
70          * forwarded or belongs to the nursery To Space.
71          */
72
73         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
74                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
75                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
76                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
77                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
78                 return;
79         }
80         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
81                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
82                 SGEN_LOG (9, " (pinned, no change)");
83                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
84                 return;
85         }
86
87 #ifndef SGEN_SIMPLE_NURSERY
88         if (sgen_nursery_is_to_space (obj)) {
89                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
90                 SGEN_LOG (9, " (tospace, no change)");
91                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
92                 return;
93         }
94 #endif
95
96         HEAVY_STAT (++stat_objects_copied_nursery);
97
98         copy = copy_object_no_checks (obj, queue);
99         SGEN_UPDATE_REFERENCE (obj_slot, copy);
100 }
101
102 /*
103  * SERIAL_COPY_OBJECT_FROM_OBJ:
104  *
105  *   Similar to SERIAL_COPY_OBJECT, but assumes that OBJ_SLOT is part of an object, so it handles global remsets as well.
106  */
107 static MONO_ALWAYS_INLINE void
108 SERIAL_COPY_OBJECT_FROM_OBJ (GCObject **obj_slot, SgenGrayQueue *queue)
109 {
110         GCObject *forwarded;
111         GCObject *obj = *obj_slot;
112         GCObject *copy;
113
114         SGEN_ASSERT (9, current_collection_generation == GENERATION_NURSERY, "calling minor-serial-copy-from-obj from a %d generation collection", current_collection_generation);
115
116         HEAVY_STAT (++stat_copy_object_called_nursery);
117
118         if (!sgen_ptr_in_nursery (obj)) {
119                 HEAVY_STAT (++stat_nursery_copy_object_failed_from_space);
120                 return;
121         }
122
123         SGEN_LOG (9, "Precise copy of %p from %p", obj, obj_slot);
124
125         /*
126          * Before we can copy the object we must make sure that we are
127          * allowed to, i.e. that the object not pinned, not already
128          * forwarded or belongs to the nursery To Space.
129          */
130
131         if ((forwarded = SGEN_OBJECT_IS_FORWARDED (obj))) {
132                 SGEN_ASSERT (9, sgen_obj_get_descriptor (forwarded),  "forwarded object %p has no gc descriptor", forwarded);
133                 SGEN_LOG (9, " (already forwarded to %p)", forwarded);
134                 HEAVY_STAT (++stat_nursery_copy_object_failed_forwarded);
135                 SGEN_UPDATE_REFERENCE (obj_slot, forwarded);
136 #ifndef SGEN_SIMPLE_NURSERY
137                 if (G_UNLIKELY (sgen_ptr_in_nursery (forwarded) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (forwarded)))
138                         sgen_add_to_global_remset (obj_slot, forwarded);
139 #endif
140                 return;
141         }
142         if (G_UNLIKELY (SGEN_OBJECT_IS_PINNED (obj))) {
143                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "pinned object %p has no gc descriptor", obj);
144                 SGEN_LOG (9, " (pinned, no change)");
145                 HEAVY_STAT (++stat_nursery_copy_object_failed_pinned);
146                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
147                         sgen_add_to_global_remset (obj_slot, obj);
148                 return;
149         }
150
151 #ifndef SGEN_SIMPLE_NURSERY
152         if (sgen_nursery_is_to_space (obj)) {
153                 /* FIXME: all of these could just use `sgen_obj_get_descriptor_safe()` */
154                 SGEN_ASSERT (9, sgen_vtable_get_descriptor (SGEN_LOAD_VTABLE(obj)), "to space object %p has no gc descriptor", obj);
155                 SGEN_LOG (9, " (tospace, no change)");
156                 HEAVY_STAT (++stat_nursery_copy_object_failed_to_space);                
157
158                 /*
159                  * FIXME:
160                  *
161                  * The card table scanning code sometimes clears cards
162                  * that have just been set for a global remset.  In
163                  * the split nursery the following situation can
164                  * occur:
165                  *
166                  * Let's say object A starts in card C but continues
167                  * into C+1.  Within A, at offset O there's a
168                  * reference to a new nursery object X.  A+O is in
169                  * card C+1.  Now card C is scanned, and as part of
170                  * it, object A.  The reference at A+O is processed by
171                  * copying X into nursery to-space at Y.  Since it's
172                  * still in the nursery, a global remset must be added
173                  * for A+O, so card C+1 is marked.  Now, however, card
174                  * C+1 is scanned, which means that it's cleared
175                  * first.  This wouldn't be terribly bad if reference
176                  * A+O were re-scanned and the global remset re-added,
177                  * but since the reference points to to-space, that
178                  * doesn't happen, and C+1 remains cleared: the remset
179                  * is lost.
180                  *
181                  * There's at least two ways to fix this.  The easy
182                  * one is to re-add the remset on the re-scan.  This
183                  * is that - the following two lines of code.
184                  *
185                  * The proper solution appears to be to first make a
186                  * copy of the cards before scanning a block, then to
187                  * clear all the cards and scan from the copy, so no
188                  * remsets will be overwritten.  Scanning objects at
189                  * most once would be the icing on the cake.
190                  */
191                 if (!sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (obj))
192                         sgen_add_to_global_remset (obj_slot, obj);
193
194                 return;
195         }
196 #endif
197
198         HEAVY_STAT (++stat_objects_copied_nursery);
199
200         copy = copy_object_no_checks (obj, queue);
201         SGEN_UPDATE_REFERENCE (obj_slot, copy);
202 #ifndef SGEN_SIMPLE_NURSERY
203         if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
204                 sgen_add_to_global_remset (obj_slot, copy);
205 #else
206         /* copy_object_no_checks () can return obj on OOM */
207         if (G_UNLIKELY (obj == copy)) {
208                 if (G_UNLIKELY (sgen_ptr_in_nursery (copy) && !sgen_ptr_in_nursery (obj_slot) && !SGEN_OBJECT_IS_CEMENTED (copy)))
209                         sgen_add_to_global_remset (obj_slot, copy);
210         }
211 #endif
212 }
213
214 #define FILL_MINOR_COLLECTOR_COPY_OBJECT(collector)     do {                    \
215                 (collector)->serial_ops.copy_or_mark_object = SERIAL_COPY_OBJECT;                       \
216         } while (0)