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