blob: e888b0aea8c6ee2a0b3fad9e9f0202f093b7723c [file] [log] [blame]
Steven Moreland39d887d2020-01-31 14:56:45 -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 <BnAidlStuff.h>
18#include <android-base/logging.h>
Steven Morelandd62b89b2023-03-22 21:05:13 +000019#include <binder/IPCThreadState.h>
Steven Moreland39d887d2020-01-31 14:56:45 -080020#include <binder/IServiceManager.h>
21#include <binderthreadstate/CallerUtils.h>
22#include <binderthreadstateutilstest/1.0/IHidlStuff.h>
23#include <gtest/gtest.h>
24#include <hidl/HidlTransportSupport.h>
Devin Moore2c688c52024-03-28 20:12:46 +000025#include <hidl/ServiceManagement.h>
Steven Morelandd62b89b2023-03-22 21:05:13 +000026#include <hwbinder/IPCThreadState.h>
27
28#include <thread>
29
Steven Moreland39d887d2020-01-31 14:56:45 -080030#include <linux/prctl.h>
31#include <sys/prctl.h>
32
33using android::BinderCallType;
34using android::defaultServiceManager;
35using android::getCurrentServingCall;
36using android::getService;
37using android::OK;
38using android::sp;
39using android::String16;
40using android::binder::Status;
Devin Moore2c688c52024-03-28 20:12:46 +000041using android::hardware::isHidlSupported;
Steven Moreland39d887d2020-01-31 14:56:45 -080042using android::hardware::Return;
43using binderthreadstateutilstest::V1_0::IHidlStuff;
44
45constexpr size_t kP1Id = 1;
46constexpr size_t kP2Id = 2;
47
48// AIDL and HIDL are in separate namespaces so using same service names
49std::string id2name(size_t id) {
50 return "libbinderthreadstateutils-" + std::to_string(id);
51}
52
53// There are two servers calling each other recursively like this.
54//
55// P1 P2
56// | --HIDL--> |
57// | <--HIDL-- |
58// | --AIDL--> |
59// | <--AIDL-- |
60// | --HIDL--> |
61// | <--HIDL-- |
62// | --AIDL--> |
63// | <--AIDL-- |
64// ..........
65//
66// Calls always come in pairs (AIDL returns AIDL, HIDL returns HIDL) because
67// this means that P1 always has a 'waitForResponse' call which can service the
68// returning call and continue the recursion. Of course, with more threads, more
69// complicated calls are possible, but this should do here.
70
71static void callHidl(size_t id, int32_t idx) {
Devin Moore2c688c52024-03-28 20:12:46 +000072 CHECK_EQ(true, isHidlSupported()) << "We shouldn't be calling HIDL if it's not supported";
Steven Moreland39d887d2020-01-31 14:56:45 -080073 auto stuff = IHidlStuff::getService(id2name(id));
74 CHECK(stuff->call(idx).isOk());
75}
76
77static void callAidl(size_t id, int32_t idx) {
78 sp<IAidlStuff> stuff;
Steven Morelandf769d822022-08-25 00:24:28 +000079 CHECK_EQ(OK, android::getService<IAidlStuff>(String16(id2name(id).c_str()), &stuff));
80 auto ret = stuff->call(idx);
81 CHECK(ret.isOk()) << ret;
82}
83
Steven Moreland3573b172022-09-24 02:29:13 +000084static std::string getStackPointerDebugInfo() {
85 const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer();
86 const void* binderSp = android::IPCThreadState::self()->getServingStackPointer();
87
88 std::stringstream ss;
89 ss << "(hwbinder sp: " << hwbinderSp << " binder sp: " << binderSp << ")";
90 return ss.str();
91}
92
Steven Morelandf769d822022-08-25 00:24:28 +000093static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) {
94 return o << static_cast<std::underlying_type_t<BinderCallType>>(s);
Steven Moreland39d887d2020-01-31 14:56:45 -080095}
96
97class HidlServer : public IHidlStuff {
98public:
99 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
100 size_t thisId;
101 size_t otherId;
102
103 Return<void> callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000104 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800105 return android::hardware::Status::ok();
106 }
107 Return<void> call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000108 bool doCallHidl = thisId == kP1Id && idx % 4 < 2;
109
Steven Moreland39d887d2020-01-31 14:56:45 -0800110 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000111 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
112 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
113 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800114 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000115 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800116 callHidl(otherId, idx - 1);
117 } else {
118 callAidl(otherId, idx - 1);
119 }
120 }
Steven Moreland3573b172022-09-24 02:29:13 +0000121 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
122 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800123 return android::hardware::Status::ok();
124 }
125};
126class AidlServer : public BnAidlStuff {
127public:
128 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
129 size_t thisId;
130 size_t otherId;
131
132 Status callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000133 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800134 return Status::ok();
135 }
136 Status call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000137 bool doCallHidl = thisId == kP2Id && idx % 4 < 2;
Steven Moreland39d887d2020-01-31 14:56:45 -0800138 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000139 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
140 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
141 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800142 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000143 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800144 callHidl(otherId, idx - 1);
145 } else {
146 callAidl(otherId, idx - 1);
147 }
148 }
Steven Moreland3573b172022-09-24 02:29:13 +0000149 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
150 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800151 return Status::ok();
152 }
153};
154
155TEST(BinderThreadState, LocalHidlCall) {
156 sp<IHidlStuff> server = new HidlServer(0, 0);
157 EXPECT_TRUE(server->callLocal().isOk());
158}
159
160TEST(BinderThreadState, LocalAidlCall) {
161 sp<IAidlStuff> server = new AidlServer(0, 0);
162 EXPECT_TRUE(server->callLocal().isOk());
163}
164
Steven Morelandd62b89b2023-03-22 21:05:13 +0000165TEST(BinderThreadState, DoesntInitializeBinderDriver) {
166 // this is on another thread, because it's testing thread-specific
167 // state and we expect it not to be initialized.
168 std::thread([&] {
169 EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull());
170 EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull());
171
172 (void)getCurrentServingCall();
173
174 EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull());
175 EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull());
176 }).join();
177}
178
Steven Moreland39d887d2020-01-31 14:56:45 -0800179TEST(BindThreadState, RemoteHidlCall) {
Devin Moore2c688c52024-03-28 20:12:46 +0000180 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
Steven Moreland39d887d2020-01-31 14:56:45 -0800181 auto stuff = IHidlStuff::getService(id2name(kP1Id));
182 ASSERT_NE(nullptr, stuff);
183 ASSERT_TRUE(stuff->call(0).isOk());
184}
185TEST(BindThreadState, RemoteAidlCall) {
186 sp<IAidlStuff> stuff;
187 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
188 ASSERT_NE(nullptr, stuff);
189 ASSERT_TRUE(stuff->call(0).isOk());
190}
191
192TEST(BindThreadState, RemoteNestedStartHidlCall) {
Devin Moore2c688c52024-03-28 20:12:46 +0000193 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
Steven Moreland39d887d2020-01-31 14:56:45 -0800194 auto stuff = IHidlStuff::getService(id2name(kP1Id));
195 ASSERT_NE(nullptr, stuff);
196 ASSERT_TRUE(stuff->call(100).isOk());
197}
198TEST(BindThreadState, RemoteNestedStartAidlCall) {
Devin Moore2c688c52024-03-28 20:12:46 +0000199 // this test case is trying ot nest a HIDL call which requires HIDL support
200 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
Steven Moreland39d887d2020-01-31 14:56:45 -0800201 sp<IAidlStuff> stuff;
202 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
203 ASSERT_NE(nullptr, stuff);
204 EXPECT_TRUE(stuff->call(100).isOk());
205}
206
207int server(size_t thisId, size_t otherId) {
208 // AIDL
209 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
210 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000211 CHECK_EQ(OK,
212 defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
Steven Moreland39d887d2020-01-31 14:56:45 -0800213 android::ProcessState::self()->startThreadPool();
214
Devin Moore2c688c52024-03-28 20:12:46 +0000215 if (isHidlSupported()) {
216 // HIDL
217 android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/);
218 sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId);
219 CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str()));
220 android::hardware::joinRpcThreadpool();
221 } else {
222 android::IPCThreadState::self()->joinThreadPool(true);
223 }
Steven Moreland39d887d2020-01-31 14:56:45 -0800224
225 return EXIT_FAILURE;
226}
227
228int main(int argc, char** argv) {
229 ::testing::InitGoogleTest(&argc, argv);
Steven Moreland85a9d322020-07-08 18:43:08 +0000230 android::hardware::details::setTrebleTestingOverride(true);
Steven Moreland39d887d2020-01-31 14:56:45 -0800231 if (fork() == 0) {
232 prctl(PR_SET_PDEATHSIG, SIGHUP);
233 return server(kP1Id, kP2Id);
234 }
235 if (fork() == 0) {
236 prctl(PR_SET_PDEATHSIG, SIGHUP);
237 return server(kP2Id, kP1Id);
238 }
239
240 android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str()));
Devin Moore2c688c52024-03-28 20:12:46 +0000241 if (isHidlSupported()) {
242 android::hardware::details::waitForHwService(IHidlStuff::descriptor,
243 id2name(kP1Id).c_str());
244 }
Steven Moreland39d887d2020-01-31 14:56:45 -0800245 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
Devin Moore2c688c52024-03-28 20:12:46 +0000246 if (isHidlSupported()) {
247 android::hardware::details::waitForHwService(IHidlStuff::descriptor,
248 id2name(kP2Id).c_str());
249 }
Steven Moreland39d887d2020-01-31 14:56:45 -0800250
251 return RUN_ALL_TESTS();
252}