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