blob: 409cfabc046c915bdcf59185ca210bf7a3cceb50 [file] [log] [blame]
Peter Collingbourne5f45c182020-01-14 17:59:41 -08001/*
2 * Copyright (C) 2020 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#include <gtest/gtest.h>
18
19#if defined(__BIONIC__)
20
Peter Collingbourneb8d13482020-02-26 19:01:13 -080021#include <sys/mman.h>
22
Peter Collingbourne5f45c182020-01-14 17:59:41 -080023#include "platform/bionic/android_unsafe_frame_pointer_chase.h"
24
25// Prevent tail calls inside recurse.
26__attribute__((weak, noinline)) size_t nop(size_t val) {
27 return val;
28}
29
30// Call android_unsafe_frame_pointer_chase inside count recurse stack frames.
31__attribute__((weak, noinline)) int recurse(int count, uintptr_t* buf, size_t num_entries) {
32 if (count != 0) return nop(recurse(count - 1, buf, num_entries));
33 return nop(android_unsafe_frame_pointer_chase(buf, num_entries));
34}
35
36static constexpr size_t kNumFrames = 32;
37
38static bool CheckFrames(uintptr_t* frames, size_t num_frames) {
39 // We expect one recurse frame calling android_unsafe_frame_pointer_chase, followed by kNumFrames identical
40 // recurse frames calling themselves, followed by at least one frame (the first caller of
41 // recurse).
42 if (num_frames < kNumFrames + 2) {
43 printf("num_frames (0x%zu) < kNumFrames + 2", num_frames);
44 return false;
45 }
46
47 if (frames[0] == frames[1]) {
48 printf("frames[0] == frames[1] (0x%zx)", frames[0]);
49 return false;
50 }
51
52 for (size_t i = 2; i <= kNumFrames; ++i) {
53 if (frames[i] != frames[1]) {
54 printf("frames[i] (0x%zx) != frames[1] (0x%zx)", frames[i], frames[1]);
55 return false;
56 }
57 }
58
59 if (frames[kNumFrames] == frames[kNumFrames + 1]) {
60 printf("frames[kNumFrames] == frames[kNumFrames + 1] (0x%zx)", frames[kNumFrames]);
61 return false;
62 }
63
64 return true;
65}
66
67TEST(android_unsafe_frame_pointer_chase, main_thread) {
68 size_t size = recurse(kNumFrames, 0, 0);
69
70 uintptr_t frames[kNumFrames + 2];
71 size_t size2 = recurse(kNumFrames, frames, kNumFrames + 2);
72 EXPECT_EQ(size2, size);
73
74 EXPECT_TRUE(CheckFrames(frames, size));
75}
76
Peter Collingbourneb8d13482020-02-26 19:01:13 -080077static const char* tester_func() {
Peter Collingbourne5f45c182020-01-14 17:59:41 -080078 size_t size = recurse(kNumFrames, 0, 0);
79
80 uintptr_t frames[kNumFrames + 2];
81 size_t size2 = recurse(kNumFrames, frames, kNumFrames + 2);
82 if (size2 != size) {
Peter Collingbourneb8d13482020-02-26 19:01:13 -080083 return "size2 != size";
Peter Collingbourne5f45c182020-01-14 17:59:41 -080084 }
85
86 if (!CheckFrames(frames, size)) {
Peter Collingbourneb8d13482020-02-26 19:01:13 -080087 return "CheckFrames failed";
Peter Collingbourne5f45c182020-01-14 17:59:41 -080088 }
89 return nullptr;
90}
91
Peter Collingbourneb8d13482020-02-26 19:01:13 -080092static void* BacktraceThread(void*) {
93 return (void*)tester_func();
94}
95
Peter Collingbourne5f45c182020-01-14 17:59:41 -080096TEST(android_unsafe_frame_pointer_chase, pthread) {
97 pthread_t t;
98 ASSERT_EQ(0, pthread_create(&t, nullptr, BacktraceThread, nullptr));
99 void* retval;
100 ASSERT_EQ(0, pthread_join(t, &retval));
101 EXPECT_EQ(nullptr, reinterpret_cast<char*>(retval));
102}
103
Peter Collingbourneb8d13482020-02-26 19:01:13 -0800104static bool g_handler_called;
105static const char* g_handler_tester_result;
106
107static void BacktraceHandler(int) {
108 g_handler_called = true;
109 g_handler_tester_result = tester_func();
110}
111
112static constexpr size_t kStackSize = 16384;
113
114static void* SignalBacktraceThread(void* sp) {
115 stack_t ss;
116 ss.ss_sp = sp;
117 ss.ss_flags = 0;
118 ss.ss_size = kStackSize;
119 sigaltstack(&ss, nullptr);
120
121 struct sigaction s = {};
122 s.sa_handler = BacktraceHandler;
123 s.sa_flags = SA_ONSTACK;
124 sigaction(SIGRTMIN, &s, nullptr);
125
126 raise(SIGRTMIN);
Florian Mayerc4c76772024-12-12 06:28:47 -0800127 sigaltstack(nullptr, nullptr);
Peter Collingbourneb8d13482020-02-26 19:01:13 -0800128 return nullptr;
129}
130
131TEST(android_unsafe_frame_pointer_chase, sigaltstack) {
132 // Create threads where the alternate stack appears both after and before the regular stack, and
133 // call android_unsafe_frame_pointer_chase from a signal handler. Without handling for the
134 // alternate signal stack, this would cause false negatives or potential false positives in the
135 // android_unsafe_frame_pointer_chase function.
136 void* stacks =
137 mmap(nullptr, kStackSize * 2, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
138
139 for (unsigned i = 0; i != 2; ++i) {
140 pthread_t t;
141 pthread_attr_t attr;
142 ASSERT_EQ(0, pthread_attr_init(&attr));
143 ASSERT_EQ(0, pthread_attr_setstack(&attr, reinterpret_cast<char*>(stacks) + kStackSize * i,
144 kStackSize));
145
146 ASSERT_EQ(0, pthread_create(&t, &attr, SignalBacktraceThread,
147 reinterpret_cast<char*>(stacks) + kStackSize * (1 - i)));
148 void* retval;
149 ASSERT_EQ(0, pthread_join(t, &retval));
150
151 EXPECT_TRUE(g_handler_called);
152 EXPECT_EQ(nullptr, g_handler_tester_result);
153 g_handler_called = false;
154 }
155
156 munmap(stacks, kStackSize * 2);
157}
158
Florian Mayerc4c76772024-12-12 06:28:47 -0800159static void* SigaltstackOnCallerStack(void*) {
160 char altstack[kStackSize];
161 SignalBacktraceThread(altstack);
162 EXPECT_TRUE(g_handler_called);
163 EXPECT_EQ(nullptr, g_handler_tester_result);
164 g_handler_called = false;
165 return nullptr;
166}
167
168TEST(android_unsafe_frame_pointer_chase, sigaltstack_on_main_thread) {
169 SigaltstackOnCallerStack(nullptr);
170}
171
172TEST(android_unsafe_frame_pointer_chase, sigaltstack_on_pthread) {
173 pthread_t t;
174 ASSERT_EQ(0, pthread_create(&t, nullptr, SigaltstackOnCallerStack, nullptr));
175 void* retval;
176 ASSERT_EQ(0, pthread_join(t, &retval));
177 EXPECT_EQ(nullptr, retval);
178}
179
Peter Collingbourne5f45c182020-01-14 17:59:41 -0800180#endif // __BIONIC__