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