blob: 2f731377f0706c8df3a036b6287e16d7dbffa2e6 [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
76static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) {
77 return o << static_cast<std::underlying_type_t<BinderCallType>>(s);
Steven Moreland39d887d2020-01-31 14:56:45 -080078}
79
80class HidlServer : public IHidlStuff {
81public:
82 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
83 size_t thisId;
84 size_t otherId;
85
86 Return<void> callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +000087 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -080088 return android::hardware::Status::ok();
89 }
90 Return<void> call(int32_t idx) {
91 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
92 << " with tid: " << gettid();
Steven Morelandf769d822022-08-25 00:24:28 +000093 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -080094 if (idx > 0) {
95 if (thisId == kP1Id && idx % 4 < 2) {
96 callHidl(otherId, idx - 1);
97 } else {
98 callAidl(otherId, idx - 1);
99 }
100 }
Steven Morelandf769d822022-08-25 00:24:28 +0000101 CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800102 return android::hardware::Status::ok();
103 }
104};
105class AidlServer : public BnAidlStuff {
106public:
107 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
108 size_t thisId;
109 size_t otherId;
110
111 Status callLocal() {
Steven Morelandf769d822022-08-25 00:24:28 +0000112 CHECK_EQ(BinderCallType::NONE, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800113 return Status::ok();
114 }
115 Status call(int32_t idx) {
116 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
117 << " with tid: " << gettid();
Steven Morelandf769d822022-08-25 00:24:28 +0000118 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800119 if (idx > 0) {
120 if (thisId == kP2Id && idx % 4 < 2) {
121 callHidl(otherId, idx - 1);
122 } else {
123 callAidl(otherId, idx - 1);
124 }
125 }
Steven Morelandf769d822022-08-25 00:24:28 +0000126 CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall());
Steven Moreland39d887d2020-01-31 14:56:45 -0800127 return Status::ok();
128 }
129};
130
131TEST(BinderThreadState, LocalHidlCall) {
132 sp<IHidlStuff> server = new HidlServer(0, 0);
133 EXPECT_TRUE(server->callLocal().isOk());
134}
135
136TEST(BinderThreadState, LocalAidlCall) {
137 sp<IAidlStuff> server = new AidlServer(0, 0);
138 EXPECT_TRUE(server->callLocal().isOk());
139}
140
141TEST(BindThreadState, RemoteHidlCall) {
142 auto stuff = IHidlStuff::getService(id2name(kP1Id));
143 ASSERT_NE(nullptr, stuff);
144 ASSERT_TRUE(stuff->call(0).isOk());
145}
146TEST(BindThreadState, RemoteAidlCall) {
147 sp<IAidlStuff> stuff;
148 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
149 ASSERT_NE(nullptr, stuff);
150 ASSERT_TRUE(stuff->call(0).isOk());
151}
152
153TEST(BindThreadState, RemoteNestedStartHidlCall) {
154 auto stuff = IHidlStuff::getService(id2name(kP1Id));
155 ASSERT_NE(nullptr, stuff);
156 ASSERT_TRUE(stuff->call(100).isOk());
157}
158TEST(BindThreadState, RemoteNestedStartAidlCall) {
159 sp<IAidlStuff> stuff;
160 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
161 ASSERT_NE(nullptr, stuff);
162 EXPECT_TRUE(stuff->call(100).isOk());
163}
164
165int server(size_t thisId, size_t otherId) {
166 // AIDL
167 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
168 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000169 CHECK_EQ(OK,
170 defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
Steven Moreland39d887d2020-01-31 14:56:45 -0800171 android::ProcessState::self()->startThreadPool();
172
173 // HIDL
Steven Moreland39d887d2020-01-31 14:56:45 -0800174 android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/);
175 sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId);
Steven Morelandf769d822022-08-25 00:24:28 +0000176 CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str()));
Steven Moreland39d887d2020-01-31 14:56:45 -0800177 android::hardware::joinRpcThreadpool();
178
179 return EXIT_FAILURE;
180}
181
182int main(int argc, char** argv) {
183 ::testing::InitGoogleTest(&argc, argv);
Steven Moreland85a9d322020-07-08 18:43:08 +0000184 android::hardware::details::setTrebleTestingOverride(true);
Steven Moreland39d887d2020-01-31 14:56:45 -0800185 if (fork() == 0) {
186 prctl(PR_SET_PDEATHSIG, SIGHUP);
187 return server(kP1Id, kP2Id);
188 }
189 if (fork() == 0) {
190 prctl(PR_SET_PDEATHSIG, SIGHUP);
191 return server(kP2Id, kP1Id);
192 }
193
194 android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str()));
195 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP1Id).c_str());
196 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
197 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP2Id).c_str());
198
199 return RUN_ALL_TESTS();
200}