xref: /aosp_15_r20/external/perfetto/src/profiling/memory/client_unittest.cc (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2018 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 "src/profiling/memory/client.h"
18 
19 #include <signal.h>
20 
21 #include <thread>
22 
23 #include "perfetto/base/thread_utils.h"
24 #include "perfetto/ext/base/unix_socket.h"
25 #include "src/profiling/memory/wire_protocol.h"
26 #include "test/gtest_and_gmock.h"
27 
28 namespace perfetto {
29 namespace profiling {
30 namespace {
31 
TEST(ClientTest,GetThreadStackRangeBase)32 TEST(ClientTest, GetThreadStackRangeBase) {
33   std::thread th([] {
34     StackRange stackrange = GetThreadStackRange();
35     ASSERT_NE(stackrange.begin, nullptr);
36     ASSERT_NE(stackrange.end, nullptr);
37     // The implementation assumes the stack grows from higher addresses to
38     // lower. We will need to rework once we encounter architectures where the
39     // stack grows the other way.
40     EXPECT_LT(stackrange.begin, __builtin_frame_address(0));
41     EXPECT_GT(stackrange.end, __builtin_frame_address(0));
42   });
43   th.join();
44 }
45 
46 #if defined(ADDRESS_SANITIZER)
47 #define MAYBE_GetSigaltStackRange DISABLED_GetSigaltStackRange
48 #else
49 #define MAYBE_GetSigaltStackRange GetSigaltStackRange
50 #endif
51 
TEST(ClientTest,MAYBE_GetSigaltStackRange)52 TEST(ClientTest, MAYBE_GetSigaltStackRange) {
53   char stack[4096];
54   stack_t altstack{};
55   stack_t old_altstack{};
56   altstack.ss_sp = stack;
57   altstack.ss_size = sizeof(stack);
58   ASSERT_NE(sigaltstack(&altstack, &old_altstack), -1);
59 
60   struct sigaction oldact;
61   struct sigaction newact {};
62 
63   static StackRange stackrange;
64   static const char* stackptr;
65   newact.sa_handler = [](int) {
66     stackrange = GetSigAltStackRange();
67     stackptr = static_cast<char*>(__builtin_frame_address(0));
68   };
69   newact.sa_flags = SA_ONSTACK;
70   int res = sigaction(SIGUSR1, &newact, &oldact);
71   ASSERT_NE(res, -1);
72 
73   raise(SIGUSR1);
74 
75   PERFETTO_CHECK(sigaction(SIGUSR1, &oldact, nullptr) != -1);
76   PERFETTO_CHECK(sigaltstack(&old_altstack, nullptr) != -1);
77 
78   ASSERT_EQ(stackrange.begin, stack);
79   ASSERT_EQ(stackrange.end, &stack[4096]);
80   ASSERT_LT(stackrange.begin, stackptr);
81   ASSERT_GT(stackrange.end, stackptr);
82 }
83 
TEST(ClientTest,GetMainThreadStackRange)84 TEST(ClientTest, GetMainThreadStackRange) {
85   if (getpid() != base::GetThreadId())
86     GTEST_SKIP() << "This test has to run on the main thread.";
87 
88   StackRange stackrange = GetMainThreadStackRange();
89   ASSERT_NE(stackrange.begin, nullptr);
90   ASSERT_NE(stackrange.end, nullptr);
91   // The implementation assumes the stack grows from higher addresses to
92   // lower. We will need to rework once we encounter architectures where the
93   // stack grows the other way.
94   EXPECT_LT(stackrange.begin, __builtin_frame_address(0));
95   EXPECT_GT(stackrange.end, __builtin_frame_address(0));
96 }
97 
TEST(ClientTest,IsMainThread)98 TEST(ClientTest, IsMainThread) {
99   // Our code relies on the fact that getpid() == GetThreadId() if this
100   // process/thread is the main thread of the process. This test ensures that is
101   // true.
102   auto pid = getpid();
103   auto main_thread_id = base::GetThreadId();
104   EXPECT_EQ(pid, main_thread_id);
105   std::thread th(
106       [main_thread_id] { EXPECT_NE(main_thread_id, base::GetThreadId()); });
107   th.join();
108 }
109 
TEST(ClientTest,GetMaxTriesBlock)110 TEST(ClientTest, GetMaxTriesBlock) {
111   ClientConfiguration cfg = {};
112   cfg.block_client = true;
113   cfg.block_client_timeout_us = 200;
114   EXPECT_EQ(GetMaxTries(cfg), 2u);
115 }
116 
TEST(ClientTest,GetMaxTriesBlockSmall)117 TEST(ClientTest, GetMaxTriesBlockSmall) {
118   ClientConfiguration cfg = {};
119   cfg.block_client = true;
120   cfg.block_client_timeout_us = 99;
121   EXPECT_EQ(GetMaxTries(cfg), 1u);
122 }
123 
TEST(ClientTest,GetMaxTriesBlockVerySmall)124 TEST(ClientTest, GetMaxTriesBlockVerySmall) {
125   ClientConfiguration cfg = {};
126   cfg.block_client = true;
127   cfg.block_client_timeout_us = 1;
128   EXPECT_EQ(GetMaxTries(cfg), 1u);
129 }
130 
TEST(ClientTest,GetMaxTriesBlockInfinite)131 TEST(ClientTest, GetMaxTriesBlockInfinite) {
132   ClientConfiguration cfg = {};
133   cfg.block_client = true;
134   cfg.block_client_timeout_us = 0;
135   EXPECT_EQ(GetMaxTries(cfg), kInfiniteTries);
136 }
137 
TEST(ClientTest,GetMaxTriesNoBlock)138 TEST(ClientTest, GetMaxTriesNoBlock) {
139   ClientConfiguration cfg = {};
140   cfg.block_client = false;
141   cfg.block_client_timeout_us = 200;
142   EXPECT_EQ(GetMaxTries(cfg), 1u);
143 }
144 
145 }  // namespace
146 }  // namespace profiling
147 }  // namespace perfetto
148