blob: b5c4010c7a387866bddf6f0c24b9787b0682e056 [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>
Steven Morelandd62b89b2023-03-22 21:05:13 +000025#include <hwbinder/IPCThreadState.h>
26
27#include <thread>
28
Steven Moreland39d887d2020-01-31 14:56:45 -080029#include <linux/prctl.h>
30#include <sys/prctl.h>
31
32using android::BinderCallType;
33using android::defaultServiceManager;
34using android::getCurrentServingCall;
35using android::getService;
36using android::OK;
37using android::sp;
38using android::String16;
39using android::binder::Status;
40using android::hardware::Return;
41using binderthreadstateutilstest::V1_0::IHidlStuff;
42
43constexpr size_t kP1Id = 1;
44constexpr size_t kP2Id = 2;
45
46// AIDL and HIDL are in separate namespaces so using same service names
47std::string id2name(size_t id) {
48 return "libbinderthreadstateutils-" + std::to_string(id);
49}
50
51// There are two servers calling each other recursively like this.
52//
53// P1 P2
54// | --HIDL--> |
55// | <--HIDL-- |
56// | --AIDL--> |
57// | <--AIDL-- |
58// | --HIDL--> |
59// | <--HIDL-- |
60// | --AIDL--> |
61// | <--AIDL-- |
62// ..........
63//
64// Calls always come in pairs (AIDL returns AIDL, HIDL returns HIDL) because
65// this means that P1 always has a 'waitForResponse' call which can service the
66// returning call and continue the recursion. Of course, with more threads, more
67// complicated calls are possible, but this should do here.
68
69static void callHidl(size_t id, int32_t idx) {
70 auto stuff = IHidlStuff::getService(id2name(id));
71 CHECK(stuff->call(idx).isOk());
72}
73
74static void callAidl(size_t id, int32_t idx) {
75 sp<IAidlStuff> stuff;
Steven Morelandf769d822022-08-25 00:24:28 +000076 CHECK_EQ(OK, android::getService<IAidlStuff>(String16(id2name(id).c_str()), &stuff));
77 auto ret = stuff->call(idx);
78 CHECK(ret.isOk()) << ret;
79}
80
Steven Moreland3573b172022-09-24 02:29:13 +000081static std::string getStackPointerDebugInfo() {
82 const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer();
83 const void* binderSp = android::IPCThreadState::self()->getServingStackPointer();
84
85 std::stringstream ss;
86 ss << "(hwbinder sp: " << hwbinderSp << " binder sp: " << binderSp << ")";
87 return ss.str();
88}
89
Steven Morelandf769d822022-08-25 00:24:28 +000090static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) {
91 return o << static_cast<std::underlying_type_t<BinderCallType>>(s);
Steven Moreland39d887d2020-01-31 14:56:45 -080092}
93
94class HidlServer : public IHidlStuff {
95public:
96 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
97 size_t thisId;
98 size_t otherId;
99
100 Return<void> callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000101 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800102 return android::hardware::Status::ok();
103 }
104 Return<void> call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000105 bool doCallHidl = thisId == kP1Id && idx % 4 < 2;
106
Steven Moreland39d887d2020-01-31 14:56:45 -0800107 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000108 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
109 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
110 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800111 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000112 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800113 callHidl(otherId, idx - 1);
114 } else {
115 callAidl(otherId, idx - 1);
116 }
117 }
Steven Moreland3573b172022-09-24 02:29:13 +0000118 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall())
119 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800120 return android::hardware::Status::ok();
121 }
122};
123class AidlServer : public BnAidlStuff {
124public:
125 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
126 size_t thisId;
127 size_t otherId;
128
129 Status callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000130 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800131 return Status::ok();
132 }
133 Status call(int32_t idx) {
Steven Moreland3573b172022-09-24 02:29:13 +0000134 bool doCallHidl = thisId == kP2Id && idx % 4 < 2;
Steven Moreland39d887d2020-01-31 14:56:45 -0800135 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
Steven Moreland3573b172022-09-24 02:29:13 +0000136 << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL");
137 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
138 << " before call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800139 if (idx > 0) {
Steven Moreland3573b172022-09-24 02:29:13 +0000140 if (doCallHidl) {
Steven Moreland39d887d2020-01-31 14:56:45 -0800141 callHidl(otherId, idx - 1);
142 } else {
143 callAidl(otherId, idx - 1);
144 }
145 }
Steven Moreland3573b172022-09-24 02:29:13 +0000146 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall())
147 << " after call " << getStackPointerDebugInfo();
Steven Moreland39d887d2020-01-31 14:56:45 -0800148 return Status::ok();
149 }
150};
151
152TEST(BinderThreadState, LocalHidlCall) {
153 sp<IHidlStuff> server = new HidlServer(0, 0);
154 EXPECT_TRUE(server->callLocal().isOk());
155}
156
157TEST(BinderThreadState, LocalAidlCall) {
158 sp<IAidlStuff> server = new AidlServer(0, 0);
159 EXPECT_TRUE(server->callLocal().isOk());
160}
161
Steven Morelandd62b89b2023-03-22 21:05:13 +0000162TEST(BinderThreadState, DoesntInitializeBinderDriver) {
163 // this is on another thread, because it's testing thread-specific
164 // state and we expect it not to be initialized.
165 std::thread([&] {
166 EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull());
167 EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull());
168
169 (void)getCurrentServingCall();
170
171 EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull());
172 EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull());
173 }).join();
174}
175
Steven Moreland39d887d2020-01-31 14:56:45 -0800176TEST(BindThreadState, RemoteHidlCall) {
177 auto stuff = IHidlStuff::getService(id2name(kP1Id));
178 ASSERT_NE(nullptr, stuff);
179 ASSERT_TRUE(stuff->call(0).isOk());
180}
181TEST(BindThreadState, RemoteAidlCall) {
182 sp<IAidlStuff> stuff;
183 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
184 ASSERT_NE(nullptr, stuff);
185 ASSERT_TRUE(stuff->call(0).isOk());
186}
187
188TEST(BindThreadState, RemoteNestedStartHidlCall) {
189 auto stuff = IHidlStuff::getService(id2name(kP1Id));
190 ASSERT_NE(nullptr, stuff);
191 ASSERT_TRUE(stuff->call(100).isOk());
192}
193TEST(BindThreadState, RemoteNestedStartAidlCall) {
194 sp<IAidlStuff> stuff;
195 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
196 ASSERT_NE(nullptr, stuff);
197 EXPECT_TRUE(stuff->call(100).isOk());
198}
199
200int server(size_t thisId, size_t otherId) {
201 // AIDL
202 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
203 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000204 CHECK_EQ(OK,
205 defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
Steven Moreland39d887d2020-01-31 14:56:45 -0800206 android::ProcessState::self()->startThreadPool();
207
208 // HIDL
Steven Moreland39d887d2020-01-31 14:56:45 -0800209 android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/);
210 sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000211 CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str()));
Steven Moreland39d887d2020-01-31 14:56:45 -0800212 android::hardware::joinRpcThreadpool();
213
214 return EXIT_FAILURE;
215}
216
217int main(int argc, char** argv) {
218 ::testing::InitGoogleTest(&argc, argv);
Steven Moreland85a9d322020-07-08 18:43:08 +0000219 android::hardware::details::setTrebleTestingOverride(true);
Steven Moreland39d887d2020-01-31 14:56:45 -0800220 if (fork() == 0) {
221 prctl(PR_SET_PDEATHSIG, SIGHUP);
222 return server(kP1Id, kP2Id);
223 }
224 if (fork() == 0) {
225 prctl(PR_SET_PDEATHSIG, SIGHUP);
226 return server(kP2Id, kP1Id);
227 }
228
229 android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str()));
230 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP1Id).c_str());
231 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
232 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP2Id).c_str());
233
234 return RUN_ALL_TESTS();
235}