blob: 2b1d0e5bf69dc8b2295f0c7750698fc0739e9afa [file] [log] [blame]
Steven Moreland5553ac42020-11-11 02:14:45 +00001/*
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
17#define LOG_TAG "RpcState"
18
19#include "RpcState.h"
20
Steven Moreland62129012021-07-29 12:14:44 -070021#include <android-base/hex.h>
Andrei Homescua39e4ed2021-12-10 08:41:54 +000022#include <android-base/macros.h>
Steven Morelandd7302072021-05-15 01:32:04 +000023#include <android-base/scopeguard.h>
Frederick Mayle69a0c992022-05-26 20:38:39 +000024#include <android-base/stringprintf.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000025#include <binder/BpBinder.h>
Steven Morelandd7302072021-05-15 01:32:04 +000026#include <binder/IPCThreadState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000027#include <binder/RpcServer.h>
28
29#include "Debug.h"
30#include "RpcWireFormat.h"
Frederick Mayledc07cf82022-05-26 20:30:12 +000031#include "Utils.h"
Steven Moreland5553ac42020-11-11 02:14:45 +000032
Steven Morelandb8176792021-06-22 20:29:21 +000033#include <random>
34
Steven Moreland5553ac42020-11-11 02:14:45 +000035#include <inttypes.h>
36
37namespace android {
38
Steven Morelandd7302072021-05-15 01:32:04 +000039using base::ScopeGuard;
Frederick Mayle69a0c992022-05-26 20:38:39 +000040using base::StringPrintf;
Steven Morelandd7302072021-05-15 01:32:04 +000041
Devin Moore08256432021-07-02 13:03:49 -070042#if RPC_FLAKE_PRONE
Steven Morelandb8176792021-06-22 20:29:21 +000043void rpcMaybeWaitToFlake() {
Devin Moore08256432021-07-02 13:03:49 -070044 [[clang::no_destroy]] static std::random_device r;
45 [[clang::no_destroy]] static std::mutex m;
Steven Morelandb8176792021-06-22 20:29:21 +000046 unsigned num;
47 {
48 std::lock_guard<std::mutex> lock(m);
49 num = r();
50 }
51 if (num % 10 == 0) usleep(num % 1000);
52}
53#endif
54
Frederick Mayle69a0c992022-05-26 20:38:39 +000055static bool enableAncillaryFds(RpcSession::FileDescriptorTransportMode mode) {
56 switch (mode) {
57 case RpcSession::FileDescriptorTransportMode::NONE:
58 return false;
59 case RpcSession::FileDescriptorTransportMode::UNIX:
60 return true;
61 }
62}
63
Steven Moreland5553ac42020-11-11 02:14:45 +000064RpcState::RpcState() {}
65RpcState::~RpcState() {}
66
Steven Morelandbdb53ab2021-05-05 17:57:41 +000067status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
Steven Moreland5623d1a2021-09-10 15:45:34 -070068 uint64_t* outAddress) {
Steven Moreland5553ac42020-11-11 02:14:45 +000069 bool isRemote = binder->remoteBinder();
70 bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder();
71
Steven Moreland99157622021-09-13 16:27:34 -070072 if (isRpc && binder->remoteBinder()->getPrivateAccessor().rpcSession() != session) {
Steven Moreland5553ac42020-11-11 02:14:45 +000073 // We need to be able to send instructions over the socket for how to
74 // connect to a different server, and we also need to let the host
75 // process know that this is happening.
Steven Morelandbdb53ab2021-05-05 17:57:41 +000076 ALOGE("Cannot send binder from unrelated binder RPC session.");
Steven Moreland5553ac42020-11-11 02:14:45 +000077 return INVALID_OPERATION;
78 }
79
80 if (isRemote && !isRpc) {
81 // Without additional work, this would have the effect of using this
82 // process to proxy calls from the socket over to the other process, and
83 // it would make those calls look like they come from us (not over the
84 // sockets). In order to make this work transparently like binder, we
85 // would instead need to send instructions over the socket for how to
86 // connect to the host process, and we also need to let the host process
87 // know this was happening.
88 ALOGE("Cannot send binder proxy %p over sockets", binder.get());
89 return INVALID_OPERATION;
90 }
91
92 std::lock_guard<std::mutex> _l(mNodeMutex);
Steven Moreland7227c8a2021-06-02 00:24:32 +000093 if (mTerminated) return DEAD_OBJECT;
Steven Moreland5553ac42020-11-11 02:14:45 +000094
95 // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map
96 // in RpcState
97 for (auto& [addr, node] : mNodeForAddress) {
98 if (binder == node.binder) {
99 if (isRpc) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700100 // check integrity of data structure
Steven Moreland99157622021-09-13 16:27:34 -0700101 uint64_t actualAddr = binder->remoteBinder()->getPrivateAccessor().rpcAddress();
Steven Moreland5623d1a2021-09-10 15:45:34 -0700102 LOG_ALWAYS_FATAL_IF(addr != actualAddr, "Address mismatch %" PRIu64 " vs %" PRIu64,
103 addr, actualAddr);
Steven Moreland5553ac42020-11-11 02:14:45 +0000104 }
105 node.timesSent++;
106 node.sentRef = binder; // might already be set
107 *outAddress = addr;
108 return OK;
109 }
110 }
111 LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point");
112
Steven Moreland91538242021-06-10 23:35:35 +0000113 bool forServer = session->server() != nullptr;
Steven Moreland5553ac42020-11-11 02:14:45 +0000114
Steven Moreland5623d1a2021-09-10 15:45:34 -0700115 // arbitrary limit for maximum number of nodes in a process (otherwise we
116 // might run out of addresses)
117 if (mNodeForAddress.size() > 100000) {
118 return NO_MEMORY;
119 }
120
121 while (true) {
122 RpcWireAddress address{
123 .options = RPC_WIRE_ADDRESS_OPTION_CREATED,
124 .address = mNextId,
125 };
126 if (forServer) {
127 address.options |= RPC_WIRE_ADDRESS_OPTION_FOR_SERVER;
128 }
129
130 // avoid ubsan abort
131 if (mNextId >= std::numeric_limits<uint32_t>::max()) {
132 mNextId = 0;
133 } else {
134 mNextId++;
135 }
136
137 auto&& [it, inserted] = mNodeForAddress.insert({RpcWireAddress::toRaw(address),
Steven Moreland91538242021-06-10 23:35:35 +0000138 BinderNode{
139 .binder = binder,
Steven Moreland91538242021-06-10 23:35:35 +0000140 .sentRef = binder,
Andrei Homescu5a036f32022-03-08 22:54:40 +0000141 .timesSent = 1,
Steven Moreland91538242021-06-10 23:35:35 +0000142 }});
143 if (inserted) {
144 *outAddress = it->first;
145 return OK;
146 }
Steven Moreland91538242021-06-10 23:35:35 +0000147 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000148}
149
Steven Moreland5623d1a2021-09-10 15:45:34 -0700150status_t RpcState::onBinderEntering(const sp<RpcSession>& session, uint64_t address,
Steven Moreland7227c8a2021-06-02 00:24:32 +0000151 sp<IBinder>* out) {
Steven Moreland91538242021-06-10 23:35:35 +0000152 // ensure that: if we want to use addresses for something else in the future (for
153 // instance, allowing transitive binder sends), that we don't accidentally
154 // send those addresses to old server. Accidentally ignoring this in that
155 // case and considering the binder to be recognized could cause this
156 // process to accidentally proxy transactions for that binder. Of course,
157 // if we communicate with a binder, it could always be proxying
158 // information. However, we want to make sure that isn't done on accident
159 // by a client.
Steven Moreland5623d1a2021-09-10 15:45:34 -0700160 RpcWireAddress addr = RpcWireAddress::fromRaw(address);
161 constexpr uint32_t kKnownOptions =
162 RPC_WIRE_ADDRESS_OPTION_CREATED | RPC_WIRE_ADDRESS_OPTION_FOR_SERVER;
163 if (addr.options & ~kKnownOptions) {
164 ALOGE("Address is of an unknown type, rejecting: %" PRIu64, address);
Steven Moreland91538242021-06-10 23:35:35 +0000165 return BAD_VALUE;
166 }
167
Steven Morelandd8083312021-09-22 13:37:10 -0700168 std::lock_guard<std::mutex> _l(mNodeMutex);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000169 if (mTerminated) return DEAD_OBJECT;
Steven Moreland5553ac42020-11-11 02:14:45 +0000170
171 if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) {
Steven Moreland7227c8a2021-06-02 00:24:32 +0000172 *out = it->second.binder.promote();
Steven Moreland5553ac42020-11-11 02:14:45 +0000173
174 // implicitly have strong RPC refcount, since we received this binder
175 it->second.timesRecd++;
Steven Morelandd8083312021-09-22 13:37:10 -0700176 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000177 }
178
Steven Moreland91538242021-06-10 23:35:35 +0000179 // we don't know about this binder, so the other side of the connection
180 // should have created it.
Steven Moreland5623d1a2021-09-10 15:45:34 -0700181 if ((addr.options & RPC_WIRE_ADDRESS_OPTION_FOR_SERVER) == !!session->server()) {
182 ALOGE("Server received unrecognized address which we should own the creation of %" PRIu64,
183 address);
Steven Moreland91538242021-06-10 23:35:35 +0000184 return BAD_VALUE;
185 }
186
Steven Moreland5553ac42020-11-11 02:14:45 +0000187 auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}});
188 LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy");
189
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000190 // Currently, all binders are assumed to be part of the same session (no
Steven Moreland5553ac42020-11-11 02:14:45 +0000191 // device global binders in the RPC world).
Steven Moreland99157622021-09-13 16:27:34 -0700192 it->second.binder = *out = BpBinder::PrivateAccessor::create(session, it->first);
Steven Moreland5553ac42020-11-11 02:14:45 +0000193 it->second.timesRecd = 1;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000194 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000195}
196
Steven Morelandd8083312021-09-22 13:37:10 -0700197status_t RpcState::flushExcessBinderRefs(const sp<RpcSession>& session, uint64_t address,
198 const sp<IBinder>& binder) {
Steven Morelande96ed0e2021-09-27 17:43:53 -0700199 // We can flush all references when the binder is destroyed. No need to send
200 // extra reference counting packets now.
201 if (binder->remoteBinder()) return OK;
202
Steven Morelandd8083312021-09-22 13:37:10 -0700203 std::unique_lock<std::mutex> _l(mNodeMutex);
204 if (mTerminated) return DEAD_OBJECT;
205
206 auto it = mNodeForAddress.find(address);
207
208 LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Can't be deleted while we hold sp<>");
209 LOG_ALWAYS_FATAL_IF(it->second.binder != binder,
210 "Caller of flushExcessBinderRefs using inconsistent arguments");
211
Steven Morelande96ed0e2021-09-27 17:43:53 -0700212 LOG_ALWAYS_FATAL_IF(it->second.timesSent <= 0, "Local binder must have been sent %p",
213 binder.get());
Steven Morelandd8083312021-09-22 13:37:10 -0700214
Steven Morelande96ed0e2021-09-27 17:43:53 -0700215 // For a local binder, we only need to know that we sent it. Now that we
216 // have an sp<> for this call, we don't need anything more. If the other
217 // process is done with this binder, it needs to know we received the
218 // refcount associated with this call, so we can acknowledge that we
219 // received it. Once (or if) it has no other refcounts, it would reply with
220 // its own decStrong so that it could be removed from this session.
221 if (it->second.timesRecd != 0) {
Steven Morelandd8083312021-09-22 13:37:10 -0700222 _l.unlock();
223
Steven Morelande96ed0e2021-09-27 17:43:53 -0700224 return session->sendDecStrongToTarget(address, 0);
Steven Morelandd8083312021-09-22 13:37:10 -0700225 }
226
227 return OK;
228}
229
Steven Moreland5553ac42020-11-11 02:14:45 +0000230size_t RpcState::countBinders() {
231 std::lock_guard<std::mutex> _l(mNodeMutex);
232 return mNodeForAddress.size();
233}
234
235void RpcState::dump() {
236 std::lock_guard<std::mutex> _l(mNodeMutex);
Steven Moreland583a14a2021-06-04 02:04:58 +0000237 dumpLocked();
238}
239
Steven Morelandc9d7b532021-06-04 20:57:41 +0000240void RpcState::clear() {
Steven Moreland583a14a2021-06-04 02:04:58 +0000241 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000242
243 if (mTerminated) {
244 LOG_ALWAYS_FATAL_IF(!mNodeForAddress.empty(),
245 "New state should be impossible after terminating!");
246 return;
247 }
248
249 if (SHOULD_LOG_RPC_DETAIL) {
250 ALOGE("RpcState::clear()");
251 dumpLocked();
252 }
253
254 // if the destructor of a binder object makes another RPC call, then calling
255 // decStrong could deadlock. So, we must hold onto these binders until
256 // mNodeMutex is no longer taken.
257 std::vector<sp<IBinder>> tempHoldBinder;
258
259 mTerminated = true;
260 for (auto& [address, node] : mNodeForAddress) {
261 sp<IBinder> binder = node.binder.promote();
262 LOG_ALWAYS_FATAL_IF(binder == nullptr, "Binder %p expected to be owned.", binder.get());
263
264 if (node.sentRef != nullptr) {
265 tempHoldBinder.push_back(node.sentRef);
266 }
267 }
268
269 mNodeForAddress.clear();
270
271 _l.unlock();
272 tempHoldBinder.clear(); // explicit
Steven Moreland583a14a2021-06-04 02:04:58 +0000273}
274
275void RpcState::dumpLocked() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000276 ALOGE("DUMP OF RpcState %p", this);
277 ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size());
278 for (const auto& [address, node] : mNodeForAddress) {
279 sp<IBinder> binder = node.binder.promote();
280
281 const char* desc;
282 if (binder) {
283 if (binder->remoteBinder()) {
284 if (binder->remoteBinder()->isRpcBinder()) {
285 desc = "(rpc binder proxy)";
286 } else {
287 desc = "(binder proxy)";
288 }
289 } else {
290 desc = "(local binder)";
291 }
292 } else {
293 desc = "(null)";
294 }
295
Steven Moreland5623d1a2021-09-10 15:45:34 -0700296 ALOGE("- BINDER NODE: %p times sent:%zu times recd: %zu a: %" PRIu64 " type: %s",
297 node.binder.unsafe_get(), node.timesSent, node.timesRecd, address, desc);
Steven Moreland5553ac42020-11-11 02:14:45 +0000298 }
299 ALOGE("END DUMP OF RpcState");
300}
301
Steven Moreland5553ac42020-11-11 02:14:45 +0000302
Steven Morelanddbe71832021-05-12 23:31:00 +0000303RpcState::CommandData::CommandData(size_t size) : mSize(size) {
304 // The maximum size for regular binder is 1MB for all concurrent
305 // transactions. A very small proportion of transactions are even
306 // larger than a page, but we need to avoid allocating too much
307 // data on behalf of an arbitrary client, or we could risk being in
308 // a position where a single additional allocation could run out of
309 // memory.
310 //
311 // Note, this limit may not reflect the total amount of data allocated for a
312 // transaction (in some cases, additional fixed size amounts are added),
313 // though for rough consistency, we should avoid cases where this data type
314 // is used for multiple dynamic allocations for a single transaction.
315 constexpr size_t kMaxTransactionAllocation = 100 * 1000;
316 if (size == 0) return;
317 if (size > kMaxTransactionAllocation) {
318 ALOGW("Transaction requested too much data allocation %zu", size);
319 return;
320 }
321 mData.reset(new (std::nothrow) uint8_t[size]);
322}
323
Frederick Mayle69a0c992022-05-26 20:38:39 +0000324status_t RpcState::rpcSend(
325 const sp<RpcSession::RpcConnection>& connection, const sp<RpcSession>& session,
326 const char* what, iovec* iovs, int niovs,
327 const std::optional<android::base::function_ref<status_t()>>& altPoll,
328 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) {
Colin Cross9adfeaf2022-01-21 17:22:09 -0800329 for (int i = 0; i < niovs; i++) {
Andrei Homescu0a692352022-03-29 06:04:26 +0000330 LOG_RPC_DETAIL("Sending %s (part %d of %d) on RpcTransport %p: %s",
331 what, i + 1, niovs, connection->rpcTransport.get(),
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000332 android::base::HexString(iovs[i].iov_base, iovs[i].iov_len).c_str());
Steven Moreland5553ac42020-11-11 02:14:45 +0000333 }
334
Yifan Hong702115c2021-06-24 15:39:18 -0700335 if (status_t status =
Yifan Hong8c950422021-08-05 17:13:55 -0700336 connection->rpcTransport->interruptableWriteFully(session->mShutdownTrigger.get(),
Frederick Mayle69a0c992022-05-26 20:38:39 +0000337 iovs, niovs, altPoll,
338 ancillaryFds);
Steven Moreland798e0d12021-07-14 23:19:25 +0000339 status != OK) {
Colin Cross9adfeaf2022-01-21 17:22:09 -0800340 LOG_RPC_DETAIL("Failed to write %s (%d iovs) on RpcTransport %p, error: %s", what, niovs,
Yifan Hong702115c2021-06-24 15:39:18 -0700341 connection->rpcTransport.get(), statusToString(status).c_str());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000342 (void)session->shutdownAndWait(false);
Steven Moreland798e0d12021-07-14 23:19:25 +0000343 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000344 }
345
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000346 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000347}
348
Steven Moreland5ae62562021-06-10 03:21:42 +0000349status_t RpcState::rpcRec(const sp<RpcSession::RpcConnection>& connection,
Colin Cross9adfeaf2022-01-21 17:22:09 -0800350 const sp<RpcSession>& session, const char* what, iovec* iovs, int niovs) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000351 if (status_t status = connection->rpcTransport->interruptableReadFully(
352 session->mShutdownTrigger.get(), iovs, niovs, std::nullopt,
353 enableAncillaryFds(session->getFileDescriptorTransportMode()));
Steven Morelandee3f4662021-05-22 01:07:33 +0000354 status != OK) {
Colin Cross9adfeaf2022-01-21 17:22:09 -0800355 LOG_RPC_DETAIL("Failed to read %s (%d iovs) on RpcTransport %p, error: %s", what, niovs,
Yifan Hong702115c2021-06-24 15:39:18 -0700356 connection->rpcTransport.get(), statusToString(status).c_str());
Steven Morelandae58f432021-08-05 17:53:16 -0700357 (void)session->shutdownAndWait(false);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000358 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000359 }
360
Colin Cross9adfeaf2022-01-21 17:22:09 -0800361 for (int i = 0; i < niovs; i++) {
Andrei Homescu0a692352022-03-29 06:04:26 +0000362 LOG_RPC_DETAIL("Received %s (part %d of %d) on RpcTransport %p: %s",
363 what, i + 1, niovs, connection->rpcTransport.get(),
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000364 android::base::HexString(iovs[i].iov_base, iovs[i].iov_len).c_str());
365 }
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000366 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000367}
368
Steven Morelandbf57bce2021-07-26 15:26:12 -0700369status_t RpcState::readNewSessionResponse(const sp<RpcSession::RpcConnection>& connection,
370 const sp<RpcSession>& session, uint32_t* version) {
371 RpcNewSessionResponse response;
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000372 iovec iov{&response, sizeof(response)};
373 if (status_t status = rpcRec(connection, session, "new session response", &iov, 1);
Steven Morelandbf57bce2021-07-26 15:26:12 -0700374 status != OK) {
375 return status;
376 }
377 *version = response.version;
378 return OK;
379}
380
Steven Moreland5ae62562021-06-10 03:21:42 +0000381status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection,
382 const sp<RpcSession>& session) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000383 RpcOutgoingConnectionInit init{
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000384 .msg = RPC_CONNECTION_INIT_OKAY,
385 };
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000386 iovec iov{&init, sizeof(init)};
Devin Moore695368f2022-06-03 22:29:14 +0000387 return rpcSend(connection, session, "connection init", &iov, 1, std::nullopt);
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000388}
389
Steven Moreland5ae62562021-06-10 03:21:42 +0000390status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection,
391 const sp<RpcSession>& session) {
Steven Moreland19fc9f72021-06-10 03:57:30 +0000392 RpcOutgoingConnectionInit init;
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000393 iovec iov{&init, sizeof(init)};
394 if (status_t status = rpcRec(connection, session, "connection init", &iov, 1); status != OK)
Steven Morelandc88b7fc2021-06-10 00:40:39 +0000395 return status;
396
397 static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY));
398 if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) {
399 ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)),
400 init.msg);
401 return BAD_VALUE;
402 }
403 return OK;
404}
405
Steven Moreland5ae62562021-06-10 03:21:42 +0000406sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection,
407 const sp<RpcSession>& session) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000408 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000409 data.markForRpc(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000410 Parcel reply;
411
Steven Moreland5623d1a2021-09-10 15:45:34 -0700412 status_t status =
413 transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_ROOT, data, session, &reply, 0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000414 if (status != OK) {
415 ALOGE("Error getting root object: %s", statusToString(status).c_str());
416 return nullptr;
417 }
418
419 return reply.readStrongBinder();
420}
421
Steven Moreland5ae62562021-06-10 03:21:42 +0000422status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection,
423 const sp<RpcSession>& session, size_t* maxThreadsOut) {
Steven Morelandf137de92021-04-24 01:54:26 +0000424 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000425 data.markForRpc(session);
Steven Morelandf137de92021-04-24 01:54:26 +0000426 Parcel reply;
427
Steven Moreland5623d1a2021-09-10 15:45:34 -0700428 status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, data,
429 session, &reply, 0);
Steven Morelandf137de92021-04-24 01:54:26 +0000430 if (status != OK) {
431 ALOGE("Error getting max threads: %s", statusToString(status).c_str());
432 return status;
433 }
434
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000435 int32_t maxThreads;
436 status = reply.readInt32(&maxThreads);
Steven Morelandf137de92021-04-24 01:54:26 +0000437 if (status != OK) return status;
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000438 if (maxThreads <= 0) {
439 ALOGE("Error invalid max maxThreads: %d", maxThreads);
Steven Morelandf137de92021-04-24 01:54:26 +0000440 return BAD_VALUE;
441 }
442
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000443 *maxThreadsOut = maxThreads;
444 return OK;
445}
446
Steven Moreland5ae62562021-06-10 03:21:42 +0000447status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland826367f2021-09-10 14:05:31 -0700448 const sp<RpcSession>& session, std::vector<uint8_t>* sessionIdOut) {
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000449 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000450 data.markForRpc(session);
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000451 Parcel reply;
452
Steven Moreland5623d1a2021-09-10 15:45:34 -0700453 status_t status = transactAddress(connection, 0, RPC_SPECIAL_TRANSACT_GET_SESSION_ID, data,
454 session, &reply, 0);
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000455 if (status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000456 ALOGE("Error getting session ID: %s", statusToString(status).c_str());
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000457 return status;
458 }
459
Steven Moreland826367f2021-09-10 14:05:31 -0700460 return reply.readByteVector(sessionIdOut);
Steven Morelandf137de92021-04-24 01:54:26 +0000461}
462
Steven Moreland5ae62562021-06-10 03:21:42 +0000463status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection,
464 const sp<IBinder>& binder, uint32_t code, const Parcel& data,
465 const sp<RpcSession>& session, Parcel* reply, uint32_t flags) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000466 std::string errorMsg;
467 if (status_t status = validateParcel(session, data, &errorMsg); status != OK) {
468 ALOGE("Refusing to send RPC on binder %p code %" PRIu32 ": Parcel %p failed validation: %s",
469 binder.get(), code, &data, errorMsg.c_str());
470 return status;
Steven Morelandf5174272021-05-25 00:39:28 +0000471 }
Steven Moreland5623d1a2021-09-10 15:45:34 -0700472 uint64_t address;
Steven Morelandf5174272021-05-25 00:39:28 +0000473 if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status;
474
Steven Moreland5ae62562021-06-10 03:21:42 +0000475 return transactAddress(connection, address, code, data, session, reply, flags);
Steven Morelandf5174272021-05-25 00:39:28 +0000476}
477
Steven Moreland5ae62562021-06-10 03:21:42 +0000478status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection,
Steven Moreland5623d1a2021-09-10 15:45:34 -0700479 uint64_t address, uint32_t code, const Parcel& data,
Steven Moreland5ae62562021-06-10 03:21:42 +0000480 const sp<RpcSession>& session, Parcel* reply, uint32_t flags) {
Steven Morelandf5174272021-05-25 00:39:28 +0000481 LOG_ALWAYS_FATAL_IF(!data.isForRpc());
482 LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0);
483
Steven Moreland5553ac42020-11-11 02:14:45 +0000484 uint64_t asyncNumber = 0;
485
Steven Moreland5623d1a2021-09-10 15:45:34 -0700486 if (address != 0) {
Steven Moreland583a14a2021-06-04 02:04:58 +0000487 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Moreland5553ac42020-11-11 02:14:45 +0000488 if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races
489 auto it = mNodeForAddress.find(address);
Steven Moreland5623d1a2021-09-10 15:45:34 -0700490 LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(),
491 "Sending transact on unknown address %" PRIu64, address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000492
493 if (flags & IBinder::FLAG_ONEWAY) {
Steven Moreland583a14a2021-06-04 02:04:58 +0000494 asyncNumber = it->second.asyncNumber;
Steven Morelandc9d7b532021-06-04 20:57:41 +0000495 if (!nodeProgressAsyncNumber(&it->second)) {
496 _l.unlock();
497 (void)session->shutdownAndWait(false);
498 return DEAD_OBJECT;
499 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000500 }
501 }
502
Frederick Mayle69a0c992022-05-26 20:38:39 +0000503 auto* rpcFields = data.maybeRpcFields();
504 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
505
506 Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(),
507 rpcFields->mObjectPositions.size()};
Frederick Mayledc07cf82022-05-26 20:30:12 +0000508
Frederick Mayle778c0902022-05-27 01:14:57 +0000509 uint32_t bodySize;
510 LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(sizeof(RpcWireTransaction), data.dataSize(),
Frederick Mayledc07cf82022-05-26 20:30:12 +0000511 &bodySize) ||
512 __builtin_add_overflow(objectTableSpan.byteSize(), bodySize,
513 &bodySize),
Steven Moreland77c30112021-06-02 20:45:46 +0000514 "Too much data %zu", data.dataSize());
Steven Moreland77c30112021-06-02 20:45:46 +0000515 RpcWireHeader command{
516 .command = RPC_COMMAND_TRANSACT,
Frederick Mayle778c0902022-05-27 01:14:57 +0000517 .bodySize = bodySize,
Steven Moreland77c30112021-06-02 20:45:46 +0000518 };
Steven Moreland5623d1a2021-09-10 15:45:34 -0700519
Steven Moreland5553ac42020-11-11 02:14:45 +0000520 RpcWireTransaction transaction{
Steven Moreland5623d1a2021-09-10 15:45:34 -0700521 .address = RpcWireAddress::fromRaw(address),
Steven Moreland5553ac42020-11-11 02:14:45 +0000522 .code = code,
523 .flags = flags,
524 .asyncNumber = asyncNumber,
Frederick Mayledc07cf82022-05-26 20:30:12 +0000525 // bodySize didn't overflow => this cast is safe
526 .parcelDataSize = static_cast<uint32_t>(data.dataSize()),
Steven Moreland5553ac42020-11-11 02:14:45 +0000527 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000528
Steven Moreland43921d52021-09-27 17:15:56 -0700529 constexpr size_t kWaitMaxUs = 1000000;
530 constexpr size_t kWaitLogUs = 10000;
531 size_t waitUs = 0;
532
533 // Oneway calls have no sync point, so if many are sent before, whether this
534 // is a twoway or oneway transaction, they may have filled up the socket.
Devin Moore695368f2022-06-03 22:29:14 +0000535 // So, make sure we drain them before polling
Steven Moreland43921d52021-09-27 17:15:56 -0700536
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000537 iovec iovs[]{
538 {&command, sizeof(RpcWireHeader)},
539 {&transaction, sizeof(RpcWireTransaction)},
540 {const_cast<uint8_t*>(data.data()), data.dataSize()},
Frederick Mayledc07cf82022-05-26 20:30:12 +0000541 objectTableSpan.toIovec(),
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000542 };
Frederick Mayle69a0c992022-05-26 20:38:39 +0000543 if (status_t status = rpcSend(
544 connection, session, "transaction", iovs, arraysize(iovs),
545 [&] {
546 if (waitUs > kWaitLogUs) {
547 ALOGE("Cannot send command, trying to process pending refcounts. Waiting "
548 "%zuus. Too many oneway calls?",
549 waitUs);
550 }
Devin Moore695368f2022-06-03 22:29:14 +0000551
Frederick Mayle69a0c992022-05-26 20:38:39 +0000552 if (waitUs > 0) {
553 usleep(waitUs);
554 waitUs = std::min(kWaitMaxUs, waitUs * 2);
555 } else {
556 waitUs = 1;
557 }
Devin Moore695368f2022-06-03 22:29:14 +0000558
Frederick Mayle69a0c992022-05-26 20:38:39 +0000559 return drainCommands(connection, session, CommandType::CONTROL_ONLY);
560 },
561 rpcFields->mFds.get());
Steven Moreland43921d52021-09-27 17:15:56 -0700562 status != OK) {
Steven Morelanda5036f02021-06-08 02:26:57 +0000563 // TODO(b/167966510): need to undo onBinderLeaving - we know the
564 // refcount isn't successfully transferred.
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000565 return status;
Steven Moreland43921d52021-09-27 17:15:56 -0700566 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000567
568 if (flags & IBinder::FLAG_ONEWAY) {
Yifan Hong702115c2021-06-24 15:39:18 -0700569 LOG_RPC_DETAIL("Oneway command, so no longer waiting on RpcTransport %p",
570 connection->rpcTransport.get());
Steven Moreland52eee942021-06-03 00:59:28 +0000571
572 // Do not wait on result.
Steven Moreland43921d52021-09-27 17:15:56 -0700573 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000574 }
575
576 LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction.");
577
Steven Moreland5ae62562021-06-10 03:21:42 +0000578 return waitForReply(connection, session, reply);
Steven Moreland5553ac42020-11-11 02:14:45 +0000579}
580
Steven Moreland438cce82021-04-02 18:04:08 +0000581static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize,
582 const binder_size_t* objects, size_t objectsCount) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000583 (void)p;
Frederick Mayle68aa3bc2022-06-07 15:51:31 +0000584 delete[] const_cast<uint8_t*>(data);
Steven Moreland5553ac42020-11-11 02:14:45 +0000585 (void)dataSize;
586 LOG_ALWAYS_FATAL_IF(objects != nullptr);
Yifan Hong239a2ca2021-06-24 16:05:16 -0700587 LOG_ALWAYS_FATAL_IF(objectsCount != 0, "%zu objects remaining", objectsCount);
Steven Moreland5553ac42020-11-11 02:14:45 +0000588}
589
Steven Moreland5ae62562021-06-10 03:21:42 +0000590status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection,
591 const sp<RpcSession>& session, Parcel* reply) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000592 RpcWireHeader command;
593 while (true) {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000594 iovec iov{&command, sizeof(command)};
595 if (status_t status = rpcRec(connection, session, "command header (for reply)", &iov, 1);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000596 status != OK)
597 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000598
599 if (command.command == RPC_COMMAND_REPLY) break;
600
Steven Moreland19fc9f72021-06-10 03:57:30 +0000601 if (status_t status = processCommand(connection, session, command, CommandType::ANY);
Steven Moreland52eee942021-06-03 00:59:28 +0000602 status != OK)
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000603 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000604 }
605
Frederick Mayledc07cf82022-05-26 20:30:12 +0000606 const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value());
607
608 if (command.bodySize < rpcReplyWireSize) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000609 ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!",
610 sizeof(RpcWireReply), command.bodySize);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000611 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000612 return BAD_VALUE;
613 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000614
Frederick Mayle68aa3bc2022-06-07 15:51:31 +0000615 RpcWireReply rpcReply;
Frederick Mayledc07cf82022-05-26 20:30:12 +0000616 memset(&rpcReply, 0, sizeof(RpcWireReply)); // zero because of potential short read
617
618 CommandData data(command.bodySize - rpcReplyWireSize);
Frederick Mayle68aa3bc2022-06-07 15:51:31 +0000619 if (!data.valid()) return NO_MEMORY;
620
621 iovec iovs[]{
Frederick Mayledc07cf82022-05-26 20:30:12 +0000622 {&rpcReply, rpcReplyWireSize},
Frederick Mayle68aa3bc2022-06-07 15:51:31 +0000623 {data.data(), data.size()},
624 };
625 if (status_t status = rpcRec(connection, session, "reply body", iovs, arraysize(iovs));
626 status != OK)
627 return status;
Frederick Mayle69a0c992022-05-26 20:38:39 +0000628
629 // Check if the reply came with any ancillary data.
630 std::vector<base::unique_fd> pendingFds;
631 if (status_t status = connection->rpcTransport->consumePendingAncillaryData(&pendingFds);
632 status != OK) {
633 return status;
634 }
635
Frederick Mayle68aa3bc2022-06-07 15:51:31 +0000636 if (rpcReply.status != OK) return rpcReply.status;
637
Frederick Mayledc07cf82022-05-26 20:30:12 +0000638 Span<const uint8_t> parcelSpan = {data.data(), data.size()};
Frederick Mayle69a0c992022-05-26 20:38:39 +0000639 Span<const uint32_t> objectTableSpan;
Frederick Mayledc07cf82022-05-26 20:30:12 +0000640 if (session->getProtocolVersion().value() >=
641 RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) {
642 Span<const uint8_t> objectTableBytes = parcelSpan.splitOff(rpcReply.parcelDataSize);
Frederick Mayle69a0c992022-05-26 20:38:39 +0000643 std::optional<Span<const uint32_t>> maybeSpan =
644 objectTableBytes.reinterpret<const uint32_t>();
645 if (!maybeSpan.has_value()) {
646 ALOGE("Bad object table size inferred from RpcWireReply. Saw bodySize=%" PRId32
647 " sizeofHeader=%zu parcelSize=%" PRId32 " objectTableBytesSize=%zu. Terminating!",
648 command.bodySize, rpcReplyWireSize, rpcReply.parcelDataSize,
649 objectTableBytes.size);
650 return BAD_VALUE;
651 }
652 objectTableSpan = *maybeSpan;
Frederick Mayledc07cf82022-05-26 20:30:12 +0000653 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000654
Frederick Mayledc07cf82022-05-26 20:30:12 +0000655 data.release();
Frederick Mayle69a0c992022-05-26 20:38:39 +0000656 return reply->rpcSetDataReference(session, parcelSpan.data, parcelSpan.size,
657 objectTableSpan.data, objectTableSpan.size,
658 std::move(pendingFds), cleanup_reply_data);
Steven Moreland5553ac42020-11-11 02:14:45 +0000659}
660
Steven Morelandfd1e8a02021-07-21 23:30:29 +0000661status_t RpcState::sendDecStrongToTarget(const sp<RpcSession::RpcConnection>& connection,
662 const sp<RpcSession>& session, uint64_t addr,
663 size_t target) {
664 RpcDecStrong body = {
665 .address = RpcWireAddress::fromRaw(addr),
666 };
667
Steven Moreland5553ac42020-11-11 02:14:45 +0000668 {
669 std::lock_guard<std::mutex> _l(mNodeMutex);
670 if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races
671 auto it = mNodeForAddress.find(addr);
Steven Moreland5623d1a2021-09-10 15:45:34 -0700672 LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(),
673 "Sending dec strong on unknown address %" PRIu64, addr);
Steven Moreland5553ac42020-11-11 02:14:45 +0000674
Steven Morelandfd1e8a02021-07-21 23:30:29 +0000675 LOG_ALWAYS_FATAL_IF(it->second.timesRecd < target, "Can't dec count of %zu to %zu.",
676 it->second.timesRecd, target);
677
678 // typically this happens when multiple threads send dec refs at the
679 // same time - the transactions will get combined automatically
680 if (it->second.timesRecd == target) return OK;
681
682 body.amount = it->second.timesRecd - target;
683 it->second.timesRecd = target;
684
Steven Moreland31bde7a2021-06-04 00:57:36 +0000685 LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it),
686 "Bad state. RpcState shouldn't own received binder");
Steven Moreland5553ac42020-11-11 02:14:45 +0000687 }
688
689 RpcWireHeader cmd = {
690 .command = RPC_COMMAND_DEC_STRONG,
Steven Morelandfd1e8a02021-07-21 23:30:29 +0000691 .bodySize = sizeof(RpcDecStrong),
Steven Moreland5553ac42020-11-11 02:14:45 +0000692 };
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000693 iovec iovs[]{{&cmd, sizeof(cmd)}, {&body, sizeof(body)}};
Devin Moore695368f2022-06-03 22:29:14 +0000694 return rpcSend(connection, session, "dec ref", iovs, arraysize(iovs), std::nullopt);
Steven Moreland5553ac42020-11-11 02:14:45 +0000695}
696
Steven Moreland5ae62562021-06-10 03:21:42 +0000697status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection,
698 const sp<RpcSession>& session, CommandType type) {
Yifan Hong702115c2021-06-24 15:39:18 -0700699 LOG_RPC_DETAIL("getAndExecuteCommand on RpcTransport %p", connection->rpcTransport.get());
Steven Moreland5553ac42020-11-11 02:14:45 +0000700
701 RpcWireHeader command;
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000702 iovec iov{&command, sizeof(command)};
703 if (status_t status = rpcRec(connection, session, "command header (for server)", &iov, 1);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000704 status != OK)
705 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000706
Steven Moreland19fc9f72021-06-10 03:57:30 +0000707 return processCommand(connection, session, command, type);
Steven Moreland52eee942021-06-03 00:59:28 +0000708}
709
Steven Moreland5ae62562021-06-10 03:21:42 +0000710status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection,
711 const sp<RpcSession>& session, CommandType type) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +0000712 while (true) {
Andrei Homescu1975aaa2022-03-19 02:34:57 +0000713 status_t status = connection->rpcTransport->pollRead();
Andrei Homescu5ad71b52022-03-11 03:49:12 +0000714 if (status == WOULD_BLOCK) break;
715 if (status != OK) return status;
Andrei Homescu5ad71b52022-03-11 03:49:12 +0000716
717 status = getAndExecuteCommand(connection, session, type);
Steven Moreland52eee942021-06-03 00:59:28 +0000718 if (status != OK) return status;
719 }
720 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000721}
722
Steven Moreland19fc9f72021-06-10 03:57:30 +0000723status_t RpcState::processCommand(const sp<RpcSession::RpcConnection>& connection,
724 const sp<RpcSession>& session, const RpcWireHeader& command,
725 CommandType type) {
Steven Morelandd7302072021-05-15 01:32:04 +0000726 IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull();
727 IPCThreadState::SpGuard spGuard{
728 .address = __builtin_frame_address(0),
729 .context = "processing binder RPC command",
730 };
731 const IPCThreadState::SpGuard* origGuard;
732 if (kernelBinderState != nullptr) {
733 origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard);
734 }
735 ScopeGuard guardUnguard = [&]() {
736 if (kernelBinderState != nullptr) {
737 kernelBinderState->restoreGetCallingSpGuard(origGuard);
738 }
739 };
740
Steven Moreland5553ac42020-11-11 02:14:45 +0000741 switch (command.command) {
742 case RPC_COMMAND_TRANSACT:
Steven Moreland52eee942021-06-03 00:59:28 +0000743 if (type != CommandType::ANY) return BAD_TYPE;
Steven Moreland5ae62562021-06-10 03:21:42 +0000744 return processTransact(connection, session, command);
Steven Moreland5553ac42020-11-11 02:14:45 +0000745 case RPC_COMMAND_DEC_STRONG:
Steven Moreland5ae62562021-06-10 03:21:42 +0000746 return processDecStrong(connection, session, command);
Steven Moreland5553ac42020-11-11 02:14:45 +0000747 }
748
749 // We should always know the version of the opposing side, and since the
750 // RPC-binder-level wire protocol is not self synchronizing, we have no way
751 // to understand where the current command ends and the next one begins. We
752 // also can't consider it a fatal error because this would allow any client
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000753 // to kill us, so ending the session for misbehaving client.
754 ALOGE("Unknown RPC command %d - terminating session", command.command);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000755 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000756 return DEAD_OBJECT;
757}
Steven Moreland5ae62562021-06-10 03:21:42 +0000758status_t RpcState::processTransact(const sp<RpcSession::RpcConnection>& connection,
759 const sp<RpcSession>& session, const RpcWireHeader& command) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000760 LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command);
761
Steven Morelanddbe71832021-05-12 23:31:00 +0000762 CommandData transactionData(command.bodySize);
Steven Morelande8393342021-05-05 23:27:53 +0000763 if (!transactionData.valid()) {
764 return NO_MEMORY;
765 }
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000766 iovec iov{transactionData.data(), transactionData.size()};
767 if (status_t status = rpcRec(connection, session, "transaction body", &iov, 1); status != OK)
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000768 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000769
Steven Moreland5ae62562021-06-10 03:21:42 +0000770 return processTransactInternal(connection, session, std::move(transactionData));
Steven Moreland5553ac42020-11-11 02:14:45 +0000771}
772
Steven Moreland438cce82021-04-02 18:04:08 +0000773static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize,
774 const binder_size_t* objects, size_t objectsCount) {
775 (void)p;
776 (void)data;
777 (void)dataSize;
778 (void)objects;
779 (void)objectsCount;
780}
781
Steven Moreland5ae62562021-06-10 03:21:42 +0000782status_t RpcState::processTransactInternal(const sp<RpcSession::RpcConnection>& connection,
783 const sp<RpcSession>& session,
Steven Morelandada72bd2021-06-09 23:29:13 +0000784 CommandData transactionData) {
785 // for 'recursive' calls to this, we have already read and processed the
786 // binder from the transaction data and taken reference counts into account,
787 // so it is cached here.
Steven Moreland3903bf02021-09-27 16:05:24 -0700788 sp<IBinder> target;
Steven Morelandada72bd2021-06-09 23:29:13 +0000789processTransactInternalTailCall:
790
Steven Moreland5553ac42020-11-11 02:14:45 +0000791 if (transactionData.size() < sizeof(RpcWireTransaction)) {
792 ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!",
793 sizeof(RpcWireTransaction), transactionData.size());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000794 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000795 return BAD_VALUE;
796 }
797 RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data());
798
Steven Moreland5623d1a2021-09-10 15:45:34 -0700799 uint64_t addr = RpcWireAddress::toRaw(transaction->address);
Steven Morelandc7d40132021-06-10 03:42:11 +0000800 bool oneway = transaction->flags & IBinder::FLAG_ONEWAY;
Steven Moreland5553ac42020-11-11 02:14:45 +0000801
802 status_t replyStatus = OK;
Steven Moreland5623d1a2021-09-10 15:45:34 -0700803 if (addr != 0) {
Steven Moreland3903bf02021-09-27 16:05:24 -0700804 if (!target) {
Steven Moreland7227c8a2021-06-02 00:24:32 +0000805 replyStatus = onBinderEntering(session, addr, &target);
Steven Morelandf5174272021-05-25 00:39:28 +0000806 }
807
Steven Moreland7227c8a2021-06-02 00:24:32 +0000808 if (replyStatus != OK) {
809 // do nothing
810 } else if (target == nullptr) {
Steven Morelandf5174272021-05-25 00:39:28 +0000811 // This can happen if the binder is remote in this process, and
812 // another thread has called the last decStrong on this binder.
813 // However, for local binders, it indicates a misbehaving client
814 // (any binder which is being transacted on should be holding a
815 // strong ref count), so in either case, terminating the
816 // session.
Steven Moreland5623d1a2021-09-10 15:45:34 -0700817 ALOGE("While transacting, binder has been deleted at address %" PRIu64 ". Terminating!",
818 addr);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000819 (void)session->shutdownAndWait(false);
Steven Morelandf5174272021-05-25 00:39:28 +0000820 replyStatus = BAD_VALUE;
821 } else if (target->localBinder() == nullptr) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700822 ALOGE("Unknown binder address or non-local binder, not address %" PRIu64
823 ". Terminating!",
824 addr);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000825 (void)session->shutdownAndWait(false);
Steven Morelandf5174272021-05-25 00:39:28 +0000826 replyStatus = BAD_VALUE;
Steven Morelandc7d40132021-06-10 03:42:11 +0000827 } else if (oneway) {
Steven Morelandd45be622021-06-04 02:19:37 +0000828 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Morelandf5174272021-05-25 00:39:28 +0000829 auto it = mNodeForAddress.find(addr);
830 if (it->second.binder.promote() != target) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700831 ALOGE("Binder became invalid during transaction. Bad client? %" PRIu64, addr);
Steven Moreland5553ac42020-11-11 02:14:45 +0000832 replyStatus = BAD_VALUE;
Steven Morelandf5174272021-05-25 00:39:28 +0000833 } else if (transaction->asyncNumber != it->second.asyncNumber) {
834 // we need to process some other asynchronous transaction
835 // first
Steven Morelandf5174272021-05-25 00:39:28 +0000836 it->second.asyncTodo.push(BinderNode::AsyncTodo{
837 .ref = target,
838 .data = std::move(transactionData),
839 .asyncNumber = transaction->asyncNumber,
840 });
Steven Morelandd45be622021-06-04 02:19:37 +0000841
842 size_t numPending = it->second.asyncTodo.size();
Steven Moreland5623d1a2021-09-10 15:45:34 -0700843 LOG_RPC_DETAIL("Enqueuing %" PRIu64 " on %" PRIu64 " (%zu pending)",
844 transaction->asyncNumber, addr, numPending);
Steven Morelandd45be622021-06-04 02:19:37 +0000845
846 constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000;
847 constexpr size_t kArbitraryOnewayCallWarnLevel = 1000;
848 constexpr size_t kArbitraryOnewayCallWarnPer = 1000;
849
850 if (numPending >= kArbitraryOnewayCallWarnLevel) {
851 if (numPending >= kArbitraryOnewayCallTerminateLevel) {
852 ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending);
853 _l.unlock();
854 (void)session->shutdownAndWait(false);
855 return FAILED_TRANSACTION;
856 }
857
858 if (numPending % kArbitraryOnewayCallWarnPer == 0) {
859 ALOGW("Warning: many oneway transactions built up on %p (%zu)",
860 target.get(), numPending);
861 }
862 }
Steven Morelandf5174272021-05-25 00:39:28 +0000863 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000864 }
865 }
866 }
867
Steven Moreland5553ac42020-11-11 02:14:45 +0000868 Parcel reply;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000869 reply.markForRpc(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000870
871 if (replyStatus == OK) {
Frederick Mayle69a0c992022-05-26 20:38:39 +0000872 // Check if the transaction came with any ancillary data.
873 std::vector<base::unique_fd> pendingFds;
874 if (status_t status = connection->rpcTransport->consumePendingAncillaryData(&pendingFds);
875 status != OK) {
876 return status;
877 }
878
Frederick Mayledc07cf82022-05-26 20:30:12 +0000879 Span<const uint8_t> parcelSpan = {transaction->data,
880 transactionData.size() -
881 offsetof(RpcWireTransaction, data)};
Frederick Mayle69a0c992022-05-26 20:38:39 +0000882 Span<const uint32_t> objectTableSpan;
883 if (session->getProtocolVersion().value() >
Frederick Mayledc07cf82022-05-26 20:30:12 +0000884 RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE) {
885 Span<const uint8_t> objectTableBytes = parcelSpan.splitOff(transaction->parcelDataSize);
Frederick Mayle69a0c992022-05-26 20:38:39 +0000886 std::optional<Span<const uint32_t>> maybeSpan =
887 objectTableBytes.reinterpret<const uint32_t>();
888 if (!maybeSpan.has_value()) {
889 ALOGE("Bad object table size inferred from RpcWireTransaction. Saw bodySize=%zu "
890 "sizeofHeader=%zu parcelSize=%" PRId32
891 " objectTableBytesSize=%zu. Terminating!",
892 transactionData.size(), sizeof(RpcWireTransaction),
893 transaction->parcelDataSize, objectTableBytes.size);
894 return BAD_VALUE;
895 }
896 objectTableSpan = *maybeSpan;
Frederick Mayledc07cf82022-05-26 20:30:12 +0000897 }
898
Steven Morelandeff77c12021-04-15 00:37:19 +0000899 Parcel data;
900 // transaction->data is owned by this function. Parcel borrows this data and
901 // only holds onto it for the duration of this function call. Parcel will be
902 // deleted before the 'transactionData' object.
Frederick Mayledc07cf82022-05-26 20:30:12 +0000903
Frederick Mayle69a0c992022-05-26 20:38:39 +0000904 replyStatus = data.rpcSetDataReference(session, parcelSpan.data, parcelSpan.size,
905 objectTableSpan.data, objectTableSpan.size,
906 std::move(pendingFds), do_nothing_to_transact_data);
Steven Morelandeff77c12021-04-15 00:37:19 +0000907
Frederick Mayle69a0c992022-05-26 20:38:39 +0000908 if (replyStatus == OK) {
909 if (target) {
910 bool origAllowNested = connection->allowNested;
911 connection->allowNested = !oneway;
Steven Morelandc7d40132021-06-10 03:42:11 +0000912
Frederick Mayle69a0c992022-05-26 20:38:39 +0000913 replyStatus = target->transact(transaction->code, data, &reply, transaction->flags);
Steven Morelandc7d40132021-06-10 03:42:11 +0000914
Frederick Mayle69a0c992022-05-26 20:38:39 +0000915 connection->allowNested = origAllowNested;
916 } else {
917 LOG_RPC_DETAIL("Got special transaction %u", transaction->code);
Steven Moreland5553ac42020-11-11 02:14:45 +0000918
Frederick Mayle69a0c992022-05-26 20:38:39 +0000919 switch (transaction->code) {
920 case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: {
921 replyStatus = reply.writeInt32(session->getMaxIncomingThreads());
922 break;
923 }
924 case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: {
925 // for client connections, this should always report the value
926 // originally returned from the server, so this is asserting
927 // that it exists
928 replyStatus = reply.writeByteVector(session->mId);
929 break;
930 }
931 default: {
932 sp<RpcServer> server = session->server();
933 if (server) {
934 switch (transaction->code) {
935 case RPC_SPECIAL_TRANSACT_GET_ROOT: {
936 sp<IBinder> root = session->mSessionSpecificRootObject
937 ?: server->getRootObject();
938 replyStatus = reply.writeStrongBinder(root);
939 break;
940 }
941 default: {
942 replyStatus = UNKNOWN_TRANSACTION;
943 }
Steven Moreland103424e2021-06-02 18:16:19 +0000944 }
Frederick Mayle69a0c992022-05-26 20:38:39 +0000945 } else {
946 ALOGE("Special command sent, but no server object attached.");
Steven Moreland103424e2021-06-02 18:16:19 +0000947 }
Steven Morelandf137de92021-04-24 01:54:26 +0000948 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000949 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000950 }
951 }
952 }
953
Steven Morelandc7d40132021-06-10 03:42:11 +0000954 if (oneway) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000955 if (replyStatus != OK) {
956 ALOGW("Oneway call failed with error: %d", replyStatus);
957 }
958
Steven Moreland5623d1a2021-09-10 15:45:34 -0700959 LOG_RPC_DETAIL("Processed async transaction %" PRIu64 " on %" PRIu64,
960 transaction->asyncNumber, addr);
Steven Moreland5553ac42020-11-11 02:14:45 +0000961
962 // Check to see if there is another asynchronous transaction to process.
963 // This behavior differs from binder behavior, since in the binder
964 // driver, asynchronous transactions will be processed after existing
965 // pending binder transactions on the queue. The downside of this is
966 // that asynchronous transactions can be drowned out by synchronous
967 // transactions. However, we have no easy way to queue these
968 // transactions after the synchronous transactions we may want to read
969 // from the wire. So, in socket binder here, we have the opposite
970 // downside: asynchronous transactions may drown out synchronous
971 // transactions.
972 {
973 std::unique_lock<std::mutex> _l(mNodeMutex);
974 auto it = mNodeForAddress.find(addr);
975 // last refcount dropped after this transaction happened
976 if (it == mNodeForAddress.end()) return OK;
977
Steven Morelandc9d7b532021-06-04 20:57:41 +0000978 if (!nodeProgressAsyncNumber(&it->second)) {
979 _l.unlock();
980 (void)session->shutdownAndWait(false);
981 return DEAD_OBJECT;
982 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000983
984 if (it->second.asyncTodo.size() == 0) return OK;
985 if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) {
Steven Moreland5623d1a2021-09-10 15:45:34 -0700986 LOG_RPC_DETAIL("Found next async transaction %" PRIu64 " on %" PRIu64,
987 it->second.asyncNumber, addr);
Steven Moreland5553ac42020-11-11 02:14:45 +0000988
989 // justification for const_cast (consider avoiding priority_queue):
Steven Morelandf5174272021-05-25 00:39:28 +0000990 // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects
Steven Moreland5553ac42020-11-11 02:14:45 +0000991 // - gotta go fast
Steven Morelandf5174272021-05-25 00:39:28 +0000992 auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top());
993
Steven Morelandada72bd2021-06-09 23:29:13 +0000994 // reset up arguments
995 transactionData = std::move(todo.data);
Steven Moreland3903bf02021-09-27 16:05:24 -0700996 LOG_ALWAYS_FATAL_IF(target != todo.ref,
997 "async list should be associated with a binder");
Steven Morelandf5174272021-05-25 00:39:28 +0000998
Steven Moreland5553ac42020-11-11 02:14:45 +0000999 it->second.asyncTodo.pop();
Steven Morelandada72bd2021-06-09 23:29:13 +00001000 goto processTransactInternalTailCall;
Steven Moreland5553ac42020-11-11 02:14:45 +00001001 }
1002 }
Steven Morelandd8083312021-09-22 13:37:10 -07001003
1004 // done processing all the async commands on this binder that we can, so
1005 // write decstrongs on the binder
1006 if (addr != 0 && replyStatus == OK) {
1007 return flushExcessBinderRefs(session, addr, target);
1008 }
1009
Steven Moreland5553ac42020-11-11 02:14:45 +00001010 return OK;
1011 }
1012
Steven Moreland6709cf42021-09-30 15:21:54 -07001013 // Binder refs are flushed for oneway calls only after all calls which are
1014 // built up are executed. Otherwise, they fill up the binder buffer.
1015 if (addr != 0 && replyStatus == OK) {
1016 replyStatus = flushExcessBinderRefs(session, addr, target);
1017 }
1018
Frederick Mayle69a0c992022-05-26 20:38:39 +00001019 std::string errorMsg;
1020 if (status_t status = validateParcel(session, reply, &errorMsg); status != OK) {
1021 ALOGE("Reply Parcel failed validation: %s", errorMsg.c_str());
1022 // Forward the error to the client of the transaction.
1023 reply.freeData();
1024 reply.markForRpc(session);
1025 replyStatus = status;
1026 }
1027
1028 auto* rpcFields = reply.maybeRpcFields();
1029 LOG_ALWAYS_FATAL_IF(rpcFields == nullptr);
1030
Frederick Mayledc07cf82022-05-26 20:30:12 +00001031 const size_t rpcReplyWireSize = RpcWireReply::wireSize(session->getProtocolVersion().value());
1032
Frederick Mayle69a0c992022-05-26 20:38:39 +00001033 Span<const uint32_t> objectTableSpan = Span<const uint32_t>{rpcFields->mObjectPositions.data(),
1034 rpcFields->mObjectPositions.size()};
Frederick Mayledc07cf82022-05-26 20:30:12 +00001035
Frederick Mayle778c0902022-05-27 01:14:57 +00001036 uint32_t bodySize;
Frederick Mayledc07cf82022-05-26 20:30:12 +00001037 LOG_ALWAYS_FATAL_IF(__builtin_add_overflow(rpcReplyWireSize, reply.dataSize(), &bodySize) ||
1038 __builtin_add_overflow(objectTableSpan.byteSize(), bodySize,
1039 &bodySize),
Steven Moreland77c30112021-06-02 20:45:46 +00001040 "Too much data for reply %zu", reply.dataSize());
Steven Moreland77c30112021-06-02 20:45:46 +00001041 RpcWireHeader cmdReply{
1042 .command = RPC_COMMAND_REPLY,
Frederick Mayle778c0902022-05-27 01:14:57 +00001043 .bodySize = bodySize,
Steven Moreland77c30112021-06-02 20:45:46 +00001044 };
Steven Moreland5553ac42020-11-11 02:14:45 +00001045 RpcWireReply rpcReply{
1046 .status = replyStatus,
Frederick Mayledc07cf82022-05-26 20:30:12 +00001047 // NOTE: Not necessarily written to socket depending on session
1048 // version.
1049 // NOTE: bodySize didn't overflow => this cast is safe
1050 .parcelDataSize = static_cast<uint32_t>(reply.dataSize()),
1051 .reserved = {0, 0, 0},
Steven Moreland5553ac42020-11-11 02:14:45 +00001052 };
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001053 iovec iovs[]{
1054 {&cmdReply, sizeof(RpcWireHeader)},
Frederick Mayledc07cf82022-05-26 20:30:12 +00001055 {&rpcReply, rpcReplyWireSize},
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001056 {const_cast<uint8_t*>(reply.data()), reply.dataSize()},
Frederick Mayledc07cf82022-05-26 20:30:12 +00001057 objectTableSpan.toIovec(),
Andrei Homescua39e4ed2021-12-10 08:41:54 +00001058 };
Frederick Mayle69a0c992022-05-26 20:38:39 +00001059 return rpcSend(connection, session, "reply", iovs, arraysize(iovs), std::nullopt,
1060 rpcFields->mFds.get());
Steven Moreland5553ac42020-11-11 02:14:45 +00001061}
1062
Steven Moreland5ae62562021-06-10 03:21:42 +00001063status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection,
1064 const sp<RpcSession>& session, const RpcWireHeader& command) {
Steven Moreland5553ac42020-11-11 02:14:45 +00001065 LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command);
1066
Steven Morelandfd1e8a02021-07-21 23:30:29 +00001067 if (command.bodySize != sizeof(RpcDecStrong)) {
1068 ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcDecStrong. Terminating!",
1069 sizeof(RpcDecStrong), command.bodySize);
Steven Morelandc9d7b532021-06-04 20:57:41 +00001070 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +00001071 return BAD_VALUE;
1072 }
Steven Moreland5553ac42020-11-11 02:14:45 +00001073
Frederick Mayleb86cda42022-06-09 23:17:45 +00001074 RpcDecStrong body;
1075 iovec iov{&body, sizeof(RpcDecStrong)};
1076 if (status_t status = rpcRec(connection, session, "dec ref body", &iov, 1); status != OK)
1077 return status;
1078
1079 uint64_t addr = RpcWireAddress::toRaw(body.address);
Steven Moreland5553ac42020-11-11 02:14:45 +00001080 std::unique_lock<std::mutex> _l(mNodeMutex);
1081 auto it = mNodeForAddress.find(addr);
1082 if (it == mNodeForAddress.end()) {
Steven Moreland5623d1a2021-09-10 15:45:34 -07001083 ALOGE("Unknown binder address %" PRIu64 " for dec strong.", addr);
Steven Moreland5553ac42020-11-11 02:14:45 +00001084 return OK;
1085 }
1086
1087 sp<IBinder> target = it->second.binder.promote();
1088 if (target == nullptr) {
Steven Moreland5623d1a2021-09-10 15:45:34 -07001089 ALOGE("While requesting dec strong, binder has been deleted at address %" PRIu64
1090 ". Terminating!",
1091 addr);
Steven Morelandc9d7b532021-06-04 20:57:41 +00001092 _l.unlock();
1093 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +00001094 return BAD_VALUE;
1095 }
1096
Frederick Mayleb86cda42022-06-09 23:17:45 +00001097 if (it->second.timesSent < body.amount) {
Steven Morelandfd1e8a02021-07-21 23:30:29 +00001098 ALOGE("Record of sending binder %zu times, but requested decStrong for %" PRIu64 " of %u",
Frederick Mayleb86cda42022-06-09 23:17:45 +00001099 it->second.timesSent, addr, body.amount);
Steven Moreland5553ac42020-11-11 02:14:45 +00001100 return OK;
1101 }
1102
Steven Moreland5623d1a2021-09-10 15:45:34 -07001103 LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %" PRIu64,
1104 addr);
Steven Moreland5553ac42020-11-11 02:14:45 +00001105
Frederick Mayleb86cda42022-06-09 23:17:45 +00001106 LOG_RPC_DETAIL("Processing dec strong of %" PRIu64 " by %u from %zu", addr, body.amount,
Steven Morelandfd1e8a02021-07-21 23:30:29 +00001107 it->second.timesSent);
1108
Frederick Mayleb86cda42022-06-09 23:17:45 +00001109 it->second.timesSent -= body.amount;
Steven Moreland31bde7a2021-06-04 00:57:36 +00001110 sp<IBinder> tempHold = tryEraseNode(it);
1111 _l.unlock();
1112 tempHold = nullptr; // destructor may make binder calls on this session
1113
1114 return OK;
1115}
1116
Frederick Mayle69a0c992022-05-26 20:38:39 +00001117status_t RpcState::validateParcel(const sp<RpcSession>& session, const Parcel& parcel,
1118 std::string* errorMsg) {
1119 auto* rpcFields = parcel.maybeRpcFields();
1120 if (rpcFields == nullptr) {
1121 *errorMsg = "Parcel not crafted for RPC call";
1122 return BAD_TYPE;
1123 }
1124
1125 if (rpcFields->mSession != session) {
1126 *errorMsg = "Parcel's session doesn't match";
1127 return BAD_TYPE;
1128 }
1129
1130 uint32_t protocolVersion = session->getProtocolVersion().value();
1131 if (protocolVersion < RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE &&
1132 !rpcFields->mObjectPositions.empty()) {
1133 *errorMsg = StringPrintf("Parcel has attached objects but the session's protocol version "
1134 "(%" PRIu32 ") is too old, must be at least %" PRIu32,
1135 protocolVersion,
1136 RPC_WIRE_PROTOCOL_VERSION_RPC_HEADER_FEATURE_EXPLICIT_PARCEL_SIZE);
1137 return BAD_VALUE;
1138 }
1139
1140 if (rpcFields->mFds && !rpcFields->mFds->empty()) {
1141 switch (session->getFileDescriptorTransportMode()) {
1142 case RpcSession::FileDescriptorTransportMode::NONE:
1143 *errorMsg =
1144 "Parcel has file descriptors, but no file descriptor transport is enabled";
1145 return FDS_NOT_ALLOWED;
1146 case RpcSession::FileDescriptorTransportMode::UNIX: {
1147 constexpr size_t kMaxFdsPerMsg = 253;
1148 if (rpcFields->mFds->size() > kMaxFdsPerMsg) {
1149 *errorMsg = StringPrintf("Too many file descriptors in Parcel for unix "
1150 "domain socket: %zu (max is %zu)",
1151 rpcFields->mFds->size(), kMaxFdsPerMsg);
1152 return BAD_VALUE;
1153 }
1154 }
1155 }
1156 }
1157
1158 return OK;
1159}
1160
Steven Moreland5623d1a2021-09-10 15:45:34 -07001161sp<IBinder> RpcState::tryEraseNode(std::map<uint64_t, BinderNode>::iterator& it) {
Steven Moreland31bde7a2021-06-04 00:57:36 +00001162 sp<IBinder> ref;
1163
Steven Moreland5553ac42020-11-11 02:14:45 +00001164 if (it->second.timesSent == 0) {
Steven Moreland31bde7a2021-06-04 00:57:36 +00001165 ref = std::move(it->second.sentRef);
Steven Moreland5553ac42020-11-11 02:14:45 +00001166
1167 if (it->second.timesRecd == 0) {
Steven Morelanda6e11cf2021-06-04 00:58:31 +00001168 LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(),
1169 "Can't delete binder w/ pending async transactions");
Steven Moreland5553ac42020-11-11 02:14:45 +00001170 mNodeForAddress.erase(it);
1171 }
1172 }
1173
Steven Moreland31bde7a2021-06-04 00:57:36 +00001174 return ref;
Steven Moreland5553ac42020-11-11 02:14:45 +00001175}
1176
Steven Morelandc9d7b532021-06-04 20:57:41 +00001177bool RpcState::nodeProgressAsyncNumber(BinderNode* node) {
Steven Moreland583a14a2021-06-04 02:04:58 +00001178 // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to
1179 // a single binder
1180 if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) {
1181 ALOGE("Out of async transaction IDs. Terminating");
Steven Moreland583a14a2021-06-04 02:04:58 +00001182 return false;
1183 }
1184 node->asyncNumber++;
1185 return true;
1186}
1187
Steven Moreland5553ac42020-11-11 02:14:45 +00001188} // namespace android