blob: df1f35d9a79d60c217f80d58d61496ba13e0895e [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>
19#include <binder/IServiceManager.h>
20#include <binderthreadstate/CallerUtils.h>
21#include <binderthreadstateutilstest/1.0/IHidlStuff.h>
22#include <gtest/gtest.h>
23#include <hidl/HidlTransportSupport.h>
24#include <linux/prctl.h>
25#include <sys/prctl.h>
26
27using android::BinderCallType;
28using android::defaultServiceManager;
29using android::getCurrentServingCall;
30using android::getService;
31using android::OK;
32using android::sp;
33using android::String16;
34using android::binder::Status;
35using android::hardware::Return;
36using binderthreadstateutilstest::V1_0::IHidlStuff;
37
38constexpr size_t kP1Id = 1;
39constexpr size_t kP2Id = 2;
40
41// AIDL and HIDL are in separate namespaces so using same service names
42std::string id2name(size_t id) {
43 return "libbinderthreadstateutils-" + std::to_string(id);
44}
45
46// There are two servers calling each other recursively like this.
47//
48// P1 P2
49// | --HIDL--> |
50// | <--HIDL-- |
51// | --AIDL--> |
52// | <--AIDL-- |
53// | --HIDL--> |
54// | <--HIDL-- |
55// | --AIDL--> |
56// | <--AIDL-- |
57// ..........
58//
59// Calls always come in pairs (AIDL returns AIDL, HIDL returns HIDL) because
60// this means that P1 always has a 'waitForResponse' call which can service the
61// returning call and continue the recursion. Of course, with more threads, more
62// complicated calls are possible, but this should do here.
63
64static void callHidl(size_t id, int32_t idx) {
65 auto stuff = IHidlStuff::getService(id2name(id));
66 CHECK(stuff->call(idx).isOk());
67}
68
69static void callAidl(size_t id, int32_t idx) {
70 sp<IAidlStuff> stuff;
Steven Morelandf769d822022-08-25 00:24:28 +000071 CHECK_EQ(OK, android::getService<IAidlStuff>(String16(id2name(id).c_str()), &stuff));
72 auto ret = stuff->call(idx);
73 CHECK(ret.isOk()) << ret;
74}
75
Steven Moreland3573b172022-09-24 02:29:13 +000076static std::string getStackPointerDebugInfo() {
77 const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer();
78 const void* binderSp = android::IPCThreadState::self()->getServingStackPointer();
79
80 std::stringstream ss;
81 ss << "(hwbinder sp: " << hwbinderSp << " binder sp: " << binderSp << ")";
82 return ss.str();
83}
84
Steven Morelandf769d822022-08-25 00:24:28 +000085static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) {
86 return o << static_cast<std::underlying_type_t<BinderCallType>>(s);
Steven Moreland39d887d2020-01-31 14:56:45 -080087}
88
89class HidlServer : public IHidlStuff {
90public:
91 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
92 size_t thisId;
93 size_t otherId;
94
95 Return<void> callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +000096 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -080097 return android::hardware::Status::ok();
98 }
99 Return<void> call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000100 bool doCallHidl = thisId == kP1Id && idx % 4 < 2;
101
Steven Moreland39d887d2020-01-31 14:56:45 -0800102 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000103 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
104 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
105 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800106 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000107 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800108 callHidl(otherId, idx - 1);
109 } else {
110 callAidl(otherId, idx - 1);
111 }
112 }
Steven Moreland3573b172022-09-24 02:29:13 +0000113 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
114 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800115 return android::hardware::Status::ok();
116 }
117};
118class AidlServer : public BnAidlStuff {
119public:
120 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
121 size_t thisId;
122 size_t otherId;
123
124 Status callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000125 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800126 return Status::ok();
127 }
128 Status call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000129 bool doCallHidl = thisId == kP2Id && idx % 4 < 2;
Steven Moreland39d887d2020-01-31 14:56:45 -0800130 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000131 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
132 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
133 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800134 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000135 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800136 callHidl(otherId, idx - 1);
137 } else {
138 callAidl(otherId, idx - 1);
139 }
140 }
Steven Moreland3573b172022-09-24 02:29:13 +0000141 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
142 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800143 return Status::ok();
144 }
145};
146
147TEST(BinderThreadState, LocalHidlCall) {
148 sp<IHidlStuff> server = new HidlServer(0, 0);
149 EXPECT_TRUE(server->callLocal().isOk());
150}
151
152TEST(BinderThreadState, LocalAidlCall) {
153 sp<IAidlStuff> server = new AidlServer(0, 0);
154 EXPECT_TRUE(server->callLocal().isOk());
155}
156
157TEST(BindThreadState, RemoteHidlCall) {
158 auto stuff = IHidlStuff::getService(id2name(kP1Id));
159 ASSERT_NE(nullptr, stuff);
160 ASSERT_TRUE(stuff->call(0).isOk());
161}
162TEST(BindThreadState, RemoteAidlCall) {
163 sp<IAidlStuff> stuff;
164 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
165 ASSERT_NE(nullptr, stuff);
166 ASSERT_TRUE(stuff->call(0).isOk());
167}
168
169TEST(BindThreadState, RemoteNestedStartHidlCall) {
170 auto stuff = IHidlStuff::getService(id2name(kP1Id));
171 ASSERT_NE(nullptr, stuff);
172 ASSERT_TRUE(stuff->call(100).isOk());
173}
174TEST(BindThreadState, RemoteNestedStartAidlCall) {
175 sp<IAidlStuff> stuff;
176 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
177 ASSERT_NE(nullptr, stuff);
178 EXPECT_TRUE(stuff->call(100).isOk());
179}
180
181int server(size_t thisId, size_t otherId) {
182 // AIDL
183 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
184 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000185 CHECK_EQ(OK,
186 defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
Steven Moreland39d887d2020-01-31 14:56:45 -0800187 android::ProcessState::self()->startThreadPool();
188
189 // HIDL
Steven Moreland39d887d2020-01-31 14:56:45 -0800190 android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/);
191 sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000192 CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str()));
Steven Moreland39d887d2020-01-31 14:56:45 -0800193 android::hardware::joinRpcThreadpool();
194
195 return EXIT_FAILURE;
196}
197
198int main(int argc, char** argv) {
199 ::testing::InitGoogleTest(&argc, argv);
Steven Moreland85a9d322020-07-08 18:43:08 +0000200 android::hardware::details::setTrebleTestingOverride(true);
Steven Moreland39d887d2020-01-31 14:56:45 -0800201 if (fork() == 0) {
202 prctl(PR_SET_PDEATHSIG, SIGHUP);
203 return server(kP1Id, kP2Id);
204 }
205 if (fork() == 0) {
206 prctl(PR_SET_PDEATHSIG, SIGHUP);
207 return server(kP2Id, kP1Id);
208 }
209
210 android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str()));
211 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP1Id).c_str());
212 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
213 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP2Id).c_str());
214
215 return RUN_ALL_TESTS();
216}