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;
 }
diff --git a/test/080-oom-throw-with-finalizer/expected.txt b/test/080-oom-throw-with-finalizer/expected.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/test/080-oom-throw-with-finalizer/expected.txt
diff --git a/test/080-oom-throw-with-finalizer/info.txt b/test/080-oom-throw-with-finalizer/info.txt
new file mode 100644
index 0000000..37091ef
--- /dev/null
+++ b/test/080-oom-throw-with-finalizer/info.txt
@@ -0,0 +1 @@
+Regression test on correct processing of OOM thrown while adding a finalizer reference.
diff --git a/test/080-oom-throw-with-finalizer/src/Main.java b/test/080-oom-throw-with-finalizer/src/Main.java
new file mode 100644
index 0000000..57e9721
--- /dev/null
+++ b/test/080-oom-throw-with-finalizer/src/Main.java
@@ -0,0 +1,71 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.Vector;
+
+public class Main {
+    static char [][] holder;
+
+    static class ArrayMemEater {
+        static boolean sawOome;
+
+        static void blowup(char[][] holder) {
+            try {
+                for (int i = 0; i < holder.length; ++i) {
+                    holder[i] = new char[1024 * 1024];
+                }
+            } catch (OutOfMemoryError oome) {
+                ArrayMemEater.sawOome = true;
+            }
+        }
+    }
+
+    static class InstanceFinalizerMemEater {
+        public void finalize() {}
+    }
+
+    static boolean triggerArrayOOM(char[][] holder) {
+        ArrayMemEater.blowup(holder);
+        return ArrayMemEater.sawOome;
+    }
+
+    static boolean triggerInstanceFinalizerOOM() {
+        boolean sawOome = false;
+        try {
+            Vector v = new Vector();
+            while (true) {
+                v.add(new InstanceFinalizerMemEater());
+            }
+        } catch (OutOfMemoryError e) {
+            sawOome = true;
+        }
+        return sawOome;
+    }
+
+    public static void main(String[] args) {
+        // Keep holder alive to make instance OOM happen faster.
+        holder = new char[128 * 1024][];
+        if (!triggerArrayOOM(holder)) {
+            System.out.println("NEW_ARRAY did not throw OOME");
+        }
+
+        if (!triggerInstanceFinalizerOOM()) {
+            System.out.println("NEW_INSTANCE (finalize) did not throw OOME");
+        }
+
+        System.runFinalization();
+    }
+}