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; |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 71 | 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 | |
| 76 | static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) { |
| 77 | return o << static_cast<std::underlying_type_t<BinderCallType>>(s); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | class HidlServer : public IHidlStuff { |
| 81 | public: |
| 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 87 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 88 | 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 93 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 94 | 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 101 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 102 | return android::hardware::Status::ok(); |
| 103 | } |
| 104 | }; |
| 105 | class AidlServer : public BnAidlStuff { |
| 106 | public: |
| 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 112 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 113 | 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 118 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 119 | 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 126 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 127 | return Status::ok(); |
| 128 | } |
| 129 | }; |
| 130 | |
| 131 | TEST(BinderThreadState, LocalHidlCall) { |
| 132 | sp<IHidlStuff> server = new HidlServer(0, 0); |
| 133 | EXPECT_TRUE(server->callLocal().isOk()); |
| 134 | } |
| 135 | |
| 136 | TEST(BinderThreadState, LocalAidlCall) { |
| 137 | sp<IAidlStuff> server = new AidlServer(0, 0); |
| 138 | EXPECT_TRUE(server->callLocal().isOk()); |
| 139 | } |
| 140 | |
| 141 | TEST(BindThreadState, RemoteHidlCall) { |
| 142 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 143 | ASSERT_NE(nullptr, stuff); |
| 144 | ASSERT_TRUE(stuff->call(0).isOk()); |
| 145 | } |
| 146 | TEST(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 | |
| 153 | TEST(BindThreadState, RemoteNestedStartHidlCall) { |
| 154 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 155 | ASSERT_NE(nullptr, stuff); |
| 156 | ASSERT_TRUE(stuff->call(100).isOk()); |
| 157 | } |
| 158 | TEST(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 | |
| 165 | int server(size_t thisId, size_t otherId) { |
| 166 | // AIDL |
| 167 | android::ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 168 | sp<AidlServer> aidlServer = new AidlServer(thisId, otherId); |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 169 | CHECK_EQ(OK, |
| 170 | defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer)); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 171 | android::ProcessState::self()->startThreadPool(); |
| 172 | |
| 173 | // HIDL |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 174 | android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/); |
| 175 | sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId); |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame^] | 176 | CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str())); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 177 | android::hardware::joinRpcThreadpool(); |
| 178 | |
| 179 | return EXIT_FAILURE; |
| 180 | } |
| 181 | |
| 182 | int main(int argc, char** argv) { |
| 183 | ::testing::InitGoogleTest(&argc, argv); |
Steven Moreland | 85a9d32 | 2020-07-08 18:43:08 +0000 | [diff] [blame] | 184 | android::hardware::details::setTrebleTestingOverride(true); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 185 | 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 | } |