Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [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 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 17 | #include <BnBinderRpcSession.h> |
| 18 | #include <BnBinderRpcTest.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 19 | #include <aidl/IBinderRpcTest.h> |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 20 | #include <android-base/file.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 21 | #include <android-base/logging.h> |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 22 | #include <android/binder_auto_utils.h> |
| 23 | #include <android/binder_libbinder.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 24 | #include <binder/Binder.h> |
| 25 | #include <binder/BpBinder.h> |
| 26 | #include <binder/IServiceManager.h> |
| 27 | #include <binder/ProcessState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 28 | #include <binder/RpcServer.h> |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 29 | #include <binder/RpcSession.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 30 | #include <gtest/gtest.h> |
| 31 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 32 | #include <chrono> |
| 33 | #include <cstdlib> |
| 34 | #include <iostream> |
| 35 | #include <thread> |
| 36 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 37 | #include <sys/prctl.h> |
| 38 | #include <unistd.h> |
| 39 | |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 40 | #include "../RpcState.h" // for debugging |
| 41 | #include "../vm_sockets.h" // for VMADDR_* |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 42 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame^] | 43 | using namespace std::chrono_literals; |
| 44 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 45 | namespace android { |
| 46 | |
Steven Moreland | 1fda67b | 2021-04-02 18:35:50 +0000 | [diff] [blame] | 47 | TEST(BinderRpcParcel, EntireParcelFormatted) { |
| 48 | Parcel p; |
| 49 | p.writeInt32(3); |
| 50 | |
| 51 | EXPECT_DEATH(p.markForBinder(sp<BBinder>::make()), ""); |
| 52 | } |
| 53 | |
Yifan Hong | 00aeb76 | 2021-05-12 17:07:36 -0700 | [diff] [blame] | 54 | TEST(BinderRpc, SetExternalServer) { |
| 55 | base::unique_fd sink(TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR))); |
| 56 | int sinkFd = sink.get(); |
| 57 | auto server = RpcServer::make(); |
| 58 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 59 | ASSERT_FALSE(server->hasServer()); |
| 60 | ASSERT_TRUE(server->setupExternalServer(std::move(sink))); |
| 61 | ASSERT_TRUE(server->hasServer()); |
| 62 | base::unique_fd retrieved = server->releaseServer(); |
| 63 | ASSERT_FALSE(server->hasServer()); |
| 64 | ASSERT_EQ(sinkFd, retrieved.get()); |
| 65 | } |
| 66 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 67 | using android::binder::Status; |
| 68 | |
| 69 | #define EXPECT_OK(status) \ |
| 70 | do { \ |
| 71 | Status stat = (status); \ |
| 72 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 73 | } while (false) |
| 74 | |
| 75 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 76 | public: |
| 77 | static std::atomic<int32_t> gNum; |
| 78 | |
| 79 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 80 | Status getName(std::string* name) override { |
| 81 | *name = mName; |
| 82 | return Status::ok(); |
| 83 | } |
| 84 | ~MyBinderRpcSession() { gNum--; } |
| 85 | |
| 86 | private: |
| 87 | std::string mName; |
| 88 | }; |
| 89 | std::atomic<int32_t> MyBinderRpcSession::gNum; |
| 90 | |
| 91 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 92 | public: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 93 | wp<RpcServer> server; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 94 | |
| 95 | Status sendString(const std::string& str) override { |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 96 | (void)str; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 97 | return Status::ok(); |
| 98 | } |
| 99 | Status doubleString(const std::string& str, std::string* strstr) override { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 100 | *strstr = str + str; |
| 101 | return Status::ok(); |
| 102 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 103 | Status countBinders(std::vector<int32_t>* out) override { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 104 | sp<RpcServer> spServer = server.promote(); |
| 105 | if (spServer == nullptr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 107 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 108 | out->clear(); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 109 | for (auto session : spServer->listSessions()) { |
| 110 | size_t count = session->state()->countBinders(); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 111 | if (count != 1) { |
| 112 | // this is called when there is only one binder held remaining, |
| 113 | // so to aid debugging |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 114 | session->state()->dump(); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 115 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 116 | out->push_back(count); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 117 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 118 | return Status::ok(); |
| 119 | } |
| 120 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 121 | if (binder == nullptr) { |
| 122 | std::cout << "Received null binder!" << std::endl; |
| 123 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 124 | } |
| 125 | *out = binder->pingBinder(); |
| 126 | return Status::ok(); |
| 127 | } |
| 128 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 129 | *out = binder; |
| 130 | return Status::ok(); |
| 131 | } |
| 132 | static sp<IBinder> mHeldBinder; |
| 133 | Status holdBinder(const sp<IBinder>& binder) override { |
| 134 | mHeldBinder = binder; |
| 135 | return Status::ok(); |
| 136 | } |
| 137 | Status getHeldBinder(sp<IBinder>* held) override { |
| 138 | *held = mHeldBinder; |
| 139 | return Status::ok(); |
| 140 | } |
| 141 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 142 | if (count <= 0) return Status::ok(); |
| 143 | return binder->nestMe(this, count - 1); |
| 144 | } |
| 145 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 146 | static sp<IBinder> binder = new BBinder; |
| 147 | *out = binder; |
| 148 | return Status::ok(); |
| 149 | } |
| 150 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 151 | *out = new MyBinderRpcSession(name); |
| 152 | return Status::ok(); |
| 153 | } |
| 154 | Status getNumOpenSessions(int32_t* out) override { |
| 155 | *out = MyBinderRpcSession::gNum; |
| 156 | return Status::ok(); |
| 157 | } |
| 158 | |
| 159 | std::mutex blockMutex; |
| 160 | Status lock() override { |
| 161 | blockMutex.lock(); |
| 162 | return Status::ok(); |
| 163 | } |
| 164 | Status unlockInMsAsync(int32_t ms) override { |
| 165 | usleep(ms * 1000); |
| 166 | blockMutex.unlock(); |
| 167 | return Status::ok(); |
| 168 | } |
| 169 | Status lockUnlock() override { |
| 170 | std::lock_guard<std::mutex> _l(blockMutex); |
| 171 | return Status::ok(); |
| 172 | } |
| 173 | |
| 174 | Status sleepMs(int32_t ms) override { |
| 175 | usleep(ms * 1000); |
| 176 | return Status::ok(); |
| 177 | } |
| 178 | |
| 179 | Status sleepMsAsync(int32_t ms) override { |
| 180 | // In-process binder calls are asynchronous, but the call to this method |
| 181 | // is synchronous wrt its client. This in/out-process threading model |
| 182 | // diffentiation is a classic binder leaky abstraction (for better or |
| 183 | // worse) and is preserved here the way binder sockets plugs itself |
| 184 | // into BpBinder, as nothing is changed at the higher levels |
| 185 | // (IInterface) which result in this behavior. |
| 186 | return sleepMs(ms); |
| 187 | } |
| 188 | |
| 189 | Status die(bool cleanup) override { |
| 190 | if (cleanup) { |
| 191 | exit(1); |
| 192 | } else { |
| 193 | _exit(1); |
| 194 | } |
| 195 | } |
| 196 | }; |
| 197 | sp<IBinder> MyBinderRpcTest::mHeldBinder; |
| 198 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 199 | class Pipe { |
| 200 | public: |
| 201 | Pipe() { CHECK(android::base::Pipe(&mRead, &mWrite)); } |
| 202 | Pipe(Pipe&&) = default; |
| 203 | android::base::borrowed_fd readEnd() { return mRead; } |
| 204 | android::base::borrowed_fd writeEnd() { return mWrite; } |
| 205 | |
| 206 | private: |
| 207 | android::base::unique_fd mRead; |
| 208 | android::base::unique_fd mWrite; |
| 209 | }; |
| 210 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 211 | class Process { |
| 212 | public: |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 213 | Process(Process&&) = default; |
| 214 | Process(const std::function<void(Pipe*)>& f) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 215 | if (0 == (mPid = fork())) { |
| 216 | // racey: assume parent doesn't crash before this is set |
| 217 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 218 | |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 219 | f(&mPipe); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | ~Process() { |
| 223 | if (mPid != 0) { |
| 224 | kill(mPid, SIGKILL); |
| 225 | } |
| 226 | } |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 227 | Pipe* getPipe() { return &mPipe; } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 228 | |
| 229 | private: |
| 230 | pid_t mPid = 0; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 231 | Pipe mPipe; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
| 234 | static std::string allocateSocketAddress() { |
| 235 | static size_t id = 0; |
Steven Moreland | 4bfbf2e | 2021-04-14 22:15:16 +0000 | [diff] [blame] | 236 | std::string temp = getenv("TMPDIR") ?: "/tmp"; |
| 237 | return temp + "/binderRpcTest_" + std::to_string(id++); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 238 | }; |
| 239 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 240 | struct ProcessSession { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 241 | // reference to process hosting a socket server |
| 242 | Process host; |
| 243 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 244 | struct SessionInfo { |
| 245 | sp<RpcSession> session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 246 | sp<IBinder> root; |
| 247 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 248 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 249 | // client session objects associated with other process |
| 250 | // each one represents a separate session |
| 251 | std::vector<SessionInfo> sessions; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 252 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 253 | ProcessSession(ProcessSession&&) = default; |
| 254 | ~ProcessSession() { |
| 255 | for (auto& session : sessions) { |
| 256 | session.root = nullptr; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 257 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 258 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 259 | for (auto& info : sessions) { |
| 260 | sp<RpcSession>& session = info.session; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 261 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 262 | EXPECT_NE(nullptr, session); |
| 263 | EXPECT_NE(nullptr, session->state()); |
| 264 | EXPECT_EQ(0, session->state()->countBinders()) << (session->state()->dump(), "dump:"); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 265 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 266 | wp<RpcSession> weakSession = session; |
| 267 | session = nullptr; |
| 268 | EXPECT_EQ(nullptr, weakSession.promote()) << "Leaked session"; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 269 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 270 | } |
| 271 | }; |
| 272 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 273 | // Process session where the process hosts IBinderRpcTest, the server used |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 274 | // for most testing here |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 275 | struct BinderRpcTestProcessSession { |
| 276 | ProcessSession proc; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 277 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 278 | // pre-fetched root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 279 | sp<IBinder> rootBinder; |
| 280 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 281 | // pre-casted root object (for first session) |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 282 | sp<IBinderRpcTest> rootIface; |
| 283 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 284 | // whether session should be invalidated by end of run |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 285 | bool expectInvalid = false; |
| 286 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 287 | BinderRpcTestProcessSession(BinderRpcTestProcessSession&&) = default; |
| 288 | ~BinderRpcTestProcessSession() { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 289 | if (!expectInvalid) { |
| 290 | std::vector<int32_t> remoteCounts; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 291 | // calling over any sessions counts across all sessions |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 292 | EXPECT_OK(rootIface->countBinders(&remoteCounts)); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 293 | EXPECT_EQ(remoteCounts.size(), proc.sessions.size()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 294 | for (auto remoteCount : remoteCounts) { |
| 295 | EXPECT_EQ(remoteCount, 1); |
| 296 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | rootIface = nullptr; |
| 300 | rootBinder = nullptr; |
| 301 | } |
| 302 | }; |
| 303 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 304 | enum class SocketType { |
| 305 | UNIX, |
| 306 | VSOCK, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 307 | INET, |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 308 | }; |
| 309 | static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) { |
| 310 | switch (info.param) { |
| 311 | case SocketType::UNIX: |
| 312 | return "unix_domain_socket"; |
| 313 | case SocketType::VSOCK: |
| 314 | return "vm_socket"; |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 315 | case SocketType::INET: |
| 316 | return "inet_socket"; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 317 | default: |
| 318 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 319 | return ""; |
| 320 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 321 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 322 | class BinderRpc : public ::testing::TestWithParam<SocketType> { |
| 323 | public: |
| 324 | // This creates a new process serving an interface on a certain number of |
| 325 | // threads. |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 326 | ProcessSession createRpcTestSocketServerProcess( |
| 327 | size_t numThreads, size_t numSessions, |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 328 | const std::function<void(const sp<RpcServer>&)>& configure) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 329 | CHECK_GE(numSessions, 1) << "Must have at least one session to a server"; |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 330 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 331 | SocketType socketType = GetParam(); |
| 332 | |
| 333 | std::string addr = allocateSocketAddress(); |
| 334 | unlink(addr.c_str()); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 335 | static unsigned int vsockPort = 3456; |
| 336 | vsockPort++; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 337 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 338 | auto ret = ProcessSession{ |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 339 | .host = Process([&](Pipe* pipe) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 340 | sp<RpcServer> server = RpcServer::make(); |
| 341 | |
| 342 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 343 | server->setMaxThreads(numThreads); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 344 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 345 | unsigned int outPort = 0; |
| 346 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 347 | switch (socketType) { |
| 348 | case SocketType::UNIX: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 349 | CHECK(server->setupUnixDomainServer(addr.c_str())) << addr; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 350 | break; |
| 351 | case SocketType::VSOCK: |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 352 | CHECK(server->setupVsockServer(vsockPort)); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 353 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 354 | case SocketType::INET: { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 355 | CHECK(server->setupInetServer(0, &outPort)); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 356 | CHECK_NE(0, outPort); |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 357 | break; |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 358 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 359 | default: |
| 360 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 361 | } |
| 362 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 363 | CHECK(android::base::WriteFully(pipe->writeEnd(), &outPort, sizeof(outPort))); |
| 364 | |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 365 | configure(server); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 366 | |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 367 | server->join(); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 368 | }), |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 369 | }; |
| 370 | |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 371 | // always read socket, so that we have waited for the server to start |
| 372 | unsigned int outPort = 0; |
| 373 | CHECK(android::base::ReadFully(ret.host.getPipe()->readEnd(), &outPort, sizeof(outPort))); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 374 | if (socketType == SocketType::INET) { |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 375 | CHECK_NE(0, outPort); |
Yifan Hong | 6d82c8a | 2021-04-26 20:26:45 -0700 | [diff] [blame] | 376 | } |
| 377 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 378 | for (size_t i = 0; i < numSessions; i++) { |
| 379 | sp<RpcSession> session = RpcSession::make(); |
Steven Moreland | 76d2c1f | 2021-05-05 20:28:58 +0000 | [diff] [blame] | 380 | switch (socketType) { |
| 381 | case SocketType::UNIX: |
| 382 | if (session->setupUnixDomainClient(addr.c_str())) goto success; |
| 383 | break; |
| 384 | case SocketType::VSOCK: |
| 385 | if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success; |
| 386 | break; |
| 387 | case SocketType::INET: |
| 388 | if (session->setupInetClient("127.0.0.1", outPort)) goto success; |
| 389 | break; |
| 390 | default: |
| 391 | LOG_ALWAYS_FATAL("Unknown socket type"); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 392 | } |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 393 | LOG_ALWAYS_FATAL("Could not connect"); |
| 394 | success: |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 395 | ret.sessions.push_back({session, session->getRootObject()}); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 396 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 397 | return ret; |
| 398 | } |
| 399 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 400 | BinderRpcTestProcessSession createRpcTestSocketServerProcess(size_t numThreads, |
| 401 | size_t numSessions = 1) { |
| 402 | BinderRpcTestProcessSession ret{ |
| 403 | .proc = createRpcTestSocketServerProcess(numThreads, numSessions, |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 404 | [&](const sp<RpcServer>& server) { |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 405 | sp<MyBinderRpcTest> service = |
| 406 | new MyBinderRpcTest; |
| 407 | server->setRootObject(service); |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 408 | service->server = server; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 409 | }), |
| 410 | }; |
| 411 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 412 | ret.rootBinder = ret.proc.sessions.at(0).root; |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 413 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 414 | |
| 415 | return ret; |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | TEST_P(BinderRpc, RootObjectIsNull) { |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 420 | auto proc = createRpcTestSocketServerProcess(1, 1, [](const sp<RpcServer>& server) { |
Steven Moreland | 611d15f | 2021-05-01 01:28:27 +0000 | [diff] [blame] | 421 | // this is the default, but to be explicit |
| 422 | server->setRootObject(nullptr); |
| 423 | }); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 424 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 425 | EXPECT_EQ(nullptr, proc.sessions.at(0).root); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 428 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 429 | auto proc = createRpcTestSocketServerProcess(1); |
| 430 | ASSERT_NE(proc.rootBinder, nullptr); |
| 431 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 432 | } |
| 433 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame] | 434 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
| 435 | auto proc = createRpcTestSocketServerProcess(1); |
| 436 | ASSERT_NE(proc.rootBinder, nullptr); |
| 437 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 438 | } |
| 439 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 440 | TEST_P(BinderRpc, MultipleSessions) { |
| 441 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 5 /*sessions*/); |
| 442 | for (auto session : proc.proc.sessions) { |
| 443 | ASSERT_NE(nullptr, session.root); |
| 444 | EXPECT_EQ(OK, session.root->pingBinder()); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 448 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 449 | auto proc = createRpcTestSocketServerProcess(1); |
| 450 | Parcel data; |
| 451 | Parcel reply; |
| 452 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 453 | } |
| 454 | |
Steven Moreland | 67753c3 | 2021-04-02 18:45:19 +0000 | [diff] [blame] | 455 | TEST_P(BinderRpc, AppendSeparateFormats) { |
| 456 | auto proc = createRpcTestSocketServerProcess(1); |
| 457 | |
| 458 | Parcel p1; |
| 459 | p1.markForBinder(proc.rootBinder); |
| 460 | p1.writeInt32(3); |
| 461 | |
| 462 | Parcel p2; |
| 463 | |
| 464 | EXPECT_EQ(BAD_TYPE, p1.appendFrom(&p2, 0, p2.dataSize())); |
| 465 | EXPECT_EQ(BAD_TYPE, p2.appendFrom(&p1, 0, p1.dataSize())); |
| 466 | } |
| 467 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 468 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 469 | auto proc = createRpcTestSocketServerProcess(1); |
| 470 | Parcel data; |
| 471 | data.markForBinder(proc.rootBinder); |
| 472 | Parcel reply; |
| 473 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 474 | } |
| 475 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 476 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 477 | auto proc = createRpcTestSocketServerProcess(1); |
| 478 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 479 | } |
| 480 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 481 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 482 | auto proc = createRpcTestSocketServerProcess(1); |
| 483 | std::string doubled; |
| 484 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 485 | EXPECT_EQ("cool cool ", doubled); |
| 486 | } |
| 487 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 488 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 489 | auto proc = createRpcTestSocketServerProcess(1); |
| 490 | std::string single = std::string(1024, 'a'); |
| 491 | std::string doubled; |
| 492 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 493 | EXPECT_EQ(single + single, doubled); |
| 494 | } |
| 495 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 496 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 497 | auto proc = createRpcTestSocketServerProcess(1); |
| 498 | |
| 499 | int32_t pingResult; |
| 500 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 501 | EXPECT_EQ(OK, pingResult); |
| 502 | |
| 503 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 504 | } |
| 505 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 506 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 507 | auto proc = createRpcTestSocketServerProcess(1); |
| 508 | |
| 509 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 510 | sp<IBinder> outBinder; |
| 511 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 512 | EXPECT_EQ(inBinder, outBinder); |
| 513 | |
| 514 | wp<IBinder> weak = inBinder; |
| 515 | inBinder = nullptr; |
| 516 | outBinder = nullptr; |
| 517 | |
| 518 | // Force reading a reply, to process any pending dec refs from the other |
| 519 | // process (the other process will process dec refs there before processing |
| 520 | // the ping here). |
| 521 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 522 | |
| 523 | EXPECT_EQ(nullptr, weak.promote()); |
| 524 | |
| 525 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 526 | } |
| 527 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 528 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 529 | auto proc = createRpcTestSocketServerProcess(1); |
| 530 | |
| 531 | sp<IBinderRpcSession> session; |
| 532 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 533 | |
| 534 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 535 | sp<IBinder> outBinder; |
| 536 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 537 | EXPECT_EQ(inBinder, outBinder); |
| 538 | |
| 539 | wp<IBinder> weak = inBinder; |
| 540 | session = nullptr; |
| 541 | inBinder = nullptr; |
| 542 | outBinder = nullptr; |
| 543 | |
| 544 | // Force reading a reply, to process any pending dec refs from the other |
| 545 | // process (the other process will process dec refs there before processing |
| 546 | // the ping here). |
| 547 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 548 | |
| 549 | EXPECT_EQ(nullptr, weak.promote()); |
| 550 | } |
| 551 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 552 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 553 | auto proc = createRpcTestSocketServerProcess(1); |
| 554 | |
| 555 | sp<IBinder> outBinder; |
| 556 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 557 | EXPECT_EQ(nullptr, outBinder); |
| 558 | } |
| 559 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 560 | TEST_P(BinderRpc, HoldBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 561 | auto proc = createRpcTestSocketServerProcess(1); |
| 562 | |
| 563 | IBinder* ptr = nullptr; |
| 564 | { |
| 565 | sp<IBinder> binder = new BBinder(); |
| 566 | ptr = binder.get(); |
| 567 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 568 | } |
| 569 | |
| 570 | sp<IBinder> held; |
| 571 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 572 | |
| 573 | EXPECT_EQ(held.get(), ptr); |
| 574 | |
| 575 | // stop holding binder, because we test to make sure references are cleaned |
| 576 | // up |
| 577 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 578 | // and flush ref counts |
| 579 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 580 | } |
| 581 | |
| 582 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 583 | // These are behavioral differences form regular binder, where certain usecases |
| 584 | // aren't supported. |
| 585 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 586 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketSessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 587 | auto proc1 = createRpcTestSocketServerProcess(1); |
| 588 | auto proc2 = createRpcTestSocketServerProcess(1); |
| 589 | |
| 590 | sp<IBinder> outBinder; |
| 591 | EXPECT_EQ(INVALID_OPERATION, |
| 592 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 593 | } |
| 594 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 595 | TEST_P(BinderRpc, CannotMixBindersBetweenTwoSessionsToTheSameServer) { |
| 596 | auto proc = createRpcTestSocketServerProcess(1 /*threads*/, 2 /*sessions*/); |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 597 | |
| 598 | sp<IBinder> outBinder; |
| 599 | EXPECT_EQ(INVALID_OPERATION, |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 600 | proc.rootIface->repeatBinder(proc.proc.sessions.at(1).root, &outBinder) |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 601 | .transactionError()); |
| 602 | } |
| 603 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 604 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 605 | auto proc = createRpcTestSocketServerProcess(1); |
| 606 | |
| 607 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 608 | sp<IBinder> outBinder; |
| 609 | EXPECT_EQ(INVALID_OPERATION, |
| 610 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 611 | } |
| 612 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 613 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 614 | auto proc = createRpcTestSocketServerProcess(1); |
| 615 | |
| 616 | // for historical reasons, IServiceManager interface only returns the |
| 617 | // exception code |
| 618 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 619 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 620 | } |
| 621 | |
| 622 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 623 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 624 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 625 | auto proc = createRpcTestSocketServerProcess(1); |
| 626 | |
| 627 | sp<IBinder> outBinder; |
| 628 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 629 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 630 | } |
| 631 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 632 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 633 | auto proc = createRpcTestSocketServerProcess(1); |
| 634 | |
| 635 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 636 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 637 | |
| 638 | wp<IBinder> weak = nastyNester; |
| 639 | nastyNester = nullptr; |
| 640 | EXPECT_EQ(nullptr, weak.promote()); |
| 641 | } |
| 642 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 643 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 644 | auto proc = createRpcTestSocketServerProcess(1); |
| 645 | |
| 646 | sp<IBinder> a; |
| 647 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 648 | |
| 649 | sp<IBinder> b; |
| 650 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 651 | |
| 652 | EXPECT_EQ(a, b); |
| 653 | } |
| 654 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 655 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 656 | auto proc = createRpcTestSocketServerProcess(1); |
| 657 | |
| 658 | sp<IBinder> a; |
| 659 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 660 | wp<IBinder> weak = a; |
| 661 | a = nullptr; |
| 662 | |
| 663 | sp<IBinder> b; |
| 664 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 665 | |
| 666 | // this is the wrong behavior, since BpBinder |
| 667 | // doesn't implement onIncStrongAttempted |
| 668 | // but make sure there is no crash |
| 669 | EXPECT_EQ(nullptr, weak.promote()); |
| 670 | |
| 671 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 672 | |
| 673 | // In order to fix this: |
| 674 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 675 | // response to promote - round trip...) |
| 676 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 677 | EXPECT_EQ(b, weak.promote()); |
| 678 | } |
| 679 | |
| 680 | #define expectSessions(expected, iface) \ |
| 681 | do { \ |
| 682 | int session; \ |
| 683 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 684 | EXPECT_EQ(expected, session); \ |
| 685 | } while (false) |
| 686 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 687 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 688 | auto proc = createRpcTestSocketServerProcess(1); |
| 689 | |
| 690 | sp<IBinderRpcSession> session; |
| 691 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 692 | std::string out; |
| 693 | EXPECT_OK(session->getName(&out)); |
| 694 | EXPECT_EQ("aoeu", out); |
| 695 | |
| 696 | expectSessions(1, proc.rootIface); |
| 697 | session = nullptr; |
| 698 | expectSessions(0, proc.rootIface); |
| 699 | } |
| 700 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 701 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 702 | auto proc = createRpcTestSocketServerProcess(1); |
| 703 | |
| 704 | std::vector<sp<IBinderRpcSession>> sessions; |
| 705 | |
| 706 | for (size_t i = 0; i < 15; i++) { |
| 707 | expectSessions(i, proc.rootIface); |
| 708 | sp<IBinderRpcSession> session; |
| 709 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 710 | sessions.push_back(session); |
| 711 | } |
| 712 | expectSessions(sessions.size(), proc.rootIface); |
| 713 | for (size_t i = 0; i < sessions.size(); i++) { |
| 714 | std::string out; |
| 715 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 716 | EXPECT_EQ(std::to_string(i), out); |
| 717 | } |
| 718 | expectSessions(sessions.size(), proc.rootIface); |
| 719 | |
| 720 | while (!sessions.empty()) { |
| 721 | sessions.pop_back(); |
| 722 | expectSessions(sessions.size(), proc.rootIface); |
| 723 | } |
| 724 | expectSessions(0, proc.rootIface); |
| 725 | } |
| 726 | |
| 727 | size_t epochMillis() { |
| 728 | using std::chrono::duration_cast; |
| 729 | using std::chrono::milliseconds; |
| 730 | using std::chrono::seconds; |
| 731 | using std::chrono::system_clock; |
| 732 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 733 | } |
| 734 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 735 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 736 | constexpr size_t kNumThreads = 10; |
| 737 | |
| 738 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 739 | |
| 740 | EXPECT_OK(proc.rootIface->lock()); |
| 741 | |
| 742 | // block all but one thread taking locks |
| 743 | std::vector<std::thread> ts; |
| 744 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 745 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 746 | } |
| 747 | |
| 748 | usleep(100000); // give chance for calls on other threads |
| 749 | |
| 750 | // other calls still work |
| 751 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 752 | |
| 753 | constexpr size_t blockTimeMs = 500; |
| 754 | size_t epochMsBefore = epochMillis(); |
| 755 | // after this, we should never see a response within this time |
| 756 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 757 | |
| 758 | // this call should be blocked for blockTimeMs |
| 759 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 760 | |
| 761 | size_t epochMsAfter = epochMillis(); |
| 762 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 763 | |
| 764 | for (auto& t : ts) t.join(); |
| 765 | } |
| 766 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 767 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 768 | constexpr size_t kNumThreads = 10; |
| 769 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 770 | constexpr size_t kSleepMs = 500; |
| 771 | |
| 772 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 773 | |
| 774 | size_t epochMsBefore = epochMillis(); |
| 775 | |
| 776 | std::vector<std::thread> ts; |
| 777 | for (size_t i = 0; i < kNumCalls; i++) { |
| 778 | ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); })); |
| 779 | } |
| 780 | |
| 781 | for (auto& t : ts) t.join(); |
| 782 | |
| 783 | size_t epochMsAfter = epochMillis(); |
| 784 | |
| 785 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs); |
| 786 | |
| 787 | // Potential flake, but make sure calls are handled in parallel. |
| 788 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs); |
| 789 | } |
| 790 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 791 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 792 | constexpr size_t kNumClientThreads = 10; |
| 793 | constexpr size_t kNumServerThreads = 10; |
| 794 | constexpr size_t kNumCalls = 100; |
| 795 | |
| 796 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 797 | |
| 798 | std::vector<std::thread> threads; |
| 799 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 800 | threads.push_back(std::thread([&] { |
| 801 | for (size_t j = 0; j < kNumCalls; j++) { |
| 802 | sp<IBinder> out; |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 803 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &out)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 804 | EXPECT_EQ(proc.rootBinder, out); |
| 805 | } |
| 806 | })); |
| 807 | } |
| 808 | |
| 809 | for (auto& t : threads) t.join(); |
| 810 | } |
| 811 | |
Steven Moreland | c604698 | 2021-04-20 00:49:42 +0000 | [diff] [blame] | 812 | TEST_P(BinderRpc, OnewayStressTest) { |
| 813 | constexpr size_t kNumClientThreads = 10; |
| 814 | constexpr size_t kNumServerThreads = 10; |
| 815 | constexpr size_t kNumCalls = 100; |
| 816 | |
| 817 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 818 | |
| 819 | std::vector<std::thread> threads; |
| 820 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 821 | threads.push_back(std::thread([&] { |
| 822 | for (size_t j = 0; j < kNumCalls; j++) { |
| 823 | EXPECT_OK(proc.rootIface->sendString("a")); |
| 824 | } |
| 825 | |
| 826 | // check threads are not stuck |
| 827 | EXPECT_OK(proc.rootIface->sleepMs(250)); |
| 828 | })); |
| 829 | } |
| 830 | |
| 831 | for (auto& t : threads) t.join(); |
| 832 | } |
| 833 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 834 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 835 | constexpr size_t kReallyLongTimeMs = 100; |
| 836 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 837 | |
| 838 | // more than one thread, just so this doesn't deadlock |
| 839 | auto proc = createRpcTestSocketServerProcess(2); |
| 840 | |
| 841 | size_t epochMsBefore = epochMillis(); |
| 842 | |
| 843 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 844 | |
| 845 | size_t epochMsAfter = epochMillis(); |
| 846 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 847 | } |
| 848 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 849 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 850 | constexpr size_t kNumSleeps = 10; |
| 851 | constexpr size_t kNumExtraServerThreads = 4; |
| 852 | constexpr size_t kSleepMs = 50; |
| 853 | |
| 854 | // make sure calls to the same object happen on the same thread |
| 855 | auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads); |
| 856 | |
| 857 | EXPECT_OK(proc.rootIface->lock()); |
| 858 | |
| 859 | for (size_t i = 0; i < kNumSleeps; i++) { |
| 860 | // these should be processed serially |
| 861 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 862 | } |
| 863 | // should also be processesed serially |
| 864 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 865 | |
| 866 | size_t epochMsBefore = epochMillis(); |
| 867 | EXPECT_OK(proc.rootIface->lockUnlock()); |
| 868 | size_t epochMsAfter = epochMillis(); |
| 869 | |
| 870 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
| 871 | } |
| 872 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 873 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 874 | for (bool doDeathCleanup : {true, false}) { |
| 875 | auto proc = createRpcTestSocketServerProcess(1); |
| 876 | |
| 877 | // make sure there is some state during crash |
| 878 | // 1. we hold their binder |
| 879 | sp<IBinderRpcSession> session; |
| 880 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 881 | // 2. they hold our binder |
| 882 | sp<IBinder> binder = new BBinder(); |
| 883 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 884 | |
| 885 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 886 | << "Do death cleanup: " << doDeathCleanup; |
| 887 | |
Steven Moreland | 736664b | 2021-05-01 04:27:25 +0000 | [diff] [blame] | 888 | proc.expectInvalid = true; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 889 | } |
| 890 | } |
| 891 | |
Steven Moreland | 37aff18 | 2021-03-26 02:04:16 +0000 | [diff] [blame] | 892 | TEST_P(BinderRpc, WorksWithLibbinderNdkPing) { |
| 893 | auto proc = createRpcTestSocketServerProcess(1); |
| 894 | |
| 895 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 896 | ASSERT_NE(binder, nullptr); |
| 897 | |
| 898 | ASSERT_EQ(STATUS_OK, AIBinder_ping(binder.get())); |
| 899 | } |
| 900 | |
| 901 | TEST_P(BinderRpc, WorksWithLibbinderNdkUserTransaction) { |
| 902 | auto proc = createRpcTestSocketServerProcess(1); |
| 903 | |
| 904 | ndk::SpAIBinder binder = ndk::SpAIBinder(AIBinder_fromPlatformBinder(proc.rootBinder)); |
| 905 | ASSERT_NE(binder, nullptr); |
| 906 | |
| 907 | auto ndkBinder = aidl::IBinderRpcTest::fromBinder(binder); |
| 908 | ASSERT_NE(ndkBinder, nullptr); |
| 909 | |
| 910 | std::string out; |
| 911 | ndk::ScopedAStatus status = ndkBinder->doubleString("aoeu", &out); |
| 912 | ASSERT_TRUE(status.isOk()) << status.getDescription(); |
| 913 | ASSERT_EQ("aoeuaoeu", out); |
| 914 | } |
| 915 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 916 | ssize_t countFds() { |
| 917 | DIR* dir = opendir("/proc/self/fd/"); |
| 918 | if (dir == nullptr) return -1; |
| 919 | ssize_t ret = 0; |
| 920 | dirent* ent; |
| 921 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 922 | closedir(dir); |
| 923 | return ret; |
| 924 | } |
| 925 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 926 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 927 | ssize_t beforeFds = countFds(); |
| 928 | ASSERT_GE(beforeFds, 0); |
| 929 | { |
| 930 | auto proc = createRpcTestSocketServerProcess(10); |
| 931 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 932 | } |
| 933 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 934 | } |
| 935 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 936 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 937 | ::testing::ValuesIn({ |
| 938 | SocketType::UNIX, |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 939 | // TODO(b/185269356): working on host |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 940 | #ifdef __BIONIC__ |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 941 | SocketType::VSOCK, |
Steven Moreland | bd5002b | 2021-05-04 23:12:56 +0000 | [diff] [blame] | 942 | #endif |
Yifan Hong | 0d2bd11 | 2021-04-13 17:38:36 -0700 | [diff] [blame] | 943 | SocketType::INET, |
| 944 | }), |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 945 | PrintSocketType); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 946 | |
Yifan Hong | 4ffb0c7 | 2021-05-07 18:35:14 -0700 | [diff] [blame] | 947 | class BinderRpcServerRootObject : public ::testing::TestWithParam<std::tuple<bool, bool>> {}; |
| 948 | |
| 949 | TEST_P(BinderRpcServerRootObject, WeakRootObject) { |
| 950 | using SetFn = std::function<void(RpcServer*, sp<IBinder>)>; |
| 951 | auto setRootObject = [](bool isStrong) -> SetFn { |
| 952 | return isStrong ? SetFn(&RpcServer::setRootObject) : SetFn(&RpcServer::setRootObjectWeak); |
| 953 | }; |
| 954 | |
| 955 | auto server = RpcServer::make(); |
| 956 | auto [isStrong1, isStrong2] = GetParam(); |
| 957 | auto binder1 = sp<BBinder>::make(); |
| 958 | IBinder* binderRaw1 = binder1.get(); |
| 959 | setRootObject(isStrong1)(server.get(), binder1); |
| 960 | EXPECT_EQ(binderRaw1, server->getRootObject()); |
| 961 | binder1.clear(); |
| 962 | EXPECT_EQ((isStrong1 ? binderRaw1 : nullptr), server->getRootObject()); |
| 963 | |
| 964 | auto binder2 = sp<BBinder>::make(); |
| 965 | IBinder* binderRaw2 = binder2.get(); |
| 966 | setRootObject(isStrong2)(server.get(), binder2); |
| 967 | EXPECT_EQ(binderRaw2, server->getRootObject()); |
| 968 | binder2.clear(); |
| 969 | EXPECT_EQ((isStrong2 ? binderRaw2 : nullptr), server->getRootObject()); |
| 970 | } |
| 971 | |
| 972 | INSTANTIATE_TEST_CASE_P(BinderRpc, BinderRpcServerRootObject, |
| 973 | ::testing::Combine(::testing::Bool(), ::testing::Bool())); |
| 974 | |
Yifan Hong | 1a23585 | 2021-05-13 16:07:47 -0700 | [diff] [blame^] | 975 | class OneOffSignal { |
| 976 | public: |
| 977 | // If notify() was previously called, or is called within |duration|, return true; else false. |
| 978 | template <typename R, typename P> |
| 979 | bool wait(std::chrono::duration<R, P> duration) { |
| 980 | std::unique_lock<std::mutex> lock(mMutex); |
| 981 | return mCv.wait_for(lock, duration, [this] { return mValue; }); |
| 982 | } |
| 983 | void notify() { |
| 984 | std::unique_lock<std::mutex> lock(mMutex); |
| 985 | mValue = true; |
| 986 | lock.unlock(); |
| 987 | mCv.notify_all(); |
| 988 | } |
| 989 | |
| 990 | private: |
| 991 | std::mutex mMutex; |
| 992 | std::condition_variable mCv; |
| 993 | bool mValue = false; |
| 994 | }; |
| 995 | |
| 996 | TEST(BinderRpc, Shutdown) { |
| 997 | auto addr = allocateSocketAddress(); |
| 998 | unlink(addr.c_str()); |
| 999 | auto server = RpcServer::make(); |
| 1000 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 1001 | ASSERT_TRUE(server->setupUnixDomainServer(addr.c_str())); |
| 1002 | auto joinEnds = std::make_shared<OneOffSignal>(); |
| 1003 | |
| 1004 | // If things are broken and the thread never stops, don't block other tests. Because the thread |
| 1005 | // may run after the test finishes, it must not access the stack memory of the test. Hence, |
| 1006 | // shared pointers are passed. |
| 1007 | std::thread([server, joinEnds] { |
| 1008 | server->join(); |
| 1009 | joinEnds->notify(); |
| 1010 | }).detach(); |
| 1011 | |
| 1012 | bool shutdown = false; |
| 1013 | for (int i = 0; i < 10 && !shutdown; i++) { |
| 1014 | usleep(300 * 1000); // 300ms; total 3s |
| 1015 | if (server->shutdown()) shutdown = true; |
| 1016 | } |
| 1017 | ASSERT_TRUE(shutdown) << "server->shutdown() never returns true"; |
| 1018 | |
| 1019 | ASSERT_TRUE(joinEnds->wait(2s)) |
| 1020 | << "After server->shutdown() returns true, join() did not stop after 2s"; |
| 1021 | } |
| 1022 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 1023 | } // namespace android |
| 1024 | |
| 1025 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1026 | ::testing::InitGoogleTest(&argc, argv); |
| 1027 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
| 1028 | return RUN_ALL_TESTS(); |
| 1029 | } |