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