Merge "Fix valgrind imgdiag test failure / memory leak in runtime"
diff --git a/build/Android.common_build.mk b/build/Android.common_build.mk
index 68f5ed7..0d3b4c9 100644
--- a/build/Android.common_build.mk
+++ b/build/Android.common_build.mk
@@ -138,7 +138,7 @@
 # Suggest final: Have to move to a more recent GCC.
 #                  -Wsuggest-final-types
 
-
+ART_TARGET_CLANG_CFLAGS := $(art_clang_cflags)
 ifeq ($(ART_HOST_CLANG),true)
   # Bug: 15446488. We don't omit the frame pointer to work around
   # clang/libunwind bugs that cause SEGVs in run-test-004-ThreadStress.
@@ -146,10 +146,14 @@
 else
   ART_HOST_CFLAGS += $(art_gcc_cflags)
 endif
-ifeq ($(ART_TARGET_CLANG),true)
-  ART_TARGET_CFLAGS += $(art_clang_cflags)
-else
+ifneq ($(ART_TARGET_CLANG),true)
   ART_TARGET_CFLAGS += $(art_gcc_cflags)
+else
+  # TODO: if we ever want to support GCC/Clang mix for multi-target products, this needs to be
+  #       split up.
+  ifeq ($(ART_TARGET_CLANG_$(TARGET_ARCH)),false)
+    ART_TARGET_CFLAGS += $(art_gcc_cflags)
+  endif
 endif
 
 # Clear local variables now their use has ended.
@@ -294,11 +298,9 @@
     LOCAL_CFLAGS += $(ART_TARGET_NON_DEBUG_CFLAGS)
   endif
 
-  # TODO: Also set when ART_TARGET_CLANG_$(arch)!=false and ART_TARGET_CLANG==true
+  LOCAL_CLANG_CFLAGS := $(ART_TARGET_CLANG_CFLAGS)
   $(foreach arch,$(ART_SUPPORTED_ARCH),
-    ifeq ($$(ART_TARGET_CLANG_$(arch)),true)
-      LOCAL_CFLAGS_$(arch) += $$(ART_TARGET_CLANG_CFLAGS_$(arch))
-  endif)
+    LOCAL_CLANG_CFLAGS_$(arch) += $$(ART_TARGET_CLANG_CFLAGS_$(arch)))
 
   # Clear locally used variables.
   art_target_cflags_ndebug_or_debug :=
diff --git a/build/Android.common_test.mk b/build/Android.common_test.mk
index 0ae42dd..da50d53 100644
--- a/build/Android.common_test.mk
+++ b/build/Android.common_test.mk
@@ -25,21 +25,7 @@
 
 # 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 \
-  test-art-host-run-test-gcstress-default-prebuild-114-ParallelGC64 \
-  test-art-host-run-test-gcstress-interpreter-prebuild-114-ParallelGC64 \
-  test-art-host-run-test-gcstress-optimizing-prebuild-114-ParallelGC64
+ART_TEST_KNOWN_BROKEN :=
 
 # Failing valgrind tests.
 # Note: *all* 64b tests involving the runtime do not work currently. b/15170219.
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 5aa264c..8180ce8 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -210,14 +210,14 @@
 LOCAL_MODULE := libart-gtest
 LOCAL_MODULE_TAGS := optional
 LOCAL_CPP_EXTENSION := cc
-LOCAL_CFLAGS := $(ART_TARGET_CFLAGS)
 LOCAL_SRC_FILES := runtime/common_runtime_test.cc compiler/common_compiler_test.cc
 LOCAL_C_INCLUDES := $(ART_C_INCLUDES) art/runtime art/compiler
 LOCAL_SHARED_LIBRARIES := libartd libartd-compiler libdl
 LOCAL_STATIC_LIBRARIES += libgtest
-LOCAL_CLANG := $(ART_TARGET_CLANG)
 LOCAL_ADDITIONAL_DEPENDENCIES := art/build/Android.common_build.mk
 LOCAL_ADDITIONAL_DEPENDENCIES += art/build/Android.gtest.mk
+$(eval $(call set-target-local-clang-vars))
+$(eval $(call set-target-local-cflags-vars,debug))
 include external/libcxx/libcxx.mk
 include $(BUILD_SHARED_LIBRARY)
 
diff --git a/compiler/optimizing/test/Inliner.java b/compiler/optimizing/test/Inliner.java
index 6aa7f8d..ce7409c 100644
--- a/compiler/optimizing/test/Inliner.java
+++ b/compiler/optimizing/test/Inliner.java
@@ -1,3 +1,19 @@
+/*
+* 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.
+*/
+
 public class Inliner {
 
   // CHECK-START: void Inliner.InlineVoid() inliner (before)
diff --git a/runtime/base/logging.cc b/runtime/base/logging.cc
index 7c2ef71..f3e0918 100644
--- a/runtime/base/logging.cc
+++ b/runtime/base/logging.cc
@@ -16,6 +16,7 @@
 
 #include "logging.h"
 
+#include <iostream>
 #include <limits>
 #include <sstream>
 
@@ -43,6 +44,19 @@
 static std::unique_ptr<std::string> gProgramInvocationName;
 static std::unique_ptr<std::string> gProgramInvocationShortName;
 
+// Print INTERNAL_FATAL messages directly instead of at destruction time. This only works on the
+// host right now: for the device, a stream buf collating output into lines and calling LogLine or
+// lower-level logging is necessary.
+#ifdef HAVE_ANDROID_OS
+static constexpr bool kPrintInternalFatalDirectly = false;
+#else
+static constexpr bool kPrintInternalFatalDirectly = !kIsTargetBuild;
+#endif
+
+static bool PrintDirectly(LogSeverity severity) {
+  return kPrintInternalFatalDirectly && severity == INTERNAL_FATAL;
+}
+
 const char* GetCmdLine() {
   return (gCmdLine.get() != nullptr) ? gCmdLine->c_str() : nullptr;
 }
@@ -170,31 +184,39 @@
 
 LogMessage::LogMessage(const char* file, unsigned int line, LogSeverity severity, int error)
   : data_(new LogMessageData(file, line, severity, error)) {
+  if (PrintDirectly(severity)) {
+    static const char* log_characters = "VDIWEFF";
+    CHECK_EQ(strlen(log_characters), INTERNAL_FATAL + 1U);
+    stream() << ProgramInvocationShortName() << " " << log_characters[static_cast<size_t>(severity)]
+             << " " << getpid() << " " << ::art::GetTid() << " " << file << ":" <<  line << "]";
+  }
 }
 LogMessage::~LogMessage() {
-  if (data_->GetSeverity() < gMinimumLogSeverity) {
-    return;  // No need to format something we're not going to output.
-  }
+  if (!PrintDirectly(data_->GetSeverity())) {
+    if (data_->GetSeverity() < gMinimumLogSeverity) {
+      return;  // No need to format something we're not going to output.
+    }
 
-  // Finish constructing the message.
-  if (data_->GetError() != -1) {
-    data_->GetBuffer() << ": " << strerror(data_->GetError());
-  }
-  std::string msg(data_->ToString());
+    // Finish constructing the message.
+    if (data_->GetError() != -1) {
+      data_->GetBuffer() << ": " << strerror(data_->GetError());
+    }
+    std::string msg(data_->ToString());
 
-  // Do the actual logging with the lock held.
-  {
-    MutexLock mu(Thread::Current(), *Locks::logging_lock_);
-    if (msg.find('\n') == std::string::npos) {
-      LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
-    } else {
-      msg += '\n';
-      size_t i = 0;
-      while (i < msg.size()) {
-        size_t nl = msg.find('\n', i);
-        msg[nl] = '\0';
-        LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
-        i = nl + 1;
+    // Do the actual logging with the lock held.
+    {
+      MutexLock mu(Thread::Current(), *Locks::logging_lock_);
+      if (msg.find('\n') == std::string::npos) {
+        LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), msg.c_str());
+      } else {
+        msg += '\n';
+        size_t i = 0;
+        while (i < msg.size()) {
+          size_t nl = msg.find('\n', i);
+          msg[nl] = '\0';
+          LogLine(data_->GetFile(), data_->GetLineNumber(), data_->GetSeverity(), &msg[i]);
+          i = nl + 1;
+        }
       }
     }
   }
@@ -206,6 +228,9 @@
 }
 
 std::ostream& LogMessage::stream() {
+  if (PrintDirectly(data_->GetSeverity())) {
+    return std::cerr;
+  }
   return data_->GetBuffer();
 }
 
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..fd66a02 100644
--- a/test/Android.run-test.mk
+++ b/test/Android.run-test.mk
@@ -214,9 +214,7 @@
 TEST_ART_BROKEN_NO_RELOCATE_TESTS :=
 
 # Tests that are broken with GC stress.
-TEST_ART_BROKEN_GCSTRESS_RUN_TESTS := \
-  004-SignalTest \
-  114-ParallelGC
+TEST_ART_BROKEN_GCSTRESS_RUN_TESTS :=
 
 ifneq (,$(filter gcstress,$(GC_TYPES)))
   ART_TEST_KNOWN_BROKEN += $(call all-run-test-names,$(TARGET_TYPES),$(RUN_TYPES),$(PREBUILD_TYPES), \