diff options
author | 2022-08-26 23:23:15 +0000 | |
---|---|---|
committer | 2022-08-30 22:19:03 +0000 | |
commit | 1c24f9c97b7dffb098e57945162e74f78477d895 (patch) | |
tree | 03257185fba939511a50f6dae48b81d79f802a76 | |
parent | 11247a1bb5b02d92645ac6d94f3aaa34476fd0c7 (diff) |
libbinder : Avoid waiting in binder_rpc_fuzzer
Introducing new APIs in RpcServer and RpcSession to check
polling state of file descriptor. Removing fixed wait time
in binder_rpc_fuzzer.
Test: m binder_rpc_fuzzer &&
$ANDROID_HOST_OUT/fuzz/x86_64/binder_rpc_fuzzer/binder_rpc_fuzzer
Bug: 218518615
Change-Id: Ied82cd9c16514761a489731488924274a17053a6
-rw-r--r-- | libs/binder/RpcServer.cpp | 10 | ||||
-rw-r--r-- | libs/binder/RpcSession.cpp | 20 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcServer.h | 5 | ||||
-rw-r--r-- | libs/binder/include/binder/RpcSession.h | 10 | ||||
-rw-r--r-- | libs/binder/tests/rpc_fuzzer/main.cpp | 6 |
5 files changed, 49 insertions, 2 deletions
diff --git a/libs/binder/RpcServer.cpp b/libs/binder/RpcServer.cpp index 0ee5f05030..e581d0b16c 100644 --- a/libs/binder/RpcServer.cpp +++ b/libs/binder/RpcServer.cpp @@ -560,4 +560,14 @@ status_t RpcServer::setupExternalServer(base::unique_fd serverFd) { return OK; } +bool RpcServer::hasActiveRequests() { + RpcMutexLockGuard _l(mLock); + for (const auto& [_, session] : mSessions) { + if (session->hasActiveRequests()) { + return true; + } + } + return !mServer.isInPollingState(); +} + } // namespace android diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index bef2ed63c7..49843e5953 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -952,4 +952,24 @@ RpcSession::ExclusiveConnection::~ExclusiveConnection() { } } +bool RpcSession::hasActiveConnection(const std::vector<sp<RpcConnection>>& connections) { + for (const auto& connection : connections) { + if (connection->exclusiveTid != std::nullopt && !connection->rpcTransport->isWaiting()) { + return true; + } + } + return false; +} + +bool RpcSession::hasActiveRequests() { + RpcMutexUniqueLock _l(mMutex); + if (hasActiveConnection(mConnections.mIncoming)) { + return true; + } + if (hasActiveConnection(mConnections.mOutgoing)) { + return true; + } + return mConnections.mWaitingThreads != 0; +} + } // namespace android diff --git a/libs/binder/include/binder/RpcServer.h b/libs/binder/include/binder/RpcServer.h index ca02ab2a78..2c99334fa4 100644 --- a/libs/binder/include/binder/RpcServer.h +++ b/libs/binder/include/binder/RpcServer.h @@ -187,6 +187,11 @@ public: std::vector<sp<RpcSession>> listSessions(); size_t numUninitializedSessions(); + /** + * Whether any requests are currently being processed. + */ + bool hasActiveRequests(); + ~RpcServer(); private: diff --git a/libs/binder/include/binder/RpcSession.h b/libs/binder/include/binder/RpcSession.h index 9630e2fafa..a25ba987c8 100644 --- a/libs/binder/include/binder/RpcSession.h +++ b/libs/binder/include/binder/RpcSession.h @@ -189,6 +189,11 @@ public: */ [[nodiscard]] status_t sendDecStrong(const BpBinder* binder); + /** + * Whether any requests are currently being processed. + */ + bool hasActiveRequests(); + ~RpcSession(); /** @@ -286,6 +291,11 @@ private: [[nodiscard]] status_t initShutdownTrigger(); + /** + * Checks whether any connection is active (Not polling on fd) + */ + bool hasActiveConnection(const std::vector<sp<RpcConnection>>& connections); + enum class ConnectionUse { CLIENT, CLIENT_ASYNC, diff --git a/libs/binder/tests/rpc_fuzzer/main.cpp b/libs/binder/tests/rpc_fuzzer/main.cpp index a8713a24d6..f68a56144d 100644 --- a/libs/binder/tests/rpc_fuzzer/main.cpp +++ b/libs/binder/tests/rpc_fuzzer/main.cpp @@ -157,8 +157,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { } } - usleep(10000); - if (hangupBeforeShutdown) { connections.clear(); while (!server->listSessions().empty() || server->numUninitializedSessions()) { @@ -167,6 +165,10 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { } } + while (server->hasActiveRequests()) { + usleep(10); + } + while (!server->shutdown()) usleep(1); serverThread.join(); |