diff options
author | 2021-05-12 00:03:15 +0000 | |
---|---|---|
committer | 2021-05-12 02:58:56 +0000 | |
commit | a63ff93a1bd754e14d0d979d8bac619fa6140c3b (patch) | |
tree | 5061611c9547ce7a772cc015ae9678f4fb932415 /libs/binder/RpcSession.cpp | |
parent | 7e563f090ba19c36b9879e14388a0e377f1523b5 (diff) |
libbinder: RPC read connection info on 2nd thread
Don't allow the main server thread to be blocked when reading a new ID.
Gotta be available for reading new clients.
Future consideration will be timeouts for reads/writes or trying to
detect DOS here in general, but this CL is what is needed for the
fuzzer.
Bug: 185167543
Bug: 182938024
Test: binderRpcTest
Change-Id: I8ba4a6ce6d1d07f3c36ac554ccb6cc403a15db2b
Diffstat (limited to 'libs/binder/RpcSession.cpp')
-rw-r--r-- | libs/binder/RpcSession.cpp | 32 |
1 files changed, 15 insertions, 17 deletions
diff --git a/libs/binder/RpcSession.cpp b/libs/binder/RpcSession.cpp index f38135b2dc..f32aa7a72d 100644 --- a/libs/binder/RpcSession.cpp +++ b/libs/binder/RpcSession.cpp @@ -131,24 +131,14 @@ status_t RpcSession::readId() { return OK; } -void RpcSession::startThread(unique_fd client) { - std::lock_guard<std::mutex> _l(mMutex); - sp<RpcSession> holdThis = sp<RpcSession>::fromExisting(this); - int fd = client.release(); - auto thread = std::thread([=] { - holdThis->join(unique_fd(fd)); - { - std::lock_guard<std::mutex> _l(holdThis->mMutex); - auto it = mThreads.find(std::this_thread::get_id()); - LOG_ALWAYS_FATAL_IF(it == mThreads.end()); - it->second.detach(); - mThreads.erase(it); - } - }); - mThreads[thread.get_id()] = std::move(thread); -} +void RpcSession::join(std::thread thread, unique_fd client) { + LOG_ALWAYS_FATAL_IF(thread.get_id() != std::this_thread::get_id(), "Must own this thread"); + + { + std::lock_guard<std::mutex> _l(mMutex); + mThreads[thread.get_id()] = std::move(thread); + } -void RpcSession::join(unique_fd client) { // must be registered to allow arbitrary client code executing commands to // be able to do nested calls (we can't only read from it) sp<RpcConnection> connection = assignServerToThisThread(std::move(client)); @@ -165,6 +155,14 @@ void RpcSession::join(unique_fd client) { LOG_ALWAYS_FATAL_IF(!removeServerConnection(connection), "bad state: connection object guaranteed to be in list"); + + { + std::lock_guard<std::mutex> _l(mMutex); + auto it = mThreads.find(std::this_thread::get_id()); + LOG_ALWAYS_FATAL_IF(it == mThreads.end()); + it->second.detach(); + mThreads.erase(it); + } } void RpcSession::terminateLocked() { |