From c88b7fccbbc449d4bd0e371ccf489df0eb401750 Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Thu, 10 Jun 2021 00:40:39 +0000 Subject: libbinder: RPC know when connections setup Previously, there was a race where: a. client creates connection to server b. client sends request for reverse connection to server (but this may still be traveling on the wire) c. client sends transaction to server d. server tries to make a callback e. server fails to make callback because no reverse connection is setup Now, when a new connection is setup, a header on this connection is setup. So, we can wait on this header to be received in (b). Note: currently, (e) results in an abort, this is tracked in b/167966510 with a TODO in the ExclusiveConnection code. This would make a less obvious flake (or perhaps the problem would be ignored), but this race still needs to be fixed for well-behaved clients to be able to function reliably. Fixes: 190639665 Test: binderRpcTest (callback test 10,000s of times) Change-Id: I13bc912692d63ea73d46c5441fa7d51121df2f58 --- libs/binder/RpcState.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'libs/binder/RpcState.cpp') diff --git a/libs/binder/RpcState.cpp b/libs/binder/RpcState.cpp index 62eb58adba..53eba5aea6 100644 --- a/libs/binder/RpcState.cpp +++ b/libs/binder/RpcState.cpp @@ -265,6 +265,27 @@ status_t RpcState::rpcRec(const base::unique_fd& fd, const sp& sessi return OK; } +status_t RpcState::sendConnectionInit(const base::unique_fd& fd, const sp& session) { + RpcClientConnectionInit init{ + .msg = RPC_CONNECTION_INIT_OKAY, + }; + return rpcSend(fd, session, "connection init", &init, sizeof(init)); +} + +status_t RpcState::readConnectionInit(const base::unique_fd& fd, const sp& session) { + RpcClientConnectionInit init; + if (status_t status = rpcRec(fd, session, "connection init", &init, sizeof(init)); status != OK) + return status; + + static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); + if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { + ALOGE("Connection init message unrecognized %.*s", static_cast(sizeof(init.msg)), + init.msg); + return BAD_VALUE; + } + return OK; +} + sp RpcState::getRootObject(const base::unique_fd& fd, const sp& session) { Parcel data; data.markForRpc(session); -- cgit v1.2.3-59-g8ed1b