Fix signal test to work with gcstress

We now avoid running GC if we are handling a stack overflow, this
helps prevent running past the end of the stack overflow reserved
bytes.

Added logic in ThrowStackOverflowError to use a stack overflow
exception without a stack trace if we fail to allocate the stack
trace.

Bug: 16406852
Change-Id: Ib34e235cd0af6d7c4c93c9705fa822f2b9b23b38
diff --git a/build/Android.common_test.mk b/build/Android.common_test.mk
index 0ae42dd..8e38078 100644
--- a/build/Android.common_test.mk
+++ b/build/Android.common_test.mk
@@ -26,14 +26,6 @@
 # List of known broken tests that we won't attempt to execute. The test name must be the full
 # rule name such as test-art-host-oat-optimizing-HelloWorld64.
 ART_TEST_KNOWN_BROKEN := \
-  test-art-target-run-test-gcstress-optimizing-prebuild-004-SignalTest32 \
-  test-art-target-run-test-gcstress-optimizing-norelocate-004-SignalTest32 \
-  test-art-target-run-test-gcstress-default-prebuild-004-SignalTest32 \
-  test-art-target-run-test-gcstress-default-norelocate-004-SignalTest32 \
-  test-art-target-run-test-gcstress-optimizing-relocate-004-SignalTest32 \
-  test-art-target-run-test-gcstress-default-relocate-004-SignalTest32 \
-  test-art-target-run-test-gcstress-optimizing-no-prebuild-004-SignalTest32 \
-  test-art-target-run-test-gcstress-default-no-prebuild-004-SignalTest32 \
   test-art-host-run-test-gcstress-default-prebuild-114-ParallelGC32 \
   test-art-host-run-test-gcstress-interpreter-prebuild-114-ParallelGC32 \
   test-art-host-run-test-gcstress-optimizing-prebuild-114-ParallelGC32 \
diff --git a/runtime/entrypoints/entrypoint_utils.cc b/runtime/entrypoints/entrypoint_utils.cc
index e734d45..db51264 100644
--- a/runtime/entrypoints/entrypoint_utils.cc
+++ b/runtime/entrypoints/entrypoint_utils.cc
@@ -183,14 +183,12 @@
         env->SetObjectField(exc.get(),
                             WellKnownClasses::java_lang_Throwable_stackTrace,
                             stack_trace_elem.get());
-
-        // Throw the exception.
-        ThrowLocation throw_location = self->GetCurrentLocationForThrow();
-        self->SetException(throw_location,
-            reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
       } else {
         error_msg = "Could not create stack trace.";
       }
+      // Throw the exception.
+      self->SetException(self->GetCurrentLocationForThrow(),
+                         reinterpret_cast<mirror::Throwable*>(self->DecodeJObject(exc.get())));
     } else {
       // Could not allocate a string object.
       error_msg = "Couldn't throw new StackOverflowError because JNI NewStringUTF failed.";
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index 26d6117..2575676 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -2106,7 +2106,9 @@
   ScopedThreadStateChange tsc(self, kWaitingPerformingGc);
   Locks::mutator_lock_->AssertNotHeld(self);
   if (self->IsHandlingStackOverflow()) {
-    LOG(WARNING) << "Performing GC on a thread that is handling a stack overflow.";
+    // If we are throwing a stack overflow error we probably don't have enough remaining stack
+    // space to run the GC.
+    return collector::kGcTypeNone;
   }
   bool compacting_gc;
   {
diff --git a/test/004-SignalTest/src/Main.java b/test/004-SignalTest/src/Main.java
index 0391592..8b1f49b 100644
--- a/test/004-SignalTest/src/Main.java
+++ b/test/004-SignalTest/src/Main.java
@@ -20,7 +20,7 @@
     private static native int testSignal();
 
     private static void stackOverflow() {
-       stackOverflow();
+        stackOverflow();
     }
 
     public static void main(String[] args) {
@@ -40,7 +40,6 @@
         }
         try {
             stackOverflow();
-
             // Should never get here.
             throw new AssertionError();
         } catch (StackOverflowError e) {
diff --git a/test/Android.run-test.mk b/test/Android.run-test.mk
index 170ec31..2abf48e 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -215,7 +215,6 @@
 
 # Tests that are broken with GC stress.
 TEST_ART_BROKEN_GCSTRESS_RUN_TESTS := \
-  004-SignalTest \
   114-ParallelGC
 
 ifneq (,$(filter gcstress,$(GC_TYPES)))