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