Merge pull request #4621 from alexanderkyte/strdup_env
[mono.git] / mono / utils / checked-build.c
index 82ce187e9c874058f1d4c5c732764d5c8fb7b3e1..a1d0d0ebcd3081341a7821c44b447642b8889b00 100644 (file)
@@ -1,5 +1,6 @@
-/*
- * checked-build.c: Expensive asserts used when mono is built with --with-checked-build=yes
+/**
+ * \file
+ * Expensive asserts used when mono is built with --with-checked-build=yes
  *
  * Author:
  *     Rodrigo Kumpera (kumpera@gmail.com)
@@ -58,6 +59,7 @@ mono_check_mode_enabled (MonoCheckMode query)
 #endif
                        }
                        g_strfreev (env_split);
+                       g_free (env_string);
                }
 
                check_mode = env_check_mode;
@@ -71,10 +73,12 @@ mono_check_transition_limit (void)
        static int transition_limit = -1;
        if (transition_limit < 0) {
                const gchar *env_string = g_getenv ("MONO_CHECK_THREAD_TRANSITION_HISTORY");
-               if (env_string)
+               if (env_string) {
                        transition_limit = atoi (env_string);
-               else
+                       g_free (env_string);
+               } else {
                        transition_limit = 3;
+               }
        }
        return transition_limit;
 }
@@ -245,7 +249,7 @@ mono_fatal_with_history (const char *msg, ...)
 #ifdef ENABLE_CHECKED_BUILD_GC
 
 void
-assert_gc_safe_mode (void)
+assert_gc_safe_mode (const char *file, int lineno)
 {
        if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
                return;
@@ -254,19 +258,19 @@ assert_gc_safe_mode (void)
        int state;
 
        if (!cur)
-               mono_fatal_with_history ("Expected GC Safe mode but thread is not attached");
+               mono_fatal_with_history ("%s:%d: Expected GC Safe mode but thread is not attached", file, lineno);
 
        switch (state = mono_thread_info_current_state (cur)) {
        case STATE_BLOCKING:
        case STATE_BLOCKING_AND_SUSPENDED:
                break;
        default:
-               mono_fatal_with_history ("Expected GC Safe mode but was in %s state", mono_thread_state_name (state));
+               mono_fatal_with_history ("%s:%d: Expected GC Safe mode but was in %s state", file, lineno, mono_thread_state_name (state));
        }
 }
 
 void
-assert_gc_unsafe_mode (void)
+assert_gc_unsafe_mode (const char *file, int lineno)
 {
        if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
                return;
@@ -275,7 +279,7 @@ assert_gc_unsafe_mode (void)
        int state;
 
        if (!cur)
-               mono_fatal_with_history ("Expected GC Unsafe mode but thread is not attached");
+               mono_fatal_with_history ("%s:%d: Expected GC Unsafe mode but thread is not attached", file, lineno);
 
        switch (state = mono_thread_info_current_state (cur)) {
        case STATE_RUNNING:
@@ -283,12 +287,12 @@ assert_gc_unsafe_mode (void)
        case STATE_SELF_SUSPEND_REQUESTED:
                break;
        default:
-               mono_fatal_with_history ("Expected GC Unsafe mode but was in %s state", mono_thread_state_name (state));
+               mono_fatal_with_history ("%s:%d: Expected GC Unsafe mode but was in %s state", file, lineno, mono_thread_state_name (state));
        }
 }
 
 void
-assert_gc_neutral_mode (void)
+assert_gc_neutral_mode (const char *file, int lineno)
 {
        if (!mono_check_mode_enabled (MONO_CHECK_MODE_GC))
                return;
@@ -297,7 +301,7 @@ assert_gc_neutral_mode (void)
        int state;
 
        if (!cur)
-               mono_fatal_with_history ("Expected GC Neutral mode but thread is not attached");
+               mono_fatal_with_history ("%s:%d: Expected GC Neutral mode but thread is not attached", file, lineno);
 
        switch (state = mono_thread_info_current_state (cur)) {
        case STATE_RUNNING:
@@ -307,7 +311,7 @@ assert_gc_neutral_mode (void)
        case STATE_BLOCKING_AND_SUSPENDED:
                break;
        default:
-               mono_fatal_with_history ("Expected GC Neutral mode but was in %s state", mono_thread_state_name (state));
+               mono_fatal_with_history ("%s:%d: Expected GC Neutral mode but was in %s state", file, lineno, mono_thread_state_name (state));
        }
 }
 
@@ -486,7 +490,12 @@ check_image_may_reference_image(MonoImage *from, MonoImage *to)
 
                        for (inner_idx = 0; !success && inner_idx < checking->nreferences; inner_idx++)
                        {
-                               CHECK_IMAGE_VISIT (checking->references[inner_idx]->image);
+                               // Assembly references are lazy-loaded and thus allowed to be NULL.
+                               // If they are NULL, we don't care about them for this search, because their images haven't impacted ref_count yet.
+                               if (checking->references[inner_idx])
+                               {
+                                       CHECK_IMAGE_VISIT (checking->references[inner_idx]->image);
+                               }
                        }
 
                        mono_image_unlock (checking);
@@ -541,10 +550,10 @@ check_image_set_may_reference_image_set (MonoImageSet *from, MonoImageSet *to)
                if (to->images[to_idx] == mono_defaults.corlib)
                        seen = TRUE;
 
-               // For each item in to->images, scan over from->images looking for it.
+               // For each item in to->images, scan over from->images seeking a path to it.
                for (from_idx = 0; !seen && from_idx < from->nimages; from_idx++)
                {
-                       if (to->images[to_idx] == from->images[from_idx])
+                       if (check_image_may_reference_image (from->images[from_idx], to->images[to_idx]))
                                seen = TRUE;
                }