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