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> |
| 19 | #include <android-base/logging.h> |
| 20 | #include <binder/Binder.h> |
| 21 | #include <binder/BpBinder.h> |
| 22 | #include <binder/IServiceManager.h> |
| 23 | #include <binder/ProcessState.h> |
| 24 | #include <binder/RpcConnection.h> |
| 25 | #include <binder/RpcServer.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 28 | #include <chrono> |
| 29 | #include <cstdlib> |
| 30 | #include <iostream> |
| 31 | #include <thread> |
| 32 | |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 33 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 34 | #include <linux/vm_sockets.h> |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 35 | #endif //__BIONIC__ |
| 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 | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 40 | #include "../RpcState.h" // for debugging |
| 41 | |
| 42 | namespace android { |
| 43 | |
| 44 | using android::binder::Status; |
| 45 | |
| 46 | #define EXPECT_OK(status) \ |
| 47 | do { \ |
| 48 | Status stat = (status); \ |
| 49 | EXPECT_TRUE(stat.isOk()) << stat; \ |
| 50 | } while (false) |
| 51 | |
| 52 | class MyBinderRpcSession : public BnBinderRpcSession { |
| 53 | public: |
| 54 | static std::atomic<int32_t> gNum; |
| 55 | |
| 56 | MyBinderRpcSession(const std::string& name) : mName(name) { gNum++; } |
| 57 | Status getName(std::string* name) override { |
| 58 | *name = mName; |
| 59 | return Status::ok(); |
| 60 | } |
| 61 | ~MyBinderRpcSession() { gNum--; } |
| 62 | |
| 63 | private: |
| 64 | std::string mName; |
| 65 | }; |
| 66 | std::atomic<int32_t> MyBinderRpcSession::gNum; |
| 67 | |
| 68 | class MyBinderRpcTest : public BnBinderRpcTest { |
| 69 | public: |
| 70 | sp<RpcConnection> connection; |
| 71 | |
| 72 | Status sendString(const std::string& str) override { |
| 73 | std::cout << "Child received string: " << str << std::endl; |
| 74 | return Status::ok(); |
| 75 | } |
| 76 | Status doubleString(const std::string& str, std::string* strstr) override { |
| 77 | std::cout << "Child received string to double: " << str << std::endl; |
| 78 | *strstr = str + str; |
| 79 | return Status::ok(); |
| 80 | } |
| 81 | Status countBinders(int32_t* out) override { |
| 82 | if (connection == nullptr) { |
| 83 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 84 | } |
| 85 | *out = connection->state()->countBinders(); |
| 86 | if (*out != 1) { |
| 87 | connection->state()->dump(); |
| 88 | } |
| 89 | return Status::ok(); |
| 90 | } |
| 91 | Status pingMe(const sp<IBinder>& binder, int32_t* out) override { |
| 92 | if (binder == nullptr) { |
| 93 | std::cout << "Received null binder!" << std::endl; |
| 94 | return Status::fromExceptionCode(Status::EX_NULL_POINTER); |
| 95 | } |
| 96 | *out = binder->pingBinder(); |
| 97 | return Status::ok(); |
| 98 | } |
| 99 | Status repeatBinder(const sp<IBinder>& binder, sp<IBinder>* out) override { |
| 100 | *out = binder; |
| 101 | return Status::ok(); |
| 102 | } |
| 103 | static sp<IBinder> mHeldBinder; |
| 104 | Status holdBinder(const sp<IBinder>& binder) override { |
| 105 | mHeldBinder = binder; |
| 106 | return Status::ok(); |
| 107 | } |
| 108 | Status getHeldBinder(sp<IBinder>* held) override { |
| 109 | *held = mHeldBinder; |
| 110 | return Status::ok(); |
| 111 | } |
| 112 | Status nestMe(const sp<IBinderRpcTest>& binder, int count) override { |
| 113 | if (count <= 0) return Status::ok(); |
| 114 | return binder->nestMe(this, count - 1); |
| 115 | } |
| 116 | Status alwaysGiveMeTheSameBinder(sp<IBinder>* out) override { |
| 117 | static sp<IBinder> binder = new BBinder; |
| 118 | *out = binder; |
| 119 | return Status::ok(); |
| 120 | } |
| 121 | Status openSession(const std::string& name, sp<IBinderRpcSession>* out) override { |
| 122 | *out = new MyBinderRpcSession(name); |
| 123 | return Status::ok(); |
| 124 | } |
| 125 | Status getNumOpenSessions(int32_t* out) override { |
| 126 | *out = MyBinderRpcSession::gNum; |
| 127 | return Status::ok(); |
| 128 | } |
| 129 | |
| 130 | std::mutex blockMutex; |
| 131 | Status lock() override { |
| 132 | blockMutex.lock(); |
| 133 | return Status::ok(); |
| 134 | } |
| 135 | Status unlockInMsAsync(int32_t ms) override { |
| 136 | usleep(ms * 1000); |
| 137 | blockMutex.unlock(); |
| 138 | return Status::ok(); |
| 139 | } |
| 140 | Status lockUnlock() override { |
| 141 | std::lock_guard<std::mutex> _l(blockMutex); |
| 142 | return Status::ok(); |
| 143 | } |
| 144 | |
| 145 | Status sleepMs(int32_t ms) override { |
| 146 | usleep(ms * 1000); |
| 147 | return Status::ok(); |
| 148 | } |
| 149 | |
| 150 | Status sleepMsAsync(int32_t ms) override { |
| 151 | // In-process binder calls are asynchronous, but the call to this method |
| 152 | // is synchronous wrt its client. This in/out-process threading model |
| 153 | // diffentiation is a classic binder leaky abstraction (for better or |
| 154 | // worse) and is preserved here the way binder sockets plugs itself |
| 155 | // into BpBinder, as nothing is changed at the higher levels |
| 156 | // (IInterface) which result in this behavior. |
| 157 | return sleepMs(ms); |
| 158 | } |
| 159 | |
| 160 | Status die(bool cleanup) override { |
| 161 | if (cleanup) { |
| 162 | exit(1); |
| 163 | } else { |
| 164 | _exit(1); |
| 165 | } |
| 166 | } |
| 167 | }; |
| 168 | sp<IBinder> MyBinderRpcTest::mHeldBinder; |
| 169 | |
| 170 | class Process { |
| 171 | public: |
| 172 | Process(const std::function<void()>& f) { |
| 173 | if (0 == (mPid = fork())) { |
| 174 | // racey: assume parent doesn't crash before this is set |
| 175 | prctl(PR_SET_PDEATHSIG, SIGHUP); |
| 176 | |
| 177 | f(); |
| 178 | } |
| 179 | } |
| 180 | ~Process() { |
| 181 | if (mPid != 0) { |
| 182 | kill(mPid, SIGKILL); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | private: |
| 187 | pid_t mPid = 0; |
| 188 | }; |
| 189 | |
| 190 | static std::string allocateSocketAddress() { |
| 191 | static size_t id = 0; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 192 | static bool gUseTmp = access("/tmp/", F_OK) != -1; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 193 | |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 194 | if (gUseTmp) { |
| 195 | return "/tmp/binderRpcTest_" + std::to_string(id++); |
| 196 | } else { |
| 197 | return "/dev/binderRpcTest_" + std::to_string(id++); |
| 198 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | struct ProcessConnection { |
| 202 | // reference to process hosting a socket server |
| 203 | Process host; |
| 204 | |
| 205 | // client connection object associated with other process |
| 206 | sp<RpcConnection> connection; |
| 207 | |
| 208 | // pre-fetched root object |
| 209 | sp<IBinder> rootBinder; |
| 210 | |
| 211 | // whether connection should be invalidated by end of run |
| 212 | bool expectInvalid = false; |
| 213 | |
| 214 | ~ProcessConnection() { |
| 215 | rootBinder = nullptr; |
| 216 | EXPECT_NE(nullptr, connection); |
| 217 | EXPECT_NE(nullptr, connection->state()); |
| 218 | EXPECT_EQ(0, connection->state()->countBinders()) << (connection->state()->dump(), "dump:"); |
| 219 | |
| 220 | wp<RpcConnection> weakConnection = connection; |
| 221 | connection = nullptr; |
| 222 | EXPECT_EQ(nullptr, weakConnection.promote()) << "Leaked connection"; |
| 223 | } |
| 224 | }; |
| 225 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 226 | // Process connection where the process hosts IBinderRpcTest, the server used |
| 227 | // for most testing here |
| 228 | struct BinderRpcTestProcessConnection { |
| 229 | ProcessConnection proc; |
| 230 | |
| 231 | // pre-fetched root object |
| 232 | sp<IBinder> rootBinder; |
| 233 | |
| 234 | // pre-casted root object |
| 235 | sp<IBinderRpcTest> rootIface; |
| 236 | |
| 237 | ~BinderRpcTestProcessConnection() { |
| 238 | if (!proc.expectInvalid) { |
| 239 | int32_t remoteBinders = 0; |
| 240 | EXPECT_OK(rootIface->countBinders(&remoteBinders)); |
| 241 | // should only be the root binder object, iface |
| 242 | EXPECT_EQ(remoteBinders, 1); |
| 243 | } |
| 244 | |
| 245 | rootIface = nullptr; |
| 246 | rootBinder = nullptr; |
| 247 | } |
| 248 | }; |
| 249 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 250 | enum class SocketType { |
| 251 | UNIX, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 252 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 253 | VSOCK, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 254 | #endif // __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 255 | }; |
| 256 | static inline std::string PrintSocketType(const testing::TestParamInfo<SocketType>& info) { |
| 257 | switch (info.param) { |
| 258 | case SocketType::UNIX: |
| 259 | return "unix_domain_socket"; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 260 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 261 | case SocketType::VSOCK: |
| 262 | return "vm_socket"; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 263 | #endif // __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 264 | default: |
| 265 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 266 | return ""; |
| 267 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 268 | } |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 269 | class BinderRpc : public ::testing::TestWithParam<SocketType> { |
| 270 | public: |
| 271 | // This creates a new process serving an interface on a certain number of |
| 272 | // threads. |
| 273 | ProcessConnection createRpcTestSocketServerProcess( |
| 274 | size_t numThreads, |
| 275 | const std::function<void(const sp<RpcServer>&, const sp<RpcConnection>&)>& configure) { |
| 276 | CHECK_GT(numThreads, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 277 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 278 | SocketType socketType = GetParam(); |
| 279 | |
| 280 | std::string addr = allocateSocketAddress(); |
| 281 | unlink(addr.c_str()); |
| 282 | static unsigned int port = 3456; |
| 283 | port++; |
| 284 | |
| 285 | auto ret = ProcessConnection{ |
| 286 | .host = Process([&] { |
| 287 | sp<RpcServer> server = RpcServer::make(); |
| 288 | |
| 289 | server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); |
| 290 | |
| 291 | // server supporting one client on one socket |
| 292 | sp<RpcConnection> connection = server->addClientConnection(); |
| 293 | |
| 294 | switch (socketType) { |
| 295 | case SocketType::UNIX: |
| 296 | CHECK(connection->setupUnixDomainServer(addr.c_str())) << addr; |
| 297 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 298 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 299 | case SocketType::VSOCK: |
| 300 | CHECK(connection->setupVsockServer(port)); |
| 301 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 302 | #endif // __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 303 | default: |
| 304 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 305 | } |
| 306 | |
| 307 | configure(server, connection); |
| 308 | |
| 309 | // accept 'numThreads' connections |
| 310 | std::vector<std::thread> pool; |
| 311 | for (size_t i = 0; i + 1 < numThreads; i++) { |
| 312 | pool.push_back(std::thread([=] { connection->join(); })); |
| 313 | } |
| 314 | connection->join(); |
| 315 | for (auto& t : pool) t.join(); |
| 316 | }), |
| 317 | .connection = RpcConnection::make(), |
| 318 | }; |
| 319 | |
| 320 | // create remainder of connections |
| 321 | for (size_t i = 0; i < numThreads; i++) { |
| 322 | for (size_t tries = 0; tries < 5; tries++) { |
| 323 | usleep(10000); |
| 324 | switch (socketType) { |
| 325 | case SocketType::UNIX: |
| 326 | if (ret.connection->addUnixDomainClient(addr.c_str())) goto success; |
| 327 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 328 | #ifdef __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 329 | case SocketType::VSOCK: |
| 330 | if (ret.connection->addVsockClient(VMADDR_CID_LOCAL, port)) goto success; |
| 331 | break; |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 332 | #endif // __BIONIC__ |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 333 | default: |
| 334 | LOG_ALWAYS_FATAL("Unknown socket type"); |
| 335 | } |
| 336 | } |
| 337 | LOG_ALWAYS_FATAL("Could not connect"); |
| 338 | success:; |
| 339 | } |
| 340 | |
| 341 | ret.rootBinder = ret.connection->getRootObject(); |
| 342 | return ret; |
| 343 | } |
| 344 | |
| 345 | BinderRpcTestProcessConnection createRpcTestSocketServerProcess(size_t numThreads) { |
| 346 | BinderRpcTestProcessConnection ret{ |
| 347 | .proc = createRpcTestSocketServerProcess(numThreads, |
| 348 | [&](const sp<RpcServer>& server, |
| 349 | const sp<RpcConnection>& connection) { |
| 350 | sp<MyBinderRpcTest> service = |
| 351 | new MyBinderRpcTest; |
| 352 | server->setRootObject(service); |
| 353 | service->connection = |
| 354 | connection; // for testing only |
| 355 | }), |
| 356 | }; |
| 357 | |
| 358 | ret.rootBinder = ret.proc.rootBinder; |
| 359 | ret.rootIface = interface_cast<IBinderRpcTest>(ret.rootBinder); |
| 360 | |
| 361 | return ret; |
| 362 | } |
| 363 | }; |
| 364 | |
| 365 | TEST_P(BinderRpc, RootObjectIsNull) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 366 | auto proc = createRpcTestSocketServerProcess(1, |
| 367 | [](const sp<RpcServer>& server, |
| 368 | const sp<RpcConnection>&) { |
| 369 | // this is the default, but to be explicit |
| 370 | server->setRootObject(nullptr); |
| 371 | }); |
| 372 | |
| 373 | // retrieved by getRootObject when process is created above |
| 374 | EXPECT_EQ(nullptr, proc.rootBinder); |
| 375 | |
| 376 | // make sure we can retrieve it again (process doesn't crash) |
| 377 | EXPECT_EQ(nullptr, proc.connection->getRootObject()); |
| 378 | } |
| 379 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 380 | TEST_P(BinderRpc, Ping) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 381 | auto proc = createRpcTestSocketServerProcess(1); |
| 382 | ASSERT_NE(proc.rootBinder, nullptr); |
| 383 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 384 | } |
| 385 | |
Steven Moreland | 4cf688f | 2021-03-31 01:48:58 +0000 | [diff] [blame^] | 386 | TEST_P(BinderRpc, GetInterfaceDescriptor) { |
| 387 | auto proc = createRpcTestSocketServerProcess(1); |
| 388 | ASSERT_NE(proc.rootBinder, nullptr); |
| 389 | EXPECT_EQ(IBinderRpcTest::descriptor, proc.rootBinder->getInterfaceDescriptor()); |
| 390 | } |
| 391 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 392 | TEST_P(BinderRpc, TransactionsMustBeMarkedRpc) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 393 | auto proc = createRpcTestSocketServerProcess(1); |
| 394 | Parcel data; |
| 395 | Parcel reply; |
| 396 | EXPECT_EQ(BAD_TYPE, proc.rootBinder->transact(IBinder::PING_TRANSACTION, data, &reply, 0)); |
| 397 | } |
| 398 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 399 | TEST_P(BinderRpc, UnknownTransaction) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 400 | auto proc = createRpcTestSocketServerProcess(1); |
| 401 | Parcel data; |
| 402 | data.markForBinder(proc.rootBinder); |
| 403 | Parcel reply; |
| 404 | EXPECT_EQ(UNKNOWN_TRANSACTION, proc.rootBinder->transact(1337, data, &reply, 0)); |
| 405 | } |
| 406 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 407 | TEST_P(BinderRpc, SendSomethingOneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 408 | auto proc = createRpcTestSocketServerProcess(1); |
| 409 | EXPECT_OK(proc.rootIface->sendString("asdf")); |
| 410 | } |
| 411 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 412 | TEST_P(BinderRpc, SendAndGetResultBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 413 | auto proc = createRpcTestSocketServerProcess(1); |
| 414 | std::string doubled; |
| 415 | EXPECT_OK(proc.rootIface->doubleString("cool ", &doubled)); |
| 416 | EXPECT_EQ("cool cool ", doubled); |
| 417 | } |
| 418 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 419 | TEST_P(BinderRpc, SendAndGetResultBackBig) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 420 | auto proc = createRpcTestSocketServerProcess(1); |
| 421 | std::string single = std::string(1024, 'a'); |
| 422 | std::string doubled; |
| 423 | EXPECT_OK(proc.rootIface->doubleString(single, &doubled)); |
| 424 | EXPECT_EQ(single + single, doubled); |
| 425 | } |
| 426 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 427 | TEST_P(BinderRpc, CallMeBack) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 428 | auto proc = createRpcTestSocketServerProcess(1); |
| 429 | |
| 430 | int32_t pingResult; |
| 431 | EXPECT_OK(proc.rootIface->pingMe(new MyBinderRpcSession("foo"), &pingResult)); |
| 432 | EXPECT_EQ(OK, pingResult); |
| 433 | |
| 434 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 435 | } |
| 436 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 437 | TEST_P(BinderRpc, RepeatBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 438 | auto proc = createRpcTestSocketServerProcess(1); |
| 439 | |
| 440 | sp<IBinder> inBinder = new MyBinderRpcSession("foo"); |
| 441 | sp<IBinder> outBinder; |
| 442 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 443 | EXPECT_EQ(inBinder, outBinder); |
| 444 | |
| 445 | wp<IBinder> weak = inBinder; |
| 446 | inBinder = nullptr; |
| 447 | outBinder = nullptr; |
| 448 | |
| 449 | // Force reading a reply, to process any pending dec refs from the other |
| 450 | // process (the other process will process dec refs there before processing |
| 451 | // the ping here). |
| 452 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 453 | |
| 454 | EXPECT_EQ(nullptr, weak.promote()); |
| 455 | |
| 456 | EXPECT_EQ(0, MyBinderRpcSession::gNum); |
| 457 | } |
| 458 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 459 | TEST_P(BinderRpc, RepeatTheirBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 460 | auto proc = createRpcTestSocketServerProcess(1); |
| 461 | |
| 462 | sp<IBinderRpcSession> session; |
| 463 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 464 | |
| 465 | sp<IBinder> inBinder = IInterface::asBinder(session); |
| 466 | sp<IBinder> outBinder; |
| 467 | EXPECT_OK(proc.rootIface->repeatBinder(inBinder, &outBinder)); |
| 468 | EXPECT_EQ(inBinder, outBinder); |
| 469 | |
| 470 | wp<IBinder> weak = inBinder; |
| 471 | session = nullptr; |
| 472 | inBinder = nullptr; |
| 473 | outBinder = nullptr; |
| 474 | |
| 475 | // Force reading a reply, to process any pending dec refs from the other |
| 476 | // process (the other process will process dec refs there before processing |
| 477 | // the ping here). |
| 478 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 479 | |
| 480 | EXPECT_EQ(nullptr, weak.promote()); |
| 481 | } |
| 482 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 483 | TEST_P(BinderRpc, RepeatBinderNull) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 484 | auto proc = createRpcTestSocketServerProcess(1); |
| 485 | |
| 486 | sp<IBinder> outBinder; |
| 487 | EXPECT_OK(proc.rootIface->repeatBinder(nullptr, &outBinder)); |
| 488 | EXPECT_EQ(nullptr, outBinder); |
| 489 | } |
| 490 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 491 | TEST_P(BinderRpc, HoldBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 492 | auto proc = createRpcTestSocketServerProcess(1); |
| 493 | |
| 494 | IBinder* ptr = nullptr; |
| 495 | { |
| 496 | sp<IBinder> binder = new BBinder(); |
| 497 | ptr = binder.get(); |
| 498 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 499 | } |
| 500 | |
| 501 | sp<IBinder> held; |
| 502 | EXPECT_OK(proc.rootIface->getHeldBinder(&held)); |
| 503 | |
| 504 | EXPECT_EQ(held.get(), ptr); |
| 505 | |
| 506 | // stop holding binder, because we test to make sure references are cleaned |
| 507 | // up |
| 508 | EXPECT_OK(proc.rootIface->holdBinder(nullptr)); |
| 509 | // and flush ref counts |
| 510 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 511 | } |
| 512 | |
| 513 | // START TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 514 | // These are behavioral differences form regular binder, where certain usecases |
| 515 | // aren't supported. |
| 516 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 517 | TEST_P(BinderRpc, CannotMixBindersBetweenUnrelatedSocketConnections) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 518 | auto proc1 = createRpcTestSocketServerProcess(1); |
| 519 | auto proc2 = createRpcTestSocketServerProcess(1); |
| 520 | |
| 521 | sp<IBinder> outBinder; |
| 522 | EXPECT_EQ(INVALID_OPERATION, |
| 523 | proc1.rootIface->repeatBinder(proc2.rootBinder, &outBinder).transactionError()); |
| 524 | } |
| 525 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 526 | TEST_P(BinderRpc, CannotSendRegularBinderOverSocketBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 527 | auto proc = createRpcTestSocketServerProcess(1); |
| 528 | |
| 529 | sp<IBinder> someRealBinder = IInterface::asBinder(defaultServiceManager()); |
| 530 | sp<IBinder> outBinder; |
| 531 | EXPECT_EQ(INVALID_OPERATION, |
| 532 | proc.rootIface->repeatBinder(someRealBinder, &outBinder).transactionError()); |
| 533 | } |
| 534 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 535 | TEST_P(BinderRpc, CannotSendSocketBinderOverRegularBinder) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 536 | auto proc = createRpcTestSocketServerProcess(1); |
| 537 | |
| 538 | // for historical reasons, IServiceManager interface only returns the |
| 539 | // exception code |
| 540 | EXPECT_EQ(binder::Status::EX_TRANSACTION_FAILED, |
| 541 | defaultServiceManager()->addService(String16("not_suspicious"), proc.rootBinder)); |
| 542 | } |
| 543 | |
| 544 | // END TESTS FOR LIMITATIONS OF SOCKET BINDER |
| 545 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 546 | TEST_P(BinderRpc, RepeatRootObject) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 547 | auto proc = createRpcTestSocketServerProcess(1); |
| 548 | |
| 549 | sp<IBinder> outBinder; |
| 550 | EXPECT_OK(proc.rootIface->repeatBinder(proc.rootBinder, &outBinder)); |
| 551 | EXPECT_EQ(proc.rootBinder, outBinder); |
| 552 | } |
| 553 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 554 | TEST_P(BinderRpc, NestedTransactions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 555 | auto proc = createRpcTestSocketServerProcess(1); |
| 556 | |
| 557 | auto nastyNester = sp<MyBinderRpcTest>::make(); |
| 558 | EXPECT_OK(proc.rootIface->nestMe(nastyNester, 10)); |
| 559 | |
| 560 | wp<IBinder> weak = nastyNester; |
| 561 | nastyNester = nullptr; |
| 562 | EXPECT_EQ(nullptr, weak.promote()); |
| 563 | } |
| 564 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 565 | TEST_P(BinderRpc, SameBinderEquality) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 566 | auto proc = createRpcTestSocketServerProcess(1); |
| 567 | |
| 568 | sp<IBinder> a; |
| 569 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 570 | |
| 571 | sp<IBinder> b; |
| 572 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 573 | |
| 574 | EXPECT_EQ(a, b); |
| 575 | } |
| 576 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 577 | TEST_P(BinderRpc, SameBinderEqualityWeak) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 578 | auto proc = createRpcTestSocketServerProcess(1); |
| 579 | |
| 580 | sp<IBinder> a; |
| 581 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&a)); |
| 582 | wp<IBinder> weak = a; |
| 583 | a = nullptr; |
| 584 | |
| 585 | sp<IBinder> b; |
| 586 | EXPECT_OK(proc.rootIface->alwaysGiveMeTheSameBinder(&b)); |
| 587 | |
| 588 | // this is the wrong behavior, since BpBinder |
| 589 | // doesn't implement onIncStrongAttempted |
| 590 | // but make sure there is no crash |
| 591 | EXPECT_EQ(nullptr, weak.promote()); |
| 592 | |
| 593 | GTEST_SKIP() << "Weak binders aren't currently re-promotable for RPC binder."; |
| 594 | |
| 595 | // In order to fix this: |
| 596 | // - need to have incStrongAttempted reflected across IPC boundary (wait for |
| 597 | // response to promote - round trip...) |
| 598 | // - sendOnLastWeakRef, to delete entries out of RpcState table |
| 599 | EXPECT_EQ(b, weak.promote()); |
| 600 | } |
| 601 | |
| 602 | #define expectSessions(expected, iface) \ |
| 603 | do { \ |
| 604 | int session; \ |
| 605 | EXPECT_OK((iface)->getNumOpenSessions(&session)); \ |
| 606 | EXPECT_EQ(expected, session); \ |
| 607 | } while (false) |
| 608 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 609 | TEST_P(BinderRpc, SingleSession) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 610 | auto proc = createRpcTestSocketServerProcess(1); |
| 611 | |
| 612 | sp<IBinderRpcSession> session; |
| 613 | EXPECT_OK(proc.rootIface->openSession("aoeu", &session)); |
| 614 | std::string out; |
| 615 | EXPECT_OK(session->getName(&out)); |
| 616 | EXPECT_EQ("aoeu", out); |
| 617 | |
| 618 | expectSessions(1, proc.rootIface); |
| 619 | session = nullptr; |
| 620 | expectSessions(0, proc.rootIface); |
| 621 | } |
| 622 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 623 | TEST_P(BinderRpc, ManySessions) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 624 | auto proc = createRpcTestSocketServerProcess(1); |
| 625 | |
| 626 | std::vector<sp<IBinderRpcSession>> sessions; |
| 627 | |
| 628 | for (size_t i = 0; i < 15; i++) { |
| 629 | expectSessions(i, proc.rootIface); |
| 630 | sp<IBinderRpcSession> session; |
| 631 | EXPECT_OK(proc.rootIface->openSession(std::to_string(i), &session)); |
| 632 | sessions.push_back(session); |
| 633 | } |
| 634 | expectSessions(sessions.size(), proc.rootIface); |
| 635 | for (size_t i = 0; i < sessions.size(); i++) { |
| 636 | std::string out; |
| 637 | EXPECT_OK(sessions.at(i)->getName(&out)); |
| 638 | EXPECT_EQ(std::to_string(i), out); |
| 639 | } |
| 640 | expectSessions(sessions.size(), proc.rootIface); |
| 641 | |
| 642 | while (!sessions.empty()) { |
| 643 | sessions.pop_back(); |
| 644 | expectSessions(sessions.size(), proc.rootIface); |
| 645 | } |
| 646 | expectSessions(0, proc.rootIface); |
| 647 | } |
| 648 | |
| 649 | size_t epochMillis() { |
| 650 | using std::chrono::duration_cast; |
| 651 | using std::chrono::milliseconds; |
| 652 | using std::chrono::seconds; |
| 653 | using std::chrono::system_clock; |
| 654 | return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count(); |
| 655 | } |
| 656 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 657 | TEST_P(BinderRpc, ThreadPoolGreaterThanEqualRequested) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 658 | constexpr size_t kNumThreads = 10; |
| 659 | |
| 660 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 661 | |
| 662 | EXPECT_OK(proc.rootIface->lock()); |
| 663 | |
| 664 | // block all but one thread taking locks |
| 665 | std::vector<std::thread> ts; |
| 666 | for (size_t i = 0; i < kNumThreads - 1; i++) { |
| 667 | ts.push_back(std::thread([&] { proc.rootIface->lockUnlock(); })); |
| 668 | } |
| 669 | |
| 670 | usleep(100000); // give chance for calls on other threads |
| 671 | |
| 672 | // other calls still work |
| 673 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 674 | |
| 675 | constexpr size_t blockTimeMs = 500; |
| 676 | size_t epochMsBefore = epochMillis(); |
| 677 | // after this, we should never see a response within this time |
| 678 | EXPECT_OK(proc.rootIface->unlockInMsAsync(blockTimeMs)); |
| 679 | |
| 680 | // this call should be blocked for blockTimeMs |
| 681 | EXPECT_EQ(OK, proc.rootBinder->pingBinder()); |
| 682 | |
| 683 | size_t epochMsAfter = epochMillis(); |
| 684 | EXPECT_GE(epochMsAfter, epochMsBefore + blockTimeMs) << epochMsBefore; |
| 685 | |
| 686 | for (auto& t : ts) t.join(); |
| 687 | } |
| 688 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 689 | TEST_P(BinderRpc, ThreadPoolOverSaturated) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 690 | constexpr size_t kNumThreads = 10; |
| 691 | constexpr size_t kNumCalls = kNumThreads + 3; |
| 692 | constexpr size_t kSleepMs = 500; |
| 693 | |
| 694 | auto proc = createRpcTestSocketServerProcess(kNumThreads); |
| 695 | |
| 696 | size_t epochMsBefore = epochMillis(); |
| 697 | |
| 698 | std::vector<std::thread> ts; |
| 699 | for (size_t i = 0; i < kNumCalls; i++) { |
| 700 | ts.push_back(std::thread([&] { proc.rootIface->sleepMs(kSleepMs); })); |
| 701 | } |
| 702 | |
| 703 | for (auto& t : ts) t.join(); |
| 704 | |
| 705 | size_t epochMsAfter = epochMillis(); |
| 706 | |
| 707 | EXPECT_GE(epochMsAfter, epochMsBefore + 2 * kSleepMs); |
| 708 | |
| 709 | // Potential flake, but make sure calls are handled in parallel. |
| 710 | EXPECT_LE(epochMsAfter, epochMsBefore + 3 * kSleepMs); |
| 711 | } |
| 712 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 713 | TEST_P(BinderRpc, ThreadingStressTest) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 714 | constexpr size_t kNumClientThreads = 10; |
| 715 | constexpr size_t kNumServerThreads = 10; |
| 716 | constexpr size_t kNumCalls = 100; |
| 717 | |
| 718 | auto proc = createRpcTestSocketServerProcess(kNumServerThreads); |
| 719 | |
| 720 | std::vector<std::thread> threads; |
| 721 | for (size_t i = 0; i < kNumClientThreads; i++) { |
| 722 | threads.push_back(std::thread([&] { |
| 723 | for (size_t j = 0; j < kNumCalls; j++) { |
| 724 | sp<IBinder> out; |
| 725 | proc.rootIface->repeatBinder(proc.rootBinder, &out); |
| 726 | EXPECT_EQ(proc.rootBinder, out); |
| 727 | } |
| 728 | })); |
| 729 | } |
| 730 | |
| 731 | for (auto& t : threads) t.join(); |
| 732 | } |
| 733 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 734 | TEST_P(BinderRpc, OnewayCallDoesNotWait) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 735 | constexpr size_t kReallyLongTimeMs = 100; |
| 736 | constexpr size_t kSleepMs = kReallyLongTimeMs * 5; |
| 737 | |
| 738 | // more than one thread, just so this doesn't deadlock |
| 739 | auto proc = createRpcTestSocketServerProcess(2); |
| 740 | |
| 741 | size_t epochMsBefore = epochMillis(); |
| 742 | |
| 743 | EXPECT_OK(proc.rootIface->sleepMsAsync(kSleepMs)); |
| 744 | |
| 745 | size_t epochMsAfter = epochMillis(); |
| 746 | EXPECT_LT(epochMsAfter, epochMsBefore + kReallyLongTimeMs); |
| 747 | } |
| 748 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 749 | TEST_P(BinderRpc, OnewayCallQueueing) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 750 | constexpr size_t kNumSleeps = 10; |
| 751 | constexpr size_t kNumExtraServerThreads = 4; |
| 752 | constexpr size_t kSleepMs = 50; |
| 753 | |
| 754 | // make sure calls to the same object happen on the same thread |
| 755 | auto proc = createRpcTestSocketServerProcess(1 + kNumExtraServerThreads); |
| 756 | |
| 757 | EXPECT_OK(proc.rootIface->lock()); |
| 758 | |
| 759 | for (size_t i = 0; i < kNumSleeps; i++) { |
| 760 | // these should be processed serially |
| 761 | proc.rootIface->sleepMsAsync(kSleepMs); |
| 762 | } |
| 763 | // should also be processesed serially |
| 764 | EXPECT_OK(proc.rootIface->unlockInMsAsync(kSleepMs)); |
| 765 | |
| 766 | size_t epochMsBefore = epochMillis(); |
| 767 | EXPECT_OK(proc.rootIface->lockUnlock()); |
| 768 | size_t epochMsAfter = epochMillis(); |
| 769 | |
| 770 | EXPECT_GT(epochMsAfter, epochMsBefore + kSleepMs * kNumSleeps); |
| 771 | } |
| 772 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 773 | TEST_P(BinderRpc, Die) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 774 | // TODO(b/183141167): handle this in library |
| 775 | signal(SIGPIPE, SIG_IGN); |
| 776 | |
| 777 | for (bool doDeathCleanup : {true, false}) { |
| 778 | auto proc = createRpcTestSocketServerProcess(1); |
| 779 | |
| 780 | // make sure there is some state during crash |
| 781 | // 1. we hold their binder |
| 782 | sp<IBinderRpcSession> session; |
| 783 | EXPECT_OK(proc.rootIface->openSession("happy", &session)); |
| 784 | // 2. they hold our binder |
| 785 | sp<IBinder> binder = new BBinder(); |
| 786 | EXPECT_OK(proc.rootIface->holdBinder(binder)); |
| 787 | |
| 788 | EXPECT_EQ(DEAD_OBJECT, proc.rootIface->die(doDeathCleanup).transactionError()) |
| 789 | << "Do death cleanup: " << doDeathCleanup; |
| 790 | |
| 791 | proc.proc.expectInvalid = true; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | ssize_t countFds() { |
| 796 | DIR* dir = opendir("/proc/self/fd/"); |
| 797 | if (dir == nullptr) return -1; |
| 798 | ssize_t ret = 0; |
| 799 | dirent* ent; |
| 800 | while ((ent = readdir(dir)) != nullptr) ret++; |
| 801 | closedir(dir); |
| 802 | return ret; |
| 803 | } |
| 804 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 805 | TEST_P(BinderRpc, Fds) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 806 | ssize_t beforeFds = countFds(); |
| 807 | ASSERT_GE(beforeFds, 0); |
| 808 | { |
| 809 | auto proc = createRpcTestSocketServerProcess(10); |
| 810 | ASSERT_EQ(OK, proc.rootBinder->pingBinder()); |
| 811 | } |
| 812 | ASSERT_EQ(beforeFds, countFds()) << (system("ls -l /proc/self/fd/"), "fd leak?"); |
| 813 | } |
| 814 | |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 815 | INSTANTIATE_TEST_CASE_P(PerSocket, BinderRpc, |
Steven Moreland | f6ec463 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 816 | ::testing::Values(SocketType::UNIX |
| 817 | #ifdef __BIONIC__ |
| 818 | , |
| 819 | SocketType::VSOCK |
| 820 | #endif // __BIONIC__ |
| 821 | ), |
| 822 | PrintSocketType); |
Steven Moreland | c163595 | 2021-04-01 16:20:47 +0000 | [diff] [blame] | 823 | |
| 824 | } // namespace android |
| 825 | |
| 826 | int main(int argc, char** argv) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 827 | ::testing::InitGoogleTest(&argc, argv); |
| 828 | android::base::InitLogging(argv, android::base::StderrLogger, android::base::DefaultAborter); |
| 829 | return RUN_ALL_TESTS(); |
| 830 | } |