Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * Contributed by: Intel Corporation |
| 19 | */ |
| 20 | |
| 21 | #include <gtest/gtest.h> |
| 22 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 23 | #include <dlfcn.h> |
| 24 | #include <signal.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
| 28 | #include <sys/types.h> |
| 29 | #include <unistd.h> |
| 30 | #include <unwind.h> |
| 31 | |
| 32 | #include "ScopedSignalHandler.h" |
| 33 | |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 34 | #define noinline __attribute__((__noinline__)) |
| 35 | #define __unused __attribute__((__unused__)) |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 36 | |
| 37 | static _Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) { |
| 38 | int* count_ptr = reinterpret_cast<int*>(arg); |
| 39 | |
| 40 | #if SHOW_FRAME_LOCATIONS |
| 41 | void* ip = reinterpret_cast<void*>(_Unwind_GetIP(ctx)); |
| 42 | |
| 43 | const char* symbol = "<unknown>"; |
| 44 | int offset = 0; |
| 45 | |
| 46 | Dl_info info; |
| 47 | memset(&info, 0, sizeof(info)); |
| 48 | if (dladdr(ip, &info) != 0) { |
| 49 | symbol = info.dli_sname; |
| 50 | if (info.dli_saddr != nullptr) { |
| 51 | offset = static_cast<int>(reinterpret_cast<char*>(ip) - reinterpret_cast<char*>(info.dli_saddr)); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | fprintf(stderr, " #%02d %p %s%+d (%s)\n", *count_ptr, ip, symbol, offset, info.dli_fname ? info.dli_fname : "??"); |
| 56 | fflush(stderr); |
| 57 | #endif |
| 58 | |
| 59 | ++*count_ptr; |
| 60 | return _URC_NO_REASON; |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 61 | } |
| 62 | |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 63 | static int noinline unwind_one_frame_deeper() { |
| 64 | int count = 0; |
| 65 | _Unwind_Backtrace(FrameCounter, &count); |
| 66 | return count; |
| 67 | } |
| 68 | |
| 69 | TEST(stack_unwinding, easy) { |
| 70 | int count = 0; |
| 71 | _Unwind_Backtrace(FrameCounter, &count); |
| 72 | int deeper_count = unwind_one_frame_deeper(); |
| 73 | ASSERT_EQ(count + 1, deeper_count); |
| 74 | } |
| 75 | |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 76 | static volatile bool signal_handler_run = false; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 77 | static int killer_count = 0; |
| 78 | static int handler_count = 0; |
| 79 | static int handler_one_deeper_count = 0; |
| 80 | |
| 81 | static void noinline UnwindSignalHandler(int) { |
| 82 | _Unwind_Backtrace(FrameCounter, &handler_count); |
| 83 | ASSERT_GT(handler_count, killer_count); |
| 84 | |
| 85 | handler_one_deeper_count = unwind_one_frame_deeper(); |
| 86 | ASSERT_EQ(handler_count + 1, handler_one_deeper_count); |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 87 | signal_handler_run = true; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST(stack_unwinding, unwind_through_signal_frame) { |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 91 | killer_count = handler_count = handler_one_deeper_count = 0; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 92 | ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler); |
| 93 | |
| 94 | _Unwind_Backtrace(FrameCounter, &killer_count); |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 95 | signal_handler_run = false; |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 96 | ASSERT_EQ(0, kill(getpid(), SIGUSR1)); |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 97 | while (!signal_handler_run) {} |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 100 | // On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore. |
| 101 | TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) { |
| 102 | killer_count = handler_count = handler_one_deeper_count = 0; |
| 103 | ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO); |
Elliott Hughes | bee1993 | 2014-09-17 17:21:20 -0700 | [diff] [blame] | 104 | |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 105 | _Unwind_Backtrace(FrameCounter, &killer_count); |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 106 | signal_handler_run = false; |
Pavel Chupin | 50321e2 | 2014-09-26 16:02:09 +0400 | [diff] [blame] | 107 | ASSERT_EQ(0, kill(getpid(), SIGUSR1)); |
Yabin Cui | e323e99 | 2014-12-02 09:57:45 -0800 | [diff] [blame^] | 108 | while (!signal_handler_run) {} |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 109 | } |