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> |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 25 | #include <hidl/ServiceManagement.h> |
Steven Moreland | d62b89b | 2023-03-22 21:05:13 +0000 | [diff] [blame] | 26 | #include <hwbinder/IPCThreadState.h> |
| 27 | |
| 28 | #include <thread> |
| 29 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 30 | #include <linux/prctl.h> |
| 31 | #include <sys/prctl.h> |
| 32 | |
| 33 | using android::BinderCallType; |
| 34 | using android::defaultServiceManager; |
| 35 | using android::getCurrentServingCall; |
| 36 | using android::getService; |
| 37 | using android::OK; |
| 38 | using android::sp; |
| 39 | using android::String16; |
| 40 | using android::binder::Status; |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 41 | using android::hardware::isHidlSupported; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 42 | using android::hardware::Return; |
| 43 | using binderthreadstateutilstest::V1_0::IHidlStuff; |
| 44 | |
| 45 | constexpr size_t kP1Id = 1; |
| 46 | constexpr size_t kP2Id = 2; |
| 47 | |
| 48 | // AIDL and HIDL are in separate namespaces so using same service names |
| 49 | std::string id2name(size_t id) { |
| 50 | return "libbinderthreadstateutils-" + std::to_string(id); |
| 51 | } |
| 52 | |
| 53 | // There are two servers calling each other recursively like this. |
| 54 | // |
| 55 | // P1 P2 |
| 56 | // | --HIDL--> | |
| 57 | // | <--HIDL-- | |
| 58 | // | --AIDL--> | |
| 59 | // | <--AIDL-- | |
| 60 | // | --HIDL--> | |
| 61 | // | <--HIDL-- | |
| 62 | // | --AIDL--> | |
| 63 | // | <--AIDL-- | |
| 64 | // .......... |
| 65 | // |
| 66 | // Calls always come in pairs (AIDL returns AIDL, HIDL returns HIDL) because |
| 67 | // this means that P1 always has a 'waitForResponse' call which can service the |
| 68 | // returning call and continue the recursion. Of course, with more threads, more |
| 69 | // complicated calls are possible, but this should do here. |
| 70 | |
| 71 | static void callHidl(size_t id, int32_t idx) { |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 72 | CHECK_EQ(true, isHidlSupported()) << "We shouldn't be calling HIDL if it's not supported"; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 73 | auto stuff = IHidlStuff::getService(id2name(id)); |
| 74 | CHECK(stuff->call(idx).isOk()); |
| 75 | } |
| 76 | |
| 77 | static void callAidl(size_t id, int32_t idx) { |
| 78 | sp<IAidlStuff> stuff; |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 79 | CHECK_EQ(OK, android::getService<IAidlStuff>(String16(id2name(id).c_str()), &stuff)); |
| 80 | auto ret = stuff->call(idx); |
| 81 | CHECK(ret.isOk()) << ret; |
| 82 | } |
| 83 | |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 84 | static std::string getStackPointerDebugInfo() { |
| 85 | const void* hwbinderSp = android::hardware::IPCThreadState::self()->getServingStackPointer(); |
| 86 | const void* binderSp = android::IPCThreadState::self()->getServingStackPointer(); |
| 87 | |
| 88 | std::stringstream ss; |
| 89 | ss << "(hwbinder sp: " << hwbinderSp << " binder sp: " << binderSp << ")"; |
| 90 | return ss.str(); |
| 91 | } |
| 92 | |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 93 | static inline std::ostream& operator<<(std::ostream& o, const BinderCallType& s) { |
| 94 | return o << static_cast<std::underlying_type_t<BinderCallType>>(s); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | class HidlServer : public IHidlStuff { |
| 98 | public: |
| 99 | HidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {} |
| 100 | size_t thisId; |
| 101 | size_t otherId; |
| 102 | |
| 103 | Return<void> callLocal() { |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 104 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 105 | return android::hardware::Status::ok(); |
| 106 | } |
| 107 | Return<void> call(int32_t idx) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 108 | bool doCallHidl = thisId == kP1Id && idx % 4 < 2; |
| 109 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 110 | LOG(INFO) << "HidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 111 | << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL"); |
| 112 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()) |
| 113 | << " before call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 114 | if (idx > 0) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 115 | if (doCallHidl) { |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 116 | callHidl(otherId, idx - 1); |
| 117 | } else { |
| 118 | callAidl(otherId, idx - 1); |
| 119 | } |
| 120 | } |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 121 | CHECK_EQ(BinderCallType::HWBINDER, getCurrentServingCall()) |
| 122 | << " after call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 123 | return android::hardware::Status::ok(); |
| 124 | } |
| 125 | }; |
| 126 | class AidlServer : public BnAidlStuff { |
| 127 | public: |
| 128 | AidlServer(size_t thisId, size_t otherId) : thisId(thisId), otherId(otherId) {} |
| 129 | size_t thisId; |
| 130 | size_t otherId; |
| 131 | |
| 132 | Status callLocal() { |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 133 | CHECK_EQ(BinderCallType::NONE, getCurrentServingCall()); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 134 | return Status::ok(); |
| 135 | } |
| 136 | Status call(int32_t idx) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 137 | bool doCallHidl = thisId == kP2Id && idx % 4 < 2; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 138 | LOG(INFO) << "AidlServer CALL " << thisId << " to " << otherId << " at idx: " << idx |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 139 | << " with tid: " << gettid() << " calling " << (doCallHidl ? "HIDL" : "AIDL"); |
| 140 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()) |
| 141 | << " before call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 142 | if (idx > 0) { |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 143 | if (doCallHidl) { |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 144 | callHidl(otherId, idx - 1); |
| 145 | } else { |
| 146 | callAidl(otherId, idx - 1); |
| 147 | } |
| 148 | } |
Steven Moreland | 3573b17 | 2022-09-24 02:29:13 +0000 | [diff] [blame] | 149 | CHECK_EQ(BinderCallType::BINDER, getCurrentServingCall()) |
| 150 | << " after call " << getStackPointerDebugInfo(); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 151 | return Status::ok(); |
| 152 | } |
| 153 | }; |
| 154 | |
| 155 | TEST(BinderThreadState, LocalHidlCall) { |
| 156 | sp<IHidlStuff> server = new HidlServer(0, 0); |
| 157 | EXPECT_TRUE(server->callLocal().isOk()); |
| 158 | } |
| 159 | |
| 160 | TEST(BinderThreadState, LocalAidlCall) { |
| 161 | sp<IAidlStuff> server = new AidlServer(0, 0); |
| 162 | EXPECT_TRUE(server->callLocal().isOk()); |
| 163 | } |
| 164 | |
Steven Moreland | d62b89b | 2023-03-22 21:05:13 +0000 | [diff] [blame] | 165 | TEST(BinderThreadState, DoesntInitializeBinderDriver) { |
| 166 | // this is on another thread, because it's testing thread-specific |
| 167 | // state and we expect it not to be initialized. |
| 168 | std::thread([&] { |
| 169 | EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); |
| 170 | EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); |
| 171 | |
| 172 | (void)getCurrentServingCall(); |
| 173 | |
| 174 | EXPECT_EQ(nullptr, android::IPCThreadState::selfOrNull()); |
| 175 | EXPECT_EQ(nullptr, android::hardware::IPCThreadState::selfOrNull()); |
| 176 | }).join(); |
| 177 | } |
| 178 | |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 179 | TEST(BindThreadState, RemoteHidlCall) { |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 180 | if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device"; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 181 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 182 | ASSERT_NE(nullptr, stuff); |
| 183 | ASSERT_TRUE(stuff->call(0).isOk()); |
| 184 | } |
| 185 | TEST(BindThreadState, RemoteAidlCall) { |
| 186 | sp<IAidlStuff> stuff; |
| 187 | ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff)); |
| 188 | ASSERT_NE(nullptr, stuff); |
| 189 | ASSERT_TRUE(stuff->call(0).isOk()); |
| 190 | } |
| 191 | |
| 192 | TEST(BindThreadState, RemoteNestedStartHidlCall) { |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 193 | if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device"; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 194 | auto stuff = IHidlStuff::getService(id2name(kP1Id)); |
| 195 | ASSERT_NE(nullptr, stuff); |
| 196 | ASSERT_TRUE(stuff->call(100).isOk()); |
| 197 | } |
| 198 | TEST(BindThreadState, RemoteNestedStartAidlCall) { |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 199 | // this test case is trying ot nest a HIDL call which requires HIDL support |
| 200 | if (!isHidlSupported()) GTEST_SKIP() << "No HIDL support on device"; |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 201 | sp<IAidlStuff> stuff; |
| 202 | ASSERT_EQ(OK, android::getService<IAidlStuff>(String16(id2name(kP1Id).c_str()), &stuff)); |
| 203 | ASSERT_NE(nullptr, stuff); |
| 204 | EXPECT_TRUE(stuff->call(100).isOk()); |
| 205 | } |
| 206 | |
| 207 | int server(size_t thisId, size_t otherId) { |
| 208 | // AIDL |
| 209 | android::ProcessState::self()->setThreadPoolMaxThreadCount(1); |
| 210 | sp<AidlServer> aidlServer = new AidlServer(thisId, otherId); |
Steven Moreland | f769d82 | 2022-08-25 00:24:28 +0000 | [diff] [blame] | 211 | CHECK_EQ(OK, |
| 212 | defaultServiceManager()->addService(String16(id2name(thisId).c_str()), aidlServer)); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 213 | android::ProcessState::self()->startThreadPool(); |
| 214 | |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 215 | if (isHidlSupported()) { |
| 216 | // HIDL |
| 217 | android::hardware::configureRpcThreadpool(1, true /*callerWillJoin*/); |
| 218 | sp<IHidlStuff> hidlServer = new HidlServer(thisId, otherId); |
| 219 | CHECK_EQ(OK, hidlServer->registerAsService(id2name(thisId).c_str())); |
| 220 | android::hardware::joinRpcThreadpool(); |
| 221 | } else { |
| 222 | android::IPCThreadState::self()->joinThreadPool(true); |
| 223 | } |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 224 | |
| 225 | return EXIT_FAILURE; |
| 226 | } |
| 227 | |
| 228 | int main(int argc, char** argv) { |
| 229 | ::testing::InitGoogleTest(&argc, argv); |
Steven Moreland | 85a9d32 | 2020-07-08 18:43:08 +0000 | [diff] [blame] | 230 | android::hardware::details::setTrebleTestingOverride(true); |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 231 | if (fork() == 0) { |
| 232 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 233 | return server(kP1Id, kP2Id); |
| 234 | } |
| 235 | if (fork() == 0) { |
| 236 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 237 | return server(kP2Id, kP1Id); |
| 238 | } |
| 239 | |
| 240 | android::waitForService<IAidlStuff>(String16(id2name(kP1Id).c_str())); |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 241 | if (isHidlSupported()) { |
| 242 | android::hardware::details::waitForHwService(IHidlStuff::descriptor, |
| 243 | id2name(kP1Id).c_str()); |
| 244 | } |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 245 | android::waitForService<IAidlStuff>(String16(id2name(kP2Id).c_str())); |
Devin Moore | 2c688c5 | 2024-03-28 20:12:46 +0000 | [diff] [blame^] | 246 | if (isHidlSupported()) { |
| 247 | android::hardware::details::waitForHwService(IHidlStuff::descriptor, |
| 248 | id2name(kP2Id).c_str()); |
| 249 | } |
Steven Moreland | 39d887d | 2020-01-31 14:56:45 -0800 | [diff] [blame] | 250 | |
| 251 | return RUN_ALL_TESTS(); |
| 252 | } |