Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 1 | /* |
| 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 Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 21 | #include <android-base/scopeguard.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 22 | #include <binder/BpBinder.h> |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 23 | #include <binder/IPCThreadState.h> |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 24 | #include <binder/RpcServer.h> |
| 25 | |
| 26 | #include "Debug.h" |
| 27 | #include "RpcWireFormat.h" |
| 28 | |
| 29 | #include <inttypes.h> |
| 30 | |
| 31 | namespace android { |
| 32 | |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 33 | using base::ScopeGuard; |
| 34 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 35 | RpcState::RpcState() {} |
| 36 | RpcState::~RpcState() {} |
| 37 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 38 | status_t RpcState::onBinderLeaving(const sp<RpcSession>& session, const sp<IBinder>& binder, |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 39 | RpcAddress* outAddress) { |
| 40 | bool isRemote = binder->remoteBinder(); |
| 41 | bool isRpc = isRemote && binder->remoteBinder()->isRpcBinder(); |
| 42 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 43 | if (isRpc && binder->remoteBinder()->getPrivateAccessorForId().rpcSession() != session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 44 | // 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 Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 47 | ALOGE("Cannot send binder from unrelated binder RPC session."); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 48 | 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 Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 64 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 65 | |
| 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 Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 99 | status_t RpcState::onBinderEntering(const sp<RpcSession>& session, const RpcAddress& address, |
| 100 | sp<IBinder>* out) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 101 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 102 | if (mTerminated) return DEAD_OBJECT; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 103 | |
| 104 | if (auto it = mNodeForAddress.find(address); it != mNodeForAddress.end()) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 105 | *out = it->second.binder.promote(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 106 | |
| 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 Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 115 | (void)session->sendDecStrong(address); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 116 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 117 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | auto&& [it, inserted] = mNodeForAddress.insert({address, BinderNode{}}); |
| 121 | LOG_ALWAYS_FATAL_IF(!inserted, "Failed to insert binder when creating proxy"); |
| 122 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 123 | // Currently, all binders are assumed to be part of the same session (no |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 124 | // device global binders in the RPC world). |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 125 | it->second.binder = *out = BpBinder::create(session, it->first); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 126 | it->second.timesRecd = 1; |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 127 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | size_t RpcState::countBinders() { |
| 131 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 132 | return mNodeForAddress.size(); |
| 133 | } |
| 134 | |
| 135 | void RpcState::dump() { |
| 136 | std::lock_guard<std::mutex> _l(mNodeMutex); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 137 | dumpLocked(); |
| 138 | } |
| 139 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 140 | void RpcState::clear() { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 141 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 142 | |
| 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 Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | void RpcState::dumpLocked() { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 176 | 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 Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 203 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 204 | RpcState::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 Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 225 | status_t RpcState::rpcSend(const sp<RpcSession::RpcConnection>& connection, |
| 226 | const sp<RpcSession>& session, const char* what, const void* data, |
| 227 | size_t size) { |
| 228 | LOG_RPC_DETAIL("Sending %s on fd %d: %s", what, connection->fd.get(), |
| 229 | hexString(data, size).c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 230 | |
| 231 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 232 | ALOGE("Cannot send %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 233 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 234 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 237 | ssize_t sent = TEMP_FAILURE_RETRY(send(connection->fd.get(), data, size, MSG_NOSIGNAL)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 238 | |
| 239 | if (sent < 0 || sent != static_cast<ssize_t>(size)) { |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 240 | int savedErrno = errno; |
Steven Moreland | c12c9d9 | 2021-05-26 18:44:11 +0000 | [diff] [blame] | 241 | LOG_RPC_DETAIL("Failed to send %s (sent %zd of %zu bytes) on fd %d, error: %s", what, sent, |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 242 | size, connection->fd.get(), strerror(savedErrno)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 243 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 244 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 245 | return -savedErrno; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 246 | } |
| 247 | |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 248 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 251 | status_t RpcState::rpcRec(const sp<RpcSession::RpcConnection>& connection, |
| 252 | const sp<RpcSession>& session, const char* what, void* data, |
| 253 | size_t size) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 254 | if (size > std::numeric_limits<ssize_t>::max()) { |
| 255 | ALOGE("Cannot rec %s at size %zu (too big)", what, size); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 256 | (void)session->shutdownAndWait(false); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 257 | return BAD_VALUE; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 258 | } |
| 259 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 260 | if (status_t status = |
| 261 | session->mShutdownTrigger->interruptableReadFully(connection->fd.get(), data, size); |
Steven Moreland | ee3f466 | 2021-05-22 01:07:33 +0000 | [diff] [blame] | 262 | status != OK) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 263 | LOG_RPC_DETAIL("Failed to read %s (%zu bytes) on fd %d, error: %s", what, size, |
| 264 | connection->fd.get(), statusToString(status).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 265 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 268 | LOG_RPC_DETAIL("Received %s on fd %d: %s", what, connection->fd.get(), |
| 269 | hexString(data, size).c_str()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 270 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 273 | status_t RpcState::sendConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 274 | const sp<RpcSession>& session) { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 275 | RpcClientConnectionInit init{ |
| 276 | .msg = RPC_CONNECTION_INIT_OKAY, |
| 277 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 278 | return rpcSend(connection, session, "connection init", &init, sizeof(init)); |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 281 | status_t RpcState::readConnectionInit(const sp<RpcSession::RpcConnection>& connection, |
| 282 | const sp<RpcSession>& session) { |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 283 | RpcClientConnectionInit init; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 284 | if (status_t status = rpcRec(connection, session, "connection init", &init, sizeof(init)); |
| 285 | status != OK) |
Steven Moreland | c88b7fc | 2021-06-10 00:40:39 +0000 | [diff] [blame] | 286 | return status; |
| 287 | |
| 288 | static_assert(sizeof(init.msg) == sizeof(RPC_CONNECTION_INIT_OKAY)); |
| 289 | if (0 != strncmp(init.msg, RPC_CONNECTION_INIT_OKAY, sizeof(init.msg))) { |
| 290 | ALOGE("Connection init message unrecognized %.*s", static_cast<int>(sizeof(init.msg)), |
| 291 | init.msg); |
| 292 | return BAD_VALUE; |
| 293 | } |
| 294 | return OK; |
| 295 | } |
| 296 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 297 | sp<IBinder> RpcState::getRootObject(const sp<RpcSession::RpcConnection>& connection, |
| 298 | const sp<RpcSession>& session) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 299 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 300 | data.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 301 | Parcel reply; |
| 302 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 303 | status_t status = transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_ROOT, |
| 304 | data, session, &reply, 0); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 305 | if (status != OK) { |
| 306 | ALOGE("Error getting root object: %s", statusToString(status).c_str()); |
| 307 | return nullptr; |
| 308 | } |
| 309 | |
| 310 | return reply.readStrongBinder(); |
| 311 | } |
| 312 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 313 | status_t RpcState::getMaxThreads(const sp<RpcSession::RpcConnection>& connection, |
| 314 | const sp<RpcSession>& session, size_t* maxThreadsOut) { |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 315 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 316 | data.markForRpc(session); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 317 | Parcel reply; |
| 318 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 319 | status_t status = |
| 320 | transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_MAX_THREADS, |
| 321 | data, session, &reply, 0); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 322 | if (status != OK) { |
| 323 | ALOGE("Error getting max threads: %s", statusToString(status).c_str()); |
| 324 | return status; |
| 325 | } |
| 326 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 327 | int32_t maxThreads; |
| 328 | status = reply.readInt32(&maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 329 | if (status != OK) return status; |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 330 | if (maxThreads <= 0) { |
| 331 | ALOGE("Error invalid max maxThreads: %d", maxThreads); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 332 | return BAD_VALUE; |
| 333 | } |
| 334 | |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 335 | *maxThreadsOut = maxThreads; |
| 336 | return OK; |
| 337 | } |
| 338 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 339 | status_t RpcState::getSessionId(const sp<RpcSession::RpcConnection>& connection, |
| 340 | const sp<RpcSession>& session, int32_t* sessionIdOut) { |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 341 | Parcel data; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 342 | data.markForRpc(session); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 343 | Parcel reply; |
| 344 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 345 | status_t status = |
| 346 | transactAddress(connection, RpcAddress::zero(), RPC_SPECIAL_TRANSACT_GET_SESSION_ID, |
| 347 | data, session, &reply, 0); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 348 | if (status != OK) { |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 349 | ALOGE("Error getting session ID: %s", statusToString(status).c_str()); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 350 | return status; |
| 351 | } |
| 352 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 353 | int32_t sessionId; |
| 354 | status = reply.readInt32(&sessionId); |
Steven Moreland | 7c5e6c2 | 2021-05-01 02:55:20 +0000 | [diff] [blame] | 355 | if (status != OK) return status; |
| 356 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 357 | *sessionIdOut = sessionId; |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 358 | return OK; |
| 359 | } |
| 360 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 361 | status_t RpcState::transact(const sp<RpcSession::RpcConnection>& connection, |
| 362 | const sp<IBinder>& binder, uint32_t code, const Parcel& data, |
| 363 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 364 | if (!data.isForRpc()) { |
| 365 | ALOGE("Refusing to send RPC with parcel not crafted for RPC"); |
| 366 | return BAD_TYPE; |
| 367 | } |
| 368 | |
| 369 | if (data.objectsCount() != 0) { |
| 370 | ALOGE("Parcel at %p has attached objects but is being used in an RPC call", &data); |
| 371 | return BAD_TYPE; |
| 372 | } |
| 373 | |
| 374 | RpcAddress address = RpcAddress::zero(); |
| 375 | if (status_t status = onBinderLeaving(session, binder, &address); status != OK) return status; |
| 376 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 377 | return transactAddress(connection, address, code, data, session, reply, flags); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 380 | status_t RpcState::transactAddress(const sp<RpcSession::RpcConnection>& connection, |
| 381 | const RpcAddress& address, uint32_t code, const Parcel& data, |
| 382 | const sp<RpcSession>& session, Parcel* reply, uint32_t flags) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 383 | LOG_ALWAYS_FATAL_IF(!data.isForRpc()); |
| 384 | LOG_ALWAYS_FATAL_IF(data.objectsCount() != 0); |
| 385 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 386 | uint64_t asyncNumber = 0; |
| 387 | |
| 388 | if (!address.isZero()) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 389 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 390 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 391 | auto it = mNodeForAddress.find(address); |
| 392 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending transact on unknown address %s", |
| 393 | address.toString().c_str()); |
| 394 | |
| 395 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 396 | asyncNumber = it->second.asyncNumber; |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 397 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 398 | _l.unlock(); |
| 399 | (void)session->shutdownAndWait(false); |
| 400 | return DEAD_OBJECT; |
| 401 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 405 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 406 | sizeof(RpcWireTransaction) < |
| 407 | data.dataSize(), |
| 408 | "Too much data %zu", data.dataSize()); |
| 409 | |
| 410 | RpcWireHeader command{ |
| 411 | .command = RPC_COMMAND_TRANSACT, |
| 412 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireTransaction) + data.dataSize()), |
| 413 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 414 | RpcWireTransaction transaction{ |
| 415 | .address = address.viewRawEmbedded(), |
| 416 | .code = code, |
| 417 | .flags = flags, |
| 418 | .asyncNumber = asyncNumber, |
| 419 | }; |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 420 | CommandData transactionData(sizeof(RpcWireHeader) + sizeof(RpcWireTransaction) + |
| 421 | data.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 422 | if (!transactionData.valid()) { |
| 423 | return NO_MEMORY; |
| 424 | } |
| 425 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 426 | memcpy(transactionData.data() + 0, &command, sizeof(RpcWireHeader)); |
| 427 | memcpy(transactionData.data() + sizeof(RpcWireHeader), &transaction, |
| 428 | sizeof(RpcWireTransaction)); |
| 429 | memcpy(transactionData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireTransaction), data.data(), |
| 430 | data.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 431 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 432 | if (status_t status = rpcSend(connection, session, "transaction", transactionData.data(), |
| 433 | transactionData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 434 | status != OK) |
Steven Moreland | a5036f0 | 2021-06-08 02:26:57 +0000 | [diff] [blame] | 435 | // TODO(b/167966510): need to undo onBinderLeaving - we know the |
| 436 | // refcount isn't successfully transferred. |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 437 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 438 | |
| 439 | if (flags & IBinder::FLAG_ONEWAY) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 440 | LOG_RPC_DETAIL("Oneway command, so no longer waiting on %d", connection->fd.get()); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 441 | |
| 442 | // Do not wait on result. |
| 443 | // However, too many oneway calls may cause refcounts to build up and fill up the socket, |
| 444 | // so process those. |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 445 | return drainCommands(connection, session, CommandType::CONTROL_ONLY); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | LOG_ALWAYS_FATAL_IF(reply == nullptr, "Reply parcel must be used for synchronous transaction."); |
| 449 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 450 | return waitForReply(connection, session, reply); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 451 | } |
| 452 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 453 | static void cleanup_reply_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 454 | const binder_size_t* objects, size_t objectsCount) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 455 | (void)p; |
| 456 | delete[] const_cast<uint8_t*>(data - offsetof(RpcWireReply, data)); |
| 457 | (void)dataSize; |
| 458 | LOG_ALWAYS_FATAL_IF(objects != nullptr); |
| 459 | LOG_ALWAYS_FATAL_IF(objectsCount, 0); |
| 460 | } |
| 461 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 462 | status_t RpcState::waitForReply(const sp<RpcSession::RpcConnection>& connection, |
| 463 | const sp<RpcSession>& session, Parcel* reply) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 464 | RpcWireHeader command; |
| 465 | while (true) { |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 466 | if (status_t status = |
| 467 | rpcRec(connection, session, "command header", &command, sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 468 | status != OK) |
| 469 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 470 | |
| 471 | if (command.command == RPC_COMMAND_REPLY) break; |
| 472 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 473 | if (status_t status = processServerCommand(connection, session, command, CommandType::ANY); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 474 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 475 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 478 | CommandData data(command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 479 | if (!data.valid()) return NO_MEMORY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 480 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 481 | if (status_t status = rpcRec(connection, session, "reply body", data.data(), command.bodySize); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 482 | status != OK) |
| 483 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 484 | |
| 485 | if (command.bodySize < sizeof(RpcWireReply)) { |
| 486 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireReply. Terminating!", |
| 487 | sizeof(RpcWireReply), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 488 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 489 | return BAD_VALUE; |
| 490 | } |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 491 | RpcWireReply* rpcReply = reinterpret_cast<RpcWireReply*>(data.data()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 492 | if (rpcReply->status != OK) return rpcReply->status; |
| 493 | |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 494 | data.release(); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 495 | reply->ipcSetDataReference(rpcReply->data, command.bodySize - offsetof(RpcWireReply, data), |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 496 | nullptr, 0, cleanup_reply_data); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 497 | |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 498 | reply->markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 499 | |
| 500 | return OK; |
| 501 | } |
| 502 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 503 | status_t RpcState::sendDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 504 | const sp<RpcSession>& session, const RpcAddress& addr) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 505 | { |
| 506 | std::lock_guard<std::mutex> _l(mNodeMutex); |
| 507 | if (mTerminated) return DEAD_OBJECT; // avoid fatal only, otherwise races |
| 508 | auto it = mNodeForAddress.find(addr); |
| 509 | LOG_ALWAYS_FATAL_IF(it == mNodeForAddress.end(), "Sending dec strong on unknown address %s", |
| 510 | addr.toString().c_str()); |
| 511 | LOG_ALWAYS_FATAL_IF(it->second.timesRecd <= 0, "Bad dec strong %s", |
| 512 | addr.toString().c_str()); |
| 513 | |
| 514 | it->second.timesRecd--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 515 | LOG_ALWAYS_FATAL_IF(nullptr != tryEraseNode(it), |
| 516 | "Bad state. RpcState shouldn't own received binder"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | RpcWireHeader cmd = { |
| 520 | .command = RPC_COMMAND_DEC_STRONG, |
| 521 | .bodySize = sizeof(RpcWireAddress), |
| 522 | }; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 523 | if (status_t status = rpcSend(connection, session, "dec ref header", &cmd, sizeof(cmd)); |
| 524 | status != OK) |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 525 | return status; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 526 | if (status_t status = rpcSend(connection, session, "dec ref body", &addr.viewRawEmbedded(), |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 527 | sizeof(RpcWireAddress)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 528 | status != OK) |
| 529 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 530 | return OK; |
| 531 | } |
| 532 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 533 | status_t RpcState::getAndExecuteCommand(const sp<RpcSession::RpcConnection>& connection, |
| 534 | const sp<RpcSession>& session, CommandType type) { |
| 535 | LOG_RPC_DETAIL("getAndExecuteCommand on fd %d", connection->fd.get()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 536 | |
| 537 | RpcWireHeader command; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 538 | if (status_t status = rpcRec(connection, session, "command header", &command, sizeof(command)); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 539 | status != OK) |
| 540 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 541 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 542 | return processServerCommand(connection, session, command, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 545 | status_t RpcState::drainCommands(const sp<RpcSession::RpcConnection>& connection, |
| 546 | const sp<RpcSession>& session, CommandType type) { |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 547 | uint8_t buf; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 548 | while (0 < TEMP_FAILURE_RETRY( |
| 549 | recv(connection->fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT))) { |
| 550 | status_t status = getAndExecuteCommand(connection, session, type); |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 551 | if (status != OK) return status; |
| 552 | } |
| 553 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 554 | } |
| 555 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 556 | status_t RpcState::processServerCommand(const sp<RpcSession::RpcConnection>& connection, |
| 557 | const sp<RpcSession>& session, const RpcWireHeader& command, |
| 558 | CommandType type) { |
Steven Moreland | d730207 | 2021-05-15 01:32:04 +0000 | [diff] [blame] | 559 | IPCThreadState* kernelBinderState = IPCThreadState::selfOrNull(); |
| 560 | IPCThreadState::SpGuard spGuard{ |
| 561 | .address = __builtin_frame_address(0), |
| 562 | .context = "processing binder RPC command", |
| 563 | }; |
| 564 | const IPCThreadState::SpGuard* origGuard; |
| 565 | if (kernelBinderState != nullptr) { |
| 566 | origGuard = kernelBinderState->pushGetCallingSpGuard(&spGuard); |
| 567 | } |
| 568 | ScopeGuard guardUnguard = [&]() { |
| 569 | if (kernelBinderState != nullptr) { |
| 570 | kernelBinderState->restoreGetCallingSpGuard(origGuard); |
| 571 | } |
| 572 | }; |
| 573 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 574 | switch (command.command) { |
| 575 | case RPC_COMMAND_TRANSACT: |
Steven Moreland | 52eee94 | 2021-06-03 00:59:28 +0000 | [diff] [blame] | 576 | if (type != CommandType::ANY) return BAD_TYPE; |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 577 | return processTransact(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 578 | case RPC_COMMAND_DEC_STRONG: |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 579 | return processDecStrong(connection, session, command); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 580 | } |
| 581 | |
| 582 | // We should always know the version of the opposing side, and since the |
| 583 | // RPC-binder-level wire protocol is not self synchronizing, we have no way |
| 584 | // to understand where the current command ends and the next one begins. We |
| 585 | // also can't consider it a fatal error because this would allow any client |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 586 | // to kill us, so ending the session for misbehaving client. |
| 587 | ALOGE("Unknown RPC command %d - terminating session", command.command); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 588 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 589 | return DEAD_OBJECT; |
| 590 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 591 | status_t RpcState::processTransact(const sp<RpcSession::RpcConnection>& connection, |
| 592 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 593 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_TRANSACT, "command: %d", command.command); |
| 594 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 595 | CommandData transactionData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 596 | if (!transactionData.valid()) { |
| 597 | return NO_MEMORY; |
| 598 | } |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 599 | if (status_t status = rpcRec(connection, session, "transaction body", transactionData.data(), |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 600 | transactionData.size()); |
| 601 | status != OK) |
| 602 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 603 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 604 | return processTransactInternal(connection, session, std::move(transactionData)); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Steven Moreland | 438cce8 | 2021-04-02 18:04:08 +0000 | [diff] [blame] | 607 | static void do_nothing_to_transact_data(Parcel* p, const uint8_t* data, size_t dataSize, |
| 608 | const binder_size_t* objects, size_t objectsCount) { |
| 609 | (void)p; |
| 610 | (void)data; |
| 611 | (void)dataSize; |
| 612 | (void)objects; |
| 613 | (void)objectsCount; |
| 614 | } |
| 615 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 616 | status_t RpcState::processTransactInternal(const sp<RpcSession::RpcConnection>& connection, |
| 617 | const sp<RpcSession>& session, |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 618 | CommandData transactionData) { |
| 619 | // for 'recursive' calls to this, we have already read and processed the |
| 620 | // binder from the transaction data and taken reference counts into account, |
| 621 | // so it is cached here. |
| 622 | sp<IBinder> targetRef; |
| 623 | processTransactInternalTailCall: |
| 624 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 625 | if (transactionData.size() < sizeof(RpcWireTransaction)) { |
| 626 | ALOGE("Expecting %zu but got %zu bytes for RpcWireTransaction. Terminating!", |
| 627 | sizeof(RpcWireTransaction), transactionData.size()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 628 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 629 | return BAD_VALUE; |
| 630 | } |
| 631 | RpcWireTransaction* transaction = reinterpret_cast<RpcWireTransaction*>(transactionData.data()); |
| 632 | |
| 633 | // TODO(b/182939933): heap allocation just for lookup in mNodeForAddress, |
| 634 | // maybe add an RpcAddress 'view' if the type remains 'heavy' |
| 635 | auto addr = RpcAddress::fromRawEmbedded(&transaction->address); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame^] | 636 | bool oneway = transaction->flags & IBinder::FLAG_ONEWAY; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 637 | |
| 638 | status_t replyStatus = OK; |
| 639 | sp<IBinder> target; |
| 640 | if (!addr.isZero()) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 641 | if (!targetRef) { |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 642 | replyStatus = onBinderEntering(session, addr, &target); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 643 | } else { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 644 | target = targetRef; |
| 645 | } |
| 646 | |
Steven Moreland | 7227c8a | 2021-06-02 00:24:32 +0000 | [diff] [blame] | 647 | if (replyStatus != OK) { |
| 648 | // do nothing |
| 649 | } else if (target == nullptr) { |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 650 | // This can happen if the binder is remote in this process, and |
| 651 | // another thread has called the last decStrong on this binder. |
| 652 | // However, for local binders, it indicates a misbehaving client |
| 653 | // (any binder which is being transacted on should be holding a |
| 654 | // strong ref count), so in either case, terminating the |
| 655 | // session. |
| 656 | ALOGE("While transacting, binder has been deleted at address %s. Terminating!", |
| 657 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 658 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 659 | replyStatus = BAD_VALUE; |
| 660 | } else if (target->localBinder() == nullptr) { |
| 661 | ALOGE("Unknown binder address or non-local binder, not address %s. Terminating!", |
| 662 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 663 | (void)session->shutdownAndWait(false); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 664 | replyStatus = BAD_VALUE; |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame^] | 665 | } else if (oneway) { |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 666 | std::unique_lock<std::mutex> _l(mNodeMutex); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 667 | auto it = mNodeForAddress.find(addr); |
| 668 | if (it->second.binder.promote() != target) { |
| 669 | ALOGE("Binder became invalid during transaction. Bad client? %s", |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 670 | addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 671 | replyStatus = BAD_VALUE; |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 672 | } else if (transaction->asyncNumber != it->second.asyncNumber) { |
| 673 | // we need to process some other asynchronous transaction |
| 674 | // first |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 675 | it->second.asyncTodo.push(BinderNode::AsyncTodo{ |
| 676 | .ref = target, |
| 677 | .data = std::move(transactionData), |
| 678 | .asyncNumber = transaction->asyncNumber, |
| 679 | }); |
Steven Moreland | d45be62 | 2021-06-04 02:19:37 +0000 | [diff] [blame] | 680 | |
| 681 | size_t numPending = it->second.asyncTodo.size(); |
| 682 | LOG_RPC_DETAIL("Enqueuing %" PRId64 " on %s (%zu pending)", |
| 683 | transaction->asyncNumber, addr.toString().c_str(), numPending); |
| 684 | |
| 685 | constexpr size_t kArbitraryOnewayCallTerminateLevel = 10000; |
| 686 | constexpr size_t kArbitraryOnewayCallWarnLevel = 1000; |
| 687 | constexpr size_t kArbitraryOnewayCallWarnPer = 1000; |
| 688 | |
| 689 | if (numPending >= kArbitraryOnewayCallWarnLevel) { |
| 690 | if (numPending >= kArbitraryOnewayCallTerminateLevel) { |
| 691 | ALOGE("WARNING: %zu pending oneway transactions. Terminating!", numPending); |
| 692 | _l.unlock(); |
| 693 | (void)session->shutdownAndWait(false); |
| 694 | return FAILED_TRANSACTION; |
| 695 | } |
| 696 | |
| 697 | if (numPending % kArbitraryOnewayCallWarnPer == 0) { |
| 698 | ALOGW("Warning: many oneway transactions built up on %p (%zu)", |
| 699 | target.get(), numPending); |
| 700 | } |
| 701 | } |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 702 | return OK; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 703 | } |
| 704 | } |
| 705 | } |
| 706 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 707 | Parcel reply; |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 708 | reply.markForRpc(session); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 709 | |
| 710 | if (replyStatus == OK) { |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 711 | Parcel data; |
| 712 | // transaction->data is owned by this function. Parcel borrows this data and |
| 713 | // only holds onto it for the duration of this function call. Parcel will be |
| 714 | // deleted before the 'transactionData' object. |
| 715 | data.ipcSetDataReference(transaction->data, |
| 716 | transactionData.size() - offsetof(RpcWireTransaction, data), |
| 717 | nullptr /*object*/, 0 /*objectCount*/, |
| 718 | do_nothing_to_transact_data); |
Steven Moreland | bdb53ab | 2021-05-05 17:57:41 +0000 | [diff] [blame] | 719 | data.markForRpc(session); |
Steven Moreland | eff77c1 | 2021-04-15 00:37:19 +0000 | [diff] [blame] | 720 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 721 | if (target) { |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame^] | 722 | bool origAllowNested = connection->allowNested; |
| 723 | connection->allowNested = !oneway; |
| 724 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 725 | replyStatus = target->transact(transaction->code, data, &reply, transaction->flags); |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame^] | 726 | |
| 727 | connection->allowNested = origAllowNested; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 728 | } else { |
| 729 | LOG_RPC_DETAIL("Got special transaction %u", transaction->code); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 730 | |
Steven Moreland | 103424e | 2021-06-02 18:16:19 +0000 | [diff] [blame] | 731 | switch (transaction->code) { |
| 732 | case RPC_SPECIAL_TRANSACT_GET_MAX_THREADS: { |
| 733 | replyStatus = reply.writeInt32(session->getMaxThreads()); |
| 734 | break; |
| 735 | } |
| 736 | case RPC_SPECIAL_TRANSACT_GET_SESSION_ID: { |
| 737 | // for client connections, this should always report the value |
| 738 | // originally returned from the server |
| 739 | int32_t id = session->mId.value(); |
| 740 | replyStatus = reply.writeInt32(id); |
| 741 | break; |
| 742 | } |
| 743 | default: { |
| 744 | sp<RpcServer> server = session->server().promote(); |
| 745 | if (server) { |
| 746 | switch (transaction->code) { |
| 747 | case RPC_SPECIAL_TRANSACT_GET_ROOT: { |
| 748 | replyStatus = reply.writeStrongBinder(server->getRootObject()); |
| 749 | break; |
| 750 | } |
| 751 | default: { |
| 752 | replyStatus = UNKNOWN_TRANSACTION; |
| 753 | } |
| 754 | } |
| 755 | } else { |
| 756 | ALOGE("Special command sent, but no server object attached."); |
Steven Moreland | f137de9 | 2021-04-24 01:54:26 +0000 | [diff] [blame] | 757 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 758 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
Steven Moreland | c7d4013 | 2021-06-10 03:42:11 +0000 | [diff] [blame^] | 763 | if (oneway) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 764 | if (replyStatus != OK) { |
| 765 | ALOGW("Oneway call failed with error: %d", replyStatus); |
| 766 | } |
| 767 | |
| 768 | LOG_RPC_DETAIL("Processed async transaction %" PRId64 " on %s", transaction->asyncNumber, |
| 769 | addr.toString().c_str()); |
| 770 | |
| 771 | // Check to see if there is another asynchronous transaction to process. |
| 772 | // This behavior differs from binder behavior, since in the binder |
| 773 | // driver, asynchronous transactions will be processed after existing |
| 774 | // pending binder transactions on the queue. The downside of this is |
| 775 | // that asynchronous transactions can be drowned out by synchronous |
| 776 | // transactions. However, we have no easy way to queue these |
| 777 | // transactions after the synchronous transactions we may want to read |
| 778 | // from the wire. So, in socket binder here, we have the opposite |
| 779 | // downside: asynchronous transactions may drown out synchronous |
| 780 | // transactions. |
| 781 | { |
| 782 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 783 | auto it = mNodeForAddress.find(addr); |
| 784 | // last refcount dropped after this transaction happened |
| 785 | if (it == mNodeForAddress.end()) return OK; |
| 786 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 787 | if (!nodeProgressAsyncNumber(&it->second)) { |
| 788 | _l.unlock(); |
| 789 | (void)session->shutdownAndWait(false); |
| 790 | return DEAD_OBJECT; |
| 791 | } |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 792 | |
| 793 | if (it->second.asyncTodo.size() == 0) return OK; |
| 794 | if (it->second.asyncTodo.top().asyncNumber == it->second.asyncNumber) { |
| 795 | LOG_RPC_DETAIL("Found next async transaction %" PRId64 " on %s", |
| 796 | it->second.asyncNumber, addr.toString().c_str()); |
| 797 | |
| 798 | // justification for const_cast (consider avoiding priority_queue): |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 799 | // - AsyncTodo operator< doesn't depend on 'data' or 'ref' objects |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 800 | // - gotta go fast |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 801 | auto& todo = const_cast<BinderNode::AsyncTodo&>(it->second.asyncTodo.top()); |
| 802 | |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 803 | // reset up arguments |
| 804 | transactionData = std::move(todo.data); |
| 805 | targetRef = std::move(todo.ref); |
Steven Moreland | f517427 | 2021-05-25 00:39:28 +0000 | [diff] [blame] | 806 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 807 | it->second.asyncTodo.pop(); |
Steven Moreland | ada72bd | 2021-06-09 23:29:13 +0000 | [diff] [blame] | 808 | goto processTransactInternalTailCall; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 809 | } |
| 810 | } |
| 811 | return OK; |
| 812 | } |
| 813 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 814 | LOG_ALWAYS_FATAL_IF(std::numeric_limits<int32_t>::max() - sizeof(RpcWireHeader) - |
| 815 | sizeof(RpcWireReply) < |
| 816 | reply.dataSize(), |
| 817 | "Too much data for reply %zu", reply.dataSize()); |
| 818 | |
| 819 | RpcWireHeader cmdReply{ |
| 820 | .command = RPC_COMMAND_REPLY, |
| 821 | .bodySize = static_cast<uint32_t>(sizeof(RpcWireReply) + reply.dataSize()), |
| 822 | }; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 823 | RpcWireReply rpcReply{ |
| 824 | .status = replyStatus, |
| 825 | }; |
| 826 | |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 827 | CommandData replyData(sizeof(RpcWireHeader) + sizeof(RpcWireReply) + reply.dataSize()); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 828 | if (!replyData.valid()) { |
| 829 | return NO_MEMORY; |
| 830 | } |
Steven Moreland | 77c3011 | 2021-06-02 20:45:46 +0000 | [diff] [blame] | 831 | memcpy(replyData.data() + 0, &cmdReply, sizeof(RpcWireHeader)); |
| 832 | memcpy(replyData.data() + sizeof(RpcWireHeader), &rpcReply, sizeof(RpcWireReply)); |
| 833 | memcpy(replyData.data() + sizeof(RpcWireHeader) + sizeof(RpcWireReply), reply.data(), |
| 834 | reply.dataSize()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 835 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 836 | return rpcSend(connection, session, "reply", replyData.data(), replyData.size()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 839 | status_t RpcState::processDecStrong(const sp<RpcSession::RpcConnection>& connection, |
| 840 | const sp<RpcSession>& session, const RpcWireHeader& command) { |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 841 | LOG_ALWAYS_FATAL_IF(command.command != RPC_COMMAND_DEC_STRONG, "command: %d", command.command); |
| 842 | |
Steven Moreland | dbe7183 | 2021-05-12 23:31:00 +0000 | [diff] [blame] | 843 | CommandData commandData(command.bodySize); |
Steven Moreland | e839334 | 2021-05-05 23:27:53 +0000 | [diff] [blame] | 844 | if (!commandData.valid()) { |
| 845 | return NO_MEMORY; |
| 846 | } |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 847 | if (status_t status = |
Steven Moreland | 5ae6256 | 2021-06-10 03:21:42 +0000 | [diff] [blame] | 848 | rpcRec(connection, session, "dec ref body", commandData.data(), commandData.size()); |
Steven Moreland | 1e4c2b8 | 2021-05-25 01:51:31 +0000 | [diff] [blame] | 849 | status != OK) |
| 850 | return status; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 851 | |
| 852 | if (command.bodySize < sizeof(RpcWireAddress)) { |
| 853 | ALOGE("Expecting %zu but got %" PRId32 " bytes for RpcWireAddress. Terminating!", |
| 854 | sizeof(RpcWireAddress), command.bodySize); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 855 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 856 | return BAD_VALUE; |
| 857 | } |
| 858 | RpcWireAddress* address = reinterpret_cast<RpcWireAddress*>(commandData.data()); |
| 859 | |
| 860 | // TODO(b/182939933): heap allocation just for lookup |
| 861 | auto addr = RpcAddress::fromRawEmbedded(address); |
| 862 | std::unique_lock<std::mutex> _l(mNodeMutex); |
| 863 | auto it = mNodeForAddress.find(addr); |
| 864 | if (it == mNodeForAddress.end()) { |
| 865 | ALOGE("Unknown binder address %s for dec strong.", addr.toString().c_str()); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 866 | return OK; |
| 867 | } |
| 868 | |
| 869 | sp<IBinder> target = it->second.binder.promote(); |
| 870 | if (target == nullptr) { |
| 871 | ALOGE("While requesting dec strong, binder has been deleted at address %s. Terminating!", |
| 872 | addr.toString().c_str()); |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 873 | _l.unlock(); |
| 874 | (void)session->shutdownAndWait(false); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 875 | return BAD_VALUE; |
| 876 | } |
| 877 | |
| 878 | if (it->second.timesSent == 0) { |
| 879 | ALOGE("No record of sending binder, but requested decStrong: %s", addr.toString().c_str()); |
| 880 | return OK; |
| 881 | } |
| 882 | |
| 883 | LOG_ALWAYS_FATAL_IF(it->second.sentRef == nullptr, "Inconsistent state, lost ref for %s", |
| 884 | addr.toString().c_str()); |
| 885 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 886 | it->second.timesSent--; |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 887 | sp<IBinder> tempHold = tryEraseNode(it); |
| 888 | _l.unlock(); |
| 889 | tempHold = nullptr; // destructor may make binder calls on this session |
| 890 | |
| 891 | return OK; |
| 892 | } |
| 893 | |
| 894 | sp<IBinder> RpcState::tryEraseNode(std::map<RpcAddress, BinderNode>::iterator& it) { |
| 895 | sp<IBinder> ref; |
| 896 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 897 | if (it->second.timesSent == 0) { |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 898 | ref = std::move(it->second.sentRef); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 899 | |
| 900 | if (it->second.timesRecd == 0) { |
Steven Moreland | a6e11cf | 2021-06-04 00:58:31 +0000 | [diff] [blame] | 901 | LOG_ALWAYS_FATAL_IF(!it->second.asyncTodo.empty(), |
| 902 | "Can't delete binder w/ pending async transactions"); |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 903 | mNodeForAddress.erase(it); |
| 904 | } |
| 905 | } |
| 906 | |
Steven Moreland | 31bde7a | 2021-06-04 00:57:36 +0000 | [diff] [blame] | 907 | return ref; |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 908 | } |
| 909 | |
Steven Moreland | c9d7b53 | 2021-06-04 20:57:41 +0000 | [diff] [blame] | 910 | bool RpcState::nodeProgressAsyncNumber(BinderNode* node) { |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 911 | // 2**64 =~ 10**19 =~ 1000 transactions per second for 585 million years to |
| 912 | // a single binder |
| 913 | if (node->asyncNumber >= std::numeric_limits<decltype(node->asyncNumber)>::max()) { |
| 914 | ALOGE("Out of async transaction IDs. Terminating"); |
Steven Moreland | 583a14a | 2021-06-04 02:04:58 +0000 | [diff] [blame] | 915 | return false; |
| 916 | } |
| 917 | node->asyncNumber++; |
| 918 | return true; |
| 919 | } |
| 920 | |
Steven Moreland | 5553ac4 | 2020-11-11 02:14:45 +0000 | [diff] [blame] | 921 | } // namespace android |