Merge pull request #2274 from esdrubal/udpclientreceive
[mono.git] / mono / sgen / sgen-gchandles.c
index b18a65afa82ddb7c436bcc3b69553e71cce526dd..f4b352eac1185d6befd2f9fb5ac8d440d0376a3c 100644 (file)
@@ -25,8 +25,8 @@
 #include "mono/utils/mono-membar.h"
 
 #ifdef HEAVY_STATISTICS
-static volatile guint64 stat_gc_handles_allocated = 0;
-static volatile guint64 stat_gc_handles_max_allocated = 0;
+static volatile guint32 stat_gc_handles_allocated = 0;
+static volatile guint32 stat_gc_handles_max_allocated = 0;
 #endif
 
 #define BUCKETS (32 - MONO_GC_HANDLE_TYPE_SHIFT)
@@ -105,17 +105,17 @@ static void
 protocol_gchandle_update (int handle_type, gpointer link, gpointer old_value, gpointer new_value)
 {
        gboolean old = MONO_GC_HANDLE_IS_OBJECT_POINTER (old_value);
-       gboolean new = MONO_GC_HANDLE_IS_OBJECT_POINTER (new_value);
+       gboolean new_ = MONO_GC_HANDLE_IS_OBJECT_POINTER (new_value);
        gboolean track = handle_type == HANDLE_WEAK_TRACK;
 
        if (!MONO_GC_HANDLE_TYPE_IS_WEAK (handle_type))
                return;
 
-       if (!old && new)
+       if (!old && new_)
                binary_protocol_dislink_add (link, MONO_GC_REVEAL_POINTER (new_value, TRUE), track);
-       else if (old && !new)
+       else if (old && !new_)
                binary_protocol_dislink_remove (link, track);
-       else if (old && new && old_value != new_value)
+       else if (old && new_ && old_value != new_value)
                binary_protocol_dislink_update (link, MONO_GC_REVEAL_POINTER (new_value, TRUE), track);
 }
 
@@ -123,15 +123,15 @@ protocol_gchandle_update (int handle_type, gpointer link, gpointer old_value, gp
 static inline gpointer
 try_set_slot (volatile gpointer *slot, GCObject *obj, gpointer old, GCHandleType type)
 {
-       gpointer new;
+       gpointer new_;
        if (obj)
-               new = MONO_GC_HANDLE_OBJECT_POINTER (obj, GC_HANDLE_TYPE_IS_WEAK (type));
+               new_ = MONO_GC_HANDLE_OBJECT_POINTER (obj, GC_HANDLE_TYPE_IS_WEAK (type));
        else
-               new = MONO_GC_HANDLE_METADATA_POINTER (sgen_client_default_metadata (), GC_HANDLE_TYPE_IS_WEAK (type));
-       SGEN_ASSERT (0, new, "Why is the occupied bit not set?");
-       if (InterlockedCompareExchangePointer (slot, new, old) == old) {
-               protocol_gchandle_update (type, (gpointer)slot, old, new);
-               return new;
+               new_ = MONO_GC_HANDLE_METADATA_POINTER (sgen_client_default_metadata (), GC_HANDLE_TYPE_IS_WEAK (type));
+       SGEN_ASSERT (0, new_, "Why is the occupied bit not set?");
+       if (InterlockedCompareExchangePointer (slot, new_, old) == old) {
+               protocol_gchandle_update (type, (gpointer)slot, old, new_);
+               return new_;
        }
        return NULL;
 }
@@ -143,32 +143,20 @@ try_occupy_slot (HandleData *handles, guint bucket, guint offset, GCObject *obj,
        volatile gpointer *link_addr = &(handles->entries [bucket] [offset]);
        if (MONO_GC_HANDLE_OCCUPIED (*link_addr))
                return FALSE;
-       return try_set_slot (link_addr, obj, NULL, handles->type) != NULL;
+       return try_set_slot (link_addr, obj, NULL, (GCHandleType)handles->type) != NULL;
 }
 
-#define EMPTY_HANDLE_DATA(t) \
-       (HandleData) { \
-               .entries = { NULL }, \
-               .capacity = 0, \
-               .slot_hint = 0, \
-               .max_index = 0, \
-               .type = (t) \
-       }
-
-/* weak and weak-track arrays will be allocated in malloc memory 
- */
 static HandleData gc_handles [] = {
-       EMPTY_HANDLE_DATA (HANDLE_WEAK),
-       EMPTY_HANDLE_DATA (HANDLE_WEAK_TRACK),
-       EMPTY_HANDLE_DATA (HANDLE_NORMAL),
-       EMPTY_HANDLE_DATA (HANDLE_PINNED)
+       { { NULL }, 0, 0, 0, (HANDLE_WEAK) },
+       { { NULL }, 0, 0, 0, (HANDLE_WEAK_TRACK) },
+       { { NULL }, 0, 0, 0, (HANDLE_NORMAL) },
+       { { NULL }, 0, 0, 0, (HANDLE_PINNED) }
 };
 
 static HandleData *
 gc_handles_for_type (GCHandleType type)
 {
-       g_assert (type < HANDLE_TYPE_MAX);
-       return &gc_handles [type];
+       return type < HANDLE_TYPE_MAX ? &gc_handles [type] : NULL;
 }
 
 /* This assumes that the world is stopped. */
@@ -228,14 +216,20 @@ handle_data_grow (HandleData *handles, guint32 old_capacity)
        const size_t new_bucket_size = sizeof (**handles->entries) * growth;
        if (handles->capacity >= new_capacity)
                return;
-       entries = g_malloc0 (new_bucket_size);
+       entries = (gpointer *)g_malloc0 (new_bucket_size);
        if (handles->type == HANDLE_PINNED)
                sgen_register_root ((char *)entries, new_bucket_size, SGEN_DESCRIPTOR_NULL, ROOT_TYPE_PINNED, MONO_ROOT_SOURCE_GC_HANDLE, "pinned gc handles");
+       /* The zeroing of the newly allocated bucket must be complete before storing
+        * the new bucket pointer.
+        */
+       mono_memory_write_barrier ();
        if (InterlockedCompareExchangePointer ((volatile gpointer *)&handles->entries [new_bucket], entries, NULL) == NULL) {
+               /* It must not be the case that we succeeded in setting the bucket
+                * pointer, while someone else succeeded in changing the capacity.
+                */
                if (InterlockedCompareExchange ((volatile gint32 *)&handles->capacity, new_capacity, old_capacity) != old_capacity)
                        g_assert_not_reached ();
                handles->slot_hint = old_capacity;
-               mono_memory_write_barrier ();
                return;
        }
        /* Someone beat us to the allocation. */
@@ -266,20 +260,30 @@ retry:
                goto retry;
        }
        handles->slot_hint = index;
-       bucketize (index, &bucket, &offset);
-       if (!try_occupy_slot (handles, bucket, offset, obj, track))
-               goto retry;
-       /* If a GC happens shortly after a new bucket is allocated, the entire
-        * bucket could be scanned even though it's mostly empty. To avoid this, we
-        * track the maximum index seen so far, so that we can skip the empty slots.
+
+       /*
+        * If a GC happens shortly after a new bucket is allocated, the entire
+        * bucket could be scanned even though it's mostly empty. To avoid this,
+        * we track the maximum index seen so far, so that we can skip the empty
+        * slots.
+        *
+        * Note that we update `max_index` before we even try occupying the
+        * slot.  If we did it the other way around and a GC happened in
+        * between, the GC wouldn't know that the slot was occupied.  This is
+        * not a huge deal since `obj` is on the stack and thus pinned anyway,
+        * but hopefully some day it won't be anymore.
         */
        do {
                max_index = handles->max_index;
                if (index <= max_index)
                        break;
-       } while (!InterlockedCompareExchange ((volatile gint32 *)&handles->max_index, index, max_index));
+       } while (InterlockedCompareExchange ((volatile gint32 *)&handles->max_index, index, max_index) != max_index);
+
+       bucketize (index, &bucket, &offset);
+       if (!try_occupy_slot (handles, bucket, offset, obj, track))
+               goto retry;
 #ifdef HEAVY_STATISTICS
-       InterlockedIncrement64 ((volatile gint64 *)&stat_gc_handles_allocated);
+       InterlockedIncrement ((volatile gint32 *)&stat_gc_handles_allocated);
        if (stat_gc_handles_allocated > stat_gc_handles_max_allocated)
                stat_gc_handles_max_allocated = stat_gc_handles_allocated;
 #endif
@@ -301,7 +305,7 @@ object_older_than (GCObject *object, int generation)
  * This assumes that the world is stopped!
  */
 void
-sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, gpointer callback(gpointer, GCHandleType, int, gpointer), gpointer user)
+sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, SgenGCHandleIterateCallback callback, gpointer user)
 {
        HandleData *handle_data = gc_handles_for_type (handle_type);
        size_t bucket, offset;
@@ -331,7 +335,7 @@ sgen_gchandle_iterate (GCHandleType handle_type, int max_generation, gpointer ca
                        if (result)
                                SGEN_ASSERT (0, MONO_GC_HANDLE_OCCUPIED (result), "Why did the callback return an unoccupied entry?");
                        else
-                               HEAVY_STAT (InterlockedDecrement64 ((volatile gint64 *)&stat_gc_handles_allocated));
+                               HEAVY_STAT (InterlockedDecrement ((volatile gint32 *)&stat_gc_handles_allocated));
                        protocol_gchandle_update (handle_type, (gpointer)&entries [offset], hidden, result);
                        entries [offset] = result;
                }
@@ -442,8 +446,11 @@ GCObject*
 mono_gchandle_get_target (guint32 gchandle)
 {
        guint index = MONO_GC_HANDLE_SLOT (gchandle);
-       guint type = MONO_GC_HANDLE_TYPE (gchandle);
+       GCHandleType type = MONO_GC_HANDLE_TYPE (gchandle);
        HandleData *handles = gc_handles_for_type (type);
+       /* Invalid handles are possible; accessing one should produce NULL. (#34276) */
+       if (!handles)
+               return NULL;
        guint bucket, offset;
        g_assert (index < handles->capacity);
        bucketize (index, &bucket, &offset);
@@ -454,8 +461,10 @@ void
 sgen_gchandle_set_target (guint32 gchandle, GCObject *obj)
 {
        guint index = MONO_GC_HANDLE_SLOT (gchandle);
-       guint type = MONO_GC_HANDLE_TYPE (gchandle);
+       GCHandleType type = MONO_GC_HANDLE_TYPE (gchandle);
        HandleData *handles = gc_handles_for_type (type);
+       if (!handles)
+               return;
        guint bucket, offset;
        gpointer slot;
 
@@ -465,7 +474,7 @@ sgen_gchandle_set_target (guint32 gchandle, GCObject *obj)
        do {
                slot = handles->entries [bucket] [offset];
                SGEN_ASSERT (0, MONO_GC_HANDLE_OCCUPIED (slot), "Why are we setting the target on an unoccupied slot?");
-       } while (!try_set_slot (&handles->entries [bucket] [offset], obj, slot, handles->type));
+       } while (!try_set_slot (&handles->entries [bucket] [offset], obj, slot, (GCHandleType)handles->type));
 }
 
 static gpointer
@@ -478,7 +487,7 @@ retry:
        if (!MONO_GC_HANDLE_OCCUPIED (slot))
                return NULL;
        if (MONO_GC_HANDLE_IS_OBJECT_POINTER (slot)) {
-               GCObject *obj = MONO_GC_REVEAL_POINTER (slot, is_weak);
+               GCObject *obj = (GCObject *)MONO_GC_REVEAL_POINTER (slot, is_weak);
                /* See note [dummy use]. */
                sgen_dummy_use (obj);
                /*
@@ -502,8 +511,10 @@ gpointer
 sgen_gchandle_get_metadata (guint32 gchandle)
 {
        guint index = MONO_GC_HANDLE_SLOT (gchandle);
-       guint type = MONO_GC_HANDLE_TYPE (gchandle);
+       GCHandleType type = MONO_GC_HANDLE_TYPE (gchandle);
        HandleData *handles = gc_handles_for_type (type);
+       if (!handles)
+               return NULL;
        guint bucket, offset;
        if (index >= handles->capacity)
                return NULL;
@@ -523,8 +534,10 @@ void
 mono_gchandle_free (guint32 gchandle)
 {
        guint index = MONO_GC_HANDLE_SLOT (gchandle);
-       guint type = MONO_GC_HANDLE_TYPE (gchandle);
+       GCHandleType type = MONO_GC_HANDLE_TYPE (gchandle);
        HandleData *handles = gc_handles_for_type (type);
+       if (!handles)
+               return;
        guint bucket, offset;
        gpointer slot;
        bucketize (index, &bucket, &offset);
@@ -532,7 +545,7 @@ mono_gchandle_free (guint32 gchandle)
        if (index < handles->capacity && MONO_GC_HANDLE_OCCUPIED (slot)) {
                handles->entries [bucket] [offset] = NULL;
                protocol_gchandle_update (handles->type, (gpointer)&handles->entries [bucket] [offset], slot, NULL);
-               HEAVY_STAT (InterlockedDecrement64 ((volatile gint64 *)&stat_gc_handles_allocated));
+               HEAVY_STAT (InterlockedDecrement ((volatile gint32 *)&stat_gc_handles_allocated));
        } else {
                /* print a warning? */
        }
@@ -553,7 +566,7 @@ null_link_if_necessary (gpointer hidden, GCHandleType handle_type, int max_gener
        if (!MONO_GC_HANDLE_VALID (hidden))
                return hidden;
 
-       obj = MONO_GC_REVEAL_POINTER (hidden, MONO_GC_HANDLE_TYPE_IS_WEAK (handle_type));
+       obj = (GCObject *)MONO_GC_REVEAL_POINTER (hidden, MONO_GC_HANDLE_TYPE_IS_WEAK (handle_type));
        SGEN_ASSERT (0, obj, "Why is the hidden pointer NULL?");
 
        if (object_older_than (obj, max_generation))
@@ -588,14 +601,13 @@ typedef struct {
 static gpointer
 null_link_if (gpointer hidden, GCHandleType handle_type, int max_generation, gpointer user)
 {
-       /* Strictly speaking, function pointers are not guaranteed to have the same size as data pointers. */
        WeakLinkAlivePredicateClosure *closure = (WeakLinkAlivePredicateClosure *)user;
        GCObject *obj;
 
        if (!MONO_GC_HANDLE_VALID (hidden))
                return hidden;
 
-       obj = MONO_GC_REVEAL_POINTER (hidden, MONO_GC_HANDLE_TYPE_IS_WEAK (handle_type));
+       obj = (GCObject *)MONO_GC_REVEAL_POINTER (hidden, MONO_GC_HANDLE_TYPE_IS_WEAK (handle_type));
        SGEN_ASSERT (0, obj, "Why is the hidden pointer NULL?");
 
        if (object_older_than (obj, max_generation))
@@ -619,8 +631,8 @@ void
 sgen_init_gchandles (void)
 {
 #ifdef HEAVY_STATISTICS
-       mono_counters_register ("GC handles allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_gc_handles_allocated);
-       mono_counters_register ("max GC handles allocated", MONO_COUNTER_GC | MONO_COUNTER_ULONG, &stat_gc_handles_max_allocated);
+       mono_counters_register ("GC handles allocated", MONO_COUNTER_GC | MONO_COUNTER_UINT, (void *)&stat_gc_handles_allocated);
+       mono_counters_register ("max GC handles allocated", MONO_COUNTER_GC | MONO_COUNTER_UINT, (void *)&stat_gc_handles_max_allocated);
 #endif
 }