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