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