| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 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/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 |  | 
|  | 27 | using android::BinderCallType; | 
|  | 28 | using android::defaultServiceManager; | 
|  | 29 | using android::getCurrentServingCall; | 
|  | 30 | using android::getService; | 
|  | 31 | using android::OK; | 
|  | 32 | using android::sp; | 
|  | 33 | using android::String16; | 
|  | 34 | using android::binder::Status; | 
|  | 35 | using android::hardware::Return; | 
|  | 36 | using binderthreadstateutilstest::V1_0::IHidlStuff; | 
|  | 37 |  | 
|  | 38 | constexpr size_t kP1Id = 1; | 
|  | 39 | constexpr size_t kP2Id = 2; | 
|  | 40 |  | 
|  | 41 | // AIDL and HIDL are in separate namespaces so using same service names | 
|  | 42 | std::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 |  | 
|  | 64 | static void callHidl(size_t id, int32_t idx) { | 
|  | 65 | auto stuff = IHidlStuff::getService(id2name(id)); | 
|  | 66 | CHECK(stuff->call(idx).isOk()); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | static 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 |  | 
|  | 75 | class HidlServer : public IHidlStuff { | 
|  | 76 | public: | 
|  | 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 | }; | 
|  | 100 | class AidlServer : public BnAidlStuff { | 
|  | 101 | public: | 
|  | 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 |  | 
|  | 126 | TEST(BinderThreadState, LocalHidlCall) { | 
|  | 127 | sp<IHidlStuff> server = new HidlServer(0, 0); | 
|  | 128 | EXPECT_TRUE(server->callLocal().isOk()); | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | TEST(BinderThreadState, LocalAidlCall) { | 
|  | 132 | sp<IAidlStuff> server = new AidlServer(0, 0); | 
|  | 133 | EXPECT_TRUE(server->callLocal().isOk()); | 
|  | 134 | } | 
|  | 135 |  | 
|  | 136 | TEST(BindThreadState, RemoteHidlCall) { | 
|  | 137 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); | 
|  | 138 | ASSERT_NE(nullptr, stuff); | 
|  | 139 | ASSERT_TRUE(stuff->call(0).isOk()); | 
|  | 140 | } | 
|  | 141 | TEST(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 |  | 
|  | 148 | TEST(BindThreadState, RemoteNestedStartHidlCall) { | 
|  | 149 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); | 
|  | 150 | ASSERT_NE(nullptr, stuff); | 
|  | 151 | ASSERT_TRUE(stuff->call(100).isOk()); | 
|  | 152 | } | 
|  | 153 | TEST(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 |  | 
|  | 160 | int 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 | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 168 | android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/); | 
|  | 169 | sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId); | 
|  | 170 | CHECK(OK == hidlServer->registerAsService(id2name(thisId).c_str())); | 
|  | 171 | android::hardware::joinRpcThreadpool(); | 
|  | 172 |  | 
|  | 173 | return EXIT_FAILURE; | 
|  | 174 | } | 
|  | 175 |  | 
|  | 176 | int main(int argc, char** argv) { | 
|  | 177 | ::testing::InitGoogleTest(&argc, argv); | 
| Steven Moreland | 85a9d32 | 2020-07-08 18:43:08 +0000 | [diff] [blame] | 178 | android::hardware::details::setTrebleTestingOverride(true); | 
| Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 179 | if (fork() == 0) { | 
|  | 180 | prctl(PR_SET_PDEATHSIG, SIGHUP); | 
|  | 181 | return server(kP1Id, kP2Id); | 
|  | 182 | } | 
|  | 183 | if (fork() == 0) { | 
|  | 184 | prctl(PR_SET_PDEATHSIG, SIGHUP); | 
|  | 185 | return server(kP2Id, kP1Id); | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str())); | 
|  | 189 | android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP1Id).c_str()); | 
|  | 190 | android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str())); | 
|  | 191 | android::hardware::details::waitForHwService(IHidlStuff::descriptor, id2name(kP2Id).c_str()); | 
|  | 192 |  | 
|  | 193 | return RUN_ALL_TESTS(); | 
|  | 194 | } |