[x86,x86_64] Fix libgcc unwinding through signal

This change provides __restore/__restore_rt on x86 and __restore_rt on
x86_64 with unwinding information to be able to unwind through signal
frame via libgcc provided unwinding interface. See comments inlined for
more details.

Also remove the test that had a dependency on
__attribute__((cleanup(foo_cleanup))). It doesn't provide us with any
better test coverage than we have from the newer tests, and it doesn't
work well across a variety architectures (presumably because no one uses
this attribute in the real world).

Tested this on host via bionic-unit-tests-run-on-host on both x86 and
x86-64.

Bug: 17436734
Change-Id: I2f06814e82c8faa732cb4f5648868dc0fd2e5fe4
Signed-off-by: Pavel Chupin <pavel.v.chupin@intel.com>
diff --git a/tests/Android.mk b/tests/Android.mk
index f61f175..c0345e4 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -91,6 +91,7 @@
     semaphore_test.cpp \
     signal_test.cpp \
     stack_protector_test.cpp \
+    stack_unwinding_test.cpp \
     stdatomic_test.cpp \
     stdint_test.cpp \
     stdio_test.cpp \
@@ -234,8 +235,6 @@
     atexit_test.cpp \
     dlext_test.cpp \
     dlfcn_test.cpp \
-    stack_unwinding_test.cpp \
-    stack_unwinding_test_impl.c \
 
 bionic-unit-tests_cflags := $(test_cflags)
 
diff --git a/tests/ScopedSignalHandler.h b/tests/ScopedSignalHandler.h
index 89a14a6..3ec23b0 100644
--- a/tests/ScopedSignalHandler.h
+++ b/tests/ScopedSignalHandler.h
@@ -21,9 +21,10 @@
 
 class ScopedSignalHandler {
  public:
-  ScopedSignalHandler(int signal_number, void (*handler)(int)) : signal_number_(signal_number) {
+  ScopedSignalHandler(int signal_number, void (*handler)(int), int sa_flags = 0)
+      : signal_number_(signal_number) {
     sigemptyset(&action_.sa_mask);
-    action_.sa_flags = 0;
+    action_.sa_flags = sa_flags;
     action_.sa_handler = handler;
     sigaction(signal_number_, &action_, &old_action_);
   }
diff --git a/tests/stack_unwinding_test.cpp b/tests/stack_unwinding_test.cpp
index 017a5f2..3fc45c5 100644
--- a/tests/stack_unwinding_test.cpp
+++ b/tests/stack_unwinding_test.cpp
@@ -31,7 +31,8 @@
 
 #include "ScopedSignalHandler.h"
 
-#define noinline __attribute__((noinline))
+#define noinline __attribute__((__noinline__))
+#define __unused __attribute__((__unused__))
 
 static _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) {
   int* count_ptr = reinterpret_cast<int*>(arg);
@@ -85,6 +86,7 @@
 }
 
 TEST(stack_unwinding, unwind_through_signal_frame) {
+  killer_count = handler_count = handler_one_deeper_count = 0;
   ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler);
 
   _Unwind_Backtrace(FrameCounter, &killer_count);
@@ -92,11 +94,12 @@
   ASSERT_EQ(0, kill(getpid(), SIGUSR1));
 }
 
-extern "C" void unwind_through_frame_with_cleanup_function();
+// On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore.
+TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) {
+  killer_count = handler_count = handler_one_deeper_count = 0;
+  ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO);
 
-// We have to say "DeathTest" here so gtest knows to run this test (which exits)
-// in its own process.
-TEST(stack_unwinding_DeathTest, unwind_through_frame_with_cleanup_function) {
-  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
-  ASSERT_EXIT(unwind_through_frame_with_cleanup_function(), ::testing::ExitedWithCode(42), "");
+  _Unwind_Backtrace(FrameCounter, &killer_count);
+
+  ASSERT_EQ(0, kill(getpid(), SIGUSR1));
 }
diff --git a/tests/stack_unwinding_test_impl.c b/tests/stack_unwinding_test_impl.c
deleted file mode 100644
index 2e03938..0000000
--- a/tests/stack_unwinding_test_impl.c
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 2013 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.
- */
-
-/*
- * Contributed by: Intel Corporation
- */
-
-#include <dlfcn.h>
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <unwind.h>
-
-#define noinline __attribute__((__noinline__))
-
-#ifndef __unused
-#define __unused __attribute__((__unused__))
-#endif
-
-static noinline _Unwind_Reason_Code cleanup_unwind_fn(int a __unused,
-    _Unwind_Action action,
-    _Unwind_Exception_Class b __unused,
-    struct _Unwind_Exception* c __unused,
-    struct _Unwind_Context* ctx __unused,
-    void* e __unused) {
-  if ((action & _UA_END_OF_STACK) != 0) {
-    abort(); // We reached the end of the stack without executing foo_cleanup (which would have exited). Test failed.
-  }
-  return _URC_NO_REASON;
-}
-
-static void noinline foo_cleanup(char* param __unused) {
-  exit(42);
-}
-
-static void noinline function_with_cleanup_function() {
-  char c __attribute__((cleanup(foo_cleanup))) __unused;
-  *((int*) 1) = 0;
-}
-
-static void noinline cleanup_sigsegv_handler(int param __unused) {
-  struct _Unwind_Exception* exception = (struct _Unwind_Exception*) calloc(1, sizeof(*exception));
-  _Unwind_ForcedUnwind(exception, cleanup_unwind_fn, 0);
-}
-
-void unwind_through_frame_with_cleanup_function() {
-  signal(SIGSEGV, &cleanup_sigsegv_handler);
-  function_with_cleanup_function();
-}