[sgen] Throw OOM also from the managed allocator
authorVlad Brezae <brezaevlad@gmail.com>
Wed, 22 Feb 2017 23:29:27 +0000 (01:29 +0200)
committerVlad Brezae <brezaevlad@gmail.com>
Wed, 22 Feb 2017 23:33:09 +0000 (01:33 +0200)
Allocation functions from object.c correctly throw OOM when sgen allocation returns NULL. When allocating with the managed allocator we invoke sgen directly (as a fallback) and we didn't handle the NULL return case.

Fixes #52437

mono/metadata/sgen-mono.c

index 36bbdfa5c95f914d4b722e436acdb107f7856703..e8dd50b13d7be09050d60feaf0c89b399961beec 100644 (file)
@@ -1051,7 +1051,7 @@ create_allocator (int atype, ManagedAllocatorVariant variant)
 {
        int p_var, size_var, real_size_var, thread_var G_GNUC_UNUSED;
        gboolean slowpath = variant == MANAGED_ALLOCATOR_SLOW_PATH;
-       guint32 fastpath_branch, max_size_branch;
+       guint32 fastpath_branch, max_size_branch, no_oom_branch;
        MonoMethodBuilder *mb;
        MonoMethod *res;
        MonoMethodSignature *csig;
@@ -1353,6 +1353,13 @@ create_allocator (int atype, ManagedAllocatorVariant variant)
        } else {
                g_assert_not_reached ();
        }
+
+       /* if (ret == NULL) throw OOM; */
+       mono_mb_emit_byte (mb, CEE_DUP);
+       no_oom_branch = mono_mb_emit_branch (mb, CEE_BRTRUE);
+       mono_mb_emit_exception (mb, "OutOfMemoryException", NULL);
+
+       mono_mb_patch_branch (mb, no_oom_branch);
        mono_mb_emit_byte (mb, CEE_RET);
 
        /* Fastpath */