ART: Handle OOM in stack overflow

When creating a stack overflow error, we must not allocate a
new out-of-memory error. Running its constructor will lead to
a nested exception.

Bug: 27663199

Change-Id: I2a7b5b937730eeade22dce654cfc4ad903c5f040
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 01db90a..c2f772f 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -1304,6 +1304,13 @@
 }
 
 void Heap::ThrowOutOfMemoryError(Thread* self, size_t byte_count, AllocatorType allocator_type) {
+  // If we're in a stack overflow, do not create a new exception. It would require running the
+  // constructor, which will of course still be in a stack overflow.
+  if (self->IsHandlingStackOverflow()) {
+    self->SetException(Runtime::Current()->GetPreAllocatedOutOfMemoryError());
+    return;
+  }
+
   std::ostringstream oss;
   size_t total_bytes_free = GetFreeMemory();
   oss << "Failed to allocate a " << byte_count << " byte allocation with " << total_bytes_free