diff options
author | 2021-08-05 15:42:01 -0700 | |
---|---|---|
committer | 2021-08-06 10:09:43 -0700 | |
commit | 2372f9d850837de10d19453088cb32f1939e46dc (patch) | |
tree | 80fd01f0864442056ffd639b1a5f7d3025cbf97b | |
parent | 176a33a2b48a6f0da85cd1eb820577fea3a60551 (diff) |
libbinder: RPC use 'status_t' over 'bool'
For ease of debugging/handling errors (I realized that binderRpcTest
currently crashes on Pixel 3 because of some missing kernel patches -
this would have been easier with this, and one potential (even if
temporary) solution would be to check the return code).
Bug: 167966510
Test: binderRpcTest, binderRpcBenchmark, binder_parcel_fuzzer,
binder_rpc_fuzzer, binderHostDeviceTest
Change-Id: I1baa2e9380e0ec8f82f8ceb250f3eeb632dc5fbc
-rw-r--r-- | libs/binder/Binder.cpp | 4 | ||||
-rw-r--r-- | libs/binder/RpcServer.cpp | 43 | ||||
-rw-r--r-- | libs/binder/RpcSession.cpp | 97 | ||||
-rw-r--r-- | libs/binder/ServiceManagerHost.cpp | 6 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcServer.h | 12 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcSession.h | 32 | ||||
-rw-r--r-- | libs/binder/libbinder_rpc_unstable.cpp | 13 | ||||
-rw-r--r-- | libs/binder/servicedispatcher.cpp | 9 | ||||
-rw-r--r-- | libs/binder/tests/binderHostDeviceTest.cpp | 5 | ||||
-rw-r--r-- | libs/binder/tests/binderLibTest.cpp | 4 | ||||
-rw-r--r-- | libs/binder/tests/binderRpcBenchmark.cpp | 10 | ||||
-rw-r--r-- | libs/binder/tests/binderRpcTest.cpp | 43 | ||||
-rw-r--r-- | libs/binder/tests/parcel_fuzzer/random_parcel.cpp | 2 | ||||
-rw-r--r-- | libs/binder/tests/rpc_fuzzer/main.cpp | 2 |
14 files changed, 157 insertions, 125 deletions
diff --git a/libs/binder/Binder.cpp b/libs/binder/Binder.cpp index 628381cc19..d3eef4e88e 100644 --- a/libs/binder/Binder.cpp +++ b/libs/binder/Binder.cpp @@ -555,7 +555,9 @@ status_t BBinder::setRpcClientDebug(android::base::unique_fd socketFd, return status; } rpcServer->setRootObjectWeak(weakThis); - rpcServer->setupExternalServer(std::move(socketFd)); + if (auto status = rpcServer->setupExternalServer(std::move(socketFd)); status != OK) { + return status; + } rpcServer->setMaxThreads(binderThreadPoolMaxCount); rpcServer->start(); e->mRpcServerLinks.emplace(link); diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 879e462e8f..4fa99c0e45 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -55,25 +55,25 @@ void RpcServer::iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction() mAgreedExperimental = true; } -bool RpcServer::setupUnixDomainServer(const char* path) { +status_t RpcServer::setupUnixDomainServer(const char* path) { return setupSocketServer(UnixSocketAddress(path)); } -bool RpcServer::setupVsockServer(unsigned int port) { +status_t RpcServer::setupVsockServer(unsigned int port) { // realizing value w/ this type at compile time to avoid ubsan abort constexpr unsigned int kAnyCid = VMADDR_CID_ANY; return setupSocketServer(VsockSocketAddress(kAnyCid, port)); } -bool RpcServer::setupInetServer(const char* address, unsigned int port, - unsigned int* assignedPort) { +status_t RpcServer::setupInetServer(const char* address, unsigned int port, + unsigned int* assignedPort) { if (assignedPort != nullptr) *assignedPort = 0; auto aiStart = InetSocketAddress::getAddrInfo(address, port); - if (aiStart == nullptr) return false; + if (aiStart == nullptr) return UNKNOWN_ERROR; for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, address, port); - if (!setupSocketServer(socketAddress)) { + if (status_t status = setupSocketServer(socketAddress); status != OK) { continue; } @@ -84,7 +84,7 @@ bool RpcServer::setupInetServer(const char* address, unsigned int port, int savedErrno = errno; ALOGE("Could not getsockname at %s: %s", socketAddress.toString().c_str(), strerror(savedErrno)); - return false; + return -savedErrno; } LOG_ALWAYS_FATAL_IF(len != sizeof(addr), "Wrong socket type: len %zu vs len %zu", static_cast<size_t>(len), sizeof(addr)); @@ -97,11 +97,11 @@ bool RpcServer::setupInetServer(const char* address, unsigned int port, *assignedPort = realPort; } - return true; + return OK; } ALOGE("None of the socket address resolved for %s:%u can be set up as inet server.", address, port); - return false; + return UNKNOWN_ERROR; } void RpcServer::setMaxThreads(size_t threads) { @@ -366,7 +366,7 @@ void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clie } if (incoming) { - LOG_ALWAYS_FATAL_IF(!session->addOutgoingConnection(std::move(client), true), + LOG_ALWAYS_FATAL_IF(OK != session->addOutgoingConnection(std::move(client), true), "server state must already be initialized"); return; } @@ -383,21 +383,22 @@ void RpcServer::establishConnection(sp<RpcServer>&& server, base::unique_fd clie RpcSession::join(std::move(session), std::move(setupResult)); } -bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) { +status_t RpcServer::setupSocketServer(const RpcSocketAddress& addr) { LOG_RPC_DETAIL("Setting up socket server %s", addr.toString().c_str()); LOG_ALWAYS_FATAL_IF(hasServer(), "Each RpcServer can only have one server."); unique_fd serverFd( TEMP_FAILURE_RETRY(socket(addr.addr()->sa_family, SOCK_STREAM | SOCK_CLOEXEC, 0))); if (serverFd == -1) { - ALOGE("Could not create socket: %s", strerror(errno)); - return false; + int savedErrno = errno; + ALOGE("Could not create socket: %s", strerror(savedErrno)); + return -savedErrno; } if (0 != TEMP_FAILURE_RETRY(bind(serverFd.get(), addr.addr(), addr.addrSize()))) { int savedErrno = errno; ALOGE("Could not bind socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); - return false; + return -savedErrno; } // Right now, we create all threads at once, making accept4 slow. To avoid hanging the client, @@ -407,16 +408,16 @@ bool RpcServer::setupSocketServer(const RpcSocketAddress& addr) { if (0 != TEMP_FAILURE_RETRY(listen(serverFd.get(), 50 /*backlog*/))) { int savedErrno = errno; ALOGE("Could not listen socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); - return false; + return -savedErrno; } LOG_RPC_DETAIL("Successfully setup socket server %s", addr.toString().c_str()); - if (!setupExternalServer(std::move(serverFd))) { + if (status_t status = setupExternalServer(std::move(serverFd)); status != OK) { ALOGE("Another thread has set up server while calling setupSocketServer. Race?"); - return false; + return status; } - return true; + return OK; } void RpcServer::onSessionAllIncomingThreadsEnded(const sp<RpcSession>& session) { @@ -449,15 +450,15 @@ unique_fd RpcServer::releaseServer() { return std::move(mServer); } -bool RpcServer::setupExternalServer(base::unique_fd serverFd) { +status_t RpcServer::setupExternalServer(base::unique_fd serverFd) { LOG_ALWAYS_FATAL_IF(!mAgreedExperimental, "no!"); std::lock_guard<std::mutex> _l(mLock); if (mServer.ok()) { ALOGE("Each RpcServer can only have one server."); - return false; + return INVALID_OPERATION; } mServer = std::move(serverFd); - return true; + return OK; } } // namespace android diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index 7565f1b53c..11dc1ab862 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -107,54 +107,55 @@ std::optional<uint32_t> RpcSession::getProtocolVersion() { return mProtocolVersion; } -bool RpcSession::setupUnixDomainClient(const char* path) { +status_t RpcSession::setupUnixDomainClient(const char* path) { return setupSocketClient(UnixSocketAddress(path)); } -bool RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { +status_t RpcSession::setupVsockClient(unsigned int cid, unsigned int port) { return setupSocketClient(VsockSocketAddress(cid, port)); } -bool RpcSession::setupInetClient(const char* addr, unsigned int port) { +status_t RpcSession::setupInetClient(const char* addr, unsigned int port) { auto aiStart = InetSocketAddress::getAddrInfo(addr, port); - if (aiStart == nullptr) return false; + if (aiStart == nullptr) return UNKNOWN_ERROR; for (auto ai = aiStart.get(); ai != nullptr; ai = ai->ai_next) { InetSocketAddress socketAddress(ai->ai_addr, ai->ai_addrlen, addr, port); - if (setupSocketClient(socketAddress)) return true; + if (status_t status = setupSocketClient(socketAddress); status == OK) return OK; } ALOGE("None of the socket address resolved for %s:%u can be added as inet client.", addr, port); - return false; + return NAME_NOT_FOUND; } -bool RpcSession::setupPreconnectedClient(unique_fd fd, std::function<unique_fd()>&& request) { - return setupClient([&](const RpcAddress& sessionId, bool incoming) { +status_t RpcSession::setupPreconnectedClient(unique_fd fd, std::function<unique_fd()>&& request) { + return setupClient([&](const RpcAddress& sessionId, bool incoming) -> status_t { // std::move'd from fd becomes -1 (!ok()) if (!fd.ok()) { fd = request(); - if (!fd.ok()) return false; + if (!fd.ok()) return BAD_VALUE; } return initAndAddConnection(std::move(fd), sessionId, incoming); }); } -bool RpcSession::addNullDebuggingClient() { +status_t RpcSession::addNullDebuggingClient() { // Note: only works on raw sockets. unique_fd serverFd(TEMP_FAILURE_RETRY(open("/dev/null", O_WRONLY | O_CLOEXEC))); if (serverFd == -1) { - ALOGE("Could not connect to /dev/null: %s", strerror(errno)); - return false; + int savedErrno = errno; + ALOGE("Could not connect to /dev/null: %s", strerror(savedErrno)); + return -savedErrno; } auto ctx = mRpcTransportCtxFactory->newClientCtx(); if (ctx == nullptr) { ALOGE("Unable to create RpcTransportCtx for null debugging client"); - return false; + return NO_MEMORY; } auto server = ctx->newTransport(std::move(serverFd)); if (server == nullptr) { ALOGE("Unable to set up RpcTransport"); - return false; + return UNKNOWN_ERROR; } return addOutgoingConnection(std::move(server), false); } @@ -475,8 +476,8 @@ sp<RpcServer> RpcSession::server() { return server; } -bool RpcSession::setupClient( - const std::function<bool(const RpcAddress& sessionId, bool incoming)>& connectAndInit) { +status_t RpcSession::setupClient( + const std::function<status_t(const RpcAddress& sessionId, bool incoming)>& connectAndInit) { { std::lock_guard<std::mutex> _l(mMutex); LOG_ALWAYS_FATAL_IF(mOutgoingConnections.size() != 0, @@ -484,18 +485,23 @@ bool RpcSession::setupClient( mOutgoingConnections.size()); } - if (!connectAndInit(RpcAddress::zero(), false /*incoming*/)) return false; + if (status_t status = connectAndInit(RpcAddress::zero(), false /*incoming*/); status != OK) + return status; { ExclusiveConnection connection; - status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), - ConnectionUse::CLIENT, &connection); - if (status != OK) return false; + if (status_t status = ExclusiveConnection::find(sp<RpcSession>::fromExisting(this), + ConnectionUse::CLIENT, &connection); + status != OK) + return status; uint32_t version; - status = state()->readNewSessionResponse(connection.get(), - sp<RpcSession>::fromExisting(this), &version); - if (!setProtocolVersion(version)) return false; + if (status_t status = + state()->readNewSessionResponse(connection.get(), + sp<RpcSession>::fromExisting(this), &version); + status != OK) + return status; + if (!setProtocolVersion(version)) return BAD_VALUE; } // TODO(b/189955605): we should add additional sessions dynamically @@ -505,13 +511,13 @@ bool RpcSession::setupClient( if (status_t status = getRemoteMaxThreads(&numThreadsAvailable); status != OK) { ALOGE("Could not get max threads after initial session setup: %s", statusToString(status).c_str()); - return false; + return status; } if (status_t status = readId(); status != OK) { ALOGE("Could not get session id after initial session setup: %s", statusToString(status).c_str()); - return false; + return status; } // TODO(b/189955605): we should add additional sessions dynamically @@ -522,24 +528,26 @@ bool RpcSession::setupClient( // we've already setup one client for (size_t i = 0; i + 1 < numThreadsAvailable; i++) { - if (!connectAndInit(mId.value(), false /*incoming*/)) return false; + if (status_t status = connectAndInit(mId.value(), false /*incoming*/); status != OK) + return status; } for (size_t i = 0; i < mMaxThreads; i++) { - if (!connectAndInit(mId.value(), true /*incoming*/)) return false; + if (status_t status = connectAndInit(mId.value(), true /*incoming*/); status != OK) + return status; } - return true; + return OK; } -bool RpcSession::setupSocketClient(const RpcSocketAddress& addr) { +status_t RpcSession::setupSocketClient(const RpcSocketAddress& addr) { return setupClient([&](const RpcAddress& sessionId, bool incoming) { return setupOneSocketConnection(addr, sessionId, incoming); }); } -bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const RpcAddress& sessionId, - bool incoming) { +status_t RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, + const RpcAddress& sessionId, bool incoming) { for (size_t tries = 0; tries < 5; tries++) { if (tries > 0) usleep(10000); @@ -549,7 +557,7 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp int savedErrno = errno; ALOGE("Could not create socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); - return false; + return -savedErrno; } if (0 != TEMP_FAILURE_RETRY(connect(serverFd.get(), addr.addr(), addr.addrSize()))) { @@ -560,7 +568,7 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp int savedErrno = errno; ALOGE("Could not connect socket at %s: %s", addr.toString().c_str(), strerror(savedErrno)); - return false; + return -savedErrno; } LOG_RPC_DETAIL("Socket at %s client with fd %d", addr.toString().c_str(), serverFd.get()); @@ -568,20 +576,21 @@ bool RpcSession::setupOneSocketConnection(const RpcSocketAddress& addr, const Rp } ALOGE("Ran out of retries to connect to %s", addr.toString().c_str()); - return false; + return UNKNOWN_ERROR; } -bool RpcSession::initAndAddConnection(unique_fd fd, const RpcAddress& sessionId, bool incoming) { +status_t RpcSession::initAndAddConnection(unique_fd fd, const RpcAddress& sessionId, + bool incoming) { auto ctx = mRpcTransportCtxFactory->newClientCtx(); if (ctx == nullptr) { ALOGE("Unable to create client RpcTransportCtx with %s sockets", mRpcTransportCtxFactory->toCString()); - return false; + return NO_MEMORY; } auto server = ctx->newTransport(std::move(fd)); if (server == nullptr) { ALOGE("Unable to set up RpcTransport in %s context", mRpcTransportCtxFactory->toCString()); - return false; + return UNKNOWN_ERROR; } LOG_RPC_DETAIL("Socket at client with RpcTransport %p", server.get()); @@ -598,12 +607,12 @@ bool RpcSession::initAndAddConnection(unique_fd fd, const RpcAddress& sessionId, if (!sentHeader.ok()) { ALOGE("Could not write connection header to socket: %s", sentHeader.error().message().c_str()); - return false; + return -sentHeader.error().code(); } if (*sentHeader != sizeof(header)) { ALOGE("Could not write connection header to socket: sent %zd bytes, expected %zd", *sentHeader, sizeof(header)); - return false; + return UNKNOWN_ERROR; } LOG_RPC_DETAIL("Socket at client: header sent"); @@ -615,7 +624,7 @@ bool RpcSession::initAndAddConnection(unique_fd fd, const RpcAddress& sessionId, } } -bool RpcSession::addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport) { +status_t RpcSession::addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport) { std::mutex mutex; std::condition_variable joinCv; std::unique_lock<std::mutex> lock(mutex); @@ -641,10 +650,10 @@ bool RpcSession::addIncomingConnection(std::unique_ptr<RpcTransport> rpcTranspor }); joinCv.wait(lock, [&] { return ownershipTransferred; }); LOG_ALWAYS_FATAL_IF(!ownershipTransferred); - return true; + return OK; } -bool RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, bool init) { +status_t RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, bool init) { sp<RpcConnection> connection = sp<RpcConnection>::make(); { std::lock_guard<std::mutex> _l(mMutex); @@ -654,7 +663,7 @@ bool RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTranspor if (mShutdownTrigger == nullptr) { mShutdownTrigger = FdTrigger::make(); mEventListener = mShutdownListener = sp<WaitForShutdownListener>::make(); - if (mShutdownTrigger == nullptr) return false; + if (mShutdownTrigger == nullptr) return INVALID_OPERATION; } connection->rpcTransport = std::move(rpcTransport); @@ -672,7 +681,7 @@ bool RpcSession::addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTranspor connection->exclusiveTid = std::nullopt; } - return status == OK; + return status; } bool RpcSession::setForServer(const wp<RpcServer>& server, const wp<EventListener>& eventListener, diff --git a/libs/binder/ServiceManagerHost.cpp b/libs/binder/ServiceManagerHost.cpp index 1c2f9b4314..59334b7542 100644 --- a/libs/binder/ServiceManagerHost.cpp +++ b/libs/binder/ServiceManagerHost.cpp @@ -158,8 +158,10 @@ sp<IBinder> getDeviceService(std::vector<std::string>&& serviceDispatcherArgs) { LOG_ALWAYS_FATAL_IF(!forwardResult->hostPort().has_value()); auto rpcSession = RpcSession::make(); - if (!rpcSession->setupInetClient("127.0.0.1", *forwardResult->hostPort())) { - ALOGE("Unable to set up inet client on host port %u", *forwardResult->hostPort()); + if (status_t status = rpcSession->setupInetClient("127.0.0.1", *forwardResult->hostPort()); + status != OK) { + ALOGE("Unable to set up inet client on host port %u: %s", *forwardResult->hostPort(), + statusToString(status).c_str()); return nullptr; } auto binder = rpcSession->getRootObject(); diff --git a/libs/binder/include/binder/RpcServer.h b/libs/binder/include/binder/RpcServer.h index 4abf3b914c..f79d85f5af 100644 --- a/libs/binder/include/binder/RpcServer.h +++ b/libs/binder/include/binder/RpcServer.h @@ -59,12 +59,12 @@ public: * process B makes binder b and sends it to A * A uses this 'back session' to send things back to B */ - [[nodiscard]] bool setupUnixDomainServer(const char* path); + [[nodiscard]] status_t setupUnixDomainServer(const char* path); /** * Creates an RPC server at the current port. */ - [[nodiscard]] bool setupVsockServer(unsigned int port); + [[nodiscard]] status_t setupVsockServer(unsigned int port); /** * Creates an RPC server at the current port using IPv4. @@ -80,8 +80,8 @@ public: * "0.0.0.0" allows for connections on any IP address that the device may * have */ - [[nodiscard]] bool setupInetServer(const char* address, unsigned int port, - unsigned int* assignedPort); + [[nodiscard]] status_t setupInetServer(const char* address, unsigned int port, + unsigned int* assignedPort); /** * If setup*Server has been successful, return true. Otherwise return false. @@ -97,7 +97,7 @@ public: * Set up server using an external FD previously set up by releaseServer(). * Return false if there's already a server. */ - bool setupExternalServer(base::unique_fd serverFd); + [[nodiscard]] status_t setupExternalServer(base::unique_fd serverFd); void iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); @@ -175,7 +175,7 @@ private: void onSessionIncomingThreadEnded() override; static void establishConnection(sp<RpcServer>&& server, base::unique_fd clientFd); - bool setupSocketServer(const RpcSocketAddress& address); + status_t setupSocketServer(const RpcSocketAddress& address); const std::unique_ptr<RpcTransportCtxFactory> mRpcTransportCtxFactory; bool mAgreedExperimental = false; diff --git a/libs/binder/include/binder/RpcSession.h b/libs/binder/include/binder/RpcSession.h index 0b46606d63..7ed6e4373e 100644 --- a/libs/binder/include/binder/RpcSession.h +++ b/libs/binder/include/binder/RpcSession.h @@ -78,17 +78,17 @@ public: * This should be called once per thread, matching 'join' in the remote * process. */ - [[nodiscard]] bool setupUnixDomainClient(const char* path); + [[nodiscard]] status_t setupUnixDomainClient(const char* path); /** * Connects to an RPC server at the CVD & port. */ - [[nodiscard]] bool setupVsockClient(unsigned int cvd, unsigned int port); + [[nodiscard]] status_t setupVsockClient(unsigned int cvd, unsigned int port); /** * Connects to an RPC server at the given address and port. */ - [[nodiscard]] bool setupInetClient(const char* addr, unsigned int port); + [[nodiscard]] status_t setupInetClient(const char* addr, unsigned int port); /** * Starts talking to an RPC server which has already been connected to. This @@ -100,8 +100,8 @@ public: * * For future compatibility, 'request' should not reference any stack data. */ - [[nodiscard]] bool setupPreconnectedClient(base::unique_fd fd, - std::function<base::unique_fd()>&& request); + [[nodiscard]] status_t setupPreconnectedClient(base::unique_fd fd, + std::function<base::unique_fd()>&& request); /** * For debugging! @@ -110,7 +110,7 @@ public: * response will never be satisfied. All data sent here will be * unceremoniously cast down the bottomless pit, /dev/null. */ - [[nodiscard]] bool addNullDebuggingClient(); + [[nodiscard]] status_t addNullDebuggingClient(); /** * Query the other side of the session for the root object hosted by that @@ -253,15 +253,17 @@ private: // join on thread passed to preJoinThreadOwnership static void join(sp<RpcSession>&& session, PreJoinSetupResult&& result); - [[nodiscard]] bool setupClient( - const std::function<bool(const RpcAddress& sessionId, bool incoming)>& connectAndInit); - [[nodiscard]] bool setupSocketClient(const RpcSocketAddress& address); - [[nodiscard]] bool setupOneSocketConnection(const RpcSocketAddress& address, - const RpcAddress& sessionId, bool incoming); - [[nodiscard]] bool initAndAddConnection(base::unique_fd fd, const RpcAddress& sessionId, - bool incoming); - [[nodiscard]] bool addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport); - [[nodiscard]] bool addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, bool init); + [[nodiscard]] status_t setupClient( + const std::function<status_t(const RpcAddress& sessionId, bool incoming)>& + connectAndInit); + [[nodiscard]] status_t setupSocketClient(const RpcSocketAddress& address); + [[nodiscard]] status_t setupOneSocketConnection(const RpcSocketAddress& address, + const RpcAddress& sessionId, bool incoming); + [[nodiscard]] status_t initAndAddConnection(base::unique_fd fd, const RpcAddress& sessionId, + bool incoming); + [[nodiscard]] status_t addIncomingConnection(std::unique_ptr<RpcTransport> rpcTransport); + [[nodiscard]] status_t addOutgoingConnection(std::unique_ptr<RpcTransport> rpcTransport, + bool init); [[nodiscard]] bool setForServer(const wp<RpcServer>& server, const wp<RpcSession::EventListener>& eventListener, const RpcAddress& sessionId); diff --git a/libs/binder/libbinder_rpc_unstable.cpp b/libs/binder/libbinder_rpc_unstable.cpp index 68ec669c31..bcb13aebdd 100644 --- a/libs/binder/libbinder_rpc_unstable.cpp +++ b/libs/binder/libbinder_rpc_unstable.cpp @@ -19,16 +19,20 @@ #include <binder/RpcServer.h> #include <binder/RpcSession.h> +using android::OK; using android::RpcServer; using android::RpcSession; +using android::status_t; +using android::statusToString; extern "C" { bool RunRpcServer(AIBinder* service, unsigned int port) { auto server = RpcServer::make(); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); - if (!server->setupVsockServer(port)) { - LOG(ERROR) << "Failed to set up vsock server with port " << port; + if (status_t status = server->setupVsockServer(port); status != OK) { + LOG(ERROR) << "Failed to set up vsock server with port " << port + << " error: " << statusToString(status).c_str(); return false; } server->setRootObject(AIBinder_toPlatformBinder(service)); @@ -41,8 +45,9 @@ bool RunRpcServer(AIBinder* service, unsigned int port) { AIBinder* RpcClient(unsigned int cid, unsigned int port) { auto session = RpcSession::make(); - if (!session->setupVsockClient(cid, port)) { - LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port; + if (status_t status = session->setupVsockClient(cid, port); status != OK) { + LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port + << " error: " << statusToString(status).c_str(); return nullptr; } return AIBinder_fromPlatformBinder(session->getRootObject()); diff --git a/libs/binder/servicedispatcher.cpp b/libs/binder/servicedispatcher.cpp index a6e3f7d1f5..48fc60ac77 100644 --- a/libs/binder/servicedispatcher.cpp +++ b/libs/binder/servicedispatcher.cpp @@ -33,6 +33,7 @@ using android::defaultServiceManager; using android::OK; using android::RpcServer; using android::sp; +using android::status_t; using android::statusToString; using android::String16; using android::base::Basename; @@ -87,8 +88,8 @@ int Dispatch(const char* name, const ServiceRetriever& serviceRetriever) { } rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); unsigned int port; - if (!rpcServer->setupInetServer(kLocalInetAddress, 0, &port)) { - LOG(ERROR) << "setupInetServer failed"; + if (status_t status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port); status != OK) { + LOG(ERROR) << "setupInetServer failed: " << statusToString(status); return EX_SOFTWARE; } auto socket = rpcServer->releaseServer(); @@ -200,8 +201,8 @@ int wrapServiceManager(const ServiceRetriever& serviceRetriever) { rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); rpcServer->setRootObject(service); unsigned int port; - if (!rpcServer->setupInetServer(kLocalInetAddress, 0, &port)) { - LOG(ERROR) << "Unable to set up inet server"; + if (status_t status = rpcServer->setupInetServer(kLocalInetAddress, 0, &port); status != OK) { + LOG(ERROR) << "Unable to set up inet server: " << statusToString(status); return EX_SOFTWARE; } LOG(INFO) << "Finish wrapping servicemanager with RPC on port " << port; diff --git a/libs/binder/tests/binderHostDeviceTest.cpp b/libs/binder/tests/binderHostDeviceTest.cpp index 5dd92121ba..3f72b8f58a 100644 --- a/libs/binder/tests/binderHostDeviceTest.cpp +++ b/libs/binder/tests/binderHostDeviceTest.cpp @@ -101,8 +101,9 @@ public: [[nodiscard]] static sp<IBinder> get(unsigned int hostPort) { auto rpcSession = RpcSession::make(); - if (!rpcSession->setupInetClient("127.0.0.1", hostPort)) { - ADD_FAILURE() << "Failed to setupInetClient on " << hostPort; + if (status_t status = rpcSession->setupInetClient("127.0.0.1", hostPort); status != OK) { + ADD_FAILURE() << "Failed to setupInetClient on " << hostPort << ": " + << statusToString(status); return nullptr; } return rpcSession->getRootObject(); diff --git a/libs/binder/tests/binderLibTest.cpp b/libs/binder/tests/binderLibTest.cpp index 65db7f65cf..eea7d8c108 100644 --- a/libs/binder/tests/binderLibTest.cpp +++ b/libs/binder/tests/binderLibTest.cpp @@ -1192,8 +1192,8 @@ public: if (rpcServer == nullptr) return {}; rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); unsigned int port; - if (!rpcServer->setupInetServer("127.0.0.1", 0, &port)) { - ADD_FAILURE() << "setupInetServer failed"; + if (status_t status = rpcServer->setupInetServer("127.0.0.1", 0, &port); status != OK) { + ADD_FAILURE() << "setupInetServer failed" << statusToString(status); return {}; } return {rpcServer->releaseServer(), port}; diff --git a/libs/binder/tests/binderRpcBenchmark.cpp b/libs/binder/tests/binderRpcBenchmark.cpp index 5f4a7b5592..8d75a531ec 100644 --- a/libs/binder/tests/binderRpcBenchmark.cpp +++ b/libs/binder/tests/binderRpcBenchmark.cpp @@ -42,6 +42,8 @@ using android::ProcessState; using android::RpcServer; using android::RpcSession; using android::sp; +using android::status_t; +using android::statusToString; using android::String16; using android::binder::Status; @@ -160,7 +162,7 @@ int main(int argc, char** argv) { sp<RpcServer> server = RpcServer::make(); server->setRootObject(sp<MyBinderRpcBenchmark>::make()); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); - CHECK(server->setupUnixDomainServer(addr.c_str())); + CHECK_EQ(OK, server->setupUnixDomainServer(addr.c_str())); server->join(); exit(1); } @@ -182,11 +184,13 @@ int main(int argc, char** argv) { CHECK_NE(nullptr, gKernelBinder.get()); #endif + status_t status; for (size_t tries = 0; tries < 5; tries++) { usleep(10000); - if (gSession->setupUnixDomainClient(addr.c_str())) goto success; + status = gSession->setupUnixDomainClient(addr.c_str()); + if (status == OK) goto success; } - LOG(FATAL) << "Could not connect."; + LOG(FATAL) << "Could not connect: " << statusToString(status).c_str(); success: ::benchmark::RunSpecifiedBenchmarks(); diff --git a/libs/binder/tests/binderRpcTest.cpp b/libs/binder/tests/binderRpcTest.cpp index 36a6804bcd..6dd4019ae8 100644 --- a/libs/binder/tests/binderRpcTest.cpp +++ b/libs/binder/tests/binderRpcTest.cpp @@ -89,7 +89,7 @@ TEST_P(BinderRpcSimple, SetExternalServerTest) { auto server = RpcServer::make(newFactory(GetParam())); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); ASSERT_FALSE(server->hasServer()); - ASSERT_TRUE(server->setupExternalServer(std::move(sink))); + ASSERT_EQ(OK, server->setupExternalServer(std::move(sink))); ASSERT_TRUE(server->hasServer()); base::unique_fd retrieved = server->releaseServer(); ASSERT_FALSE(server->hasServer()); @@ -484,13 +484,13 @@ public: case SocketType::PRECONNECTED: [[fallthrough]]; case SocketType::UNIX: - CHECK(server->setupUnixDomainServer(addr.c_str())) << addr; + CHECK_EQ(OK, server->setupUnixDomainServer(addr.c_str())) << addr; break; case SocketType::VSOCK: - CHECK(server->setupVsockServer(vsockPort)); + CHECK_EQ(OK, server->setupVsockServer(vsockPort)); break; case SocketType::INET: { - CHECK(server->setupInetServer(kLocalInetAddress, 0, &outPort)); + CHECK_EQ(OK, server->setupInetServer(kLocalInetAddress, 0, &outPort)); CHECK_NE(0, outPort); break; } @@ -516,30 +516,35 @@ public: CHECK_NE(0, outPort); } + status_t status; + for (size_t i = 0; i < options.numSessions; i++) { sp<RpcSession> session = RpcSession::make(newFactory(rpcSecurity)); session->setMaxThreads(options.numIncomingConnections); switch (socketType) { case SocketType::PRECONNECTED: - if (session->setupPreconnectedClient({}, [=]() { - return connectToUds(addr.c_str()); - })) - goto success; + status = session->setupPreconnectedClient({}, [=]() { + return connectToUds(addr.c_str()); + }); + if (status == OK) goto success; break; case SocketType::UNIX: - if (session->setupUnixDomainClient(addr.c_str())) goto success; + status = session->setupUnixDomainClient(addr.c_str()); + if (status == OK) goto success; break; case SocketType::VSOCK: - if (session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort)) goto success; + status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); + if (status == OK) goto success; break; case SocketType::INET: - if (session->setupInetClient("127.0.0.1", outPort)) goto success; + status = session->setupInetClient("127.0.0.1", outPort); + if (status == OK) goto success; break; default: LOG_ALWAYS_FATAL("Unknown socket type"); } - LOG_ALWAYS_FATAL("Could not connect"); + LOG_ALWAYS_FATAL("Could not connect %s", statusToString(status).c_str()); success: ret.sessions.push_back({session, session->getRootObject()}); } @@ -1191,14 +1196,14 @@ static bool testSupportVsockLoopback() { unsigned int vsockPort = allocateVsockPort(); sp<RpcServer> server = RpcServer::make(RpcTransportCtxFactoryRaw::make()); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); - CHECK(server->setupVsockServer(vsockPort)); + CHECK_EQ(OK, server->setupVsockServer(vsockPort)); server->start(); sp<RpcSession> session = RpcSession::make(RpcTransportCtxFactoryRaw::make()); - bool okay = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); + status_t status = session->setupVsockClient(VMADDR_CID_LOCAL, vsockPort); while (!server->shutdown()) usleep(10000); - ALOGE("Detected vsock loopback supported: %d", okay); - return okay; + ALOGE("Detected vsock loopback supported: %s", statusToString(status).c_str()); + return status == OK; } static std::vector<SocketType> testSocketTypes() { @@ -1274,7 +1279,7 @@ TEST_P(BinderRpcSimple, Shutdown) { unlink(addr.c_str()); auto server = RpcServer::make(newFactory(GetParam())); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); - ASSERT_TRUE(server->setupUnixDomainServer(addr.c_str())); + ASSERT_EQ(OK, server->setupUnixDomainServer(addr.c_str())); auto joinEnds = std::make_shared<OneOffSignal>(); // If things are broken and the thread never stops, don't block other tests. Because the thread @@ -1315,14 +1320,14 @@ TEST(BinderRpc, Java) { auto rpcServer = RpcServer::make(); rpcServer->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); unsigned int port; - ASSERT_TRUE(rpcServer->setupInetServer(kLocalInetAddress, 0, &port)); + ASSERT_EQ(OK, rpcServer->setupInetServer(kLocalInetAddress, 0, &port)); auto socket = rpcServer->releaseServer(); auto keepAlive = sp<BBinder>::make(); ASSERT_EQ(OK, binder->setRpcClientDebug(std::move(socket), keepAlive)); auto rpcSession = RpcSession::make(); - ASSERT_TRUE(rpcSession->setupInetClient("127.0.0.1", port)); + ASSERT_EQ(OK, rpcSession->setupInetClient("127.0.0.1", port)); auto rpcBinder = rpcSession->getRootObject(); ASSERT_NE(nullptr, rpcBinder); diff --git a/libs/binder/tests/parcel_fuzzer/random_parcel.cpp b/libs/binder/tests/parcel_fuzzer/random_parcel.cpp index a2472f860e..8bf04ccae0 100644 --- a/libs/binder/tests/parcel_fuzzer/random_parcel.cpp +++ b/libs/binder/tests/parcel_fuzzer/random_parcel.cpp @@ -37,7 +37,7 @@ private: void fillRandomParcel(Parcel* p, FuzzedDataProvider&& provider) { if (provider.ConsumeBool()) { auto session = RpcSession::make(RpcTransportCtxFactoryRaw::make()); - CHECK(session->addNullDebuggingClient()); + CHECK_EQ(OK, session->addNullDebuggingClient()); p->markForRpc(session); fillRandomParcelData(p, std::move(provider)); return; diff --git a/libs/binder/tests/rpc_fuzzer/main.cpp b/libs/binder/tests/rpc_fuzzer/main.cpp index 9fc496f460..230f5c7b77 100644 --- a/libs/binder/tests/rpc_fuzzer/main.cpp +++ b/libs/binder/tests/rpc_fuzzer/main.cpp @@ -60,7 +60,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { sp<RpcServer> server = RpcServer::make(); server->setRootObject(sp<SomeBinder>::make()); server->iUnderstandThisCodeIsExperimentalAndIWillNotUseItInProduction(); - CHECK(server->setupUnixDomainServer(kSock.c_str())); + CHECK_EQ(OK, server->setupUnixDomainServer(kSock.c_str())); std::thread serverThread([=] { (void)server->join(); }); |