1 /*
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/IPCThreadState.h>
20 #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>
25 #include <hidl/ServiceManagement.h>
26 #include <hwbinder/IPCThreadState.h>
27
28 #include <thread>
29
30 #include <linux/prctl.h>
31 #include <sys/prctl.h>
32
33 using android::BinderCallType;
34 using android::defaultServiceManager;
35 using android::getCurrentServingCall;
36 using android::getService;
37 using android::OK;
38 using android::sp;
39 using android::String16;
40 using android::binder::Status;
41 using android::hardware::isHidlSupported;
42 using android::hardware::Return;
43 using binderthreadstateutilstest::V1_0::IHidlStuff;
44
45 constexpr size_t kP1Id = 1;
46 constexpr size_t kP2Id = 2;
47
48 // AIDL and HIDL are in separate namespaces so using same service names
id2name(size_t id)49 std::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
callHidl(size_t id,int32_t idx)71 static void callHidl(size_t id, int32_t idx) {
72 CHECK_EQ(true, isHidlSupported()) << "We shouldn't be calling HIDL if it's not supported";
73 auto stuff = IHidlStuff::getService(id2name(id));
74 CHECK(stuff->call(idx).isOk());
75 }
76
callAidl(size_t id,int32_t idx)77 static void callAidl(size_t id, int32_t idx) {
78 sp<IAidlStuff> stuff;
79 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
getStackPointerDebugInfo()84 static 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
operator <<(std::ostream & o,const BinderCallType & s)93 static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) {
94 return o << static_cast<std::underlying_type_t<BinderCallType>>(s);
95 }
96
97 class HidlServer : public IHidlStuff {
98 public:
HidlServer(size_t thisId,size_t otherId)99 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
100 size_t thisId;
101 size_t otherId;
102
callLocal()103 Return<void> callLocal() {
104 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
105 return android::hardware::Status::ok();
106 }
call(int32_t idx)107 Return<void> call(int32_t idx) {
108 bool doCallHidl = thisId == kP1Id && idx % 4 < 2;
109
110 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
111 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
112 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
113 << " before call " << getStackPointerDebugInfo();
114 if (idx > 0) {
115 if (doCallHidl) {
116 callHidl(otherId, idx - 1);
117 } else {
118 callAidl(otherId, idx - 1);
119 }
120 }
121 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
122 << " after call " << getStackPointerDebugInfo();
123 return android::hardware::Status::ok();
124 }
125 };
126 class AidlServer : public BnAidlStuff {
127 public:
AidlServer(size_t thisId,size_t otherId)128 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
129 size_t thisId;
130 size_t otherId;
131
callLocal()132 Status callLocal() {
133 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
134 return Status::ok();
135 }
call(int32_t idx)136 Status call(int32_t idx) {
137 bool doCallHidl = thisId == kP2Id && idx % 4 < 2;
138 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
139 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
140 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
141 << " before call " << getStackPointerDebugInfo();
142 if (idx > 0) {
143 if (doCallHidl) {
144 callHidl(otherId, idx - 1);
145 } else {
146 callAidl(otherId, idx - 1);
147 }
148 }
149 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
150 << " after call " << getStackPointerDebugInfo();
151 return Status::ok();
152 }
153 };
154
TEST(BinderThreadState,LocalHidlCall)155 TEST(BinderThreadState, LocalHidlCall) {
156 sp<IHidlStuff> server = new HidlServer(0, 0);
157 EXPECT_TRUE(server->callLocal().isOk());
158 }
159
TEST(BinderThreadState,LocalAidlCall)160 TEST(BinderThreadState, LocalAidlCall) {
161 sp<IAidlStuff> server = new AidlServer(0, 0);
162 EXPECT_TRUE(server->callLocal().isOk());
163 }
164
TEST(BinderThreadState,DoesntInitializeBinderDriver)165 TEST(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
TEST(BindThreadState,RemoteHidlCall)179 TEST(BindThreadState, RemoteHidlCall) {
180 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
181 auto stuff = IHidlStuff::getService(id2name(kP1Id));
182 ASSERT_NE(nullptr, stuff);
183 ASSERT_TRUE(stuff->call(0).isOk());
184 }
TEST(BindThreadState,RemoteAidlCall)185 TEST(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
TEST(BindThreadState,RemoteNestedStartHidlCall)192 TEST(BindThreadState, RemoteNestedStartHidlCall) {
193 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
194 auto stuff = IHidlStuff::getService(id2name(kP1Id));
195 ASSERT_NE(nullptr, stuff);
196 ASSERT_TRUE(stuff->call(100).isOk());
197 }
TEST(BindThreadState,RemoteNestedStartAidlCall)198 TEST(BindThreadState, RemoteNestedStartAidlCall) {
199 // this test case is trying ot nest a HIDL call which requires HIDL support
200 if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device";
201 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
server(size_t thisId,size_t otherId)207 int server(size_t thisId, size_t otherId) {
208 // AIDL
209 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
210 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
211 CHECK_EQ(OK,
212 defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
213 android::ProcessState::self()->startThreadPool();
214
215 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 }
224
225 return EXIT_FAILURE;
226 }
227
main(int argc,char ** argv)228 int main(int argc, char** argv) {
229 ::testing::InitGoogleTest(&argc, argv);
230 android::hardware::details::setTrebleTestingOverride(true);
231 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()));
241 if (isHidlSupported()) {
242 android::hardware::details::waitForHwService(IHidlStuff::descriptor,
243 id2name(kP1Id).c_str());
244 }
245 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
246 if (isHidlSupported()) {
247 android::hardware::details::waitForHwService(IHidlStuff::descriptor,
248 id2name(kP2Id).c_str());
249 }
250
251 return RUN_ALL_TESTS();
252 }
253