blob: 62eb58adbab8d1b646936fff40eafc70119e6aac [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 Morelandd7302072021-05-15 01:32:04 +000021#include <android-base/scopeguard.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000022#include <binder/BpBinder.h>
Steven Morelandd7302072021-05-15 01:32:04 +000023#include <binder/IPCThreadState.h>
Steven Moreland5553ac42020-11-11 02:14:45 +000024#include <binder/RpcServer.h>
25
26#include "Debug.h"
27#include "RpcWireFormat.h"
28
29#include <inttypes.h>
30
31namespace android {
32
Steven Morelandd7302072021-05-15 01:32:04 +000033using base::ScopeGuard;
34
Steven Moreland5553ac42020-11-11 02:14:45 +000035RpcState::RpcState() {}
36RpcState::~RpcState() {}
37
Steven Morelandbdb53ab2021-05-05 17:57:41 +000038status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder,
Steven Moreland5553ac42020-11-11 02:14:45 +000039 RpcAddress* outAddress) {
40 bool isRemote = binder->remoteBinder();
41 bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder();
42
Steven Morelandbdb53ab2021-05-05 17:57:41 +000043 if (isRpc && binder->remoteBinder()->getPrivateAccessorForId().rpcSession() != session) {
Steven Moreland5553ac42020-11-11 02:14:45 +000044 // We need to be able to send instructions over the socket for how to
45 // connect to a different server, and we also need to let the host
46 // process know that this is happening.
Steven Morelandbdb53ab2021-05-05 17:57:41 +000047 ALOGE("Cannot send binder from unrelated binder RPC session.");
Steven Moreland5553ac42020-11-11 02:14:45 +000048 return INVALID_OPERATION;
49 }
50
51 if (isRemote && !isRpc) {
52 // Without additional work, this would have the effect of using this
53 // process to proxy calls from the socket over to the other process, and
54 // it would make those calls look like they come from us (not over the
55 // sockets). In order to make this work transparently like binder, we
56 // would instead need to send instructions over the socket for how to
57 // connect to the host process, and we also need to let the host process
58 // know this was happening.
59 ALOGE("Cannot send binder proxy %p over sockets", binder.get());
60 return INVALID_OPERATION;
61 }
62
63 std::lock_guard<std::mutex> _l(mNodeMutex);
Steven Moreland7227c8a2021-06-02 00:24:32 +000064 if (mTerminated) return DEAD_OBJECT;
Steven Moreland5553ac42020-11-11 02:14:45 +000065
66 // TODO(b/182939933): maybe move address out of BpBinder, and keep binder->address map
67 // in RpcState
68 for (auto& [addr, node] : mNodeForAddress) {
69 if (binder == node.binder) {
70 if (isRpc) {
71 const RpcAddress& actualAddr =
72 binder->remoteBinder()->getPrivateAccessorForId().rpcAddress();
73 // TODO(b/182939933): this is only checking integrity of data structure
74 // a different data structure doesn't need this
75 LOG_ALWAYS_FATAL_IF(addr < actualAddr, "Address mismatch");
76 LOG_ALWAYS_FATAL_IF(actualAddr < addr, "Address mismatch");
77 }
78 node.timesSent++;
79 node.sentRef = binder; // might already be set
80 *outAddress = addr;
81 return OK;
82 }
83 }
84 LOG_ALWAYS_FATAL_IF(isRpc, "RPC binder must have known address at this point");
85
86 auto&& [it, inserted] = mNodeForAddress.insert({RpcAddress::unique(),
87 BinderNode{
88 .binder = binder,
89 .timesSent = 1,
90 .sentRef = binder,
91 }});
92 // TODO(b/182939933): better organization could avoid needing this log
93 LOG_ALWAYS_FATAL_IF(!inserted);
94
95 *outAddress = it->first;
96 return OK;
97}
98
Steven Moreland7227c8a2021-06-02 00:24:32 +000099status_t RpcState::onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address,
100 sp<IBinder>* out) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000101 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Moreland7227c8a2021-06-02 00:24:32 +0000102 if (mTerminated) return DEAD_OBJECT;
Steven Moreland5553ac42020-11-11 02:14:45 +0000103
104 if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) {
Steven Moreland7227c8a2021-06-02 00:24:32 +0000105 *out = it->second.binder.promote();
Steven Moreland5553ac42020-11-11 02:14:45 +0000106
107 // implicitly have strong RPC refcount, since we received this binder
108 it->second.timesRecd++;
109
110 _l.unlock();
111
112 // We have timesRecd RPC refcounts, but we only need to hold on to one
113 // when we keep the object. All additional dec strongs are sent
114 // immediately, we wait to send the last one in BpBinder::onLastDecStrong.
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000115 (void)session->sendDecStrong(address);
Steven Moreland5553ac42020-11-11 02:14:45 +0000116
Steven Moreland7227c8a2021-06-02 00:24:32 +0000117 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000118 }
119
120 auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}});
121 LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy");
122
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000123 // Currently, all binders are assumed to be part of the same session (no
Steven Moreland5553ac42020-11-11 02:14:45 +0000124 // device global binders in the RPC world).
Steven Moreland7227c8a2021-06-02 00:24:32 +0000125 it->second.binder = *out = BpBinder::create(session, it->first);
Steven Moreland5553ac42020-11-11 02:14:45 +0000126 it->second.timesRecd = 1;
Steven Moreland7227c8a2021-06-02 00:24:32 +0000127 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000128}
129
130size_t RpcState::countBinders() {
131 std::lock_guard<std::mutex> _l(mNodeMutex);
132 return mNodeForAddress.size();
133}
134
135void RpcState::dump() {
136 std::lock_guard<std::mutex> _l(mNodeMutex);
Steven Moreland583a14a2021-06-04 02:04:58 +0000137 dumpLocked();
138}
139
Steven Morelandc9d7b532021-06-04 20:57:41 +0000140void RpcState::clear() {
Steven Moreland583a14a2021-06-04 02:04:58 +0000141 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000142
143 if (mTerminated) {
144 LOG_ALWAYS_FATAL_IF(!mNodeForAddress.empty(),
145 "New state should be impossible after terminating!");
146 return;
147 }
148
149 if (SHOULD_LOG_RPC_DETAIL) {
150 ALOGE("RpcState::clear()");
151 dumpLocked();
152 }
153
154 // if the destructor of a binder object makes another RPC call, then calling
155 // decStrong could deadlock. So, we must hold onto these binders until
156 // mNodeMutex is no longer taken.
157 std::vector<sp<IBinder>> tempHoldBinder;
158
159 mTerminated = true;
160 for (auto& [address, node] : mNodeForAddress) {
161 sp<IBinder> binder = node.binder.promote();
162 LOG_ALWAYS_FATAL_IF(binder == nullptr, "Binder %p expected to be owned.", binder.get());
163
164 if (node.sentRef != nullptr) {
165 tempHoldBinder.push_back(node.sentRef);
166 }
167 }
168
169 mNodeForAddress.clear();
170
171 _l.unlock();
172 tempHoldBinder.clear(); // explicit
Steven Moreland583a14a2021-06-04 02:04:58 +0000173}
174
175void RpcState::dumpLocked() {
Steven Moreland5553ac42020-11-11 02:14:45 +0000176 ALOGE("DUMP OF RpcState %p", this);
177 ALOGE("DUMP OF RpcState (%zu nodes)", mNodeForAddress.size());
178 for (const auto& [address, node] : mNodeForAddress) {
179 sp<IBinder> binder = node.binder.promote();
180
181 const char* desc;
182 if (binder) {
183 if (binder->remoteBinder()) {
184 if (binder->remoteBinder()->isRpcBinder()) {
185 desc = "(rpc binder proxy)";
186 } else {
187 desc = "(binder proxy)";
188 }
189 } else {
190 desc = "(local binder)";
191 }
192 } else {
193 desc = "(null)";
194 }
195
196 ALOGE("- BINDER NODE: %p times sent:%zu times recd: %zu a:%s type:%s",
197 node.binder.unsafe_get(), node.timesSent, node.timesRecd, address.toString().c_str(),
198 desc);
199 }
200 ALOGE("END DUMP OF RpcState");
201}
202
Steven Moreland5553ac42020-11-11 02:14:45 +0000203
Steven Morelanddbe71832021-05-12 23:31:00 +0000204RpcState::CommandData::CommandData(size_t size) : mSize(size) {
205 // The maximum size for regular binder is 1MB for all concurrent
206 // transactions. A very small proportion of transactions are even
207 // larger than a page, but we need to avoid allocating too much
208 // data on behalf of an arbitrary client, or we could risk being in
209 // a position where a single additional allocation could run out of
210 // memory.
211 //
212 // Note, this limit may not reflect the total amount of data allocated for a
213 // transaction (in some cases, additional fixed size amounts are added),
214 // though for rough consistency, we should avoid cases where this data type
215 // is used for multiple dynamic allocations for a single transaction.
216 constexpr size_t kMaxTransactionAllocation = 100 * 1000;
217 if (size == 0) return;
218 if (size > kMaxTransactionAllocation) {
219 ALOGW("Transaction requested too much data allocation %zu", size);
220 return;
221 }
222 mData.reset(new (std::nothrow) uint8_t[size]);
223}
224
Steven Morelandc9d7b532021-06-04 20:57:41 +0000225status_t RpcState::rpcSend(const base::unique_fd& fd, const sp<RpcSession>& session,
226 const char* what, const void* data, size_t size) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000227 LOG_RPC_DETAIL("Sending %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str());
228
229 if (size > std::numeric_limits<ssize_t>::max()) {
230 ALOGE("Cannot send %s at size %zu (too big)", what, size);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000231 (void)session->shutdownAndWait(false);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000232 return BAD_VALUE;
Steven Moreland5553ac42020-11-11 02:14:45 +0000233 }
234
Steven Morelandc6ddf362021-04-02 01:13:36 +0000235 ssize_t sent = TEMP_FAILURE_RETRY(send(fd.get(), data, size, MSG_NOSIGNAL));
Steven Moreland5553ac42020-11-11 02:14:45 +0000236
237 if (sent < 0 || sent != static_cast<ssize_t>(size)) {
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000238 int savedErrno = errno;
Steven Morelandc12c9d92021-05-26 18:44:11 +0000239 LOG_RPC_DETAIL("Failed to send %s (sent %zd of %zu bytes) on fd %d, error: %s", what, sent,
240 size, fd.get(), strerror(savedErrno));
Steven Moreland5553ac42020-11-11 02:14:45 +0000241
Steven Morelandc9d7b532021-06-04 20:57:41 +0000242 (void)session->shutdownAndWait(false);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000243 return -savedErrno;
Steven Moreland5553ac42020-11-11 02:14:45 +0000244 }
245
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000246 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000247}
248
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000249status_t RpcState::rpcRec(const base::unique_fd& fd, const sp<RpcSession>& session,
250 const char* what, void* data, size_t size) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000251 if (size > std::numeric_limits<ssize_t>::max()) {
252 ALOGE("Cannot rec %s at size %zu (too big)", what, size);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000253 (void)session->shutdownAndWait(false);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000254 return BAD_VALUE;
Steven Moreland5553ac42020-11-11 02:14:45 +0000255 }
256
Steven Morelandee3f4662021-05-22 01:07:33 +0000257 if (status_t status = session->mShutdownTrigger->interruptableReadFully(fd.get(), data, size);
258 status != OK) {
Steven Morelandc12c9d92021-05-26 18:44:11 +0000259 LOG_RPC_DETAIL("Failed to read %s (%zu bytes) on fd %d, error: %s", what, size, fd.get(),
260 statusToString(status).c_str());
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000261 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000262 }
263
Steven Morelandee3f4662021-05-22 01:07:33 +0000264 LOG_RPC_DETAIL("Received %s on fd %d: %s", what, fd.get(), hexString(data, size).c_str());
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000265 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000266}
267
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000268sp<IBinder> RpcState::getRootObject(const base::unique_fd& fd, const sp<RpcSession>& session) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000269 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000270 data.markForRpc(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000271 Parcel reply;
272
Steven Morelandf5174272021-05-25 00:39:28 +0000273 status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_ROOT, data,
274 session, &reply, 0);
Steven Moreland5553ac42020-11-11 02:14:45 +0000275 if (status != OK) {
276 ALOGE("Error getting root object: %s", statusToString(status).c_str());
277 return nullptr;
278 }
279
280 return reply.readStrongBinder();
281}
282
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000283status_t RpcState::getMaxThreads(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000284 size_t* maxThreadsOut) {
Steven Morelandf137de92021-04-24 01:54:26 +0000285 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000286 data.markForRpc(session);
Steven Morelandf137de92021-04-24 01:54:26 +0000287 Parcel reply;
288
Steven Morelandf5174272021-05-25 00:39:28 +0000289 status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_MAX_THREADS,
290 data, session, &reply, 0);
Steven Morelandf137de92021-04-24 01:54:26 +0000291 if (status != OK) {
292 ALOGE("Error getting max threads: %s", statusToString(status).c_str());
293 return status;
294 }
295
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000296 int32_t maxThreads;
297 status = reply.readInt32(&maxThreads);
Steven Morelandf137de92021-04-24 01:54:26 +0000298 if (status != OK) return status;
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000299 if (maxThreads <= 0) {
300 ALOGE("Error invalid max maxThreads: %d", maxThreads);
Steven Morelandf137de92021-04-24 01:54:26 +0000301 return BAD_VALUE;
302 }
303
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000304 *maxThreadsOut = maxThreads;
305 return OK;
306}
307
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000308status_t RpcState::getSessionId(const base::unique_fd& fd, const sp<RpcSession>& session,
309 int32_t* sessionIdOut) {
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000310 Parcel data;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000311 data.markForRpc(session);
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000312 Parcel reply;
313
Steven Morelandf5174272021-05-25 00:39:28 +0000314 status_t status = transactAddress(fd, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_SESSION_ID,
315 data, session, &reply, 0);
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000316 if (status != OK) {
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000317 ALOGE("Error getting session ID: %s", statusToString(status).c_str());
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000318 return status;
319 }
320
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000321 int32_t sessionId;
322 status = reply.readInt32(&sessionId);
Steven Moreland7c5e6c22021-05-01 02:55:20 +0000323 if (status != OK) return status;
324
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000325 *sessionIdOut = sessionId;
Steven Morelandf137de92021-04-24 01:54:26 +0000326 return OK;
327}
328
Steven Morelandf5174272021-05-25 00:39:28 +0000329status_t RpcState::transact(const base::unique_fd& fd, const sp<IBinder>& binder, uint32_t code,
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000330 const Parcel& data, const sp<RpcSession>& session, Parcel* reply,
Steven Moreland5553ac42020-11-11 02:14:45 +0000331 uint32_t flags) {
Steven Morelandf5174272021-05-25 00:39:28 +0000332 if (!data.isForRpc()) {
333 ALOGE("Refusing to send RPC with parcel not crafted for RPC");
334 return BAD_TYPE;
335 }
336
337 if (data.objectsCount() != 0) {
338 ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data);
339 return BAD_TYPE;
340 }
341
342 RpcAddress address = RpcAddress::zero();
343 if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status;
344
345 return transactAddress(fd, address, code, data, session, reply, flags);
346}
347
348status_t RpcState::transactAddress(const base::unique_fd& fd, const RpcAddress& address,
349 uint32_t code, const Parcel& data, const sp<RpcSession>& session,
350 Parcel* reply, uint32_t flags) {
351 LOG_ALWAYS_FATAL_IF(!data.isForRpc());
352 LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0);
353
Steven Moreland5553ac42020-11-11 02:14:45 +0000354 uint64_t asyncNumber = 0;
355
356 if (!address.isZero()) {
Steven Moreland583a14a2021-06-04 02:04:58 +0000357 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Moreland5553ac42020-11-11 02:14:45 +0000358 if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races
359 auto it = mNodeForAddress.find(address);
360 LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending transact on unknown address %s",
361 address.toString().c_str());
362
363 if (flags & IBinder::FLAG_ONEWAY) {
Steven Moreland583a14a2021-06-04 02:04:58 +0000364 asyncNumber = it->second.asyncNumber;
Steven Morelandc9d7b532021-06-04 20:57:41 +0000365 if (!nodeProgressAsyncNumber(&it->second)) {
366 _l.unlock();
367 (void)session->shutdownAndWait(false);
368 return DEAD_OBJECT;
369 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000370 }
371 }
372
Steven Moreland77c30112021-06-02 20:45:46 +0000373 LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) -
374 sizeof(RpcWireTransaction) <
375 data.dataSize(),
376 "Too much data %zu", data.dataSize());
377
378 RpcWireHeader command{
379 .command = RPC_COMMAND_TRANSACT,
380 .bodySize = static_cast<uint32_t>(sizeof(RpcWireTransaction) + data.dataSize()),
381 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000382 RpcWireTransaction transaction{
383 .address = address.viewRawEmbedded(),
384 .code = code,
385 .flags = flags,
386 .asyncNumber = asyncNumber,
387 };
Steven Moreland77c30112021-06-02 20:45:46 +0000388 CommandData transactionData(sizeof(RpcWireHeader) + sizeof(RpcWireTransaction) +
389 data.dataSize());
Steven Morelande8393342021-05-05 23:27:53 +0000390 if (!transactionData.valid()) {
391 return NO_MEMORY;
392 }
393
Steven Moreland77c30112021-06-02 20:45:46 +0000394 memcpy(transactionData.data() + 0, &command, sizeof(RpcWireHeader));
395 memcpy(transactionData.data() + sizeof(RpcWireHeader), &transaction,
396 sizeof(RpcWireTransaction));
397 memcpy(transactionData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireTransaction), data.data(),
398 data.dataSize());
Steven Moreland5553ac42020-11-11 02:14:45 +0000399
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000400 if (status_t status =
Steven Morelandc9d7b532021-06-04 20:57:41 +0000401 rpcSend(fd, session, "transaction", transactionData.data(), transactionData.size());
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000402 status != OK)
Steven Morelanda5036f02021-06-08 02:26:57 +0000403 // TODO(b/167966510): need to undo onBinderLeaving - we know the
404 // refcount isn't successfully transferred.
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000405 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000406
407 if (flags & IBinder::FLAG_ONEWAY) {
Steven Moreland659416d2021-05-11 00:47:50 +0000408 LOG_RPC_DETAIL("Oneway command, so no longer waiting on %d", fd.get());
Steven Moreland52eee942021-06-03 00:59:28 +0000409
410 // Do not wait on result.
411 // However, too many oneway calls may cause refcounts to build up and fill up the socket,
412 // so process those.
413 return drainCommands(fd, session, CommandType::CONTROL_ONLY);
Steven Moreland5553ac42020-11-11 02:14:45 +0000414 }
415
416 LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction.");
417
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000418 return waitForReply(fd, session, reply);
Steven Moreland5553ac42020-11-11 02:14:45 +0000419}
420
Steven Moreland438cce82021-04-02 18:04:08 +0000421static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize,
422 const binder_size_t* objects, size_t objectsCount) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000423 (void)p;
424 delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data));
425 (void)dataSize;
426 LOG_ALWAYS_FATAL_IF(objects != nullptr);
427 LOG_ALWAYS_FATAL_IF(objectsCount, 0);
428}
429
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000430status_t RpcState::waitForReply(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000431 Parcel* reply) {
432 RpcWireHeader command;
433 while (true) {
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000434 if (status_t status = rpcRec(fd, session, "command header", &command, sizeof(command));
435 status != OK)
436 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000437
438 if (command.command == RPC_COMMAND_REPLY) break;
439
Steven Moreland52eee942021-06-03 00:59:28 +0000440 if (status_t status = processServerCommand(fd, session, command, CommandType::ANY);
441 status != OK)
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000442 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000443 }
444
Steven Morelanddbe71832021-05-12 23:31:00 +0000445 CommandData data(command.bodySize);
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000446 if (!data.valid()) return NO_MEMORY;
Steven Moreland5553ac42020-11-11 02:14:45 +0000447
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000448 if (status_t status = rpcRec(fd, session, "reply body", data.data(), command.bodySize);
449 status != OK)
450 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000451
452 if (command.bodySize < sizeof(RpcWireReply)) {
453 ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!",
454 sizeof(RpcWireReply), command.bodySize);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000455 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000456 return BAD_VALUE;
457 }
Steven Morelande8393342021-05-05 23:27:53 +0000458 RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data());
Steven Moreland5553ac42020-11-11 02:14:45 +0000459 if (rpcReply->status != OK) return rpcReply->status;
460
Steven Morelande8393342021-05-05 23:27:53 +0000461 data.release();
Steven Moreland5553ac42020-11-11 02:14:45 +0000462 reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data),
Steven Moreland438cce82021-04-02 18:04:08 +0000463 nullptr, 0, cleanup_reply_data);
Steven Moreland5553ac42020-11-11 02:14:45 +0000464
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000465 reply->markForRpc(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000466
467 return OK;
468}
469
Steven Morelandc9d7b532021-06-04 20:57:41 +0000470status_t RpcState::sendDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session,
471 const RpcAddress& addr) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000472 {
473 std::lock_guard<std::mutex> _l(mNodeMutex);
474 if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races
475 auto it = mNodeForAddress.find(addr);
476 LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending dec strong on unknown address %s",
477 addr.toString().c_str());
478 LOG_ALWAYS_FATAL_IF(it->second.timesRecd <= 0, "Bad dec strong %s",
479 addr.toString().c_str());
480
481 it->second.timesRecd--;
Steven Moreland31bde7a2021-06-04 00:57:36 +0000482 LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it),
483 "Bad state. RpcState shouldn't own received binder");
Steven Moreland5553ac42020-11-11 02:14:45 +0000484 }
485
486 RpcWireHeader cmd = {
487 .command = RPC_COMMAND_DEC_STRONG,
488 .bodySize = sizeof(RpcWireAddress),
489 };
Steven Morelandc9d7b532021-06-04 20:57:41 +0000490 if (status_t status = rpcSend(fd, session, "dec ref header", &cmd, sizeof(cmd)); status != OK)
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000491 return status;
Steven Morelandc9d7b532021-06-04 20:57:41 +0000492 if (status_t status = rpcSend(fd, session, "dec ref body", &addr.viewRawEmbedded(),
493 sizeof(RpcWireAddress));
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000494 status != OK)
495 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000496 return OK;
497}
498
Steven Moreland52eee942021-06-03 00:59:28 +0000499status_t RpcState::getAndExecuteCommand(const base::unique_fd& fd, const sp<RpcSession>& session,
500 CommandType type) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000501 LOG_RPC_DETAIL("getAndExecuteCommand on fd %d", fd.get());
502
503 RpcWireHeader command;
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000504 if (status_t status = rpcRec(fd, session, "command header", &command, sizeof(command));
505 status != OK)
506 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000507
Steven Moreland52eee942021-06-03 00:59:28 +0000508 return processServerCommand(fd, session, command, type);
509}
510
511status_t RpcState::drainCommands(const base::unique_fd& fd, const sp<RpcSession>& session,
512 CommandType type) {
513 uint8_t buf;
514 while (0 < TEMP_FAILURE_RETRY(recv(fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT))) {
515 status_t status = getAndExecuteCommand(fd, session, type);
516 if (status != OK) return status;
517 }
518 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000519}
520
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000521status_t RpcState::processServerCommand(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland52eee942021-06-03 00:59:28 +0000522 const RpcWireHeader& command, CommandType type) {
Steven Morelandd7302072021-05-15 01:32:04 +0000523 IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull();
524 IPCThreadState::SpGuard spGuard{
525 .address = __builtin_frame_address(0),
526 .context = "processing binder RPC command",
527 };
528 const IPCThreadState::SpGuard* origGuard;
529 if (kernelBinderState != nullptr) {
530 origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard);
531 }
532 ScopeGuard guardUnguard = [&]() {
533 if (kernelBinderState != nullptr) {
534 kernelBinderState->restoreGetCallingSpGuard(origGuard);
535 }
536 };
537
Steven Moreland5553ac42020-11-11 02:14:45 +0000538 switch (command.command) {
539 case RPC_COMMAND_TRANSACT:
Steven Moreland52eee942021-06-03 00:59:28 +0000540 if (type != CommandType::ANY) return BAD_TYPE;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000541 return processTransact(fd, session, command);
Steven Moreland5553ac42020-11-11 02:14:45 +0000542 case RPC_COMMAND_DEC_STRONG:
Steven Morelandee3f4662021-05-22 01:07:33 +0000543 return processDecStrong(fd, session, command);
Steven Moreland5553ac42020-11-11 02:14:45 +0000544 }
545
546 // We should always know the version of the opposing side, and since the
547 // RPC-binder-level wire protocol is not self synchronizing, we have no way
548 // to understand where the current command ends and the next one begins. We
549 // also can't consider it a fatal error because this would allow any client
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000550 // to kill us, so ending the session for misbehaving client.
551 ALOGE("Unknown RPC command %d - terminating session", command.command);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000552 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000553 return DEAD_OBJECT;
554}
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000555status_t RpcState::processTransact(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Moreland5553ac42020-11-11 02:14:45 +0000556 const RpcWireHeader& command) {
557 LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command);
558
Steven Morelanddbe71832021-05-12 23:31:00 +0000559 CommandData transactionData(command.bodySize);
Steven Morelande8393342021-05-05 23:27:53 +0000560 if (!transactionData.valid()) {
561 return NO_MEMORY;
562 }
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000563 if (status_t status = rpcRec(fd, session, "transaction body", transactionData.data(),
564 transactionData.size());
565 status != OK)
566 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000567
Steven Morelandada72bd2021-06-09 23:29:13 +0000568 return processTransactInternal(fd, session, std::move(transactionData));
Steven Moreland5553ac42020-11-11 02:14:45 +0000569}
570
Steven Moreland438cce82021-04-02 18:04:08 +0000571static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize,
572 const binder_size_t* objects, size_t objectsCount) {
573 (void)p;
574 (void)data;
575 (void)dataSize;
576 (void)objects;
577 (void)objectsCount;
578}
579
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000580status_t RpcState::processTransactInternal(const base::unique_fd& fd, const sp<RpcSession>& session,
Steven Morelandada72bd2021-06-09 23:29:13 +0000581 CommandData transactionData) {
582 // for 'recursive' calls to this, we have already read and processed the
583 // binder from the transaction data and taken reference counts into account,
584 // so it is cached here.
585 sp<IBinder> targetRef;
586processTransactInternalTailCall:
587
Steven Moreland5553ac42020-11-11 02:14:45 +0000588 if (transactionData.size() < sizeof(RpcWireTransaction)) {
589 ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!",
590 sizeof(RpcWireTransaction), transactionData.size());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000591 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000592 return BAD_VALUE;
593 }
594 RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data());
595
596 // TODO(b/182939933): heap allocation just for lookup in mNodeForAddress,
597 // maybe add an RpcAddress 'view' if the type remains 'heavy'
598 auto addr = RpcAddress::fromRawEmbedded(&transaction->address);
599
600 status_t replyStatus = OK;
601 sp<IBinder> target;
602 if (!addr.isZero()) {
Steven Morelandf5174272021-05-25 00:39:28 +0000603 if (!targetRef) {
Steven Moreland7227c8a2021-06-02 00:24:32 +0000604 replyStatus = onBinderEntering(session, addr, &target);
Steven Moreland5553ac42020-11-11 02:14:45 +0000605 } else {
Steven Morelandf5174272021-05-25 00:39:28 +0000606 target = targetRef;
607 }
608
Steven Moreland7227c8a2021-06-02 00:24:32 +0000609 if (replyStatus != OK) {
610 // do nothing
611 } else if (target == nullptr) {
Steven Morelandf5174272021-05-25 00:39:28 +0000612 // This can happen if the binder is remote in this process, and
613 // another thread has called the last decStrong on this binder.
614 // However, for local binders, it indicates a misbehaving client
615 // (any binder which is being transacted on should be holding a
616 // strong ref count), so in either case, terminating the
617 // session.
618 ALOGE("While transacting, binder has been deleted at address %s. Terminating!",
619 addr.toString().c_str());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000620 (void)session->shutdownAndWait(false);
Steven Morelandf5174272021-05-25 00:39:28 +0000621 replyStatus = BAD_VALUE;
622 } else if (target->localBinder() == nullptr) {
623 ALOGE("Unknown binder address or non-local binder, not address %s. Terminating!",
624 addr.toString().c_str());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000625 (void)session->shutdownAndWait(false);
Steven Morelandf5174272021-05-25 00:39:28 +0000626 replyStatus = BAD_VALUE;
627 } else if (transaction->flags & IBinder::FLAG_ONEWAY) {
Steven Morelandd45be622021-06-04 02:19:37 +0000628 std::unique_lock<std::mutex> _l(mNodeMutex);
Steven Morelandf5174272021-05-25 00:39:28 +0000629 auto it = mNodeForAddress.find(addr);
630 if (it->second.binder.promote() != target) {
631 ALOGE("Binder became invalid during transaction. Bad client? %s",
Steven Moreland5553ac42020-11-11 02:14:45 +0000632 addr.toString().c_str());
Steven Moreland5553ac42020-11-11 02:14:45 +0000633 replyStatus = BAD_VALUE;
Steven Morelandf5174272021-05-25 00:39:28 +0000634 } else if (transaction->asyncNumber != it->second.asyncNumber) {
635 // we need to process some other asynchronous transaction
636 // first
Steven Morelandf5174272021-05-25 00:39:28 +0000637 it->second.asyncTodo.push(BinderNode::AsyncTodo{
638 .ref = target,
639 .data = std::move(transactionData),
640 .asyncNumber = transaction->asyncNumber,
641 });
Steven Morelandd45be622021-06-04 02:19:37 +0000642
643 size_t numPending = it->second.asyncTodo.size();
644 LOG_RPC_DETAIL("Enqueuing %" PRId64 " on %s (%zu pending)",
645 transaction->asyncNumber, addr.toString().c_str(), numPending);
646
647 constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000;
648 constexpr size_t kArbitraryOnewayCallWarnLevel = 1000;
649 constexpr size_t kArbitraryOnewayCallWarnPer = 1000;
650
651 if (numPending >= kArbitraryOnewayCallWarnLevel) {
652 if (numPending >= kArbitraryOnewayCallTerminateLevel) {
653 ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending);
654 _l.unlock();
655 (void)session->shutdownAndWait(false);
656 return FAILED_TRANSACTION;
657 }
658
659 if (numPending % kArbitraryOnewayCallWarnPer == 0) {
660 ALOGW("Warning: many oneway transactions built up on %p (%zu)",
661 target.get(), numPending);
662 }
663 }
Steven Morelandf5174272021-05-25 00:39:28 +0000664 return OK;
Steven Moreland5553ac42020-11-11 02:14:45 +0000665 }
666 }
667 }
668
Steven Moreland5553ac42020-11-11 02:14:45 +0000669 Parcel reply;
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000670 reply.markForRpc(session);
Steven Moreland5553ac42020-11-11 02:14:45 +0000671
672 if (replyStatus == OK) {
Steven Morelandeff77c12021-04-15 00:37:19 +0000673 Parcel data;
674 // transaction->data is owned by this function. Parcel borrows this data and
675 // only holds onto it for the duration of this function call. Parcel will be
676 // deleted before the 'transactionData' object.
677 data.ipcSetDataReference(transaction->data,
678 transactionData.size() - offsetof(RpcWireTransaction, data),
679 nullptr /*object*/, 0 /*objectCount*/,
680 do_nothing_to_transact_data);
Steven Morelandbdb53ab2021-05-05 17:57:41 +0000681 data.markForRpc(session);
Steven Morelandeff77c12021-04-15 00:37:19 +0000682
Steven Moreland5553ac42020-11-11 02:14:45 +0000683 if (target) {
684 replyStatus = target->transact(transaction->code, data, &reply, transaction->flags);
685 } else {
686 LOG_RPC_DETAIL("Got special transaction %u", transaction->code);
Steven Moreland5553ac42020-11-11 02:14:45 +0000687
Steven Moreland103424e2021-06-02 18:16:19 +0000688 switch (transaction->code) {
689 case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: {
690 replyStatus = reply.writeInt32(session->getMaxThreads());
691 break;
692 }
693 case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: {
694 // for client connections, this should always report the value
695 // originally returned from the server
696 int32_t id = session->mId.value();
697 replyStatus = reply.writeInt32(id);
698 break;
699 }
700 default: {
701 sp<RpcServer> server = session->server().promote();
702 if (server) {
703 switch (transaction->code) {
704 case RPC_SPECIAL_TRANSACT_GET_ROOT: {
705 replyStatus = reply.writeStrongBinder(server->getRootObject());
706 break;
707 }
708 default: {
709 replyStatus = UNKNOWN_TRANSACTION;
710 }
711 }
712 } else {
713 ALOGE("Special command sent, but no server object attached.");
Steven Morelandf137de92021-04-24 01:54:26 +0000714 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000715 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000716 }
717 }
718 }
719
720 if (transaction->flags & IBinder::FLAG_ONEWAY) {
721 if (replyStatus != OK) {
722 ALOGW("Oneway call failed with error: %d", replyStatus);
723 }
724
725 LOG_RPC_DETAIL("Processed async transaction %" PRId64 " on %s", transaction->asyncNumber,
726 addr.toString().c_str());
727
728 // Check to see if there is another asynchronous transaction to process.
729 // This behavior differs from binder behavior, since in the binder
730 // driver, asynchronous transactions will be processed after existing
731 // pending binder transactions on the queue. The downside of this is
732 // that asynchronous transactions can be drowned out by synchronous
733 // transactions. However, we have no easy way to queue these
734 // transactions after the synchronous transactions we may want to read
735 // from the wire. So, in socket binder here, we have the opposite
736 // downside: asynchronous transactions may drown out synchronous
737 // transactions.
738 {
739 std::unique_lock<std::mutex> _l(mNodeMutex);
740 auto it = mNodeForAddress.find(addr);
741 // last refcount dropped after this transaction happened
742 if (it == mNodeForAddress.end()) return OK;
743
Steven Morelandc9d7b532021-06-04 20:57:41 +0000744 if (!nodeProgressAsyncNumber(&it->second)) {
745 _l.unlock();
746 (void)session->shutdownAndWait(false);
747 return DEAD_OBJECT;
748 }
Steven Moreland5553ac42020-11-11 02:14:45 +0000749
750 if (it->second.asyncTodo.size() == 0) return OK;
751 if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) {
752 LOG_RPC_DETAIL("Found next async transaction %" PRId64 " on %s",
753 it->second.asyncNumber, addr.toString().c_str());
754
755 // justification for const_cast (consider avoiding priority_queue):
Steven Morelandf5174272021-05-25 00:39:28 +0000756 // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects
Steven Moreland5553ac42020-11-11 02:14:45 +0000757 // - gotta go fast
Steven Morelandf5174272021-05-25 00:39:28 +0000758 auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top());
759
Steven Morelandada72bd2021-06-09 23:29:13 +0000760 // reset up arguments
761 transactionData = std::move(todo.data);
762 targetRef = std::move(todo.ref);
Steven Morelandf5174272021-05-25 00:39:28 +0000763
Steven Moreland5553ac42020-11-11 02:14:45 +0000764 it->second.asyncTodo.pop();
Steven Morelandada72bd2021-06-09 23:29:13 +0000765 goto processTransactInternalTailCall;
Steven Moreland5553ac42020-11-11 02:14:45 +0000766 }
767 }
768 return OK;
769 }
770
Steven Moreland77c30112021-06-02 20:45:46 +0000771 LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) -
772 sizeof(RpcWireReply) <
773 reply.dataSize(),
774 "Too much data for reply %zu", reply.dataSize());
775
776 RpcWireHeader cmdReply{
777 .command = RPC_COMMAND_REPLY,
778 .bodySize = static_cast<uint32_t>(sizeof(RpcWireReply) + reply.dataSize()),
779 };
Steven Moreland5553ac42020-11-11 02:14:45 +0000780 RpcWireReply rpcReply{
781 .status = replyStatus,
782 };
783
Steven Moreland77c30112021-06-02 20:45:46 +0000784 CommandData replyData(sizeof(RpcWireHeader) + sizeof(RpcWireReply) + reply.dataSize());
Steven Morelande8393342021-05-05 23:27:53 +0000785 if (!replyData.valid()) {
786 return NO_MEMORY;
787 }
Steven Moreland77c30112021-06-02 20:45:46 +0000788 memcpy(replyData.data() + 0, &cmdReply, sizeof(RpcWireHeader));
789 memcpy(replyData.data() + sizeof(RpcWireHeader), &rpcReply, sizeof(RpcWireReply));
790 memcpy(replyData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireReply), reply.data(),
791 reply.dataSize());
Steven Moreland5553ac42020-11-11 02:14:45 +0000792
Steven Morelandc9d7b532021-06-04 20:57:41 +0000793 return rpcSend(fd, session, "reply", replyData.data(), replyData.size());
Steven Moreland5553ac42020-11-11 02:14:45 +0000794}
795
Steven Morelandee3f4662021-05-22 01:07:33 +0000796status_t RpcState::processDecStrong(const base::unique_fd& fd, const sp<RpcSession>& session,
797 const RpcWireHeader& command) {
Steven Moreland5553ac42020-11-11 02:14:45 +0000798 LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command);
799
Steven Morelanddbe71832021-05-12 23:31:00 +0000800 CommandData commandData(command.bodySize);
Steven Morelande8393342021-05-05 23:27:53 +0000801 if (!commandData.valid()) {
802 return NO_MEMORY;
803 }
Steven Moreland1e4c2b82021-05-25 01:51:31 +0000804 if (status_t status =
805 rpcRec(fd, session, "dec ref body", commandData.data(), commandData.size());
806 status != OK)
807 return status;
Steven Moreland5553ac42020-11-11 02:14:45 +0000808
809 if (command.bodySize < sizeof(RpcWireAddress)) {
810 ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireAddress. Terminating!",
811 sizeof(RpcWireAddress), command.bodySize);
Steven Morelandc9d7b532021-06-04 20:57:41 +0000812 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000813 return BAD_VALUE;
814 }
815 RpcWireAddress* address = reinterpret_cast<RpcWireAddress*>(commandData.data());
816
817 // TODO(b/182939933): heap allocation just for lookup
818 auto addr = RpcAddress::fromRawEmbedded(address);
819 std::unique_lock<std::mutex> _l(mNodeMutex);
820 auto it = mNodeForAddress.find(addr);
821 if (it == mNodeForAddress.end()) {
822 ALOGE("Unknown binder address %s for dec strong.", addr.toString().c_str());
Steven Moreland5553ac42020-11-11 02:14:45 +0000823 return OK;
824 }
825
826 sp<IBinder> target = it->second.binder.promote();
827 if (target == nullptr) {
828 ALOGE("While requesting dec strong, binder has been deleted at address %s. Terminating!",
829 addr.toString().c_str());
Steven Morelandc9d7b532021-06-04 20:57:41 +0000830 _l.unlock();
831 (void)session->shutdownAndWait(false);
Steven Moreland5553ac42020-11-11 02:14:45 +0000832 return BAD_VALUE;
833 }
834
835 if (it->second.timesSent == 0) {
836 ALOGE("No record of sending binder, but requested decStrong: %s", addr.toString().c_str());
837 return OK;
838 }
839
840 LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %s",
841 addr.toString().c_str());
842
Steven Moreland5553ac42020-11-11 02:14:45 +0000843 it->second.timesSent--;
Steven Moreland31bde7a2021-06-04 00:57:36 +0000844 sp<IBinder> tempHold = tryEraseNode(it);
845 _l.unlock();
846 tempHold = nullptr; // destructor may make binder calls on this session
847
848 return OK;
849}
850
851sp<IBinder> RpcState::tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it) {
852 sp<IBinder> ref;
853
Steven Moreland5553ac42020-11-11 02:14:45 +0000854 if (it->second.timesSent == 0) {
Steven Moreland31bde7a2021-06-04 00:57:36 +0000855 ref = std::move(it->second.sentRef);
Steven Moreland5553ac42020-11-11 02:14:45 +0000856
857 if (it->second.timesRecd == 0) {
Steven Morelanda6e11cf2021-06-04 00:58:31 +0000858 LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(),
859 "Can't delete binder w/ pending async transactions");
Steven Moreland5553ac42020-11-11 02:14:45 +0000860 mNodeForAddress.erase(it);
861 }
862 }
863
Steven Moreland31bde7a2021-06-04 00:57:36 +0000864 return ref;
Steven Moreland5553ac42020-11-11 02:14:45 +0000865}
866
Steven Morelandc9d7b532021-06-04 20:57:41 +0000867bool RpcState::nodeProgressAsyncNumber(BinderNode* node) {
Steven Moreland583a14a2021-06-04 02:04:58 +0000868 // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to
869 // a single binder
870 if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) {
871 ALOGE("Out of async transaction IDs. Terminating");
Steven Moreland583a14a2021-06-04 02:04:58 +0000872 return false;
873 }
874 node->asyncNumber++;
875 return true;
876}
877
Steven Moreland5553ac42020-11-11 02:14:45 +0000878} // namespace android