Fix OOM throwing if it happens in finalizer reference (take 2)

The Class::Alloc should return null if OOM happened during
adding finalizer reference, even if finalizable object is
allocated succesfully.

Added new more reliable test.

Change-Id: Id5fed3bdb16297d6d3a2b14ce62cc305aa703d60
Signed-off-by: Dmitry Petrochenko <dmitry.petrochenko@intel.com>
Signed-off-by: Serguei Katkov <serguei.i.katkov@intel.com>
Signed-off-by: Pavel Vyssotski <pavel.n.vyssotski@intel.com>
diff --git a/runtime/mirror/class-inl.h b/runtime/mirror/class-inl.h
index 599f178..1662ebf 100644
--- a/runtime/mirror/class-inl.h
+++ b/runtime/mirror/class-inl.h
@@ -575,6 +575,10 @@
                                                              allocator_type, VoidFunctor());
   if (add_finalizer && LIKELY(obj != nullptr)) {
     heap->AddFinalizerReference(self, &obj);
+    if (UNLIKELY(self->IsExceptionPending())) {
+      // Failed to allocate finalizer reference, it means that the whole allocation failed.
+      obj = nullptr;
+    }
   }
   return obj;
 }