blob: 68cc22505736c32ea72a71ef02bb8edf350af3a0 [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;
71 CHECK(OK == android::getService<IAidlStuff>(String16(id2name(id).c_str()), &stuff));
72 CHECK(stuff->call(idx).isOk());
73}
74
75class HidlServer : public IHidlStuff {
76public:
77 HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
78 size_t thisId;
79 size_t otherId;
80
81 Return<void> callLocal() {
82 CHECK(BinderCallType::NONE == getCurrentServingCall());
83 return android::hardware::Status::ok();
84 }
85 Return<void> call(int32_t idx) {
86 LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
87 << " with tid: " << gettid();
88 CHECK(BinderCallType::HWBINDER == getCurrentServingCall());
89 if (idx > 0) {
90 if (thisId == kP1Id && idx % 4 < 2) {
91 callHidl(otherId, idx - 1);
92 } else {
93 callAidl(otherId, idx - 1);
94 }
95 }
96 CHECK(BinderCallType::HWBINDER == getCurrentServingCall());
97 return android::hardware::Status::ok();
98 }
99};
100class AidlServer : public BnAidlStuff {
101public:
102 AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {}
103 size_t thisId;
104 size_t otherId;
105
106 Status callLocal() {
107 CHECK(BinderCallType::NONE == getCurrentServingCall());
108 return Status::ok();
109 }
110 Status call(int32_t idx) {
111 LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx
112 << " with tid: " << gettid();
113 CHECK(BinderCallType::BINDER == getCurrentServingCall());
114 if (idx > 0) {
115 if (thisId == kP2Id && idx % 4 < 2) {
116 callHidl(otherId, idx - 1);
117 } else {
118 callAidl(otherId, idx - 1);
119 }
120 }
121 CHECK(BinderCallType::BINDER == getCurrentServingCall());
122 return Status::ok();
123 }
124};
125
126TEST(BinderThreadState, LocalHidlCall) {
127 sp<IHidlStuff> server = new HidlServer(0, 0);
128 EXPECT_TRUE(server->callLocal().isOk());
129}
130
131TEST(BinderThreadState, LocalAidlCall) {
132 sp<IAidlStuff> server = new AidlServer(0, 0);
133 EXPECT_TRUE(server->callLocal().isOk());
134}
135
136TEST(BindThreadState, RemoteHidlCall) {
137 auto stuff = IHidlStuff::getService(id2name(kP1Id));
138 ASSERT_NE(nullptr, stuff);
139 ASSERT_TRUE(stuff->call(0).isOk());
140}
141TEST(BindThreadState, RemoteAidlCall) {
142 sp<IAidlStuff> stuff;
143 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
144 ASSERT_NE(nullptr, stuff);
145 ASSERT_TRUE(stuff->call(0).isOk());
146}
147
148TEST(BindThreadState, RemoteNestedStartHidlCall) {
149 auto stuff = IHidlStuff::getService(id2name(kP1Id));
150 ASSERT_NE(nullptr, stuff);
151 ASSERT_TRUE(stuff->call(100).isOk());
152}
153TEST(BindThreadState, RemoteNestedStartAidlCall) {
154 sp<IAidlStuff> stuff;
155 ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff));
156 ASSERT_NE(nullptr, stuff);
157 EXPECT_TRUE(stuff->call(100).isOk());
158}
159
160int server(size_t thisId, size_t otherId) {
161 // AIDL
162 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
163 sp<AidlServer> aidlServer = new AidlServer(thisId, otherId);
164 CHECK(OK == defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer));
165 android::ProcessState::self()->startThreadPool();
166
167 // HIDL
168 setenv("TREBLE_TESTING_OVERRIDE", "true", true);
169 android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/);
170 sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId);
171 CHECK(OK == hidlServer->registerAsService(id2name(thisId).c_str()));
172 android::hardware::joinRpcThreadpool();
173
174 return EXIT_FAILURE;
175}
176
177int main(int argc, char** argv) {
178 ::testing::InitGoogleTest(&argc, argv);
179 setenv("TREBLE_TESTING_OVERRIDE", "true", true);
180 if (fork() == 0) {
181 prctl(PR_SET_PDEATHSIG, SIGHUP);
182 return server(kP1Id, kP2Id);
183 }
184 if (fork() == 0) {
185 prctl(PR_SET_PDEATHSIG, SIGHUP);
186 return server(kP2Id, kP1Id);
187 }
188
189 android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str()));
190 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP1Id).c_str());
191 android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str()));
192 android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP2Id).c_str());
193
194 return RUN_ALL_TESTS();
195}