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