blob: 2f891a6e145f907c7605f25dbf2b4b87bee4d716 [file] [log] [blame]
Sergey Melnikovc45087b2013-01-25 16:40:13 +04001/*
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 Hughesbee19932014-09-17 17:21:20 -070023#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
Elliott Hughes71ba5892018-02-07 12:44:45 -080032#include "SignalUtils.h"
Elliott Hughesbee19932014-09-17 17:21:20 -070033
Pavel Chupin50321e22014-09-26 16:02:09 +040034#define noinline __attribute__((__noinline__))
35#define __unused __attribute__((__unused__))
Elliott Hughesbee19932014-09-17 17:21:20 -070036
Yabin Cui33ac04a2015-09-22 11:16:15 -070037_Unwind_Reason_Code FrameCounter(_Unwind_Context* ctx __unused, void* arg) {
Elliott Hughesbee19932014-09-17 17:21:20 -070038 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 Melnikovc45087b2013-01-25 16:40:13 +040061}
62
Elliott Hughesbee19932014-09-17 17:21:20 -070063static int noinline unwind_one_frame_deeper() {
64 int count = 0;
65 _Unwind_Backtrace(FrameCounter, &count);
66 return count;
67}
68
Peter Collingbourne26d83ba2021-06-04 14:35:13 -070069static void UnwindTest() {
Elliott Hughesbee19932014-09-17 17:21:20 -070070 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
Peter Collingbourne26d83ba2021-06-04 14:35:13 -070076TEST(stack_unwinding, easy) {
77 UnwindTest();
78}
79
80TEST(stack_unwinding, thread) {
81 pthread_t thread;
82 ASSERT_EQ(0, pthread_create(&thread, nullptr, [](void*) -> void* {
83 UnwindTest();
84 return nullptr;
85 }, nullptr));
86 void *retval;
87 ASSERT_EQ(0, pthread_join(thread, &retval));
88 EXPECT_EQ(nullptr, retval);
89}
90
Christopher Ferris05d26212015-02-03 15:16:03 -080091struct UnwindData {
92 volatile bool signal_handler_complete = false;
93 int expected_frame_count = 0;
94 int handler_frame_count = 0;
95 int handler_one_deeper_frame_count = 0;
96};
97
98static UnwindData g_unwind_data;
Elliott Hughesbee19932014-09-17 17:21:20 -070099
Ryan Prichardcdace2f2021-02-10 07:08:18 +0000100static void noinline UnwindSignalHandler(int) {
Christopher Ferris05d26212015-02-03 15:16:03 -0800101 _Unwind_Backtrace(FrameCounter, &g_unwind_data.handler_frame_count);
Elliott Hughesbee19932014-09-17 17:21:20 -0700102
Christopher Ferris05d26212015-02-03 15:16:03 -0800103 g_unwind_data.handler_one_deeper_frame_count = unwind_one_frame_deeper();
104 g_unwind_data.signal_handler_complete = true;
105}
106
107static void verify_unwind_data(const UnwindData& unwind_data) {
Christopher Ferrisac2f3dd2017-03-22 19:05:50 -0700108 // In order to avoid a false positive, the caller must have at least 2 frames
109 // outside of the signal handler. This avoids a case where the only frame
110 // right after the signal handler winds up being garbage.
111 EXPECT_GT(unwind_data.handler_frame_count, unwind_data.expected_frame_count + 1);
112
Christopher Ferris05d26212015-02-03 15:16:03 -0800113 EXPECT_EQ(unwind_data.handler_frame_count + 1, unwind_data.handler_one_deeper_frame_count);
Elliott Hughesbee19932014-09-17 17:21:20 -0700114}
115
Peter Collingbourne26d83ba2021-06-04 14:35:13 -0700116static void noinline SignalUnwindTest() {
Christopher Ferris05d26212015-02-03 15:16:03 -0800117 g_unwind_data = {};
Elliott Hughesbee19932014-09-17 17:21:20 -0700118
Christopher Ferris05d26212015-02-03 15:16:03 -0800119 _Unwind_Backtrace(FrameCounter, &g_unwind_data.expected_frame_count);
Christopher Ferrisac2f3dd2017-03-22 19:05:50 -0700120 ASSERT_LE(2, g_unwind_data.expected_frame_count)
121 << "The current call must contain at least 2 frames for the test to be valid.";
Christopher Ferris05d26212015-02-03 15:16:03 -0800122
Elliott Hughesbee19932014-09-17 17:21:20 -0700123 ASSERT_EQ(0, kill(getpid(), SIGUSR1));
Christopher Ferris05d26212015-02-03 15:16:03 -0800124 while (!g_unwind_data.signal_handler_complete) {}
125
126 verify_unwind_data(g_unwind_data);
Elliott Hughesbee19932014-09-17 17:21:20 -0700127}
128
Christopher Ferrisac2f3dd2017-03-22 19:05:50 -0700129TEST(stack_unwinding, unwind_through_signal_frame) {
130 ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler);
131
Peter Collingbourne26d83ba2021-06-04 14:35:13 -0700132 SignalUnwindTest();
Christopher Ferrisac2f3dd2017-03-22 19:05:50 -0700133}
134
Pavel Chupin50321e22014-09-26 16:02:09 +0400135// On LP32, the SA_SIGINFO flag gets you __restore_rt instead of __restore.
136TEST(stack_unwinding, unwind_through_signal_frame_SA_SIGINFO) {
Pavel Chupin50321e22014-09-26 16:02:09 +0400137 ScopedSignalHandler ssh(SIGUSR1, UnwindSignalHandler, SA_SIGINFO);
Elliott Hughesbee19932014-09-17 17:21:20 -0700138
Peter Collingbourne26d83ba2021-06-04 14:35:13 -0700139 SignalUnwindTest();
Sergey Melnikovc45087b2013-01-25 16:40:13 +0400140}