3 * Expensive asserts used when mono is built with --with-checked-build=yes
6 * Rodrigo Kumpera (kumpera@gmail.com)
12 #ifdef ENABLE_CHECKED_BUILD
14 #include <mono/utils/checked-build.h>
15 #include <mono/utils/mono-threads.h>
16 #include <mono/utils/mono-threads-coop.h>
17 #include <mono/utils/mono-tls.h>
18 #include <mono/metadata/mempool.h>
19 #include <mono/metadata/metadata-internals.h>
20 #include <mono/metadata/image-internals.h>
21 #include <mono/metadata/class-internals.h>
22 #include <mono/metadata/reflection-internals.h>
25 #ifdef HAVE_BACKTRACE_SYMBOLS
29 // Selective-enable support
31 // Returns true for check modes which are allowed by both the current DISABLE_ macros and the MONO_CHECK_MODE env var.
32 // Argument may be a bitmask; if so, result is true if at least one specified mode is enabled.
34 mono_check_mode_enabled (MonoCheckMode query)
36 static MonoCheckMode check_mode = MONO_CHECK_MODE_UNKNOWN;
37 if (G_UNLIKELY (check_mode == MONO_CHECK_MODE_UNKNOWN))
39 MonoCheckMode env_check_mode = MONO_CHECK_MODE_NONE;
40 const gchar *env_string = g_getenv ("MONO_CHECK_MODE");
44 gchar **env_split = g_strsplit (env_string, ",", 0);
45 for (gchar **env_component = env_split; *env_component; env_component++)
47 mono_bool check_all = g_str_equal (*env_component, "all");
48 #ifdef ENABLE_CHECKED_BUILD_GC
49 if (check_all || g_str_equal (*env_component, "gc"))
50 env_check_mode |= MONO_CHECK_MODE_GC;
52 #ifdef ENABLE_CHECKED_BUILD_METADATA
53 if (check_all || g_str_equal (*env_component, "metadata"))
54 env_check_mode |= MONO_CHECK_MODE_METADATA;
56 #ifdef ENABLE_CHECKED_BUILD_THREAD
57 if (check_all || g_str_equal (*env_component, "thread"))
58 env_check_mode |= MONO_CHECK_MODE_THREAD;
61 g_strfreev (env_split);
65 check_mode = env_check_mode;
67 return check_mode & query;
71 mono_check_transition_limit (void)
73 static int transition_limit = -1;
74 if (transition_limit < 0) {
75 const gchar *env_string = g_getenv ("MONO_CHECK_THREAD_TRANSITION_HISTORY");
77 transition_limit = atoi (env_string);
83 return transition_limit;
87 GPtrArray *transitions;
88 guint32 in_gc_critical_region;
91 static MonoNativeTlsKey thread_status;
94 checked_build_init (void)
96 // Init state for get_state, which can be called either by gc or thread mode
97 if (mono_check_mode_enabled (MONO_CHECK_MODE_GC | MONO_CHECK_MODE_THREAD))
98 mono_native_tls_alloc (&thread_status, NULL);
104 CheckState *state = mono_native_tls_get_value (thread_status);
106 state = g_new0 (CheckState, 1);
107 state->transitions = g_ptr_array_new ();
108 mono_native_tls_set_value (thread_status, state);
114 #ifdef ENABLE_CHECKED_BUILD_THREAD
116 #define MAX_NATIVE_BT 6
117 #define MAX_NATIVE_BT_PROBE (MAX_NATIVE_BT + 5)
118 #define MAX_TRANSITIONS (mono_check_transition_limit ())
120 #ifdef HAVE_BACKTRACE_SYMBOLS
122 //XXX We should collect just the IPs and lazily symbolificate them.
124 collect_backtrace (gpointer out_data[])
126 return backtrace (out_data, MAX_NATIVE_BT_PROBE);
130 translate_backtrace (gpointer native_trace[], int size)
132 char **names = backtrace_symbols (native_trace, size);
133 GString* bt = g_string_sized_new (100);
137 //Figure out the cut point of useless backtraces
138 //We'll skip up to the caller of checked_build_thread_transition
139 for (i = 0; i < size; ++i) {
140 if (strstr (names [i], "checked_build_thread_transition")) {
148 for (i = j; i < size; ++i) {
149 if (i - j <= MAX_NATIVE_BT)
150 g_string_append_printf (bt, "\tat %s\n", names [i]);
154 return g_string_free (bt, FALSE);
160 collect_backtrace (gpointer out_data[])
166 translate_backtrace (gpointer native_trace[], int size)
168 return g_strdup ("\tno backtrace available\n");
175 int from_state, next_state, suspend_count, suspend_count_delta, size;
176 gpointer backtrace [MAX_NATIVE_BT_PROBE];
180 free_transition (ThreadTransition *t)
186 checked_build_thread_transition (const char *transition, void *info, int from_state, int suspend_count, int next_state, int suspend_count_delta)
188 if (!mono_check_mode_enabled (MONO_CHECK_MODE_THREAD))
191 CheckState *state = get_state ();
192 /* We currently don't record external changes as those are hard to reason about. */
193 if (!mono_thread_info_is_current (info))
196 if (state->transitions->len >= MAX_TRANSITIONS)
197 free_transition (g_ptr_array_remove_index (state->transitions, 0));
199 ThreadTransition *t = g_new0 (ThreadTransition, 1);
200 t->name = transition;
201 t->from_state = from_state;
202 t->next_state = next_state;
203 t->suspend_count = suspend_count;
204 t->suspend_count_delta = suspend_count_delta;
205 t->size = collect_backtrace (t->backtrace);
206 g_ptr_array_add (state->transitions, t);
210 mono_fatal_with_history (const char *msg, ...)
213 GString* err = g_string_sized_new (100);
215 g_string_append_printf (err, "Assertion failure in thread %p due to: ", mono_native_thread_id_get ());
218 va_start (args, msg);
219 g_string_append_vprintf (err, msg, args);
222 if (mono_check_mode_enabled (MONO_CHECK_MODE_THREAD))
224 CheckState *state = get_state ();
226 g_string_append_printf (err, "\nLast %d state transitions: (most recent first)\n", state->transitions->len);
228 for (i = state->transitions->len - 1; i >= 0; --i) {
229 ThreadTransition *t = state->transitions->pdata [i];
230 char *bt = translate_backtrace (t->backtrace, t->size);
231 g_string_append_printf (err, "[%s] %s -> %s (%d) %s%d at:\n%s",
233 mono_thread_state_name (t->from_state),
234 mono_thread_state_name (t->next_state),
236 t->suspend_count_delta > 0 ? "+" : "", //I'd like to see this sort of values: -1, 0, +1
237 t->suspend_count_delta,
244 g_string_free (err, TRUE);
247 #endif /* defined(ENABLE_CHECKED_BUILD_THREAD) */
249 #ifdef ENABLE_CHECKED_BUILD_GC
252 assert_gc_safe_mode (const char *file, int lineno)
254 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
257 MonoThreadInfo *cur = mono_thread_info_current ();
261 mono_fatal_with_history ("%s:%d: Expected GC Safe mode but thread is not attached", file, lineno);
263 switch (state = mono_thread_info_current_state (cur)) {
265 case STATE_BLOCKING_AND_SUSPENDED:
268 mono_fatal_with_history ("%s:%d: Expected GC Safe mode but was in %s state", file, lineno, mono_thread_state_name (state));
273 assert_gc_unsafe_mode (const char *file, int lineno)
275 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
278 MonoThreadInfo *cur = mono_thread_info_current ();
282 mono_fatal_with_history ("%s:%d: Expected GC Unsafe mode but thread is not attached", file, lineno);
284 switch (state = mono_thread_info_current_state (cur)) {
286 case STATE_ASYNC_SUSPEND_REQUESTED:
287 case STATE_SELF_SUSPEND_REQUESTED:
290 mono_fatal_with_history ("%s:%d: Expected GC Unsafe mode but was in %s state", file, lineno, mono_thread_state_name (state));
295 assert_gc_neutral_mode (const char *file, int lineno)
297 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
300 MonoThreadInfo *cur = mono_thread_info_current ();
304 mono_fatal_with_history ("%s:%d: Expected GC Neutral mode but thread is not attached", file, lineno);
306 switch (state = mono_thread_info_current_state (cur)) {
308 case STATE_ASYNC_SUSPEND_REQUESTED:
309 case STATE_SELF_SUSPEND_REQUESTED:
311 case STATE_BLOCKING_AND_SUSPENDED:
314 mono_fatal_with_history ("%s:%d: Expected GC Neutral mode but was in %s state", file, lineno, mono_thread_state_name (state));
319 critical_gc_region_begin(void)
321 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
324 CheckState *state = get_state ();
325 state->in_gc_critical_region++;
331 critical_gc_region_end(void* token)
333 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
336 CheckState *state = get_state();
337 g_assert (state == token);
338 state->in_gc_critical_region--;
342 assert_not_in_gc_critical_region(void)
344 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
347 CheckState *state = get_state();
348 if (state->in_gc_critical_region > 0) {
349 mono_fatal_with_history("Expected GC Unsafe mode, but was in %s state", mono_thread_state_name (mono_thread_info_current_state (mono_thread_info_current ())));
354 assert_in_gc_critical_region (void)
356 if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
359 CheckState *state = get_state();
360 if (state->in_gc_critical_region == 0)
361 mono_fatal_with_history("Expected GC critical region");
364 #endif /* defined(ENABLE_CHECKED_BUILD_GC) */
366 #ifdef ENABLE_CHECKED_BUILD_METADATA
368 // check_metadata_store et al: The goal of these functions is to verify that if there is a pointer from one mempool into
369 // another, that the pointed-to memory is protected by the reference count mechanism for MonoImages.
371 // Note: The code below catches only some kinds of failures. Failures outside its scope notably incode:
372 // * Code below absolutely assumes that no mempool is ever held as "mempool" member by more than one Image or ImageSet at once
373 // * Code below assumes reference counts never underflow (ie: if we have a pointer to something, it won't be deallocated while we're looking at it)
374 // Locking strategy is a little slapdash overall.
376 // Reference audit support
377 #define check_mempool_assert_message(...) \
378 g_assertion_message("Mempool reference violation: " __VA_ARGS__)
383 MonoImageSet *image_set;
386 static MonoMemPoolOwner mono_mempool_no_owner = {NULL,NULL};
389 check_mempool_owner_eq (MonoMemPoolOwner a, MonoMemPoolOwner b)
391 return a.image == b.image && a.image_set == b.image_set;
394 // Say image X "references" image Y if X either contains Y in its modules field, or X’s "references" field contains an
395 // assembly whose image is Y.
396 // Say image X transitively references image Y if there is any chain of images-referencing-images which leads from X to Y.
397 // Once the mempools for two pointers have been looked up, there are four possibilities:
399 // Case 1. Image FROM points to Image TO: Legal if FROM transitively references TO
401 // We'll do a simple BFS graph search on images. For each image we visit:
403 check_image_search (GHashTable *visited, GPtrArray *next, MonoImage *candidate, MonoImage *goal, gboolean *success)
405 // Image hasn't even been loaded-- ignore it
409 // Image has already been visited-- ignore it
410 if (g_hash_table_lookup_extended (visited, candidate, NULL, NULL))
413 // Image is the target-- mark success
414 if (candidate == goal)
420 // Unvisited image, queue it to have its children visited
421 g_hash_table_insert (visited, candidate, NULL);
422 g_ptr_array_add (next, candidate);
427 check_image_may_reference_image(MonoImage *from, MonoImage *to)
429 if (to == from) // Shortcut
432 // Corlib is never unloaded, and all images implicitly reference it.
433 // Some images avoid explicitly referencing it as an optimization, so special-case it here.
434 if (to == mono_defaults.corlib)
437 // Non-dynamic images may NEVER reference dynamic images
438 if (to->dynamic && !from->dynamic)
441 // FIXME: We currently give a dynamic images a pass on the reference rules.
442 // Dynamic images may ALWAYS reference non-dynamic images.
443 // We allow this because the dynamic image code is known "messy", and in theory it is already
444 // protected because dynamic images can only reference classes their assembly has retained.
445 // However, long term, we should make this rigorous.
446 if (from->dynamic && !to->dynamic)
449 gboolean success = FALSE;
451 // Images to inspect on this pass, images to inspect on the next pass
452 GPtrArray *current = g_ptr_array_sized_new (1), *next = g_ptr_array_new ();
454 // Because in practice the image graph contains cycles, we must track which images we've visited
455 GHashTable *visited = g_hash_table_new (NULL, NULL);
457 #define CHECK_IMAGE_VISIT(i) check_image_search (visited, next, (i), to, &success)
459 CHECK_IMAGE_VISIT (from); // Initially "next" contains only from node
461 // For each pass exhaust the "to check" queue while filling up the "check next" queue
462 while (!success && next->len > 0) // Halt on success or when out of nodes to process
464 // Swap "current" and "next" and clear next
465 GPtrArray *temp = current;
468 g_ptr_array_set_size (next, 0);
471 for(current_idx = 0; current_idx < current->len; current_idx++)
473 MonoImage *checking = g_ptr_array_index (current, current_idx); // CAST?
475 mono_image_lock (checking);
477 // For each queued image visit all directly referenced images
480 // 'files' and 'modules' semantically contain the same items but because of lazy loading we must check both
481 for (inner_idx = 0; !success && inner_idx < checking->file_count; inner_idx++)
483 CHECK_IMAGE_VISIT (checking->files[inner_idx]);
486 for (inner_idx = 0; !success && inner_idx < checking->module_count; inner_idx++)
488 CHECK_IMAGE_VISIT (checking->modules[inner_idx]);
491 for (inner_idx = 0; !success && inner_idx < checking->nreferences; inner_idx++)
493 // Assembly references are lazy-loaded and thus allowed to be NULL.
494 // If they are NULL, we don't care about them for this search, because their images haven't impacted ref_count yet.
495 if (checking->references[inner_idx])
497 CHECK_IMAGE_VISIT (checking->references[inner_idx]->image);
501 mono_image_unlock (checking);
505 g_ptr_array_free (current, TRUE); g_ptr_array_free (next, TRUE); g_hash_table_destroy (visited);
510 // Case 2. ImageSet FROM points to Image TO: One of FROM's "images" either is, or transitively references, TO.
512 check_image_set_may_reference_image (MonoImageSet *from, MonoImage *to)
514 // See above-- All images implicitly reference corlib
515 if (to == mono_defaults.corlib)
519 gboolean success = FALSE;
520 mono_image_set_lock (from);
521 for (idx = 0; !success && idx < from->nimages; idx++)
523 if (check_image_may_reference_image (from->images[idx], to))
526 mono_image_set_unlock (from);
528 return success; // No satisfying image found in from->images
531 // Case 3. ImageSet FROM points to ImageSet TO: The images in TO are a strict subset of FROM (no transitive relationship is important here)
533 check_image_set_may_reference_image_set (MonoImageSet *from, MonoImageSet *to)
538 gboolean valid = TRUE; // Until proven otherwise
540 mono_image_set_lock (from); mono_image_set_lock (to);
542 int to_idx, from_idx;
543 for (to_idx = 0; valid && to_idx < to->nimages; to_idx++)
545 gboolean seen = FALSE;
547 // If TO set includes corlib, the FROM set may
548 // implicitly reference corlib, even if it's not
549 // present in the set explicitly.
550 if (to->images[to_idx] == mono_defaults.corlib)
553 // For each item in to->images, scan over from->images seeking a path to it.
554 for (from_idx = 0; !seen && from_idx < from->nimages; from_idx++)
556 if (check_image_may_reference_image (from->images[from_idx], to->images[to_idx]))
560 // If the to->images item is not found in from->images, the subset check has failed
565 mono_image_set_unlock (from); mono_image_set_unlock (to);
567 return valid; // All items in "to" were found in "from"
570 // Case 4. Image FROM points to ImageSet TO: FROM transitively references *ALL* of the “images” listed in TO
572 check_image_may_reference_image_set (MonoImage *from, MonoImageSet *to)
574 if (to->nimages == 0) // Malformed image_set
577 gboolean valid = TRUE;
579 mono_image_set_lock (to);
581 for (idx = 0; valid && idx < to->nimages; idx++)
583 if (!check_image_may_reference_image (from, to->images[idx]))
586 mono_image_set_unlock (to);
588 return valid; // All images in to->images checked out
591 // Small helper-- get a descriptive string for a MonoMemPoolOwner
592 // Callers are obligated to free buffer with g_free after use
594 check_mempool_owner_name (MonoMemPoolOwner owner)
596 GString *result = g_string_new (NULL);
599 if (owner.image->dynamic)
600 g_string_append (result, "(Dynamic)");
601 g_string_append (result, owner.image->name);
603 else if (owner.image_set)
605 char *temp = mono_image_set_description (owner.image_set);
606 g_string_append (result, "(Image set)");
607 g_string_append (result, temp);
612 g_string_append (result, "(Non-image memory)");
614 return g_string_free (result, FALSE);
617 // Helper -- surf various image-locating functions looking for the owner of this pointer
618 static MonoMemPoolOwner
619 mono_find_mempool_owner (void *ptr)
621 MonoMemPoolOwner owner = mono_mempool_no_owner;
623 owner.image = mono_find_image_owner (ptr);
624 if (!check_mempool_owner_eq (owner, mono_mempool_no_owner))
627 owner.image_set = mono_find_image_set_owner (ptr);
628 if (!check_mempool_owner_eq (owner, mono_mempool_no_owner))
631 owner.image = mono_find_dynamic_image_owner (ptr);
636 // Actually perform reference audit
638 check_mempool_may_reference_mempool (void *from_ptr, void *to_ptr, gboolean require_local)
640 if (!mono_check_mode_enabled (MONO_CHECK_MODE_METADATA))
643 // Null pointers are OK
647 MonoMemPoolOwner from = mono_find_mempool_owner (from_ptr), to = mono_find_mempool_owner (to_ptr);
651 if (!check_mempool_owner_eq (from,to))
652 check_mempool_assert_message ("Pointer in image %s should have been internal, but instead pointed to image %s", check_mempool_owner_name (from), check_mempool_owner_name (to));
655 // Writing into unknown mempool
656 else if (check_mempool_owner_eq (from, mono_mempool_no_owner))
658 check_mempool_assert_message ("Non-image memory attempting to write pointer to image %s", check_mempool_owner_name (to));
661 // Reading from unknown mempool
662 else if (check_mempool_owner_eq (to, mono_mempool_no_owner))
664 check_mempool_assert_message ("Attempting to write pointer from image %s to non-image memory", check_mempool_owner_name (from));
667 // Split out the four cases described above:
668 else if (from.image && to.image)
670 if (!check_image_may_reference_image (from.image, to.image))
671 check_mempool_assert_message ("Image %s tried to point to image %s, but does not retain a reference", check_mempool_owner_name (from), check_mempool_owner_name (to));
674 else if (from.image && to.image_set)
676 if (!check_image_may_reference_image_set (from.image, to.image_set))
677 check_mempool_assert_message ("Image %s tried to point to image set %s, but does not retain a reference", check_mempool_owner_name (from), check_mempool_owner_name (to));
680 else if (from.image_set && to.image_set)
682 if (!check_image_set_may_reference_image_set (from.image_set, to.image_set))
683 check_mempool_assert_message ("Image set %s tried to point to image set %s, but does not retain a reference", check_mempool_owner_name (from), check_mempool_owner_name (to));
686 else if (from.image_set && to.image)
688 if (!check_image_set_may_reference_image (from.image_set, to.image))
689 check_mempool_assert_message ("Image set %s tried to point to image %s, but does not retain a reference", check_mempool_owner_name (from), check_mempool_owner_name (to));
694 check_mempool_assert_message ("Internal logic error: Unreachable code");
699 check_metadata_store (void *from, void *to)
701 check_mempool_may_reference_mempool (from, to, FALSE);
705 check_metadata_store_local (void *from, void *to)
707 check_mempool_may_reference_mempool (from, to, TRUE);
710 #endif /* defined(ENABLE_CHECKED_BUILD_METADATA) */
712 #endif /* ENABLE_CHECKED_BUILD */