blob: dd04c3377db8279c4482cf3723d9dbb662f28fb8 [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
21#include "platform/bionic/android_unsafe_frame_pointer_chase.h"
22
23// Prevent tail calls inside recurse.
24__attribute__((weak, noinline)) size_t nop(size_t val) {
25 return val;
26}
27
28// Call android_unsafe_frame_pointer_chase inside count recurse stack frames.
29__attribute__((weak, noinline)) int recurse(int count, uintptr_t* buf, size_t num_entries) {
30 if (count != 0) return nop(recurse(count - 1, buf, num_entries));
31 return nop(android_unsafe_frame_pointer_chase(buf, num_entries));
32}
33
34static constexpr size_t kNumFrames = 32;
35
36static bool CheckFrames(uintptr_t* frames, size_t num_frames) {
37 // We expect one recurse frame calling android_unsafe_frame_pointer_chase, followed by kNumFrames identical
38 // recurse frames calling themselves, followed by at least one frame (the first caller of
39 // recurse).
40 if (num_frames < kNumFrames + 2) {
41 printf("num_frames (0x%zu) < kNumFrames + 2", num_frames);
42 return false;
43 }
44
45 if (frames[0] == frames[1]) {
46 printf("frames[0] == frames[1] (0x%zx)", frames[0]);
47 return false;
48 }
49
50 for (size_t i = 2; i <= kNumFrames; ++i) {
51 if (frames[i] != frames[1]) {
52 printf("frames[i] (0x%zx) != frames[1] (0x%zx)", frames[i], frames[1]);
53 return false;
54 }
55 }
56
57 if (frames[kNumFrames] == frames[kNumFrames + 1]) {
58 printf("frames[kNumFrames] == frames[kNumFrames + 1] (0x%zx)", frames[kNumFrames]);
59 return false;
60 }
61
62 return true;
63}
64
65TEST(android_unsafe_frame_pointer_chase, main_thread) {
66 size_t size = recurse(kNumFrames, 0, 0);
67
68 uintptr_t frames[kNumFrames + 2];
69 size_t size2 = recurse(kNumFrames, frames, kNumFrames + 2);
70 EXPECT_EQ(size2, size);
71
72 EXPECT_TRUE(CheckFrames(frames, size));
73}
74
75static void *BacktraceThread(void *) {
76 size_t size = recurse(kNumFrames, 0, 0);
77
78 uintptr_t frames[kNumFrames + 2];
79 size_t size2 = recurse(kNumFrames, frames, kNumFrames + 2);
80 if (size2 != size) {
81 return (void*)"size2 != size";
82 }
83
84 if (!CheckFrames(frames, size)) {
85 return (void*)"CheckFrames failed";
86 }
87 return nullptr;
88}
89
90TEST(android_unsafe_frame_pointer_chase, pthread) {
91 pthread_t t;
92 ASSERT_EQ(0, pthread_create(&t, nullptr, BacktraceThread, nullptr));
93 void* retval;
94 ASSERT_EQ(0, pthread_join(t, &retval));
95 EXPECT_EQ(nullptr, reinterpret_cast<char*>(retval));
96}
97
98#endif // __BIONIC__