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> |
Steven Moreland | d62b89b | 2023-03-22 21:05:13 +0000 | [diff] [blame^] | 19 | #include <binder/IPCThreadState.h> |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 20 | #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 Moreland | d62b89b | 2023-03-22 21:05:13 +0000 | [diff] [blame^] | 25 | #include <hwbinder/IPCThreadState.h> |
| 26 | |
| 27 | #include <thread> |
| 28 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 29 | #include <linux/prctl.h> |
| 30 | #include <sys/prctl.h> |
| 31 | |
| 32 | using android::BinderCallType; |
| 33 | using android::defaultServiceManager; |
| 34 | using android::getCurrentServingCall; |
| 35 | using android::getService; |
| 36 | using android::OK; |
| 37 | using android::sp; |
| 38 | using android::String16; |
| 39 | using android::binder::Status; |
| 40 | using android::hardware::Return; |
| 41 | using binderthreadstateutilstest::V1_0::IHidlStuff; |
| 42 | |
| 43 | constexpr size_t kP1Id = 1; |
| 44 | constexpr size_t kP2Id = 2; |
| 45 | |
| 46 | // AIDL and HIDL are in separate namespaces so using same service names |
| 47 | std::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 | |
| 69 | static void callHidl(size_t id, int32_t idx) { |
| 70 | auto stuff = IHidlStuff::getService(id2name(id)); |
| 71 | CHECK(stuff->call(idx).isOk()); |
| 72 | } |
| 73 | |
| 74 | static void callAidl(size_t id, int32_t idx) { |
| 75 | sp<IAidlStuff> stuff; |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 76 | 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 Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 81 | static 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 90 | static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) { |
| 91 | return o << static_cast<std::underlying_type_t<BinderCallType>>(s); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | class HidlServer : public IHidlStuff { |
| 95 | public: |
| 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 101 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 102 | return android::hardware::Status::ok(); |
| 103 | } |
| 104 | Return<void> call(int32_t idx) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 105 | bool doCallHidl = thisId == kP1Id && idx % 4 < 2; |
| 106 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 107 | LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 108 | << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL"); |
| 109 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()) |
| 110 | << " before call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 111 | if (idx > 0) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 112 | if (doCallHidl) { |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 113 | callHidl(otherId, idx - 1); |
| 114 | } else { |
| 115 | callAidl(otherId, idx - 1); |
| 116 | } |
| 117 | } |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 118 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()) |
| 119 | << " after call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 120 | return android::hardware::Status::ok(); |
| 121 | } |
| 122 | }; |
| 123 | class AidlServer : public BnAidlStuff { |
| 124 | public: |
| 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 Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 130 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 131 | return Status::ok(); |
| 132 | } |
| 133 | Status call(int32_t idx) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 134 | bool doCallHidl = thisId == kP2Id && idx % 4 < 2; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 135 | LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 136 | << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL"); |
| 137 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()) |
| 138 | << " before call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 139 | if (idx > 0) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 140 | if (doCallHidl) { |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 141 | callHidl(otherId, idx - 1); |
| 142 | } else { |
| 143 | callAidl(otherId, idx - 1); |
| 144 | } |
| 145 | } |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 146 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()) |
| 147 | << " after call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 148 | return Status::ok(); |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | TEST(BinderThreadState, LocalHidlCall) { |
| 153 | sp<IHidlStuff> server = new HidlServer(0, 0); |
| 154 | EXPECT_TRUE(server->callLocal().isOk()); |
| 155 | } |
| 156 | |
| 157 | TEST(BinderThreadState, LocalAidlCall) { |
| 158 | sp<IAidlStuff> server = new AidlServer(0, 0); |
| 159 | EXPECT_TRUE(server->callLocal().isOk()); |
| 160 | } |
| 161 | |
Steven Moreland | d62b89b | 2023-03-22 21:05:13 +0000 | [diff] [blame^] | 162 | TEST(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 Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 176 | TEST(BindThreadState, RemoteHidlCall) { |
| 177 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 178 | ASSERT_NE(nullptr, stuff); |
| 179 | ASSERT_TRUE(stuff->call(0).isOk()); |
| 180 | } |
| 181 | TEST(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 | |
| 188 | TEST(BindThreadState, RemoteNestedStartHidlCall) { |
| 189 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 190 | ASSERT_NE(nullptr, stuff); |
| 191 | ASSERT_TRUE(stuff->call(100).isOk()); |
| 192 | } |
| 193 | TEST(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 | |
| 200 | int server(size_t thisId, size_t otherId) { |
| 201 | // AIDL |
| 202 | android::ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 203 | sp<AidlServer> aidlServer = new AidlServer(thisId, otherId); |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 204 | CHECK_EQ(OK, |
| 205 | defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer)); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 206 | android::ProcessState::self()->startThreadPool(); |
| 207 | |
| 208 | // HIDL |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 209 | android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/); |
| 210 | sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId); |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 211 | CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str())); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 212 | android::hardware::joinRpcThreadpool(); |
| 213 | |
| 214 | return EXIT_FAILURE; |
| 215 | } |
| 216 | |
| 217 | int main(int argc, char** argv) { |
| 218 | ::testing::InitGoogleTest(&argc, argv); |
Steven Moreland | 85a9d32 | 2020-07-08 18:43:08 +0000 | [diff] [blame] | 219 | android::hardware::details::setTrebleTestingOverride(true); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 220 | 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 | } |